Skip to content

Commit 64f1be1

Browse files
committed
feat: add overlap logic
Signed-off-by: Michael Pollind <mpollind@gmail.com>
1 parent 4d7d32f commit 64f1be1

9 files changed

Lines changed: 88 additions & 18 deletions

File tree

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,23 @@
11
const std = @import("std");
2-
const vec = @import("../vector.zig");
2+
const vec = @import("vector.zig");
3+
4+
pub const AABB = @import("geometry/aabb.zig").AABB;
5+
pub const Plane = @import("geometry/plane.zig").Plane;
6+
pub const Sphere = @import("geometry/sphere.zig").Sphere;
7+
pub const OrientedBox = @import("geometry/oriented_box.zig").OrientedBox;
8+
pub const overlap = @import("geometry/overlap.zig");
9+
10+
test {
11+
@import("std").testing.refAllDeclsRecursive(@This());
12+
}
13+
14+
pub const Primative = enum {
15+
AABB,
16+
Plane,
17+
Sphere,
18+
OrientedBox,
19+
};
320

4-
pub const AABB = @import("aabb.zig").AABB;
5-
pub const Plane = @import("plane.zig").Plane;
6-
pub const Sphere = @import("sphere.zig").Sphere;
721

822
pub fn get_vector_from_buffer(comptime T: type, vertex_index: usize, buffer: []align(4) const u8, byte_offset: usize, byte_stride: usize) T {
923
const vector_len = switch(@typeInfo(T)) {
@@ -22,11 +36,6 @@ pub fn get_vector_from_buffer(comptime T: type, vertex_index: usize, buffer: []a
2236
return arr;
2337
}
2438

25-
test {
26-
@import("std").testing.refAllDeclsRecursive(@This());
27-
}
28-
29-
3039
test "vector_from_buffer - basic 2D f32 vector extraction" {
3140
// Create a buffer with 2D f32 vectors: [1.0, 2.0], [3.0, 4.0], [5.0, 6.0]
3241
const data = [_]f32{ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 };
@@ -202,3 +211,4 @@ test "vector_from_buffer - mixed data types with padding" {
202211
try std.testing.expectEqual(@as(f32, 0.5), norm2[1]);
203212
try std.testing.expectEqual(@as(f32, 0.6), norm2[2]);
204213
}
214+

src/geometry/aabb.zig

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const std = @import("std");
22
const vec = @import("../vector.zig");
3-
const math = std.math;
43

54
pub fn AABB(comptime T: type) type {
65
return struct {
@@ -36,8 +35,8 @@ pub fn AABB(comptime T: type) type {
3635
}
3736

3837
pub fn ray_intersection_with_inverse(self: Self, origin: @Vector(3, T), inv_direction: InvDirection) T {
39-
const flt_min: @Vector(3, T) = @as(@Vector(3, T), @splat(-math.floatMax(T)));
40-
const flt_max: @Vector(3, T) = @as(@Vector(3, T), @splat(math.floatMax(T)));
38+
const flt_min: @Vector(3, T) = @as(@Vector(3, T), @splat(-std.math.floatMax(T)));
39+
const flt_max: @Vector(3, T) = @as(@Vector(3, T), @splat(std.math.floatMax(T)));
4140

4241
// test against all three axes simultaneously
4342
const t1 = (self.min - origin) * inv_direction.inv_direction;

src/geometry/buffer_view.zig

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const std = @import("std");
2+
3+
pub fn BufferView(comptime T: type) type {
4+
return struct {
5+
pub const inner_type = switch (@typeInfo(T)) {
6+
.vector => |v| [v.len]std.meta.Child(T),
7+
.array => |a| [a.len]std.meta.Child(T),
8+
else => @compileError("Expected a vector or array type, got: " ++ @typeName(T)),
9+
};
10+
pub const num_elements = switch (@typeInfo(T)) {
11+
.vector => |v| v.len,
12+
.array => |a| a.len,
13+
else => @compileError("Expected a vector or array type, got: " ++ @typeName(T)),
14+
};
15+
};
16+
}

src/geometry/oriented_box.zig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const Mat = @import("../matrix.zig").Mat;
2+
const geometry = @import("../geometry.zig");
3+
4+
pub fn OrientedBox(comptime T: type) type {
5+
return struct {
6+
pub const primative_type = geometry.Primative.OrientedBox;
7+
orientation: Mat(4,4, T),
8+
half_extent: @Vector(3, T),
9+
};
10+
}
11+

src/geometry/overlap.zig

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const std = @import("std");
2+
const Mat = @import("../matrix.zig").Mat;
3+
const geometry = @import("../geometry.zig");
4+
const Sphere = @import("sphere.zig").Sphere;
5+
const vector = @import("../vector.zig");
6+
7+
pub fn overlap_sphere_sphere(a: anytype, b: anytype) bool {
8+
if (@TypeOf(a).primative_type != .Sphere) @compileError("Expected Sphere" ++ @typeName(@TypeOf(a)));
9+
if (@TypeOf(b).primative_type != .Sphere) @compileError("Expected Sphere" ++ @typeName(@TypeOf(b)));
10+
if (@TypeOf(a).inner_type != @TypeOf(b).inner_type) @compileError("Expected same type: " ++ @typeName(@TypeOf(a).inner_type) ++ " " ++ @typeName(@TypeOf(b).inner_type));
11+
return vector.norm_sqr(a.center - b.center) <= (a.radius + b.radius) * (a.radius + b.radius);
12+
}
13+
14+
pub inline fn overlap(a: anytype, b: anytype) bool {
15+
const a_primative: geometry.Primative = @TypeOf(a).primative_type;
16+
const b_primative: geometry.Primative = @TypeOf(b).primative_type;
17+
if (a_primative == .Sphere and b_primative == .Sphere) {
18+
return overlap_sphere_sphere(a, b);
19+
}
20+
@compileError("Unsupported primative overlap: " ++ @typeName(a_primative) ++ " " ++ @typeName(b_primative));
21+
}
22+
23+
test overlap_sphere_sphere {
24+
const s1: Sphere(f32) = .from_center_radius(.{ 0, 0, 0 }, 1);
25+
const s2: Sphere(f32) = .from_center_radius(.{ 0, 0, 1.5 }, 1);
26+
const s3: Sphere(f32) = .from_center_radius(.{ 0, 0, 3 }, 1);
27+
try std.testing.expect(overlap_sphere_sphere(s1, s2));
28+
try std.testing.expect(!overlap_sphere_sphere(s1, s3));
29+
// Symmetric
30+
try std.testing.expect(overlap(s1, s2));
31+
try std.testing.expect(overlap(s2, s1));
32+
}

src/geometry/plane.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pub fn Plane(comptime T: type) type {
66
return struct {
77
normal: @Vector(3, T), // normal vector
88
c: T, // constant
9-
9+
1010
const Self = @This();
1111

1212
pub fn normal_and_constant(self: Self) @Vector(4, T) {

src/geometry/sphere.zig

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
const std = @import("std");
2-
const vector = @import("../vector.zig");
31
const meta = @import("../meta.zig");
4-
2+
const zla = @import("../root.zig");
3+
const geometry = zla.geom;
54

65
pub fn Sphere(comptime T: type) type {
76
return struct {
7+
pub const inner_type: type = T;
8+
pub const primative_type: geometry.Primative = .Sphere;
9+
810
center: @Vector(3, T),
911
radius: T,
1012

11-
pub fn from_center_radius(center: anytype, radius: T) Sphere(T) {
13+
pub fn from_center_radius(center: @Vector(3, T), radius: T) Sphere(T) {
1214
return .{
1315
.center = meta.map_to_vector(center),
1416
.radius = radius,

src/root.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const Vec2f64 = vec.Vec2f64;
1313
const Quat4f32 = quat.Quat4f32;
1414
const Quat4f64 = quat.Quat4f64;
1515

16-
pub const geom = @import("geometry/geom.zig");
16+
pub const geom = @import("geometry.zig");
1717

1818
test {
1919
@import("std").testing.refAllDeclsRecursive(@This());

0 commit comments

Comments
 (0)