-
Notifications
You must be signed in to change notification settings - Fork 68
/
impl.cu
638 lines (557 loc) · 21.6 KB
/
impl.cu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
// Copyright 2021 Emmett Lalish
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <thrust/execution_policy.h>
#include <thrust/logical.h>
#include <algorithm>
#include <boost/config.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/connected_components.hpp>
#include <map>
#include "impl.cuh"
namespace {
using namespace manifold;
__host__ __device__ void AtomicAddVec3(glm::vec3& target,
const glm::vec3& add) {
for (int i : {0, 1, 2}) {
#ifdef __CUDA_ARCH__
atomicAdd(&target[i], add[i]);
#else
#pragma omp atomic
target[i] += add[i];
#endif
}
}
struct Normalize {
__host__ __device__ void operator()(glm::vec3& v) { v = SafeNormalize(v); }
};
struct Transform4x3 {
const glm::mat4x3 transform;
__host__ __device__ void operator()(glm::vec3& position) {
position = transform * glm::vec4(position, 1.0f);
}
};
struct TransformNormals {
const glm::mat3 transform;
__host__ __device__ void operator()(glm::vec3& normal) {
normal = glm::normalize(transform * normal);
if (isnan(normal.x)) normal = glm::vec3(0.0f);
}
};
struct AssignNormals {
glm::vec3* vertNormal;
const glm::vec3* vertPos;
const Halfedge* halfedges;
const float precision;
const bool calculateTriNormal;
__host__ __device__ void operator()(thrust::tuple<glm::vec3&, int> in) {
glm::vec3& triNormal = thrust::get<0>(in);
const int face = thrust::get<1>(in);
glm::ivec3 triVerts;
for (int i : {0, 1, 2}) triVerts[i] = halfedges[3 * face + i].startVert;
glm::vec3 edge[3];
for (int i : {0, 1, 2}) {
const int j = (i + 1) % 3;
edge[i] = glm::normalize(vertPos[triVerts[j]] - vertPos[triVerts[i]]);
}
if (calculateTriNormal) {
triNormal = glm::normalize(glm::cross(edge[0], edge[1]));
if (isnan(triNormal.x)) triNormal = glm::vec3(0, 0, 1);
}
// corner angles
glm::vec3 phi;
float dot = -glm::dot(edge[2], edge[0]);
phi[0] = dot >= 1 ? 0 : (dot <= -1 ? glm::pi<float>() : glm::acos(dot));
dot = -glm::dot(edge[0], edge[1]);
phi[1] = dot >= 1 ? 0 : (dot <= -1 ? glm::pi<float>() : glm::acos(dot));
phi[2] = glm::pi<float>() - phi[0] - phi[1];
// assign weighted sum
for (int i : {0, 1, 2}) {
AtomicAddVec3(vertNormal[triVerts[i]], phi[i] * triNormal);
}
}
};
struct Tri2Halfedges {
Halfedge* halfedges;
TmpEdge* edges;
__host__ __device__ void operator()(
thrust::tuple<int, const glm::ivec3&> in) {
const int tri = thrust::get<0>(in);
const glm::ivec3& triVerts = thrust::get<1>(in);
for (const int i : {0, 1, 2}) {
const int j = (i + 1) % 3;
const int edge = 3 * tri + i;
halfedges[edge] = {triVerts[i], triVerts[j], -1, tri};
edges[edge] = TmpEdge(triVerts[i], triVerts[j], edge);
}
}
};
struct LinkHalfedges {
Halfedge* halfedges;
const TmpEdge* edges;
__host__ __device__ void operator()(int k) {
const int i = 2 * k;
const int j = i + 1;
const int pair0 = edges[i].halfedgeIdx;
const int pair1 = edges[j].halfedgeIdx;
halfedges[pair0].pairedHalfedge = pair1;
halfedges[pair1].pairedHalfedge = pair0;
}
};
struct SwapHalfedges {
Halfedge* halfedges;
const TmpEdge* edges;
__host__ void operator()(int k) {
const int i = 2 * k;
const int j = i - 2;
const TmpEdge thisEdge = edges[i];
const TmpEdge lastEdge = edges[j];
if (thisEdge.first == lastEdge.first &&
thisEdge.second == lastEdge.second) {
const int swap0idx = thisEdge.halfedgeIdx;
Halfedge& swap0 = halfedges[swap0idx];
const int swap1idx = swap0.pairedHalfedge;
Halfedge& swap1 = halfedges[swap1idx];
const int next0idx = swap0idx + ((swap0idx + 1) % 3 == 0 ? -2 : 1);
const int next1idx = swap1idx + ((swap1idx + 1) % 3 == 0 ? -2 : 1);
Halfedge& next0 = halfedges[next0idx];
Halfedge& next1 = halfedges[next1idx];
next0.startVert = swap0.endVert = next1.endVert;
swap0.pairedHalfedge = next1.pairedHalfedge;
halfedges[swap0.pairedHalfedge].pairedHalfedge = swap0idx;
next1.startVert = swap1.endVert = next0.endVert;
swap1.pairedHalfedge = next0.pairedHalfedge;
halfedges[swap1.pairedHalfedge].pairedHalfedge = swap1idx;
next0.pairedHalfedge = next1idx;
next1.pairedHalfedge = next0idx;
}
}
};
struct InitializeBaryRef {
const int meshID;
const Halfedge* halfedge;
__host__ __device__ void operator()(thrust::tuple<BaryRef&, int> inOut) {
BaryRef& baryRef = thrust::get<0>(inOut);
int tri = thrust::get<1>(inOut);
// Leave existing meshID if input is negative
if (meshID >= 0) baryRef.meshID = meshID;
baryRef.tri = tri;
baryRef.vertBary = {-3, -2, -1};
}
};
struct CheckProperties {
const int numSets;
__host__ __device__ bool operator()(glm::ivec3 triProp) {
bool good = true;
for (int i : {0, 1, 2}) good &= (triProp[i] >= 0 && triProp[i] < numSets);
return good;
}
};
struct CoplanarEdge {
float* triArea;
const Halfedge* halfedge;
const glm::vec3* vertPos;
const glm::ivec3* triProp;
const float* prop;
const float* propTol;
const int numProp;
const float precision;
__host__ __device__ void operator()(
thrust::tuple<thrust::pair<int, int>&, int> inOut) {
thrust::pair<int, int>& face2face = thrust::get<0>(inOut);
const int edgeIdx = thrust::get<1>(inOut);
const Halfedge edge = halfedge[edgeIdx];
if (!edge.IsForward()) return;
const Halfedge pair = halfedge[edge.pairedHalfedge];
const glm::vec3 base = vertPos[edge.startVert];
const int baseNum = edgeIdx - 3 * edge.face;
const int jointNum = edge.pairedHalfedge - 3 * pair.face;
const int edgeNum = baseNum == 0 ? 2 : baseNum - 1;
const int pairNum = jointNum == 0 ? 2 : jointNum - 1;
const glm::vec3 jointVec = vertPos[pair.startVert] - base;
const glm::vec3 edgeVec =
vertPos[halfedge[3 * edge.face + edgeNum].startVert] - base;
const glm::vec3 pairVec =
vertPos[halfedge[3 * pair.face + pairNum].startVert] - base;
const float length = glm::max(glm::length(jointVec), glm::length(edgeVec));
const float lengthPair =
glm::max(glm::length(jointVec), glm::length(pairVec));
glm::vec3 normal = glm::cross(jointVec, edgeVec);
const float area = glm::length(normal);
const float areaPair = glm::length(glm::cross(pairVec, jointVec));
// Don't link degenerate triangles
if (area < length * precision || areaPair < lengthPair * precision) return;
const float volume = glm::abs(glm::dot(normal, pairVec));
// Only operate on coplanar triangles
if (volume > glm::max(area, areaPair) * precision) return;
// Check property linearity
if (area > 0) {
normal /= area;
for (int i = 0; i < numProp; ++i) {
const float scale = precision / propTol[i];
const float baseProp = prop[numProp * triProp[edge.face][baseNum] + i];
const float jointProp =
prop[numProp * triProp[pair.face][jointNum] + i];
const float edgeProp = prop[numProp * triProp[edge.face][edgeNum] + i];
const float pairProp = prop[numProp * triProp[pair.face][pairNum] + i];
const glm::vec3 iJointVec =
jointVec + normal * scale * (jointProp - baseProp);
const glm::vec3 iEdgeVec =
edgeVec + normal * scale * (edgeProp - baseProp);
const glm::vec3 iPairVec =
pairVec + normal * scale * (pairProp - baseProp);
glm::vec3 cross = glm::cross(iJointVec, iEdgeVec);
const float area = glm::max(
glm::length(cross), glm::length(glm::cross(iPairVec, iJointVec)));
const float volume = glm::abs(glm::dot(cross, iPairVec));
// Only operate on consistent triangles
if (volume > area * precision) return;
}
}
triArea[edge.face] = area;
triArea[pair.face] = areaPair;
face2face.first = edge.face;
face2face.second = pair.face;
}
};
struct EdgeBox {
const glm::vec3* vertPos;
__host__ __device__ void operator()(
thrust::tuple<Box&, const TmpEdge&> inout) {
const TmpEdge& edge = thrust::get<1>(inout);
thrust::get<0>(inout) = Box(vertPos[edge.first], vertPos[edge.second]);
}
};
} // namespace
namespace manifold {
std::vector<int> Manifold::Impl::meshID2Original_;
/**
* Create a manifold from an input triangle Mesh. Will throw if the Mesh is not
* manifold. TODO: update halfedgeTangent during SimplifyTopology.
*/
Manifold::Impl::Impl(const Mesh& mesh,
const std::vector<glm::ivec3>& triProperties,
const std::vector<float>& properties,
const std::vector<float>& propertyTolerance)
: vertPos_(mesh.vertPos), halfedgeTangent_(mesh.halfedgeTangent) {
CheckDevice();
CalculateBBox();
SetPrecision();
CreateAndFixHalfedges(mesh.triVerts);
ALWAYS_ASSERT(IsManifold(), topologyErr, "Input mesh is not manifold!");
CalculateNormals();
InitializeNewReference(triProperties, properties, propertyTolerance);
SimplifyTopology();
Finish();
}
/**
* Create eiter a unit tetrahedron, cube or octahedron. The cube is in the first
* octant, while the others are symmetric about the origin.
*/
Manifold::Impl::Impl(Shape shape) {
std::vector<glm::vec3> vertPos;
std::vector<glm::ivec3> triVerts;
switch (shape) {
case Shape::TETRAHEDRON:
vertPos = {{-1.0f, -1.0f, 1.0f},
{-1.0f, 1.0f, -1.0f},
{1.0f, -1.0f, -1.0f},
{1.0f, 1.0f, 1.0f}};
triVerts = {{2, 0, 1}, {0, 3, 1}, {2, 3, 0}, {3, 2, 1}};
break;
case Shape::CUBE:
vertPos = {{0.0f, 0.0f, 0.0f}, //
{1.0f, 0.0f, 0.0f}, //
{1.0f, 1.0f, 0.0f}, //
{0.0f, 1.0f, 0.0f}, //
{0.0f, 0.0f, 1.0f}, //
{1.0f, 0.0f, 1.0f}, //
{1.0f, 1.0f, 1.0f}, //
{0.0f, 1.0f, 1.0f}};
triVerts = {{0, 2, 1}, {0, 3, 2}, //
{4, 5, 6}, {4, 6, 7}, //
{0, 1, 5}, {0, 5, 4}, //
{1, 2, 6}, {1, 6, 5}, //
{2, 3, 7}, {2, 7, 6}, //
{3, 0, 4}, {3, 4, 7}};
break;
case Shape::OCTAHEDRON:
vertPos = {{1.0f, 0.0f, 0.0f}, //
{-1.0f, 0.0f, 0.0f}, //
{0.0f, 1.0f, 0.0f}, //
{0.0f, -1.0f, 0.0f}, //
{0.0f, 0.0f, 1.0f}, //
{0.0f, 0.0f, -1.0f}};
triVerts = {{0, 2, 4}, {1, 5, 3}, //
{2, 1, 4}, {3, 5, 0}, //
{1, 3, 4}, {0, 5, 2}, //
{3, 0, 4}, {2, 5, 1}};
break;
default:
throw userErr("Unrecognized shape!");
}
vertPos_ = vertPos;
CreateHalfedges(triVerts);
Finish();
InitializeNewReference();
}
/**
* When a manifold is copied, it is given a new unique set of mesh relation IDs,
* identifying a particular instance of a copied input mesh. The original mesh
* ID can be found using the meshID2Original mapping.
*/
void Manifold::Impl::DuplicateMeshIDs() {
std::map<int, int> old2new;
for (BaryRef& ref : meshRelation_.triBary) {
if (old2new.find(ref.meshID) == old2new.end()) {
old2new[ref.meshID] = meshID2Original_.size();
meshID2Original_.push_back(meshID2Original_[ref.meshID]);
}
ref.meshID = old2new[ref.meshID];
}
}
void Manifold::Impl::ReinitializeReference(int meshID) {
thrust::for_each_n(zip(meshRelation_.triBary.beginD(), countAt(0)), NumTri(),
InitializeBaryRef({meshID, halfedge_.cptrD()}));
}
int Manifold::Impl::InitializeNewReference(
const std::vector<glm::ivec3>& triProperties,
const std::vector<float>& properties,
const std::vector<float>& propertyTolerance) {
meshRelation_.triBary.resize(NumTri());
const int nextMeshID = meshID2Original_.size();
meshID2Original_.push_back(nextMeshID);
ReinitializeReference(nextMeshID);
const int numProps = propertyTolerance.size();
VecDH<glm::ivec3> triPropertiesD(triProperties);
VecDH<float> propertiesD(properties);
VecDH<float> propertyToleranceD(propertyTolerance);
if (numProps > 0) {
ALWAYS_ASSERT(
triProperties.size() == NumTri() || triProperties.size() == 0, userErr,
"If specified, triProperties vector length must match NumTri().");
ALWAYS_ASSERT(properties.size() % numProps == 0, userErr,
"properties vector must be a multiple of the size of "
"propertyTolerance.");
const int numSets = properties.size() / numProps;
ALWAYS_ASSERT(thrust::all_of(triPropertiesD.beginD(), triPropertiesD.endD(),
CheckProperties({numSets})),
userErr,
"triProperties value is outside the properties range.");
}
VecDH<thrust::pair<int, int>> face2face(halfedge_.size(), {-1, -1});
VecDH<float> triArea(NumTri());
thrust::for_each_n(
zip(face2face.beginD(), countAt(0)), halfedge_.size(),
CoplanarEdge({triArea.ptrD(), halfedge_.cptrD(), vertPos_.cptrD(),
triPropertiesD.cptrD(), propertiesD.cptrD(),
propertyToleranceD.cptrD(), numProps, precision_}));
boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS> graph(
NumTri());
for (int i = 0; i < face2face.size(); ++i) {
const thrust::pair<int, int> edge = face2face.H()[i];
if (edge.first < 0) continue;
boost::add_edge(edge.first, edge.second, graph);
}
std::vector<int> components(NumTri());
const int numComponent =
boost::connected_components(graph, components.data());
std::vector<int> comp2tri(numComponent, -1);
for (int tri = 0; tri < NumTri(); ++tri) {
const int comp = components[tri];
const int current = comp2tri[comp];
if (current < 0 || triArea.H()[tri] > triArea.H()[current]) {
comp2tri[comp] = tri;
triArea.H()[comp] = triArea.H()[tri];
}
}
VecH<BaryRef>& triBary = meshRelation_.triBary.H();
std::map<std::pair<int, int>, int> triVert2bary;
for (int tri = 0; tri < NumTri(); ++tri) {
const int refTri = comp2tri[components[tri]];
if (refTri == tri) continue;
glm::mat3 triPos;
for (int i : {0, 1, 2}) {
const int vert = halfedge_.H()[3 * refTri + i].startVert;
triPos[i] = vertPos_.H()[vert];
triVert2bary[{refTri, vert}] = i - 3;
}
glm::ivec3 vertBary;
bool coplanar = true;
for (int i : {0, 1, 2}) {
const int vert = halfedge_.H()[3 * tri + i].startVert;
if (triVert2bary.find({refTri, vert}) == triVert2bary.end()) {
const glm::vec3 uvw =
GetBarycentric(vertPos_.H()[vert], triPos, precision_);
if (isnan(uvw[0])) {
coplanar = false;
triVert2bary[{refTri, vert}] = -4;
break;
}
triVert2bary[{refTri, vert}] = meshRelation_.barycentric.size();
meshRelation_.barycentric.H().push_back(uvw);
}
const int bary = triVert2bary[{refTri, vert}];
if (bary < -3) {
coplanar = false;
break;
}
vertBary[i] = bary;
}
if (coplanar) {
BaryRef& ref = triBary[tri];
ref.tri = refTri;
ref.vertBary = vertBary;
}
}
return nextMeshID;
}
/**
* Create the halfedge_ data structure from an input triVerts array like Mesh.
*/
void Manifold::Impl::CreateHalfedges(const VecDH<glm::ivec3>& triVerts) {
const int numTri = triVerts.size();
halfedge_.resize(3 * numTri);
VecDH<TmpEdge> edge(3 * numTri);
thrust::for_each_n(zip(countAt(0), triVerts.beginD()), numTri,
Tri2Halfedges({halfedge_.ptrD(), edge.ptrD()}));
thrust::sort(edge.beginD(), edge.endD());
thrust::for_each_n(countAt(0), halfedge_.size() / 2,
LinkHalfedges({halfedge_.ptrD(), edge.cptrD()}));
}
/**
* Create the halfedge_ data structure from an input triVerts array like Mesh.
* Check that the input is an even-manifold, and if it is not 2-manifold,
* perform edge swaps until it is. This is a host function.
*/
void Manifold::Impl::CreateAndFixHalfedges(const VecDH<glm::ivec3>& triVerts) {
const int numTri = triVerts.size();
halfedge_.resize(3 * numTri);
VecDH<TmpEdge> edge(3 * numTri);
thrust::for_each_n(zip(countAt(0), triVerts.begin()), numTri,
Tri2Halfedges({halfedge_.ptrH(), edge.ptrH()}));
// Stable sort is required here so that halfedges from the same face are
// paired together (the triangles were created in face order). In some
// degenerate situations the triangulator can add the same internal edge in
// two different faces, causing this edge to not be 2-manifold. We detect this
// and fix it by swapping one of the identical edges, so it is important that
// we have the edges paired according to their face.
std::stable_sort(edge.begin(), edge.end());
thrust::for_each_n(thrust::host, countAt(0), halfedge_.size() / 2,
LinkHalfedges({halfedge_.ptrH(), edge.cptrH()}));
thrust::for_each(thrust::host, countAt(1), countAt(halfedge_.size() / 2),
SwapHalfedges({halfedge_.ptrH(), edge.cptrH()}));
}
/**
* Does a full recalculation of the face bounding boxes, including updating the
* collider, but does not resort the faces.
*/
void Manifold::Impl::Update() {
CalculateBBox();
VecDH<Box> faceBox;
VecDH<uint32_t> faceMorton;
GetFaceBoxMorton(faceBox, faceMorton);
collider_.UpdateBoxes(faceBox);
}
void Manifold::Impl::ApplyTransform() const {
// This const_cast is here because these operations cancel out, leaving the
// state conceptually unchanged. This enables lazy transformation evaluation.
const_cast<Impl*>(this)->ApplyTransform();
}
/**
* Bake the manifold's transform into its vertices. This function allows lazy
* evaluation, which is important because often several transforms are applied
* between operations.
*/
void Manifold::Impl::ApplyTransform() {
if (transform_ == glm::mat4x3(1.0f)) return;
thrust::for_each(vertPos_.beginD(), vertPos_.endD(),
Transform4x3({transform_}));
glm::mat3 normalTransform =
glm::inverse(glm::transpose(glm::mat3(transform_)));
thrust::for_each(faceNormal_.beginD(), faceNormal_.endD(),
TransformNormals({normalTransform}));
thrust::for_each(vertNormal_.beginD(), vertNormal_.endD(),
TransformNormals({normalTransform}));
// This optimization does a cheap collider update if the transform is
// axis-aligned.
if (!collider_.Transform(transform_)) Update();
const float oldScale = bBox_.Scale();
transform_ = glm::mat4x3(1.0f);
CalculateBBox();
const float newScale = bBox_.Scale();
precision_ *= glm::max(1.0f, newScale / oldScale) *
glm::max(glm::length(transform_[0]),
glm::max(glm::length(transform_[1]),
glm::length(transform_[2])));
// Maximum of inherited precision loss and translational precision loss.
SetPrecision(precision_);
}
/**
* Sets the precision based on the bounding box, and limits its minimum value by
* the optional input.
*/
void Manifold::Impl::SetPrecision(float minPrecision) {
precision_ = glm::max(minPrecision, kTolerance * bBox_.Scale());
if (!glm::isfinite(precision_)) precision_ = -1;
}
/**
* If face normals are already present, this function uses them to compute
* vertex normals (angle-weighted pseudo-normals); otherwise it also computes
* the face normals. Face normals are only calculated when needed because nearly
* degenerate faces will accrue rounding error, while the Boolean can retain
* their original normal, which is more accurate and can help with merging
* coplanar faces.
*
* If the face normals have been invalidated by an operation like Warp(), ensure
* you do faceNormal_.resize(0) before calling this function to force
* recalculation.
*/
void Manifold::Impl::CalculateNormals() {
vertNormal_.resize(NumVert());
thrust::fill(vertNormal_.beginD(), vertNormal_.endD(), glm::vec3(0));
bool calculateTriNormal = false;
if (faceNormal_.size() != NumTri()) {
faceNormal_.resize(NumTri());
calculateTriNormal = true;
}
thrust::for_each_n(
zip(faceNormal_.beginD(), countAt(0)), NumTri(),
AssignNormals({vertNormal_.ptrD(), vertPos_.cptrD(), halfedge_.cptrD(),
precision_, calculateTriNormal}));
thrust::for_each(vertNormal_.beginD(), vertNormal_.endD(), Normalize());
}
/**
* Returns a sparse array of the bounding box overlaps between the edges of the
* input manifold, Q and the faces of this manifold. Returned indices only
* point to forward halfedges.
*/
SparseIndices Manifold::Impl::EdgeCollisions(const Impl& Q) const {
VecDH<TmpEdge> edges = CreateTmpEdges(Q.halfedge_);
const int numEdge = edges.size();
VecDH<Box> QedgeBB(numEdge);
thrust::for_each_n(zip(QedgeBB.beginD(), edges.cbeginD()), numEdge,
EdgeBox({Q.vertPos_.cptrD()}));
SparseIndices q1p2 = collider_.Collisions(QedgeBB);
thrust::for_each(q1p2.beginD(0), q1p2.endD(0), ReindexEdge({edges.cptrD()}));
return q1p2;
}
/**
* Returns a sparse array of the input vertices that project inside the XY
* bounding boxes of the faces of this manifold.
*/
SparseIndices Manifold::Impl::VertexCollisionsZ(
const VecDH<glm::vec3>& vertsIn) const {
return collider_.Collisions(vertsIn);
}
} // namespace manifold