ConvexHullTest.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include <TestFramework.h>
  4. #include <Tests/ConvexCollision/ConvexHullTest.h>
  5. #include <Jolt/Geometry/ConvexHullBuilder.h>
  6. #include <Utils/Log.h>
  7. #include <Renderer/DebugRendererImp.h>
  8. JPH_SUPPRESS_WARNINGS_STD_BEGIN
  9. #include <fstream>
  10. JPH_SUPPRESS_WARNINGS_STD_END
  11. JPH_IMPLEMENT_RTTI_VIRTUAL(ConvexHullTest)
  12. {
  13. JPH_ADD_BASE_CLASS(ConvexHullTest, Test)
  14. }
  15. void ConvexHullTest::Initialize()
  16. {
  17. // First add a list of shapes that were problematic before
  18. mPoints = {
  19. {
  20. Vec3(-1, 0, -1),
  21. Vec3(1, 0, -1),
  22. Vec3(-1, 0, 1),
  23. Vec3(1, 0, 1)
  24. },
  25. {
  26. Vec3(-1, 0, -1),
  27. Vec3(1, 0, -1),
  28. Vec3(-1, 0, 1),
  29. Vec3(-0.5f, 0, -0.5f)
  30. },
  31. {
  32. Vec3(-1, 0, -1),
  33. Vec3(1, 0, -1),
  34. Vec3(-1, 0, 1),
  35. Vec3(1, 0, 1),
  36. Vec3(0, 1, 0)
  37. },
  38. {
  39. Vec3(1.25793016f, 0.157113776f, 1.22066617f),
  40. Vec3(1.92657053f, 0.157114446f, 0.240761176f),
  41. Vec3(1.40259242f, 0.157115221f, -0.834863901f),
  42. Vec3(1.94086421f, 0.157113507f, -0.790734947f),
  43. Vec3(2.20533752f, 0.157113209f, -0.281754375f),
  44. Vec3(0.0426187329f, 0.157113969f, -1.40533638f),
  45. Vec3(1.11055744f, 0.157113969f, -1.33626819f),
  46. Vec3(0.180490851f, 0.157114655f, 1.16420007f),
  47. Vec3(-1.34696794f, 0.157110974f, -0.978962243f),
  48. Vec3(-0.981223822f, 0.157110706f, -1.44589376f),
  49. Vec3(-1.8200444f, 0.157106474f, 1.05036092f),
  50. Vec3(-0.376947045f, 0.15711388f, 1.13544536f),
  51. Vec3(-1.37966835f, 0.157109678f, 1.08289516f),
  52. Vec3(-1.04599845f, 0.157108605f, 1.54891157f),
  53. Vec3(-0.597127378f, 0.157110557f, 1.57243586f),
  54. Vec3(-2.09407234f, 0.157106325f, 0.560136259f),
  55. Vec3(-1.91857386f, 0.157108605f, 0.0392456949f),
  56. Vec3(-2.08503342f, 0.157106936f, -0.506603181f),
  57. Vec3(-1.80278254f, 0.157107696f, -0.986931145f),
  58. Vec3(0.434835076f, 0.157112151f, 1.62568307f),
  59. Vec3(0.917346299f, 0.157111734f, 1.65097046f),
  60. Vec3(1.77710009f, 0.157112047f, 1.2388792f),
  61. Vec3(2.11432409f, 0.157112464f, 0.780689001f),
  62. },
  63. {
  64. Vec3(1.32055235f, -0.0982032791f, 0.020047307f),
  65. Vec3(-0.0175848603f, -0.104957283f, 0.020047307f),
  66. Vec3(-0.0175848603f, 0.098285675f, 0.020047307f),
  67. Vec3(1.32055235f, 0.098285675f, 0.020047307f),
  68. Vec3(1.00427914f, -0.0982032791f, 0.868395209f),
  69. Vec3(1.32055235f, -0.0982032791f, 2.63605499f),
  70. Vec3(1.00427914f, -0.0982032791f, 1.95698023f),
  71. Vec3(1.00427914f, -0.104957283f, 0.511006474f),
  72. Vec3(0.00150847435f, -0.104957283f, 0.511006474f),
  73. Vec3(0.271511227f, -0.179470509f, 0.868395209f),
  74. Vec3(0.00150847435f, -0.179470509f, 0.868395209f),
  75. Vec3(0.00150847435f, -0.179470509f, 0.511006474f),
  76. Vec3(0.271511227f, -0.179470509f, 0.511006474f),
  77. Vec3(1.00427914f, -0.145700991f, 1.95698023f),
  78. Vec3(1.00427914f, -0.145700991f, 2.40789247f),
  79. Vec3(0.271511227f, -0.179470509f, 2.40789247f),
  80. Vec3(0.271511227f, -0.179470509f, 1.95698023f),
  81. Vec3(0.00150847435f, -0.104957283f, 2.40789247f),
  82. Vec3(1.00427914f, -0.104957283f, 2.40789247f),
  83. Vec3(-0.0175848603f, -0.104957283f, 2.63605499f),
  84. Vec3(1.32055235f, 0.098285675f, 2.63605499f),
  85. Vec3(-0.0175848603f, 0.098285675f, 2.63605499f),
  86. Vec3(-0.0175848603f, -0.0929760709f, 1.31891572f),
  87. Vec3(-0.0175848603f, 0.0915316716f, 1.31891572f),
  88. Vec3(1.00427914f, -0.145700991f, 0.868395209f),
  89. Vec3(1.00427914f, -0.145700991f, 0.511006474f),
  90. Vec3(0.00150847435f, -0.104957283f, 0.868395209f),
  91. Vec3(0.00150847435f, -0.104957283f, 1.95698023f),
  92. Vec3(0.00150847435f, -0.179470509f, 1.95698023f),
  93. Vec3(0.00150847435f, -0.179470509f, 2.40789247f),
  94. Vec3(-0.0175848603f, -0.100129686f, 0.959797204f),
  95. Vec3(0.0878298879f, 0.139223307f, 1.04704332f),
  96. Vec3(0.122709334f, -0.147821367f, 1.15395057f),
  97. Vec3(0.122709334f, 0.139223307f, 1.15395057f),
  98. Vec3(0.19671753f, -0.118080139f, 1.15425301f),
  99. Vec3(0.0986568928f, -0.147821367f, 1.22612f),
  100. Vec3(0.175069571f, -0.118080139f, 1.2711879f),
  101. Vec3(-0.0175848603f, -0.147821367f, 0.959797204f),
  102. Vec3(0.0767889619f, -0.118080139f, 0.947003484f),
  103. Vec3(0.0878298879f, -0.147821367f, 1.04704332f),
  104. Vec3(0.18563965f, -0.118080139f, 1.03236175f),
  105. Vec3(-0.0175848603f, 0.098285675f, 0.959797204f),
  106. Vec3(0.0986568928f, 0.139223307f, 1.22612f),
  107. Vec3(0.0897113085f, -0.104957283f, 1.32667887f),
  108. Vec3(-0.0175848603f, -0.147821367f, 1.31891572f),
  109. Vec3(0.0897113085f, -0.118080139f, 1.32667887f),
  110. Vec3(0.175069571f, -0.104957283f, 1.2711879f),
  111. Vec3(0.18563965f, -0.104957283f, 1.03236175f),
  112. Vec3(0.19671753f, -0.104957283f, 1.15425301f),
  113. Vec3(0.0767889619f, -0.104957283f, 0.947003484f),
  114. Vec3(1.00427914f, 0.098285675f, 0.868395209f),
  115. Vec3(1.00427914f, 0.098285675f, 1.95698023f),
  116. Vec3(1.00427914f, 0.098285675f, 0.511006474f),
  117. Vec3(0.00150847435f, 0.098285675f, 0.511006474f),
  118. Vec3(0.00150847435f, 0.17087248f, 0.511006474f),
  119. Vec3(0.00150847435f, 0.17087248f, 0.868395209f),
  120. Vec3(0.271511227f, 0.17087248f, 0.868395209f),
  121. Vec3(0.271511227f, 0.17087248f, 0.511006474f),
  122. Vec3(0.271511227f, 0.17087248f, 2.40789247f),
  123. Vec3(1.00427914f, 0.137102962f, 2.40789247f),
  124. Vec3(1.00427914f, 0.137102962f, 1.95698023f),
  125. Vec3(0.271511227f, 0.17087248f, 1.95698023f),
  126. Vec3(0.00150847435f, 0.098285675f, 2.40789247f),
  127. Vec3(1.00427914f, 0.098285675f, 2.40789247f),
  128. Vec3(1.00427914f, 0.137102962f, 0.868395209f),
  129. Vec3(1.00427914f, 0.137102962f, 0.511006474f),
  130. Vec3(0.00150847435f, 0.098285675f, 0.868395209f),
  131. Vec3(0.00150847435f, 0.098285675f, 1.95698023f),
  132. Vec3(0.00150847435f, 0.17087248f, 1.95698023f),
  133. Vec3(0.00150847435f, 0.17087248f, 2.40789247f),
  134. Vec3(0.19671753f, 0.109482117f, 1.15425301f),
  135. Vec3(0.175069571f, 0.109482117f, 1.2711879f),
  136. Vec3(-0.0175848603f, 0.139223307f, 0.959797204f),
  137. Vec3(0.0767889619f, 0.109482117f, 0.947003484f),
  138. Vec3(0.18563965f, 0.109482117f, 1.03236175f),
  139. Vec3(0.0897113085f, 0.098285675f, 1.32667887f),
  140. Vec3(-0.0175848603f, 0.139223307f, 1.31891572f),
  141. Vec3(0.0897113085f, 0.109482117f, 1.32667887f),
  142. Vec3(0.175069571f, 0.098285675f, 1.2711879f),
  143. Vec3(0.19671753f, 0.098285675f, 1.15425301f),
  144. Vec3(0.18563965f, 0.098285675f, 1.03236175f),
  145. Vec3(0.0767889619f, 0.098285675f, 0.947003484f)
  146. },
  147. {
  148. Vec3(0.0212580804f, 1.29376173f, 0.0102035152f),
  149. Vec3(0.0225791596f, 1.05854928f, 0.0887729526f),
  150. Vec3(0.0596007220f, 0.984267414f, 0.0408750288f),
  151. Vec3(0.0722020790f, 0.980246127f, -0.0416274220f),
  152. Vec3(-0.00376634207f, -0.718282819f, 0.00411359267f),
  153. Vec3(-0.00188124576f, -0.718283117f, 0.00229378697f),
  154. Vec3(-0.00162511703f, -0.718282461f, 0.00753012672f),
  155. Vec3(-0.00118427153f, 1.36079276f, 0.00107491738f),
  156. Vec3(-6.78644137e-05f, -0.718282998f, 0.00426622201f),
  157. Vec3(0.00102991192f, 1.29927433f, 0.0230795704f),
  158. Vec3(0.00699944887f, 1.05855191f, 0.0887731761f),
  159. Vec3(-0.00603519706f, 1.04913890f, -0.102404378f),
  160. Vec3(-0.0212373994f, 1.31092644f, 0.00530112581f),
  161. Vec3(-0.0542707182f, 1.07623804f, 0.0403260253f),
  162. Vec3(-0.0946691483f, 1.07357991f, -0.0185115524f),
  163. Vec3(-0.0946691483f, 1.07357991f, -0.0185115524f)
  164. },
  165. {
  166. Vec3(0.0283679180f, 0.0443800166f, -0.00569444988f),
  167. Vec3(0.0327114500f, -0.0221119970f, 0.0232404359f),
  168. Vec3(0.0374971032f, 0.0148781445f, -0.0245264377f),
  169. Vec3(0.0439460576f, 0.0126368264f, 0.0197663195f),
  170. Vec3(-0.0327170566f, 0.0423904508f, 0.0181609988f),
  171. Vec3(-0.0306955911f, 0.0311534479f, -0.0281516202f),
  172. Vec3(-0.0262422040f, 0.0248970203f, 0.0450032614f),
  173. Vec3(-0.0262093470f, 0.00906597450f, 0.0481815264f),
  174. Vec3(-0.0256845430f, -0.00607067533f, -0.0401362479f),
  175. Vec3(-0.0179684199f, 0.0266145933f, -0.0394567028f),
  176. Vec3(-0.00567848794f, -0.0313231349f, -0.0263656937f),
  177. Vec3(-0.00444967486f, -0.0383231938f, 0.0206601117f),
  178. Vec3(-0.00329093798f, 0.0464436933f, 0.0343827978f),
  179. Vec3(-0.00225042878f, 0.0550651476f, -0.00304153794f),
  180. Vec3(0.00310287252f, 0.00219658483f, 0.0542362332f),
  181. Vec3(0.00435558241f, 0.00644031307f, -0.0455060303f),
  182. Vec3(0.00495047215f, -0.0144955292f, 0.0482611060f),
  183. Vec3(0.00510909408f, 0.0300753452f, -0.0415933356f),
  184. Vec3(0.00619197031f, 0.0269140154f, 0.0500008501f),
  185. Vec3(0.0190936550f, -0.0106478147f, 0.0453430638f),
  186. Vec3(0.0202461667f, 0.00821140409f, 0.0500608832f),
  187. Vec3(0.0199985132f, 0.0353404805f, 0.0413853638f),
  188. Vec3(0.0267947838f, -0.0155944452f, -0.0300960485f),
  189. Vec3(0.0274163429f, 0.0318853259f, -0.0288569275f),
  190. Vec3(-0.0404368788f, -0.0213200711f, -0.00530833099f),
  191. Vec3(-0.0383560173f, -0.0111571737f, 0.0346816145f),
  192. Vec3(-0.0453024730f, 0.00178011740f, -0.0218658112f),
  193. Vec3(-0.0482929349f, 0.0101582557f, 0.0191618335f)
  194. },
  195. {
  196. Vec3(0.19555497f, 0.06892325f, 0.21078214f),
  197. Vec3(0.20527978f, -0.01703966f, -0.09207391f),
  198. Vec3(0.21142941f, 0.01785821f, -0.09836373f),
  199. Vec3(0.21466828f, 0.05084385f, -0.03549951f),
  200. Vec3(-0.20511348f, -0.07018351f, -0.31925454f),
  201. Vec3(-0.19310803f, -0.13756239f, -0.33457401f),
  202. Vec3(-0.20095457f, -0.09572067f, -0.11383702f),
  203. Vec3(-0.18695570f, -0.14865115f, -0.19356145f),
  204. Vec3(-0.18073241f, -0.08639215f, -0.35319963f),
  205. Vec3(-0.18014188f, -0.15241129f, -0.34185338f),
  206. Vec3(-0.18174356f, -0.15312561f, -0.19147469f),
  207. Vec3(-0.19579467f, 0.01310298f, -0.00632396f),
  208. Vec3(-0.16814114f, -0.05610058f, -0.34890732f),
  209. Vec3(-0.16448530f, -0.16787034f, -0.29141789f),
  210. Vec3(-0.17525161f, 0.01533679f, 0.08730947f),
  211. Vec3(-0.17286175f, 0.08774700f, -0.01591185f),
  212. Vec3(-0.17077128f, 0.01983560f, 0.10070839f),
  213. Vec3(-0.14615997f, -0.16541340f, -0.37489247f),
  214. Vec3(-0.14595763f, -0.16490393f, -0.37515628f),
  215. Vec3(-0.16272801f, 0.07975677f, 0.08464866f),
  216. Vec3(-0.13369306f, -0.06286648f, -0.37556374f),
  217. Vec3(-0.14785704f, 0.14323678f, -0.01563696f),
  218. Vec3(-0.12817731f, -0.04268694f, -0.36287897f),
  219. Vec3(-0.14112462f, 0.13547241f, 0.05140329f),
  220. Vec3(-0.12341158f, -0.17782864f, -0.36954373f),
  221. Vec3(-0.12310848f, -0.18070405f, -0.20412853f),
  222. Vec3(-0.09967888f, -0.18289816f, -0.38768309f),
  223. Vec3(-0.09960851f, 0.14144828f, 0.12903015f),
  224. Vec3(-0.08962545f, -0.17236463f, -0.39919903f),
  225. Vec3(-0.09338194f, -0.00865331f, 0.23358464f),
  226. Vec3(-0.09496998f, 0.17418922f, 0.03730623f),
  227. Vec3(-0.09499961f, 0.16077143f, -0.03914160f),
  228. Vec3(-0.08221246f, -0.07778487f, -0.39787262f),
  229. Vec3(-0.07918695f, -0.14616625f, -0.40242865f),
  230. Vec3(-0.08256439f, 0.01469633f, 0.24209134f),
  231. Vec3(-0.07199146f, 0.16959090f, 0.11185526f),
  232. Vec3(-0.05876892f, -0.18819671f, -0.40239989f),
  233. Vec3(-0.05744339f, -0.18692162f, -0.40386000f),
  234. Vec3(-0.04441069f, -0.04126521f, -0.37501192f),
  235. Vec3(-0.04648328f, 0.18093951f, 0.03905040f),
  236. Vec3(-0.03611449f, -0.14904837f, -0.40508240f),
  237. Vec3(-0.03163360f, 0.17144355f, 0.13303288f),
  238. Vec3(-0.02255749f, -0.01798030f, 0.33883106f),
  239. Vec3(-0.01062212f, -0.11764656f, -0.39784804f),
  240. Vec3(0.00002799f, -0.18946082f, -0.39155373f),
  241. Vec3(0.00190875f, -0.16691279f, -0.40337407f),
  242. Vec3(0.02337403f, -0.03170533f, 0.38295418f),
  243. Vec3(0.02689898f, -0.03111388f, 0.38642361f),
  244. Vec3(0.03513940f, -0.09795553f, -0.38733068f),
  245. Vec3(0.04139633f, -0.18845227f, -0.32015734f),
  246. Vec3(0.04843888f, 0.12765829f, -0.09677977f),
  247. Vec3(0.04454701f, -0.14539991f, -0.38590988f),
  248. Vec3(0.04690936f, -0.17584648f, -0.38177087f),
  249. Vec3(0.05052238f, -0.18907529f, -0.35411724f),
  250. Vec3(0.07129140f, -0.02806735f, 0.41684112f),
  251. Vec3(0.07599759f, 0.02516599f, 0.43382310f),
  252. Vec3(0.08328492f, -0.18135514f, -0.32588836f),
  253. Vec3(0.08443428f, 0.07232403f, 0.37877142f),
  254. Vec3(0.09074404f, -0.15272216f, -0.36002999f),
  255. Vec3(0.09381036f, -0.04931259f, -0.32999005f),
  256. Vec3(0.09348832f, -0.17767928f, -0.33666068f),
  257. Vec3(0.09247280f, -0.01328942f, 0.44227284f),
  258. Vec3(0.09364306f, 0.03557658f, 0.44191616f),
  259. Vec3(0.09611026f, -0.01203391f, 0.44345939f),
  260. Vec3(0.09662163f, 0.03456752f, 0.44326156f),
  261. Vec3(0.10482377f, 0.12817247f, 0.27224415f),
  262. Vec3(0.11271536f, 0.12685699f, 0.26856660f),
  263. Vec3(0.10957191f, 0.03837919f, 0.43455946f),
  264. Vec3(0.11146642f, -0.01284471f, 0.42120608f),
  265. Vec3(0.11088928f, 0.00377234f, 0.44789928f),
  266. Vec3(0.11571233f, -0.12474029f, -0.34762913f),
  267. Vec3(0.12183426f, -0.16410264f, -0.30295142f),
  268. Vec3(0.12211698f, 0.01099167f, 0.44373258f),
  269. Vec3(0.12308656f, 0.01315179f, 0.44303578f),
  270. Vec3(0.13090495f, -0.15086941f, -0.31031519f),
  271. Vec3(0.14427974f, 0.09778974f, 0.30786031f),
  272. Vec3(0.14200252f, 0.01419945f, 0.41783332f),
  273. Vec3(0.14424091f, 0.06972501f, 0.37377491f),
  274. Vec3(0.14422383f, 0.02227210f, 0.41717034f),
  275. Vec3(0.15133176f, -0.03861540f, -0.27380293f),
  276. Vec3(0.14738929f, 0.06972805f, 0.37101438f),
  277. Vec3(0.15116664f, -0.13012324f, -0.26891800f),
  278. Vec3(0.15432675f, -0.05065062f, -0.27696538f),
  279. Vec3(0.17231981f, 0.09891064f, -0.04109610f),
  280. Vec3(0.15486444f, 0.03080789f, 0.39333733f),
  281. Vec3(0.16293872f, 0.09977609f, 0.23133035f),
  282. Vec3(0.17278114f, 0.05925680f, -0.13166353f),
  283. Vec3(0.17344120f, 0.06815492f, 0.29800513f),
  284. Vec3(0.18346339f, 0.03002923f, -0.16944433f),
  285. Vec3(0.18475264f, -0.03337195f, -0.21144425f),
  286. Vec3(0.18153211f, 0.05077920f, 0.29410797f),
  287. Vec3(0.18872119f, 0.08419117f, 0.18681980f),
  288. Vec3(0.19402013f, 0.03129275f, -0.14645814f),
  289. Vec3(0.20299899f, 0.06450803f, -0.05323168f),
  290. Vec3(-0.20916573f, -0.14482390f, -0.28754678f),
  291. Vec3(-0.21912349f, -0.12297497f, -0.25853595f),
  292. Vec3(-0.21891747f, -0.11492035f, -0.30946639f),
  293. Vec3(-0.22503024f, -0.09871494f, -0.27031892f),
  294. Vec3(-0.22503024f, -0.09871494f, -0.27031892f),
  295. Vec3(-0.22503024f, -0.09871494f, -0.27031892f)
  296. },
  297. {
  298. Vec3(0.28483882f, 0.09470236f, 0.11433057f),
  299. Vec3(0.30260321f, 0.07340867f, 0.00849266f),
  300. Vec3(0.30380272f, 0.05582517f, -0.22405298f),
  301. Vec3(0.30670973f, 0.02778204f, -0.22415190f),
  302. Vec3(-0.29766368f, -0.06492511f, -0.19135096f),
  303. Vec3(-0.28324991f, 0.02856347f, 0.16558051f),
  304. Vec3(-0.27339774f, 0.11253071f, -0.13812468f),
  305. Vec3(-0.26324614f, -0.03483995f, 0.34903234f),
  306. Vec3(-0.27118766f, -0.15035510f, -0.06431498f),
  307. Vec3(-0.26041472f, 0.10464326f, -0.20795805f),
  308. Vec3(-0.22156618f, -0.00712212f, 0.40348106f),
  309. Vec3(-0.20013636f, 0.13795423f, -0.23888915f),
  310. Vec3(-0.19368620f, 0.04208890f, 0.42129427f),
  311. Vec3(-0.18170905f, -0.10169907f, 0.38139578f),
  312. Vec3(-0.18724660f, 0.18995818f, 0.08522552f),
  313. Vec3(-0.17479378f, -0.05597380f, 0.41057986f),
  314. Vec3(-0.15012621f, 0.08595391f, 0.43914794f),
  315. Vec3(-0.11722116f, -0.10298516f, -0.30289822f),
  316. Vec3(-0.11217459f, 0.00596011f, 0.44133874f),
  317. Vec3(-0.11709289f, 0.23012112f, 0.12055066f),
  318. Vec3(-0.10705470f, 0.15775623f, -0.33419770f),
  319. Vec3(-0.08655276f, 0.09824081f, 0.43651989f),
  320. Vec3(-0.08401379f, 0.08668444f, -0.41111666f),
  321. Vec3(-0.08026488f, -0.24695427f, -0.01228247f),
  322. Vec3(-0.06294082f, 0.12666735f, -0.39178270f),
  323. Vec3(-0.05308891f, -0.07724215f, -0.37346649f),
  324. Vec3(-0.04869145f, -0.23846265f, -0.11154356f),
  325. Vec3(-0.04377052f, 0.06346821f, 0.44263243f),
  326. Vec3(-0.03821557f, 0.05776290f, -0.43330976f),
  327. Vec3(-0.01401243f, -0.07849873f, 0.37016886f),
  328. Vec3(-0.01267736f, -0.24327334f, -0.09846258f),
  329. Vec3(-0.00871999f, -0.24532425f, -0.01158716f),
  330. Vec3(0.00610917f, 0.20575316f, -0.32363408f),
  331. Vec3(0.01893912f, -0.02637211f, -0.44099009f),
  332. Vec3(0.03742292f, 0.25572568f, 0.11976100f),
  333. Vec3(0.04572892f, -0.02452080f, 0.37599292f),
  334. Vec3(0.04809525f, 0.11413645f, 0.38247618f),
  335. Vec3(0.04934106f, -0.01875172f, -0.43612641f),
  336. Vec3(0.07854398f, 0.13351599f, 0.34539741f),
  337. Vec3(0.11064179f, 0.03347895f, 0.33272063f),
  338. Vec3(0.11110801f, 0.04016598f, -0.42360800f),
  339. Vec3(0.12390327f, -0.20230874f, -0.01599736f),
  340. Vec3(0.13082972f, -0.19843940f, -0.08606190f),
  341. Vec3(0.12559986f, -0.02563187f, -0.38013845f),
  342. Vec3(0.12924608f, 0.16206453f, -0.34893369f),
  343. Vec3(0.15646456f, 0.21451330f, 0.16623015f),
  344. Vec3(0.17851203f, -0.14074428f, 0.08427754f),
  345. Vec3(0.19401437f, -0.15288332f, -0.03272480f),
  346. Vec3(0.20102191f, 0.08705597f, -0.37915167f),
  347. Vec3(0.20596674f, 0.06604006f, -0.38868805f),
  348. Vec3(0.26085311f, 0.08702713f, -0.32507085f),
  349. Vec3(0.27331018f, 0.15497627f, 0.11259682f),
  350. Vec3(0.27269470f, 0.03719006f, -0.31962081f),
  351. Vec3(0.27288356f, 0.06217747f, -0.33064606f),
  352. Vec3(-0.29314118f, -0.18079891f, 0.24351751f),
  353. Vec3(-0.30831277f, -0.06952596f, 0.07340523f),
  354. Vec3(-0.30126276f, -0.18365636f, 0.22815129f),
  355. Vec3(-0.30392047f, -0.17969127f, 0.22713920f),
  356. Vec3(-0.30392047f, -0.17969127f, 0.22713920f),
  357. Vec3(-0.30392047f, -0.17969127f, 0.22713920f)
  358. },
  359. {
  360. // A really small hull
  361. Vec3(-0.00707678869f, 0.00559568405f, -0.0239779726f),
  362. Vec3(0.0136205591f, 0.00541752577f, -0.0225500446f),
  363. Vec3(0.0135576781f, 0.00559568405f, -0.0224227905f),
  364. Vec3(-0.0108219199f, 0.00559568405f, -0.0223935191f),
  365. Vec3(0.0137226451f, 0.00559568405f, -0.0220940933f),
  366. Vec3(0.00301175844f, -0.0232942104f, -0.0214947499f),
  367. Vec3(0.017349612f, 0.00559568405f, 0.0241708681f),
  368. Vec3(0.00390899926f, -0.0368074179f, 0.0541367307f),
  369. Vec3(-0.0164459459f, 0.00559568405f, 0.0607497096f),
  370. Vec3(-0.0169881769f, 0.00559568405f, 0.0608173609f),
  371. Vec3(-0.0168782212f, 0.0052883029f, 0.0613293499f),
  372. Vec3(-0.00663783913f, 0.00559568405f, -0.024154868f),
  373. Vec3(-0.00507298298f, 0.00559568405f, -0.0242112875f),
  374. Vec3(-0.00565947127f, 0.00477081537f, -0.0243848339f),
  375. Vec3(0.0118075963f, 0.00124305487f, -0.0258472487f),
  376. Vec3(0.00860248506f, -0.00697988272f, -0.0276725553f),
  377. },
  378. {
  379. // Nearly co-planar hull (but not enough to go through the 2d hull builder)
  380. Vec3(0.129325435f, -0.213319957f, 0.00901593268f),
  381. Vec3(0.129251331f, -0.213436425f, 0.00932094082f),
  382. Vec3(0.160741553f, -0.171540618f, 0.0494558439f),
  383. Vec3(0.160671368f, -0.17165187f, 0.049765937f),
  384. Vec3(0.14228563f, 0.432965666f, 0.282429159f),
  385. Vec3(0.142746598f, 0.433226734f, 0.283286631f),
  386. Vec3(0.296031296f, 0.226935148f, 0.312804461f),
  387. Vec3(0.296214104f, 0.227568939f, 0.313606918f),
  388. Vec3(-0.00354258716f, -0.180767179f, -0.0762089267f),
  389. Vec3(-0.00372517109f, -0.1805875f, -0.0766792595f),
  390. Vec3(-0.0157070309f, -0.176182508f, -0.0833940506f),
  391. Vec3(-0.0161666721f, -0.175898403f, -0.0840280354f),
  392. Vec3(-0.342764735f, 0.0259497911f, -0.244388372f),
  393. Vec3(-0.342298329f, 0.0256615728f, -0.24456653f),
  394. Vec3(-0.366584063f, 0.0554589033f, -0.250078142f),
  395. Vec3(-0.366478682f, 0.0556178838f, -0.250342518f),
  396. },
  397. {
  398. // A hull with a very acute angle that won't properly build when using distance to plane only
  399. Vec3(-0.0451235026f, -0.103826642f, -0.0346511155f),
  400. Vec3(-0.0194563419f, -0.123563275f, -0.032212317f),
  401. Vec3(0.0323024541f, -0.0468643308f, -0.0307639092f),
  402. Vec3(0.0412166864f, -0.0884782523f, -0.0288816988f),
  403. Vec3(-0.0564572513f, 0.0207469314f, 0.0169318169f),
  404. Vec3(0.00537410378f, 0.105688639f, 0.0355164111f),
  405. Vec3(0.0209896415f, 0.117749952f, 0.0365252197f),
  406. Vec3(0.0211542398f, 0.118546993f, 0.0375355929f),
  407. }
  408. };
  409. // Add a cube formed out of a regular grid of vertices, this shows how the algorithm deals
  410. // with many coplanar points
  411. {
  412. Points p;
  413. for (int x = 0; x < 10; ++x)
  414. for (int y = 0; y < 10; ++y)
  415. for (int z = 0; z < 10; ++z)
  416. p.push_back(Vec3::sReplicate(-0.5f) * 0.1f * Vec3(float(x), float(y), float(z)));
  417. mPoints.push_back(std::move(p));
  418. }
  419. // Add disc of many points
  420. {
  421. Points p;
  422. Mat44 rot = Mat44::sRotationZ(0.25f * JPH_PI);
  423. for (float r = 0.0f; r < 2.0f; r += 0.1f)
  424. for (float phi = 0.0f; phi <= 2.0f * JPH_PI; phi += 2.0f * JPH_PI / 20.0f)
  425. p.push_back(rot * Vec3(r * Cos(phi), r * Sin(phi), 0));
  426. mPoints.push_back(std::move(p));
  427. }
  428. // Add wedge shaped disc that is just above the hull tolerance on its widest side and zero on the other side
  429. {
  430. Points p;
  431. for (float phi = 0.0f; phi <= 2.0f * JPH_PI; phi += 2.0f * JPH_PI / 40.0f)
  432. {
  433. Vec3 pos(2.0f * Cos(phi), 0, 2.0f * Sin(phi));
  434. p.push_back(pos);
  435. p.push_back(pos + Vec3(0, 2.0e-3f * (2.0f + pos.GetX()) / 4.0f, 0));
  436. }
  437. mPoints.push_back(std::move(p));
  438. }
  439. // Add a sphere of many points
  440. {
  441. Points p;
  442. for (float theta = 0.0f; theta <= JPH_PI; theta += JPH_PI / 20.0f)
  443. for (float phi = 0.0f; phi <= 2.0f * JPH_PI; phi += 2.0f * JPH_PI / 20.0f)
  444. p.push_back(Vec3::sUnitSpherical(theta, phi));
  445. mPoints.push_back(std::move(p));
  446. }
  447. // Open the external file with hulls
  448. // A stream containing predefined convex hulls
  449. ifstream points_stream("Assets/convex_hulls.bin", std::ios::binary);
  450. if (points_stream.is_open())
  451. {
  452. for (;;)
  453. {
  454. // Read the length of the next point cloud
  455. uint32 len = 0;
  456. points_stream.read((char *)&len, sizeof(len));
  457. if (points_stream.eof())
  458. break;
  459. // Read the points
  460. if (len > 0)
  461. {
  462. Points p;
  463. for (uint32 i = 0; i < len; ++i)
  464. {
  465. Float3 v;
  466. points_stream.read((char *)&v, sizeof(v));
  467. p.push_back(Vec3(v));
  468. }
  469. mPoints.push_back(std::move(p));
  470. }
  471. }
  472. }
  473. }
  474. void ConvexHullTest::PrePhysicsUpdate(const PreUpdateParams &inParams)
  475. {
  476. const float display_scale = 10.0f;
  477. float tolerance = 1.0e-3f;
  478. Points points;
  479. if (mIteration < mPoints.size())
  480. {
  481. // Take one of the predefined shapes
  482. points = mPoints[mIteration];
  483. }
  484. else
  485. {
  486. uniform_real_distribution<float> zero_one(0.0f, 1.0f);
  487. uniform_real_distribution<float> zero_two(0.0f, 2.0f);
  488. // Define vertex scale
  489. uniform_real_distribution<float> scale_start(0.1f, 0.5f);
  490. uniform_real_distribution<float> scale_range(0.1f, 2.0f);
  491. float start = scale_start(mRandom);
  492. uniform_real_distribution<float> vertex_scale(start, start + scale_range(mRandom));
  493. // Define shape scale to make shape less sphere like
  494. uniform_real_distribution<float> shape_scale(0.1f, 1.0f);
  495. Vec3 scale(shape_scale(mRandom), shape_scale(mRandom), shape_scale(mRandom));
  496. // Add some random points
  497. for (int i = 0; i < 100; ++i)
  498. {
  499. // Add random point
  500. Vec3 p1 = vertex_scale(mRandom) * Vec3::sRandom(mRandom) * scale;
  501. points.push_back(p1);
  502. // Point close to p1
  503. Vec3 p2 = p1 + tolerance * zero_two(mRandom) * Vec3::sRandom(mRandom);
  504. points.push_back(p2);
  505. // Point on a line to another point
  506. float fraction = zero_one(mRandom);
  507. Vec3 p3 = fraction * p1 + (1.0f - fraction) * points[mRandom() % points.size()];
  508. points.push_back(p3);
  509. // Point close to p3
  510. Vec3 p4 = p3 + tolerance * zero_two(mRandom) * Vec3::sRandom(mRandom);
  511. points.push_back(p4);
  512. }
  513. }
  514. mIteration++;
  515. using Face = ConvexHullBuilder::Face;
  516. using Edge = ConvexHullBuilder::Edge;
  517. ConvexHullBuilder builder(points);
  518. // Build the hull
  519. const char *error = nullptr;
  520. ConvexHullBuilder::EResult result = builder.Initialize(INT_MAX, tolerance, error);
  521. if (result != ConvexHullBuilder::EResult::Success && result != ConvexHullBuilder::EResult::MaxVerticesReached)
  522. {
  523. Trace("Iteration %d: Failed to initialize from positions: %s", mIteration - 1, error);
  524. JPH_ASSERT(false);
  525. return;
  526. }
  527. // Determine center of mass
  528. Vec3 com;
  529. float vol;
  530. builder.GetCenterOfMassAndVolume(com, vol);
  531. // Test if all points are inside the hull with the given tolerance
  532. float max_error, coplanar_distance;
  533. int max_error_point;
  534. Face *max_error_face;
  535. builder.DetermineMaxError(max_error_face, max_error, max_error_point, coplanar_distance);
  536. // Check if error is bigger than 4 * the tolerance
  537. if (max_error > 4.0f * max(tolerance, coplanar_distance))
  538. {
  539. Trace("Iteration %d: max_error=%g", mIteration - 1, (double)max_error);
  540. // Draw point that had the max error
  541. Vec3 point = display_scale * (points[max_error_point] - com);
  542. mDebugRenderer->DrawMarker(point, Color::sRed, 1.0f);
  543. mDebugRenderer->DrawText3D(point, StringFormat("%d: %g", max_error_point, (double)max_error), Color::sRed);
  544. // Length of normal (2x area) for max error face
  545. Vec3 centroid = display_scale * (max_error_face->mCentroid - com);
  546. Vec3 centroid_plus_normal = centroid + max_error_face->mNormal.Normalized();
  547. mDebugRenderer->DrawArrow(centroid, centroid_plus_normal, Color::sGreen, 0.1f);
  548. mDebugRenderer->DrawText3D(centroid_plus_normal, ConvertToString(max_error_face->mNormal.Length()), Color::sGreen);
  549. // Draw face that had the max error
  550. const Edge *e = max_error_face->mFirstEdge;
  551. Vec3 prev = display_scale * (points[e->mStartIdx] - com);
  552. do
  553. {
  554. const Edge *next = e->mNextEdge;
  555. Vec3 cur = display_scale * (points[next->mStartIdx] - com);
  556. mDebugRenderer->DrawArrow(prev, cur, Color::sYellow, 0.01f);
  557. mDebugRenderer->DrawText3D(prev, ConvertToString(e->mStartIdx), Color::sYellow);
  558. e = next;
  559. prev = cur;
  560. } while (e != max_error_face->mFirstEdge);
  561. JPH_ASSERT(false);
  562. }
  563. // Draw input points around center of mass
  564. for (Vec3 v : points)
  565. mDebugRenderer->DrawMarker(display_scale * (v - com), Color::sWhite, 0.01f);
  566. // Draw the hull around center of mass
  567. int color_idx = 0;
  568. for (Face *f : builder.GetFaces())
  569. {
  570. Color color = Color::sGetDistinctColor(color_idx++);
  571. // First point
  572. const Edge *e = f->mFirstEdge;
  573. Vec3 p1 = display_scale * (points[e->mStartIdx] - com);
  574. // Second point
  575. e = e->mNextEdge;
  576. Vec3 p2 = display_scale * (points[e->mStartIdx] - com);
  577. // First line
  578. mDebugRenderer->DrawLine(p1, p2, Color::sGrey);
  579. do
  580. {
  581. // Third point
  582. e = e->mNextEdge;
  583. Vec3 p3 = display_scale * (points[e->mStartIdx] - com);
  584. mDebugRenderer->DrawTriangle(p1, p2, p3, color);
  585. mDebugRenderer->DrawLine(p2, p3, Color::sGrey);
  586. p2 = p3;
  587. }
  588. while (e != f->mFirstEdge);
  589. }
  590. }