Add Mesh translate function

This commit is contained in:
Alex Selimov 2026-09-21 09:37:38 -04:00
parent 8ecc49ac79
commit 600753dc0b

View file

@ -110,6 +110,13 @@ pub fn rotateMeshAroundPoint(mesh: *Mesh, p: vec3.Vec3F64, q: vec3.QuatF64) void
return;
}
pub fn translateMesh(mesh: *Mesh, p: vec3.Vec3F64) void {
for (mesh.vertices.items) |*vertex| {
vertex.* = vec3.vec3Add(vertex.*, p);
}
return;
}
test "Verify fixed point conversion works" {
const vec = vec3.Vec3F64.init(1.0, 2.134, 1010.11);
const fixed_point = vecToFixedPoint(vec, 2);
@ -175,9 +182,7 @@ test "Test binary stl is parsed correctly" {
));
}
test "mesh is rotated correctly" {
const allocator = std.testing.allocator;
fn createDummyMesh(allocator: std.mem.Allocator) !Mesh {
var vertices: std.ArrayList(vec3.Vec3F64) = .empty;
try vertices.appendSlice(allocator, &.{
vec3.Vec3F64.init(0, 0, 0),
@ -193,11 +198,16 @@ test "mesh is rotated correctly" {
var tri_indices: std.ArrayList([3]usize) = .empty;
try tri_indices.appendSlice(allocator, &.{.{ 0, 1, 2 }});
var mesh: Mesh = .{
return .{
.vertices = vertices,
.triangle_normals = triangle_normals,
.triangle_vertices = tri_indices,
};
}
test "mesh is rotated correctly" {
const allocator = std.testing.allocator;
var mesh = try createDummyMesh(allocator);
defer mesh.deinit(allocator);
var orig_mesh: Mesh = .{
@ -229,3 +239,28 @@ test "mesh is rotated correctly" {
1e-12,
));
}
test "mesh is translated correctly" {
const allocator = std.testing.allocator;
var mesh = try createDummyMesh(allocator);
defer mesh.deinit(allocator);
translateMesh(&mesh, vec3.Vec3F64.init(1, 1, 1));
try std.testing.expect(vec3.vec3ApproxEq(
mesh.vertices.items[0],
vec3.Vec3F64.init(1, 1, 1),
1e-12,
));
try std.testing.expect(vec3.vec3ApproxEq(
mesh.vertices.items[1],
vec3.Vec3F64.init(1.5, 1 + std.math.sqrt(3.0) / 2.0, 1),
1e-12,
));
try std.testing.expect(vec3.vec3ApproxEq(
mesh.vertices.items[2],
vec3.Vec3F64.init(2, 1, 1),
1e-12,
));
}