summary refs log tree commit diff
path: root/src/geom.zig
blob: 80cf1dbc19be2567b10df6bb2f279c983051e430 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Geometry functions
// Copyright (C) 2002  David Rosen
// Copyright (C) 2023  Nguyễn Gia Phong
//
// This file is part of Black Shades.
//
// Black Shades is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Black Shades is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// 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");

fn sqr(x: anytype) @TypeOf(x) {
    return x * x;
}

fn dot(u: anytype, v: @TypeOf(u)) Child(@TypeOf(u)) {
    return @reduce(.Add, u * v);
}

export fn sqrlen(v: XYZ) f32 {
    const u: @Vector(3, f32) = @bitCast(v);
    return dot(u, u);
}

fn norm(v: anytype) Child(@TypeOf(v)) {
    return @sqrt(dot(v, v));
}

export fn len(v: XYZ) f32 {
    const u: @Vector(3, f32) = @bitCast(v);
    return norm(u);
}

const XYZ = extern struct { x: f32, y: f32, z: f32 };

export fn crossProduct(u: XYZ, v: XYZ) XYZ {
    return .{
        .x = u.y * v.z - u.z * v.y,
        .y = u.z * v.x - u.x * v.z,
        .z = u.x * v.y - u.y * v.x,
    };
}

inline fn splat(comptime n: comptime_int, x: anytype) @Vector(n, @TypeOf(x)) {
    return @splat(x);
}

export fn normalize(v: XYZ) XYZ {
    const u: @Vector(3, f32) = @bitCast(v);
    const d = norm(u);
    return if (d == 0) v else @bitCast(u / splat(3, d));
}

export fn reflect(v: XYZ, n: XYZ) XYZ {
    const u: @Vector(3, f32) = @bitCast(v);
    const m: @Vector(3, f32) = @bitCast(n);
    return @bitCast(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;
    // TODO: optimize
    rotate2d(&u.x, &u.y, degreesToRadians(f32, deg_z));
    rotate2d(&u.z, &u.x, degreesToRadians(f32, deg_y));
    rotate2d(&u.y, &u.z, degreesToRadians(f32, deg_x));
    return u;
}

export fn segmentIntersectsSphere(a: XYZ, b: XYZ, i: XYZ, r: f32) bool {
    // FIXME: call directly with vectors
    const p: @Vector(3, f32) = @bitCast(a);
    const q: @Vector(3, f32) = @bitCast(b);
    const c: @Vector(3, f32) = @bitCast(i);

    if (@reduce(.Or, @max(p, q) < c - splat(3, r))) return false;
    if (@reduce(.Or, @min(p, q) > c + splat(3, r))) return false;
    // https://en.wikipedia.org/wiki/Line–sphere_intersection
    const d = q - p; // line's direction
    const u = d / splat(3, norm(d)); // unit vector
    return sqr(dot(u, (p - c))) >= @reduce(.Add, sqr(p - c)) - sqr(r);
}

fn transpose(comptime n: comptime_int, m: [n]@Vector(n, f32)) @TypeOf(m) {
    const flat: @Vector(sqr(n), f32) = @bitCast(m);
    return @bitCast(@shuffle(f32, flat, undefined, blk: {
        var v: @Vector(sqr(n), i32) = undefined;
        for (0..n) |i| {
            for (0..n) |j|
                v[i * n + j] = @intCast(j * n + i);
        }
        break :blk v;
    }));
}

export fn setFrustum(frustum: *[6]@Vector(4, f32),
                     p: *const [4]@Vector(4, f32),
                     mv: *const [4]@Vector(4, f32)) void {
    var mvp: [4]@Vector(4, f32) = undefined;
    for (&mvp, transpose(4, p.*)) |*u, p_col| {
        for (mv, 0..4) |mv_row, i|
            u[i] = dot(p_col, mv_row);
    } // matrix multiplication

    frustum.* = .{
        mvp[3] - mvp[0], mvp[3] + mvp[0], // right & left planes
        mvp[3] - mvp[1], mvp[3] + mvp[1], // bottom & top planes
        mvp[3] - mvp[2], mvp[3] + mvp[2], // far & near planes
    };
    for (frustum) |*plane| // normalize
        plane.* /= @splat(norm(plane.* * @Vector(4, f32){ 1, 1, 1, 0 }));
}

export fn cubeInFrustum(frustum: *const [6]@Vector(4, f32),
                        x: f32, y: f32, z: f32, size: f32) bool {
    const delta = [_]f32{ -size, size };
    loop: for (frustum) |*plane| {
        for (delta) |dx| for (delta) |dy| for (delta) |dz|
            if (dot(plane.*, @Vector(4, f32){ x + dx, y + dy, z + dz, 1 }) > 0)
                continue :loop;
        return false;
    }
    return true;
}

export fn sphereInFrustum(frustum: *const [6]@Vector(4, f32),
                          x: f32, y: f32, z: f32, r: f32) bool {
    for (frustum) |*plane|
        if (dot(plane.*, @Vector(4, f32){ x, y, z, 1 }) <= -r)
            return false;
    return true;
}