Skip to content

Commit fb2c4ec

Browse files
committed
Plane function to transform by a Mat4
1 parent 93e759a commit fb2c4ec

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

src/framework/spatial/plane.zig

+36
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,20 @@ pub const Plane = struct {
139139

140140
return ret;
141141
}
142+
143+
pub fn mulMat4(self: *const Plane, transform: math.Mat4) Plane {
144+
const origin = self.normal.scale(self.d);
145+
const along = origin.add(self.normal);
146+
147+
// get a transformed origin and point down the plane normal direction
148+
const origin_trans = origin.mulMat4(transform);
149+
const along_trans = along.mulMat4(transform);
150+
151+
// make the new normal based on the new direction
152+
const normal = along_trans.sub(origin_trans).norm();
153+
154+
return Plane.init(normal, origin_trans.scale(-1));
155+
}
142156
};
143157

144158
test "Plane.distanceToPoint" {
@@ -199,3 +213,25 @@ test "Plane.planeIntersectPoint" {
199213
assert(intersect.y == 4);
200214
assert(intersect.z == 5);
201215
}
216+
217+
test "Plane.mulMat4" {
218+
const plane = Plane.init(Vec3.new(0, 0, 1), Vec3.new(1, 1, 1));
219+
220+
// multiplying by the identity should result in an identical plane
221+
const t1 = plane.mulMat4(math.Mat4.identity);
222+
assert(std.meta.eql(plane, t1));
223+
224+
// scaling should keep the normal, but increase the distance
225+
const t2 = plane.mulMat4(math.Mat4.scale(Vec3.new(2, 2, 2)));
226+
assert(std.meta.eql(plane.normal, t2.normal));
227+
assert(t2.d == plane.d * 2);
228+
229+
// translating should also just change the distance
230+
const t3 = plane.mulMat4(math.Mat4.translate(Vec3.new(1, 1, 1)));
231+
assert(std.meta.eql(plane.normal, t3.normal));
232+
assert(t3.d == 0);
233+
234+
// rotating should just change the normal
235+
const t4 = plane.mulMat4(math.Mat4.rotate(45, Vec3.new(1, 1, 1)));
236+
assert(t4.d == plane.d);
237+
}

0 commit comments

Comments
 (0)