diff options
Diffstat (limited to 'src/geom.zig')
-rw-r--r-- | src/geom.zig | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/src/geom.zig b/src/geom.zig index 910f355..96add58 100644 --- a/src/geom.zig +++ b/src/geom.zig @@ -18,6 +18,7 @@ // along with Black Shades. If not, see <https://www.gnu.org/licenses/>. const Child = std.meta.Child; +const degreesToRadians = std.math.degreesToRadians; const std = @import("std"); const c = @import("cimport.zig"); @@ -39,6 +40,11 @@ fn norm(v: anytype) Child(@TypeOf(v)) { return @sqrt(dot(v, v)); } +export fn len(v: XYZ) f32 { + const u = @bitCast(@Vector(3, f32), v); + return norm(u); +} + const XYZ = extern struct { x: f32, y: f32, z: f32 }; export fn crossProduct(u: XYZ, v: XYZ) XYZ { @@ -61,6 +67,22 @@ export fn reflect(v: XYZ, n: XYZ) XYZ { return @bitCast(XYZ, u - m * @splat(3, dot(u, m) * 2)); } +fn rotate2d(i: *f32, j: *f32, a: f32) void { + if (a == 0) return; + const x = i.*; + const y = j.*; + i.* = x * @cos(a) - y * @sin(a); + j.* = x * @sin(a) + y * @cos(a); +} + +export fn rotate(v: XYZ, deg_x: f32, deg_y: f32, deg_z: f32) XYZ { + var u = v; + rotate2d(&u.z, &u.x, degreesToRadians(f32, deg_y)); + rotate2d(&u.x, &u.y, degreesToRadians(f32, deg_z)); + rotate2d(&u.y, &u.z, degreesToRadians(f32, deg_x)); + return u; +} + export fn segmentIntersectsSphere(a: XYZ, b: XYZ, j: XYZ, r: f32) bool { // FIXME: call directly with vectors const p = @bitCast(@Vector(3, f32), a); |