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#ifdef MANIFOLD_EXPORT
20#include <iostream>
21#endif
22
23#include "manifold/common.h"
24#include "manifold/vec_view.h"
25
26namespace manifold {
27
35ExecutionParams& ManifoldParams();
36
37class CsgNode;
38class CsgLeafNode;
39
55template <typename Precision, typename I = uint32_t>
56struct MeshGLP {
58 I NumVert() const { return vertProperties.size() / numProp; };
60 I NumTri() const { return triVerts.size() / 3; };
62 I numProp = 3;
66 std::vector<Precision> vertProperties;
69 std::vector<I> triVerts;
72 std::vector<I> mergeFromVert;
76 std::vector<I> mergeToVert;
84 std::vector<I> runIndex;
90 std::vector<uint32_t> runOriginalID;
95 std::vector<Precision> runTransform;
100 std::vector<I> faceID;
105 std::vector<Precision> halfedgeTangent;
110 Precision tolerance = 0;
111
112 MeshGLP() = default;
113
128 bool Merge();
129
136 size_t offset = v * numProp;
138 vertProperties[offset + 1],
139 vertProperties[offset + 2]);
140 }
141
147 la::vec<I, 3> GetTriVerts(size_t t) const {
148 size_t offset = 3 * t;
149 return la::vec<I, 3>(triVerts[offset], triVerts[offset + 1],
150 triVerts[offset + 2]);
151 }
152
159 size_t offset = 4 * h;
161 halfedgeTangent[offset], halfedgeTangent[offset + 1],
162 halfedgeTangent[offset + 2], halfedgeTangent[offset + 3]);
163 }
164};
165
174
198class Manifold {
199 public:
204 Manifold();
205 ~Manifold();
206 Manifold(const Manifold& other);
207 Manifold& operator=(const Manifold& other);
208 Manifold(Manifold&&) noexcept;
209 Manifold& operator=(Manifold&&) noexcept;
211
216 Manifold(const MeshGL&);
217 Manifold(const MeshGL64&);
218 MeshGL GetMeshGL(int normalIdx = -1) const;
219 MeshGL64 GetMeshGL64(int normalIdx = -1) const;
221
226 std::vector<Manifold> Decompose() const;
227 static Manifold Compose(const std::vector<Manifold>&);
228 static Manifold Tetrahedron();
229 static Manifold Cube(vec3 size = vec3(1.0), bool center = false);
230 static Manifold Cylinder(double height, double radiusLow,
231 double radiusHigh = -1.0, int circularSegments = 0,
232 bool center = false);
233 static Manifold Sphere(double radius, int circularSegments = 0);
234 static Manifold LevelSet(std::function<double(vec3)> sdf, Box bounds,
235 double edgeLength, double level = 0,
236 double tolerance = -1, bool canParallel = true);
238
243 Polygons Slice(double height = 0) const;
244 Polygons Project() const;
245 static Manifold Extrude(const Polygons& crossSection, double height,
246 int nDivisions = 0, double twistDegrees = 0.0,
247 vec2 scaleTop = vec2(1.0));
248 static Manifold Revolve(const Polygons& crossSection,
249 int circularSegments = 0,
250 double revolveDegrees = 360.0f);
252
253 enum class Error {
254 NoError,
255 NonFiniteVertex,
256 NotManifold,
257 VertexOutOfBounds,
258 PropertiesWrongLength,
259 MissingPositionProperties,
260 MergeVectorsDifferentLengths,
261 MergeIndexOutOfBounds,
262 TransformWrongLength,
263 RunIndexWrongLength,
264 FaceIDWrongLength,
265 InvalidConstruction,
266 };
267
272 Error Status() const;
273 bool IsEmpty() const;
274 size_t NumVert() const;
275 size_t NumEdge() const;
276 size_t NumTri() const;
277 size_t NumProp() const;
278 size_t NumPropVert() const;
279 Box BoundingBox() const;
280 int Genus() const;
281 double GetTolerance() const;
283
287 double SurfaceArea() const;
288 double Volume() const;
289 double MinGap(const Manifold& other, double searchLength) const;
291
297 int OriginalID() const;
298 Manifold AsOriginal() const;
299 static uint32_t ReserveIDs(uint32_t);
301
305 Manifold Translate(vec3) const;
306 Manifold Scale(vec3) const;
307 Manifold Rotate(double xDegrees, double yDegrees = 0.0,
308 double zDegrees = 0.0) const;
309 Manifold Mirror(vec3) const;
310 Manifold Transform(const mat3x4&) const;
311 Manifold Warp(std::function<void(vec3&)>) const;
312 Manifold WarpBatch(std::function<void(VecView<vec3>)>) const;
313 Manifold SetTolerance(double) const;
315
320 Manifold Boolean(const Manifold& second, OpType op) const;
321 static Manifold BatchBoolean(const std::vector<Manifold>& manifolds,
322 OpType op);
323 // Boolean operation shorthand
324 Manifold operator+(const Manifold&) const; // Add (Union)
326 Manifold operator-(const Manifold&) const; // Subtract (Difference)
328 Manifold operator^(const Manifold&) const; // Intersect
330 std::pair<Manifold, Manifold> Split(const Manifold&) const;
331 std::pair<Manifold, Manifold> SplitByPlane(vec3 normal,
332 double originOffset) const;
333 Manifold TrimByPlane(vec3 normal, double originOffset) const;
335
341 int numProp,
342 std::function<void(double*, vec3, const double*)> propFunc) const;
343 Manifold CalculateCurvature(int gaussianIdx, int meanIdx) const;
344 Manifold CalculateNormals(int normalIdx, double minSharpAngle = 60) const;
346
352 Manifold Refine(int) const;
353 Manifold RefineToLength(double) const;
354 Manifold RefineToTolerance(double) const;
355 Manifold SmoothByNormals(int normalIdx) const;
356 Manifold SmoothOut(double minSharpAngle = 60, double minSmoothness = 0) const;
357 static Manifold Smooth(const MeshGL&,
358 const std::vector<Smoothness>& sharpenedEdges = {});
359 static Manifold Smooth(const MeshGL64&,
360 const std::vector<Smoothness>& sharpenedEdges = {});
362
366 Manifold Hull() const;
367 static Manifold Hull(const std::vector<Manifold>& manifolds);
368 static Manifold Hull(const std::vector<vec3>& pts);
370
375 bool MatchesTriNormals() const;
376 size_t NumDegenerateTris() const;
377 size_t NumOverlaps(const Manifold& second) const;
378 double GetEpsilon() const;
380
381 struct Impl;
382
383#ifdef MANIFOLD_EXPORT
384 static Manifold ImportMeshGL64(std::istream& stream);
385#endif
386
387 private:
388 Manifold(std::shared_ptr<CsgNode> pNode_);
389 Manifold(std::shared_ptr<Impl> pImpl_);
390 static Manifold Invalid();
391 mutable std::shared_ptr<CsgNode> pNode_;
392
393 CsgLeafNode& GetCsgLeafNode() const;
394};
405#ifdef MANIFOLD_DEBUG
406inline std::string ToString(const Manifold::Error& error) {
407 switch (error) {
408 case Manifold::Error::NoError:
409 return "No Error";
410 case Manifold::Error::NonFiniteVertex:
411 return "Non Finite Vertex";
412 case Manifold::Error::NotManifold:
413 return "Not Manifold";
414 case Manifold::Error::VertexOutOfBounds:
415 return "Vertex Out Of Bounds";
416 case Manifold::Error::PropertiesWrongLength:
417 return "Properties Wrong Length";
418 case Manifold::Error::MissingPositionProperties:
419 return "Missing Position Properties";
420 case Manifold::Error::MergeVectorsDifferentLengths:
421 return "Merge Vectors Different Lengths";
422 case Manifold::Error::MergeIndexOutOfBounds:
423 return "Merge Index Out Of Bounds";
424 case Manifold::Error::TransformWrongLength:
425 return "Transform Wrong Length";
426 case Manifold::Error::RunIndexWrongLength:
427 return "Run Index Wrong Length";
428 case Manifold::Error::FaceIDWrongLength:
429 return "Face ID Wrong Length";
430 case Manifold::Error::InvalidConstruction:
431 return "Invalid Construction";
432 default:
433 return "Unknown Error";
434 };
435}
436
437inline std::ostream& operator<<(std::ostream& stream,
438 const Manifold::Error& error) {
439 return stream << ToString(error);
440}
441#endif
443} // namespace manifold
This library's internal representation of an oriented, 2-manifold, triangle mesh - a simple boundary-...
Definition manifold.h:198
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:56
la::vec< I, 3 > GetTriVerts(size_t t) const
Definition manifold.h:147
std::vector< I > mergeFromVert
Definition manifold.h:72
std::vector< I > runIndex
Definition manifold.h:84
std::vector< Precision > vertProperties
Definition manifold.h:66
std::vector< Precision > runTransform
Definition manifold.h:95
std::vector< I > mergeToVert
Definition manifold.h:76
I NumTri() const
Number of triangles.
Definition manifold.h:60
std::vector< Precision > halfedgeTangent
Definition manifold.h:105
std::vector< uint32_t > runOriginalID
Definition manifold.h:90
std::vector< I > triVerts
Definition manifold.h:69
la::vec< Precision, 4 > GetTangent(size_t h) const
Definition manifold.h:158
Precision tolerance
Definition manifold.h:110
I NumVert() const
Number of property vertices.
Definition manifold.h:58
std::vector< I > faceID
Definition manifold.h:100
la::vec< Precision, 3 > GetVertPos(size_t v) const
Definition manifold.h:135
I numProp
Number of properties per vertex, always >= 3.
Definition manifold.h:62