-
-
Notifications
You must be signed in to change notification settings - Fork 21.4k
/
btConvexHullComputer.cpp
2487 lines (2206 loc) · 69.6 KB
/
btConvexHullComputer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (c) 2011 Ole Kniemeyer, MAXON, www.maxon.net
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it freely,
subject to the following restrictions:
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.
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include <string.h>
#include "btAlignedObjectArray.h"
#include "btConvexHullComputer.h"
#include "btMinMax.h"
#include "btVector3.h"
#ifdef __GNUC__
#include <stdint.h>
#elif defined(_MSC_VER)
typedef __int32 int32_t;
typedef __int64 int64_t;
typedef unsigned __int32 uint32_t;
typedef unsigned __int64 uint64_t;
#else
typedef int32_t int32_t;
typedef long long int32_t int64_t;
typedef uint32_t uint32_t;
typedef unsigned long long int32_t uint64_t;
#endif
#ifdef _MSC_VER
#pragma warning(disable:4458)
#endif
//The definition of USE_X86_64_ASM is moved into the build system. You can enable it manually by commenting out the following lines
//#if (defined(__GNUC__) && defined(__x86_64__) && !defined(__ICL)) // || (defined(__ICL) && defined(_M_X64)) bug in Intel compiler, disable inline assembly
// #define USE_X86_64_ASM
//#endif
//#define DEBUG_CONVEX_HULL
//#define SHOW_ITERATIONS
#if defined(DEBUG_CONVEX_HULL) || defined(SHOW_ITERATIONS)
#include <stdio.h>
#endif
// -- GODOT start --
namespace VHACD {
// -- GODOT end --
// Convex hull implementation based on Preparata and Hong
// Ole Kniemeyer, MAXON Computer GmbH
class btConvexHullInternal {
public:
class Point64 {
public:
int64_t x;
int64_t y;
int64_t z;
Point64(int64_t x, int64_t y, int64_t z)
: x(x)
, y(y)
, z(z)
{
}
bool isZero()
{
return (x == 0) && (y == 0) && (z == 0);
}
int64_t dot(const Point64& b) const
{
return x * b.x + y * b.y + z * b.z;
}
};
class Point32 {
public:
int32_t x;
int32_t y;
int32_t z;
int32_t index;
Point32()
{
}
Point32(int32_t x, int32_t y, int32_t z)
: x(x)
, y(y)
, z(z)
, index(-1)
{
}
bool operator==(const Point32& b) const
{
return (x == b.x) && (y == b.y) && (z == b.z);
}
bool operator!=(const Point32& b) const
{
return (x != b.x) || (y != b.y) || (z != b.z);
}
bool isZero()
{
return (x == 0) && (y == 0) && (z == 0);
}
Point64 cross(const Point32& b) const
{
return Point64(y * b.z - z * b.y, z * b.x - x * b.z, x * b.y - y * b.x);
}
Point64 cross(const Point64& b) const
{
return Point64(y * b.z - z * b.y, z * b.x - x * b.z, x * b.y - y * b.x);
}
int64_t dot(const Point32& b) const
{
return x * b.x + y * b.y + z * b.z;
}
int64_t dot(const Point64& b) const
{
return x * b.x + y * b.y + z * b.z;
}
Point32 operator+(const Point32& b) const
{
return Point32(x + b.x, y + b.y, z + b.z);
}
Point32 operator-(const Point32& b) const
{
return Point32(x - b.x, y - b.y, z - b.z);
}
};
class Int128 {
public:
uint64_t low;
uint64_t high;
Int128()
{
}
Int128(uint64_t low, uint64_t high)
: low(low)
, high(high)
{
}
Int128(uint64_t low)
: low(low)
, high(0)
{
}
Int128(int64_t value)
: low(value)
, high((value >= 0) ? 0 : (uint64_t)-1LL)
{
}
static Int128 mul(int64_t a, int64_t b);
static Int128 mul(uint64_t a, uint64_t b);
Int128 operator-() const
{
return Int128((uint64_t) - (int64_t)low, ~high + (low == 0));
}
Int128 operator+(const Int128& b) const
{
#ifdef USE_X86_64_ASM
Int128 result;
__asm__("addq %[bl], %[rl]\n\t"
"adcq %[bh], %[rh]\n\t"
: [rl] "=r"(result.low), [rh] "=r"(result.high)
: "0"(low), "1"(high), [bl] "g"(b.low), [bh] "g"(b.high)
: "cc");
return result;
#else
uint64_t lo = low + b.low;
return Int128(lo, high + b.high + (lo < low));
#endif
}
Int128 operator-(const Int128& b) const
{
#ifdef USE_X86_64_ASM
Int128 result;
__asm__("subq %[bl], %[rl]\n\t"
"sbbq %[bh], %[rh]\n\t"
: [rl] "=r"(result.low), [rh] "=r"(result.high)
: "0"(low), "1"(high), [bl] "g"(b.low), [bh] "g"(b.high)
: "cc");
return result;
#else
return *this + -b;
#endif
}
Int128& operator+=(const Int128& b)
{
#ifdef USE_X86_64_ASM
__asm__("addq %[bl], %[rl]\n\t"
"adcq %[bh], %[rh]\n\t"
: [rl] "=r"(low), [rh] "=r"(high)
: "0"(low), "1"(high), [bl] "g"(b.low), [bh] "g"(b.high)
: "cc");
#else
uint64_t lo = low + b.low;
if (lo < low) {
++high;
}
low = lo;
high += b.high;
#endif
return *this;
}
Int128& operator++()
{
if (++low == 0) {
++high;
}
return *this;
}
Int128 operator*(int64_t b) const;
btScalar toScalar() const
{
return ((int64_t)high >= 0) ? btScalar(high) * (btScalar(0x100000000LL) * btScalar(0x100000000LL)) + btScalar(low)
: -(-*this).toScalar();
}
int32_t getSign() const
{
return ((int64_t)high < 0) ? -1 : (high || low) ? 1 : 0;
}
bool operator<(const Int128& b) const
{
return (high < b.high) || ((high == b.high) && (low < b.low));
}
int32_t ucmp(const Int128& b) const
{
if (high < b.high) {
return -1;
}
if (high > b.high) {
return 1;
}
if (low < b.low) {
return -1;
}
if (low > b.low) {
return 1;
}
return 0;
}
};
class Rational64 {
private:
uint64_t m_numerator;
uint64_t m_denominator;
int32_t sign;
public:
Rational64(int64_t numerator, int64_t denominator)
{
if (numerator > 0) {
sign = 1;
m_numerator = (uint64_t)numerator;
}
else if (numerator < 0) {
sign = -1;
m_numerator = (uint64_t)-numerator;
}
else {
sign = 0;
m_numerator = 0;
}
if (denominator > 0) {
m_denominator = (uint64_t)denominator;
}
else if (denominator < 0) {
sign = -sign;
m_denominator = (uint64_t)-denominator;
}
else {
m_denominator = 0;
}
}
bool isNegativeInfinity() const
{
return (sign < 0) && (m_denominator == 0);
}
bool isNaN() const
{
return (sign == 0) && (m_denominator == 0);
}
int32_t compare(const Rational64& b) const;
btScalar toScalar() const
{
return sign * ((m_denominator == 0) ? SIMD_INFINITY : (btScalar)m_numerator / m_denominator);
}
};
class Rational128 {
private:
Int128 numerator;
Int128 denominator;
int32_t sign;
bool isInt64;
public:
Rational128(int64_t value)
{
if (value > 0) {
sign = 1;
this->numerator = value;
}
else if (value < 0) {
sign = -1;
this->numerator = -value;
}
else {
sign = 0;
this->numerator = (uint64_t)0;
}
this->denominator = (uint64_t)1;
isInt64 = true;
}
Rational128(const Int128& numerator, const Int128& denominator)
{
sign = numerator.getSign();
if (sign >= 0) {
this->numerator = numerator;
}
else {
this->numerator = -numerator;
}
int32_t dsign = denominator.getSign();
if (dsign >= 0) {
this->denominator = denominator;
}
else {
sign = -sign;
this->denominator = -denominator;
}
isInt64 = false;
}
int32_t compare(const Rational128& b) const;
int32_t compare(int64_t b) const;
btScalar toScalar() const
{
return sign * ((denominator.getSign() == 0) ? SIMD_INFINITY : numerator.toScalar() / denominator.toScalar());
}
};
class PointR128 {
public:
Int128 x;
Int128 y;
Int128 z;
Int128 denominator;
PointR128()
{
}
PointR128(Int128 x, Int128 y, Int128 z, Int128 denominator)
: x(x)
, y(y)
, z(z)
, denominator(denominator)
{
}
btScalar xvalue() const
{
return x.toScalar() / denominator.toScalar();
}
btScalar yvalue() const
{
return y.toScalar() / denominator.toScalar();
}
btScalar zvalue() const
{
return z.toScalar() / denominator.toScalar();
}
};
class Edge;
class Face;
class Vertex {
public:
Vertex* next;
Vertex* prev;
Edge* edges;
Face* firstNearbyFace;
Face* lastNearbyFace;
PointR128 point128;
Point32 point;
int32_t copy;
Vertex()
: next(NULL)
, prev(NULL)
, edges(NULL)
, firstNearbyFace(NULL)
, lastNearbyFace(NULL)
, copy(-1)
{
}
#ifdef DEBUG_CONVEX_HULL
void print()
{
printf("V%d (%d, %d, %d)", point.index, point.x, point.y, point.z);
}
void printGraph();
#endif
Point32 operator-(const Vertex& b) const
{
return point - b.point;
}
Rational128 dot(const Point64& b) const
{
return (point.index >= 0) ? Rational128(point.dot(b))
: Rational128(point128.x * b.x + point128.y * b.y + point128.z * b.z, point128.denominator);
}
btScalar xvalue() const
{
return (point.index >= 0) ? btScalar(point.x) : point128.xvalue();
}
btScalar yvalue() const
{
return (point.index >= 0) ? btScalar(point.y) : point128.yvalue();
}
btScalar zvalue() const
{
return (point.index >= 0) ? btScalar(point.z) : point128.zvalue();
}
void receiveNearbyFaces(Vertex* src)
{
if (lastNearbyFace) {
lastNearbyFace->nextWithSameNearbyVertex = src->firstNearbyFace;
}
else {
firstNearbyFace = src->firstNearbyFace;
}
if (src->lastNearbyFace) {
lastNearbyFace = src->lastNearbyFace;
}
for (Face* f = src->firstNearbyFace; f; f = f->nextWithSameNearbyVertex) {
btAssert(f->nearbyVertex == src);
f->nearbyVertex = this;
}
src->firstNearbyFace = NULL;
src->lastNearbyFace = NULL;
}
};
class Edge {
public:
Edge* next;
Edge* prev;
Edge* reverse;
Vertex* target;
Face* face;
int32_t copy;
~Edge()
{
next = NULL;
prev = NULL;
reverse = NULL;
target = NULL;
face = NULL;
}
void link(Edge* n)
{
btAssert(reverse->target == n->reverse->target);
next = n;
n->prev = this;
}
#ifdef DEBUG_CONVEX_HULL
void print()
{
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,
reverse->target->point.x, reverse->target->point.y, reverse->target->point.z, target->point.x, target->point.y, target->point.z);
}
#endif
};
class Face {
public:
Face* next;
Vertex* nearbyVertex;
Face* nextWithSameNearbyVertex;
Point32 origin;
Point32 dir0;
Point32 dir1;
Face()
: next(NULL)
, nearbyVertex(NULL)
, nextWithSameNearbyVertex(NULL)
{
}
void init(Vertex* a, Vertex* b, Vertex* c)
{
nearbyVertex = a;
origin = a->point;
dir0 = *b - *a;
dir1 = *c - *a;
if (a->lastNearbyFace) {
a->lastNearbyFace->nextWithSameNearbyVertex = this;
}
else {
a->firstNearbyFace = this;
}
a->lastNearbyFace = this;
}
Point64 getNormal()
{
return dir0.cross(dir1);
}
};
template <typename UWord, typename UHWord>
class DMul {
private:
static uint32_t high(uint64_t value)
{
return (uint32_t)(value >> 32);
}
static uint32_t low(uint64_t value)
{
return (uint32_t)value;
}
static uint64_t mul(uint32_t a, uint32_t b)
{
return (uint64_t)a * (uint64_t)b;
}
static void shlHalf(uint64_t& value)
{
value <<= 32;
}
static uint64_t high(Int128 value)
{
return value.high;
}
static uint64_t low(Int128 value)
{
return value.low;
}
static Int128 mul(uint64_t a, uint64_t b)
{
return Int128::mul(a, b);
}
static void shlHalf(Int128& value)
{
value.high = value.low;
value.low = 0;
}
public:
static void mul(UWord a, UWord b, UWord& resLow, UWord& resHigh)
{
UWord p00 = mul(low(a), low(b));
UWord p01 = mul(low(a), high(b));
UWord p10 = mul(high(a), low(b));
UWord p11 = mul(high(a), high(b));
UWord p0110 = UWord(low(p01)) + UWord(low(p10));
p11 += high(p01);
p11 += high(p10);
p11 += high(p0110);
shlHalf(p0110);
p00 += p0110;
if (p00 < p0110) {
++p11;
}
resLow = p00;
resHigh = p11;
}
};
private:
class IntermediateHull {
public:
Vertex* minXy;
Vertex* maxXy;
Vertex* minYx;
Vertex* maxYx;
IntermediateHull()
: minXy(NULL)
, maxXy(NULL)
, minYx(NULL)
, maxYx(NULL)
{
}
void print();
};
enum Orientation { NONE,
CLOCKWISE,
COUNTER_CLOCKWISE };
template <typename T>
class PoolArray {
private:
T* array;
int32_t size;
public:
PoolArray<T>* next;
PoolArray(int32_t size)
: size(size)
, next(NULL)
{
array = (T*)btAlignedAlloc(sizeof(T) * size, 16);
}
~PoolArray()
{
btAlignedFree(array);
}
T* init()
{
T* o = array;
for (int32_t i = 0; i < size; i++, o++) {
o->next = (i + 1 < size) ? o + 1 : NULL;
}
return array;
}
};
template <typename T>
class Pool {
private:
PoolArray<T>* arrays;
PoolArray<T>* nextArray;
T* freeObjects;
int32_t arraySize;
public:
Pool()
: arrays(NULL)
, nextArray(NULL)
, freeObjects(NULL)
, arraySize(256)
{
}
~Pool()
{
while (arrays) {
PoolArray<T>* p = arrays;
arrays = p->next;
p->~PoolArray<T>();
btAlignedFree(p);
}
}
void reset()
{
nextArray = arrays;
freeObjects = NULL;
}
void setArraySize(int32_t arraySize)
{
this->arraySize = arraySize;
}
T* newObject()
{
T* o = freeObjects;
if (!o) {
PoolArray<T>* p = nextArray;
if (p) {
nextArray = p->next;
}
else {
p = new (btAlignedAlloc(sizeof(PoolArray<T>), 16)) PoolArray<T>(arraySize);
p->next = arrays;
arrays = p;
}
o = p->init();
}
freeObjects = o->next;
return new (o) T();
};
void freeObject(T* object)
{
object->~T();
object->next = freeObjects;
freeObjects = object;
}
};
btVector3 scaling;
btVector3 center;
Pool<Vertex> vertexPool;
Pool<Edge> edgePool;
Pool<Face> facePool;
btAlignedObjectArray<Vertex*> originalVertices;
int32_t mergeStamp;
int32_t minAxis;
int32_t medAxis;
int32_t maxAxis;
int32_t usedEdgePairs;
int32_t maxUsedEdgePairs;
static Orientation getOrientation(const Edge* prev, const Edge* next, const Point32& s, const Point32& t);
Edge* findMaxAngle(bool ccw, const Vertex* start, const Point32& s, const Point64& rxs, const Point64& sxrxs, Rational64& minCot);
void findEdgeForCoplanarFaces(Vertex* c0, Vertex* c1, Edge*& e0, Edge*& e1, Vertex* stop0, Vertex* stop1);
Edge* newEdgePair(Vertex* from, Vertex* to);
void removeEdgePair(Edge* edge)
{
Edge* n = edge->next;
Edge* r = edge->reverse;
btAssert(edge->target && r->target);
if (n != edge) {
n->prev = edge->prev;
edge->prev->next = n;
r->target->edges = n;
}
else {
r->target->edges = NULL;
}
n = r->next;
if (n != r) {
n->prev = r->prev;
r->prev->next = n;
edge->target->edges = n;
}
else {
edge->target->edges = NULL;
}
edgePool.freeObject(edge);
edgePool.freeObject(r);
usedEdgePairs--;
}
void computeInternal(int32_t start, int32_t end, IntermediateHull& result);
bool mergeProjection(IntermediateHull& h0, IntermediateHull& h1, Vertex*& c0, Vertex*& c1);
void merge(IntermediateHull& h0, IntermediateHull& h1);
btVector3 toBtVector(const Point32& v);
btVector3 getBtNormal(Face* face);
bool shiftFace(Face* face, btScalar amount, btAlignedObjectArray<Vertex*> stack);
public:
Vertex* vertexList;
void compute(const void* coords, bool doubleCoords, int32_t stride, int32_t count);
btVector3 getCoordinates(const Vertex* v);
btScalar shrink(btScalar amount, btScalar clampAmount);
};
btConvexHullInternal::Int128 btConvexHullInternal::Int128::operator*(int64_t b) const
{
bool negative = (int64_t)high < 0;
Int128 a = negative ? -*this : *this;
if (b < 0) {
negative = !negative;
b = -b;
}
Int128 result = mul(a.low, (uint64_t)b);
result.high += a.high * (uint64_t)b;
return negative ? -result : result;
}
btConvexHullInternal::Int128 btConvexHullInternal::Int128::mul(int64_t a, int64_t b)
{
Int128 result;
#ifdef USE_X86_64_ASM
__asm__("imulq %[b]"
: "=a"(result.low), "=d"(result.high)
: "0"(a), [b] "r"(b)
: "cc");
return result;
#else
bool negative = a < 0;
if (negative) {
a = -a;
}
if (b < 0) {
negative = !negative;
b = -b;
}
DMul<uint64_t, uint32_t>::mul((uint64_t)a, (uint64_t)b, result.low, result.high);
return negative ? -result : result;
#endif
}
btConvexHullInternal::Int128 btConvexHullInternal::Int128::mul(uint64_t a, uint64_t b)
{
Int128 result;
#ifdef USE_X86_64_ASM
__asm__("mulq %[b]"
: "=a"(result.low), "=d"(result.high)
: "0"(a), [b] "r"(b)
: "cc");
#else
DMul<uint64_t, uint32_t>::mul(a, b, result.low, result.high);
#endif
return result;
}
int32_t btConvexHullInternal::Rational64::compare(const Rational64& b) const
{
if (sign != b.sign) {
return sign - b.sign;
}
else if (sign == 0) {
return 0;
}
// return (numerator * b.denominator > b.numerator * denominator) ? sign : (numerator * b.denominator < b.numerator * denominator) ? -sign : 0;
#ifdef USE_X86_64_ASM
int32_t result;
int64_t tmp;
int64_t dummy;
__asm__("mulq %[bn]\n\t"
"movq %%rax, %[tmp]\n\t"
"movq %%rdx, %%rbx\n\t"
"movq %[tn], %%rax\n\t"
"mulq %[bd]\n\t"
"subq %[tmp], %%rax\n\t"
"sbbq %%rbx, %%rdx\n\t" // rdx:rax contains 128-bit-difference "numerator*b.denominator - b.numerator*denominator"
"setnsb %%bh\n\t" // bh=1 if difference is non-negative, bh=0 otherwise
"orq %%rdx, %%rax\n\t"
"setnzb %%bl\n\t" // bl=1 if difference if non-zero, bl=0 if it is zero
"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)
"shll $16, %%ebx\n\t" // ebx has same sign as difference
: "=&b"(result), [tmp] "=&r"(tmp), "=a"(dummy)
: "a"(denominator), [bn] "g"(b.numerator), [tn] "g"(numerator), [bd] "g"(b.denominator)
: "%rdx", "cc");
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)
// if sign is -1, all bits of result are inverted, which changes the sign of result (and again cannot result in zero)
: 0;
#else
return sign * Int128::mul(m_numerator, b.m_denominator).ucmp(Int128::mul(m_denominator, b.m_numerator));
#endif
}
int32_t btConvexHullInternal::Rational128::compare(const Rational128& b) const
{
if (sign != b.sign) {
return sign - b.sign;
}
else if (sign == 0) {
return 0;
}
if (isInt64) {
return -b.compare(sign * (int64_t)numerator.low);
}
Int128 nbdLow, nbdHigh, dbnLow, dbnHigh;
DMul<Int128, uint64_t>::mul(numerator, b.denominator, nbdLow, nbdHigh);
DMul<Int128, uint64_t>::mul(denominator, b.numerator, dbnLow, dbnHigh);
int32_t cmp = nbdHigh.ucmp(dbnHigh);
if (cmp) {
return cmp * sign;
}
return nbdLow.ucmp(dbnLow) * sign;
}
int32_t btConvexHullInternal::Rational128::compare(int64_t b) const
{
if (isInt64) {
int64_t a = sign * (int64_t)numerator.low;
return (a > b) ? 1 : (a < b) ? -1 : 0;
}
if (b > 0) {
if (sign <= 0) {
return -1;
}
}
else if (b < 0) {
if (sign >= 0) {
return 1;
}
b = -b;
}
else {
return sign;
}
return numerator.ucmp(denominator * b) * sign;
}
btConvexHullInternal::Edge* btConvexHullInternal::newEdgePair(Vertex* from, Vertex* to)
{
btAssert(from && to);
Edge* e = edgePool.newObject();
Edge* r = edgePool.newObject();
e->reverse = r;
r->reverse = e;
e->copy = mergeStamp;
r->copy = mergeStamp;
e->target = to;
r->target = from;
e->face = NULL;
r->face = NULL;
usedEdgePairs++;
if (usedEdgePairs > maxUsedEdgePairs) {
maxUsedEdgePairs = usedEdgePairs;
}
return e;
}
bool btConvexHullInternal::mergeProjection(IntermediateHull& h0, IntermediateHull& h1, Vertex*& c0, Vertex*& c1)
{
Vertex* v0 = h0.maxYx;
Vertex* v1 = h1.minYx;
if ((v0->point.x == v1->point.x) && (v0->point.y == v1->point.y)) {
btAssert(v0->point.z < v1->point.z);
Vertex* v1p = v1->prev;
if (v1p == v1) {
c0 = v0;