From c1abc7a06f35266aff1e00d8668aba87e7815499 Mon Sep 17 00:00:00 2001 From: Om Prakaash Date: Mon, 16 Oct 2023 19:35:45 -0700 Subject: [PATCH] math: add fromInt constructor helper for vectors (#1061) --- src/math/main.zig | 9 +++++++++ src/math/vec.zig | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/src/math/main.zig b/src/math/main.zig index 6f6667dd..8c7d1507 100644 --- a/src/math/main.zig +++ b/src/math/main.zig @@ -77,6 +77,9 @@ pub const Rayd = ray.Ray(Vec3d); pub const vec2 = Vec2.init; pub const vec3 = Vec3.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 mat3x3 = Mat3x3.init; pub const mat4x4 = Mat4x4.init; @@ -85,6 +88,9 @@ pub const mat4x4 = Mat4x4.init; pub const vec2h = Vec2h.init; pub const vec3h = Vec3h.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 mat3x3h = Mat3x3h.init; pub const mat4x4h = Mat4x4h.init; @@ -93,6 +99,9 @@ pub const mat4x4h = Mat4x4h.init; pub const vec2d = Vec2d.init; pub const vec3d = Vec3d.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 mat3x3d = Mat3x3d.init; pub const mat4x4d = Mat4x4d.init; diff --git a/src/math/vec.zig b/src/math/vec.zig index 4de2e120..482a825d 100644 --- a/src/math/vec.zig +++ b/src/math/vec.zig @@ -27,6 +27,9 @@ pub fn Vec(comptime n_value: usize, comptime Scalar: type) type { pub inline fn init(xs: Scalar, ys: Scalar) VecN { 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 { 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 { 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 { 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 { 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 { return v.v[0]; } @@ -903,3 +912,40 @@ test "Mat4x4_mulMat" { const expected = math.vec4(7, 9, 6, -1); 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); +} \ No newline at end of file