Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Mirror transformation method to Manifold #354

Merged
merged 4 commits into from
Mar 8, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions bindings/c/include/manifoldc.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ ManifoldManifold *manifold_transform(void *mem, ManifoldManifold *m, float x1,
float y1, float z1, float x2, float y2,
float z2, float x3, float y3, float z3,
float x4, float y4, float z4);
ManifoldManifold *manifold_mirror(void *mem, ManifoldManifold *m, float nx,
float ny, float nz);
ManifoldManifold *manifold_warp(void *mem, ManifoldManifold *m,
ManifoldVec3 (*fun)(float, float, float));
ManifoldManifold *manifold_refine(void *mem, ManifoldManifold *m, int refine);
Expand Down
6 changes: 6 additions & 0 deletions bindings/c/manifoldc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@ ManifoldManifold *manifold_transform(void *mem, ManifoldManifold *m, float x1,
return to_c(new (mem) Manifold(transformed));
}

ManifoldManifold *manifold_mirror(void *mem, ManifoldManifold *m, float nx,
float ny, float nz) {
auto mirrored = from_c(m)->Mirror({nx, ny, nz});
return to_c(new (mem) Manifold(mirrored));
}

ManifoldManifold *manifold_warp(void *mem, ManifoldManifold *m,
ManifoldVec3 (*fun)(float, float, float)) {
std::function<void(glm::vec3 & v)> warp = [fun](glm::vec3 &v) {
Expand Down
1 change: 1 addition & 0 deletions src/manifold/include/manifold.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ class Manifold {
Manifold Rotate(float xDegrees, float yDegrees = 0.0f,
float zDegrees = 0.0f) const;
Manifold Transform(const glm::mat4x3&) const;
Manifold Mirror(glm::vec3) const;
Manifold Warp(std::function<void(glm::vec3&)>) const;
Manifold Refine(int) const;
// Manifold RefineToLength(float);
Expand Down
24 changes: 24 additions & 0 deletions src/manifold/src/manifold.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,30 @@ Manifold Manifold::Transform(const glm::mat4x3& m) const {
return Manifold(pNode_->Transform(m));
}

/**
* Mirror this Manifold over the plane described by the unit form of the given
* normal vector. If the length of the normal is zero, an empty Manifold is
* returned. This operation can be chained. Transforms are combined and applied
* lazily.
*
* @param normal The normal vector of the plane to be mirrored over
*/
Manifold Manifold::Mirror(glm::vec3 normal) const {
if (glm::length(normal) == 0.) {
return Manifold();
}
auto n = glm::normalize(normal);
glm::mat4x3 m(1.0f - 2.0f * n.x * n.x, -2.0f * n.x * n.y,
-2.0f * n.x * n.z, //
-2.0f * n.x * n.y, 1.0f - 2.0f * n.y * n.y,
-2.0f * n.y * n.z, //
-2.0f * n.x * n.z, -2.0f * n.y * n.z,
1.0f - 2.0f * n.z * n.z //
,
0.0f, 0.0f, 0.0f);
geoffder marked this conversation as resolved.
Show resolved Hide resolved
return Manifold(pNode_->Transform(m));
}

/**
* This function does not change the topology, but allows the vertices to be
* moved according to any arbitrary input function. It is easy to create a
Expand Down
15 changes: 15 additions & 0 deletions test/manifold_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -490,3 +490,18 @@ TEST(Manifold, FaceIDRoundTrip) {
const MeshGL outGL = cube2.GetMeshGL();
ASSERT_EQ(NumUnique(outGL.faceID), 12);
}

TEST(Manifold, MirrorUnion) {
auto a = Manifold::Cube({5., 5., 5.}, true);
auto b = a.Translate({2.5, 2.5, 2.5});
auto result = a + b + b.Mirror({1, 1, 0});

#ifdef MANIFOLD_EXPORT
if (options.exportModels)
ExportMesh("manifold_mirror_union.glb", result.GetMesh(), {});
#endif

auto vol_a = a.GetProperties().volume;
EXPECT_FLOAT_EQ(vol_a + 1.75 * vol_a, result.GetProperties().volume);
geoffder marked this conversation as resolved.
Show resolved Hide resolved
EXPECT_TRUE(a.Mirror(glm::vec3(0)).IsEmpty());
}