Debian Codegraph @

thirdparty/vhacd/src/btConvexHullComputer.cpp

raw symbols SCIP
1 /*
2 Copyright (c) 2011 Ole Kniemeyer, MAXON, www.maxon.net
3
4 This software is provided 'as-is', without any express or implied warranty.
5 In no event will the authors be held liable for any damages arising from the use of this software.
6 Permission is granted to anyone to use this software for any purpose,
7 including commercial applications, and to alter it and redistribute it freely,
8 subject to the following restrictions:
9
10 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
11 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
12 3. This notice may not be removed or altered from any source distribution.
13 */
14
15 #include <string.h>
16
17 #include "btAlignedObjectArray.h"
18 #include "btConvexHullComputer.h"
19 #include "btMinMax.h"
20 #include "btVector3.h"
21
22 #ifdef __GNUC__
23 #include <stdint.h>
24 #elif defined(_MSC_VER)
25 typedef __int32 int32_t;
26 typedef __int64 int64_t;
27 typedef unsigned __int32 uint32_t;
28 typedef unsigned __int64 uint64_t;
29 #else
30 typedef int32_t int32_t;
31 typedef long long int32_t int64_t;
32 typedef uint32_t uint32_t;
33 typedef unsigned long long int32_t uint64_t;
34 #endif
35
36 #ifdef _MSC_VER
37 #pragma warning(disable:4458)
38 #endif
39
40 //The definition of USE_X86_64_ASM is moved into the build system. You can enable it manually by commenting out the following lines
41 //#if (defined(__GNUC__) && defined(__x86_64__) && !defined(__ICL)) // || (defined(__ICL) && defined(_M_X64)) bug in Intel compiler, disable inline assembly
42 // #define USE_X86_64_ASM
43 //#endif
44
45 //#define DEBUG_CONVEX_HULL
46 //#define SHOW_ITERATIONS
47
48 #if defined(DEBUG_CONVEX_HULL) || defined(SHOW_ITERATIONS)
49 #include <stdio.h>
50 #endif
51
52 namespace VHACD {
53
54 // Convex hull implementation based on Preparata and Hong
55 // Ole Kniemeyer, MAXON Computer GmbH
56 class btConvexHullInternal {
57 public:
58 class Point64 {
59 public:
60 int64_t x;
61 int64_t y;
62 int64_t z;
63
64 Point64(int64_t x, int64_t y, int64_t z)
65 : x(x)
66 , y(y)
67 , z(z)
68 {
69 }
70
71 bool isZero()
72 {
73 return (x == 0) && (y == 0) && (z == 0);
74 }
75
76 int64_t dot(const Point64& b) const
77 {
78 return x * b.x + y * b.y + z * b.z;
79 }
80 };
81
82 class Point32 {
83 public:
84 int32_t x;
85 int32_t y;
86 int32_t z;
87 int32_t index;
88
89 Point32()
90 {
91 }
92
93 Point32(int32_t x, int32_t y, int32_t z)
94 : x(x)
95 , y(y)
96 , z(z)
97 , index(-1)
98 {
99 }
100
101 bool operator==(const Point32& b) const
102 {
103 return (x == b.x) && (y == b.y) && (z == b.z);
104 }
105
106 bool operator!=(const Point32& b) const
107 {
108 return (x != b.x) || (y != b.y) || (z != b.z);
109 }
110
111 bool isZero()
112 {
113 return (x == 0) && (y == 0) && (z == 0);
114 }
115
116 Point64 cross(const Point32& b) const
117 {
118 return Point64(y * b.z - z * b.y, z * b.x - x * b.z, x * b.y - y * b.x);
119 }
120
121 Point64 cross(const Point64& b) const
122 {
123 return Point64(y * b.z - z * b.y, z * b.x - x * b.z, x * b.y - y * b.x);
124 }
125
126 int64_t dot(const Point32& b) const
127 {
128 return x * b.x + y * b.y + z * b.z;
129 }
130
131 int64_t dot(const Point64& b) const
132 {
133 return x * b.x + y * b.y + z * b.z;
134 }
135
136 Point32 operator+(const Point32& b) const
137 {
138 return Point32(x + b.x, y + b.y, z + b.z);
139 }
140
141 Point32 operator-(const Point32& b) const
142 {
143 return Point32(x - b.x, y - b.y, z - b.z);
144 }
145 };
146
147 class Int128 {
148 public:
149 uint64_t low;
150 uint64_t high;
151
152 Int128()
153 {
154 }
155
156 Int128(uint64_t low, uint64_t high)
157 : low(low)
158 , high(high)
159 {
160 }
161
162 Int128(uint64_t low)
163 : low(low)
164 , high(0)
165 {
166 }
167
168 Int128(int64_t value)
169 : low(value)
170 , high((value >= 0) ? 0 : (uint64_t)-1LL)
171 {
172 }
173
174 static Int128 mul(int64_t a, int64_t b);
175
176 static Int128 mul(uint64_t a, uint64_t b);
177
178 Int128 operator-() const
179 {
180 return Int128((uint64_t) - (int64_t)low, ~high + (low == 0));
181 }
182
183 Int128 operator+(const Int128& b) const
184 {
185 #ifdef USE_X86_64_ASM
186 Int128 result;
187 __asm__("addq %[bl], %[rl]\n\t"
188 "adcq %[bh], %[rh]\n\t"
189 : [rl] "=r"(result.low), [rh] "=r"(result.high)
190 : "0"(low), "1"(high), [bl] "g"(b.low), [bh] "g"(b.high)
191 : "cc");
192 return result;
193 #else
194 uint64_t lo = low + b.low;
195 return Int128(lo, high + b.high + (lo < low));
196 #endif
197 }
198
199 Int128 operator-(const Int128& b) const
200 {
201 #ifdef USE_X86_64_ASM
202 Int128 result;
203 __asm__("subq %[bl], %[rl]\n\t"
204 "sbbq %[bh], %[rh]\n\t"
205 : [rl] "=r"(result.low), [rh] "=r"(result.high)
206 : "0"(low), "1"(high), [bl] "g"(b.low), [bh] "g"(b.high)
207 : "cc");
208 return result;
209 #else
210 return *this + -b;
211 #endif
212 }
213
214 Int128& operator+=(const Int128& b)
215 {
216 #ifdef USE_X86_64_ASM
217 __asm__("addq %[bl], %[rl]\n\t"
218 "adcq %[bh], %[rh]\n\t"
219 : [rl] "=r"(low), [rh] "=r"(high)
220 : "0"(low), "1"(high), [bl] "g"(b.low), [bh] "g"(b.high)
221 : "cc");
222 #else
223 uint64_t lo = low + b.low;
224 if (lo < low) {
225 ++high;
226 }
227 low = lo;
228 high += b.high;
229 #endif
230 return *this;
231 }
232
233 Int128& operator++()
234 {
235 if (++low == 0) {
236 ++high;
237 }
238 return *this;
239 }
240
241 Int128 operator*(int64_t b) const;
242
243 btScalar toScalar() const
244 {
245 return ((int64_t)high >= 0) ? btScalar(high) * (btScalar(0x100000000LL) * btScalar(0x100000000LL)) + btScalar(low)
246 : -(-*this).toScalar();
247 }
248
249 int32_t getSign() const
250 {
251 return ((int64_t)high < 0) ? -1 : (high || low) ? 1 : 0;
252 }
253
254 bool operator<(const Int128& b) const
255 {
256 return (high < b.high) || ((high == b.high) && (low < b.low));
257 }
258
259 int32_t ucmp(const Int128& b) const
260 {
261 if (high < b.high) {
262 return -1;
263 }
264 if (high > b.high) {
265 return 1;
266 }
267 if (low < b.low) {
268 return -1;
269 }
270 if (low > b.low) {
271 return 1;
272 }
273 return 0;
274 }
275 };
276
277 class Rational64 {
278 private:
279 uint64_t m_numerator;
280 uint64_t m_denominator;
281 int32_t sign;
282
283 public:
284 Rational64(int64_t numerator, int64_t denominator)
285 {
286 if (numerator > 0) {
287 sign = 1;
288 m_numerator = (uint64_t)numerator;
289 }
290 else if (numerator < 0) {
291 sign = -1;
292 m_numerator = (uint64_t)-numerator;
293 }
294 else {
295 sign = 0;
296 m_numerator = 0;
297 }
298 if (denominator > 0) {
299 m_denominator = (uint64_t)denominator;
300 }
301 else if (denominator < 0) {
302 sign = -sign;
303 m_denominator = (uint64_t)-denominator;
304 }
305 else {
306 m_denominator = 0;
307 }
308 }
309
310 bool isNegativeInfinity() const
311 {
312 return (sign < 0) && (m_denominator == 0);
313 }
314
315 bool isNaN() const
316 {
317 return (sign == 0) && (m_denominator == 0);
318 }
319
320 int32_t compare(const Rational64& b) const;
321
322 btScalar toScalar() const
323 {
324 return sign * ((m_denominator == 0) ? SIMD_INFINITY : (btScalar)m_numerator / m_denominator);
325 }
326 };
327
328 class Rational128 {
329 private:
330 Int128 numerator;
331 Int128 denominator;
332 int32_t sign;
333 bool isInt64;
334
335 public:
336 Rational128(int64_t value)
337 {
338 if (value > 0) {
339 sign = 1;
340 this->numerator = value;
341 }
342 else if (value < 0) {
343 sign = -1;
344 this->numerator = -value;
345 }
346 else {
347 sign = 0;
348 this->numerator = (uint64_t)0;
349 }
350 this->denominator = (uint64_t)1;
351 isInt64 = true;
352 }
353
354 Rational128(const Int128& numerator, const Int128& denominator)
355 {
356 sign = numerator.getSign();
357 if (sign >= 0) {
358 this->numerator = numerator;
359 }
360 else {
361 this->numerator = -numerator;
362 }
363 int32_t dsign = denominator.getSign();
364 if (dsign >= 0) {
365 this->denominator = denominator;
366 }
367 else {
368 sign = -sign;
369 this->denominator = -denominator;
370 }
371 isInt64 = false;
372 }
373
374 int32_t compare(const Rational128& b) const;
375
376 int32_t compare(int64_t b) const;
377
378 btScalar toScalar() const
379 {
380 return sign * ((denominator.getSign() == 0) ? SIMD_INFINITY : numerator.toScalar() / denominator.toScalar());
381 }
382 };
383
384 class PointR128 {
385 public:
386 Int128 x;
387 Int128 y;
388 Int128 z;
389 Int128 denominator;
390
391 PointR128()
392 {
393 }
394
395 PointR128(Int128 x, Int128 y, Int128 z, Int128 denominator)
396 : x(x)
397 , y(y)
398 , z(z)
399 , denominator(denominator)
400 {
401 }
402
403 btScalar xvalue() const
404 {
405 return x.toScalar() / denominator.toScalar();
406 }
407
408 btScalar yvalue() const
409 {
410 return y.toScalar() / denominator.toScalar();
411 }
412
413 btScalar zvalue() const
414 {
415 return z.toScalar() / denominator.toScalar();
416 }
417 };
418
419 class Edge;
420 class Face;
421
422 class Vertex {
423 public:
424 Vertex* next;
425 Vertex* prev;
426 Edge* edges;
427 Face* firstNearbyFace;
428 Face* lastNearbyFace;
429 PointR128 point128;
430 Point32 point;
431 int32_t copy;
432
433 Vertex()
434 : next(NULL)
435 , prev(NULL)
436 , edges(NULL)
437 , firstNearbyFace(NULL)
438 , lastNearbyFace(NULL)
439 , copy(-1)
440 {
441 }
442
443 #ifdef DEBUG_CONVEX_HULL
444 void print()
445 {
446 printf("V%d (%d, %d, %d)", point.index, point.x, point.y, point.z);
447 }
448
449 void printGraph();
450 #endif
451
452 Point32 operator-(const Vertex& b) const
453 {
454 return point - b.point;
455 }
456
457 Rational128 dot(const Point64& b) const
458 {
459 return (point.index >= 0) ? Rational128(point.dot(b))
460 : Rational128(point128.x * b.x + point128.y * b.y + point128.z * b.z, point128.denominator);
461 }
462
463 btScalar xvalue() const
464 {
465 return (point.index >= 0) ? btScalar(point.x) : point128.xvalue();
466 }
467
468 btScalar yvalue() const
469 {
470 return (point.index >= 0) ? btScalar(point.y) : point128.yvalue();
471 }
472
473 btScalar zvalue() const
474 {
475 return (point.index >= 0) ? btScalar(point.z) : point128.zvalue();
476 }
477
478 void receiveNearbyFaces(Vertex* src)
479 {
480 if (lastNearbyFace) {
481 lastNearbyFace->nextWithSameNearbyVertex = src->firstNearbyFace;
482 }
483 else {
484 firstNearbyFace = src->firstNearbyFace;
485 }
486 if (src->lastNearbyFace) {
487 lastNearbyFace = src->lastNearbyFace;
488 }
489 for (Face* f = src->firstNearbyFace; f; f = f->nextWithSameNearbyVertex) {
490 btAssert(f->nearbyVertex == src);
491 f->nearbyVertex = this;
492 }
493 src->firstNearbyFace = NULL;
494 src->lastNearbyFace = NULL;
495 }
496 };
497
498 class Edge {
499 public:
500 Edge* next;
501 Edge* prev;
502 Edge* reverse;
503 Vertex* target;
504 Face* face;
505 int32_t copy;
506
507 ~Edge()
508 {
509 next = NULL;
510 prev = NULL;
511 reverse = NULL;
512 target = NULL;
513 face = NULL;
514 }
515
516 void link(Edge* n)
517 {
518 btAssert(reverse->target == n->reverse->target);
519 next = n;
520 n->prev = this;
521 }
522
523 #ifdef DEBUG_CONVEX_HULL
524 void print()
525 {
526 printf("E%p : %d -> %d, n=%p p=%p (0 %d\t%d\t%d) -> (%d %d %d)", this, reverse->target->point.index, target->point.index, next, prev,
527 reverse->target->point.x, reverse->target->point.y, reverse->target->point.z, target->point.x, target->point.y, target->point.z);
528 }
529 #endif
530 };
531
532 class Face {
533 public:
534 Face* next;
535 Vertex* nearbyVertex;
536 Face* nextWithSameNearbyVertex;
537 Point32 origin;
538 Point32 dir0;
539 Point32 dir1;
540
541 Face()
542 : next(NULL)
543 , nearbyVertex(NULL)
544 , nextWithSameNearbyVertex(NULL)
545 {
546 }
547
548 void init(Vertex* a, Vertex* b, Vertex* c)
549 {
550 nearbyVertex = a;
551 origin = a->point;
552 dir0 = *b - *a;
553 dir1 = *c - *a;
554 if (a->lastNearbyFace) {
555 a->lastNearbyFace->nextWithSameNearbyVertex = this;
556 }
557 else {
558 a->firstNearbyFace = this;
559 }
560 a->lastNearbyFace = this;
561 }
562
563 Point64 getNormal()
564 {
565 return dir0.cross(dir1);
566 }
567 };
568
569 template <typename UWord, typename UHWord>
570 class DMul {
571 private:
572 static uint32_t high(uint64_t value)
573 {
574 return (uint32_t)(value >> 32);
575 }
576
577 static uint32_t low(uint64_t value)
578 {
579 return (uint32_t)value;
580 }
581
582 static uint64_t mul(uint32_t a, uint32_t b)
583 {
584 return (uint64_t)a * (uint64_t)b;
585 }
586
587 static void shlHalf(uint64_t& value)
588 {
589 value <<= 32;
590 }
591
592 static uint64_t high(Int128 value)
593 {
594 return value.high;
595 }
596
597 static uint64_t low(Int128 value)
598 {
599 return value.low;
600 }
601
602 static Int128 mul(uint64_t a, uint64_t b)
603 {
604 return Int128::mul(a, b);
605 }
606
607 static void shlHalf(Int128& value)
608 {
609 value.high = value.low;
610 value.low = 0;
611 }
612
613 public:
614 static void mul(UWord a, UWord b, UWord& resLow, UWord& resHigh)
615 {
616 UWord p00 = mul(low(a), low(b));
617 UWord p01 = mul(low(a), high(b));
618 UWord p10 = mul(high(a), low(b));
619 UWord p11 = mul(high(a), high(b));
620 UWord p0110 = UWord(low(p01)) + UWord(low(p10));
621 p11 += high(p01);
622 p11 += high(p10);
623 p11 += high(p0110);
624 shlHalf(p0110);
625 p00 += p0110;
626 if (p00 < p0110) {
627 ++p11;
628 }
629 resLow = p00;
630 resHigh = p11;
631 }
632 };
633
634 private:
635 class IntermediateHull {
636 public:
637 Vertex* minXy;
638 Vertex* maxXy;
639 Vertex* minYx;
640 Vertex* maxYx;
641
642 IntermediateHull()
643 : minXy(NULL)
644 , maxXy(NULL)
645 , minYx(NULL)
646 , maxYx(NULL)
647 {
648 }
649
650 void print();
651 };
652
653 enum Orientation { NONE,
654 CLOCKWISE,
655 COUNTER_CLOCKWISE };
656
657 template <typename T>
658 class PoolArray {
659 private:
660 T* array;
661 int32_t size;
662
663 public:
664 PoolArray<T>* next;
665
666 PoolArray(int32_t size)
667 : size(size)
668 , next(NULL)
669 {
670 array = (T*)btAlignedAlloc(sizeof(T) * size, 16);
671 }
672
673 ~PoolArray()
674 {
675 btAlignedFree(array);
676 }
677
678 T* init()
679 {
680 T* o = array;
681 for (int32_t i = 0; i < size; i++, o++) {
682 o->next = (i + 1 < size) ? o + 1 : NULL;
683 }
684 return array;
685 }
686 };
687
688 template <typename T>
689 class Pool {
690 private:
691 PoolArray<T>* arrays;
692 PoolArray<T>* nextArray;
693 T* freeObjects;
694 int32_t arraySize;
695
696 public:
697 Pool()
698 : arrays(NULL)
699 , nextArray(NULL)
700 , freeObjects(NULL)
701 , arraySize(256)
702 {
703 }
704
705 ~Pool()
706 {
707 while (arrays) {
708 PoolArray<T>* p = arrays;
709 arrays = p->next;
710 p->~PoolArray<T>();
711 btAlignedFree(p);
712 }
713 }
714
715 void reset()
716 {
717 nextArray = arrays;
718 freeObjects = NULL;
719 }
720
721 void setArraySize(int32_t arraySize)
722 {
723 this->arraySize = arraySize;
724 }
725
726 T* newObject()
727 {
728 T* o = freeObjects;
729 if (!o) {
730 PoolArray<T>* p = nextArray;
731 if (p) {
732 nextArray = p->next;
733 }
734 else {
735 p = new (btAlignedAlloc(sizeof(PoolArray<T>), 16)) PoolArray<T>(arraySize);
736 p->next = arrays;
737 arrays = p;
738 }
739 o = p->init();
740 }
741 freeObjects = o->next;
742 return new (o) T();
743 };
744
745 void freeObject(T* object)
746 {
747 object->~T();
748 object->next = freeObjects;
749 freeObjects = object;
750 }
751 };
752
753 btVector3 scaling;
754 btVector3 center;
755 Pool<Vertex> vertexPool;
756 Pool<Edge> edgePool;
757 Pool<Face> facePool;
758 btAlignedObjectArray<Vertex*> originalVertices;
759 int32_t mergeStamp;
760 int32_t minAxis;
761 int32_t medAxis;
762 int32_t maxAxis;
763 int32_t usedEdgePairs;
764 int32_t maxUsedEdgePairs;
765
766 static Orientation getOrientation(const Edge* prev, const Edge* next, const Point32& s, const Point32& t);
767 Edge* findMaxAngle(bool ccw, const Vertex* start, const Point32& s, const Point64& rxs, const Point64& sxrxs, Rational64& minCot);
768 void findEdgeForCoplanarFaces(Vertex* c0, Vertex* c1, Edge*& e0, Edge*& e1, Vertex* stop0, Vertex* stop1);
769
770 Edge* newEdgePair(Vertex* from, Vertex* to);
771
772 void removeEdgePair(Edge* edge)
773 {
774 Edge* n = edge->next;
775 Edge* r = edge->reverse;
776
777 btAssert(edge->target && r->target);
778
779 if (n != edge) {
780 n->prev = edge->prev;
781 edge->prev->next = n;
782 r->target->edges = n;
783 }
784 else {
785 r->target->edges = NULL;
786 }
787
788 n = r->next;
789
790 if (n != r) {
791 n->prev = r->prev;
792 r->prev->next = n;
793 edge->target->edges = n;
794 }
795 else {
796 edge->target->edges = NULL;
797 }
798
799 edgePool.freeObject(edge);
800 edgePool.freeObject(r);
801 usedEdgePairs--;
802 }
803
804 void computeInternal(int32_t start, int32_t end, IntermediateHull& result);
805
806 bool mergeProjection(IntermediateHull& h0, IntermediateHull& h1, Vertex*& c0, Vertex*& c1);
807
808 void merge(IntermediateHull& h0, IntermediateHull& h1);
809
810 btVector3 toBtVector(const Point32& v);
811
812 btVector3 getBtNormal(Face* face);
813
814 bool shiftFace(Face* face, btScalar amount, btAlignedObjectArray<Vertex*> stack);
815
816 public:
817 Vertex* vertexList;
818
819 void compute(const void* coords, bool doubleCoords, int32_t stride, int32_t count);
820
821 btVector3 getCoordinates(const Vertex* v);
822
823 btScalar shrink(btScalar amount, btScalar clampAmount);
824 };
825
826 btConvexHullInternal::Int128 btConvexHullInternal::Int128::operator*(int64_t b) const
827 {
828 bool negative = (int64_t)high < 0;
829 Int128 a = negative ? -*this : *this;
830 if (b < 0) {
831 negative = !negative;
832 b = -b;
833 }
834 Int128 result = mul(a.low, (uint64_t)b);
835 result.high += a.high * (uint64_t)b;
836 return negative ? -result : result;
837 }
838
839 btConvexHullInternal::Int128 btConvexHullInternal::Int128::mul(int64_t a, int64_t b)
840 {
841 Int128 result;
842
843 #ifdef USE_X86_64_ASM
844 __asm__("imulq %[b]"
845 : "=a"(result.low), "=d"(result.high)
846 : "0"(a), [b] "r"(b)
847 : "cc");
848 return result;
849
850 #else
851 bool negative = a < 0;
852 if (negative) {
853 a = -a;
854 }
855 if (b < 0) {
856 negative = !negative;
857 b = -b;
858 }
859 DMul<uint64_t, uint32_t>::mul((uint64_t)a, (uint64_t)b, result.low, result.high);
860 return negative ? -result : result;
861 #endif
862 }
863
864 btConvexHullInternal::Int128 btConvexHullInternal::Int128::mul(uint64_t a, uint64_t b)
865 {
866 Int128 result;
867
868 #ifdef USE_X86_64_ASM
869 __asm__("mulq %[b]"
870 : "=a"(result.low), "=d"(result.high)
871 : "0"(a), [b] "r"(b)
872 : "cc");
873
874 #else
875 DMul<uint64_t, uint32_t>::mul(a, b, result.low, result.high);
876 #endif
877
878 return result;
879 }
880
881 int32_t btConvexHullInternal::Rational64::compare(const Rational64& b) const
882 {
883 if (sign != b.sign) {
884 return sign - b.sign;
885 }
886 else if (sign == 0) {
887 return 0;
888 }
889
890 // return (numerator * b.denominator > b.numerator * denominator) ? sign : (numerator * b.denominator < b.numerator * denominator) ? -sign : 0;
891
892 #ifdef USE_X86_64_ASM
893
894 int32_t result;
895 int64_t tmp;
896 int64_t dummy;
897 __asm__("mulq %[bn]\n\t"
898 "movq %%rax, %[tmp]\n\t"
899 "movq %%rdx, %%rbx\n\t"
900 "movq %[tn], %%rax\n\t"
901 "mulq %[bd]\n\t"
902 "subq %[tmp], %%rax\n\t"
903 "sbbq %%rbx, %%rdx\n\t" // rdx:rax contains 128-bit-difference "numerator*b.denominator - b.numerator*denominator"
904 "setnsb %%bh\n\t" // bh=1 if difference is non-negative, bh=0 otherwise
905 "orq %%rdx, %%rax\n\t"
906 "setnzb %%bl\n\t" // bl=1 if difference if non-zero, bl=0 if it is zero
907 "decb %%bh\n\t" // now bx=0x0000 if difference is zero, 0xff01 if it is negative, 0x0001 if it is positive (i.e., same sign as difference)
908 "shll $16, %%ebx\n\t" // ebx has same sign as difference
909 : "=&b"(result), [tmp] "=&r"(tmp), "=a"(dummy)
910 : "a"(denominator), [bn] "g"(b.numerator), [tn] "g"(numerator), [bd] "g"(b.denominator)
911 : "%rdx", "cc");
912 return result ? result ^ sign // if sign is +1, only bit 0 of result is inverted, which does not change the sign of result (and cannot result in zero)
913 // if sign is -1, all bits of result are inverted, which changes the sign of result (and again cannot result in zero)
914 : 0;
915
916 #else
917
918 return sign * Int128::mul(m_numerator, b.m_denominator).ucmp(Int128::mul(m_denominator, b.m_numerator));
919
920 #endif
921 }
922
923 int32_t btConvexHullInternal::Rational128::compare(const Rational128& b) const
924 {
925 if (sign != b.sign) {
926 return sign - b.sign;
927 }
928 else if (sign == 0) {
929 return 0;
930 }
931 if (isInt64) {
932 return -b.compare(sign * (int64_t)numerator.low);
933 }
934
935 Int128 nbdLow, nbdHigh, dbnLow, dbnHigh;
936 DMul<Int128, uint64_t>::mul(numerator, b.denominator, nbdLow, nbdHigh);
937 DMul<Int128, uint64_t>::mul(denominator, b.numerator, dbnLow, dbnHigh);
938
939 int32_t cmp = nbdHigh.ucmp(dbnHigh);
940 if (cmp) {
941 return cmp * sign;
942 }
943 return nbdLow.ucmp(dbnLow) * sign;
944 }
945
946 int32_t btConvexHullInternal::Rational128::compare(int64_t b) const
947 {
948 if (isInt64) {
949 int64_t a = sign * (int64_t)numerator.low;
950 return (a > b) ? 1 : (a < b) ? -1 : 0;
951 }
952 if (b > 0) {
953 if (sign <= 0) {
954 return -1;
955 }
956 }
957 else if (b < 0) {
958 if (sign >= 0) {
959 return 1;
960 }
961 b = -b;
962 }
963 else {
964 return sign;
965 }
966
967 return numerator.ucmp(denominator * b) * sign;
968 }
969
970 btConvexHullInternal::Edge* btConvexHullInternal::newEdgePair(Vertex* from, Vertex* to)
971 {
972 btAssert(from && to);
973 Edge* e = edgePool.newObject();
974 Edge* r = edgePool.newObject();
975 e->reverse = r;
976 r->reverse = e;
977 e->copy = mergeStamp;
978 r->copy = mergeStamp;
979 e->target = to;
980 r->target = from;
981 e->face = NULL;
982 r->face = NULL;
983 usedEdgePairs++;
984 if (usedEdgePairs > maxUsedEdgePairs) {
985 maxUsedEdgePairs = usedEdgePairs;
986 }
987 return e;
988 }
989
990 bool btConvexHullInternal::mergeProjection(IntermediateHull& h0, IntermediateHull& h1, Vertex*& c0, Vertex*& c1)
991 {
992 Vertex* v0 = h0.maxYx;
993 Vertex* v1 = h1.minYx;
994 if ((v0->point.x == v1->point.x) && (v0->point.y == v1->point.y)) {
995 btAssert(v0->point.z < v1->point.z);
996 Vertex* v1p = v1->prev;
997 if (v1p == v1) {
998 c0 = v0;
999 if (v1->edges) {
1000 btAssert(v1->edges->next == v1->edges);
1001 v1 = v1->edges->target;
1002 btAssert(v1->edges->next == v1->edges);
1003 }
1004 c1 = v1;
1005 return false;
1006 }
1007 Vertex* v1n = v1->next;
1008 v1p->next = v1n;
1009 v1n->prev = v1p;
1010 if (v1 == h1.minXy) {
1011 if ((v1n->point.x < v1p->point.x) || ((v1n->point.x == v1p->point.x) && (v1n->point.y < v1p->point.y))) {
1012 h1.minXy = v1n;
1013 }
1014 else {
1015 h1.minXy = v1p;
1016 }
1017 }
1018 if (v1 == h1.maxXy) {
1019 if ((v1n->point.x > v1p->point.x) || ((v1n->point.x == v1p->point.x) && (v1n->point.y > v1p->point.y))) {
1020 h1.maxXy = v1n;
1021 }
1022 else {
1023 h1.maxXy = v1p;
1024 }
1025 }
1026 }
1027
1028 v0 = h0.maxXy;
1029 v1 = h1.maxXy;
1030 Vertex* v00 = NULL;
1031 Vertex* v10 = NULL;
1032 int32_t sign = 1;
1033
1034 for (int32_t side = 0; side <= 1; side++) {
1035 int32_t dx = (v1->point.x - v0->point.x) * sign;
1036 if (dx > 0) {
1037 while (true) {
1038 int32_t dy = v1->point.y - v0->point.y;
1039
1040 Vertex* w0 = side ? v0->next : v0->prev;
1041 if (w0 != v0) {
1042 int32_t dx0 = (w0->point.x - v0->point.x) * sign;
1043 int32_t dy0 = w0->point.y - v0->point.y;
1044 if ((dy0 <= 0) && ((dx0 == 0) || ((dx0 < 0) && (dy0 * dx <= dy * dx0)))) {
1045 v0 = w0;
1046 dx = (v1->point.x - v0->point.x) * sign;
1047 continue;
1048 }
1049 }
1050
1051 Vertex* w1 = side ? v1->next : v1->prev;
1052 if (w1 != v1) {
1053 int32_t dx1 = (w1->point.x - v1->point.x) * sign;
1054 int32_t dy1 = w1->point.y - v1->point.y;
1055 int32_t dxn = (w1->point.x - v0->point.x) * sign;
1056 if ((dxn > 0) && (dy1 < 0) && ((dx1 == 0) || ((dx1 < 0) && (dy1 * dx < dy * dx1)))) {
1057 v1 = w1;
1058 dx = dxn;
1059 continue;
1060 }
1061 }
1062
1063 break;
1064 }
1065 }
1066 else if (dx < 0) {
1067 while (true) {
1068 int32_t dy = v1->point.y - v0->point.y;
1069
1070 Vertex* w1 = side ? v1->prev : v1->next;
1071 if (w1 != v1) {
1072 int32_t dx1 = (w1->point.x - v1->point.x) * sign;
1073 int32_t dy1 = w1->point.y - v1->point.y;
1074 if ((dy1 >= 0) && ((dx1 == 0) || ((dx1 < 0) && (dy1 * dx <= dy * dx1)))) {
1075 v1 = w1;
1076 dx = (v1->point.x - v0->point.x) * sign;
1077 continue;
1078 }
1079 }
1080
1081 Vertex* w0 = side ? v0->prev : v0->next;
1082 if (w0 != v0) {
1083 int32_t dx0 = (w0->point.x - v0->point.x) * sign;
1084 int32_t dy0 = w0->point.y - v0->point.y;
1085 int32_t dxn = (v1->point.x - w0->point.x) * sign;
1086 if ((dxn < 0) && (dy0 > 0) && ((dx0 == 0) || ((dx0 < 0) && (dy0 * dx < dy * dx0)))) {
1087 v0 = w0;
1088 dx = dxn;
1089 continue;
1090 }
1091 }
1092
1093 break;
1094 }
1095 }
1096 else {
1097 int32_t x = v0->point.x;
1098 int32_t y0 = v0->point.y;
1099 Vertex* w0 = v0;
1100 Vertex* t;
1101 while (((t = side ? w0->next : w0->prev) != v0) && (t->point.x == x) && (t->point.y <= y0)) {
1102 w0 = t;
1103 y0 = t->point.y;
1104 }
1105 v0 = w0;
1106
1107 int32_t y1 = v1->point.y;
1108 Vertex* w1 = v1;
1109 while (((t = side ? w1->prev : w1->next) != v1) && (t->point.x == x) && (t->point.y >= y1)) {
1110 w1 = t;
1111 y1 = t->point.y;
1112 }
1113 v1 = w1;
1114 }
1115
1116 if (side == 0) {
1117 v00 = v0;
1118 v10 = v1;
1119
1120 v0 = h0.minXy;
1121 v1 = h1.minXy;
1122 sign = -1;
1123 }
1124 }
1125
1126 v0->prev = v1;
1127 v1->next = v0;
1128
1129 v00->next = v10;
1130 v10->prev = v00;
1131
1132 if (h1.minXy->point.x < h0.minXy->point.x) {
1133 h0.minXy = h1.minXy;
1134 }
1135 if (h1.maxXy->point.x >= h0.maxXy->point.x) {
1136 h0.maxXy = h1.maxXy;
1137 }
1138
1139 h0.maxYx = h1.maxYx;
1140
1141 c0 = v00;
1142 c1 = v10;
1143
1144 return true;
1145 }
1146
1147 void btConvexHullInternal::computeInternal(int32_t start, int32_t end, IntermediateHull& result)
1148 {
1149 int32_t n = end - start;
1150 switch (n) {
1151 case 0:
1152 result.minXy = NULL;
1153 result.maxXy = NULL;
1154 result.minYx = NULL;
1155 result.maxYx = NULL;
1156 return;
1157 case 2: {
1158 Vertex* v = originalVertices[start];
1159 Vertex* w = v + 1;
1160 if (v->point != w->point) {
1161 int32_t dx = v->point.x - w->point.x;
1162 int32_t dy = v->point.y - w->point.y;
1163
1164 if ((dx == 0) && (dy == 0)) {
1165 if (v->point.z > w->point.z) {
1166 Vertex* t = w;
1167 w = v;
1168 v = t;
1169 }
1170 btAssert(v->point.z < w->point.z);
1171 v->next = v;
1172 v->prev = v;
1173 result.minXy = v;
1174 result.maxXy = v;
1175 result.minYx = v;
1176 result.maxYx = v;
1177 }
1178 else {
1179 v->next = w;
1180 v->prev = w;
1181 w->next = v;
1182 w->prev = v;
1183
1184 if ((dx < 0) || ((dx == 0) && (dy < 0))) {
1185 result.minXy = v;
1186 result.maxXy = w;
1187 }
1188 else {
1189 result.minXy = w;
1190 result.maxXy = v;
1191 }
1192
1193 if ((dy < 0) || ((dy == 0) && (dx < 0))) {
1194 result.minYx = v;
1195 result.maxYx = w;
1196 }
1197 else {
1198 result.minYx = w;
1199 result.maxYx = v;
1200 }
1201 }
1202
1203 Edge* e = newEdgePair(v, w);
1204 e->link(e);
1205 v->edges = e;
1206
1207 e = e->reverse;
1208 e->link(e);
1209 w->edges = e;
1210
1211 return;
1212 }
1213 }
1214 // lint -fallthrough
1215 case 1: {
1216 Vertex* v = originalVertices[start];
1217 v->edges = NULL;
1218 v->next = v;
1219 v->prev = v;
1220
1221 result.minXy = v;
1222 result.maxXy = v;
1223 result.minYx = v;
1224 result.maxYx = v;
1225
1226 return;
1227 }
1228 }
1229
1230 int32_t split0 = start + n / 2;
1231 Point32 p = originalVertices[split0 - 1]->point;
1232 int32_t split1 = split0;
1233 while ((split1 < end) && (originalVertices[split1]->point == p)) {
1234 split1++;
1235 }
1236 computeInternal(start, split0, result);
1237 IntermediateHull hull1;
1238 computeInternal(split1, end, hull1);
1239 #ifdef DEBUG_CONVEX_HULL
1240 printf("\n\nMerge\n");
1241 result.print();
1242 hull1.print();
1243 #endif
1244 merge(result, hull1);
1245 #ifdef DEBUG_CONVEX_HULL
1246 printf("\n Result\n");
1247 result.print();
1248 #endif
1249 }
1250
1251 #ifdef DEBUG_CONVEX_HULL
1252 void btConvexHullInternal::IntermediateHull::print()
1253 {
1254 printf(" Hull\n");
1255 for (Vertex* v = minXy; v;) {
1256 printf(" ");
1257 v->print();
1258 if (v == maxXy) {
1259 printf(" maxXy");
1260 }
1261 if (v == minYx) {
1262 printf(" minYx");
1263 }
1264 if (v == maxYx) {
1265 printf(" maxYx");
1266 }
1267 if (v->next->prev != v) {
1268 printf(" Inconsistency");
1269 }
1270 printf("\n");
1271 v = v->next;
1272 if (v == minXy) {
1273 break;
1274 }
1275 }
1276 if (minXy) {
1277 minXy->copy = (minXy->copy == -1) ? -2 : -1;
1278 minXy->printGraph();
1279 }
1280 }
1281
1282 void btConvexHullInternal::Vertex::printGraph()
1283 {
1284 print();
1285 printf("\nEdges\n");
1286 Edge* e = edges;
1287 if (e) {
1288 do {
1289 e->print();
1290 printf("\n");
1291 e = e->next;
1292 } while (e != edges);
1293 do {
1294 Vertex* v = e->target;
1295 if (v->copy != copy) {
1296 v->copy = copy;
1297 v->printGraph();
1298 }
1299 e = e->next;
1300 } while (e != edges);
1301 }
1302 }
1303 #endif
1304
1305 btConvexHullInternal::Orientation btConvexHullInternal::getOrientation(const Edge* prev, const Edge* next, const Point32& s, const Point32& t)
1306 {
1307 btAssert(prev->reverse->target == next->reverse->target);
1308 if (prev->next == next) {
1309 if (prev->prev == next) {
1310 Point64 n = t.cross(s);
1311 Point64 m = (*prev->target - *next->reverse->target).cross(*next->target - *next->reverse->target);
1312 btAssert(!m.isZero());
1313 int64_t dot = n.dot(m);
1314 btAssert(dot != 0);
1315 return (dot > 0) ? COUNTER_CLOCKWISE : CLOCKWISE;
1316 }
1317 return COUNTER_CLOCKWISE;
1318 }
1319 else if (prev->prev == next) {
1320 return CLOCKWISE;
1321 }
1322 else {
1323 return NONE;
1324 }
1325 }
1326
1327 btConvexHullInternal::Edge* btConvexHullInternal::findMaxAngle(bool ccw, const Vertex* start, const Point32& s, const Point64& rxs, const Point64& sxrxs, Rational64& minCot)
1328 {
1329 Edge* minEdge = NULL;
1330
1331 #ifdef DEBUG_CONVEX_HULL
1332 printf("find max edge for %d\n", start->point.index);
1333 #endif
1334 Edge* e = start->edges;
1335 if (e) {
1336 do {
1337 if (e->copy > mergeStamp) {
1338 Point32 t = *e->target - *start;
1339 Rational64 cot(t.dot(sxrxs), t.dot(rxs));
1340 #ifdef DEBUG_CONVEX_HULL
1341 printf(" Angle is %f (%d) for ", (float)btAtan(cot.toScalar()), (int32_t)cot.isNaN());
1342 e->print();
1343 #endif
1344 if (cot.isNaN()) {
1345 btAssert(ccw ? (t.dot(s) < 0) : (t.dot(s) > 0));
1346 }
1347 else {
1348 int32_t cmp;
1349 if (minEdge == NULL) {
1350 minCot = cot;
1351 minEdge = e;
1352 }
1353 else if ((cmp = cot.compare(minCot)) < 0) {
1354 minCot = cot;
1355 minEdge = e;
1356 }
1357 else if ((cmp == 0) && (ccw == (getOrientation(minEdge, e, s, t) == COUNTER_CLOCKWISE))) {
1358 minEdge = e;
1359 }
1360 }
1361 #ifdef DEBUG_CONVEX_HULL
1362 printf("\n");
1363 #endif
1364 }
1365 e = e->next;
1366 } while (e != start->edges);
1367 }
1368 return minEdge;
1369 }
1370
1371 void btConvexHullInternal::findEdgeForCoplanarFaces(Vertex* c0, Vertex* c1, Edge*& e0, Edge*& e1, Vertex* stop0, Vertex* stop1)
1372 {
1373 Edge* start0 = e0;
1374 Edge* start1 = e1;
1375 Point32 et0 = start0 ? start0->target->point : c0->point;
1376 Point32 et1 = start1 ? start1->target->point : c1->point;
1377 Point32 s = c1->point - c0->point;
1378 Point64 normal = ((start0 ? start0 : start1)->target->point - c0->point).cross(s);
1379 int64_t dist = c0->point.dot(normal);
1380 btAssert(!start1 || (start1->target->point.dot(normal) == dist));
1381 Point64 perp = s.cross(normal);
1382 btAssert(!perp.isZero());
1383
1384 #ifdef DEBUG_CONVEX_HULL
1385 printf(" Advancing %d %d (%p %p, %d %d)\n", c0->point.index, c1->point.index, start0, start1, start0 ? start0->target->point.index : -1, start1 ? start1->target->point.index : -1);
1386 #endif
1387
1388 int64_t maxDot0 = et0.dot(perp);
1389 if (e0) {
1390 while (e0->target != stop0) {
1391 Edge* e = e0->reverse->prev;
1392 if (e->target->point.dot(normal) < dist) {
1393 break;
1394 }
1395 btAssert(e->target->point.dot(normal) == dist);
1396 if (e->copy == mergeStamp) {
1397 break;
1398 }
1399 int64_t dot = e->target->point.dot(perp);
1400 if (dot <= maxDot0) {
1401 break;
1402 }
1403 maxDot0 = dot;
1404 e0 = e;
1405 et0 = e->target->point;
1406 }
1407 }
1408
1409 int64_t maxDot1 = et1.dot(perp);
1410 if (e1) {
1411 while (e1->target != stop1) {
1412 Edge* e = e1->reverse->next;
1413 if (e->target->point.dot(normal) < dist) {
1414 break;
1415 }
1416 btAssert(e->target->point.dot(normal) == dist);
1417 if (e->copy == mergeStamp) {
1418 break;
1419 }
1420 int64_t dot = e->target->point.dot(perp);
1421 if (dot <= maxDot1) {
1422 break;
1423 }
1424 maxDot1 = dot;
1425 e1 = e;
1426 et1 = e->target->point;
1427 }
1428 }
1429
1430 #ifdef DEBUG_CONVEX_HULL
1431 printf(" Starting at %d %d\n", et0.index, et1.index);
1432 #endif
1433
1434 int64_t dx = maxDot1 - maxDot0;
1435 if (dx > 0) {
1436 while (true) {
1437 int64_t dy = (et1 - et0).dot(s);
1438
1439 if (e0 && (e0->target != stop0)) {
1440 Edge* f0 = e0->next->reverse;
1441 if (f0->copy > mergeStamp) {
1442 int64_t dx0 = (f0->target->point - et0).dot(perp);
1443 int64_t dy0 = (f0->target->point - et0).dot(s);
1444 if ((dx0 == 0) ? (dy0 < 0) : ((dx0 < 0) && (Rational64(dy0, dx0).compare(Rational64(dy, dx)) >= 0))) {
1445 et0 = f0->target->point;
1446 dx = (et1 - et0).dot(perp);
1447 e0 = (e0 == start0) ? NULL : f0;
1448 continue;
1449 }
1450 }
1451 }
1452
1453 if (e1 && (e1->target != stop1)) {
1454 Edge* f1 = e1->reverse->next;
1455 if (f1->copy > mergeStamp) {
1456 Point32 d1 = f1->target->point - et1;
1457 if (d1.dot(normal) == 0) {
1458 int64_t dx1 = d1.dot(perp);
1459 int64_t dy1 = d1.dot(s);
1460 int64_t dxn = (f1->target->point - et0).dot(perp);
1461 if ((dxn > 0) && ((dx1 == 0) ? (dy1 < 0) : ((dx1 < 0) && (Rational64(dy1, dx1).compare(Rational64(dy, dx)) > 0)))) {
1462 e1 = f1;
1463 et1 = e1->target->point;
1464 dx = dxn;
1465 continue;
1466 }
1467 }
1468 else {
1469 btAssert((e1 == start1) && (d1.dot(normal) < 0));
1470 }
1471 }
1472 }
1473
1474 break;
1475 }
1476 }
1477 else if (dx < 0) {
1478 while (true) {
1479 int64_t dy = (et1 - et0).dot(s);
1480
1481 if (e1 && (e1->target != stop1)) {
1482 Edge* f1 = e1->prev->reverse;
1483 if (f1->copy > mergeStamp) {
1484 int64_t dx1 = (f1->target->point - et1).dot(perp);
1485 int64_t dy1 = (f1->target->point - et1).dot(s);
1486 if ((dx1 == 0) ? (dy1 > 0) : ((dx1 < 0) && (Rational64(dy1, dx1).compare(Rational64(dy, dx)) <= 0))) {
1487 et1 = f1->target->point;
1488 dx = (et1 - et0).dot(perp);
1489 e1 = (e1 == start1) ? NULL : f1;
1490 continue;
1491 }
1492 }
1493 }
1494
1495 if (e0 && (e0->target != stop0)) {
1496 Edge* f0 = e0->reverse->prev;
1497 if (f0->copy > mergeStamp) {
1498 Point32 d0 = f0->target->point - et0;
1499 if (d0.dot(normal) == 0) {
1500 int64_t dx0 = d0.dot(perp);
1501 int64_t dy0 = d0.dot(s);
1502 int64_t dxn = (et1 - f0->target->point).dot(perp);
1503 if ((dxn < 0) && ((dx0 == 0) ? (dy0 > 0) : ((dx0 < 0) && (Rational64(dy0, dx0).compare(Rational64(dy, dx)) < 0)))) {
1504 e0 = f0;
1505 et0 = e0->target->point;
1506 dx = dxn;
1507 continue;
1508 }
1509 }
1510 else {
1511 btAssert((e0 == start0) && (d0.dot(normal) < 0));
1512 }
1513 }
1514 }
1515
1516 break;
1517 }
1518 }
1519 #ifdef DEBUG_CONVEX_HULL
1520 printf(" Advanced edges to %d %d\n", et0.index, et1.index);
1521 #endif
1522 }
1523
1524 void btConvexHullInternal::merge(IntermediateHull& h0, IntermediateHull& h1)
1525 {
1526 if (!h1.maxXy) {
1527 return;
1528 }
1529 if (!h0.maxXy) {
1530 h0 = h1;
1531 return;
1532 }
1533
1534 mergeStamp--;
1535
1536 Vertex* c0 = NULL;
1537 Edge* toPrev0 = NULL;
1538 Edge* firstNew0 = NULL;
1539 Edge* pendingHead0 = NULL;
1540 Edge* pendingTail0 = NULL;
1541 Vertex* c1 = NULL;
1542 Edge* toPrev1 = NULL;
1543 Edge* firstNew1 = NULL;
1544 Edge* pendingHead1 = NULL;
1545 Edge* pendingTail1 = NULL;
1546 Point32 prevPoint;
1547
1548 if (mergeProjection(h0, h1, c0, c1)) {
1549 Point32 s = *c1 - *c0;
1550 Point64 normal = Point32(0, 0, -1).cross(s);
1551 Point64 t = s.cross(normal);
1552 btAssert(!t.isZero());
1553
1554 Edge* e = c0->edges;
1555 Edge* start0 = NULL;
1556 if (e) {
1557 do {
1558 int64_t dot = (*e->target - *c0).dot(normal);
1559 btAssert(dot <= 0);
1560 if ((dot == 0) && ((*e->target - *c0).dot(t) > 0)) {
1561 if (!start0 || (getOrientation(start0, e, s, Point32(0, 0, -1)) == CLOCKWISE)) {
1562 start0 = e;
1563 }
1564 }
1565 e = e->next;
1566 } while (e != c0->edges);
1567 }
1568
1569 e = c1->edges;
1570 Edge* start1 = NULL;
1571 if (e) {
1572 do {
1573 int64_t dot = (*e->target - *c1).dot(normal);
1574 btAssert(dot <= 0);
1575 if ((dot == 0) && ((*e->target - *c1).dot(t) > 0)) {
1576 if (!start1 || (getOrientation(start1, e, s, Point32(0, 0, -1)) == COUNTER_CLOCKWISE)) {
1577 start1 = e;
1578 }
1579 }
1580 e = e->next;
1581 } while (e != c1->edges);
1582 }
1583
1584 if (start0 || start1) {
1585 findEdgeForCoplanarFaces(c0, c1, start0, start1, NULL, NULL);
1586 if (start0) {
1587 c0 = start0->target;
1588 }
1589 if (start1) {
1590 c1 = start1->target;
1591 }
1592 }
1593
1594 prevPoint = c1->point;
1595 prevPoint.z++;
1596 }
1597 else {
1598 prevPoint = c1->point;
1599 prevPoint.x++;
1600 }
1601
1602 Vertex* first0 = c0;
1603 Vertex* first1 = c1;
1604 bool firstRun = true;
1605
1606 while (true) {
1607 Point32 s = *c1 - *c0;
1608 Point32 r = prevPoint - c0->point;
1609 Point64 rxs = r.cross(s);
1610 Point64 sxrxs = s.cross(rxs);
1611
1612 #ifdef DEBUG_CONVEX_HULL
1613 printf("\n Checking %d %d\n", c0->point.index, c1->point.index);
1614 #endif
1615 Rational64 minCot0(0, 0);
1616 Edge* min0 = findMaxAngle(false, c0, s, rxs, sxrxs, minCot0);
1617 Rational64 minCot1(0, 0);
1618 Edge* min1 = findMaxAngle(true, c1, s, rxs, sxrxs, minCot1);
1619 if (!min0 && !min1) {
1620 Edge* e = newEdgePair(c0, c1);
1621 e->link(e);
1622 c0->edges = e;
1623
1624 e = e->reverse;
1625 e->link(e);
1626 c1->edges = e;
1627 return;
1628 }
1629 else {
1630 int32_t cmp = !min0 ? 1 : !min1 ? -1 : minCot0.compare(minCot1);
1631 #ifdef DEBUG_CONVEX_HULL
1632 printf(" -> Result %d\n", cmp);
1633 #endif
1634 if (firstRun || ((cmp >= 0) ? !minCot1.isNegativeInfinity() : !minCot0.isNegativeInfinity())) {
1635 Edge* e = newEdgePair(c0, c1);
1636 if (pendingTail0) {
1637 pendingTail0->prev = e;
1638 }
1639 else {
1640 pendingHead0 = e;
1641 }
1642 e->next = pendingTail0;
1643 pendingTail0 = e;
1644
1645 e = e->reverse;
1646 if (pendingTail1) {
1647 pendingTail1->next = e;
1648 }
1649 else {
1650 pendingHead1 = e;
1651 }
1652 e->prev = pendingTail1;
1653 pendingTail1 = e;
1654 }
1655
1656 Edge* e0 = min0;
1657 Edge* e1 = min1;
1658
1659 #ifdef DEBUG_CONVEX_HULL
1660 printf(" Found min edges to %d %d\n", e0 ? e0->target->point.index : -1, e1 ? e1->target->point.index : -1);
1661 #endif
1662
1663 if (cmp == 0) {
1664 findEdgeForCoplanarFaces(c0, c1, e0, e1, NULL, NULL);
1665 }
1666
1667 if ((cmp >= 0) && e1) {
1668 if (toPrev1) {
1669 for (Edge *e = toPrev1->next, *n = NULL; e != min1; e = n) {
1670 n = e->next;
1671 removeEdgePair(e);
1672 }
1673 }
1674
1675 if (pendingTail1) {
1676 if (toPrev1) {
1677 toPrev1->link(pendingHead1);
1678 }
1679 else {
1680 min1->prev->link(pendingHead1);
1681 firstNew1 = pendingHead1;
1682 }
1683 pendingTail1->link(min1);
1684 pendingHead1 = NULL;
1685 pendingTail1 = NULL;
1686 }
1687 else if (!toPrev1) {
1688 firstNew1 = min1;
1689 }
1690
1691 prevPoint = c1->point;
1692 c1 = e1->target;
1693 toPrev1 = e1->reverse;
1694 }
1695
1696 if ((cmp <= 0) && e0) {
1697 if (toPrev0) {
1698 for (Edge *e = toPrev0->prev, *n = NULL; e != min0; e = n) {
1699 n = e->prev;
1700 removeEdgePair(e);
1701 }
1702 }
1703
1704 if (pendingTail0) {
1705 if (toPrev0) {
1706 pendingHead0->link(toPrev0);
1707 }
1708 else {
1709 pendingHead0->link(min0->next);
1710 firstNew0 = pendingHead0;
1711 }
1712 min0->link(pendingTail0);
1713 pendingHead0 = NULL;
1714 pendingTail0 = NULL;
1715 }
1716 else if (!toPrev0) {
1717 firstNew0 = min0;
1718 }
1719
1720 prevPoint = c0->point;
1721 c0 = e0->target;
1722 toPrev0 = e0->reverse;
1723 }
1724 }
1725
1726 if ((c0 == first0) && (c1 == first1)) {
1727 if (toPrev0 == NULL) {
1728 pendingHead0->link(pendingTail0);
1729 c0->edges = pendingTail0;
1730 }
1731 else {
1732 for (Edge *e = toPrev0->prev, *n = NULL; e != firstNew0; e = n) {
1733 n = e->prev;
1734 removeEdgePair(e);
1735 }
1736 if (pendingTail0) {
1737 pendingHead0->link(toPrev0);
1738 firstNew0->link(pendingTail0);
1739 }
1740 }
1741
1742 if (toPrev1 == NULL) {
1743 pendingTail1->link(pendingHead1);
1744 c1->edges = pendingTail1;
1745 }
1746 else {
1747 for (Edge *e = toPrev1->next, *n = NULL; e != firstNew1; e = n) {
1748 n = e->next;
1749 removeEdgePair(e);
1750 }
1751 if (pendingTail1) {
1752 toPrev1->link(pendingHead1);
1753 pendingTail1->link(firstNew1);
1754 }
1755 }
1756
1757 return;
1758 }
1759
1760 firstRun = false;
1761 }
1762 }
1763
1764 static bool pointCmp(const btConvexHullInternal::Point32& p, const btConvexHullInternal::Point32& q)
1765 {
1766 return (p.y < q.y) || ((p.y == q.y) && ((p.x < q.x) || ((p.x == q.x) && (p.z < q.z))));
1767 }
1768
1769 void btConvexHullInternal::compute(const void* coords, bool doubleCoords, int32_t stride, int32_t count)
1770 {
1771 btVector3 min(btScalar(1e30), btScalar(1e30), btScalar(1e30)), max(btScalar(-1e30), btScalar(-1e30), btScalar(-1e30));
1772 const char* ptr = (const char*)coords;
1773 if (doubleCoords) {
1774 for (int32_t i = 0; i < count; i++) {
1775 const double* v = (const double*)ptr;
1776 btVector3 p((btScalar)v[0], (btScalar)v[1], (btScalar)v[2]);
1777 ptr += stride;
1778 min.setMin(p);
1779 max.setMax(p);
1780 }
1781 }
1782 else {
1783 for (int32_t i = 0; i < count; i++) {
1784 const float* v = (const float*)ptr;
1785 btVector3 p(v[0], v[1], v[2]);
1786 ptr += stride;
1787 min.setMin(p);
1788 max.setMax(p);
1789 }
1790 }
1791
1792 btVector3 s = max - min;
1793 maxAxis = s.maxAxis();
1794 minAxis = s.minAxis();
1795 if (minAxis == maxAxis) {
1796 minAxis = (maxAxis + 1) % 3;
1797 }
1798 medAxis = 3 - maxAxis - minAxis;
1799
1800 s /= btScalar(10216);
1801 if (((medAxis + 1) % 3) != maxAxis) {
1802 s *= -1;
1803 }
1804 scaling = s;
1805
1806 if (s[0] != 0) {
1807 s[0] = btScalar(1) / s[0];
1808 }
1809 if (s[1] != 0) {
1810 s[1] = btScalar(1) / s[1];
1811 }
1812 if (s[2] != 0) {
1813 s[2] = btScalar(1) / s[2];
1814 }
1815
1816 center = (min + max) * btScalar(0.5);
1817
1818 btAlignedObjectArray<Point32> points;
1819 points.resize(count);
1820 ptr = (const char*)coords;
1821 if (doubleCoords) {
1822 for (int32_t i = 0; i < count; i++) {
1823 const double* v = (const double*)ptr;
1824 btVector3 p((btScalar)v[0], (btScalar)v[1], (btScalar)v[2]);
1825 ptr += stride;
1826 p = (p - center) * s;
1827 points[i].x = (int32_t)p[medAxis];
1828 points[i].y = (int32_t)p[maxAxis];
1829 points[i].z = (int32_t)p[minAxis];
1830 points[i].index = i;
1831 }
1832 }
1833 else {
1834 for (int32_t i = 0; i < count; i++) {
1835 const float* v = (const float*)ptr;
1836 btVector3 p(v[0], v[1], v[2]);
1837 ptr += stride;
1838 p = (p - center) * s;
1839 points[i].x = (int32_t)p[medAxis];
1840 points[i].y = (int32_t)p[maxAxis];
1841 points[i].z = (int32_t)p[minAxis];
1842 points[i].index = i;
1843 }
1844 }
1845 points.quickSort(pointCmp);
1846
1847 vertexPool.reset();
1848 vertexPool.setArraySize(count);
1849 originalVertices.resize(count);
1850 for (int32_t i = 0; i < count; i++) {
1851 Vertex* v = vertexPool.newObject();
1852 v->edges = NULL;
1853 v->point = points[i];
1854 v->copy = -1;
1855 originalVertices[i] = v;
1856 }
1857
1858 points.clear();
1859
1860 edgePool.reset();
1861 edgePool.setArraySize(6 * count);
1862
1863 usedEdgePairs = 0;
1864 maxUsedEdgePairs = 0;
1865
1866 mergeStamp = -3;
1867
1868 IntermediateHull hull;
1869 computeInternal(0, count, hull);
1870 vertexList = hull.minXy;
1871 #ifdef DEBUG_CONVEX_HULL
1872 printf("max. edges %d (3v = %d)", maxUsedEdgePairs, 3 * count);
1873 #endif
1874 }
1875
1876 btVector3 btConvexHullInternal::toBtVector(const Point32& v)
1877 {
1878 btVector3 p;
1879 p[medAxis] = btScalar(v.x);
1880 p[maxAxis] = btScalar(v.y);
1881 p[minAxis] = btScalar(v.z);
1882 return p * scaling;
1883 }
1884
1885 btVector3 btConvexHullInternal::getBtNormal(Face* face)
1886 {
1887 return toBtVector(face->dir0).cross(toBtVector(face->dir1)).normalized();
1888 }
1889
1890 btVector3 btConvexHullInternal::getCoordinates(const Vertex* v)
1891 {
1892 btVector3 p;
1893 p[medAxis] = v->xvalue();
1894 p[maxAxis] = v->yvalue();
1895 p[minAxis] = v->zvalue();
1896 return p * scaling + center;
1897 }
1898
1899 btScalar btConvexHullInternal::shrink(btScalar amount, btScalar clampAmount)
1900 {
1901 if (!vertexList) {
1902 return 0;
1903 }
1904 int32_t stamp = --mergeStamp;
1905 btAlignedObjectArray<Vertex*> stack;
1906 vertexList->copy = stamp;
1907 stack.push_back(vertexList);
1908 btAlignedObjectArray<Face*> faces;
1909
1910 Point32 ref = vertexList->point;
1911 Int128 hullCenterX(0, 0);
1912 Int128 hullCenterY(0, 0);
1913 Int128 hullCenterZ(0, 0);
1914 Int128 volume(0, 0);
1915
1916 while (stack.size() > 0) {
1917 Vertex* v = stack[stack.size() - 1];
1918 stack.pop_back();
1919 Edge* e = v->edges;
1920 if (e) {
1921 do {
1922 if (e->target->copy != stamp) {
1923 e->target->copy = stamp;
1924 stack.push_back(e->target);
1925 }
1926 if (e->copy != stamp) {
1927 Face* face = facePool.newObject();
1928 face->init(e->target, e->reverse->prev->target, v);
1929 faces.push_back(face);
1930 Edge* f = e;
1931
1932 Vertex* a = NULL;
1933 Vertex* b = NULL;
1934 do {
1935 if (a && b) {
1936 int64_t vol = (v->point - ref).dot((a->point - ref).cross(b->point - ref));
1937 btAssert(vol >= 0);
1938 Point32 c = v->point + a->point + b->point + ref;
1939 hullCenterX += vol * c.x;
1940 hullCenterY += vol * c.y;
1941 hullCenterZ += vol * c.z;
1942 volume += vol;
1943 }
1944
1945 btAssert(f->copy != stamp);
1946 f->copy = stamp;
1947 f->face = face;
1948
1949 a = b;
1950 b = f->target;
1951
1952 f = f->reverse->prev;
1953 } while (f != e);
1954 }
1955 e = e->next;
1956 } while (e != v->edges);
1957 }
1958 }
1959
1960 if (volume.getSign() <= 0) {
1961 return 0;
1962 }
1963
1964 btVector3 hullCenter;
1965 hullCenter[medAxis] = hullCenterX.toScalar();
1966 hullCenter[maxAxis] = hullCenterY.toScalar();
1967 hullCenter[minAxis] = hullCenterZ.toScalar();
1968 hullCenter /= 4 * volume.toScalar();
1969 hullCenter *= scaling;
1970
1971 int32_t faceCount = faces.size();
1972
1973 if (clampAmount > 0) {
1974 btScalar minDist = SIMD_INFINITY;
1975 for (int32_t i = 0; i < faceCount; i++) {
1976 btVector3 normal = getBtNormal(faces[i]);
1977 btScalar dist = normal.dot(toBtVector(faces[i]->origin) - hullCenter);
1978 if (dist < minDist) {
1979 minDist = dist;
1980 }
1981 }
1982
1983 if (minDist <= 0) {
1984 return 0;
1985 }
1986
1987 amount = btMin(amount, minDist * clampAmount);
1988 }
1989
1990 uint32_t seed = 243703;
1991 for (int32_t i = 0; i < faceCount; i++, seed = 1664525 * seed + 1013904223) {
1992 btSwap(faces[i], faces[seed % faceCount]);
1993 }
1994
1995 for (int32_t i = 0; i < faceCount; i++) {
1996 if (!shiftFace(faces[i], amount, stack)) {
1997 return -amount;
1998 }
1999 }
2000
2001 return amount;
2002 }
2003
2004 bool btConvexHullInternal::shiftFace(Face* face, btScalar amount, btAlignedObjectArray<Vertex*> stack)
2005 {
2006 btVector3 origShift = getBtNormal(face) * -amount;
2007 if (scaling[0] != 0) {
2008 origShift[0] /= scaling[0];
2009 }
2010 if (scaling[1] != 0) {
2011 origShift[1] /= scaling[1];
2012 }
2013 if (scaling[2] != 0) {
2014 origShift[2] /= scaling[2];
2015 }
2016 Point32 shift((int32_t)origShift[medAxis], (int32_t)origShift[maxAxis], (int32_t)origShift[minAxis]);
2017 if (shift.isZero()) {
2018 return true;
2019 }
2020 Point64 normal = face->getNormal();
2021 #ifdef DEBUG_CONVEX_HULL
2022 printf("\nShrinking face (%d %d %d) (%d %d %d) (%d %d %d) by (%d %d %d)\n",
2023 face->origin.x, face->origin.y, face->origin.z, face->dir0.x, face->dir0.y, face->dir0.z, face->dir1.x, face->dir1.y, face->dir1.z, shift.x, shift.y, shift.z);
2024 #endif
2025 int64_t origDot = face->origin.dot(normal);
2026 Point32 shiftedOrigin = face->origin + shift;
2027 int64_t shiftedDot = shiftedOrigin.dot(normal);
2028 btAssert(shiftedDot <= origDot);
2029 if (shiftedDot >= origDot) {
2030 return false;
2031 }
2032
2033 Edge* intersection = NULL;
2034
2035 Edge* startEdge = face->nearbyVertex->edges;
2036 #ifdef DEBUG_CONVEX_HULL
2037 printf("Start edge is ");
2038 startEdge->print();
2039 printf(", normal is (%lld %lld %lld), shifted dot is %lld\n", normal.x, normal.y, normal.z, shiftedDot);
2040 #endif
2041 Rational128 optDot = face->nearbyVertex->dot(normal);
2042 int32_t cmp = optDot.compare(shiftedDot);
2043 #ifdef SHOW_ITERATIONS
2044 int32_t n = 0;
2045 #endif
2046 if (cmp >= 0) {
2047 Edge* e = startEdge;
2048 do {
2049 #ifdef SHOW_ITERATIONS
2050 n++;
2051 #endif
2052 Rational128 dot = e->target->dot(normal);
2053 btAssert(dot.compare(origDot) <= 0);
2054 #ifdef DEBUG_CONVEX_HULL
2055 printf("Moving downwards, edge is ");
2056 e->print();
2057 printf(", dot is %f (%f %lld)\n", (float)dot.toScalar(), (float)optDot.toScalar(), shiftedDot);
2058 #endif
2059 if (dot.compare(optDot) < 0) {
2060 int32_t c = dot.compare(shiftedDot);
2061 optDot = dot;
2062 e = e->reverse;
2063 startEdge = e;
2064 if (c < 0) {
2065 intersection = e;
2066 break;
2067 }
2068 cmp = c;
2069 }
2070 e = e->prev;
2071 } while (e != startEdge);
2072
2073 if (!intersection) {
2074 return false;
2075 }
2076 }
2077 else {
2078 Edge* e = startEdge;
2079 do {
2080 #ifdef SHOW_ITERATIONS
2081 n++;
2082 #endif
2083 Rational128 dot = e->target->dot(normal);
2084 btAssert(dot.compare(origDot) <= 0);
2085 #ifdef DEBUG_CONVEX_HULL
2086 printf("Moving upwards, edge is ");
2087 e->print();
2088 printf(", dot is %f (%f %lld)\n", (float)dot.toScalar(), (float)optDot.toScalar(), shiftedDot);
2089 #endif
2090 if (dot.compare(optDot) > 0) {
2091 cmp = dot.compare(shiftedDot);
2092 if (cmp >= 0) {
2093 intersection = e;
2094 break;
2095 }
2096 optDot = dot;
2097 e = e->reverse;
2098 startEdge = e;
2099 }
2100 e = e->prev;
2101 } while (e != startEdge);
2102
2103 if (!intersection) {
2104 return true;
2105 }
2106 }
2107
2108 #ifdef SHOW_ITERATIONS
2109 printf("Needed %d iterations to find initial intersection\n", n);
2110 #endif
2111
2112 if (cmp == 0) {
2113 Edge* e = intersection->reverse->next;
2114 #ifdef SHOW_ITERATIONS
2115 n = 0;
2116 #endif
2117 while (e->target->dot(normal).compare(shiftedDot) <= 0) {
2118 #ifdef SHOW_ITERATIONS
2119 n++;
2120 #endif
2121 e = e->next;
2122 if (e == intersection->reverse) {
2123 return true;
2124 }
2125 #ifdef DEBUG_CONVEX_HULL
2126 printf("Checking for outwards edge, current edge is ");
2127 e->print();
2128 printf("\n");
2129 #endif
2130 }
2131 #ifdef SHOW_ITERATIONS
2132 printf("Needed %d iterations to check for complete containment\n", n);
2133 #endif
2134 }
2135
2136 Edge* firstIntersection = NULL;
2137 Edge* faceEdge = NULL;
2138 Edge* firstFaceEdge = NULL;
2139
2140 #ifdef SHOW_ITERATIONS
2141 int32_t m = 0;
2142 #endif
2143 while (true) {
2144 #ifdef SHOW_ITERATIONS
2145 m++;
2146 #endif
2147 #ifdef DEBUG_CONVEX_HULL
2148 printf("Intersecting edge is ");
2149 intersection->print();
2150 printf("\n");
2151 #endif
2152 if (cmp == 0) {
2153 Edge* e = intersection->reverse->next;
2154 startEdge = e;
2155 #ifdef SHOW_ITERATIONS
2156 n = 0;
2157 #endif
2158 while (true) {
2159 #ifdef SHOW_ITERATIONS
2160 n++;
2161 #endif
2162 if (e->target->dot(normal).compare(shiftedDot) >= 0) {
2163 break;
2164 }
2165 intersection = e->reverse;
2166 e = e->next;
2167 if (e == startEdge) {
2168 return true;
2169 }
2170 }
2171 #ifdef SHOW_ITERATIONS
2172 printf("Needed %d iterations to advance intersection\n", n);
2173 #endif
2174 }
2175
2176 #ifdef DEBUG_CONVEX_HULL
2177 printf("Advanced intersecting edge to ");
2178 intersection->print();
2179 printf(", cmp = %d\n", cmp);
2180 #endif
2181
2182 if (!firstIntersection) {
2183 firstIntersection = intersection;
2184 }
2185 else if (intersection == firstIntersection) {
2186 break;
2187 }
2188
2189 int32_t prevCmp = cmp;
2190 Edge* prevIntersection = intersection;
2191 Edge* prevFaceEdge = faceEdge;
2192
2193 Edge* e = intersection->reverse;
2194 #ifdef SHOW_ITERATIONS
2195 n = 0;
2196 #endif
2197 while (true) {
2198 #ifdef SHOW_ITERATIONS
2199 n++;
2200 #endif
2201 e = e->reverse->prev;
2202 btAssert(e != intersection->reverse);
2203 cmp = e->target->dot(normal).compare(shiftedDot);
2204 #ifdef DEBUG_CONVEX_HULL
2205 printf("Testing edge ");
2206 e->print();
2207 printf(" -> cmp = %d\n", cmp);
2208 #endif
2209 if (cmp >= 0) {
2210 intersection = e;
2211 break;
2212 }
2213 }
2214 #ifdef SHOW_ITERATIONS
2215 printf("Needed %d iterations to find other intersection of face\n", n);
2216 #endif
2217
2218 if (cmp > 0) {
2219 Vertex* removed = intersection->target;
2220 e = intersection->reverse;
2221 if (e->prev == e) {
2222 removed->edges = NULL;
2223 }
2224 else {
2225 removed->edges = e->prev;
2226 e->prev->link(e->next);
2227 e->link(e);
2228 }
2229 #ifdef DEBUG_CONVEX_HULL
2230 printf("1: Removed part contains (%d %d %d)\n", removed->point.x, removed->point.y, removed->point.z);
2231 #endif
2232
2233 Point64 n0 = intersection->face->getNormal();
2234 Point64 n1 = intersection->reverse->face->getNormal();
2235 int64_t m00 = face->dir0.dot(n0);
2236 int64_t m01 = face->dir1.dot(n0);
2237 int64_t m10 = face->dir0.dot(n1);
2238 int64_t m11 = face->dir1.dot(n1);
2239 int64_t r0 = (intersection->face->origin - shiftedOrigin).dot(n0);
2240 int64_t r1 = (intersection->reverse->face->origin - shiftedOrigin).dot(n1);
2241 Int128 det = Int128::mul(m00, m11) - Int128::mul(m01, m10);
2242 btAssert(det.getSign() != 0);
2243 Vertex* v = vertexPool.newObject();
2244 v->point.index = -1;
2245 v->copy = -1;
2246 v->point128 = PointR128(Int128::mul(face->dir0.x * r0, m11) - Int128::mul(face->dir0.x * r1, m01)
2247 + Int128::mul(face->dir1.x * r1, m00) - Int128::mul(face->dir1.x * r0, m10) + det * shiftedOrigin.x,
2248 Int128::mul(face->dir0.y * r0, m11) - Int128::mul(face->dir0.y * r1, m01)
2249 + Int128::mul(face->dir1.y * r1, m00) - Int128::mul(face->dir1.y * r0, m10) + det * shiftedOrigin.y,
2250 Int128::mul(face->dir0.z * r0, m11) - Int128::mul(face->dir0.z * r1, m01)
2251 + Int128::mul(face->dir1.z * r1, m00) - Int128::mul(face->dir1.z * r0, m10) + det * shiftedOrigin.z,
2252 det);
2253 v->point.x = (int32_t)v->point128.xvalue();
2254 v->point.y = (int32_t)v->point128.yvalue();
2255 v->point.z = (int32_t)v->point128.zvalue();
2256 intersection->target = v;
2257 v->edges = e;
2258
2259 stack.push_back(v);
2260 stack.push_back(removed);
2261 stack.push_back(NULL);
2262 }
2263
2264 if (cmp || prevCmp || (prevIntersection->reverse->next->target != intersection->target)) {
2265 faceEdge = newEdgePair(prevIntersection->target, intersection->target);
2266 if (prevCmp == 0) {
2267 faceEdge->link(prevIntersection->reverse->next);
2268 }
2269 if ((prevCmp == 0) || prevFaceEdge) {
2270 prevIntersection->reverse->link(faceEdge);
2271 }
2272 if (cmp == 0) {
2273 intersection->reverse->prev->link(faceEdge->reverse);
2274 }
2275 faceEdge->reverse->link(intersection->reverse);
2276 }
2277 else {
2278 faceEdge = prevIntersection->reverse->next;
2279 }
2280
2281 if (prevFaceEdge) {
2282 if (prevCmp > 0) {
2283 faceEdge->link(prevFaceEdge->reverse);
2284 }
2285 else if (faceEdge != prevFaceEdge->reverse) {
2286 stack.push_back(prevFaceEdge->target);
2287 while (faceEdge->next != prevFaceEdge->reverse) {
2288 Vertex* removed = faceEdge->next->target;
2289 removeEdgePair(faceEdge->next);
2290 stack.push_back(removed);
2291 #ifdef DEBUG_CONVEX_HULL
2292 printf("2: Removed part contains (%d %d %d)\n", removed->point.x, removed->point.y, removed->point.z);
2293 #endif
2294 }
2295 stack.push_back(NULL);
2296 }
2297 }
2298 faceEdge->face = face;
2299 faceEdge->reverse->face = intersection->face;
2300
2301 if (!firstFaceEdge) {
2302 firstFaceEdge = faceEdge;
2303 }
2304 }
2305 #ifdef SHOW_ITERATIONS
2306 printf("Needed %d iterations to process all intersections\n", m);
2307 #endif
2308
2309 if (cmp > 0) {
2310 firstFaceEdge->reverse->target = faceEdge->target;
2311 firstIntersection->reverse->link(firstFaceEdge);
2312 firstFaceEdge->link(faceEdge->reverse);
2313 }
2314 else if (firstFaceEdge != faceEdge->reverse) {
2315 stack.push_back(faceEdge->target);
2316 while (firstFaceEdge->next != faceEdge->reverse) {
2317 Vertex* removed = firstFaceEdge->next->target;
2318 removeEdgePair(firstFaceEdge->next);
2319 stack.push_back(removed);
2320 #ifdef DEBUG_CONVEX_HULL
2321 printf("3: Removed part contains (%d %d %d)\n", removed->point.x, removed->point.y, removed->point.z);
2322 #endif
2323 }
2324 stack.push_back(NULL);
2325 }
2326
2327 btAssert(stack.size() > 0);
2328 vertexList = stack[0];
2329
2330 #ifdef DEBUG_CONVEX_HULL
2331 printf("Removing part\n");
2332 #endif
2333 #ifdef SHOW_ITERATIONS
2334 n = 0;
2335 #endif
2336 int32_t pos = 0;
2337 while (pos < stack.size()) {
2338 int32_t end = stack.size();
2339 while (pos < end) {
2340 Vertex* kept = stack[pos++];
2341 #ifdef DEBUG_CONVEX_HULL
2342 kept->print();
2343 #endif
2344 bool deeper = false;
2345 Vertex* removed;
2346 while ((removed = stack[pos++]) != NULL) {
2347 #ifdef SHOW_ITERATIONS
2348 n++;
2349 #endif
2350 kept->receiveNearbyFaces(removed);
2351 while (removed->edges) {
2352 if (!deeper) {
2353 deeper = true;
2354 stack.push_back(kept);
2355 }
2356 stack.push_back(removed->edges->target);
2357 removeEdgePair(removed->edges);
2358 }
2359 }
2360 if (deeper) {
2361 stack.push_back(NULL);
2362 }
2363 }
2364 }
2365 #ifdef SHOW_ITERATIONS
2366 printf("Needed %d iterations to remove part\n", n);
2367 #endif
2368
2369 stack.resize(0);
2370 face->origin = shiftedOrigin;
2371
2372 return true;
2373 }
2374
2375 static int32_t getVertexCopy(btConvexHullInternal::Vertex* vertex, btAlignedObjectArray<btConvexHullInternal::Vertex*>& vertices)
2376 {
2377 int32_t index = vertex->copy;
2378 if (index < 0) {
2379 index = vertices.size();
2380 vertex->copy = index;
2381 vertices.push_back(vertex);
2382 #ifdef DEBUG_CONVEX_HULL
2383 printf("Vertex %d gets index *%d\n", vertex->point.index, index);
2384 #endif
2385 }
2386 return index;
2387 }
2388
2389 btScalar btConvexHullComputer::compute(const void* coords, bool doubleCoords, int32_t stride, int32_t count, btScalar shrink, btScalar shrinkClamp)
2390 {
2391 if (count <= 0) {
2392 vertices.clear();
2393 edges.clear();
2394 faces.clear();
2395 return 0;
2396 }
2397
2398 btConvexHullInternal hull;
2399 hull.compute(coords, doubleCoords, stride, count);
2400
2401 btScalar shift = 0;
2402 if ((shrink > 0) && ((shift = hull.shrink(shrink, shrinkClamp)) < 0)) {
2403 vertices.clear();
2404 edges.clear();
2405 faces.clear();
2406 return shift;
2407 }
2408
2409 vertices.resize(0);
2410 edges.resize(0);
2411 faces.resize(0);
2412
2413 btAlignedObjectArray<btConvexHullInternal::Vertex*> oldVertices;
2414 getVertexCopy(hull.vertexList, oldVertices);
2415 int32_t copied = 0;
2416 while (copied < oldVertices.size()) {
2417 btConvexHullInternal::Vertex* v = oldVertices[copied];
2418 vertices.push_back(hull.getCoordinates(v));
2419 btConvexHullInternal::Edge* firstEdge = v->edges;
2420 if (firstEdge) {
2421 int32_t firstCopy = -1;
2422 int32_t prevCopy = -1;
2423 btConvexHullInternal::Edge* e = firstEdge;
2424 do {
2425 if (e->copy < 0) {
2426 int32_t s = edges.size();
2427 edges.push_back(Edge());
2428 edges.push_back(Edge());
2429 Edge* c = &edges[s];
2430 Edge* r = &edges[s + 1];
2431 e->copy = s;
2432 e->reverse->copy = s + 1;
2433 c->reverse = 1;
2434 r->reverse = -1;
2435 c->targetVertex = getVertexCopy(e->target, oldVertices);
2436 r->targetVertex = copied;
2437 #ifdef DEBUG_CONVEX_HULL
2438 printf(" CREATE: Vertex *%d has edge to *%d\n", copied, c->getTargetVertex());
2439 #endif
2440 }
2441 if (prevCopy >= 0) {
2442 edges[e->copy].next = prevCopy - e->copy;
2443 }
2444 else {
2445 firstCopy = e->copy;
2446 }
2447 prevCopy = e->copy;
2448 e = e->next;
2449 } while (e != firstEdge);
2450 edges[firstCopy].next = prevCopy - firstCopy;
2451 }
2452 copied++;
2453 }
2454
2455 for (int32_t i = 0; i < copied; i++) {
2456 btConvexHullInternal::Vertex* v = oldVertices[i];
2457 btConvexHullInternal::Edge* firstEdge = v->edges;
2458 if (firstEdge) {
2459 btConvexHullInternal::Edge* e = firstEdge;
2460 do {
2461 if (e->copy >= 0) {
2462 #ifdef DEBUG_CONVEX_HULL
2463 printf("Vertex *%d has edge to *%d\n", i, edges[e->copy].getTargetVertex());
2464 #endif
2465 faces.push_back(e->copy);
2466 btConvexHullInternal::Edge* f = e;
2467 do {
2468 #ifdef DEBUG_CONVEX_HULL
2469 printf(" Face *%d\n", edges[f->copy].getTargetVertex());
2470 #endif
2471 f->copy = -1;
2472 f = f->reverse->prev;
2473 } while (f != e);
2474 }
2475 e = e->next;
2476 } while (e != firstEdge);
2477 }
2478 }
2479
2480 return shift;
2481 }
2482
2483 }; // namespace VHACD