b3ConvexHullComputer.cpp 55 KB

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