Skip to content

Commit

Permalink
WIP: Directly create Manifold from PolySet before attempting repair
Browse files Browse the repository at this point in the history
  • Loading branch information
kintel committed Feb 26, 2024
1 parent 4d9ccd3 commit 85994ac
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/geometry/manifold/manifoldutils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,49 @@ template std::shared_ptr<const ManifoldGeometry> createManifoldFromSurfaceMesh(c
template std::shared_ptr<const ManifoldGeometry> createManifoldFromSurfaceMesh(const CGAL_DoubleMesh &tm);
#endif

std::unique_ptr<const manifold::Manifold> createManifoldFromTriangularPolySet(const PolySet& ps)
{
assert(ps.isTriangular);

manifold::Mesh mesh;

mesh.vertPos.reserve(ps.vertices.size());
for (const auto& v : ps.vertices) {
mesh.vertPos.emplace_back((float)v.x(), (float)v.y(), (float)v.z());
}

mesh.triVerts.reserve(ps.indices.size());
for (const auto& face : ps.indices) {
assert(face.size() == 3);
mesh.triVerts.emplace_back(face[0], face[1], face[2]);
}

return std::make_unique<manifold::Manifold>(std::move(mesh));
}

std::shared_ptr<const ManifoldGeometry> createManifoldFromPolySet(const PolySet& ps)
{
// FIXME: To create a Manifold object, we may need to first triangulate.
// The incoming PolySet could be severely broken if it originated from user or file input, so
// be prepared to repair it.
// 1. If it's not triangular, attempt to triangulate it directly
// 2. Try to create a Manifold object
// 3. If the Manifold object is broken (e.g. not a manifold), attempt repair
// 4. If successfully repaired, try to create Manifold again.

std::unique_ptr<const PolySet> ps_tri;
if (!ps.isTriangular) {
ps_tri = PolySetUtils::tessellate_faces(ps);
}
const PolySet good_ps = ps.isTriangular ? ps : *ps_tri;
auto mani = createManifoldFromTriangularPolySet(good_ps);
if (mani->Status() == Error::NoError) {
return std::make_shared<const ManifoldGeometry>(std::shared_ptr<const manifold::Manifold>(std::move(mani)));
}

LOG("Warning: [manifold] Surface_mesh -> Manifold conversion failed: %1$s",
ManifoldUtils::statusToString(mani->Status()));

{
#ifdef ENABLE_CGAL
PolySet psq(ps);
Expand Down Expand Up @@ -146,6 +188,7 @@ std::shared_ptr<const ManifoldGeometry> createManifoldFromPolySet(const PolySet&
return std::make_shared<ManifoldGeometry>();
#endif
}
}

std::shared_ptr<const ManifoldGeometry> createManifoldFromGeometry(const std::shared_ptr<const Geometry>& geom) {
if (auto mani = std::dynamic_pointer_cast<const ManifoldGeometry>(geom)) {
Expand Down

0 comments on commit 85994ac

Please sign in to comment.