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 <cstdint> // uint32_t, uint64_t
17#include <functional>
18#include <memory> // needed for shared_ptr
19#include <mutex>
20
21#include "manifold/common.h"
22#include "manifold/vec_view.h"
23
24namespace manifold {
25
34
35class CsgNode;
36class CsgLeafNode;
37
42
113template <typename Precision, typename I = uint32_t>
114struct MeshGLP {
116 I NumVert() const { return vertProperties.size() / numProp; };
118 I NumTri() const { return triVerts.size() / 3; };
120 I NumRun() const { return runOriginalID.size(); };
122 I numProp = 3;
126 std::vector<Precision> vertProperties;
129 std::vector<I> triVerts;
132 std::vector<I> mergeFromVert;
136 std::vector<I> mergeToVert;
144 std::vector<I> runIndex;
150 std::vector<uint32_t> runOriginalID;
155 std::vector<Precision> runTransform;
159 std::vector<uint8_t> runFlags;
165 std::vector<I> faceID;
170 std::vector<Precision> halfedgeTangent;
175 Precision tolerance = 0;
176
177 MeshGLP() = default;
178
188 void UpdateNormals(int normalIdx);
189
205 bool Merge();
206
212 la::vec<Precision, 3> GetVertPos(size_t v) const {
213 size_t offset = v * numProp;
214 return la::vec<Precision, 3>(vertProperties[offset],
215 vertProperties[offset + 1],
216 vertProperties[offset + 2]);
217 }
218
224 la::vec<I, 3> GetTriVerts(size_t t) const {
225 size_t offset = 3 * t;
226 return la::vec<I, 3>(triVerts[offset], triVerts[offset + 1],
227 triVerts[offset + 2]);
228 }
229
235 la::vec<Precision, 4> GetTangent(size_t h) const {
236 size_t offset = 4 * h;
237 return la::vec<Precision, 4>(
238 halfedgeTangent[offset], halfedgeTangent[offset + 1],
239 halfedgeTangent[offset + 2], halfedgeTangent[offset + 3]);
240 }
241
247 mat3x4 GetRunTransform(size_t run) const {
248 size_t offset = 12 * run;
249 if (offset + 12 > runTransform.size()) {
250 return la::identity;
251 }
252 return mat3x4(la::mat<Precision, 3, 4>(&runTransform[offset]));
253 }
254
262 bool Backside(size_t run) const {
263 return run < runFlags.size() && runFlags[run] == 1;
264 }
265};
266
275
276MeshGL64 ReadOBJ(std::istream& stream);
277bool WriteOBJ(std::ostream& stream, const MeshGL64& mesh);
278
302class Manifold {
303 public:
308 Manifold();
309 ~Manifold();
310 Manifold(const Manifold& other);
311 Manifold& operator=(const Manifold& other);
312 Manifold(Manifold&&) noexcept;
313 Manifold& operator=(Manifold&&) noexcept;
315
320 Manifold(const MeshGL&);
321 Manifold(const MeshGL64&);
322 MeshGL GetMeshGL(int normalIdx = -1) const;
323 MeshGL64 GetMeshGL64(int normalIdx = -1) const;
325
330 std::vector<Manifold> Decompose() const;
331 [[deprecated(
332 "Compose is deprecated, use BatchBoolean with OpType::Add instead.")]]
333 static Manifold Compose(const std::vector<Manifold>&);
334 static Manifold Tetrahedron();
335 static Manifold Cube(vec3 size = vec3(1.0), bool center = false);
336 static Manifold Cylinder(double height, double radiusLow,
337 double radiusHigh = -1.0, int circularSegments = 0,
338 bool center = false);
339 static Manifold Sphere(double radius, int circularSegments = 0);
340 static Manifold LevelSet(std::function<double(vec3)> sdf, Box bounds,
341 double edgeLength, double level = 0,
342 double tolerance = -1, bool canParallel = true);
344
349 Polygons Slice(double height = 0) const;
350 Polygons Project() const;
351 static Manifold Extrude(const Polygons& crossSection, double height,
352 int nDivisions = 0, double twistDegrees = 0.0,
353 vec2 scaleTop = vec2(1.0));
354 static Manifold Revolve(const Polygons& crossSection,
355 int circularSegments = 0,
356 double revolveDegrees = 360.0f);
358
359 enum class Error {
360 NoError,
361 NonFiniteVertex,
362 NotManifold,
363 VertexOutOfBounds,
364 PropertiesWrongLength,
365 MissingPositionProperties,
366 MergeVectorsDifferentLengths,
367 MergeIndexOutOfBounds,
368 TransformWrongLength,
369 RunIndexWrongLength,
370 FaceIDWrongLength,
371 InvalidConstruction,
372 ResultTooLarge,
373 InvalidTangents,
374 Cancelled,
375 };
376
381 Error Status() const;
382 Error Status(ExecutionContext& ctx) const;
383 bool IsEmpty() const;
384 size_t NumVert() const;
385 size_t NumEdge() const;
386 size_t NumTri() const;
387 size_t NumProp() const;
388 size_t NumPropVert() const;
389 Box BoundingBox() const;
390 int Genus() const;
391 double GetTolerance() const;
393
397 double SurfaceArea() const;
398 double Volume() const;
399 double MinGap(const Manifold& other, double searchLength) const;
400 std::vector<RayHit> RayCast(vec3 origin, vec3 endpoint) const;
402
408 int OriginalID() const;
409 Manifold AsOriginal() const;
410 static uint32_t ReserveIDs(uint32_t);
412
416 Manifold Translate(vec3) const;
417 Manifold Scale(vec3) const;
418 Manifold Rotate(double xDegrees, double yDegrees = 0.0,
419 double zDegrees = 0.0) const;
420 Manifold Mirror(vec3) const;
421 Manifold Transform(const mat3x4&) const;
422 Manifold Warp(std::function<void(vec3&)>) const;
423 Manifold WarpBatch(std::function<void(VecView<vec3>)>) const;
424 Manifold SetTolerance(double) const;
425 Manifold Simplify(double tolerance = 0) const;
427
432 Manifold Boolean(const Manifold& second, OpType op) const;
433 static Manifold BatchBoolean(const std::vector<Manifold>& manifolds,
434 OpType op);
435 // Boolean operation shorthand
436 Manifold operator+(const Manifold&) const; // Add (Union)
438 Manifold operator-(const Manifold&) const; // Subtract (Difference)
440 Manifold operator^(const Manifold&) const; // Intersect
442 std::pair<Manifold, Manifold> Split(const Manifold&) const;
443 std::pair<Manifold, Manifold> SplitByPlane(vec3 normal,
444 double originOffset) const;
445 Manifold TrimByPlane(vec3 normal, double originOffset) const;
446 Manifold MinkowskiSum(const Manifold&) const;
449
455 int numProp,
456 std::function<void(double*, vec3, const double*)> propFunc) const;
457 Manifold CalculateCurvature(int gaussianIdx, int meanIdx) const;
458 Manifold CalculateNormals(int normalIdx, double minSharpAngle = 60) const;
460
466 Manifold Refine(int) const;
467 Manifold RefineToLength(double) const;
468 Manifold RefineToTolerance(double) const;
469 Manifold SmoothByNormals(int normalIdx) const;
470 Manifold SmoothOut(double minSharpAngle = 60, double minSmoothness = 0) const;
471 static Manifold Smooth(const MeshGL&,
472 const std::vector<Smoothness>& sharpenedEdges = {});
473 static Manifold Smooth(const MeshGL64&,
474 const std::vector<Smoothness>& sharpenedEdges = {});
476
480 Manifold Hull() const;
481 static Manifold Hull(const std::vector<Manifold>& manifolds);
482 static Manifold Hull(const std::vector<vec3>& pts);
484
520 static Manifold ReadOBJ(std::istream& stream);
521 bool WriteOBJ(std::ostream& stream) const;
522
527 bool MatchesTriNormals() const;
528 size_t NumDegenerateTris() const;
529 double GetEpsilon() const;
531
532 struct Impl;
533
534 private:
535 Manifold(std::shared_ptr<CsgNode> pNode_);
536 Manifold(std::shared_ptr<Impl> pImpl_);
537 static Manifold Invalid();
538 static Manifold PropagateStatus(Error status);
539 mutable std::shared_ptr<std::mutex> pNodeMutex_ =
540 std::make_shared<std::mutex>();
541 mutable std::shared_ptr<CsgNode> pNode_;
542
543 std::shared_ptr<CsgNode> LoadPNode() const;
544 CsgLeafNode& GetCsgLeafNode(ExecutionContext::Impl* ctx = nullptr) const;
545};
546
547
556#ifdef MANIFOLD_DEBUG
557inline std::string ToString(const Manifold::Error& error) {
558 switch (error) {
559 case Manifold::Error::NoError:
560 return "No Error";
561 case Manifold::Error::NonFiniteVertex:
562 return "Non Finite Vertex";
563 case Manifold::Error::NotManifold:
564 return "Not Manifold";
565 case Manifold::Error::VertexOutOfBounds:
566 return "Vertex Out Of Bounds";
567 case Manifold::Error::PropertiesWrongLength:
568 return "Properties Wrong Length";
569 case Manifold::Error::MissingPositionProperties:
570 return "Missing Position Properties";
571 case Manifold::Error::MergeVectorsDifferentLengths:
572 return "Merge Vectors Different Lengths";
573 case Manifold::Error::MergeIndexOutOfBounds:
574 return "Merge Index Out Of Bounds";
575 case Manifold::Error::TransformWrongLength:
576 return "Transform Wrong Length";
577 case Manifold::Error::RunIndexWrongLength:
578 return "Run Index Wrong Length";
579 case Manifold::Error::FaceIDWrongLength:
580 return "Face ID Wrong Length";
581 case Manifold::Error::InvalidConstruction:
582 return "Invalid Construction";
583 case Manifold::Error::ResultTooLarge:
584 return "Result Too Large";
585 case Manifold::Error::InvalidTangents:
586 return "Invalid Tangents";
587 case Manifold::Error::Cancelled:
588 return "Cancelled";
589 default:
590 return "Unknown Error";
591 };
592}
593
594inline std::ostream& operator<<(std::ostream& stream,
595 const Manifold::Error& error) {
596 return stream << ToString(error);
597}
598#endif
600} // namespace manifold
Observe and control a long-running Manifold evaluation.
Definition common.h:215
static Manifold LevelSet(std::function< double(vec3)> sdf, Box bounds, double edgeLength, double level=0, double tolerance=-1, bool canParallel=true)
Definition sdf.cpp:468
Manifold SmoothOut(double minSharpAngle=60, double minSmoothness=0) const
Definition manifold.cpp:711
std::vector< RayHit > RayCast(vec3 origin, vec3 endpoint) const
Definition manifold.cpp:1075
Manifold RefineToLength(double) const
Definition manifold.cpp:766
Manifold CalculateCurvature(int gaussianIdx, int meanIdx) const
Definition manifold.cpp:635
static Manifold BatchBoolean(const std::vector< Manifold > &manifolds, OpType op)
Definition manifold.cpp:838
double MinGap(const Manifold &other, double searchLength) const
Definition manifold.cpp:1059
static uint32_t ReserveIDs(uint32_t)
Definition manifold.cpp:436
size_t NumProp() const
Definition manifold.cpp:299
Polygons Project() const
Definition manifold.cpp:997
size_t NumPropVert() const
Definition manifold.cpp:307
static Manifold Extrude(const Polygons &crossSection, double height, int nDivisions=0, double twistDegrees=0.0, vec2 scaleTop=vec2(1.0))
Definition constructors.cpp:247
Box BoundingBox() const
Definition manifold.cpp:314
std::pair< Manifold, Manifold > SplitByPlane(vec3 normal, double originOffset) const
Definition manifold.cpp:923
Error Status() const
Definition manifold.cpp:264
Manifold MinkowskiDifference(const Manifold &) const
Definition manifold.cpp:973
int Genus() const
Definition manifold.cpp:386
Manifold & operator+=(const Manifold &)
Definition manifold.cpp:860
Manifold Warp(std::function< void(vec3 &)>) const
Definition manifold.cpp:542
Manifold Refine(int) const
Definition manifold.cpp:745
MeshGL64 GetMeshGL64(int normalIdx=-1) const
Definition manifold.cpp:249
static Manifold Sphere(double radius, int circularSegments=0)
Definition constructors.cpp:203
std::pair< Manifold, Manifold > Split(const Manifold &) const
Definition manifold.cpp:902
bool MatchesTriNormals() const
Definition manifold.cpp:445
size_t NumEdge() const
Definition manifold.cpp:289
int OriginalID() const
Definition manifold.cpp:410
double Volume() const
Definition manifold.cpp:401
Manifold Transform(const mat3x4 &) const
Definition manifold.cpp:509
MeshGL GetMeshGL(int normalIdx=-1) const
Definition manifold.cpp:228
Manifold RefineToTolerance(double) const
Definition manifold.cpp:790
Manifold WarpBatch(std::function< void(VecView< vec3 >)>) const
Definition manifold.cpp:558
Manifold Translate(vec3) const
Definition manifold.cpp:464
std::vector< Manifold > Decompose() const
Definition constructors.cpp:487
Manifold Rotate(double xDegrees, double yDegrees=0.0, double zDegrees=0.0) const
Definition manifold.cpp:497
Manifold SetTolerance(double) const
Definition manifold.cpp:339
static Manifold Cylinder(double height, double radiusLow, double radiusHigh=-1.0, int circularSegments=0, bool center=false)
Definition constructors.cpp:160
Manifold & operator^=(const Manifold &)
Definition manifold.cpp:890
static Manifold Revolve(const Polygons &crossSection, int circularSegments=0, double revolveDegrees=360.0f)
Definition constructors.cpp:336
bool IsEmpty() const
Definition manifold.cpp:257
static Manifold Compose(const std::vector< Manifold > &)
Definition constructors.cpp:478
Manifold Boolean(const Manifold &second, OpType op) const
Definition manifold.cpp:830
Manifold AsOriginal() const
Definition manifold.cpp:421
Manifold Hull() const
Definition manifold.cpp:1019
Manifold SmoothByNormals(int normalIdx) const
Definition manifold.cpp:682
Manifold Scale(vec3) const
Definition manifold.cpp:474
Manifold TrimByPlane(vec3 normal, double originOffset) const
Definition manifold.cpp:943
size_t NumTri() const
Definition manifold.cpp:295
Manifold SetProperties(int numProp, std::function< void(double *, vec3, const double *)> propFunc) const
Definition manifold.cpp:580
Manifold CalculateNormals(int normalIdx, double minSharpAngle=60) const
Definition manifold.cpp:659
double SurfaceArea() const
Definition manifold.cpp:394
size_t NumVert() const
Definition manifold.cpp:283
Polygons Slice(double height=0) const
Definition manifold.cpp:987
double GetTolerance() const
Definition manifold.cpp:331
Manifold operator^(const Manifold &) const
Definition manifold.cpp:883
double GetEpsilon() const
Definition manifold.cpp:322
static Manifold Cube(vec3 size=vec3(1.0), bool center=false)
Definition constructors.cpp:137
Manifold()
Definition manifold.cpp:121
Manifold Mirror(vec3) const
Definition manifold.cpp:521
Manifold operator-(const Manifold &) const
Definition manifold.cpp:868
Manifold MinkowskiSum(const Manifold &) const
Definition manifold.cpp:956
Manifold & operator-=(const Manifold &)
Definition manifold.cpp:875
Manifold Simplify(double tolerance=0) const
Definition manifold.cpp:364
size_t NumDegenerateTris() const
Definition manifold.cpp:454
Manifold operator+(const Manifold &) const
Definition manifold.cpp:853
static Manifold Tetrahedron()
Definition constructors.cpp:125
static Manifold Smooth(const MeshGL &, const std::vector< Smoothness > &sharpenedEdges={})
Definition constructors.cpp:83
Definition vec_view.h:32
MeshGLP< double, uint64_t > MeshGL64
Double-precision, 64-bit indices - best for huge meshes.
Definition manifold.h:274
MeshGLP< float > MeshGL
Single-precision - ideal for most uses, especially graphics.
Definition manifold.h:270
ExecutionParams & ManifoldParams()
Definition manifold.cpp:1001
Global parameters that control debugging output. Only has an effect when compiled with the MANIFOLD_D...
Definition common.h:603
OpType
Boolean operation type: Add (Union), Subtract (Difference), and Intersect.
Definition common.h:569
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:144
Axis-aligned 3D box, primarily for bounding.
Definition common.h:241
Mesh input/output suitable for pushing directly into graphics libraries.
Definition manifold.h:114
la::vec< I, 3 > GetTriVerts(size_t t) const
Definition manifold.h:224
bool Backside(size_t run) const
Definition manifold.h:262
std::vector< uint32_t > mergeFromVert
Definition manifold.h:132
I NumRun() const
Number of triangle runs.
Definition manifold.h:120
void UpdateNormals(int normalIdx)
std::vector< uint32_t > runIndex
Definition manifold.h:144
std::vector< float > vertProperties
Definition manifold.h:126
std::vector< float > runTransform
Definition manifold.h:155
std::vector< uint8_t > runFlags
Definition manifold.h:159
std::vector< uint32_t > mergeToVert
Definition manifold.h:136
I NumTri() const
Number of triangles.
Definition manifold.h:118
mat3x4 GetRunTransform(size_t run) const
Definition manifold.h:247
std::vector< float > halfedgeTangent
Definition manifold.h:170
std::vector< uint32_t > runOriginalID
Definition manifold.h:150
std::vector< uint32_t > triVerts
Definition manifold.h:129
la::vec< Precision, 4 > GetTangent(size_t h) const
Definition manifold.h:235
float tolerance
Definition manifold.h:175
I NumVert() const
Number of property vertices.
Definition manifold.h:116
std::vector< uint32_t > faceID
Definition manifold.h:165
la::vec< Precision, 3 > GetVertPos(size_t v) const
Definition manifold.h:212
uint32_t numProp
Definition manifold.h:122