b3ConvexHullComputer.cpp 57 KB

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