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

[FEAT] Add Sweep Operation #636

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions bindings/c/include/manifoldc.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ ManifoldManifold *manifold_hull(void *mem, ManifoldManifold *m);
ManifoldManifold *manifold_batch_hull(void *mem, ManifoldManifoldVec *ms);
ManifoldManifold *manifold_hull_pts(void *mem, ManifoldVec3 *ps, size_t length);

// Sweeps

ManifoldManifold *manifold_sweep(void *mem, ManifoldManifold *m, float x,
float y, float z);

// Manifold Transformations

ManifoldManifold *manifold_translate(void *mem, ManifoldManifold *m, float x,
Expand Down
7 changes: 7 additions & 0 deletions bindings/c/manifoldc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,13 @@ ManifoldManifold *manifold_hull_pts(void *mem, ManifoldVec3 *ps,
return to_c(new (mem) Manifold(hulled));
}

ManifoldManifold *manifold_sweep(void *mem, ManifoldManifold *m, float x,
float y, float z) {
auto v = glm::vec3(x, y, z);
auto swept = from_c(m)->Sweep(v);
return to_c(new (mem) Manifold(swept));
}

ManifoldManifold *manifold_translate(void *mem, ManifoldManifold *m, float x,
float y, float z) {
auto v = glm::vec3(x, y, z);
Expand Down
15 changes: 15 additions & 0 deletions bindings/python/manifold3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,21 @@ NB_MODULE(manifold3d, m) {
return Manifold::Hull(vec);
},
"Compute the convex hull enveloping a set of 3d points.")
.def(
"sweep",
[](Manifold &self, float x = 0.0f, float y = 0.0f, float z = 0.0f) {
return self.Sweep(glm::vec3(x, y, z));
},
nb::arg("x") = 0.0f, nb::arg("y") = 0.0f, nb::arg("z") = 0.0f,
"Sweep this Manifold through space."
"\n\n"
":param x: X axis translation. (default 0.0).\n"
":param y: Y axis translation. (default 0.0).\n"
":param z: Z axis translation. (default 0.0).")
.def("sweep", &Manifold::Sweep, nb::arg("t"),
"Sweep this Manifold through space."
"\n\n"
":param v: The vector to add to every vertex.")
.def(
"transform",
[](Manifold &self, nb::ndarray<float, nb::shape<3, 4>> &mat) {
Expand Down
1 change: 1 addition & 0 deletions bindings/wasm/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ EMSCRIPTEN_BINDINGS(whatever) {
.function("_SplitByPlane", &man_js::SplitByPlane)
.function("_TrimByPlane", &Manifold::TrimByPlane)
.function("hull", select_overload<Manifold() const>(&Manifold::Hull))
.function("sweep", &Manifold::Sweep)
.function("_GetMeshJS", &js::GetMeshJS)
.function("refine", &Manifold::Refine)
.function("_Warp", &man_js::Warp)
Expand Down
4 changes: 4 additions & 0 deletions bindings/wasm/bindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,10 @@ Module.setup = function() {
return out;
};

Module.Manifold.prototype.sweep = function(...vec) {
return this.sweep(vararg2vec3(vec));
};

Module.Manifold.prototype.translate = function(...vec) {
return this._Translate(vararg2vec3(vec));
};
Expand Down
9 changes: 9 additions & 0 deletions bindings/wasm/helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,15 @@ Manifold IntersectionN(const std::vector<Manifold>& manifolds) {
return Manifold::BatchBoolean(manifolds, OpType::Intersect);
}

Manifold Sweep(Manifold& manifold, const val& v) {
std::vector<float> array = convertJSArrayToNumberVector<float>(v);
glm::vec3 offset;
offset[0] = array[0];
offset[1] = array[1];
offset[2] = array[2];
return manifold.Sweep(offset);
}

Manifold Transform(Manifold& manifold, const val& mat) {
std::vector<float> array = convertJSArrayToNumberVector<float>(mat);
glm::mat4x3 matrix;
Expand Down
10 changes: 10 additions & 0 deletions bindings/wasm/manifold-encapsulated-types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,16 @@ export class Manifold {
*/
static hull(points: (Manifold|Vec3)[]): Manifold;

// Sweeps

/**
* Sweep this Manifold through space.
*
* @param v The vector to add to every vertex.
*/
sweep(v: Vec3): Manifold;
sweep(x: number, y?: number, z?: number): Manifold;

// Topological Operations

/**
Expand Down
2 changes: 2 additions & 0 deletions src/manifold/include/manifold.h
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,8 @@ class Manifold {
static Manifold Hull(const std::vector<Manifold>& manifolds);
static Manifold Hull(const std::vector<glm::vec3>& pts);

Manifold Sweep(glm::vec3) const;

/** @name Testing hooks
* These are just for internal testing.
*/
Expand Down
37 changes: 37 additions & 0 deletions src/manifold/src/manifold.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -824,4 +824,41 @@ Manifold Manifold::Hull() const { return Hull(GetMesh().vertPos); }
Manifold Manifold::Hull(const std::vector<Manifold>& manifolds) {
return Compose(manifolds).Hull();
}

/**
* Sweep this Manifold through space.
*
* @param v The vector to add to every vertex.
*/
Manifold Manifold::Sweep(glm::vec3 v) const {
// Slow(?) isConvex Check - Make Faster??
bool isConvex = GetProperties().volume == Hull().GetProperties().volume;

if (isConvex) {
// If Convex, just hull with its translated self
return Hull({*this, Translate(v)});
} else {
// Else, sweep each triangle individually
Mesh mesh = GetMesh();
Manifold output = AsOriginal();
for (size_t i = 0; i < mesh.triVerts.size(); i++) {
glm::vec3 normal = glm::cross(
mesh.vertPos[mesh.triVerts[i].y] - mesh.vertPos[mesh.triVerts[i].x],
mesh.vertPos[mesh.triVerts[i].z] - mesh.vertPos[mesh.triVerts[i].x]);
if (glm::dot(normal, v) > -0.0001f) { // Only Sweep Forward Triangles
Manifold sweptTriangle = Hull({
mesh.vertPos[mesh.triVerts[i].x],
mesh.vertPos[mesh.triVerts[i].y],
mesh.vertPos[mesh.triVerts[i].z],
mesh.vertPos[mesh.triVerts[i].x] + v,
mesh.vertPos[mesh.triVerts[i].y] + v,
mesh.vertPos[mesh.triVerts[i].z] + v,
});
output += sweptTriangle;
}
}
return output;
}
}

} // namespace manifold
16 changes: 16 additions & 0 deletions test/manifold_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -759,3 +759,19 @@ TEST(Manifold, EmptyHull) {
{0, 0, 0}, {1, 0, 0}, {0, 1, 0}, {1, 1, 0}};
EXPECT_TRUE(Manifold::Hull(coplanar).IsEmpty());
}

TEST(Manifold, SweptCube) {
std::vector<glm::vec3> cubePts = {
{0, 0, 0}, {1, 0, 0}, {0, 1, 0}, {0, 0, 1}, // corners
{1, 1, 0}, {0, 1, 1}, {1, 0, 1}, {1, 1, 1}, // corners
};
auto sweptCube = Manifold::Hull(cubePts).Sweep({1, 0, 0});
EXPECT_FLOAT_EQ(sweptCube.GetProperties().volume, 2);
}

TEST(Manifold, SweptComplex) {
auto sphere = Manifold::Sphere(0.6, 20);
auto cube = Manifold::Cube({1.0, 1.0, 1.0}, true);
auto sweptShape = (cube - sphere).Sweep({0.1, 0.3, 0.1});
EXPECT_FLOAT_EQ(sweptShape.GetProperties().volume, 0.7779319882392883);
}
Loading