Skip to content

Commit 4b645fc

Browse files
authored
updated manifold to latest version with double precision floating point (openscad#5282)
* updated manifold to latest version * use tbb parallelization * allow find_package for manifold * update to MeshGL64 * remove shared_ptr and fix minkowski color
1 parent 3995517 commit 4b645fc

File tree

12 files changed

+115
-104
lines changed

12 files changed

+115
-104
lines changed

CMakeLists.txt

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ option(INFO "Display build configuration info at end of cmake config" ON)
3232
option(ENABLE_TESTS "Run testsuite after building." ON)
3333
option(EXPERIMENTAL "Enable Experimental Features" OFF)
3434
option(USE_MANIFOLD_TRIANGULATOR "Use Manifold's triangulator instead of CGAL's" OFF)
35+
option(USE_BUILTIN_MANIFOLD "Use manifold from submodule" ON)
3536
option(USE_LEGACY_RENDERERS "Use legacy (non-VBO) OpenGL renderers" OFF)
3637
option(SNAPSHOT "Create dev snapshot, uses nightly icons" OFF)
3738
option(HEADLESS "Build without GUI frontend" OFF)
@@ -949,15 +950,18 @@ if(EXPERIMENTAL)
949950
set(PYBIND11_FINDPYTHON OFF)
950951
set(MANIFOLD_PYBIND OFF)
951952
set(MANIFOLD_TEST OFF)
952-
953-
if(CMAKE_UNITY_BUILD)
954-
set(CMAKE_UNITY_BUILD OFF)
955-
add_subdirectory(submodules/manifold EXCLUDE_FROM_ALL)
956-
set(CMAKE_UNITY_BUILD ON)
953+
if(USE_BUILTIN_MANIFOLD)
954+
if(CMAKE_UNITY_BUILD)
955+
set(CMAKE_UNITY_BUILD OFF)
956+
add_subdirectory(submodules/manifold EXCLUDE_FROM_ALL)
957+
set(CMAKE_UNITY_BUILD ON)
958+
else()
959+
add_subdirectory(submodules/manifold EXCLUDE_FROM_ALL)
960+
endif()
957961
else()
958-
add_subdirectory(submodules/manifold EXCLUDE_FROM_ALL)
962+
find_package(manifold REQUIRED)
959963
endif()
960-
target_link_libraries(OpenSCAD PRIVATE manifold polygon)
964+
target_link_libraries(OpenSCAD PRIVATE manifold polygon cross_section)
961965
add_sanitizers(manifold)
962966

963967
target_compile_definitions(OpenSCAD PRIVATE ENABLE_MANIFOLD)

src/RenderStatistic.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939

4040
#ifdef ENABLE_MANIFOLD
4141
#include "ManifoldGeometry.h"
42-
#include "manifold.h"
4342
#include "manifoldutils.h"
4443
#endif // ENABLE_MANIFOLD
4544

src/geometry/manifold/ManifoldGeometry.cc

Lines changed: 43 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// Portions of this file are Copyright 2023 Google LLC, and licensed under GPL2+. See COPYING.
22
#include "ManifoldGeometry.h"
33
#include "Polygon2d.h"
4-
#include "manifold.h"
4+
#include "manifold/cross_section.h"
5+
#include "manifold/manifold.h"
56
#include "PolySet.h"
6-
#include "Feature.h"
77
#include "PolySetBuilder.h"
88
#include "PolySetUtils.h"
99
#include "manifoldutils.h"
@@ -23,20 +23,18 @@ Result vector_convert(V const& v) {
2323

2424
}
2525

26-
ManifoldGeometry::ManifoldGeometry() : manifold_(std::make_shared<const manifold::Manifold>()) {}
26+
ManifoldGeometry::ManifoldGeometry() : manifold_(manifold::Manifold()) {}
2727

2828
ManifoldGeometry::ManifoldGeometry(
29-
const std::shared_ptr<const manifold::Manifold>& mani,
29+
manifold::Manifold mani,
3030
const std::set<uint32_t> & originalIDs,
3131
const std::map<uint32_t, Color4f> & originalIDToColor,
3232
const std::set<uint32_t> & subtractedIDs)
33-
: manifold_(mani),
33+
: manifold_(std::move(mani)),
3434
originalIDs_(originalIDs),
3535
originalIDToColor_(originalIDToColor),
3636
subtractedIDs_(subtractedIDs)
3737
{
38-
assert(manifold_);
39-
if (!manifold_) clear();
4038
}
4139

4240
std::unique_ptr<Geometry> ManifoldGeometry::copy() const
@@ -45,8 +43,7 @@ std::unique_ptr<Geometry> ManifoldGeometry::copy() const
4543
}
4644

4745
const manifold::Manifold& ManifoldGeometry::getManifold() const {
48-
assert(manifold_);
49-
return *manifold_;
46+
return manifold_;
5047
}
5148

5249
bool ManifoldGeometry::isEmpty() const {
@@ -66,11 +63,11 @@ bool ManifoldGeometry::isManifold() const {
6663
}
6764

6865
bool ManifoldGeometry::isValid() const {
69-
return manifold_->Status() == manifold::Manifold::Error::NoError;
66+
return manifold_.Status() == manifold::Manifold::Error::NoError;
7067
}
7168

7269
void ManifoldGeometry::clear() {
73-
manifold_ = std::make_shared<manifold::Manifold>();
70+
manifold_ = manifold::Manifold();
7471
}
7572

7673
size_t ManifoldGeometry::memsize() const {
@@ -81,18 +78,18 @@ size_t ManifoldGeometry::memsize() const {
8178
std::string ManifoldGeometry::dump() const {
8279
std::ostringstream out;
8380
auto &manifold = getManifold();
84-
auto mesh = manifold.GetMesh();
81+
auto meshgl = manifold.GetMeshGL64();
8582
out << "Manifold:"
8683
<< "\n status: " << ManifoldUtils::statusToString(manifold.Status())
8784
<< "\n genus: " << manifold.Genus()
88-
<< "\n num vertices: " << mesh.vertPos.size()
89-
<< "\n num polygons: " << mesh.triVerts.size()
85+
<< "\n num vertices: " << meshgl.NumVert()
86+
<< "\n num polygons: " << meshgl.NumTri()
9087
<< "\n polygons data:";
9188

92-
for (const auto &tv : mesh.triVerts) {
89+
for (size_t faceid = 0; faceid < meshgl.NumTri(); faceid++) {
9390
out << "\n polygon begin:";
9491
for (const int j : {0, 1, 2}) {
95-
Vector3d v = vector_convert<Vector3d>(mesh.vertPos[tv[j]]);
92+
auto v = vector_convert<Vector3d>(meshgl.GetVertPos(meshgl.GetTriVerts(faceid)[j]));
9693
out << "\n vertex:" << v;
9794
}
9895
}
@@ -101,7 +98,7 @@ std::string ManifoldGeometry::dump() const {
10198
}
10299

103100
std::shared_ptr<PolySet> ManifoldGeometry::toPolySet() const {
104-
manifold::MeshGL mesh = getManifold().GetMeshGL();
101+
manifold::MeshGL64 mesh = getManifold().GetMeshGL64();
105102
auto ps = std::make_shared<PolySet>(3);
106103
ps->setTriangular(true);
107104
ps->vertices.reserve(mesh.NumVert());
@@ -194,18 +191,18 @@ class CGALPolyhedronBuilderFromManifold : public CGAL::Modifier_base<typename Po
194191
public:
195192
using CGALPoint = typename CGAL_Polybuilder::Point_3;
196193

197-
const manifold::Mesh& mesh;
198-
CGALPolyhedronBuilderFromManifold(const manifold::Mesh& mesh) : mesh(mesh) { }
194+
const manifold::MeshGL64& meshgl;
195+
CGALPolyhedronBuilderFromManifold(const manifold::MeshGL64& mesh) : meshgl(mesh) { }
199196

200197
void operator()(HDS& hds) override {
201198
CGAL_Polybuilder B(hds, true);
202199

203-
B.begin_surface(mesh.vertPos.size(), mesh.triVerts.size());
204-
for (const auto &v : mesh.vertPos) {
205-
B.add_vertex(CGALUtils::vector_convert<CGALPoint>(v));
206-
}
200+
B.begin_surface(meshgl.NumVert(), meshgl.NumTri());
201+
for (size_t vertid = 0; vertid < meshgl.NumVert(); vertid++)
202+
B.add_vertex(CGALUtils::vector_convert<CGALPoint>(meshgl.GetVertPos(vertid)));
207203

208-
for (const auto &tv : mesh.triVerts) {
204+
for (size_t faceid = 0; faceid < meshgl.NumTri(); faceid++) {
205+
const auto tv = meshgl.GetTriVerts(faceid);
209206
B.begin_facet();
210207
for (const int j : {0, 1, 2}) {
211208
B.add_vertex_to_facet(tv[j]);
@@ -221,8 +218,8 @@ std::shared_ptr<Polyhedron> ManifoldGeometry::toPolyhedron() const
221218
{
222219
auto p = std::make_shared<Polyhedron>();
223220
try {
224-
manifold::Mesh mesh = getManifold().GetMesh();
225-
CGALPolyhedronBuilderFromManifold<Polyhedron> builder(mesh);
221+
auto meshgl = getManifold().GetMeshGL64();
222+
CGALPolyhedronBuilderFromManifold<Polyhedron> builder(meshgl);
226223
p->delegate(builder);
227224
} catch (const CGAL::Assertion_exception& e) {
228225
LOG(message_group::Error, "CGAL error in CGALUtils::createPolyhedronFromPolySet: %1$s", e.what());
@@ -234,7 +231,7 @@ template std::shared_ptr<CGAL::Polyhedron_3<CGAL_Kernel3>> ManifoldGeometry::toP
234231
#endif
235232

236233
ManifoldGeometry ManifoldGeometry::binOp(const ManifoldGeometry& lhs, const ManifoldGeometry& rhs, manifold::OpType opType) const {
237-
auto mani = std::make_shared<manifold::Manifold>(lhs.manifold_->Boolean(*rhs.manifold_, opType));
234+
auto mani = lhs.manifold_.Boolean(rhs.manifold_, opType);
238235
auto originalIDToColor = lhs.originalIDToColor_;
239236
auto subtractedIDs = lhs.subtractedIDs_;
240237

@@ -304,34 +301,44 @@ ManifoldGeometry ManifoldGeometry::minkowski(const ManifoldGeometry& other) cons
304301
}
305302

306303
Polygon2d ManifoldGeometry::slice() const {
307-
auto cross_section = manifold_->Slice();
304+
auto cross_section = manifold::CrossSection(manifold_.Slice());
308305
return ManifoldUtils::polygonsToPolygon2d(cross_section.ToPolygons());
309306
}
310307

311308
Polygon2d ManifoldGeometry::project() const {
312-
auto cross_section = manifold_->Project();
309+
auto cross_section = manifold::CrossSection(manifold_.Project());
313310
return ManifoldUtils::polygonsToPolygon2d(cross_section.ToPolygons());
314311
}
315312

316313
void ManifoldGeometry::transform(const Transform3d& mat) {
317-
glm::mat4x3 glMat(
314+
manifold::mat4x3 glMat(
318315
// Column-major ordering
319316
mat(0, 0), mat(1, 0), mat(2, 0),
320317
mat(0, 1), mat(1, 1), mat(2, 1),
321318
mat(0, 2), mat(1, 2), mat(2, 2),
322319
mat(0, 3), mat(1, 3), mat(2, 3)
323320
);
324-
manifold_ = std::make_shared<manifold::Manifold>(getManifold().Transform(glMat));
321+
manifold_ = getManifold().Transform(glMat);
325322
}
326323

327324
void ManifoldGeometry::setColor(const Color4f& c) {
328-
if (manifold_->OriginalID() == -1) {
329-
manifold_ = std::make_shared<manifold::Manifold>(manifold_->AsOriginal());
325+
if (manifold_.OriginalID() == -1) {
326+
manifold_ = manifold_.AsOriginal();
327+
}
328+
originalIDs_.clear();
329+
originalIDs_.insert(manifold_.OriginalID());
330+
originalIDToColor_.clear();
331+
originalIDToColor_[manifold_.OriginalID()] = c;
332+
subtractedIDs_.clear();
333+
}
334+
335+
void ManifoldGeometry::toOriginal() {
336+
if (manifold_.OriginalID() == -1) {
337+
manifold_ = manifold_.AsOriginal();
330338
}
331339
originalIDs_.clear();
332-
originalIDs_.insert(manifold_->OriginalID());
340+
originalIDs_.insert(manifold_.OriginalID());
333341
originalIDToColor_.clear();
334-
originalIDToColor_[manifold_->OriginalID()] = c;
335342
subtractedIDs_.clear();
336343
}
337344

@@ -349,7 +356,7 @@ void ManifoldGeometry::resize(const Vector3d& newsize, const Eigen::Matrix<bool,
349356
}
350357

351358
/*! Iterate over all vertices' points until the function returns true (for done). */
352-
void ManifoldGeometry::foreachVertexUntilTrue(const std::function<bool(const glm::vec3& pt)>& f) const {
359+
void ManifoldGeometry::foreachVertexUntilTrue(const std::function<bool(const manifold::vec3& pt)>& f) const {
353360
auto mesh = getManifold().GetMesh();
354361
for (const auto &pt : mesh.vertPos) {
355362
if (f(pt)) {

src/geometry/manifold/ManifoldGeometry.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include "Geometry.h"
55
#include <glm/glm.hpp>
66
#include "linalg.h"
7-
#include "manifold.h"
7+
#include "manifold/manifold.h"
88
#include <map>
99
#include <set>
1010

@@ -21,7 +21,7 @@ class ManifoldGeometry : public Geometry
2121

2222
ManifoldGeometry();
2323
ManifoldGeometry(
24-
const std::shared_ptr<const manifold::Manifold>& object,
24+
manifold::Manifold object,
2525
const std::set<uint32_t> & originalIDs = {},
2626
const std::map<uint32_t, Color4f> & originalIDToColor = {},
2727
const std::set<uint32_t> & subtractedIDs = {});
@@ -60,17 +60,18 @@ class ManifoldGeometry : public Geometry
6060

6161
void transform(const Transform3d& mat) override;
6262
void setColor(const Color4f& c) override;
63+
void toOriginal();
6364
void resize(const Vector3d& newsize, const Eigen::Matrix<bool, 3, 1>& autosize) override;
6465

6566
/*! Iterate over all vertices' points until the function returns true (for done). */
66-
void foreachVertexUntilTrue(const std::function<bool(const glm::vec3& pt)>& f) const;
67+
void foreachVertexUntilTrue(const std::function<bool(const manifold::vec3& pt)>& f) const;
6768

6869
const manifold::Manifold& getManifold() const;
6970

7071
private:
7172
ManifoldGeometry binOp(const ManifoldGeometry& lhs, const ManifoldGeometry& rhs, manifold::OpType opType) const;
7273

73-
std::shared_ptr<const manifold::Manifold> manifold_;
74+
manifold::Manifold manifold_;
7475
std::set<uint32_t> originalIDs_;
7576
std::map<uint32_t, Color4f> originalIDToColor_;
7677
std::set<uint32_t> subtractedIDs_;

src/geometry/manifold/manifold-applyops-minkowski.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ std::shared_ptr<const Geometry> applyMinkowskiManifold(const Geometry::Geometrie
204204
PRINTDB("Minkowski: Union done: %f s", t.time());
205205
t.reset();
206206

207+
N->toOriginal();
207208
operands[0] = N;
208209
}
209210

src/geometry/manifold/manifoldutils.cc

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#include "ManifoldGeometry.h"
44
#include "PolySetBuilder.h"
55
#include "Feature.h"
6-
#include "manifold.h"
76
#include "printutils.h"
87
#ifdef ENABLE_CGAL
98
#include "cgalutils.h"
@@ -13,7 +12,7 @@
1312
#endif
1413
#include "PolySetUtils.h"
1514
#include "PolySet.h"
16-
#include "polygon.h"
15+
#include "manifold/polygon.h"
1716

1817
using Error = manifold::Manifold::Error;
1918

@@ -41,15 +40,18 @@ std::shared_ptr<ManifoldGeometry> createManifoldFromSurfaceMesh(const TriangleMe
4140
{
4241
typedef typename TriangleMesh::Vertex_index vertex_descriptor;
4342

44-
manifold::Mesh mesh;
43+
manifold::MeshGL64 meshgl;
4544

46-
mesh.vertPos.resize(tm.number_of_vertices());
45+
meshgl.numProp = 3;
46+
meshgl.vertProperties.resize(tm.number_of_vertices() * 3);
4747
for (vertex_descriptor vd : tm.vertices()){
4848
const auto &v = tm.point(vd);
49-
mesh.vertPos[vd] = glm::vec3((float) v.x(), (float) v.y(), (float) v.z());
49+
meshgl.vertProperties[3 * vd] = v.x();
50+
meshgl.vertProperties[3 * vd + 1] = v.y();
51+
meshgl.vertProperties[3 * vd + 2] = v.z();
5052
}
5153

52-
mesh.triVerts.reserve(tm.number_of_faces());
54+
meshgl.triVerts.reserve(tm.number_of_faces() * 3);
5355
for (const auto& f : tm.faces()) {
5456
size_t idx[3];
5557
size_t i = 0;
@@ -58,20 +60,21 @@ std::shared_ptr<ManifoldGeometry> createManifoldFromSurfaceMesh(const TriangleMe
5860
idx[i++] = vd;
5961
}
6062
if (i < 3) continue;
61-
mesh.triVerts.emplace_back(idx[0], idx[1], idx[2]);
63+
for (size_t j : {0, 1, 2})
64+
meshgl.triVerts.emplace_back(idx[j]);
6265
}
6366

64-
assert((mesh.triVerts.size() == tm.number_of_faces()) || !"Mesh was not triangular!");
67+
assert((meshgl.triVerts.size() == tm.number_of_faces() * 3) || !"Mesh was not triangular!");
6568

66-
auto mani = std::make_shared<manifold::Manifold>(std::move(mesh));
67-
if (mani->Status() != Error::NoError) {
69+
auto mani = manifold::Manifold(meshgl).AsOriginal();
70+
if (mani.Status() != Error::NoError) {
6871
LOG(message_group::Error,
6972
"[manifold] Surface_mesh -> Manifold conversion failed: %1$s",
70-
ManifoldUtils::statusToString(mani->Status()));
73+
ManifoldUtils::statusToString(mani.Status()));
7174
return nullptr;
7275
}
7376
std::set<uint32_t> originalIDs;
74-
auto id = mani->OriginalID();
77+
auto id = mani.OriginalID();
7578
if (id >= 0) {
7679
originalIDs.insert(id);
7780
}
@@ -87,13 +90,14 @@ std::shared_ptr<ManifoldGeometry> createManifoldFromTriangularPolySet(const Poly
8790
{
8891
assert(ps.isTriangular());
8992

90-
manifold::MeshGL mesh;
93+
manifold::MeshGL64 mesh;
9194

95+
mesh.numProp = 3;
9296
mesh.vertProperties.reserve(ps.vertices.size() * 3);
9397
for (const auto& v : ps.vertices) {
94-
mesh.vertProperties.push_back((float)v.x());
95-
mesh.vertProperties.push_back((float)v.y());
96-
mesh.vertProperties.push_back((float)v.z());
98+
mesh.vertProperties.push_back(v.x());
99+
mesh.vertProperties.push_back(v.y());
100+
mesh.vertProperties.push_back(v.z());
97101
}
98102

99103
mesh.triVerts.reserve(ps.indices.size() * 3);
@@ -132,7 +136,7 @@ std::shared_ptr<ManifoldGeometry> createManifoldFromTriangularPolySet(const Poly
132136
}
133137
mesh.runIndex.push_back(mesh.triVerts.size());
134138

135-
auto mani = std::make_shared<const manifold::Manifold>(std::move(mesh));
139+
auto mani = manifold::Manifold(mesh);
136140
return std::make_shared<ManifoldGeometry>(mani, originalIDs, originalIDToColor);
137141
}
138142

src/geometry/manifold/manifoldutils.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#include "Geometry.h"
44
#include "enums.h"
55
#include "ManifoldGeometry.h"
6-
#include "manifold.h"
76

87
namespace ManifoldUtils {
98

0 commit comments

Comments
 (0)