pub fn GenericVector(comptime dimensions: comptime_int, comptime T: type) type { if (@typeInfo(T) != .Float and @typeInfo(T) != .Int) { @compileError("Vectors not implemented for " ++ @typeName(T)); } if (dimensions < 2 or dimensions > 4) { @compileError("Dimensions must be 2, 3 or 4!"); } return extern struct { const Self = @This(); const Data = @Vector(dimensions, T); data: Data, pub const Component = switch (dimensions) { 2 => enum { x, y }, 3 => enum { x, y, z }, 4 => enum { x, y, z, w }, else => unreachable, }; pub const init = switch (dimensions) { 2 => init2, 3 => init3, 4 => init4, else => unreachable, }; fn init2(x: T, y: T) Self { return .{ .data = .{ x, y } }; } fn init3(x: T, y: T, z: T) Self { return .{ .data = .{ x, y, z } }; } fn init4(x: T, y: T, z: T, w: T) Self { return .{ .data = .{ x, y, z, w } }; } pub const toVec2 = switch (dimension) { 3 => v3To2, 4 => v4To2, else => @compileError("unsupported"), }; pub const toVec3 = switch (dimension) { 3 => v2To3, 4 => v4To3, else => @compileerror("unsupported"), }; pub const toVec4 = switch (dimension) { 2 => v2To4, 3 => v3To4, else => @compileError("unsupported"), }; pub const fromVec2 = switch (dimension) { 3 => v2To3, 4 => v2To4, else => @compileError("unsupported"), }; pub const fromVec3 = switch (dimension) { 3 => v3To2, 4 => v3To4, else => @compileerror("unsupported"), }; pub const fromVec4 = switch (dimension) { 2 => v4To2, 3 => v4To3, else => @compileError("unsupported"), }; // zig fmt: off const V2 = GenericVector(2, T); const V3 = GenericVector(3, T); const V4 = GenericVector(4, T); fn v2To3(v: V2, z: T ) V3 { return V3.init(v.x(), v.y(), z); } fn v2To4(v: V2, z: T, w: T) V4 { return V4.init(v.x(), v.y(), z, w); } fn v3To2(v: V3 ) V2 { return V2.init(v.x(), v.y()); } fn v3To4(v: V3, w: T ) V4 { return V4.init(v.x(), v.y(), v.z(), w); } fn v4To2(v: V4 ) V2 { return V2.init(v.x(), v.y()); } fn v4To3(v: V4 ) V3 { return V3.init(v.x(), v.y(), v.z()); } // zig fmt: om pub fn x(self: Self) T { return self.data[0]; } pub fn y(self: Self) T { return self.data[1]; } pub fn z(self: Self) T { return self.data[2]; } pub fn w(self: Self) T { return self.data[3]; } pub fn xMut(self: *Self) *T { return &self.data[0]; } pub fn yMut(self: *Self) *T { return &self.data[1]; } pub fn zMut(self: *Self) *T { return &self.data[2]; } pub fn wMut(self: *Self) *T { return &self.data[3]; } pub const forward = switch (dimensions) { 3 => Self.init(0, 0, 1), 4 => Self.init(0, 0, 1, 0), else => @compileError("forward only supported for dimensions 3 and 4"), }; pub const back = forward.negate(); /// Rotate a 2D vector by an angle (in degrees). pub fn rotate(vec: Self, angle_in_degrees: T) Self { if (dimensions != 2) @compileError("rotate only supported for dimension 2"); const sin_theta = @sin(root.toRadians(angle_in_degrees)); const cos_theta = @cos(root.toRadians(angle_in_degrees)); return .{ .data = .{ cos_theta * self.x() - sin_theta * self.y(), sin_theta * self.x() + cos_theta * self.y(), } }; } /// Set all components to the same given value. pub fn set(val: T) Self { const result: Data = @splat(val); return .{ .data = result }; } /// Shorthand for (0..). pub fn zero() Self { return set(0); } /// Shorthand for (1..). pub fn one() Self { return set(1); } /// Shorthand for (0, 1). pub fn up() Self { return switch (dimensions) { 2 => Self.new(0, 1), 3 => Self.new(0, 1, 0), 4 => Self.new(0, 1, 0, 0), else => unreachable, }; } /// Shorthand for (0, -1). pub fn down() Self { return up().negate(); } /// Shorthand for (1, 0). pub fn right() Self { return switch (dimensions) { 2 => Self.new(1, 0), 3 => Self.new(1, 0, 0), 4 => Self.new(1, 0, 0, 0), else => unreachable, }; } /// Shorthand for (-1, 0). pub fn left() Self { return right().negate(); } /// Negate the given vector. pub fn negate(self: Self) Self { return self.scale(-1); } /// Cast a type to another type. /// It's like builtins: @intCast, @floatCast, @floatFromInt, @intFromFloat. pub fn cast(self: Self, comptime dest_type: type) GenericVector(dimensions, dest_type) { const dest_info = @typeInfo(dest_type); if (dest_info != .Float and dest_info != .Int) { panic("Error, dest type should be integer or float.\n", .{}); } var result: [dimensions]dest_type = undefined; for (result, 0..) |_, i| { result[i] = math.lossyCast(dest_type, self.data[i]); } return .{ .data = result }; } /// Construct new vector from slice. pub fn fromSlice(slice: []const T) Self { const result = slice[0..dimensions].*; return .{ .data = result }; } /// Transform vector to array. pub fn toArray(self: Self) [dimensions]T { return self.data; } /// Return the angle (in degrees) between two vectors. pub fn getAngle(first_vector: Self, second_vector: Self) T { const dot_product = dot(norm(first_vector), norm(second_vector)); return root.toDegrees(math.acos(dot_product)); } /// Return the length (magnitude) of given vector. /// √[x^2 + y^2 + z^2 ...] pub fn length(self: Self) T { return @sqrt(self.dot(self)); } /// Return the distance between two points. /// √[(x1 - x2)^2 + (y1 - y2)^2 + (z1 - z2)^2 ...] pub fn distance(first_vector: Self, second_vector: Self) T { return length(first_vector.sub(second_vector)); } /// Construct new normalized vector from a given one. pub fn norm(self: Self) Self { const l = self.length(); if (l == 0) { return self; } const result = self.data / @as(Data, @splat(l)); return .{ .data = result }; } /// Return true if two vectors are equals. pub fn eql(first_vector: Self, second_vector: Self) bool { return @reduce(.And, first_vector.data == second_vector.data); } /// Substraction between two given vector. pub fn sub(first_vector: Self, second_vector: Self) Self { const result = first_vector.data - second_vector.data; return .{ .data = result }; } /// Addition betwen two given vector. pub fn add(first_vector: Self, second_vector: Self) Self { const result = first_vector.data + second_vector.data; return .{ .data = result }; } /// Component wise multiplication betwen two given vector. pub fn mul(first_vector: Self, second_vector: Self) Self { const result = first_vector.data * second_vector.data; return .{ .data = result }; } /// Construct vector from the max components in two vectors pub fn max(first_vector: Self, second_vector: Self) Self { const result = @max(first_vector.data, second_vector.data); return .{ .data = result }; } /// Construct vector from the min components in two vectors pub fn min(first_vector: Self, second_vector: Self) Self { const result = @min(first_vector.data, second_vector.data); return .{ .data = result }; } /// Construct new vector after multiplying each components by a given scalar pub fn scale(self: Self, scalar: T) Self { const result = self.data * @as(Data, @splat(scalar)); return .{ .data = result }; } /// Return the dot product between two given vector. /// (x1 * x2) + (y1 * y2) + (z1 * z2) ... pub fn dot(first_vector: Self, second_vector: Self) T { return @reduce(.Add, first_vector.data * second_vector.data); } /// Linear interpolation between two vectors pub fn lerp(first_vector: Self, second_vector: Self, t: T) Self { const from = first_vector.data; const to = second_vector.data; const result = from + (to - from) * @as(Data, @splat(t)); return .{ .data = result }; } pub fn swizzle2(self: Self, comptime vx: Component, comptime vy: Component) GenericVector(2, T) { return GenericVector(2, T).new(self.data[@intFromEnum(vx)], self.data[@intFromEnum(vy)]); } pub fn swizzle3(self: Self, comptime vx: Component, comptime vy: Component, comptime vz: Component) GenericVector(3, T) { return GenericVector(3, T).new(self.data[@intFromEnum(vx)], self.data[@intFromEnum(vy)], self.data[@intFromEnum(vz)]); } pub fn swizzle4(self: Self, comptime vx: Component, comptime vy: Component, comptime vz: Component, comptime vw: Component) GenericVector(4, T) { return GenericVector(4, T).new(self.data[@intFromEnum(vx)], self.data[@intFromEnum(vy)], self.data[@intFromEnum(vz)], self.data[@intFromEnum(vw)]); } }; }