btConvexHullComputer.cpp 55 KB

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