1 /* This file is part of Dilay
2 * Copyright © 2015-2018 Alexander Bau
3 * Use and redistribute under the terms of the GNU General Public License
4 */
5 #include <cstring>
6 #include <glm/glm.hpp>
7 #include <glm/gtx/norm.hpp>
8 #include <vector>
9 #include "../mesh.hpp"
10 #include "config.hpp"
11 #include "distance.hpp"
12 #include "dynamic/faces.hpp"
13 #include "dynamic/mesh-intersection.hpp"
14 #include "dynamic/mesh.hpp"
15 #include "dynamic/octree.hpp"
16 #include "intersection.hpp"
17 #include "mesh-util.hpp"
18 #include "primitive/aabox.hpp"
19 #include "primitive/plane.hpp"
20 #include "primitive/ray.hpp"
21 #include "primitive/triangle.hpp"
22 #include "tool/sculpt/util/action.hpp"
23 #include "util.hpp"
24
25 namespace
26 {
27 struct VertexData
28 {
29 bool isFree;
30 std::vector<unsigned int> adjacentFaces;
31
VertexData__anon02d9ed000111::VertexData32 VertexData () { this->reset (); }
reset__anon02d9ed000111::VertexData33 void reset ()
34 {
35 this->isFree = true;
36 this->adjacentFaces.clear ();
37 }
38
addAdjacentFace__anon02d9ed000111::VertexData39 void addAdjacentFace (unsigned int face) { this->adjacentFaces.push_back (face); }
40
deleteAdjacentFace__anon02d9ed000111::VertexData41 void deleteAdjacentFace (unsigned int face)
42 {
43 for (auto it = this->adjacentFaces.begin (); it != this->adjacentFaces.end (); ++it)
44 {
45 if (*it == face)
46 {
47 this->adjacentFaces.erase (it);
48 return;
49 }
50 }
51 DILAY_IMPOSSIBLE
52 }
53 };
54
55 struct FaceData
56 {
57 bool isFree;
58
FaceData__anon02d9ed000111::FaceData59 FaceData () { this->reset (); }
reset__anon02d9ed000111::FaceData60 void reset () { this->isFree = true; }
61 };
62 }
63
64 struct DynamicMesh::Impl
65 {
66 DynamicMesh* self;
67 Mesh mesh;
68 std::vector<VertexData> vertexData;
69 std::vector<unsigned char> vertexVisited;
70 std::vector<unsigned int> freeVertexIndices;
71 std::vector<FaceData> faceData;
72 std::vector<unsigned char> faceVisited;
73 std::vector<unsigned int> freeFaceIndices;
74 DynamicOctree octree;
75
ImplDynamicMesh::Impl76 Impl (DynamicMesh* s)
77 : self (s)
78 {
79 }
80
ImplDynamicMesh::Impl81 Impl (DynamicMesh* s, const Mesh& m)
82 : self (s)
83 {
84 this->fromMesh (m);
85 }
86
numVerticesDynamicMesh::Impl87 unsigned int numVertices () const
88 {
89 assert (this->mesh.numVertices () >= this->freeVertexIndices.size ());
90 assert (this->mesh.numVertices () == this->vertexData.size ());
91 return this->mesh.numVertices () - this->freeVertexIndices.size ();
92 }
93
numFacesDynamicMesh::Impl94 unsigned int numFaces () const
95 {
96 assert (this->faceData.size () >= this->freeFaceIndices.size ());
97 return this->faceData.size () - this->freeFaceIndices.size ();
98 }
99
isEmptyDynamicMesh::Impl100 bool isEmpty () const { return this->numFaces () == 0; }
101
isFreeVertexDynamicMesh::Impl102 bool isFreeVertex (unsigned int i) const
103 {
104 assert (i < this->vertexData.size ());
105 return this->vertexData[i].isFree;
106 }
107
isFreeFaceDynamicMesh::Impl108 bool isFreeFace (unsigned int i) const
109 {
110 assert (i < this->faceData.size ());
111 return this->faceData[i].isFree;
112 }
113
isPrunedDynamicMesh::Impl114 bool isPruned () const
115 {
116 return this->freeFaceIndices.empty () && this->freeVertexIndices.empty ();
117 }
118
valenceDynamicMesh::Impl119 unsigned int valence (unsigned int i) const
120 {
121 assert (this->isFreeVertex (i) == false);
122 return this->vertexData[i].adjacentFaces.size ();
123 }
124
vertexIndicesDynamicMesh::Impl125 void vertexIndices (unsigned int i, unsigned int& i1, unsigned int& i2, unsigned int& i3) const
126 {
127 assert (this->isFreeFace (i) == false);
128
129 i1 = this->mesh.index ((3 * i) + 0);
130 i2 = this->mesh.index ((3 * i) + 1);
131 i3 = this->mesh.index ((3 * i) + 2);
132 }
133
faceDynamicMesh::Impl134 PrimTriangle face (unsigned int i) const
135 {
136 assert (this->isFreeFace (i) == false);
137
138 return PrimTriangle (this->mesh.vertex (this->mesh.index ((3 * i) + 0)),
139 this->mesh.vertex (this->mesh.index ((3 * i) + 1)),
140 this->mesh.vertex (this->mesh.index ((3 * i) + 2)));
141 }
142
vertexNormalDynamicMesh::Impl143 const glm::vec3& vertexNormal (unsigned int i) const { return this->mesh.normal (i); }
144
faceNormalDynamicMesh::Impl145 glm::vec3 faceNormal (unsigned int i) const
146 {
147 assert (this->isFreeFace (i) == false);
148
149 unsigned int i1, i2, i3;
150 this->vertexIndices (i, i1, i2, i3);
151
152 return glm::normalize (glm::cross (this->mesh.vertex (i2) - this->mesh.vertex (i1),
153 this->mesh.vertex (i3) - this->mesh.vertex (i1)));
154 }
155
findAdjacentDynamicMesh::Impl156 void findAdjacent (unsigned int e1, unsigned int e2, unsigned int& leftFace,
157 unsigned int& leftVertex, unsigned int& rightFace,
158 unsigned int& rightVertex) const
159 {
160 assert (this->isFreeVertex (e1) == false);
161 assert (this->isFreeVertex (e2) == false);
162
163 leftFace = Util::invalidIndex ();
164 leftVertex = Util::invalidIndex ();
165 rightFace = Util::invalidIndex ();
166 rightVertex = Util::invalidIndex ();
167
168 for (unsigned int a : this->vertexData[e1].adjacentFaces)
169 {
170 unsigned int i1, i2, i3;
171 this->vertexIndices (a, i1, i2, i3);
172
173 if (e1 == i1 && e2 == i2)
174 {
175 leftFace = a;
176 leftVertex = i3;
177 }
178 else if (e1 == i2 && e2 == i1)
179 {
180 rightFace = a;
181 rightVertex = i3;
182 }
183 else if (e1 == i2 && e2 == i3)
184 {
185 leftFace = a;
186 leftVertex = i1;
187 }
188 else if (e1 == i3 && e2 == i2)
189 {
190 rightFace = a;
191 rightVertex = i1;
192 }
193 else if (e1 == i3 && e2 == i1)
194 {
195 leftFace = a;
196 leftVertex = i2;
197 }
198 else if (e1 == i1 && e2 == i3)
199 {
200 rightFace = a;
201 rightVertex = i2;
202 }
203 }
204 assert (leftFace != Util::invalidIndex ());
205 assert (leftVertex != Util::invalidIndex ());
206 assert (rightFace != Util::invalidIndex ());
207 assert (rightVertex != Util::invalidIndex ());
208 }
209
adjacentFacesDynamicMesh::Impl210 const std::vector<unsigned int>& adjacentFaces (unsigned int i) const
211 {
212 assert (this->isFreeVertex (i) == false);
213 return this->vertexData[i].adjacentFaces;
214 }
215
forEachVertexDynamicMesh::Impl216 void forEachVertex (const std::function<void(unsigned int)>& f)
217 {
218 for (unsigned int i = 0; i < this->vertexData.size (); i++)
219 {
220 if (this->isFreeVertex (i) == false)
221 {
222 f (i);
223 }
224 }
225 }
226
visitVerticesDynamicMesh::Impl227 void visitVertices (unsigned int i, const std::function<void(unsigned int)>& f)
228 {
229 assert (this->isFreeFace (i) == false);
230
231 unsigned int i1, i2, i3;
232 this->vertexIndices (i, i1, i2, i3);
233
234 if (this->vertexVisited[i1] == 0)
235 {
236 this->vertexVisited[i1] = 1;
237 f (i1);
238 }
239 if (this->vertexVisited[i2] == 0)
240 {
241 this->vertexVisited[i2] = 1;
242 f (i2);
243 }
244 if (this->vertexVisited[i3] == 0)
245 {
246 this->vertexVisited[i3] = 1;
247 f (i3);
248 }
249 this->faceVisited[i] = 1;
250 }
251
unvisitVerticesDynamicMesh::Impl252 void unvisitVertices ()
253 {
254 std::memset (this->vertexVisited.data (), 0, this->vertexVisited.size ());
255 }
256
unvisitFacesDynamicMesh::Impl257 void unvisitFaces () { std::memset (this->faceVisited.data (), 0, this->faceVisited.size ()); }
258
forEachVertexDynamicMesh::Impl259 void forEachVertex (const DynamicFaces& faces, const std::function<void(unsigned int)>& f)
260 {
261 this->unvisitVertices ();
262
263 for (unsigned int i : faces)
264 {
265 this->visitVertices (i, f);
266 }
267 }
268
forEachVertexExtDynamicMesh::Impl269 void forEachVertexExt (const DynamicFaces& faces, const std::function<void(unsigned int)>& f)
270 {
271 this->unvisitVertices ();
272 this->unvisitFaces ();
273
274 for (unsigned int i : faces)
275 {
276 this->visitVertices (i, [this, &f](unsigned int j) {
277 f (j);
278
279 for (unsigned int a : this->vertexData[j].adjacentFaces)
280 {
281 if (this->faceVisited[a] == 0)
282 {
283 this->visitVertices (a, f);
284 }
285 }
286 });
287 }
288 }
289
forEachVertexAdjacentToVertexDynamicMesh::Impl290 void forEachVertexAdjacentToVertex (unsigned int i,
291 const std::function<void(unsigned int)>& f) const
292 {
293 assert (this->isFreeVertex (i) == false);
294
295 for (unsigned int a : this->vertexData[i].adjacentFaces)
296 {
297 unsigned int a1, a2, a3;
298 this->vertexIndices (a, a1, a2, a3);
299
300 if (i == a1)
301 {
302 f (a2);
303 }
304 else if (i == a2)
305 {
306 f (a3);
307 }
308 else if (i == a3)
309 {
310 f (a1);
311 }
312 else
313 {
314 DILAY_IMPOSSIBLE
315 }
316 }
317 }
318
forEachVertexAdjacentToFaceDynamicMesh::Impl319 void forEachVertexAdjacentToFace (unsigned int i,
320 const std::function<void(unsigned int)>& f) const
321 {
322 assert (this->isFreeFace (i) == false);
323
324 unsigned int i1, i2, i3;
325 this->vertexIndices (i, i1, i2, i3);
326
327 f (i1);
328 f (i2);
329 f (i3);
330 }
331
forEachFaceDynamicMesh::Impl332 void forEachFace (const std::function<void(unsigned int)>& f)
333 {
334 for (unsigned int i = 0; i < this->faceData.size (); i++)
335 {
336 if (this->isFreeFace (i) == false)
337 {
338 f (i);
339 }
340 }
341 }
342
forEachFaceExtDynamicMesh::Impl343 void forEachFaceExt (const DynamicFaces& faces, const std::function<void(unsigned int)>& f)
344 {
345 this->unvisitVertices ();
346 this->unvisitFaces ();
347
348 for (unsigned int i : faces)
349 {
350 if (this->faceVisited[i] == 0)
351 {
352 f (i);
353 this->faceVisited[i] = 1;
354 }
355 this->visitVertices (i, [this, &f](unsigned int j) {
356 for (unsigned int a : this->vertexData[j].adjacentFaces)
357 {
358 if (this->faceVisited[a] == 0)
359 {
360 f (a);
361 this->faceVisited[a] = 1;
362 }
363 }
364 });
365 }
366 }
367
averageDynamicMesh::Impl368 void average (const DynamicFaces& faces, glm::vec3& position, glm::vec3& normal) const
369 {
370 assert (faces.numElements () > 0);
371
372 position = glm::vec3 (0.0f);
373 normal = glm::vec3 (0.0f);
374
375 for (unsigned int f : faces)
376 {
377 unsigned int i1, i2, i3;
378 this->vertexIndices (f, i1, i2, i3);
379
380 position += this->mesh.vertex (i1);
381 position += this->mesh.vertex (i2);
382 position += this->mesh.vertex (i3);
383 normal += glm::cross (this->mesh.vertex (i2) - this->mesh.vertex (i1),
384 this->mesh.vertex (i3) - this->mesh.vertex (i1));
385 }
386 position /= float(faces.numElements () * 3);
387 normal = glm::normalize (normal);
388 }
389
averagePositionDynamicMesh::Impl390 glm::vec3 averagePosition (const DynamicFaces& faces) const
391 {
392 assert (faces.numElements () > 0);
393
394 glm::vec3 position = glm::vec3 (0.0f);
395
396 for (unsigned int f : faces)
397 {
398 this->forEachVertexAdjacentToFace (
399 f, [this, &position](unsigned int v) { position += this->mesh.vertex (v); });
400 }
401 return position / float(faces.numElements () * 3);
402 }
403
averagePositionDynamicMesh::Impl404 glm::vec3 averagePosition (unsigned int i) const
405 {
406 assert (this->isFreeVertex (i) == false);
407 assert (this->vertexData[i].adjacentFaces.size () > 0);
408
409 glm::vec3 position = glm::vec3 (0.0f);
410
411 this->forEachVertexAdjacentToVertex (
412 i, [this, &position](unsigned int v) { position += this->mesh.vertex (v); });
413 return position / float(this->vertexData[i].adjacentFaces.size ());
414 }
415
averageNormalDynamicMesh::Impl416 glm::vec3 averageNormal (const DynamicFaces& faces) const
417 {
418 assert (faces.numElements () > 0);
419
420 glm::vec3 normal = glm::vec3 (0.0f);
421
422 for (unsigned int f : faces)
423 {
424 unsigned int i1, i2, i3;
425 this->vertexIndices (f, i1, i2, i3);
426
427 normal += glm::cross (this->mesh.vertex (i2) - this->mesh.vertex (i1),
428 this->mesh.vertex (i3) - this->mesh.vertex (i1));
429 }
430 return glm::normalize (normal);
431 }
432
averageNormalDynamicMesh::Impl433 glm::vec3 averageNormal (unsigned int i) const
434 {
435 assert (this->isFreeVertex (i) == false);
436 assert (this->vertexData[i].adjacentFaces.size () > 0);
437
438 glm::vec3 normal = glm::vec3 (0.0f);
439
440 for (unsigned int f : this->vertexData[i].adjacentFaces)
441 {
442 unsigned int i1, i2, i3;
443 this->vertexIndices (f, i1, i2, i3);
444
445 normal += glm::cross (this->mesh.vertex (i2) - this->mesh.vertex (i1),
446 this->mesh.vertex (i3) - this->mesh.vertex (i1));
447 }
448 return glm::normalize (normal);
449 }
450
averageEdgeLengthSqrDynamicMesh::Impl451 float averageEdgeLengthSqr (const DynamicFaces& faces) const
452 {
453 assert (faces.numElements () > 0);
454
455 float length = 0.0f;
456 for (unsigned int i : faces)
457 {
458 length += this->averageEdgeLengthSqr (i);
459 }
460 return length / float(faces.numElements ());
461 }
462
averageEdgeLengthSqrDynamicMesh::Impl463 float averageEdgeLengthSqr (unsigned int i) const
464 {
465 assert (this->isFreeFace (i) == false);
466
467 unsigned int i1, i2, i3;
468 this->vertexIndices (i, i1, i2, i3);
469
470 const glm::vec3& v1 = this->mesh.vertex (i1);
471 const glm::vec3& v2 = this->mesh.vertex (i2);
472 const glm::vec3& v3 = this->mesh.vertex (i3);
473
474 return (glm::distance2 (v1, v2) + glm::distance2 (v1, v3) + glm::distance2 (v2, v3)) / 3.0f;
475 }
476
addVertexDynamicMesh::Impl477 unsigned int addVertex (const glm::vec3& vertex, const glm::vec3& normal)
478 {
479 assert (this->vertexData.size () == this->mesh.numVertices ());
480 assert (this->vertexVisited.size () == this->mesh.numVertices ());
481
482 if (this->freeVertexIndices.empty ())
483 {
484 this->vertexData.emplace_back ();
485 this->vertexData.back ().isFree = false;
486 this->vertexVisited.push_back (0);
487 return this->mesh.addVertex (vertex, normal);
488 }
489 else
490 {
491 const unsigned int index = this->freeVertexIndices.back ();
492 this->mesh.vertex (index, vertex);
493 this->mesh.normal (index, normal);
494 this->vertexData[index].reset ();
495 this->vertexData[index].isFree = false;
496 this->vertexVisited[index] = 0;
497 this->freeVertexIndices.pop_back ();
498 return index;
499 }
500 }
501
addFaceDynamicMesh::Impl502 unsigned int addFace (unsigned int i1, unsigned int i2, unsigned int i3)
503 {
504 assert (i1 < this->mesh.numVertices ());
505 assert (i2 < this->mesh.numVertices ());
506 assert (i3 < this->mesh.numVertices ());
507 assert (3 * this->faceData.size () == this->mesh.numIndices ());
508 assert (this->faceData.size () == this->faceVisited.size ());
509
510 unsigned int index = Util::invalidIndex ();
511
512 if (this->freeFaceIndices.empty ())
513 {
514 index = this->numFaces ();
515 this->faceData.emplace_back ();
516 this->faceVisited.push_back (0);
517
518 this->mesh.addIndex (i1);
519 this->mesh.addIndex (i2);
520 this->mesh.addIndex (i3);
521 }
522 else
523 {
524 index = this->freeFaceIndices.back ();
525 this->faceData[index].reset ();
526 this->faceVisited[index] = 0;
527 this->freeFaceIndices.pop_back ();
528
529 this->mesh.index ((3 * index) + 0, i1);
530 this->mesh.index ((3 * index) + 1, i2);
531 this->mesh.index ((3 * index) + 2, i3);
532 }
533 this->faceData[index].isFree = false;
534
535 this->vertexData[i1].addAdjacentFace (index);
536 this->vertexData[i2].addAdjacentFace (index);
537 this->vertexData[i3].addAdjacentFace (index);
538
539 this->addFaceToOctree (index);
540
541 return index;
542 }
543
addFaceToOctreeDynamicMesh::Impl544 void addFaceToOctree (unsigned int i)
545 {
546 const PrimTriangle tri = this->face (i);
547
548 if (this->octree.hasRoot () == false)
549 {
550 this->octree.setupRoot (tri.center (), tri.maxDimExtent ());
551 }
552
553 this->octree.addElement (i, tri.center (), tri.maxDimExtent ());
554 }
555
deleteVertexDynamicMesh::Impl556 void deleteVertex (unsigned int i)
557 {
558 assert (i < this->vertexData.size ());
559 assert (i < this->vertexVisited.size ());
560
561 std::vector<unsigned int> adjacentFaces = this->vertexData[i].adjacentFaces;
562 for (unsigned int f : adjacentFaces)
563 {
564 this->deleteFace (f);
565 }
566 this->vertexData[i].reset ();
567 this->vertexVisited[i] = 0;
568 this->freeVertexIndices.push_back (i);
569 }
570
deleteFaceDynamicMesh::Impl571 void deleteFace (unsigned int i)
572 {
573 assert (i < this->faceData.size ());
574 assert (i < this->faceVisited.size ());
575
576 this->vertexData[this->mesh.index ((3 * i) + 0)].deleteAdjacentFace (i);
577 this->vertexData[this->mesh.index ((3 * i) + 1)].deleteAdjacentFace (i);
578 this->vertexData[this->mesh.index ((3 * i) + 2)].deleteAdjacentFace (i);
579
580 this->faceData[i].reset ();
581 this->faceVisited[i] = 0;
582 this->freeFaceIndices.push_back (i);
583 this->octree.deleteElement (i);
584 }
585
vertexNormalDynamicMesh::Impl586 void vertexNormal (unsigned int i, const glm::vec3& n)
587 {
588 assert (this->isFreeVertex (i) == false);
589 assert (this->mesh.numVertices () == this->vertexData.size ());
590
591 this->mesh.normal (i, n);
592 }
593
setVertexNormalDynamicMesh::Impl594 void setVertexNormal (unsigned int i)
595 {
596 const glm::vec3 avg = this->averageNormal (i);
597
598 if (Util::isNaN (avg))
599 {
600 this->mesh.normal (i, glm::vec3 (0.0f));
601 }
602 else
603 {
604 this->mesh.normal (i, avg);
605 }
606 }
607
setAllNormalsDynamicMesh::Impl608 void setAllNormals ()
609 {
610 this->forEachVertex ([this](unsigned int i) { this->setVertexNormal (i); });
611 }
612
resetDynamicMesh::Impl613 void reset ()
614 {
615 this->mesh.reset ();
616 this->vertexData.clear ();
617 this->vertexVisited.clear ();
618 this->freeVertexIndices.clear ();
619 this->faceData.clear ();
620 this->faceVisited.clear ();
621 this->freeFaceIndices.clear ();
622 this->octree.reset ();
623 }
624
fromMeshDynamicMesh::Impl625 void fromMesh (const Mesh& mesh)
626 {
627 this->reset ();
628 this->mesh.reserveVertices (mesh.numVertices ());
629
630 for (unsigned int i = 0; i < mesh.numVertices (); i++)
631 {
632 this->addVertex (mesh.vertex (i), mesh.normal (i));
633 }
634
635 assert (mesh.numIndices () % 3 == 0);
636 this->mesh.reserveIndices (mesh.numIndices ());
637
638 for (unsigned int i = 0; i < mesh.numIndices (); i += 3)
639 {
640 this->addFace (mesh.index (i), mesh.index (i + 1), mesh.index (i + 2));
641 }
642 this->setAllNormals ();
643 this->mesh.bufferData ();
644 }
645
realignFaceDynamicMesh::Impl646 void realignFace (unsigned int i)
647 {
648 assert (this->isFreeFace (i) == false);
649
650 const PrimTriangle tri = this->face (i);
651
652 this->octree.realignElement (i, tri.center (), tri.maxDimExtent ());
653 }
654
realignFacesDynamicMesh::Impl655 void realignFaces (const DynamicFaces& faces)
656 {
657 for (unsigned int i : faces)
658 {
659 this->realignFace (i);
660 }
661 }
662
realignAllFacesDynamicMesh::Impl663 void realignAllFaces ()
664 {
665 this->forEachFace ([this](unsigned int i) { this->realignFace (i); });
666 }
667
sanitizeDynamicMesh::Impl668 void sanitize ()
669 {
670 this->octree.deleteEmptyChildren ();
671 this->octree.shrinkRoot ();
672 }
673
pruneDynamicMesh::Impl674 void prune (std::vector<unsigned int>* pVertexIndexMap, std::vector<unsigned int>* pFaceIndexMap)
675 {
676 if (this->isPruned () == false)
677 {
678 std::vector<unsigned int> defaultVertexIndexMap;
679 std::vector<unsigned int> defaultFaceIndexMap;
680
681 if (pVertexIndexMap == nullptr)
682 {
683 pVertexIndexMap = &defaultVertexIndexMap;
684 }
685 if (pFaceIndexMap == nullptr)
686 {
687 pFaceIndexMap = &defaultFaceIndexMap;
688 }
689
690 Util::prune<VertexData> (this->vertexData, [](const VertexData& d) { return d.isFree; },
691 pVertexIndexMap);
692 Util::prune<FaceData> (this->faceData, [](const FaceData& d) { return d.isFree; },
693 pFaceIndexMap);
694
695 const unsigned int newNumVertices = this->vertexData.size ();
696 const unsigned int newNumFaces = this->faceData.size ();
697
698 for (VertexData& d : this->vertexData)
699 {
700 for (unsigned int& f : d.adjacentFaces)
701 {
702 assert (pFaceIndexMap->at (f) != Util::invalidIndex ());
703
704 f = pFaceIndexMap->at (f);
705 }
706 }
707
708 for (unsigned int i = 0; i < pVertexIndexMap->size (); i++)
709 {
710 const unsigned int newV = pVertexIndexMap->at (i);
711 if (newV != Util::invalidIndex ())
712 {
713 this->mesh.vertex (newV, this->mesh.vertex (i));
714 this->mesh.normal (newV, this->mesh.normal (i));
715 }
716 else
717 {
718 // Expensive (!) in debug builds
719 assert (std::find (this->freeVertexIndices.begin (), this->freeVertexIndices.end (), i) !=
720 this->freeVertexIndices.end ());
721 }
722 }
723 this->freeVertexIndices.clear ();
724 this->mesh.shrinkVertices (newNumVertices);
725 this->vertexVisited.resize (newNumVertices);
726 assert (this->numVertices () == newNumVertices);
727
728 for (unsigned int i = 0; i < pFaceIndexMap->size (); i++)
729 {
730 const unsigned int newF = pFaceIndexMap->at (i);
731 if (newF != Util::invalidIndex ())
732 {
733 const unsigned int oldI1 = this->mesh.index ((3 * i) + 0);
734 const unsigned int oldI2 = this->mesh.index ((3 * i) + 1);
735 const unsigned int oldI3 = this->mesh.index ((3 * i) + 2);
736
737 assert (pVertexIndexMap->at (oldI1) != Util::invalidIndex ());
738 assert (pVertexIndexMap->at (oldI2) != Util::invalidIndex ());
739 assert (pVertexIndexMap->at (oldI3) != Util::invalidIndex ());
740
741 this->mesh.index ((3 * newF) + 0, pVertexIndexMap->at (oldI1));
742 this->mesh.index ((3 * newF) + 1, pVertexIndexMap->at (oldI2));
743 this->mesh.index ((3 * newF) + 2, pVertexIndexMap->at (oldI3));
744 }
745 else
746 {
747 // Expensive (!) in debug builds
748 assert (std::find (this->freeFaceIndices.begin (), this->freeFaceIndices.end (), i) !=
749 this->freeFaceIndices.end ());
750 }
751 }
752 this->freeFaceIndices.clear ();
753 this->mesh.shrinkIndices (3 * newNumFaces);
754 this->faceVisited.resize (newNumFaces);
755 assert (this->numFaces () == newNumFaces);
756
757 this->octree.updateIndices (*pFaceIndexMap);
758 }
759 }
760
pruneAndCheckConsistencyDynamicMesh::Impl761 bool pruneAndCheckConsistency (std::vector<unsigned int>* pVertexIndexMap,
762 std::vector<unsigned int>* pFaceIndexMap)
763 {
764 this->prune (pVertexIndexMap, pFaceIndexMap);
765 this->bufferData ();
766
767 if (MeshUtil::checkConsistency (this->mesh))
768 {
769 for (unsigned int i = 0; i < this->vertexData.size (); i++)
770 {
771 if (this->vertexData[i].isFree == false)
772 {
773 if (this->vertexData[i].adjacentFaces.empty ())
774 {
775 DILAY_WARN ("vertex %u is not free but has no adjacent faces", i);
776 return false;
777 }
778 }
779 }
780 return true;
781 }
782 else
783 {
784 return false;
785 }
786 }
787
mirrorPositiveDynamicMesh::Impl788 bool mirrorPositive (const PrimPlane& plane)
789 {
790 assert (this->pruneAndCheckConsistency (nullptr, nullptr));
791
792 const auto inBorder = [this, &plane](unsigned int f) {
793 unsigned int i1, i2, i3;
794 this->vertexIndices (f, i1, i2, i3);
795
796 const float d1 = glm::abs (plane.distance (this->mesh.vertex (i1)));
797 const float d2 = glm::abs (plane.distance (this->mesh.vertex (i2)));
798 const float d3 = glm::abs (plane.distance (this->mesh.vertex (i3)));
799
800 return d1 <= Util::epsilon () && d2 <= Util::epsilon () && d3 <= Util::epsilon ();
801 };
802
803 DynamicFaces faces;
804 do
805 {
806 faces.reset ();
807 if (this->intersects (plane, faces) == false)
808 {
809 break;
810 }
811 faces.filter (inBorder);
812 if (faces.isEmpty ())
813 {
814 break;
815 }
816 } while (ToolSculptAction::deleteFaces (*this->self, faces));
817 assert (this->pruneAndCheckConsistency (nullptr, nullptr));
818
819 this->prune (nullptr, nullptr);
820
821 Mesh mirrored = MeshUtil::mirrorPositive (this->mesh, plane);
822 if (mirrored.numVertices () == 0)
823 {
824 this->setAllNormals ();
825 return false;
826 }
827 else
828 {
829 this->fromMesh (mirrored);
830 assert (this->pruneAndCheckConsistency (nullptr, nullptr));
831 return true;
832 }
833 }
834
mirrorDynamicMesh::Impl835 void mirror (const PrimPlane& plane)
836 {
837 MeshUtil::mirror (this->mesh, plane);
838 this->realignAllFaces ();
839 this->bufferData ();
840 }
841
moveToCenterDynamicMesh::Impl842 void moveToCenter ()
843 {
844 MeshUtil::moveToCenter (this->mesh);
845 this->realignAllFaces ();
846 this->bufferData ();
847 }
848
normalizeScalingDynamicMesh::Impl849 void normalizeScaling ()
850 {
851 MeshUtil::normalizeScaling (this->mesh);
852 this->realignAllFaces ();
853 this->bufferData ();
854 }
855
bufferDataDynamicMesh::Impl856 void bufferData ()
857 {
858 const auto findNonFreeFaceIndex = [this]() -> unsigned int {
859 assert (this->numFaces () > 0);
860
861 for (unsigned int i = 0; i < this->faceData.size (); i++)
862 {
863 if (this->isFreeFace (i) == false)
864 {
865 return i;
866 }
867 }
868 DILAY_IMPOSSIBLE
869 };
870
871 if (this->numFaces () > 0 && this->freeFaceIndices.empty () == false)
872 {
873 const unsigned int nonFree = findNonFreeFaceIndex ();
874
875 for (unsigned int i : this->freeFaceIndices)
876 {
877 this->mesh.index ((3 * i) + 0, this->mesh.index ((3 * nonFree) + 0));
878 this->mesh.index ((3 * i) + 1, this->mesh.index ((3 * nonFree) + 1));
879 this->mesh.index ((3 * i) + 2, this->mesh.index ((3 * nonFree) + 2));
880 }
881 }
882 this->mesh.bufferData ();
883 }
884
renderDynamicMesh::Impl885 void render (Camera& camera) const
886 {
887 this->mesh.render (camera);
888 #ifdef DILAY_RENDER_OCTREE
889 this->octree.render (camera);
890 #endif
891 }
892
intersectsDynamicMesh::Impl893 bool intersects (const PrimRay& ray, Intersection& intersection, bool bothSides) const
894 {
895 this->octree.intersects (ray, [this, &ray, &intersection, bothSides](unsigned int i) -> float {
896 const PrimTriangle tri = this->face (i);
897 float t;
898
899 if (IntersectionUtil::intersects (ray, tri, bothSides, &t))
900 {
901 intersection.update (t, ray.pointAt (t), tri.normal ());
902 return t;
903 }
904 else
905 {
906 return Util::maxFloat ();
907 }
908 });
909 return intersection.isIntersection ();
910 }
911
intersectsDynamicMesh::Impl912 bool intersects (const PrimRay& ray, DynamicMeshIntersection& intersection)
913 {
914 this->octree.intersects (ray, [this, &ray, &intersection](unsigned int i) -> float {
915 const PrimTriangle tri = this->face (i);
916 float t;
917
918 if (IntersectionUtil::intersects (ray, tri, false, &t))
919 {
920 intersection.update (t, ray.pointAt (t), tri.normal (), i, *this->self);
921 return intersection.distance ();
922 }
923 else
924 {
925 return Util::maxFloat ();
926 }
927 });
928 return intersection.isIntersection ();
929 }
930
931 template <typename T, typename... Ts>
intersectsTDynamicMesh::Impl932 bool intersectsT (const T& t, DynamicFaces& faces, const Ts&... args) const
933 {
934 this->octree.intersects (t, [this, &t, &faces, &args...](unsigned int i) {
935 if (IntersectionUtil::intersects (t, this->face (i), args...))
936 {
937 faces.insert (i);
938 }
939 });
940 faces.commit ();
941 return faces.isEmpty () == false;
942 }
943
944 template <typename T, typename... Ts>
containsOrIntersectsTDynamicMesh::Impl945 bool containsOrIntersectsT (const T& t, DynamicFaces& faces, const Ts&... args) const
946 {
947 this->octree.intersects (t, [this, &t, &faces, &args...](bool contains, unsigned int i) {
948 if (contains || IntersectionUtil::intersects (t, this->face (i), args...))
949 {
950 faces.insert (i);
951 faces.commit ();
952 }
953 });
954 return faces.isEmpty () == false;
955 }
956
intersectsDynamicMesh::Impl957 bool intersects (const PrimPlane& plane, DynamicFaces& faces) const
958 {
959 return this->intersectsT<PrimPlane> (plane, faces);
960 }
961
intersectsDynamicMesh::Impl962 bool intersects (const PrimSphere& sphere, DynamicFaces& faces) const
963 {
964 return this->containsOrIntersectsT<PrimSphere> (sphere, faces);
965 }
966
intersectsDynamicMesh::Impl967 bool intersects (const PrimAABox& box, DynamicFaces& faces) const
968 {
969 return this->containsOrIntersectsT<PrimAABox> (box, faces);
970 }
971
unsignedDistanceDynamicMesh::Impl972 float unsignedDistance (const glm::vec3& pos) const
973 {
974 return this->octree.distance (
975 pos, [this, &pos](unsigned int i) { return Distance::distance (this->face (i), pos); });
976 }
977
normalizeDynamicMesh::Impl978 void normalize ()
979 {
980 this->mesh.normalize ();
981 this->octree.reset ();
982
983 this->forEachFace ([this](unsigned int i) { this->addFaceToOctree (i); });
984 }
985
printStatisticsDynamicMesh::Impl986 void printStatistics () const { this->octree.printStatistics (); }
987
runFromConfigDynamicMesh::Impl988 void runFromConfig (const Config& config)
989 {
990 this->mesh.color (config.get<Color> ("editor/mesh/color/normal"));
991 this->mesh.wireframeColor (config.get<Color> ("editor/mesh/color/wireframe"));
992 }
993 };
994
995 DELEGATE_BIG4_COPY_SELF (DynamicMesh)
DELEGATE1_CONSTRUCTOR_SELF(DynamicMesh,const Mesh &)996 DELEGATE1_CONSTRUCTOR_SELF (DynamicMesh, const Mesh&)
997 DELEGATE_CONST (unsigned int, DynamicMesh, numVertices)
998 DELEGATE_CONST (unsigned int, DynamicMesh, numFaces)
999 DELEGATE_CONST (bool, DynamicMesh, isEmpty)
1000 DELEGATE1_CONST (bool, DynamicMesh, isFreeVertex, unsigned int)
1001 DELEGATE1_CONST (bool, DynamicMesh, isFreeFace, unsigned int)
1002 DELEGATE1_MEMBER_CONST (const glm::vec3&, DynamicMesh, vertex, mesh, unsigned int)
1003 DELEGATE1_CONST (unsigned int, DynamicMesh, valence, unsigned int)
1004 DELEGATE4_CONST (void, DynamicMesh, vertexIndices, unsigned int, unsigned int&, unsigned int&,
1005 unsigned int&)
1006 DELEGATE1_CONST (PrimTriangle, DynamicMesh, face, unsigned int)
1007 DELEGATE1_CONST (const glm::vec3&, DynamicMesh, vertexNormal, unsigned int)
1008 DELEGATE1_CONST (glm::vec3, DynamicMesh, faceNormal, unsigned int)
1009 DELEGATE1_CONST (const std::vector<unsigned int>&, DynamicMesh, adjacentFaces, unsigned int)
1010 GETTER_CONST (const Mesh&, DynamicMesh, mesh)
1011 DELEGATE1 (void, DynamicMesh, forEachVertex, const std::function<void(unsigned int)>&)
1012 DELEGATE2 (void, DynamicMesh, forEachVertex, const DynamicFaces&,
1013 const std::function<void(unsigned int)>&)
1014 DELEGATE2 (void, DynamicMesh, forEachVertexExt, const DynamicFaces&,
1015 const std::function<void(unsigned int)>&)
1016 DELEGATE2_CONST (void, DynamicMesh, forEachVertexAdjacentToVertex, unsigned int,
1017 const std::function<void(unsigned int)>&)
1018 DELEGATE2_CONST (void, DynamicMesh, forEachVertexAdjacentToFace, unsigned int,
1019 const std::function<void(unsigned int)>&)
1020 DELEGATE1 (void, DynamicMesh, forEachFace, const std::function<void(unsigned int)>&)
1021 DELEGATE2 (void, DynamicMesh, forEachFaceExt, const DynamicFaces&,
1022 const std::function<void(unsigned int)>&)
1023 DELEGATE3_CONST (void, DynamicMesh, average, const DynamicFaces&, glm::vec3&, glm::vec3&)
1024 DELEGATE1_CONST (glm::vec3, DynamicMesh, averagePosition, const DynamicFaces&)
1025 DELEGATE1_CONST (glm::vec3, DynamicMesh, averagePosition, unsigned int)
1026 DELEGATE1_CONST (glm::vec3, DynamicMesh, averageNormal, const DynamicFaces&)
1027 DELEGATE1_CONST (glm::vec3, DynamicMesh, averageNormal, unsigned int)
1028 DELEGATE1_CONST (float, DynamicMesh, averageEdgeLengthSqr, const DynamicFaces&)
1029 DELEGATE1_CONST (float, DynamicMesh, averageEdgeLengthSqr, unsigned int)
1030 DELEGATE2 (unsigned int, DynamicMesh, addVertex, const glm::vec3&, const glm::vec3&)
1031 DELEGATE3 (unsigned int, DynamicMesh, addFace, unsigned int, unsigned int, unsigned int)
1032 DELEGATE1 (void, DynamicMesh, deleteVertex, unsigned int)
1033 DELEGATE1 (void, DynamicMesh, deleteFace, unsigned int)
1034 DELEGATE2_MEMBER (void, DynamicMesh, vertex, mesh, unsigned int, const glm::vec3&)
1035 DELEGATE2 (void, DynamicMesh, vertexNormal, unsigned int, const glm::vec3&)
1036 DELEGATE1 (void, DynamicMesh, setVertexNormal, unsigned int)
1037 DELEGATE (void, DynamicMesh, setAllNormals)
1038 DELEGATE (void, DynamicMesh, reset)
1039 DELEGATE1 (void, DynamicMesh, fromMesh, const Mesh&)
1040 DELEGATE1 (void, DynamicMesh, realignFace, unsigned int)
1041 DELEGATE1 (void, DynamicMesh, realignFaces, const DynamicFaces&)
1042 DELEGATE (void, DynamicMesh, realignAllFaces)
1043 DELEGATE (void, DynamicMesh, sanitize)
1044 DELEGATE2 (void, DynamicMesh, prune, std::vector<unsigned int>*, std::vector<unsigned int>*)
1045 DELEGATE2 (bool, DynamicMesh, pruneAndCheckConsistency, std::vector<unsigned int>*,
1046 std::vector<unsigned int>*)
1047 DELEGATE1 (bool, DynamicMesh, mirrorPositive, const PrimPlane&)
1048 DELEGATE1 (void, DynamicMesh, mirror, const PrimPlane&)
1049 DELEGATE (void, DynamicMesh, moveToCenter)
1050 DELEGATE (void, DynamicMesh, normalizeScaling)
1051 DELEGATE (void, DynamicMesh, bufferData)
1052 DELEGATE1_CONST (void, DynamicMesh, render, Camera&)
1053 DELEGATE_MEMBER_CONST (const RenderMode&, DynamicMesh, renderMode, mesh)
1054 DELEGATE_MEMBER (RenderMode&, DynamicMesh, renderMode, mesh)
1055
1056 DELEGATE3_CONST (bool, DynamicMesh, intersects, const PrimRay&, Intersection&, bool)
1057 DELEGATE2 (bool, DynamicMesh, intersects, const PrimRay&, DynamicMeshIntersection&)
1058 DELEGATE2_CONST (bool, DynamicMesh, intersects, const PrimPlane&, DynamicFaces&)
1059 DELEGATE2_CONST (bool, DynamicMesh, intersects, const PrimSphere&, DynamicFaces&)
1060 DELEGATE2_CONST (bool, DynamicMesh, intersects, const PrimAABox&, DynamicFaces&)
1061 DELEGATE1_CONST (float, DynamicMesh, unsignedDistance, const glm::vec3&)
1062
1063 DELEGATE (void, DynamicMesh, normalize)
1064 DELEGATE1_MEMBER (void, DynamicMesh, scale, mesh, const glm::vec3&)
1065 DELEGATE1_MEMBER (void, DynamicMesh, scaling, mesh, const glm::vec3&)
1066 DELEGATE_MEMBER_CONST (glm::vec3, DynamicMesh, scaling, mesh)
1067 DELEGATE1_MEMBER (void, DynamicMesh, translate, mesh, const glm::vec3&)
1068 DELEGATE1_MEMBER (void, DynamicMesh, position, mesh, const glm::vec3&)
1069 DELEGATE_MEMBER_CONST (glm::vec3, DynamicMesh, position, mesh)
1070 DELEGATE1_MEMBER (void, DynamicMesh, rotationMatrix, mesh, const glm::mat4x4&)
1071 DELEGATE_MEMBER_CONST (const glm::mat4x4&, DynamicMesh, rotationMatrix, mesh)
1072 DELEGATE2_MEMBER (void, DynamicMesh, rotation, mesh, const glm::vec3&, float)
1073 DELEGATE1_MEMBER (void, DynamicMesh, rotationX, mesh, float)
1074 DELEGATE1_MEMBER (void, DynamicMesh, rotationY, mesh, float)
1075 DELEGATE1_MEMBER (void, DynamicMesh, rotationZ, mesh, float)
1076 DELEGATE1_MEMBER (void, DynamicMesh, rotate, mesh, const glm::mat4x4&)
1077 DELEGATE2_MEMBER (void, DynamicMesh, rotate, mesh, const glm::vec3&, float)
1078 DELEGATE1_MEMBER (void, DynamicMesh, rotateX, mesh, float)
1079 DELEGATE1_MEMBER (void, DynamicMesh, rotateY, mesh, float)
1080 DELEGATE1_MEMBER (void, DynamicMesh, rotateZ, mesh, float)
1081 DELEGATE_MEMBER_CONST (const Color&, DynamicMesh, color, mesh)
1082 DELEGATE1_MEMBER (void, DynamicMesh, color, mesh, const Color&)
1083 DELEGATE_MEMBER_CONST (const Color&, DynamicMesh, wireframeColor, mesh)
1084 DELEGATE1_MEMBER (void, DynamicMesh, wireframeColor, mesh, const Color&)
1085
1086 DELEGATE_CONST (void, DynamicMesh, printStatistics)
1087 DELEGATE1 (void, DynamicMesh, runFromConfig, const Config&)
1088
1089 void DynamicMesh::findAdjacent (unsigned int e1, unsigned int e2, unsigned int& leftFace,
1090 unsigned int& leftVertex, unsigned int& rightFace,
1091 unsigned int& rightVertex) const
1092 {
1093 return this->impl->findAdjacent (e1, e2, leftFace, leftVertex, rightFace, rightVertex);
1094 }
1095