math: add fromInt constructor helper for vectors (#1061)

This commit is contained in:
Om Prakaash 2023-10-16 19:35:45 -07:00 committed by GitHub
parent fde4f820d7
commit c1abc7a06f
Failed to generate hash of commit
2 changed files with 55 additions and 0 deletions

View file

@ -77,6 +77,9 @@ pub const Rayd = ray.Ray(Vec3d);
pub const vec2 = Vec2.init; pub const vec2 = Vec2.init;
pub const vec3 = Vec3.init; pub const vec3 = Vec3.init;
pub const vec4 = Vec4.init; pub const vec4 = Vec4.init;
pub const vec2FromInt = Vec2.fromInt;
pub const vec3FromInt = Vec3.fromInt;
pub const vec4FromInt = Vec4.fromInt;
pub const quat = Quat.init; pub const quat = Quat.init;
pub const mat3x3 = Mat3x3.init; pub const mat3x3 = Mat3x3.init;
pub const mat4x4 = Mat4x4.init; pub const mat4x4 = Mat4x4.init;
@ -85,6 +88,9 @@ pub const mat4x4 = Mat4x4.init;
pub const vec2h = Vec2h.init; pub const vec2h = Vec2h.init;
pub const vec3h = Vec3h.init; pub const vec3h = Vec3h.init;
pub const vec4h = Vec4h.init; pub const vec4h = Vec4h.init;
pub const vec2hFromInt = Vec2h.fromInt;
pub const vec3hFromInt = Vec3h.fromInt;
pub const vec4hFromInt = Vec4h.fromInt;
pub const quath = Quath.init; pub const quath = Quath.init;
pub const mat3x3h = Mat3x3h.init; pub const mat3x3h = Mat3x3h.init;
pub const mat4x4h = Mat4x4h.init; pub const mat4x4h = Mat4x4h.init;
@ -93,6 +99,9 @@ pub const mat4x4h = Mat4x4h.init;
pub const vec2d = Vec2d.init; pub const vec2d = Vec2d.init;
pub const vec3d = Vec3d.init; pub const vec3d = Vec3d.init;
pub const vec4d = Vec4d.init; pub const vec4d = Vec4d.init;
pub const vec2dFromInt = Vec2d.fromInt;
pub const vec3dFromInt = Vec3d.fromInt;
pub const vec4dFromInt = Vec4d.fromInt;
pub const quatd = Quatd.init; pub const quatd = Quatd.init;
pub const mat3x3d = Mat3x3d.init; pub const mat3x3d = Mat3x3d.init;
pub const mat4x4d = Mat4x4d.init; pub const mat4x4d = Mat4x4d.init;

View file

@ -27,6 +27,9 @@ pub fn Vec(comptime n_value: usize, comptime Scalar: type) type {
pub inline fn init(xs: Scalar, ys: Scalar) VecN { pub inline fn init(xs: Scalar, ys: Scalar) VecN {
return .{ .v = .{ xs, ys } }; return .{ .v = .{ xs, ys } };
} }
pub inline fn fromInt(xs: anytype, ys: anytype) VecN {
return .{ .v = .{ @floatFromInt(xs), @floatFromInt(ys) } };
}
pub inline fn x(v: *const VecN) Scalar { pub inline fn x(v: *const VecN) Scalar {
return v.v[0]; return v.v[0];
} }
@ -38,6 +41,9 @@ pub fn Vec(comptime n_value: usize, comptime Scalar: type) type {
pub inline fn init(xs: Scalar, ys: Scalar, zs: Scalar) VecN { pub inline fn init(xs: Scalar, ys: Scalar, zs: Scalar) VecN {
return .{ .v = .{ xs, ys, zs } }; return .{ .v = .{ xs, ys, zs } };
} }
pub inline fn fromInt(xs: anytype, ys: anytype, zs: anytype) VecN {
return .{ .v = .{ @floatFromInt(xs), @floatFromInt(ys), @floatFromInt(zs) } };
}
pub inline fn x(v: *const VecN) Scalar { pub inline fn x(v: *const VecN) Scalar {
return v.v[0]; return v.v[0];
} }
@ -87,6 +93,9 @@ pub fn Vec(comptime n_value: usize, comptime Scalar: type) type {
pub inline fn init(xs: Scalar, ys: Scalar, zs: Scalar, ws: Scalar) VecN { pub inline fn init(xs: Scalar, ys: Scalar, zs: Scalar, ws: Scalar) VecN {
return .{ .v = .{ xs, ys, zs, ws } }; return .{ .v = .{ xs, ys, zs, ws } };
} }
pub inline fn fromInt(xs: anytype, ys: anytype, zs: anytype, ws: anytype) VecN {
return .{ .v = .{ @floatFromInt(xs), @floatFromInt(ys), @floatFromInt(zs), @floatFromInt(ws) } };
}
pub inline fn x(v: *const VecN) Scalar { pub inline fn x(v: *const VecN) Scalar {
return v.v[0]; return v.v[0];
} }
@ -903,3 +912,40 @@ test "Mat4x4_mulMat" {
const expected = math.vec4(7, 9, 6, -1); const expected = math.vec4(7, 9, 6, -1);
try testing.expect(math.Vec4, expected).eql(m); try testing.expect(math.Vec4, expected).eql(m);
} }
test "Vec2_fromInt" {
const x: i8 = 0;
const y: i32 = 1;
const v = math.vec2FromInt(x, y);
const expected = math.vec2(0, 1);
try testing.expect(math.Vec2, expected).eql(v);
}
test "Vec3_fromInt" {
const x: i8 = 0;
const y: i32 = 1;
const z: i64 = 2;
const v = math.vec3FromInt(x, y, z);
const expected = math.vec3(0, 1, 2);
try testing.expect(math.Vec3, expected).eql(v);
}
test "Vec4_fromInt" {
const x: i8 = 0;
const y: i32 = 1;
const z: i64 = 2;
const w: i128 = 3;
const v = math.vec4FromInt(x, y, z, w);
const expected = math.vec4(0, 1, 2, 3);
try testing.expect(math.Vec4, expected).eql(v);
}
test "Vec4d_fromInt" {
const x: i8 = 0;
const y: i32 = 1;
const z: i64 = 2;
const w: i128 = 3;
const v = math.vec4dFromInt(x, y, z, w);
const expected = math.vec4d(0, 1, 2, 3);
try testing.expect(math.Vec4d, expected).eql(v);
}