Manifold 3.0
Robust geometry
 
Loading...
Searching...
No Matches
manifold.h
1// Copyright 2021 The Manifold Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#pragma once
16#include <functional>
17#include <memory>
18
19#include "manifold/common.h"
20#include "manifold/vec_view.h"
21
22namespace manifold {
23
31ExecutionParams& ManifoldParams();
32
33class CsgNode;
34class CsgLeafNode;
35
51template <typename Precision, typename I = uint32_t>
52struct MeshGLP {
54 I NumVert() const { return vertProperties.size() / numProp; };
56 I NumTri() const { return triVerts.size() / 3; };
58 I numProp = 3;
62 std::vector<Precision> vertProperties;
65 std::vector<I> triVerts;
68 std::vector<I> mergeFromVert;
72 std::vector<I> mergeToVert;
80 std::vector<I> runIndex;
86 std::vector<uint32_t> runOriginalID;
91 std::vector<Precision> runTransform;
96 std::vector<I> faceID;
101 std::vector<Precision> halfedgeTangent;
106 Precision tolerance = 0;
107
108 MeshGLP() = default;
109
124 bool Merge();
125
132 size_t offset = v * numProp;
134 vertProperties[offset + 1],
135 vertProperties[offset + 2]);
136 }
137
143 la::vec<I, 3> GetTriVerts(size_t t) const {
144 size_t offset = 3 * t;
145 return la::vec<I, 3>(triVerts[offset], triVerts[offset + 1],
146 triVerts[offset + 2]);
147 }
148
155 size_t offset = 4 * h;
157 halfedgeTangent[offset], halfedgeTangent[offset + 1],
158 halfedgeTangent[offset + 2], halfedgeTangent[offset + 3]);
159 }
160};
161
170
194class Manifold {
195 public:
200 Manifold();
201 ~Manifold();
202 Manifold(const Manifold& other);
203 Manifold& operator=(const Manifold& other);
204 Manifold(Manifold&&) noexcept;
205 Manifold& operator=(Manifold&&) noexcept;
207
212 Manifold(const MeshGL&);
213 Manifold(const MeshGL64&);
214 MeshGL GetMeshGL(int normalIdx = -1) const;
215 MeshGL64 GetMeshGL64(int normalIdx = -1) const;
217
222 std::vector<Manifold> Decompose() const;
223 static Manifold Compose(const std::vector<Manifold>&);
224 static Manifold Tetrahedron();
225 static Manifold Cube(vec3 size = vec3(1.0), bool center = false);
226 static Manifold Cylinder(double height, double radiusLow,
227 double radiusHigh = -1.0, int circularSegments = 0,
228 bool center = false);
229 static Manifold Sphere(double radius, int circularSegments = 0);
230 static Manifold LevelSet(std::function<double(vec3)> sdf, Box bounds,
231 double edgeLength, double level = 0,
232 double tolerance = -1, bool canParallel = true);
234
239 Polygons Slice(double height = 0) const;
240 Polygons Project() const;
241 static Manifold Extrude(const Polygons& crossSection, double height,
242 int nDivisions = 0, double twistDegrees = 0.0,
243 vec2 scaleTop = vec2(1.0));
244 static Manifold Revolve(const Polygons& crossSection,
245 int circularSegments = 0,
246 double revolveDegrees = 360.0f);
248
249 enum class Error {
250 NoError,
251 NonFiniteVertex,
252 NotManifold,
253 VertexOutOfBounds,
254 PropertiesWrongLength,
255 MissingPositionProperties,
256 MergeVectorsDifferentLengths,
257 MergeIndexOutOfBounds,
258 TransformWrongLength,
259 RunIndexWrongLength,
260 FaceIDWrongLength,
261 InvalidConstruction,
262 };
263
268 Error Status() const;
269 bool IsEmpty() const;
270 size_t NumVert() const;
271 size_t NumEdge() const;
272 size_t NumTri() const;
273 size_t NumProp() const;
274 size_t NumPropVert() const;
275 Box BoundingBox() const;
276 int Genus() const;
277 double GetTolerance() const;
279
283 double SurfaceArea() const;
284 double Volume() const;
285 double MinGap(const Manifold& other, double searchLength) const;
287
293 int OriginalID() const;
294 Manifold AsOriginal() const;
295 static uint32_t ReserveIDs(uint32_t);
297
301 Manifold Translate(vec3) const;
302 Manifold Scale(vec3) const;
303 Manifold Rotate(double xDegrees, double yDegrees = 0.0,
304 double zDegrees = 0.0) const;
305 Manifold Mirror(vec3) const;
306 Manifold Transform(const mat3x4&) const;
307 Manifold Warp(std::function<void(vec3&)>) const;
308 Manifold WarpBatch(std::function<void(VecView<vec3>)>) const;
309 Manifold SetTolerance(double) const;
311
316 Manifold Boolean(const Manifold& second, OpType op) const;
317 static Manifold BatchBoolean(const std::vector<Manifold>& manifolds,
318 OpType op);
319 // Boolean operation shorthand
320 Manifold operator+(const Manifold&) const; // Add (Union)
322 Manifold operator-(const Manifold&) const; // Subtract (Difference)
324 Manifold operator^(const Manifold&) const; // Intersect
326 std::pair<Manifold, Manifold> Split(const Manifold&) const;
327 std::pair<Manifold, Manifold> SplitByPlane(vec3 normal,
328 double originOffset) const;
329 Manifold TrimByPlane(vec3 normal, double originOffset) const;
331
337 int numProp,
338 std::function<void(double*, vec3, const double*)> propFunc) const;
339 Manifold CalculateCurvature(int gaussianIdx, int meanIdx) const;
340 Manifold CalculateNormals(int normalIdx, double minSharpAngle = 60) const;
342
348 Manifold Refine(int) const;
349 Manifold RefineToLength(double) const;
350 Manifold RefineToTolerance(double) const;
351 Manifold SmoothByNormals(int normalIdx) const;
352 Manifold SmoothOut(double minSharpAngle = 60, double minSmoothness = 0) const;
353 static Manifold Smooth(const MeshGL&,
354 const std::vector<Smoothness>& sharpenedEdges = {});
355 static Manifold Smooth(const MeshGL64&,
356 const std::vector<Smoothness>& sharpenedEdges = {});
358
362 Manifold Hull() const;
363 static Manifold Hull(const std::vector<Manifold>& manifolds);
364 static Manifold Hull(const std::vector<vec3>& pts);
366
371 bool MatchesTriNormals() const;
372 size_t NumDegenerateTris() const;
373 size_t NumOverlaps(const Manifold& second) const;
374 double GetEpsilon() const;
376
377 struct Impl;
378
379 private:
380 Manifold(std::shared_ptr<CsgNode> pNode_);
381 Manifold(std::shared_ptr<Impl> pImpl_);
382 static Manifold Invalid();
383 mutable std::shared_ptr<CsgNode> pNode_;
384
385 CsgLeafNode& GetCsgLeafNode() const;
386};
397#ifdef MANIFOLD_DEBUG
398inline std::string ToString(const Manifold::Error& error) {
399 switch (error) {
400 case Manifold::Error::NoError:
401 return "No Error";
402 case Manifold::Error::NonFiniteVertex:
403 return "Non Finite Vertex";
404 case Manifold::Error::NotManifold:
405 return "Not Manifold";
406 case Manifold::Error::VertexOutOfBounds:
407 return "Vertex Out Of Bounds";
408 case Manifold::Error::PropertiesWrongLength:
409 return "Properties Wrong Length";
410 case Manifold::Error::MissingPositionProperties:
411 return "Missing Position Properties";
412 case Manifold::Error::MergeVectorsDifferentLengths:
413 return "Merge Vectors Different Lengths";
414 case Manifold::Error::MergeIndexOutOfBounds:
415 return "Merge Index Out Of Bounds";
416 case Manifold::Error::TransformWrongLength:
417 return "Transform Wrong Length";
418 case Manifold::Error::RunIndexWrongLength:
419 return "Run Index Wrong Length";
420 case Manifold::Error::FaceIDWrongLength:
421 return "Face ID Wrong Length";
422 case Manifold::Error::InvalidConstruction:
423 return "Invalid Construction";
424 default:
425 return "Unknown Error";
426 };
427}
428
429inline std::ostream& operator<<(std::ostream& stream,
430 const Manifold::Error& error) {
431 return stream << ToString(error);
432}
433#endif
435} // namespace manifold
This library's internal representation of an oriented, 2-manifold, triangle mesh - a simple boundary-...
Definition manifold.h:194
static Manifold LevelSet(std::function< double(vec3)> sdf, Box bounds, double edgeLength, double level=0, double tolerance=-1, bool canParallel=true)
Definition sdf.cpp:455
Manifold SmoothOut(double minSharpAngle=60, double minSmoothness=0) const
Definition manifold.cpp:753
Manifold RefineToLength(double) const
Definition manifold.cpp:802
Manifold CalculateCurvature(int gaussianIdx, int meanIdx) const
Definition manifold.cpp:689
static Manifold BatchBoolean(const std::vector< Manifold > &manifolds, OpType op)
Definition manifold.cpp:868
double MinGap(const Manifold &other, double searchLength) const
Definition manifold.cpp:1031
static uint32_t ReserveIDs(uint32_t)
Definition manifold.cpp:478
size_t NumProp() const
Definition manifold.cpp:363
Polygons Project() const
Definition manifold.cpp:987
size_t NumPropVert() const
Definition manifold.cpp:371
static Manifold Extrude(const Polygons &crossSection, double height, int nDivisions=0, double twistDegrees=0.0, vec2 scaleTop=vec2(1.0))
Definition constructors.cpp:209
Box BoundingBox() const
Definition manifold.cpp:378
std::pair< Manifold, Manifold > SplitByPlane(vec3 normal, double originOffset) const
Definition manifold.cpp:953
Error Status() const
Definition manifold.cpp:341
int Genus() const
Definition manifold.cpp:423
Manifold & operator+=(const Manifold &)
Definition manifold.cpp:890
Manifold Warp(std::function< void(vec3 &)>) const
Definition manifold.cpp:588
Manifold Refine(int) const
Definition manifold.cpp:783
MeshGL64 GetMeshGL64(int normalIdx=-1) const
Definition manifold.cpp:324
static Manifold Sphere(double radius, int circularSegments=0)
Definition constructors.cpp:169
std::pair< Manifold, Manifold > Split(const Manifold &) const
Definition manifold.cpp:932
bool MatchesTriNormals() const
Definition manifold.cpp:487
size_t NumEdge() const
Definition manifold.cpp:353
int OriginalID() const
Definition manifold.cpp:447
double Volume() const
Definition manifold.cpp:438
Manifold Transform(const mat3x4 &) const
Definition manifold.cpp:558
MeshGL GetMeshGL(int normalIdx=-1) const
Definition manifold.cpp:305
Manifold RefineToTolerance(double) const
Definition manifold.cpp:823
Manifold WarpBatch(std::function< void(VecView< vec3 >)>) const
Definition manifold.cpp:607
Manifold Translate(vec3) const
Definition manifold.cpp:522
std::vector< Manifold > Decompose() const
Definition constructors.cpp:447
Manifold Rotate(double xDegrees, double yDegrees=0.0, double zDegrees=0.0) const
Definition manifold.cpp:546
Manifold SetTolerance(double) const
Definition manifold.cpp:403
size_t NumOverlaps(const Manifold &second) const
Definition manifold.cpp:506
static Manifold Cylinder(double height, double radiusLow, double radiusHigh=-1.0, int circularSegments=0, bool center=false)
Definition constructors.cpp:137
Manifold & operator^=(const Manifold &)
Definition manifold.cpp:920
static Manifold Revolve(const Polygons &crossSection, int circularSegments=0, double revolveDegrees=360.0f)
Definition constructors.cpp:296
bool IsEmpty() const
Definition manifold.cpp:332
static Manifold Compose(const std::vector< Manifold > &)
Definition constructors.cpp:434
Manifold Boolean(const Manifold &second, OpType op) const
Definition manifold.cpp:860
Manifold AsOriginal() const
Definition manifold.cpp:457
Manifold Hull() const
Definition manifold.cpp:1009
Manifold SmoothByNormals(int normalIdx) const
Definition manifold.cpp:727
Manifold Scale(vec3) const
Definition manifold.cpp:532
Manifold TrimByPlane(vec3 normal, double originOffset) const
Definition manifold.cpp:967
size_t NumTri() const
Definition manifold.cpp:359
Manifold SetProperties(int numProp, std::function< void(double *, vec3, const double *)> propFunc) const
Definition manifold.cpp:632
Manifold CalculateNormals(int normalIdx, double minSharpAngle=60) const
Definition manifold.cpp:710
double SurfaceArea() const
Definition manifold.cpp:431
size_t NumVert() const
Definition manifold.cpp:347
Polygons Slice(double height=0) const
Definition manifold.cpp:977
double GetTolerance() const
Definition manifold.cpp:395
Manifold operator^(const Manifold &) const
Definition manifold.cpp:913
double GetEpsilon() const
Definition manifold.cpp:386
static Manifold Cube(vec3 size=vec3(1.0), bool center=false)
Definition constructors.cpp:115
Manifold()
Definition manifold.cpp:227
Manifold Mirror(vec3) const
Definition manifold.cpp:570
Manifold operator-(const Manifold &) const
Definition manifold.cpp:898
Manifold & operator-=(const Manifold &)
Definition manifold.cpp:905
size_t NumDegenerateTris() const
Definition manifold.cpp:496
Manifold operator+(const Manifold &) const
Definition manifold.cpp:883
static Manifold Tetrahedron()
Definition constructors.cpp:103
static Manifold Smooth(const MeshGL &, const std::vector< Smoothness > &sharpenedEdges={})
Definition constructors.cpp:49
Definition vec_view.h:32
ExecutionParams & ManifoldParams()
Definition manifold.cpp:991
std::vector< SimplePolygon > Polygons
Set of polygons with holes. Order of contours is arbitrary. Can contain any depth of nested holes and...
Definition common.h:139
OpType
Boolean operation type: Add (Union), Subtract (Difference), and Intersect.
Definition common.h:473
Definition linalg.h:68
Definition linalg.h:63
Axis-aligned 3D box, primarily for bounding.
Definition common.h:157
Mesh input/output suitable for pushing directly into graphics libraries.
Definition manifold.h:52
la::vec< I, 3 > GetTriVerts(size_t t) const
Definition manifold.h:143
std::vector< I > mergeFromVert
Definition manifold.h:68
std::vector< I > runIndex
Definition manifold.h:80
std::vector< Precision > vertProperties
Definition manifold.h:62
std::vector< Precision > runTransform
Definition manifold.h:91
std::vector< I > mergeToVert
Definition manifold.h:72
I NumTri() const
Number of triangles.
Definition manifold.h:56
std::vector< Precision > halfedgeTangent
Definition manifold.h:101
std::vector< uint32_t > runOriginalID
Definition manifold.h:86
std::vector< I > triVerts
Definition manifold.h:65
la::vec< Precision, 4 > GetTangent(size_t h) const
Definition manifold.h:154
Precision tolerance
Definition manifold.h:106
I NumVert() const
Number of property vertices.
Definition manifold.h:54
std::vector< I > faceID
Definition manifold.h:96
la::vec< Precision, 3 > GetVertPos(size_t v) const
Definition manifold.h:131
I numProp
Number of properties per vertex, always >= 3.
Definition manifold.h:58