HullUtils.cs 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868
  1. /* The MIT License
  2. *
  3. * Copyright (c) 2010 Intel Corporation.
  4. * All rights reserved.
  5. *
  6. * Based on the convexdecomposition library from
  7. * <http://codesuppository.googlecode.com> by John W. Ratcliff and Stan Melax.
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Diagnostics;
  30. namespace OpenSim.Region.Physics.ConvexDecompositionDotNet
  31. {
  32. public static class HullUtils
  33. {
  34. public static int argmin(float[] a, int n)
  35. {
  36. int r = 0;
  37. for (int i = 1; i < n; i++)
  38. {
  39. if (a[i] < a[r])
  40. {
  41. r = i;
  42. }
  43. }
  44. return r;
  45. }
  46. public static float clampf(float a)
  47. {
  48. return Math.Min(1.0f, Math.Max(0.0f, a));
  49. }
  50. public static float Round(float a, float precision)
  51. {
  52. return (float)Math.Floor(0.5f + a / precision) * precision;
  53. }
  54. public static float Interpolate(float f0, float f1, float alpha)
  55. {
  56. return f0 * (1 - alpha) + f1 * alpha;
  57. }
  58. public static void Swap<T>(ref T a, ref T b)
  59. {
  60. T tmp = a;
  61. a = b;
  62. b = tmp;
  63. }
  64. public static bool above(List<float3> vertices, int3 t, float3 p, float epsilon)
  65. {
  66. float3 vtx = vertices[t.x];
  67. float3 n = TriNormal(vtx, vertices[t.y], vertices[t.z]);
  68. return (float3.dot(n, p - vtx) > epsilon); // EPSILON???
  69. }
  70. public static int hasedge(int3 t, int a, int b)
  71. {
  72. for (int i = 0; i < 3; i++)
  73. {
  74. int i1 = (i + 1) % 3;
  75. if (t[i] == a && t[i1] == b)
  76. return 1;
  77. }
  78. return 0;
  79. }
  80. public static bool hasvert(int3 t, int v)
  81. {
  82. return (t[0] == v || t[1] == v || t[2] == v);
  83. }
  84. public static int shareedge(int3 a, int3 b)
  85. {
  86. int i;
  87. for (i = 0; i < 3; i++)
  88. {
  89. int i1 = (i + 1) % 3;
  90. if (hasedge(a, b[i1], b[i]) != 0)
  91. return 1;
  92. }
  93. return 0;
  94. }
  95. public static void b2bfix(HullTriangle s, HullTriangle t, List<HullTriangle> tris)
  96. {
  97. int i;
  98. for (i = 0; i < 3; i++)
  99. {
  100. int i1 = (i + 1) % 3;
  101. int i2 = (i + 2) % 3;
  102. int a = (s)[i1];
  103. int b = (s)[i2];
  104. Debug.Assert(tris[s.neib(a, b)].neib(b, a) == s.id);
  105. Debug.Assert(tris[t.neib(a, b)].neib(b, a) == t.id);
  106. tris[s.neib(a, b)].setneib(b, a, t.neib(b, a));
  107. tris[t.neib(b, a)].setneib(a, b, s.neib(a, b));
  108. }
  109. }
  110. public static void removeb2b(HullTriangle s, HullTriangle t, List<HullTriangle> tris)
  111. {
  112. b2bfix(s, t, tris);
  113. s.Dispose();
  114. t.Dispose();
  115. }
  116. public static void checkit(HullTriangle t, List<HullTriangle> tris)
  117. {
  118. int i;
  119. Debug.Assert(tris[t.id] == t);
  120. for (i = 0; i < 3; i++)
  121. {
  122. int i1 = (i + 1) % 3;
  123. int i2 = (i + 2) % 3;
  124. int a = (t)[i1];
  125. int b = (t)[i2];
  126. Debug.Assert(a != b);
  127. Debug.Assert(tris[t.n[i]].neib(b, a) == t.id);
  128. }
  129. }
  130. public static void extrude(HullTriangle t0, int v, List<HullTriangle> tris)
  131. {
  132. int3 t = t0;
  133. int n = tris.Count;
  134. HullTriangle ta = new HullTriangle(v, t[1], t[2], tris);
  135. ta.n = new int3(t0.n[0], n + 1, n + 2);
  136. tris[t0.n[0]].setneib(t[1], t[2], n + 0);
  137. HullTriangle tb = new HullTriangle(v, t[2], t[0], tris);
  138. tb.n = new int3(t0.n[1], n + 2, n + 0);
  139. tris[t0.n[1]].setneib(t[2], t[0], n + 1);
  140. HullTriangle tc = new HullTriangle(v, t[0], t[1], tris);
  141. tc.n = new int3(t0.n[2], n + 0, n + 1);
  142. tris[t0.n[2]].setneib(t[0], t[1], n + 2);
  143. checkit(ta, tris);
  144. checkit(tb, tris);
  145. checkit(tc, tris);
  146. if (hasvert(tris[ta.n[0]], v))
  147. removeb2b(ta, tris[ta.n[0]], tris);
  148. if (hasvert(tris[tb.n[0]], v))
  149. removeb2b(tb, tris[tb.n[0]], tris);
  150. if (hasvert(tris[tc.n[0]], v))
  151. removeb2b(tc, tris[tc.n[0]], tris);
  152. t0.Dispose();
  153. }
  154. public static HullTriangle extrudable(float epsilon, List<HullTriangle> tris)
  155. {
  156. int i;
  157. HullTriangle t = null;
  158. for (i = 0; i < tris.Count; i++)
  159. {
  160. if (t == null || (tris.Count > i && (object)tris[i] != null && t.rise < tris[i].rise))
  161. {
  162. t = tris[i];
  163. }
  164. }
  165. return (t.rise > epsilon) ? t : null;
  166. }
  167. public static Quaternion RotationArc(float3 v0, float3 v1)
  168. {
  169. Quaternion q = new Quaternion();
  170. v0 = float3.normalize(v0); // Comment these two lines out if you know its not needed.
  171. v1 = float3.normalize(v1); // If vector is already unit length then why do it again?
  172. float3 c = float3.cross(v0, v1);
  173. float d = float3.dot(v0, v1);
  174. if (d <= -1.0f) // 180 about x axis
  175. {
  176. return new Quaternion(1f, 0f, 0f, 0f);
  177. }
  178. float s = (float)Math.Sqrt((1 + d) * 2f);
  179. q.x = c.x / s;
  180. q.y = c.y / s;
  181. q.z = c.z / s;
  182. q.w = s / 2.0f;
  183. return q;
  184. }
  185. public static float3 PlaneLineIntersection(Plane plane, float3 p0, float3 p1)
  186. {
  187. // returns the point where the line p0-p1 intersects the plane n&d
  188. float3 dif = p1 - p0;
  189. float dn = float3.dot(plane.normal, dif);
  190. float t = -(plane.dist + float3.dot(plane.normal, p0)) / dn;
  191. return p0 + (dif * t);
  192. }
  193. public static float3 LineProject(float3 p0, float3 p1, float3 a)
  194. {
  195. float3 w = new float3();
  196. w = p1 - p0;
  197. float t = float3.dot(w, (a - p0)) / (w.x * w.x + w.y * w.y + w.z * w.z);
  198. return p0 + w * t;
  199. }
  200. public static float3 PlaneProject(Plane plane, float3 point)
  201. {
  202. return point - plane.normal * (float3.dot(point, plane.normal) + plane.dist);
  203. }
  204. public static float LineProjectTime(float3 p0, float3 p1, float3 a)
  205. {
  206. float3 w = new float3();
  207. w = p1 - p0;
  208. float t = float3.dot(w, (a - p0)) / (w.x * w.x + w.y * w.y + w.z * w.z);
  209. return t;
  210. }
  211. public static float3 ThreePlaneIntersection(Plane p0, Plane p1, Plane p2)
  212. {
  213. float3x3 mp = float3x3.Transpose(new float3x3(p0.normal, p1.normal, p2.normal));
  214. float3x3 mi = float3x3.Inverse(mp);
  215. float3 b = new float3(p0.dist, p1.dist, p2.dist);
  216. return -b * mi;
  217. }
  218. public static bool PolyHit(List<float3> vert, float3 v0, float3 v1)
  219. {
  220. float3 impact = new float3();
  221. float3 normal = new float3();
  222. return PolyHit(vert, v0, v1, out impact, out normal);
  223. }
  224. public static bool PolyHit(List<float3> vert, float3 v0, float3 v1, out float3 impact)
  225. {
  226. float3 normal = new float3();
  227. return PolyHit(vert, v0, v1, out impact, out normal);
  228. }
  229. public static bool PolyHit(List<float3> vert, float3 v0, float3 v1, out float3 impact, out float3 normal)
  230. {
  231. float3 the_point = new float3();
  232. impact = null;
  233. normal = null;
  234. int i;
  235. float3 nrml = new float3(0, 0, 0);
  236. for (i = 0; i < vert.Count; i++)
  237. {
  238. int i1 = (i + 1) % vert.Count;
  239. int i2 = (i + 2) % vert.Count;
  240. nrml = nrml + float3.cross(vert[i1] - vert[i], vert[i2] - vert[i1]);
  241. }
  242. float m = float3.magnitude(nrml);
  243. if (m == 0.0)
  244. {
  245. return false;
  246. }
  247. nrml = nrml * (1.0f / m);
  248. float dist = -float3.dot(nrml, vert[0]);
  249. float d0;
  250. float d1;
  251. if ((d0 = float3.dot(v0, nrml) + dist) < 0 || (d1 = float3.dot(v1, nrml) + dist) > 0)
  252. {
  253. return false;
  254. }
  255. // By using the cached plane distances d0 and d1
  256. // we can optimize the following:
  257. // the_point = planelineintersection(nrml,dist,v0,v1);
  258. float a = d0 / (d0 - d1);
  259. the_point = v0 * (1 - a) + v1 * a;
  260. bool inside = true;
  261. for (int j = 0; inside && j < vert.Count; j++)
  262. {
  263. // let inside = 0 if outside
  264. float3 pp1 = new float3();
  265. float3 pp2 = new float3();
  266. float3 side = new float3();
  267. pp1 = vert[j];
  268. pp2 = vert[(j + 1) % vert.Count];
  269. side = float3.cross((pp2 - pp1), (the_point - pp1));
  270. inside = (float3.dot(nrml, side) >= 0.0);
  271. }
  272. if (inside)
  273. {
  274. if (normal != null)
  275. {
  276. normal = nrml;
  277. }
  278. if (impact != null)
  279. {
  280. impact = the_point;
  281. }
  282. }
  283. return inside;
  284. }
  285. public static bool BoxInside(float3 p, float3 bmin, float3 bmax)
  286. {
  287. return (p.x >= bmin.x && p.x <= bmax.x && p.y >= bmin.y && p.y <= bmax.y && p.z >= bmin.z && p.z <= bmax.z);
  288. }
  289. public static bool BoxIntersect(float3 v0, float3 v1, float3 bmin, float3 bmax, float3 impact)
  290. {
  291. if (BoxInside(v0, bmin, bmax))
  292. {
  293. impact = v0;
  294. return true;
  295. }
  296. if (v0.x <= bmin.x && v1.x >= bmin.x)
  297. {
  298. float a = (bmin.x - v0.x) / (v1.x - v0.x);
  299. //v.x = bmin.x;
  300. float vy = (1 - a) * v0.y + a * v1.y;
  301. float vz = (1 - a) * v0.z + a * v1.z;
  302. if (vy >= bmin.y && vy <= bmax.y && vz >= bmin.z && vz <= bmax.z)
  303. {
  304. impact.x = bmin.x;
  305. impact.y = vy;
  306. impact.z = vz;
  307. return true;
  308. }
  309. }
  310. else if (v0.x >= bmax.x && v1.x <= bmax.x)
  311. {
  312. float a = (bmax.x - v0.x) / (v1.x - v0.x);
  313. //v.x = bmax.x;
  314. float vy = (1 - a) * v0.y + a * v1.y;
  315. float vz = (1 - a) * v0.z + a * v1.z;
  316. if (vy >= bmin.y && vy <= bmax.y && vz >= bmin.z && vz <= bmax.z)
  317. {
  318. impact.x = bmax.x;
  319. impact.y = vy;
  320. impact.z = vz;
  321. return true;
  322. }
  323. }
  324. if (v0.y <= bmin.y && v1.y >= bmin.y)
  325. {
  326. float a = (bmin.y - v0.y) / (v1.y - v0.y);
  327. float vx = (1 - a) * v0.x + a * v1.x;
  328. //v.y = bmin.y;
  329. float vz = (1 - a) * v0.z + a * v1.z;
  330. if (vx >= bmin.x && vx <= bmax.x && vz >= bmin.z && vz <= bmax.z)
  331. {
  332. impact.x = vx;
  333. impact.y = bmin.y;
  334. impact.z = vz;
  335. return true;
  336. }
  337. }
  338. else if (v0.y >= bmax.y && v1.y <= bmax.y)
  339. {
  340. float a = (bmax.y - v0.y) / (v1.y - v0.y);
  341. float vx = (1 - a) * v0.x + a * v1.x;
  342. // vy = bmax.y;
  343. float vz = (1 - a) * v0.z + a * v1.z;
  344. if (vx >= bmin.x && vx <= bmax.x && vz >= bmin.z && vz <= bmax.z)
  345. {
  346. impact.x = vx;
  347. impact.y = bmax.y;
  348. impact.z = vz;
  349. return true;
  350. }
  351. }
  352. if (v0.z <= bmin.z && v1.z >= bmin.z)
  353. {
  354. float a = (bmin.z - v0.z) / (v1.z - v0.z);
  355. float vx = (1 - a) * v0.x + a * v1.x;
  356. float vy = (1 - a) * v0.y + a * v1.y;
  357. // v.z = bmin.z;
  358. if (vy >= bmin.y && vy <= bmax.y && vx >= bmin.x && vx <= bmax.x)
  359. {
  360. impact.x = vx;
  361. impact.y = vy;
  362. impact.z = bmin.z;
  363. return true;
  364. }
  365. }
  366. else if (v0.z >= bmax.z && v1.z <= bmax.z)
  367. {
  368. float a = (bmax.z - v0.z) / (v1.z - v0.z);
  369. float vx = (1 - a) * v0.x + a * v1.x;
  370. float vy = (1 - a) * v0.y + a * v1.y;
  371. // v.z = bmax.z;
  372. if (vy >= bmin.y && vy <= bmax.y && vx >= bmin.x && vx <= bmax.x)
  373. {
  374. impact.x = vx;
  375. impact.y = vy;
  376. impact.z = bmax.z;
  377. return true;
  378. }
  379. }
  380. return false;
  381. }
  382. public static float DistanceBetweenLines(float3 ustart, float3 udir, float3 vstart, float3 vdir, float3 upoint)
  383. {
  384. return DistanceBetweenLines(ustart, udir, vstart, vdir, upoint, null);
  385. }
  386. public static float DistanceBetweenLines(float3 ustart, float3 udir, float3 vstart, float3 vdir)
  387. {
  388. return DistanceBetweenLines(ustart, udir, vstart, vdir, null, null);
  389. }
  390. public static float DistanceBetweenLines(float3 ustart, float3 udir, float3 vstart, float3 vdir, float3 upoint, float3 vpoint)
  391. {
  392. float3 cp = float3.normalize(float3.cross(udir, vdir));
  393. float distu = -float3.dot(cp, ustart);
  394. float distv = -float3.dot(cp, vstart);
  395. float dist = (float)Math.Abs(distu - distv);
  396. if (upoint != null)
  397. {
  398. Plane plane = new Plane();
  399. plane.normal = float3.normalize(float3.cross(vdir, cp));
  400. plane.dist = -float3.dot(plane.normal, vstart);
  401. upoint = PlaneLineIntersection(plane, ustart, ustart + udir);
  402. }
  403. if (vpoint != null)
  404. {
  405. Plane plane = new Plane();
  406. plane.normal = float3.normalize(float3.cross(udir, cp));
  407. plane.dist = -float3.dot(plane.normal, ustart);
  408. vpoint = PlaneLineIntersection(plane, vstart, vstart + vdir);
  409. }
  410. return dist;
  411. }
  412. public static float3 TriNormal(float3 v0, float3 v1, float3 v2)
  413. {
  414. // return the normal of the triangle
  415. // inscribed by v0, v1, and v2
  416. float3 cp = float3.cross(v1 - v0, v2 - v1);
  417. float m = float3.magnitude(cp);
  418. if (m == 0)
  419. return new float3(1, 0, 0);
  420. return cp * (1.0f / m);
  421. }
  422. public static int PlaneTest(Plane p, float3 v, float planetestepsilon)
  423. {
  424. float a = float3.dot(v, p.normal) + p.dist;
  425. int flag = (a > planetestepsilon) ? (2) : ((a < -planetestepsilon) ? (1) : (0));
  426. return flag;
  427. }
  428. public static int SplitTest(ref ConvexH convex, Plane plane, float planetestepsilon)
  429. {
  430. int flag = 0;
  431. for (int i = 0; i < convex.vertices.Count; i++)
  432. {
  433. flag |= PlaneTest(plane, convex.vertices[i], planetestepsilon);
  434. }
  435. return flag;
  436. }
  437. public static Quaternion VirtualTrackBall(float3 cop, float3 cor, float3 dir1, float3 dir2)
  438. {
  439. // routine taken from game programming gems.
  440. // Implement track ball functionality to spin stuf on the screen
  441. // cop center of projection
  442. // cor center of rotation
  443. // dir1 old mouse direction
  444. // dir2 new mouse direction
  445. // pretend there is a sphere around cor. Then find the points
  446. // where dir1 and dir2 intersect that sphere. Find the
  447. // rotation that takes the first point to the second.
  448. float m;
  449. // compute plane
  450. float3 nrml = cor - cop;
  451. float fudgefactor = 1.0f / (float3.magnitude(nrml) * 0.25f); // since trackball proportional to distance from cop
  452. nrml = float3.normalize(nrml);
  453. float dist = -float3.dot(nrml, cor);
  454. float3 u = PlaneLineIntersection(new Plane(nrml, dist), cop, cop + dir1);
  455. u = u - cor;
  456. u = u * fudgefactor;
  457. m = float3.magnitude(u);
  458. if (m > 1)
  459. {
  460. u /= m;
  461. }
  462. else
  463. {
  464. u = u - (nrml * (float)Math.Sqrt(1 - m * m));
  465. }
  466. float3 v = PlaneLineIntersection(new Plane(nrml, dist), cop, cop + dir2);
  467. v = v - cor;
  468. v = v * fudgefactor;
  469. m = float3.magnitude(v);
  470. if (m > 1)
  471. {
  472. v /= m;
  473. }
  474. else
  475. {
  476. v = v - (nrml * (float)Math.Sqrt(1 - m * m));
  477. }
  478. return RotationArc(u, v);
  479. }
  480. public static bool AssertIntact(ConvexH convex, float planetestepsilon)
  481. {
  482. int i;
  483. int estart = 0;
  484. for (i = 0; i < convex.edges.Count; i++)
  485. {
  486. if (convex.edges[estart].p != convex.edges[i].p)
  487. {
  488. estart = i;
  489. }
  490. int inext = i + 1;
  491. if (inext >= convex.edges.Count || convex.edges[inext].p != convex.edges[i].p)
  492. {
  493. inext = estart;
  494. }
  495. Debug.Assert(convex.edges[inext].p == convex.edges[i].p);
  496. int nb = convex.edges[i].ea;
  497. Debug.Assert(nb != 255);
  498. if (nb == 255 || nb == -1)
  499. return false;
  500. Debug.Assert(nb != -1);
  501. Debug.Assert(i == convex.edges[nb].ea);
  502. }
  503. for (i = 0; i < convex.edges.Count; i++)
  504. {
  505. Debug.Assert((0) == PlaneTest(convex.facets[convex.edges[i].p], convex.vertices[convex.edges[i].v], planetestepsilon));
  506. if ((0) != PlaneTest(convex.facets[convex.edges[i].p], convex.vertices[convex.edges[i].v], planetestepsilon))
  507. return false;
  508. if (convex.edges[estart].p != convex.edges[i].p)
  509. {
  510. estart = i;
  511. }
  512. int i1 = i + 1;
  513. if (i1 >= convex.edges.Count || convex.edges[i1].p != convex.edges[i].p)
  514. {
  515. i1 = estart;
  516. }
  517. int i2 = i1 + 1;
  518. if (i2 >= convex.edges.Count || convex.edges[i2].p != convex.edges[i].p)
  519. {
  520. i2 = estart;
  521. }
  522. if (i == i2) // i sliced tangent to an edge and created 2 meaningless edges
  523. continue;
  524. float3 localnormal = TriNormal(convex.vertices[convex.edges[i].v], convex.vertices[convex.edges[i1].v], convex.vertices[convex.edges[i2].v]);
  525. Debug.Assert(float3.dot(localnormal, convex.facets[convex.edges[i].p].normal) > 0);
  526. if (float3.dot(localnormal, convex.facets[convex.edges[i].p].normal) <= 0)
  527. return false;
  528. }
  529. return true;
  530. }
  531. public static ConvexH test_btbq(float planetestepsilon)
  532. {
  533. // back to back quads
  534. ConvexH convex = new ConvexH(4, 8, 2);
  535. convex.vertices[0] = new float3(0, 0, 0);
  536. convex.vertices[1] = new float3(1, 0, 0);
  537. convex.vertices[2] = new float3(1, 1, 0);
  538. convex.vertices[3] = new float3(0, 1, 0);
  539. convex.facets[0] = new Plane(new float3(0, 0, 1), 0);
  540. convex.facets[1] = new Plane(new float3(0, 0, -1), 0);
  541. convex.edges[0] = new ConvexH.HalfEdge(7, 0, 0);
  542. convex.edges[1] = new ConvexH.HalfEdge(6, 1, 0);
  543. convex.edges[2] = new ConvexH.HalfEdge(5, 2, 0);
  544. convex.edges[3] = new ConvexH.HalfEdge(4, 3, 0);
  545. convex.edges[4] = new ConvexH.HalfEdge(3, 0, 1);
  546. convex.edges[5] = new ConvexH.HalfEdge(2, 3, 1);
  547. convex.edges[6] = new ConvexH.HalfEdge(1, 2, 1);
  548. convex.edges[7] = new ConvexH.HalfEdge(0, 1, 1);
  549. AssertIntact(convex, planetestepsilon);
  550. return convex;
  551. }
  552. public static ConvexH test_cube()
  553. {
  554. ConvexH convex = new ConvexH(8, 24, 6);
  555. convex.vertices[0] = new float3(0, 0, 0);
  556. convex.vertices[1] = new float3(0, 0, 1);
  557. convex.vertices[2] = new float3(0, 1, 0);
  558. convex.vertices[3] = new float3(0, 1, 1);
  559. convex.vertices[4] = new float3(1, 0, 0);
  560. convex.vertices[5] = new float3(1, 0, 1);
  561. convex.vertices[6] = new float3(1, 1, 0);
  562. convex.vertices[7] = new float3(1, 1, 1);
  563. convex.facets[0] = new Plane(new float3(-1, 0, 0), 0);
  564. convex.facets[1] = new Plane(new float3(1, 0, 0), -1);
  565. convex.facets[2] = new Plane(new float3(0, -1, 0), 0);
  566. convex.facets[3] = new Plane(new float3(0, 1, 0), -1);
  567. convex.facets[4] = new Plane(new float3(0, 0, -1), 0);
  568. convex.facets[5] = new Plane(new float3(0, 0, 1), -1);
  569. convex.edges[0] = new ConvexH.HalfEdge(11, 0, 0);
  570. convex.edges[1] = new ConvexH.HalfEdge(23, 1, 0);
  571. convex.edges[2] = new ConvexH.HalfEdge(15, 3, 0);
  572. convex.edges[3] = new ConvexH.HalfEdge(16, 2, 0);
  573. convex.edges[4] = new ConvexH.HalfEdge(13, 6, 1);
  574. convex.edges[5] = new ConvexH.HalfEdge(21, 7, 1);
  575. convex.edges[6] = new ConvexH.HalfEdge(9, 5, 1);
  576. convex.edges[7] = new ConvexH.HalfEdge(18, 4, 1);
  577. convex.edges[8] = new ConvexH.HalfEdge(19, 0, 2);
  578. convex.edges[9] = new ConvexH.HalfEdge(6, 4, 2);
  579. convex.edges[10] = new ConvexH.HalfEdge(20, 5, 2);
  580. convex.edges[11] = new ConvexH.HalfEdge(0, 1, 2);
  581. convex.edges[12] = new ConvexH.HalfEdge(22, 3, 3);
  582. convex.edges[13] = new ConvexH.HalfEdge(4, 7, 3);
  583. convex.edges[14] = new ConvexH.HalfEdge(17, 6, 3);
  584. convex.edges[15] = new ConvexH.HalfEdge(2, 2, 3);
  585. convex.edges[16] = new ConvexH.HalfEdge(3, 0, 4);
  586. convex.edges[17] = new ConvexH.HalfEdge(14, 2, 4);
  587. convex.edges[18] = new ConvexH.HalfEdge(7, 6, 4);
  588. convex.edges[19] = new ConvexH.HalfEdge(8, 4, 4);
  589. convex.edges[20] = new ConvexH.HalfEdge(10, 1, 5);
  590. convex.edges[21] = new ConvexH.HalfEdge(5, 5, 5);
  591. convex.edges[22] = new ConvexH.HalfEdge(12, 7, 5);
  592. convex.edges[23] = new ConvexH.HalfEdge(1, 3, 5);
  593. return convex;
  594. }
  595. public static ConvexH ConvexHMakeCube(float3 bmin, float3 bmax)
  596. {
  597. ConvexH convex = test_cube();
  598. convex.vertices[0] = new float3(bmin.x, bmin.y, bmin.z);
  599. convex.vertices[1] = new float3(bmin.x, bmin.y, bmax.z);
  600. convex.vertices[2] = new float3(bmin.x, bmax.y, bmin.z);
  601. convex.vertices[3] = new float3(bmin.x, bmax.y, bmax.z);
  602. convex.vertices[4] = new float3(bmax.x, bmin.y, bmin.z);
  603. convex.vertices[5] = new float3(bmax.x, bmin.y, bmax.z);
  604. convex.vertices[6] = new float3(bmax.x, bmax.y, bmin.z);
  605. convex.vertices[7] = new float3(bmax.x, bmax.y, bmax.z);
  606. convex.facets[0] = new Plane(new float3(-1, 0, 0), bmin.x);
  607. convex.facets[1] = new Plane(new float3(1, 0, 0), -bmax.x);
  608. convex.facets[2] = new Plane(new float3(0, -1, 0), bmin.y);
  609. convex.facets[3] = new Plane(new float3(0, 1, 0), -bmax.y);
  610. convex.facets[4] = new Plane(new float3(0, 0, -1), bmin.z);
  611. convex.facets[5] = new Plane(new float3(0, 0, 1), -bmax.z);
  612. return convex;
  613. }
  614. public static ConvexH ConvexHCrop(ref ConvexH convex, Plane slice, float planetestepsilon)
  615. {
  616. int i;
  617. int vertcountunder = 0;
  618. int vertcountover = 0;
  619. List<int> vertscoplanar = new List<int>(); // existing vertex members of convex that are coplanar
  620. List<int> edgesplit = new List<int>(); // existing edges that members of convex that cross the splitplane
  621. Debug.Assert(convex.edges.Count < 480);
  622. EdgeFlag[] edgeflag = new EdgeFlag[512];
  623. VertFlag[] vertflag = new VertFlag[256];
  624. PlaneFlag[] planeflag = new PlaneFlag[128];
  625. ConvexH.HalfEdge[] tmpunderedges = new ConvexH.HalfEdge[512];
  626. Plane[] tmpunderplanes = new Plane[128];
  627. Coplanar[] coplanaredges = new Coplanar[512];
  628. int coplanaredges_num = 0;
  629. List<float3> createdverts = new List<float3>();
  630. // do the side-of-plane tests
  631. for (i = 0; i < convex.vertices.Count; i++)
  632. {
  633. vertflag[i].planetest = (byte)PlaneTest(slice, convex.vertices[i], planetestepsilon);
  634. if (vertflag[i].planetest == (0))
  635. {
  636. // ? vertscoplanar.Add(i);
  637. vertflag[i].undermap = (byte)vertcountunder++;
  638. vertflag[i].overmap = (byte)vertcountover++;
  639. }
  640. else if (vertflag[i].planetest == (1))
  641. {
  642. vertflag[i].undermap = (byte)vertcountunder++;
  643. }
  644. else
  645. {
  646. Debug.Assert(vertflag[i].planetest == (2));
  647. vertflag[i].overmap = (byte)vertcountover++;
  648. vertflag[i].undermap = 255; // for debugging purposes
  649. }
  650. }
  651. int vertcountunderold = vertcountunder; // for debugging only
  652. int under_edge_count = 0;
  653. int underplanescount = 0;
  654. int e0 = 0;
  655. for (int currentplane = 0; currentplane < convex.facets.Count; currentplane++)
  656. {
  657. int estart = e0;
  658. int enextface = 0;
  659. int planeside = 0;
  660. int e1 = e0 + 1;
  661. int vout = -1;
  662. int vin = -1;
  663. int coplanaredge = -1;
  664. do
  665. {
  666. if (e1 >= convex.edges.Count || convex.edges[e1].p != currentplane)
  667. {
  668. enextface = e1;
  669. e1 = estart;
  670. }
  671. ConvexH.HalfEdge edge0 = convex.edges[e0];
  672. ConvexH.HalfEdge edge1 = convex.edges[e1];
  673. ConvexH.HalfEdge edgea = convex.edges[edge0.ea];
  674. planeside |= vertflag[edge0.v].planetest;
  675. //if((vertflag[edge0.v].planetest & vertflag[edge1.v].planetest) == COPLANAR) {
  676. // assert(ecop==-1);
  677. // ecop=e;
  678. //}
  679. if (vertflag[edge0.v].planetest == (2) && vertflag[edge1.v].planetest == (2))
  680. {
  681. // both endpoints over plane
  682. edgeflag[e0].undermap = -1;
  683. }
  684. else if ((vertflag[edge0.v].planetest | vertflag[edge1.v].planetest) == (1))
  685. {
  686. // at least one endpoint under, the other coplanar or under
  687. edgeflag[e0].undermap = (short)under_edge_count;
  688. tmpunderedges[under_edge_count].v = vertflag[edge0.v].undermap;
  689. tmpunderedges[under_edge_count].p = (byte)underplanescount;
  690. if (edge0.ea < e0)
  691. {
  692. // connect the neighbors
  693. Debug.Assert(edgeflag[edge0.ea].undermap != -1);
  694. tmpunderedges[under_edge_count].ea = edgeflag[edge0.ea].undermap;
  695. tmpunderedges[edgeflag[edge0.ea].undermap].ea = (short)under_edge_count;
  696. }
  697. under_edge_count++;
  698. }
  699. else if ((vertflag[edge0.v].planetest | vertflag[edge1.v].planetest) == (0))
  700. {
  701. // both endpoints coplanar
  702. // must check a 3rd point to see if UNDER
  703. int e2 = e1 + 1;
  704. if (e2 >= convex.edges.Count || convex.edges[e2].p != currentplane)
  705. {
  706. e2 = estart;
  707. }
  708. Debug.Assert(convex.edges[e2].p == currentplane);
  709. ConvexH.HalfEdge edge2 = convex.edges[e2];
  710. if (vertflag[edge2.v].planetest == (1))
  711. {
  712. edgeflag[e0].undermap = (short)under_edge_count;
  713. tmpunderedges[under_edge_count].v = vertflag[edge0.v].undermap;
  714. tmpunderedges[under_edge_count].p = (byte)underplanescount;
  715. tmpunderedges[under_edge_count].ea = -1;
  716. // make sure this edge is added to the "coplanar" list
  717. coplanaredge = under_edge_count;
  718. vout = vertflag[edge0.v].undermap;
  719. vin = vertflag[edge1.v].undermap;
  720. under_edge_count++;
  721. }
  722. else
  723. {
  724. edgeflag[e0].undermap = -1;
  725. }
  726. }
  727. else if (vertflag[edge0.v].planetest == (1) && vertflag[edge1.v].planetest == (2))
  728. {
  729. // first is under 2nd is over
  730. edgeflag[e0].undermap = (short)under_edge_count;
  731. tmpunderedges[under_edge_count].v = vertflag[edge0.v].undermap;
  732. tmpunderedges[under_edge_count].p = (byte)underplanescount;
  733. if (edge0.ea < e0)
  734. {
  735. Debug.Assert(edgeflag[edge0.ea].undermap != -1);
  736. // connect the neighbors
  737. tmpunderedges[under_edge_count].ea = edgeflag[edge0.ea].undermap;
  738. tmpunderedges[edgeflag[edge0.ea].undermap].ea = (short)under_edge_count;
  739. vout = tmpunderedges[edgeflag[edge0.ea].undermap].v;
  740. }
  741. else
  742. {
  743. Plane p0 = convex.facets[edge0.p];
  744. Plane pa = convex.facets[edgea.p];
  745. createdverts.Add(ThreePlaneIntersection(p0, pa, slice));
  746. //createdverts.Add(PlaneProject(slice,PlaneLineIntersection(slice,convex.vertices[edge0.v],convex.vertices[edgea.v])));
  747. //createdverts.Add(PlaneLineIntersection(slice,convex.vertices[edge0.v],convex.vertices[edgea.v]));
  748. vout = vertcountunder++;
  749. }
  750. under_edge_count++;
  751. /// hmmm something to think about: i might be able to output this edge regarless of
  752. // wheter or not we know v-in yet. ok i;ll try this now:
  753. tmpunderedges[under_edge_count].v = (byte)vout;
  754. tmpunderedges[under_edge_count].p = (byte)underplanescount;
  755. tmpunderedges[under_edge_count].ea = -1;
  756. coplanaredge = under_edge_count;
  757. under_edge_count++;
  758. if (vin != -1)
  759. {
  760. // we previously processed an edge where we came under
  761. // now we know about vout as well
  762. // ADD THIS EDGE TO THE LIST OF EDGES THAT NEED NEIGHBOR ON PARTITION PLANE!!
  763. }
  764. }
  765. else if (vertflag[edge0.v].planetest == (0) && vertflag[edge1.v].planetest == (2))
  766. {
  767. // first is coplanar 2nd is over
  768. edgeflag[e0].undermap = -1;
  769. vout = vertflag[edge0.v].undermap;
  770. // I hate this but i have to make sure part of this face is UNDER before ouputting this vert
  771. int k = estart;
  772. Debug.Assert(edge0.p == currentplane);
  773. while (!((planeside & 1) != 0) && k < convex.edges.Count && convex.edges[k].p == edge0.p)
  774. {
  775. planeside |= vertflag[convex.edges[k].v].planetest;
  776. k++;
  777. }
  778. if ((planeside & 1) != 0)
  779. {
  780. tmpunderedges[under_edge_count].v = (byte)vout;
  781. tmpunderedges[under_edge_count].p = (byte)underplanescount;
  782. tmpunderedges[under_edge_count].ea = -1;
  783. coplanaredge = under_edge_count; // hmmm should make a note of the edge # for later on
  784. under_edge_count++;
  785. }
  786. }
  787. else if (vertflag[edge0.v].planetest == (2) && vertflag[edge1.v].planetest == (1))
  788. {
  789. // first is over next is under
  790. // new vertex!!!
  791. Debug.Assert(vin == -1);
  792. if (e0 < edge0.ea)
  793. {
  794. Plane p0 = convex.facets[edge0.p];
  795. Plane pa = convex.facets[edgea.p];
  796. createdverts.Add(ThreePlaneIntersection(p0, pa, slice));
  797. //createdverts.Add(PlaneLineIntersection(slice,convex.vertices[edge0.v],convex.vertices[edgea.v]));
  798. //createdverts.Add(PlaneProject(slice,PlaneLineIntersection(slice,convex.vertices[edge0.v],convex.vertices[edgea.v])));
  799. vin = vertcountunder++;
  800. }
  801. else
  802. {
  803. // find the new vertex that was created by edge[edge0.ea]
  804. int nea = edgeflag[edge0.ea].undermap;
  805. Debug.Assert(tmpunderedges[nea].p == tmpunderedges[nea + 1].p);
  806. vin = tmpunderedges[nea + 1].v;
  807. Debug.Assert(vin < vertcountunder);
  808. Debug.Assert(vin >= vertcountunderold); // for debugging only
  809. }
  810. if (vout != -1)
  811. {
  812. // we previously processed an edge where we went over
  813. // now we know vin too
  814. // ADD THIS EDGE TO THE LIST OF EDGES THAT NEED NEIGHBOR ON PARTITION PLANE!!
  815. }
  816. // output edge
  817. tmpunderedges[under_edge_count].v = (byte)vin;
  818. tmpunderedges[under_edge_count].p = (byte)underplanescount;
  819. edgeflag[e0].undermap = (short)under_edge_count;
  820. if (e0 > edge0.ea)
  821. {
  822. Debug.Assert(edgeflag[edge0.ea].undermap != -1);
  823. // connect the neighbors
  824. tmpunderedges[under_edge_count].ea = edgeflag[edge0.ea].undermap;
  825. tmpunderedges[edgeflag[edge0.ea].undermap].ea = (short)under_edge_count;
  826. }
  827. Debug.Assert(edgeflag[e0].undermap == under_edge_count);
  828. under_edge_count++;
  829. }
  830. else if (vertflag[edge0.v].planetest == (2) && vertflag[edge1.v].planetest == (0))
  831. {
  832. // first is over next is coplanar
  833. edgeflag[e0].undermap = -1;
  834. vin = vertflag[edge1.v].undermap;
  835. Debug.Assert(vin != -1);
  836. if (vout != -1)
  837. {
  838. // we previously processed an edge where we came under
  839. // now we know both endpoints
  840. // ADD THIS EDGE TO THE LIST OF EDGES THAT NEED NEIGHBOR ON PARTITION PLANE!!
  841. }
  842. }
  843. else
  844. {
  845. Debug.Assert(false);
  846. }
  847. e0 = e1;
  848. e1++; // do the modulo at the beginning of the loop
  849. } while (e0 != estart);
  850. e0 = enextface;
  851. if ((planeside & 1) != 0)
  852. {
  853. planeflag[currentplane].undermap = (byte)underplanescount;
  854. tmpunderplanes[underplanescount] = convex.facets[currentplane];
  855. underplanescount++;
  856. }
  857. else
  858. {
  859. planeflag[currentplane].undermap = 0;
  860. }
  861. if (vout >= 0 && (planeside & 1) != 0)
  862. {
  863. Debug.Assert(vin >= 0);
  864. Debug.Assert(coplanaredge >= 0);
  865. Debug.Assert(coplanaredge != 511);
  866. coplanaredges[coplanaredges_num].ea = (ushort)coplanaredge;
  867. coplanaredges[coplanaredges_num].v0 = (byte)vin;
  868. coplanaredges[coplanaredges_num].v1 = (byte)vout;
  869. coplanaredges_num++;
  870. }
  871. }
  872. // add the new plane to the mix:
  873. if (coplanaredges_num > 0)
  874. {
  875. tmpunderplanes[underplanescount++] = slice;
  876. }
  877. for (i = 0; i < coplanaredges_num - 1; i++)
  878. {
  879. if (coplanaredges[i].v1 != coplanaredges[i + 1].v0)
  880. {
  881. int j = 0;
  882. for (j = i + 2; j < coplanaredges_num; j++)
  883. {
  884. if (coplanaredges[i].v1 == coplanaredges[j].v0)
  885. {
  886. Coplanar tmp = coplanaredges[i + 1];
  887. coplanaredges[i + 1] = coplanaredges[j];
  888. coplanaredges[j] = tmp;
  889. break;
  890. }
  891. }
  892. if (j >= coplanaredges_num)
  893. {
  894. Debug.Assert(j < coplanaredges_num);
  895. return null;
  896. }
  897. }
  898. }
  899. ConvexH punder = new ConvexH(vertcountunder, under_edge_count + coplanaredges_num, underplanescount);
  900. ConvexH under = punder;
  901. {
  902. int k = 0;
  903. for (i = 0; i < convex.vertices.Count; i++)
  904. {
  905. if (vertflag[i].planetest != (2))
  906. {
  907. under.vertices[k++] = convex.vertices[i];
  908. }
  909. }
  910. i = 0;
  911. while (k < vertcountunder)
  912. {
  913. under.vertices[k++] = createdverts[i++];
  914. }
  915. Debug.Assert(i == createdverts.Count);
  916. }
  917. for (i = 0; i < coplanaredges_num; i++)
  918. {
  919. ConvexH.HalfEdge edge = under.edges[under_edge_count + i];
  920. edge.p = (byte)(underplanescount - 1);
  921. edge.ea = (short)coplanaredges[i].ea;
  922. edge.v = (byte)coplanaredges[i].v0;
  923. under.edges[under_edge_count + i] = edge;
  924. tmpunderedges[coplanaredges[i].ea].ea = (short)(under_edge_count + i);
  925. }
  926. under.edges = new List<ConvexH.HalfEdge>(tmpunderedges);
  927. under.facets = new List<Plane>(tmpunderplanes);
  928. return punder;
  929. }
  930. public static ConvexH ConvexHDup(ConvexH src)
  931. {
  932. ConvexH dst = new ConvexH(src.vertices.Count, src.edges.Count, src.facets.Count);
  933. dst.vertices = new List<float3>(src.vertices.Count);
  934. foreach (float3 f in src.vertices)
  935. dst.vertices.Add(new float3(f));
  936. dst.edges = new List<ConvexH.HalfEdge>(src.edges.Count);
  937. foreach (ConvexH.HalfEdge e in src.edges)
  938. dst.edges.Add(new ConvexH.HalfEdge(e));
  939. dst.facets = new List<Plane>(src.facets.Count);
  940. foreach (Plane p in src.facets)
  941. dst.facets.Add(new Plane(p));
  942. return dst;
  943. }
  944. public static int candidateplane(List<Plane> planes, int planes_count, ConvexH convex, float epsilon)
  945. {
  946. int p = 0;
  947. float md = 0;
  948. int i;
  949. for (i = 0; i < planes_count; i++)
  950. {
  951. float d = 0;
  952. for (int j = 0; j < convex.vertices.Count; j++)
  953. {
  954. d = Math.Max(d, float3.dot(convex.vertices[j], planes[i].normal) + planes[i].dist);
  955. }
  956. if (i == 0 || d > md)
  957. {
  958. p = i;
  959. md = d;
  960. }
  961. }
  962. return (md > epsilon) ? p : -1;
  963. }
  964. public static float3 orth(float3 v)
  965. {
  966. float3 a = float3.cross(v, new float3(0f, 0f, 1f));
  967. float3 b = float3.cross(v, new float3(0f, 1f, 0f));
  968. return float3.normalize((float3.magnitude(a) > float3.magnitude(b)) ? a : b);
  969. }
  970. public static int maxdir(List<float3> p, int count, float3 dir)
  971. {
  972. Debug.Assert(count != 0);
  973. int m = 0;
  974. float currDotm = float3.dot(p[0], dir);
  975. for (int i = 1; i < count; i++)
  976. {
  977. float currDoti = float3.dot(p[i], dir);
  978. if (currDoti > currDotm)
  979. {
  980. currDotm = currDoti;
  981. m = i;
  982. }
  983. }
  984. return m;
  985. }
  986. public static int maxdirfiltered(List<float3> p, int count, float3 dir, byte[] allow)
  987. {
  988. //Debug.Assert(count != 0);
  989. int m = 0;
  990. float currDotm = float3.dot(p[0], dir);
  991. float currDoti;
  992. while (allow[m] == 0)
  993. m++;
  994. for (int i = 1; i < count; i++)
  995. {
  996. if (allow[i] != 0)
  997. {
  998. currDoti = float3.dot(p[i], dir);
  999. if (currDoti > currDotm)
  1000. {
  1001. currDotm = currDoti;
  1002. m = i;
  1003. }
  1004. }
  1005. }
  1006. //Debug.Assert(m != -1);
  1007. return m;
  1008. }
  1009. public static int maxdirsterid(List<float3> p, int count, float3 dir, byte[] allow)
  1010. {
  1011. int m = -1;
  1012. while (m == -1)
  1013. {
  1014. m = maxdirfiltered(p, count, dir, allow);
  1015. if (allow[m] == 3)
  1016. return m;
  1017. float3 u = orth(dir);
  1018. float3 v = float3.cross(u, dir);
  1019. int ma = -1;
  1020. for (float x = 0.0f; x <= 360.0f; x += 45.0f)
  1021. {
  1022. int mb;
  1023. {
  1024. float s = (float)Math.Sin((3.14159264f / 180.0f) * (x));
  1025. float c = (float)Math.Cos((3.14159264f / 180.0f) * (x));
  1026. mb = maxdirfiltered(p, count, dir + (u * s + v * c) * 0.025f, allow);
  1027. }
  1028. if (ma == m && mb == m)
  1029. {
  1030. allow[m] = 3;
  1031. return m;
  1032. }
  1033. if (ma != -1 && ma != mb) // Yuck - this is really ugly
  1034. {
  1035. int mc = ma;
  1036. for (float xx = x - 40.0f; xx <= x; xx += 5.0f)
  1037. {
  1038. float s = (float)Math.Sin((3.14159264f / 180.0f) * (xx));
  1039. float c = (float)Math.Cos((3.14159264f / 180.0f) * (xx));
  1040. int md = maxdirfiltered(p, count, dir + (u * s + v * c) * 0.025f, allow);
  1041. if (mc == m && md == m)
  1042. {
  1043. allow[m] = 3;
  1044. return m;
  1045. }
  1046. mc = md;
  1047. }
  1048. }
  1049. ma = mb;
  1050. }
  1051. allow[m] = 0;
  1052. m = -1;
  1053. }
  1054. Debug.Assert(false);
  1055. return m;
  1056. }
  1057. public static int4 FindSimplex(List<float3> verts, byte[] allow)
  1058. {
  1059. float3[] basis = new float3[3];
  1060. basis[0] = new float3(0.01f, 0.02f, 1.0f);
  1061. int p0 = maxdirsterid(verts, verts.Count, basis[0], allow);
  1062. int p1 = maxdirsterid(verts, verts.Count, -basis[0], allow);
  1063. basis[0] = verts[p0] - verts[p1];
  1064. if (p0 == p1 || basis[0] == new float3(0, 0, 0))
  1065. return new int4(-1, -1, -1, -1);
  1066. basis[1] = float3.cross(new float3(1, 0.02f, 0), basis[0]);
  1067. basis[2] = float3.cross(new float3(-0.02f, 1, 0), basis[0]);
  1068. basis[1] = float3.normalize((float3.magnitude(basis[1]) > float3.magnitude(basis[2])) ? basis[1] : basis[2]);
  1069. int p2 = maxdirsterid(verts, verts.Count, basis[1], allow);
  1070. if (p2 == p0 || p2 == p1)
  1071. {
  1072. p2 = maxdirsterid(verts, verts.Count, -basis[1], allow);
  1073. }
  1074. if (p2 == p0 || p2 == p1)
  1075. return new int4(-1, -1, -1, -1);
  1076. basis[1] = verts[p2] - verts[p0];
  1077. basis[2] = float3.normalize(float3.cross(basis[1], basis[0]));
  1078. int p3 = maxdirsterid(verts, verts.Count, basis[2], allow);
  1079. if (p3 == p0 || p3 == p1 || p3 == p2)
  1080. p3 = maxdirsterid(verts, verts.Count, -basis[2], allow);
  1081. if (p3 == p0 || p3 == p1 || p3 == p2)
  1082. return new int4(-1, -1, -1, -1);
  1083. Debug.Assert(!(p0 == p1 || p0 == p2 || p0 == p3 || p1 == p2 || p1 == p3 || p2 == p3));
  1084. if (float3.dot(verts[p3] - verts[p0], float3.cross(verts[p1] - verts[p0], verts[p2] - verts[p0])) < 0)
  1085. {
  1086. Swap(ref p2, ref p3);
  1087. }
  1088. return new int4(p0, p1, p2, p3);
  1089. }
  1090. public static float GetDist(float px, float py, float pz, float3 p2)
  1091. {
  1092. float dx = px - p2.x;
  1093. float dy = py - p2.y;
  1094. float dz = pz - p2.z;
  1095. return dx * dx + dy * dy + dz * dz;
  1096. }
  1097. public static void ReleaseHull(PHullResult result)
  1098. {
  1099. if (result.Indices != null)
  1100. result.Indices = null;
  1101. if (result.Vertices != null)
  1102. result.Vertices = null;
  1103. }
  1104. public static int calchullgen(List<float3> verts, int vlimit, List<HullTriangle> tris)
  1105. {
  1106. if (verts.Count < 4)
  1107. return 0;
  1108. if (vlimit == 0)
  1109. vlimit = 1000000000;
  1110. int j;
  1111. float3 bmin = new float3(verts[0]);
  1112. float3 bmax = new float3(verts[0]);
  1113. List<int> isextreme = new List<int>(verts.Count);
  1114. byte[] allow = new byte[verts.Count];
  1115. for (j = 0; j < verts.Count; j++)
  1116. {
  1117. allow[j] = 1;
  1118. isextreme.Add(0);
  1119. bmin = float3.VectorMin(bmin, verts[j]);
  1120. bmax = float3.VectorMax(bmax, verts[j]);
  1121. }
  1122. float epsilon = float3.magnitude(bmax - bmin) * 0.001f;
  1123. int4 p = FindSimplex(verts, allow);
  1124. if (p.x == -1) // simplex failed
  1125. return 0;
  1126. float3 center = (verts[p[0]] + verts[p[1]] + verts[p[2]] + verts[p[3]]) / 4.0f; // a valid interior point
  1127. HullTriangle t0 = new HullTriangle(p[2], p[3], p[1], tris);
  1128. t0.n = new int3(2, 3, 1);
  1129. HullTriangle t1 = new HullTriangle(p[3], p[2], p[0], tris);
  1130. t1.n = new int3(3, 2, 0);
  1131. HullTriangle t2 = new HullTriangle(p[0], p[1], p[3], tris);
  1132. t2.n = new int3(0, 1, 3);
  1133. HullTriangle t3 = new HullTriangle(p[1], p[0], p[2], tris);
  1134. t3.n = new int3(1, 0, 2);
  1135. isextreme[p[0]] = isextreme[p[1]] = isextreme[p[2]] = isextreme[p[3]] = 1;
  1136. checkit(t0, tris);
  1137. checkit(t1, tris);
  1138. checkit(t2, tris);
  1139. checkit(t3, tris);
  1140. for (j = 0; j < tris.Count; j++)
  1141. {
  1142. HullTriangle t = tris[j];
  1143. Debug.Assert((object)t != null);
  1144. Debug.Assert(t.vmax < 0);
  1145. float3 n = TriNormal(verts[(t)[0]], verts[(t)[1]], verts[(t)[2]]);
  1146. t.vmax = maxdirsterid(verts, verts.Count, n, allow);
  1147. t.rise = float3.dot(n, verts[t.vmax] - verts[(t)[0]]);
  1148. }
  1149. HullTriangle te;
  1150. vlimit -= 4;
  1151. while (vlimit > 0 && (te = extrudable(epsilon, tris)) != null)
  1152. {
  1153. int3 ti = te;
  1154. int v = te.vmax;
  1155. Debug.Assert(isextreme[v] == 0); // wtf we've already done this vertex
  1156. isextreme[v] = 1;
  1157. //if(v==p0 || v==p1 || v==p2 || v==p3) continue; // done these already
  1158. j = tris.Count;
  1159. while (j-- != 0)
  1160. {
  1161. if (tris.Count <= j || (object)tris[j] == null)
  1162. continue;
  1163. int3 t = tris[j];
  1164. if (above(verts, t, verts[v], 0.01f * epsilon))
  1165. {
  1166. extrude(tris[j], v, tris);
  1167. }
  1168. }
  1169. // now check for those degenerate cases where we have a flipped triangle or a really skinny triangle
  1170. j = tris.Count;
  1171. while (j-- != 0)
  1172. {
  1173. if (tris.Count <= j || (object)tris[j] == null)
  1174. continue;
  1175. if (!hasvert(tris[j], v))
  1176. break;
  1177. int3 nt = tris[j];
  1178. if (above(verts, nt, center, 0.01f * epsilon) || float3.magnitude(float3.cross(verts[nt[1]] - verts[nt[0]], verts[nt[2]] - verts[nt[1]])) < epsilon * epsilon * 0.1f)
  1179. {
  1180. HullTriangle nb = tris[tris[j].n[0]];
  1181. Debug.Assert(nb != null);
  1182. Debug.Assert(!hasvert(nb, v));
  1183. Debug.Assert(nb.id < j);
  1184. extrude(nb, v, tris);
  1185. j = tris.Count;
  1186. }
  1187. }
  1188. j = tris.Count;
  1189. while (j-- != 0)
  1190. {
  1191. HullTriangle t = tris[j];
  1192. if (t == null)
  1193. continue;
  1194. if (t.vmax >= 0)
  1195. break;
  1196. float3 n = TriNormal(verts[(t)[0]], verts[(t)[1]], verts[(t)[2]]);
  1197. t.vmax = maxdirsterid(verts, verts.Count, n, allow);
  1198. if (isextreme[t.vmax] != 0)
  1199. {
  1200. t.vmax = -1; // already done that vertex - algorithm needs to be able to terminate.
  1201. }
  1202. else
  1203. {
  1204. t.rise = float3.dot(n, verts[t.vmax] - verts[(t)[0]]);
  1205. }
  1206. }
  1207. vlimit--;
  1208. }
  1209. return 1;
  1210. }
  1211. public static bool calchull(List<float3> verts, out List<int> tris_out, int vlimit, List<HullTriangle> tris)
  1212. {
  1213. tris_out = null;
  1214. int rc = calchullgen(verts, vlimit, tris);
  1215. if (rc == 0)
  1216. return false;
  1217. List<int> ts = new List<int>();
  1218. for (int i = 0; i < tris.Count; i++)
  1219. {
  1220. if ((object)tris[i] != null)
  1221. {
  1222. for (int j = 0; j < 3; j++)
  1223. ts.Add((tris[i])[j]);
  1224. tris[i] = null;
  1225. }
  1226. }
  1227. tris_out = ts;
  1228. tris.Clear();
  1229. return true;
  1230. }
  1231. public static int calchullpbev(List<float3> verts, int vlimit, out List<Plane> planes, float bevangle, List<HullTriangle> tris)
  1232. {
  1233. int i;
  1234. int j;
  1235. planes = new List<Plane>();
  1236. int rc = calchullgen(verts, vlimit, tris);
  1237. if (rc == 0)
  1238. return 0;
  1239. for (i = 0; i < tris.Count; i++)
  1240. {
  1241. if (tris[i] != null)
  1242. {
  1243. Plane p = new Plane();
  1244. HullTriangle t = tris[i];
  1245. p.normal = TriNormal(verts[(t)[0]], verts[(t)[1]], verts[(t)[2]]);
  1246. p.dist = -float3.dot(p.normal, verts[(t)[0]]);
  1247. planes.Add(p);
  1248. for (j = 0; j < 3; j++)
  1249. {
  1250. if (t.n[j] < t.id)
  1251. continue;
  1252. HullTriangle s = tris[t.n[j]];
  1253. float3 snormal = TriNormal(verts[(s)[0]], verts[(s)[1]], verts[(s)[2]]);
  1254. if (float3.dot(snormal, p.normal) >= Math.Cos(bevangle * (3.14159264f / 180.0f)))
  1255. continue;
  1256. float3 n = float3.normalize(snormal + p.normal);
  1257. planes.Add(new Plane(n, -float3.dot(n, verts[maxdir(verts, verts.Count, n)])));
  1258. }
  1259. }
  1260. }
  1261. tris.Clear();
  1262. return 1;
  1263. }
  1264. public static int overhull(List<Plane> planes, List<float3> verts, int maxplanes, out List<float3> verts_out, out List<int> faces_out, float inflate)
  1265. {
  1266. verts_out = null;
  1267. faces_out = null;
  1268. int i;
  1269. int j;
  1270. if (verts.Count < 4)
  1271. return 0;
  1272. maxplanes = Math.Min(maxplanes, planes.Count);
  1273. float3 bmin = new float3(verts[0]);
  1274. float3 bmax = new float3(verts[0]);
  1275. for (i = 0; i < verts.Count; i++)
  1276. {
  1277. bmin = float3.VectorMin(bmin, verts[i]);
  1278. bmax = float3.VectorMax(bmax, verts[i]);
  1279. }
  1280. // float diameter = magnitude(bmax-bmin);
  1281. // inflate *=diameter; // RELATIVE INFLATION
  1282. bmin -= new float3(inflate, inflate, inflate);
  1283. bmax += new float3(inflate, inflate, inflate);
  1284. for (i = 0; i < planes.Count; i++)
  1285. {
  1286. planes[i].dist -= inflate;
  1287. }
  1288. float3 emin = new float3(bmin);
  1289. float3 emax = new float3(bmax);
  1290. float epsilon = float3.magnitude(emax - emin) * 0.025f;
  1291. float planetestepsilon = float3.magnitude(emax - emin) * (0.001f);
  1292. // todo: add bounding cube planes to force bevel. or try instead not adding the diameter expansion ??? must think.
  1293. // ConvexH *convex = ConvexHMakeCube(bmin - float3(diameter,diameter,diameter),bmax+float3(diameter,diameter,diameter));
  1294. ConvexH c = ConvexHMakeCube(new float3(bmin), new float3(bmax));
  1295. int k;
  1296. while (maxplanes-- != 0 && (k = candidateplane(planes, planes.Count, c, epsilon)) >= 0)
  1297. {
  1298. ConvexH tmp = c;
  1299. c = ConvexHCrop(ref tmp, planes[k], planetestepsilon);
  1300. if (c == null) // might want to debug this case better!!!
  1301. {
  1302. c = tmp;
  1303. break;
  1304. }
  1305. if (AssertIntact(c, planetestepsilon) == false) // might want to debug this case better too!!!
  1306. {
  1307. c = tmp;
  1308. break;
  1309. }
  1310. tmp.edges = null;
  1311. tmp.facets = null;
  1312. tmp.vertices = null;
  1313. }
  1314. Debug.Assert(AssertIntact(c, planetestepsilon));
  1315. //return c;
  1316. //C++ TO C# CONVERTER TODO TASK: The memory management function 'malloc' has no equivalent in C#:
  1317. faces_out = new List<int>(); //(int)malloc(sizeof(int) * (1 + c.facets.Count + c.edges.Count)); // new int[1+c->facets.count+c->edges.count];
  1318. int faces_count_out = 0;
  1319. i = 0;
  1320. faces_out[faces_count_out++] = -1;
  1321. k = 0;
  1322. while (i < c.edges.Count)
  1323. {
  1324. j = 1;
  1325. while (j + i < c.edges.Count && c.edges[i].p == c.edges[i + j].p)
  1326. {
  1327. j++;
  1328. }
  1329. faces_out[faces_count_out++] = j;
  1330. while (j-- != 0)
  1331. {
  1332. faces_out[faces_count_out++] = c.edges[i].v;
  1333. i++;
  1334. }
  1335. k++;
  1336. }
  1337. faces_out[0] = k; // number of faces.
  1338. Debug.Assert(k == c.facets.Count);
  1339. Debug.Assert(faces_count_out == 1 + c.facets.Count + c.edges.Count);
  1340. verts_out = c.vertices; // new float3[c->vertices.count];
  1341. int verts_count_out = c.vertices.Count;
  1342. for (i = 0; i < c.vertices.Count; i++)
  1343. {
  1344. verts_out[i] = new float3(c.vertices[i]);
  1345. }
  1346. c.edges = null;
  1347. c.facets = null;
  1348. c.vertices = null;
  1349. return 1;
  1350. }
  1351. public static int overhullv(List<float3> verts, int maxplanes, out List<float3> verts_out, out List<int> faces_out, float inflate, float bevangle, int vlimit, List<HullTriangle> tris)
  1352. {
  1353. verts_out = null;
  1354. faces_out = null;
  1355. if (verts.Count == 0)
  1356. return 0;
  1357. List<Plane> planes = new List<Plane>();
  1358. int rc = calchullpbev(verts, vlimit, out planes, bevangle, tris);
  1359. if (rc == 0)
  1360. return 0;
  1361. return overhull(planes, verts, maxplanes, out verts_out, out faces_out, inflate);
  1362. }
  1363. public static void addPoint(ref uint vcount, List<float3> p, float x, float y, float z)
  1364. {
  1365. p.Add(new float3(x, y, z));
  1366. vcount++;
  1367. }
  1368. public static bool ComputeHull(List<float3> vertices, ref PHullResult result, int vlimit, float inflate)
  1369. {
  1370. List<HullTriangle> tris = new List<HullTriangle>();
  1371. List<int> faces;
  1372. List<float3> verts_out;
  1373. if (inflate == 0.0f)
  1374. {
  1375. List<int> tris_out;
  1376. bool ret = calchull(vertices, out tris_out, vlimit, tris);
  1377. if (ret == false)
  1378. return false;
  1379. result.Indices = tris_out;
  1380. result.Vertices = vertices;
  1381. return true;
  1382. }
  1383. else
  1384. {
  1385. int ret = overhullv(vertices, 35, out verts_out, out faces, inflate, 120.0f, vlimit, tris);
  1386. if (ret == 0)
  1387. return false;
  1388. List<int3> tris2 = new List<int3>();
  1389. int n = faces[0];
  1390. int k = 1;
  1391. for (int i = 0; i < n; i++)
  1392. {
  1393. int pn = faces[k++];
  1394. for (int j = 2; j < pn; j++)
  1395. tris2.Add(new int3(faces[k], faces[k + j - 1], faces[k + j]));
  1396. k += pn;
  1397. }
  1398. Debug.Assert(tris2.Count == faces.Count - 1 - (n * 3));
  1399. result.Indices = new List<int>(tris2.Count * 3);
  1400. for (int i = 0; i < tris2.Count; i++)
  1401. {
  1402. result.Indices.Add(tris2[i].x);
  1403. result.Indices.Add(tris2[i].y);
  1404. result.Indices.Add(tris2[i].z);
  1405. }
  1406. result.Vertices = verts_out;
  1407. return true;
  1408. }
  1409. }
  1410. private static bool CleanupVertices(List<float3> svertices, out List<float3> vertices, float normalepsilon, out float3 scale)
  1411. {
  1412. const float EPSILON = 0.000001f;
  1413. vertices = new List<float3>();
  1414. scale = new float3(1f, 1f, 1f);
  1415. if (svertices.Count == 0)
  1416. return false;
  1417. uint vcount = 0;
  1418. float[] recip = new float[3];
  1419. float[] bmin = { Single.MaxValue, Single.MaxValue, Single.MaxValue };
  1420. float[] bmax = { Single.MinValue, Single.MinValue, Single.MinValue };
  1421. for (int i = 0; i < svertices.Count; i++)
  1422. {
  1423. float3 p = svertices[i];
  1424. for (int j = 0; j < 3; j++)
  1425. {
  1426. if (p[j] < bmin[j])
  1427. bmin[j] = p[j];
  1428. if (p[j] > bmax[j])
  1429. bmax[j] = p[j];
  1430. }
  1431. }
  1432. float dx = bmax[0] - bmin[0];
  1433. float dy = bmax[1] - bmin[1];
  1434. float dz = bmax[2] - bmin[2];
  1435. float3 center = new float3();
  1436. center.x = dx * 0.5f + bmin[0];
  1437. center.y = dy * 0.5f + bmin[1];
  1438. center.z = dz * 0.5f + bmin[2];
  1439. if (dx < EPSILON || dy < EPSILON || dz < EPSILON || svertices.Count < 3)
  1440. {
  1441. float len = Single.MaxValue;
  1442. if (dx > EPSILON && dx < len)
  1443. len = dx;
  1444. if (dy > EPSILON && dy < len)
  1445. len = dy;
  1446. if (dz > EPSILON && dz < len)
  1447. len = dz;
  1448. if (len == Single.MaxValue)
  1449. {
  1450. dx = dy = dz = 0.01f; // one centimeter
  1451. }
  1452. else
  1453. {
  1454. if (dx < EPSILON) // 1/5th the shortest non-zero edge.
  1455. dx = len * 0.05f;
  1456. if (dy < EPSILON)
  1457. dy = len * 0.05f;
  1458. if (dz < EPSILON)
  1459. dz = len * 0.05f;
  1460. }
  1461. float x1 = center[0] - dx;
  1462. float x2 = center[0] + dx;
  1463. float y1 = center[1] - dy;
  1464. float y2 = center[1] + dy;
  1465. float z1 = center[2] - dz;
  1466. float z2 = center[2] + dz;
  1467. addPoint(ref vcount, vertices, x1, y1, z1);
  1468. addPoint(ref vcount, vertices, x2, y1, z1);
  1469. addPoint(ref vcount, vertices, x2, y2, z1);
  1470. addPoint(ref vcount, vertices, x1, y2, z1);
  1471. addPoint(ref vcount, vertices, x1, y1, z2);
  1472. addPoint(ref vcount, vertices, x2, y1, z2);
  1473. addPoint(ref vcount, vertices, x2, y2, z2);
  1474. addPoint(ref vcount, vertices, x1, y2, z2);
  1475. return true; // return cube
  1476. }
  1477. else
  1478. {
  1479. scale.x = dx;
  1480. scale.y = dy;
  1481. scale.z = dz;
  1482. recip[0] = 1f / dx;
  1483. recip[1] = 1f / dy;
  1484. recip[2] = 1f / dz;
  1485. center.x *= recip[0];
  1486. center.y *= recip[1];
  1487. center.z *= recip[2];
  1488. }
  1489. for (int i = 0; i < svertices.Count; i++)
  1490. {
  1491. float3 p = svertices[i];
  1492. float px = p[0];
  1493. float py = p[1];
  1494. float pz = p[2];
  1495. px = px * recip[0]; // normalize
  1496. py = py * recip[1]; // normalize
  1497. pz = pz * recip[2]; // normalize
  1498. if (true)
  1499. {
  1500. int j;
  1501. for (j = 0; j < vcount; j++)
  1502. {
  1503. float3 v = vertices[j];
  1504. float x = v[0];
  1505. float y = v[1];
  1506. float z = v[2];
  1507. float dx1 = Math.Abs(x - px);
  1508. float dy1 = Math.Abs(y - py);
  1509. float dz1 = Math.Abs(z - pz);
  1510. if (dx1 < normalepsilon && dy1 < normalepsilon && dz1 < normalepsilon)
  1511. {
  1512. // ok, it is close enough to the old one
  1513. // now let us see if it is further from the center of the point cloud than the one we already recorded.
  1514. // in which case we keep this one instead.
  1515. float dist1 = GetDist(px, py, pz, center);
  1516. float dist2 = GetDist(v[0], v[1], v[2], center);
  1517. if (dist1 > dist2)
  1518. {
  1519. v.x = px;
  1520. v.y = py;
  1521. v.z = pz;
  1522. }
  1523. break;
  1524. }
  1525. }
  1526. if (j == vcount)
  1527. {
  1528. float3 dest = new float3(px, py, pz);
  1529. vertices.Add(dest);
  1530. vcount++;
  1531. }
  1532. }
  1533. }
  1534. // ok..now make sure we didn't prune so many vertices it is now invalid.
  1535. if (true)
  1536. {
  1537. float[] bmin2 = { Single.MaxValue, Single.MaxValue, Single.MaxValue };
  1538. float[] bmax2 = { Single.MinValue, Single.MinValue, Single.MinValue };
  1539. for (int i = 0; i < vcount; i++)
  1540. {
  1541. float3 p = vertices[i];
  1542. for (int j = 0; j < 3; j++)
  1543. {
  1544. if (p[j] < bmin2[j])
  1545. bmin2[j] = p[j];
  1546. if (p[j] > bmax2[j])
  1547. bmax2[j] = p[j];
  1548. }
  1549. }
  1550. float dx2 = bmax2[0] - bmin2[0];
  1551. float dy2 = bmax2[1] - bmin2[1];
  1552. float dz2 = bmax2[2] - bmin2[2];
  1553. if (dx2 < EPSILON || dy2 < EPSILON || dz2 < EPSILON || vcount < 3)
  1554. {
  1555. float cx = dx2 * 0.5f + bmin2[0];
  1556. float cy = dy2 * 0.5f + bmin2[1];
  1557. float cz = dz2 * 0.5f + bmin2[2];
  1558. float len = Single.MaxValue;
  1559. if (dx2 >= EPSILON && dx2 < len)
  1560. len = dx2;
  1561. if (dy2 >= EPSILON && dy2 < len)
  1562. len = dy2;
  1563. if (dz2 >= EPSILON && dz2 < len)
  1564. len = dz2;
  1565. if (len == Single.MaxValue)
  1566. {
  1567. dx2 = dy2 = dz2 = 0.01f; // one centimeter
  1568. }
  1569. else
  1570. {
  1571. if (dx2 < EPSILON) // 1/5th the shortest non-zero edge.
  1572. dx2 = len * 0.05f;
  1573. if (dy2 < EPSILON)
  1574. dy2 = len * 0.05f;
  1575. if (dz2 < EPSILON)
  1576. dz2 = len * 0.05f;
  1577. }
  1578. float x1 = cx - dx2;
  1579. float x2 = cx + dx2;
  1580. float y1 = cy - dy2;
  1581. float y2 = cy + dy2;
  1582. float z1 = cz - dz2;
  1583. float z2 = cz + dz2;
  1584. vcount = 0; // add box
  1585. addPoint(ref vcount, vertices, x1, y1, z1);
  1586. addPoint(ref vcount, vertices, x2, y1, z1);
  1587. addPoint(ref vcount, vertices, x2, y2, z1);
  1588. addPoint(ref vcount, vertices, x1, y2, z1);
  1589. addPoint(ref vcount, vertices, x1, y1, z2);
  1590. addPoint(ref vcount, vertices, x2, y1, z2);
  1591. addPoint(ref vcount, vertices, x2, y2, z2);
  1592. addPoint(ref vcount, vertices, x1, y2, z2);
  1593. return true;
  1594. }
  1595. }
  1596. return true;
  1597. }
  1598. private static void BringOutYourDead(List<float3> verts, out List<float3> overts, List<int> indices)
  1599. {
  1600. int[] used = new int[verts.Count];
  1601. int ocount = 0;
  1602. overts = new List<float3>();
  1603. for (int i = 0; i < indices.Count; i++)
  1604. {
  1605. int v = indices[i]; // original array index
  1606. Debug.Assert(v >= 0 && v < verts.Count);
  1607. if (used[v] != 0) // if already remapped
  1608. {
  1609. indices[i] = used[v] - 1; // index to new array
  1610. }
  1611. else
  1612. {
  1613. indices[i] = ocount; // new index mapping
  1614. overts.Add(verts[v]); // copy old vert to new vert array
  1615. ocount++; // increment output vert count
  1616. Debug.Assert(ocount >= 0 && ocount <= verts.Count);
  1617. used[v] = ocount; // assign new index remapping
  1618. }
  1619. }
  1620. }
  1621. public static HullError CreateConvexHull(HullDesc desc, ref HullResult result)
  1622. {
  1623. HullError ret = HullError.QE_FAIL;
  1624. PHullResult hr = new PHullResult();
  1625. uint vcount = (uint)desc.Vertices.Count;
  1626. if (vcount < 8)
  1627. vcount = 8;
  1628. List<float3> vsource;
  1629. float3 scale = new float3();
  1630. bool ok = CleanupVertices(desc.Vertices, out vsource, desc.NormalEpsilon, out scale); // normalize point cloud, remove duplicates!
  1631. if (ok)
  1632. {
  1633. if (true) // scale vertices back to their original size.
  1634. {
  1635. for (int i = 0; i < vsource.Count; i++)
  1636. {
  1637. float3 v = vsource[i];
  1638. v.x *= scale[0];
  1639. v.y *= scale[1];
  1640. v.z *= scale[2];
  1641. }
  1642. }
  1643. float skinwidth = 0;
  1644. if (desc.HasHullFlag(HullFlag.QF_SKIN_WIDTH))
  1645. skinwidth = desc.SkinWidth;
  1646. ok = ComputeHull(vsource, ref hr, (int)desc.MaxVertices, skinwidth);
  1647. if (ok)
  1648. {
  1649. List<float3> vscratch;
  1650. BringOutYourDead(hr.Vertices, out vscratch, hr.Indices);
  1651. ret = HullError.QE_OK;
  1652. if (desc.HasHullFlag(HullFlag.QF_TRIANGLES)) // if he wants the results as triangle!
  1653. {
  1654. result.Polygons = false;
  1655. result.Indices = hr.Indices;
  1656. result.OutputVertices = vscratch;
  1657. }
  1658. else
  1659. {
  1660. result.Polygons = true;
  1661. result.OutputVertices = vscratch;
  1662. if (true)
  1663. {
  1664. List<int> source = hr.Indices;
  1665. List<int> dest = new List<int>();
  1666. for (int i = 0; i < hr.Indices.Count / 3; i++)
  1667. {
  1668. dest.Add(3);
  1669. dest.Add(source[i * 3 + 0]);
  1670. dest.Add(source[i * 3 + 1]);
  1671. dest.Add(source[i * 3 + 2]);
  1672. }
  1673. result.Indices = dest;
  1674. }
  1675. }
  1676. }
  1677. }
  1678. return ret;
  1679. }
  1680. }
  1681. }