Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/manifold/src/manifold.cpp
9903 views
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
#include <algorithm>
16
#include <map>
17
#include <numeric>
18
19
#include "boolean3.h"
20
#include "csg_tree.h"
21
#include "impl.h"
22
#include "parallel.h"
23
#include "shared.h"
24
25
namespace {
26
using namespace manifold;
27
28
ExecutionParams manifoldParams;
29
30
Manifold Halfspace(Box bBox, vec3 normal, double originOffset) {
31
normal = la::normalize(normal);
32
Manifold cutter = Manifold::Cube(vec3(2.0), true).Translate({1.0, 0.0, 0.0});
33
double size = la::length(bBox.Center() - normal * originOffset) +
34
0.5 * la::length(bBox.Size());
35
cutter = cutter.Scale(vec3(size)).Translate({originOffset, 0.0, 0.0});
36
double yDeg = degrees(-std::asin(normal.z));
37
double zDeg = degrees(std::atan2(normal.y, normal.x));
38
return cutter.Rotate(0.0, yDeg, zDeg);
39
}
40
41
template <typename Precision, typename I>
42
MeshGLP<Precision, I> GetMeshGLImpl(const manifold::Manifold::Impl& impl,
43
int normalIdx) {
44
ZoneScoped;
45
const int numProp = impl.NumProp();
46
const int numVert = impl.NumPropVert();
47
const int numTri = impl.NumTri();
48
49
const bool isOriginal = impl.meshRelation_.originalID >= 0;
50
const bool updateNormals = !isOriginal && normalIdx >= 0;
51
52
MeshGLP<Precision, I> out;
53
out.numProp = 3 + numProp;
54
out.tolerance = impl.tolerance_;
55
if (std::is_same<Precision, float>::value)
56
out.tolerance =
57
std::max(out.tolerance,
58
static_cast<Precision>(std::numeric_limits<float>::epsilon() *
59
impl.bBox_.Scale()));
60
out.triVerts.resize(3 * numTri);
61
62
const int numHalfedge = impl.halfedgeTangent_.size();
63
out.halfedgeTangent.resize(4 * numHalfedge);
64
for (int i = 0; i < numHalfedge; ++i) {
65
const vec4 t = impl.halfedgeTangent_[i];
66
out.halfedgeTangent[4 * i] = t.x;
67
out.halfedgeTangent[4 * i + 1] = t.y;
68
out.halfedgeTangent[4 * i + 2] = t.z;
69
out.halfedgeTangent[4 * i + 3] = t.w;
70
}
71
// Sort the triangles into runs
72
out.faceID.resize(numTri);
73
std::vector<int> triNew2Old(numTri);
74
std::iota(triNew2Old.begin(), triNew2Old.end(), 0);
75
VecView<const TriRef> triRef = impl.meshRelation_.triRef;
76
// Don't sort originals - keep them in order
77
if (!isOriginal) {
78
std::stable_sort(triNew2Old.begin(), triNew2Old.end(),
79
[triRef](int a, int b) {
80
return triRef[a].originalID == triRef[b].originalID
81
? triRef[a].meshID < triRef[b].meshID
82
: triRef[a].originalID < triRef[b].originalID;
83
});
84
}
85
86
std::vector<mat3> runNormalTransform;
87
auto addRun = [updateNormals, isOriginal](
88
MeshGLP<Precision, I>& out,
89
std::vector<mat3>& runNormalTransform, int tri,
90
const manifold::Manifold::Impl::Relation& rel) {
91
out.runIndex.push_back(3 * tri);
92
out.runOriginalID.push_back(rel.originalID);
93
if (updateNormals) {
94
runNormalTransform.push_back(NormalTransform(rel.transform) *
95
(rel.backSide ? -1.0 : 1.0));
96
}
97
if (!isOriginal) {
98
for (const int col : {0, 1, 2, 3}) {
99
for (const int row : {0, 1, 2}) {
100
out.runTransform.push_back(rel.transform[col][row]);
101
}
102
}
103
}
104
};
105
106
auto meshIDtransform = impl.meshRelation_.meshIDtransform;
107
int lastID = -1;
108
for (int tri = 0; tri < numTri; ++tri) {
109
const int oldTri = triNew2Old[tri];
110
const auto ref = triRef[oldTri];
111
const int meshID = ref.meshID;
112
113
out.faceID[tri] = ref.faceID >= 0 ? ref.faceID : ref.coplanarID;
114
for (const int i : {0, 1, 2})
115
out.triVerts[3 * tri + i] = impl.halfedge_[3 * oldTri + i].startVert;
116
117
if (meshID != lastID) {
118
manifold::Manifold::Impl::Relation rel;
119
auto it = meshIDtransform.find(meshID);
120
if (it != meshIDtransform.end()) rel = it->second;
121
addRun(out, runNormalTransform, tri, rel);
122
meshIDtransform.erase(meshID);
123
lastID = meshID;
124
}
125
}
126
// Add runs for originals that did not contribute any faces to the output
127
for (const auto& pair : meshIDtransform) {
128
addRun(out, runNormalTransform, numTri, pair.second);
129
}
130
out.runIndex.push_back(3 * numTri);
131
132
// Early return for no props
133
if (numProp == 0) {
134
out.vertProperties.resize(3 * numVert);
135
for (int i = 0; i < numVert; ++i) {
136
const vec3 v = impl.vertPos_[i];
137
out.vertProperties[3 * i] = v.x;
138
out.vertProperties[3 * i + 1] = v.y;
139
out.vertProperties[3 * i + 2] = v.z;
140
}
141
return out;
142
}
143
// Duplicate verts with different props
144
std::vector<int> vert2idx(impl.NumVert(), -1);
145
std::vector<std::vector<ivec2>> vertPropPair(impl.NumVert());
146
out.vertProperties.reserve(numVert * static_cast<size_t>(out.numProp));
147
148
for (size_t run = 0; run < out.runOriginalID.size(); ++run) {
149
for (size_t tri = out.runIndex[run] / 3; tri < out.runIndex[run + 1] / 3;
150
++tri) {
151
for (const int i : {0, 1, 2}) {
152
const int prop = impl.halfedge_[3 * triNew2Old[tri] + i].propVert;
153
const int vert = out.triVerts[3 * tri + i];
154
155
auto& bin = vertPropPair[vert];
156
bool bFound = false;
157
for (const auto& b : bin) {
158
if (b.x == prop) {
159
bFound = true;
160
out.triVerts[3 * tri + i] = b.y;
161
break;
162
}
163
}
164
if (bFound) continue;
165
const int idx = out.vertProperties.size() / out.numProp;
166
out.triVerts[3 * tri + i] = idx;
167
bin.push_back({prop, idx});
168
169
for (int p : {0, 1, 2}) {
170
out.vertProperties.push_back(impl.vertPos_[vert][p]);
171
}
172
for (int p = 0; p < numProp; ++p) {
173
out.vertProperties.push_back(impl.properties_[prop * numProp + p]);
174
}
175
176
if (updateNormals) {
177
vec3 normal;
178
const int start = out.vertProperties.size() - out.numProp;
179
for (int i : {0, 1, 2}) {
180
normal[i] = out.vertProperties[start + 3 + normalIdx + i];
181
}
182
normal = la::normalize(runNormalTransform[run] * normal);
183
for (int i : {0, 1, 2}) {
184
out.vertProperties[start + 3 + normalIdx + i] = normal[i];
185
}
186
}
187
188
if (vert2idx[vert] == -1) {
189
vert2idx[vert] = idx;
190
} else {
191
out.mergeFromVert.push_back(idx);
192
out.mergeToVert.push_back(vert2idx[vert]);
193
}
194
}
195
}
196
}
197
return out;
198
}
199
} // namespace
200
201
namespace manifold {
202
203
/**
204
* Construct an empty Manifold.
205
*
206
*/
207
Manifold::Manifold() : pNode_{std::make_shared<CsgLeafNode>()} {}
208
Manifold::~Manifold() = default;
209
Manifold::Manifold(Manifold&&) noexcept = default;
210
Manifold& Manifold::operator=(Manifold&&) noexcept = default;
211
212
Manifold::Manifold(const Manifold& other) : pNode_(other.pNode_) {}
213
214
Manifold::Manifold(std::shared_ptr<CsgNode> pNode) : pNode_(pNode) {}
215
216
Manifold::Manifold(std::shared_ptr<Impl> pImpl_)
217
: pNode_(std::make_shared<CsgLeafNode>(pImpl_)) {}
218
219
Manifold Manifold::Invalid() {
220
auto pImpl_ = std::make_shared<Impl>();
221
pImpl_->status_ = Error::InvalidConstruction;
222
return Manifold(pImpl_);
223
}
224
225
Manifold& Manifold::operator=(const Manifold& other) {
226
if (this != &other) {
227
pNode_ = other.pNode_;
228
}
229
return *this;
230
}
231
232
CsgLeafNode& Manifold::GetCsgLeafNode() const {
233
if (pNode_->GetNodeType() != CsgNodeType::Leaf) {
234
pNode_ = pNode_->ToLeafNode();
235
}
236
return *std::static_pointer_cast<CsgLeafNode>(pNode_);
237
}
238
239
/**
240
* Convert a MeshGL into a Manifold, retaining its properties and merging only
241
* the positions according to the merge vectors. Will return an empty Manifold
242
* and set an Error Status if the result is not an oriented 2-manifold. Will
243
* collapse degenerate triangles and unnecessary vertices.
244
*
245
* All fields are read, making this structure suitable for a lossless round-trip
246
* of data from GetMeshGL. For multi-material input, use ReserveIDs to set a
247
* unique originalID for each material, and sort the materials into triangle
248
* runs.
249
*
250
* @param meshGL The input MeshGL.
251
*/
252
Manifold::Manifold(const MeshGL& meshGL)
253
: pNode_(std::make_shared<CsgLeafNode>(std::make_shared<Impl>(meshGL))) {}
254
255
/**
256
* Convert a MeshGL into a Manifold, retaining its properties and merging only
257
* the positions according to the merge vectors. Will return an empty Manifold
258
* and set an Error Status if the result is not an oriented 2-manifold. Will
259
* collapse degenerate triangles and unnecessary vertices.
260
*
261
* All fields are read, making this structure suitable for a lossless round-trip
262
* of data from GetMeshGL. For multi-material input, use ReserveIDs to set a
263
* unique originalID for each material, and sort the materials into triangle
264
* runs.
265
*
266
* @param meshGL64 The input MeshGL64.
267
*/
268
Manifold::Manifold(const MeshGL64& meshGL64)
269
: pNode_(std::make_shared<CsgLeafNode>(std::make_shared<Impl>(meshGL64))) {}
270
271
/**
272
* The most complete output of this library, returning a MeshGL that is designed
273
* to easily push into a renderer, including all interleaved vertex properties
274
* that may have been input. It also includes relations to all the input meshes
275
* that form a part of this result and the transforms applied to each.
276
*
277
* @param normalIdx If the original MeshGL inputs that formed this manifold had
278
* properties corresponding to normal vectors, you can specify the first of the
279
* three consecutive property channels forming the (x, y, z) normals, which will
280
* cause this output MeshGL to automatically update these normals according to
281
* the applied transforms and front/back side. normalIdx + 3 must be <=
282
* numProp, and all original MeshGLs must use the same channels for their
283
* normals.
284
*/
285
MeshGL Manifold::GetMeshGL(int normalIdx) const {
286
const Impl& impl = *GetCsgLeafNode().GetImpl();
287
return GetMeshGLImpl<float, uint32_t>(impl, normalIdx);
288
}
289
290
/**
291
* The most complete output of this library, returning a MeshGL that is designed
292
* to easily push into a renderer, including all interleaved vertex properties
293
* that may have been input. It also includes relations to all the input meshes
294
* that form a part of this result and the transforms applied to each.
295
*
296
* @param normalIdx If the original MeshGL inputs that formed this manifold had
297
* properties corresponding to normal vectors, you can specify the first of the
298
* three consecutive property channels forming the (x, y, z) normals, which will
299
* cause this output MeshGL to automatically update these normals according to
300
* the applied transforms and front/back side. normalIdx + 3 must be <=
301
* numProp, and all original MeshGLs must use the same channels for their
302
* normals.
303
*/
304
MeshGL64 Manifold::GetMeshGL64(int normalIdx) const {
305
const Impl& impl = *GetCsgLeafNode().GetImpl();
306
return GetMeshGLImpl<double, uint64_t>(impl, normalIdx);
307
}
308
309
/**
310
* Does the Manifold have any triangles?
311
*/
312
bool Manifold::IsEmpty() const { return GetCsgLeafNode().GetImpl()->IsEmpty(); }
313
/**
314
* Returns the reason for an input Mesh producing an empty Manifold. This Status
315
* will carry on through operations like NaN propogation, ensuring an errored
316
* mesh doesn't get mysteriously lost. Empty meshes may still show
317
* NoError, for instance the intersection of non-overlapping meshes.
318
*/
319
Manifold::Error Manifold::Status() const {
320
return GetCsgLeafNode().GetImpl()->status_;
321
}
322
/**
323
* The number of vertices in the Manifold.
324
*/
325
size_t Manifold::NumVert() const {
326
return GetCsgLeafNode().GetImpl()->NumVert();
327
}
328
/**
329
* The number of edges in the Manifold.
330
*/
331
size_t Manifold::NumEdge() const {
332
return GetCsgLeafNode().GetImpl()->NumEdge();
333
}
334
/**
335
* The number of triangles in the Manifold.
336
*/
337
size_t Manifold::NumTri() const { return GetCsgLeafNode().GetImpl()->NumTri(); }
338
/**
339
* The number of properties per vertex in the Manifold.
340
*/
341
size_t Manifold::NumProp() const {
342
return GetCsgLeafNode().GetImpl()->NumProp();
343
}
344
/**
345
* The number of property vertices in the Manifold. This will always be >=
346
* NumVert, as some physical vertices may be duplicated to account for different
347
* properties on different neighboring triangles.
348
*/
349
size_t Manifold::NumPropVert() const {
350
return GetCsgLeafNode().GetImpl()->NumPropVert();
351
}
352
353
/**
354
* Returns the axis-aligned bounding box of all the Manifold's vertices.
355
*/
356
Box Manifold::BoundingBox() const { return GetCsgLeafNode().GetImpl()->bBox_; }
357
358
/**
359
* Returns the epsilon value of this Manifold's vertices, which tracks the
360
* approximate rounding error over all the transforms and operations that have
361
* led to this state. This is the value of &epsilon; defining
362
* [&epsilon;-valid](https://github.com/elalish/manifold/wiki/Manifold-Library#definition-of-%CE%B5-valid).
363
*/
364
double Manifold::GetEpsilon() const {
365
return GetCsgLeafNode().GetImpl()->epsilon_;
366
}
367
368
/**
369
* Returns the tolerance value of this Manifold. Triangles that are coplanar
370
* within tolerance tend to be merged and edges shorter than tolerance tend to
371
* be collapsed.
372
*/
373
double Manifold::GetTolerance() const {
374
return GetCsgLeafNode().GetImpl()->tolerance_;
375
}
376
377
/**
378
* Return a copy of the manifold with the set tolerance value.
379
* This performs mesh simplification when the tolerance value is increased.
380
*/
381
Manifold Manifold::SetTolerance(double tolerance) const {
382
auto impl = std::make_shared<Impl>(*GetCsgLeafNode().GetImpl());
383
if (tolerance > impl->tolerance_) {
384
impl->tolerance_ = tolerance;
385
impl->MarkCoplanar();
386
impl->SimplifyTopology();
387
impl->Finish();
388
} else {
389
// for reducing tolerance, we need to make sure it is still at least
390
// equal to epsilon.
391
impl->tolerance_ = std::max(impl->epsilon_, tolerance);
392
}
393
return Manifold(impl);
394
}
395
396
/**
397
* Return a copy of the manifold simplified to the given tolerance, but with its
398
* actual tolerance value unchanged. If the tolerance is not given or is less
399
* than the current tolerance, the current tolerance is used for simplification.
400
* The result will contain a subset of the original verts and all surfaces will
401
* have moved by less than tolerance.
402
*/
403
Manifold Manifold::Simplify(double tolerance) const {
404
auto impl = std::make_shared<Impl>(*GetCsgLeafNode().GetImpl());
405
const double oldTolerance = impl->tolerance_;
406
if (tolerance == 0) tolerance = oldTolerance;
407
if (tolerance > oldTolerance) {
408
impl->tolerance_ = tolerance;
409
impl->MarkCoplanar();
410
}
411
impl->SimplifyTopology();
412
impl->Finish();
413
impl->tolerance_ = oldTolerance;
414
return Manifold(impl);
415
}
416
417
/**
418
* The genus is a topological property of the manifold, representing the number
419
* of "handles". A sphere is 0, torus 1, etc. It is only meaningful for a single
420
* mesh, so it is best to call Decompose() first.
421
*/
422
int Manifold::Genus() const {
423
int chi = NumVert() - NumEdge() + NumTri();
424
return 1 - chi / 2;
425
}
426
427
/**
428
* Returns the surface area of the manifold.
429
*/
430
double Manifold::SurfaceArea() const {
431
return GetCsgLeafNode().GetImpl()->GetProperty(Impl::Property::SurfaceArea);
432
}
433
434
/**
435
* Returns the volume of the manifold.
436
*/
437
double Manifold::Volume() const {
438
return GetCsgLeafNode().GetImpl()->GetProperty(Impl::Property::Volume);
439
}
440
441
/**
442
* If this mesh is an original, this returns its meshID that can be referenced
443
* by product manifolds' MeshRelation. If this manifold is a product, this
444
* returns -1.
445
*/
446
int Manifold::OriginalID() const {
447
return GetCsgLeafNode().GetImpl()->meshRelation_.originalID;
448
}
449
450
/**
451
* This removes all relations (originalID, faceID, transform) to ancestor meshes
452
* and this new Manifold is marked an original. It also recreates faces
453
* - these don't get joined at boundaries where originalID changes, so the
454
* reset may allow triangles of flat faces to be further collapsed with
455
* Simplify().
456
*/
457
Manifold Manifold::AsOriginal() const {
458
auto oldImpl = GetCsgLeafNode().GetImpl();
459
if (oldImpl->status_ != Error::NoError) {
460
auto newImpl = std::make_shared<Impl>();
461
newImpl->status_ = oldImpl->status_;
462
return Manifold(std::make_shared<CsgLeafNode>(newImpl));
463
}
464
auto newImpl = std::make_shared<Impl>(*oldImpl);
465
newImpl->InitializeOriginal();
466
newImpl->MarkCoplanar();
467
newImpl->InitializeOriginal(true);
468
return Manifold(std::make_shared<CsgLeafNode>(newImpl));
469
}
470
471
/**
472
* Returns the first of n sequential new unique mesh IDs for marking sets of
473
* triangles that can be looked up after further operations. Assign to
474
* MeshGL.runOriginalID vector.
475
*/
476
uint32_t Manifold::ReserveIDs(uint32_t n) {
477
return Manifold::Impl::ReserveIDs(n);
478
}
479
480
/**
481
* The triangle normal vectors are saved over the course of operations rather
482
* than recalculated to avoid rounding error. This checks that triangles still
483
* match their normal vectors within Precision().
484
*/
485
bool Manifold::MatchesTriNormals() const {
486
return GetCsgLeafNode().GetImpl()->MatchesTriNormals();
487
}
488
489
/**
490
* The number of triangles that are colinear within Precision(). This library
491
* attempts to remove all of these, but it cannot always remove all of them
492
* without changing the mesh by too much.
493
*/
494
size_t Manifold::NumDegenerateTris() const {
495
return GetCsgLeafNode().GetImpl()->NumDegenerateTris();
496
}
497
498
/**
499
* Move this Manifold in space. This operation can be chained. Transforms are
500
* combined and applied lazily.
501
*
502
* @param v The vector to add to every vertex.
503
*/
504
Manifold Manifold::Translate(vec3 v) const {
505
return Manifold(pNode_->Translate(v));
506
}
507
508
/**
509
* Scale this Manifold in space. This operation can be chained. Transforms are
510
* combined and applied lazily.
511
*
512
* @param v The vector to multiply every vertex by per component.
513
*/
514
Manifold Manifold::Scale(vec3 v) const { return Manifold(pNode_->Scale(v)); }
515
516
/**
517
* Applies an Euler angle rotation to the manifold, first about the X axis, then
518
* Y, then Z, in degrees. We use degrees so that we can minimize rounding error,
519
* and eliminate it completely for any multiples of 90 degrees. Additionally,
520
* more efficient code paths are used to update the manifold when the transforms
521
* only rotate by multiples of 90 degrees. This operation can be chained.
522
* Transforms are combined and applied lazily.
523
*
524
* @param xDegrees First rotation, degrees about the X-axis.
525
* @param yDegrees Second rotation, degrees about the Y-axis.
526
* @param zDegrees Third rotation, degrees about the Z-axis.
527
*/
528
Manifold Manifold::Rotate(double xDegrees, double yDegrees,
529
double zDegrees) const {
530
return Manifold(pNode_->Rotate(xDegrees, yDegrees, zDegrees));
531
}
532
533
/**
534
* Transform this Manifold in space. The first three columns form a 3x3 matrix
535
* transform and the last is a translation vector. This operation can be
536
* chained. Transforms are combined and applied lazily.
537
*
538
* @param m The affine transform matrix to apply to all the vertices.
539
*/
540
Manifold Manifold::Transform(const mat3x4& m) const {
541
return Manifold(pNode_->Transform(m));
542
}
543
544
/**
545
* Mirror this Manifold over the plane described by the unit form of the given
546
* normal vector. If the length of the normal is zero, an empty Manifold is
547
* returned. This operation can be chained. Transforms are combined and applied
548
* lazily.
549
*
550
* @param normal The normal vector of the plane to be mirrored over
551
*/
552
Manifold Manifold::Mirror(vec3 normal) const {
553
if (la::length(normal) == 0.) {
554
return Manifold();
555
}
556
auto n = la::normalize(normal);
557
auto m = mat3x4(mat3(la::identity) - 2.0 * la::outerprod(n, n), vec3());
558
return Manifold(pNode_->Transform(m));
559
}
560
561
/**
562
* This function does not change the topology, but allows the vertices to be
563
* moved according to any arbitrary input function. It is easy to create a
564
* function that warps a geometrically valid object into one which overlaps, but
565
* that is not checked here, so it is up to the user to choose their function
566
* with discretion.
567
*
568
* @param warpFunc A function that modifies a given vertex position.
569
*/
570
Manifold Manifold::Warp(std::function<void(vec3&)> warpFunc) const {
571
auto oldImpl = GetCsgLeafNode().GetImpl();
572
if (oldImpl->status_ != Error::NoError) {
573
auto pImpl = std::make_shared<Impl>();
574
pImpl->status_ = oldImpl->status_;
575
return Manifold(std::make_shared<CsgLeafNode>(pImpl));
576
}
577
auto pImpl = std::make_shared<Impl>(*oldImpl);
578
pImpl->Warp(warpFunc);
579
return Manifold(std::make_shared<CsgLeafNode>(pImpl));
580
}
581
582
/**
583
* Same as Manifold::Warp but calls warpFunc with with
584
* a VecView which is roughly equivalent to std::span
585
* pointing to all vec3 elements to be modified in-place
586
*
587
* @param warpFunc A function that modifies multiple vertex positions.
588
*/
589
Manifold Manifold::WarpBatch(
590
std::function<void(VecView<vec3>)> warpFunc) const {
591
auto oldImpl = GetCsgLeafNode().GetImpl();
592
if (oldImpl->status_ != Error::NoError) {
593
auto pImpl = std::make_shared<Impl>();
594
pImpl->status_ = oldImpl->status_;
595
return Manifold(std::make_shared<CsgLeafNode>(pImpl));
596
}
597
auto pImpl = std::make_shared<Impl>(*oldImpl);
598
pImpl->WarpBatch(warpFunc);
599
return Manifold(std::make_shared<CsgLeafNode>(pImpl));
600
}
601
602
/**
603
* Create a new copy of this manifold with updated vertex properties by
604
* supplying a function that takes the existing position and properties as
605
* input. You may specify any number of output properties, allowing creation and
606
* removal of channels. Note: undefined behavior will result if you read past
607
* the number of input properties or write past the number of output properties.
608
*
609
* If propFunc is a nullptr, this function will just set the channel to zeroes.
610
*
611
* @param numProp The new number of properties per vertex.
612
* @param propFunc A function that modifies the properties of a given vertex.
613
*/
614
Manifold Manifold::SetProperties(
615
int numProp,
616
std::function<void(double* newProp, vec3 position, const double* oldProp)>
617
propFunc) const {
618
auto pImpl = std::make_shared<Impl>(*GetCsgLeafNode().GetImpl());
619
const int oldNumProp = NumProp();
620
const Vec<double> oldProperties = pImpl->properties_;
621
622
if (numProp == 0) {
623
pImpl->properties_.clear();
624
} else {
625
pImpl->properties_ = Vec<double>(numProp * NumPropVert(), 0);
626
for_each_n(
627
propFunc == nullptr ? ExecutionPolicy::Par : ExecutionPolicy::Seq,
628
countAt(0), NumTri(), [&](int tri) {
629
for (int i : {0, 1, 2}) {
630
const Halfedge& edge = pImpl->halfedge_[3 * tri + i];
631
const int vert = edge.startVert;
632
const int propVert = edge.propVert;
633
if (propFunc == nullptr) {
634
for (int p = 0; p < numProp; ++p) {
635
pImpl->properties_[numProp * propVert + p] = 0;
636
}
637
} else {
638
propFunc(&pImpl->properties_[numProp * propVert],
639
pImpl->vertPos_[vert],
640
oldProperties.data() + oldNumProp * propVert);
641
}
642
}
643
});
644
}
645
646
pImpl->numProp_ = numProp;
647
return Manifold(std::make_shared<CsgLeafNode>(pImpl));
648
}
649
650
/**
651
* Curvature is the inverse of the radius of curvature, and signed such that
652
* positive is convex and negative is concave. There are two orthogonal
653
* principal curvatures at any point on a manifold, with one maximum and the
654
* other minimum. Gaussian curvature is their product, while mean
655
* curvature is their sum. This approximates them for every vertex and assigns
656
* them as vertex properties on the given channels.
657
*
658
* @param gaussianIdx The property channel index in which to store the Gaussian
659
* curvature. An index < 0 will be ignored (stores nothing). The property set
660
* will be automatically expanded to include the channel index specified.
661
*
662
* @param meanIdx The property channel index in which to store the mean
663
* curvature. An index < 0 will be ignored (stores nothing). The property set
664
* will be automatically expanded to include the channel index specified.
665
*/
666
Manifold Manifold::CalculateCurvature(int gaussianIdx, int meanIdx) const {
667
auto pImpl = std::make_shared<Impl>(*GetCsgLeafNode().GetImpl());
668
pImpl->CalculateCurvature(gaussianIdx, meanIdx);
669
return Manifold(std::make_shared<CsgLeafNode>(pImpl));
670
}
671
672
/**
673
* Fills in vertex properties for normal vectors, calculated from the mesh
674
* geometry. Flat faces composed of three or more triangles will remain flat.
675
*
676
* @param normalIdx The property channel in which to store the X
677
* values of the normals. The X, Y, and Z channels will be sequential. The
678
* property set will be automatically expanded such that NumProp will be at
679
* least normalIdx + 3.
680
*
681
* @param minSharpAngle Any edges with angles greater than this value will
682
* remain sharp, getting different normal vector properties on each side of the
683
* edge. By default, no edges are sharp and all normals are shared. With a value
684
* of zero, the model is faceted and all normals match their triangle normals,
685
* but in this case it would be better not to calculate normals at all.
686
*/
687
Manifold Manifold::CalculateNormals(int normalIdx, double minSharpAngle) const {
688
auto pImpl = std::make_shared<Impl>(*GetCsgLeafNode().GetImpl());
689
pImpl->SetNormals(normalIdx, minSharpAngle);
690
return Manifold(std::make_shared<CsgLeafNode>(pImpl));
691
}
692
693
/**
694
* Smooths out the Manifold by filling in the halfedgeTangent vectors. The
695
* geometry will remain unchanged until Refine or RefineToLength is called to
696
* interpolate the surface. This version uses the supplied vertex normal
697
* properties to define the tangent vectors. Faces of two coplanar triangles
698
* will be marked as quads, while faces with three or more will be flat.
699
*
700
* @param normalIdx The first property channel of the normals. NumProp must be
701
* at least normalIdx + 3. Any vertex where multiple normals exist and don't
702
* agree will result in a sharp edge.
703
*/
704
Manifold Manifold::SmoothByNormals(int normalIdx) const {
705
auto pImpl = std::make_shared<Impl>(*GetCsgLeafNode().GetImpl());
706
if (!IsEmpty()) {
707
pImpl->CreateTangents(normalIdx);
708
}
709
return Manifold(std::make_shared<CsgLeafNode>(pImpl));
710
}
711
712
/**
713
* Smooths out the Manifold by filling in the halfedgeTangent vectors. The
714
* geometry will remain unchanged until Refine or RefineToLength is called to
715
* interpolate the surface. This version uses the geometry of the triangles and
716
* pseudo-normals to define the tangent vectors. Faces of two coplanar triangles
717
* will be marked as quads.
718
*
719
* @param minSharpAngle degrees, default 60. Any edges with angles greater than
720
* this value will remain sharp. The rest will be smoothed to G1 continuity,
721
* with the caveat that flat faces of three or more triangles will always remain
722
* flat. With a value of zero, the model is faceted, but in this case there is
723
* no point in smoothing.
724
*
725
* @param minSmoothness range: 0 - 1, default 0. The smoothness applied to sharp
726
* angles. The default gives a hard edge, while values > 0 will give a small
727
* fillet on these sharp edges. A value of 1 is equivalent to a minSharpAngle of
728
* 180 - all edges will be smooth.
729
*/
730
Manifold Manifold::SmoothOut(double minSharpAngle, double minSmoothness) const {
731
auto pImpl = std::make_shared<Impl>(*GetCsgLeafNode().GetImpl());
732
if (!IsEmpty()) {
733
if (minSmoothness == 0) {
734
const int numProp = pImpl->numProp_;
735
Vec<double> properties = pImpl->properties_;
736
Vec<Halfedge> halfedge = pImpl->halfedge_;
737
pImpl->SetNormals(0, minSharpAngle);
738
pImpl->CreateTangents(0);
739
// Reset the properties to the original values, removing temporary normals
740
pImpl->numProp_ = numProp;
741
pImpl->properties_.swap(properties);
742
pImpl->halfedge_.swap(halfedge);
743
} else {
744
pImpl->CreateTangents(pImpl->SharpenEdges(minSharpAngle, minSmoothness));
745
}
746
}
747
return Manifold(std::make_shared<CsgLeafNode>(pImpl));
748
}
749
750
/**
751
* Increase the density of the mesh by splitting every edge into n pieces. For
752
* instance, with n = 2, each triangle will be split into 4 triangles. Quads
753
* will ignore their interior triangle bisector. These will all be coplanar (and
754
* will not be immediately collapsed) unless the Mesh/Manifold has
755
* halfedgeTangents specified (e.g. from the Smooth() constructor), in which
756
* case the new vertices will be moved to the interpolated surface according to
757
* their barycentric coordinates.
758
*
759
* @param n The number of pieces to split every edge into. Must be > 1.
760
*/
761
Manifold Manifold::Refine(int n) const {
762
auto pImpl = std::make_shared<Impl>(*GetCsgLeafNode().GetImpl());
763
if (n > 1) {
764
pImpl->Refine([n](vec3, vec4, vec4) { return n - 1; });
765
}
766
return Manifold(std::make_shared<CsgLeafNode>(pImpl));
767
}
768
769
/**
770
* Increase the density of the mesh by splitting each edge into pieces of
771
* roughly the input length. Interior verts are added to keep the rest of the
772
* triangulation edges also of roughly the same length. If halfedgeTangents are
773
* present (e.g. from the Smooth() constructor), the new vertices will be moved
774
* to the interpolated surface according to their barycentric coordinates. Quads
775
* will ignore their interior triangle bisector.
776
*
777
* @param length The length that edges will be broken down to.
778
*/
779
Manifold Manifold::RefineToLength(double length) const {
780
length = std::abs(length);
781
auto pImpl = std::make_shared<Impl>(*GetCsgLeafNode().GetImpl());
782
pImpl->Refine([length](vec3 edge, vec4, vec4) {
783
return static_cast<int>(la::length(edge) / length);
784
});
785
return Manifold(std::make_shared<CsgLeafNode>(pImpl));
786
}
787
788
/**
789
* Increase the density of the mesh by splitting each edge into pieces such that
790
* any point on the resulting triangles is roughly within tolerance of the
791
* smoothly curved surface defined by the tangent vectors. This means tightly
792
* curving regions will be divided more finely than smoother regions. If
793
* halfedgeTangents are not present, the result will simply be a copy of the
794
* original. Quads will ignore their interior triangle bisector.
795
*
796
* @param tolerance The desired maximum distance between the faceted mesh
797
* produced and the exact smoothly curving surface. All vertices are exactly on
798
* the surface, within rounding error.
799
*/
800
Manifold Manifold::RefineToTolerance(double tolerance) const {
801
tolerance = std::abs(tolerance);
802
auto pImpl = std::make_shared<Impl>(*GetCsgLeafNode().GetImpl());
803
if (!pImpl->halfedgeTangent_.empty()) {
804
pImpl->Refine(
805
[tolerance](vec3 edge, vec4 tangentStart, vec4 tangentEnd) {
806
const vec3 edgeNorm = la::normalize(edge);
807
// Weight heuristic
808
const vec3 tStart = vec3(tangentStart);
809
const vec3 tEnd = vec3(tangentEnd);
810
// Perpendicular to edge
811
const vec3 start = tStart - edgeNorm * la::dot(edgeNorm, tStart);
812
const vec3 end = tEnd - edgeNorm * la::dot(edgeNorm, tEnd);
813
// Circular arc result plus heuristic term for non-circular curves
814
const double d = 0.5 * (la::length(start) + la::length(end)) +
815
la::length(start - end);
816
return static_cast<int>(std::sqrt(3 * d / (4 * tolerance)));
817
},
818
true);
819
}
820
return Manifold(std::make_shared<CsgLeafNode>(pImpl));
821
}
822
823
/**
824
* The central operation of this library: the Boolean combines two manifolds
825
* into another by calculating their intersections and removing the unused
826
* portions.
827
* [&epsilon;-valid](https://github.com/elalish/manifold/wiki/Manifold-Library#definition-of-%CE%B5-valid)
828
* inputs will produce &epsilon;-valid output. &epsilon;-invalid input may fail
829
* triangulation.
830
*
831
* These operations are optimized to produce nearly-instant results if either
832
* input is empty or their bounding boxes do not overlap.
833
*
834
* @param second The other Manifold.
835
* @param op The type of operation to perform.
836
*/
837
Manifold Manifold::Boolean(const Manifold& second, OpType op) const {
838
return Manifold(pNode_->Boolean(second.pNode_, op));
839
}
840
841
/**
842
* Perform the given boolean operation on a list of Manifolds. In case of
843
* Subtract, all Manifolds in the tail are differenced from the head.
844
*/
845
Manifold Manifold::BatchBoolean(const std::vector<Manifold>& manifolds,
846
OpType op) {
847
if (manifolds.size() == 0)
848
return Manifold();
849
else if (manifolds.size() == 1)
850
return manifolds[0];
851
std::vector<std::shared_ptr<CsgNode>> children;
852
children.reserve(manifolds.size());
853
for (const auto& m : manifolds) children.push_back(m.pNode_);
854
return Manifold(std::make_shared<CsgOpNode>(children, op));
855
}
856
857
/**
858
* Shorthand for Boolean Union.
859
*/
860
Manifold Manifold::operator+(const Manifold& Q) const {
861
return Boolean(Q, OpType::Add);
862
}
863
864
/**
865
* Shorthand for Boolean Union assignment.
866
*/
867
Manifold& Manifold::operator+=(const Manifold& Q) {
868
*this = *this + Q;
869
return *this;
870
}
871
872
/**
873
* Shorthand for Boolean Difference.
874
*/
875
Manifold Manifold::operator-(const Manifold& Q) const {
876
return Boolean(Q, OpType::Subtract);
877
}
878
879
/**
880
* Shorthand for Boolean Difference assignment.
881
*/
882
Manifold& Manifold::operator-=(const Manifold& Q) {
883
*this = *this - Q;
884
return *this;
885
}
886
887
/**
888
* Shorthand for Boolean Intersection.
889
*/
890
Manifold Manifold::operator^(const Manifold& Q) const {
891
return Boolean(Q, OpType::Intersect);
892
}
893
894
/**
895
* Shorthand for Boolean Intersection assignment.
896
*/
897
Manifold& Manifold::operator^=(const Manifold& Q) {
898
*this = *this ^ Q;
899
return *this;
900
}
901
902
/**
903
* Split cuts this manifold in two using the cutter manifold. The first result
904
* is the intersection, second is the difference. This is more efficient than
905
* doing them separately.
906
*
907
* @param cutter
908
*/
909
std::pair<Manifold, Manifold> Manifold::Split(const Manifold& cutter) const {
910
auto impl1 = GetCsgLeafNode().GetImpl();
911
auto impl2 = cutter.GetCsgLeafNode().GetImpl();
912
913
Boolean3 boolean(*impl1, *impl2, OpType::Subtract);
914
auto result1 = std::make_shared<CsgLeafNode>(
915
std::make_unique<Impl>(boolean.Result(OpType::Intersect)));
916
auto result2 = std::make_shared<CsgLeafNode>(
917
std::make_unique<Impl>(boolean.Result(OpType::Subtract)));
918
return std::make_pair(Manifold(result1), Manifold(result2));
919
}
920
921
/**
922
* Convenient version of Split() for a half-space.
923
*
924
* @param normal This vector is normal to the cutting plane and its length does
925
* not matter. The first result is in the direction of this vector, the second
926
* result is on the opposite side.
927
* @param originOffset The distance of the plane from the origin in the
928
* direction of the normal vector.
929
*/
930
std::pair<Manifold, Manifold> Manifold::SplitByPlane(
931
vec3 normal, double originOffset) const {
932
return Split(Halfspace(BoundingBox(), normal, originOffset));
933
}
934
935
/**
936
* Identical to SplitByPlane(), but calculating and returning only the first
937
* result.
938
*
939
* @param normal This vector is normal to the cutting plane and its length does
940
* not matter. The result is in the direction of this vector from the plane.
941
* @param originOffset The distance of the plane from the origin in the
942
* direction of the normal vector.
943
*/
944
Manifold Manifold::TrimByPlane(vec3 normal, double originOffset) const {
945
return *this ^ Halfspace(BoundingBox(), normal, originOffset);
946
}
947
948
/**
949
* Returns the cross section of this object parallel to the X-Y plane at the
950
* specified Z height, defaulting to zero. Using a height equal to the bottom of
951
* the bounding box will return the bottom faces, while using a height equal to
952
* the top of the bounding box will return empty.
953
*/
954
Polygons Manifold::Slice(double height) const {
955
return GetCsgLeafNode().GetImpl()->Slice(height);
956
}
957
958
/**
959
* Returns polygons representing the projected outline of this object
960
* onto the X-Y plane. These polygons will often self-intersect, so it is
961
* recommended to run them through the positive fill rule of CrossSection to get
962
* a sensible result before using them.
963
*/
964
Polygons Manifold::Project() const {
965
return GetCsgLeafNode().GetImpl()->Project();
966
}
967
968
ExecutionParams& ManifoldParams() { return manifoldParams; }
969
970
/**
971
* Compute the convex hull of a set of points. If the given points are fewer
972
* than 4, or they are all coplanar, an empty Manifold will be returned.
973
*
974
* @param pts A vector of 3-dimensional points over which to compute a convex
975
* hull.
976
*/
977
Manifold Manifold::Hull(const std::vector<vec3>& pts) {
978
std::shared_ptr<Impl> impl = std::make_shared<Impl>();
979
impl->Hull(Vec<vec3>(pts));
980
return Manifold(std::make_shared<CsgLeafNode>(impl));
981
}
982
983
/**
984
* Compute the convex hull of this manifold.
985
*/
986
Manifold Manifold::Hull() const {
987
std::shared_ptr<Impl> impl = std::make_shared<Impl>();
988
impl->Hull(GetCsgLeafNode().GetImpl()->vertPos_);
989
return Manifold(std::make_shared<CsgLeafNode>(impl));
990
}
991
992
/**
993
* Compute the convex hull enveloping a set of manifolds.
994
*
995
* @param manifolds A vector of manifolds over which to compute a convex hull.
996
*/
997
Manifold Manifold::Hull(const std::vector<Manifold>& manifolds) {
998
return Compose(manifolds).Hull();
999
}
1000
1001
/**
1002
* Returns the minimum gap between two manifolds. Returns a double between
1003
* 0 and searchLength.
1004
*
1005
* @param other The other manifold to compute the minimum gap to.
1006
* @param searchLength The maximum distance to search for a minimum gap.
1007
*/
1008
double Manifold::MinGap(const Manifold& other, double searchLength) const {
1009
auto intersect = *this ^ other;
1010
if (!intersect.IsEmpty()) return 0.0;
1011
1012
return GetCsgLeafNode().GetImpl()->MinGap(*other.GetCsgLeafNode().GetImpl(),
1013
searchLength);
1014
}
1015
} // namespace manifold
1016
1017