cd_vector.h 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192
  1. #ifndef CD_VECTOR_H
  2. #define CD_VECTOR_H
  3. /*----------------------------------------------------------------------
  4. Copyright (c) 2004 Open Dynamics Framework Group
  5. www.physicstools.org
  6. All rights reserved.
  7. Redistribution and use in source and binary forms, with or without modification, are permitted provided
  8. that the following conditions are met:
  9. Redistributions of source code must retain the above copyright notice, this list of conditions
  10. and the following disclaimer.
  11. Redistributions in binary form must reproduce the above copyright notice,
  12. this list of conditions and the following disclaimer in the documentation
  13. and/or other materials provided with the distribution.
  14. Neither the name of the Open Dynamics Framework Group nor the names of its contributors may
  15. be used to endorse or promote products derived from this software without specific prior written permission.
  16. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES,
  17. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. DISCLAIMED. IN NO EVENT SHALL THE INTEL OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  19. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  20. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  21. IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  22. THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. -----------------------------------------------------------------------*/
  24. // http://codesuppository.blogspot.com
  25. //
  26. // mailto: [email protected]
  27. //
  28. // http://www.amillionpixels.us
  29. //
  30. #pragma warning(disable : 4786)
  31. #include <math.h>
  32. #include <float.h>
  33. #include <vector>
  34. namespace ConvexDecomposition
  35. {
  36. const float DEG_TO_RAD = ((2.0f * 3.14152654f) / 360.0f);
  37. const float RAD_TO_DEG = (360.0f / (2.0f * 3.141592654f));
  38. class Vector3d
  39. {
  40. public:
  41. Vector3d(void){}; // null constructor, does not inialize point.
  42. Vector3d(const Vector3d &a) // constructor copies existing vector.
  43. {
  44. x = a.x;
  45. y = a.y;
  46. z = a.z;
  47. };
  48. Vector3d(float a, float b, float c) // construct with initial point.
  49. {
  50. x = a;
  51. y = b;
  52. z = c;
  53. };
  54. Vector3d(const float *t)
  55. {
  56. x = t[0];
  57. y = t[1];
  58. z = t[2];
  59. };
  60. Vector3d(const int *t)
  61. {
  62. x = t[0];
  63. y = t[1];
  64. z = t[2];
  65. };
  66. bool operator==(const Vector3d &a) const
  67. {
  68. return (a.x == x && a.y == y && a.z == z);
  69. };
  70. bool operator!=(const Vector3d &a) const
  71. {
  72. return (a.x != x || a.y != y || a.z != z);
  73. };
  74. // Operators
  75. Vector3d &operator=(const Vector3d &A) // ASSIGNMENT (=)
  76. {
  77. x = A.x;
  78. y = A.y;
  79. z = A.z;
  80. return (*this);
  81. };
  82. Vector3d operator+(const Vector3d &A) const // ADDITION (+)
  83. {
  84. Vector3d Sum(x + A.x, y + A.y, z + A.z);
  85. return (Sum);
  86. };
  87. Vector3d operator-(const Vector3d &A) const // SUBTRACTION (-)
  88. {
  89. Vector3d Diff(x - A.x, y - A.y, z - A.z);
  90. return (Diff);
  91. };
  92. Vector3d operator*(const float s) const // MULTIPLY BY SCALAR (*)
  93. {
  94. Vector3d Scaled(x * s, y * s, z * s);
  95. return (Scaled);
  96. };
  97. Vector3d operator+(const float s) const // ADD CONSTANT TO ALL 3 COMPONENTS (*)
  98. {
  99. Vector3d Scaled(x + s, y + s, z + s);
  100. return (Scaled);
  101. };
  102. Vector3d operator/(const float s) const // DIVIDE BY SCALAR (/)
  103. {
  104. float r = 1.0f / s;
  105. Vector3d Scaled(x * r, y * r, z * r);
  106. return (Scaled);
  107. };
  108. void operator/=(float A) // ACCUMULATED VECTOR ADDITION (/=)
  109. {
  110. x /= A;
  111. y /= A;
  112. z /= A;
  113. };
  114. void operator+=(const Vector3d A) // ACCUMULATED VECTOR ADDITION (+=)
  115. {
  116. x += A.x;
  117. y += A.y;
  118. z += A.z;
  119. };
  120. void operator-=(const Vector3d A) // ACCUMULATED VECTOR SUBTRACTION (+=)
  121. {
  122. x -= A.x;
  123. y -= A.y;
  124. z -= A.z;
  125. };
  126. void operator*=(const float s) // ACCUMULATED SCALAR MULTIPLICATION (*=) (bpc 4/24/2000)
  127. {
  128. x *= s;
  129. y *= s;
  130. z *= s;
  131. }
  132. void operator+=(const float A) // ACCUMULATED VECTOR ADDITION (+=)
  133. {
  134. x += A;
  135. y += A;
  136. z += A;
  137. };
  138. Vector3d operator-(void) const // NEGATION (-)
  139. {
  140. Vector3d Negated(-x, -y, -z);
  141. return (Negated);
  142. };
  143. float operator[](const int i) const // ALLOWS VECTOR ACCESS AS AN ARRAY.
  144. {
  145. return ((i == 0) ? x : ((i == 1) ? y : z));
  146. };
  147. float &operator[](const int i)
  148. {
  149. return ((i == 0) ? x : ((i == 1) ? y : z));
  150. };
  151. //
  152. // accessor methods.
  153. float GetX(void) const { return x; };
  154. float GetY(void) const { return y; };
  155. float GetZ(void) const { return z; };
  156. float X(void) const { return x; };
  157. float Y(void) const { return y; };
  158. float Z(void) const { return z; };
  159. void SetX(float t) { x = t; };
  160. void SetY(float t) { y = t; };
  161. void SetZ(float t) { z = t; };
  162. bool IsSame(const Vector3d &v, float epsilon) const
  163. {
  164. float dx = fabsf(x - v.x);
  165. if (dx > epsilon) return false;
  166. float dy = fabsf(y - v.y);
  167. if (dy > epsilon) return false;
  168. float dz = fabsf(z - v.z);
  169. if (dz > epsilon) return false;
  170. return true;
  171. }
  172. float ComputeNormal(const Vector3d &A,
  173. const Vector3d &B,
  174. const Vector3d &C)
  175. {
  176. float vx, vy, vz, wx, wy, wz, vw_x, vw_y, vw_z, mag;
  177. vx = (B.x - C.x);
  178. vy = (B.y - C.y);
  179. vz = (B.z - C.z);
  180. wx = (A.x - B.x);
  181. wy = (A.y - B.y);
  182. wz = (A.z - B.z);
  183. vw_x = vy * wz - vz * wy;
  184. vw_y = vz * wx - vx * wz;
  185. vw_z = vx * wy - vy * wx;
  186. mag = sqrtf((vw_x * vw_x) + (vw_y * vw_y) + (vw_z * vw_z));
  187. if (mag < 0.000001f)
  188. {
  189. mag = 0;
  190. }
  191. else
  192. {
  193. mag = 1.0f / mag;
  194. }
  195. x = vw_x * mag;
  196. y = vw_y * mag;
  197. z = vw_z * mag;
  198. return mag;
  199. }
  200. void ScaleSumScale(float c0, float c1, const Vector3d &pos)
  201. {
  202. x = (x * c0) + (pos.x * c1);
  203. y = (y * c0) + (pos.y * c1);
  204. z = (z * c0) + (pos.z * c1);
  205. }
  206. void SwapYZ(void)
  207. {
  208. float t = y;
  209. y = z;
  210. z = t;
  211. };
  212. void Get(float *v) const
  213. {
  214. v[0] = x;
  215. v[1] = y;
  216. v[2] = z;
  217. };
  218. void Set(const int *p)
  219. {
  220. x = (float)p[0];
  221. y = (float)p[1];
  222. z = (float)p[2];
  223. }
  224. void Set(const float *p)
  225. {
  226. x = (float)p[0];
  227. y = (float)p[1];
  228. z = (float)p[2];
  229. }
  230. void Set(float a, float b, float c)
  231. {
  232. x = a;
  233. y = b;
  234. z = c;
  235. };
  236. void Zero(void)
  237. {
  238. x = y = z = 0;
  239. };
  240. const float *Ptr() const { return &x; }
  241. float *Ptr() { return &x; }
  242. // return -(*this).
  243. Vector3d negative(void) const
  244. {
  245. Vector3d result;
  246. result.x = -x;
  247. result.y = -y;
  248. result.z = -z;
  249. return result;
  250. }
  251. float Magnitude(void) const
  252. {
  253. return float(sqrt(x * x + y * y + z * z));
  254. };
  255. float FastMagnitude(void) const
  256. {
  257. return float(sqrtf(x * x + y * y + z * z));
  258. };
  259. float FasterMagnitude(void) const
  260. {
  261. return float(sqrtf(x * x + y * y + z * z));
  262. };
  263. void Lerp(const Vector3d &from, const Vector3d &to, float slerp)
  264. {
  265. x = ((to.x - from.x) * slerp) + from.x;
  266. y = ((to.y - from.y) * slerp) + from.y;
  267. z = ((to.z - from.z) * slerp) + from.z;
  268. };
  269. // Highly specialized interpolate routine. Will compute the interpolated position
  270. // shifted forward or backwards along the ray defined between (from) and (to).
  271. // Reason for existance is so that when a bullet collides with a wall, for
  272. // example, you can generate a graphic effect slightly *before* it hit the
  273. // wall so that the effect doesn't sort into the wall itself.
  274. void Interpolate(const Vector3d &from, const Vector3d &to, float offset)
  275. {
  276. x = to.x - from.x;
  277. y = to.y - from.y;
  278. z = to.z - from.z;
  279. float d = sqrtf(x * x + y * y + z * z);
  280. float recip = 1.0f / d;
  281. x *= recip;
  282. y *= recip;
  283. z *= recip; // normalize vector
  284. d += offset; // shift along ray
  285. x = x * d + from.x;
  286. y = y * d + from.y;
  287. z = z * d + from.z;
  288. };
  289. bool BinaryEqual(const Vector3d &p) const
  290. {
  291. const int *source = (const int *)&x;
  292. const int *dest = (const int *)&p.x;
  293. if (source[0] == dest[0] &&
  294. source[1] == dest[1] &&
  295. source[2] == dest[2]) return true;
  296. return false;
  297. };
  298. /*bool BinaryEqual(const Vector3d<int> &p) const
  299. {
  300. if ( x == p.x && y == p.y && z == p.z ) return true;
  301. return false;
  302. }
  303. */
  304. /** Computes the reflection vector between two vectors.*/
  305. void Reflection(const Vector3d &a, const Vector3d &b) // compute reflection vector.
  306. {
  307. Vector3d c;
  308. Vector3d d;
  309. float dot = a.Dot(b) * 2.0f;
  310. c = b * dot;
  311. d = c - a;
  312. x = -d.x;
  313. y = -d.y;
  314. z = -d.z;
  315. };
  316. void AngleAxis(float angle, const Vector3d &axis)
  317. {
  318. x = axis.x * angle;
  319. y = axis.y * angle;
  320. z = axis.z * angle;
  321. };
  322. float Length(void) const // length of vector.
  323. {
  324. return float(sqrt(x * x + y * y + z * z));
  325. };
  326. float ComputePlane(const Vector3d &A,
  327. const Vector3d &B,
  328. const Vector3d &C)
  329. {
  330. float vx, vy, vz, wx, wy, wz, vw_x, vw_y, vw_z, mag;
  331. vx = (B.x - C.x);
  332. vy = (B.y - C.y);
  333. vz = (B.z - C.z);
  334. wx = (A.x - B.x);
  335. wy = (A.y - B.y);
  336. wz = (A.z - B.z);
  337. vw_x = vy * wz - vz * wy;
  338. vw_y = vz * wx - vx * wz;
  339. vw_z = vx * wy - vy * wx;
  340. mag = sqrtf((vw_x * vw_x) + (vw_y * vw_y) + (vw_z * vw_z));
  341. if (mag < 0.000001f)
  342. {
  343. mag = 0;
  344. }
  345. else
  346. {
  347. mag = 1.0f / mag;
  348. }
  349. x = vw_x * mag;
  350. y = vw_y * mag;
  351. z = vw_z * mag;
  352. float D = 0.0f - ((x * A.x) + (y * A.y) + (z * A.z));
  353. return D;
  354. }
  355. float FastLength(void) const // length of vector.
  356. {
  357. return float(sqrtf(x * x + y * y + z * z));
  358. };
  359. float FasterLength(void) const // length of vector.
  360. {
  361. return float(sqrtf(x * x + y * y + z * z));
  362. };
  363. float Length2(void) const // squared distance, prior to square root.
  364. {
  365. float l2 = x * x + y * y + z * z;
  366. return l2;
  367. };
  368. float Distance(const Vector3d &a) const // distance between two points.
  369. {
  370. Vector3d d(a.x - x, a.y - y, a.z - z);
  371. return d.Length();
  372. }
  373. float FastDistance(const Vector3d &a) const // distance between two points.
  374. {
  375. Vector3d d(a.x - x, a.y - y, a.z - z);
  376. return d.FastLength();
  377. }
  378. float FasterDistance(const Vector3d &a) const // distance between two points.
  379. {
  380. Vector3d d(a.x - x, a.y - y, a.z - z);
  381. return d.FasterLength();
  382. }
  383. float DistanceXY(const Vector3d &a) const
  384. {
  385. float dx = a.x - x;
  386. float dy = a.y - y;
  387. float dist = dx * dx + dy * dy;
  388. return dist;
  389. }
  390. float Distance2(const Vector3d &a) const // squared distance.
  391. {
  392. float dx = a.x - x;
  393. float dy = a.y - y;
  394. float dz = a.z - z;
  395. return dx * dx + dy * dy + dz * dz;
  396. };
  397. float Partial(const Vector3d &p) const
  398. {
  399. return (x * p.y) - (p.x * y);
  400. }
  401. float Area(const Vector3d &p1, const Vector3d &p2) const
  402. {
  403. float A = Partial(p1);
  404. A += p1.Partial(p2);
  405. A += p2.Partial(*this);
  406. return A * 0.5f;
  407. }
  408. inline float Normalize(void) // normalize to a unit vector, returns distance.
  409. {
  410. float d = sqrtf(static_cast<float>(x * x + y * y + z * z));
  411. if (d > 0)
  412. {
  413. float r = 1.0f / d;
  414. x *= r;
  415. y *= r;
  416. z *= r;
  417. }
  418. else
  419. {
  420. x = y = z = 1;
  421. }
  422. return d;
  423. };
  424. inline float FastNormalize(void) // normalize to a unit vector, returns distance.
  425. {
  426. float d = sqrt(static_cast<float>(x * x + y * y + z * z));
  427. if (d > 0)
  428. {
  429. float r = 1.0f / d;
  430. x *= r;
  431. y *= r;
  432. z *= r;
  433. }
  434. else
  435. {
  436. x = y = z = 1;
  437. }
  438. return d;
  439. };
  440. inline float FasterNormalize(void) // normalize to a unit vector, returns distance.
  441. {
  442. float d = sqrtf(static_cast<float>(x * x + y * y + z * z));
  443. if (d > 0)
  444. {
  445. float r = 1.0f / d;
  446. x *= r;
  447. y *= r;
  448. z *= r;
  449. }
  450. else
  451. {
  452. x = y = z = 1;
  453. }
  454. return d;
  455. };
  456. float Dot(const Vector3d &a) const // computes dot product.
  457. {
  458. return (x * a.x + y * a.y + z * a.z);
  459. };
  460. Vector3d Cross(const Vector3d &other) const
  461. {
  462. Vector3d result(y * other.z - z * other.y, z * other.x - x * other.z, x * other.y - y * other.x);
  463. return result;
  464. }
  465. void Cross(const Vector3d &a, const Vector3d &b) // cross two vectors result in this one.
  466. {
  467. x = a.y * b.z - a.z * b.y;
  468. y = a.z * b.x - a.x * b.z;
  469. z = a.x * b.y - a.y * b.x;
  470. };
  471. /******************************************/
  472. // Check if next edge (b to c) turns inward
  473. //
  474. // Edge from a to b is already in face
  475. // Edge from b to c is being considered for addition to face
  476. /******************************************/
  477. bool Concave(const Vector3d &a, const Vector3d &b)
  478. {
  479. float vx, vy, vz, wx, wy, wz, vw_x, vw_y, vw_z, mag, nx, ny, nz, mag_a, mag_b;
  480. wx = b.x - a.x;
  481. wy = b.y - a.y;
  482. wz = b.z - a.z;
  483. mag_a = (float)sqrtf((wx * wx) + (wy * wy) + (wz * wz));
  484. vx = x - b.x;
  485. vy = y - b.y;
  486. vz = z - b.z;
  487. mag_b = (float)sqrtf((vx * vx) + (vy * vy) + (vz * vz));
  488. vw_x = (vy * wz) - (vz * wy);
  489. vw_y = (vz * wx) - (vx * wz);
  490. vw_z = (vx * wy) - (vy * wx);
  491. mag = (float)sqrtf((vw_x * vw_x) + (vw_y * vw_y) + (vw_z * vw_z));
  492. // Check magnitude of cross product, which is a sine function
  493. // i.e., mag (a x b) = mag (a) * mag (b) * sin (theta);
  494. // If sin (theta) small, then angle between edges is very close to
  495. // 180, which we may want to call a concavity. Setting the
  496. // CONCAVITY_TOLERANCE value greater than about 0.01 MAY cause
  497. // face consolidation to get stuck on particular face. Most meshes
  498. // convert properly with a value of 0.0
  499. if (mag / (mag_a * mag_b) <= 0.0f) return true;
  500. mag = 1.0f / mag;
  501. nx = vw_x * mag;
  502. ny = vw_y * mag;
  503. nz = vw_z * mag;
  504. // Dot product of tri normal with cross product result will
  505. // yield positive number if edges are convex (+1.0 if two tris
  506. // are coplanar), negative number if edges are concave (-1.0 if
  507. // two tris are coplanar.)
  508. mag = (x * nx) + (y * ny) + (z * nz);
  509. if (mag > 0.0f) return false;
  510. return (true);
  511. };
  512. bool PointTestXY(const Vector3d &i, const Vector3d &j) const
  513. {
  514. if ((((i.y <= y) && (y < j.y)) ||
  515. ((j.y <= y) && (y < i.y))) &&
  516. (x < (j.x - i.x) * (y - i.y) / (j.y - i.y) + i.x)) return true;
  517. return false;
  518. }
  519. // test to see if this point is inside the triangle specified by
  520. // these three points on the X/Y plane.
  521. bool PointInTriXY(const Vector3d &p1,
  522. const Vector3d &p2,
  523. const Vector3d &p3) const
  524. {
  525. float ax = p3.x - p2.x;
  526. float ay = p3.y - p2.y;
  527. float bx = p1.x - p3.x;
  528. float by = p1.y - p3.y;
  529. float cx = p2.x - p1.x;
  530. float cy = p2.y - p1.y;
  531. float apx = x - p1.x;
  532. float apy = y - p1.y;
  533. float bpx = x - p2.x;
  534. float bpy = y - p2.y;
  535. float cpx = x - p3.x;
  536. float cpy = y - p3.y;
  537. float aCROSSbp = ax * bpy - ay * bpx;
  538. float cCROSSap = cx * apy - cy * apx;
  539. float bCROSScp = bx * cpy - by * cpx;
  540. return ((aCROSSbp >= 0.0f) && (bCROSScp >= 0.0f) && (cCROSSap >= 0.0f));
  541. };
  542. // test to see if this point is inside the triangle specified by
  543. // these three points on the X/Y plane.
  544. bool PointInTriYZ(const Vector3d &p1,
  545. const Vector3d &p2,
  546. const Vector3d &p3) const
  547. {
  548. float ay = p3.y - p2.y;
  549. float az = p3.z - p2.z;
  550. float by = p1.y - p3.y;
  551. float bz = p1.z - p3.z;
  552. float cy = p2.y - p1.y;
  553. float cz = p2.z - p1.z;
  554. float apy = y - p1.y;
  555. float apz = z - p1.z;
  556. float bpy = y - p2.y;
  557. float bpz = z - p2.z;
  558. float cpy = y - p3.y;
  559. float cpz = z - p3.z;
  560. float aCROSSbp = ay * bpz - az * bpy;
  561. float cCROSSap = cy * apz - cz * apy;
  562. float bCROSScp = by * cpz - bz * cpy;
  563. return ((aCROSSbp >= 0.0f) && (bCROSScp >= 0.0f) && (cCROSSap >= 0.0f));
  564. };
  565. // test to see if this point is inside the triangle specified by
  566. // these three points on the X/Y plane.
  567. bool PointInTriXZ(const Vector3d &p1,
  568. const Vector3d &p2,
  569. const Vector3d &p3) const
  570. {
  571. float az = p3.z - p2.z;
  572. float ax = p3.x - p2.x;
  573. float bz = p1.z - p3.z;
  574. float bx = p1.x - p3.x;
  575. float cz = p2.z - p1.z;
  576. float cx = p2.x - p1.x;
  577. float apz = z - p1.z;
  578. float apx = x - p1.x;
  579. float bpz = z - p2.z;
  580. float bpx = x - p2.x;
  581. float cpz = z - p3.z;
  582. float cpx = x - p3.x;
  583. float aCROSSbp = az * bpx - ax * bpz;
  584. float cCROSSap = cz * apx - cx * apz;
  585. float bCROSScp = bz * cpx - bx * cpz;
  586. return ((aCROSSbp >= 0.0f) && (bCROSScp >= 0.0f) && (cCROSSap >= 0.0f));
  587. };
  588. // Given a point and a line (defined by two points), compute the closest point
  589. // in the line. (The line is treated as infinitely long.)
  590. void NearestPointInLine(const Vector3d &point,
  591. const Vector3d &line0,
  592. const Vector3d &line1)
  593. {
  594. Vector3d &nearestPoint = *this;
  595. Vector3d lineDelta = line1 - line0;
  596. // Handle degenerate lines
  597. if (lineDelta == Vector3d(0, 0, 0))
  598. {
  599. nearestPoint = line0;
  600. }
  601. else
  602. {
  603. float delta = (point - line0).Dot(lineDelta) / (lineDelta).Dot(lineDelta);
  604. nearestPoint = line0 + lineDelta * delta;
  605. }
  606. }
  607. // Given a point and a line segment (defined by two points), compute the closest point
  608. // in the line. Cap the point at the endpoints of the line segment.
  609. void NearestPointInLineSegment(const Vector3d &point,
  610. const Vector3d &line0,
  611. const Vector3d &line1)
  612. {
  613. Vector3d &nearestPoint = *this;
  614. Vector3d lineDelta = line1 - line0;
  615. // Handle degenerate lines
  616. if (lineDelta == Vector3d(0, 0, 0))
  617. {
  618. nearestPoint = line0;
  619. }
  620. else
  621. {
  622. float delta = (point - line0).Dot(lineDelta) / (lineDelta).Dot(lineDelta);
  623. // Clamp the point to conform to the segment's endpoints
  624. if (delta < 0)
  625. delta = 0;
  626. else if (delta > 1)
  627. delta = 1;
  628. nearestPoint = line0 + lineDelta * delta;
  629. }
  630. }
  631. // Given a point and a plane (defined by three points), compute the closest point
  632. // in the plane. (The plane is unbounded.)
  633. void NearestPointInPlane(const Vector3d &point,
  634. const Vector3d &triangle0,
  635. const Vector3d &triangle1,
  636. const Vector3d &triangle2)
  637. {
  638. Vector3d &nearestPoint = *this;
  639. Vector3d lineDelta0 = triangle1 - triangle0;
  640. Vector3d lineDelta1 = triangle2 - triangle0;
  641. Vector3d pointDelta = point - triangle0;
  642. Vector3d normal;
  643. // Get the normal of the polygon (doesn't have to be a unit vector)
  644. normal.Cross(lineDelta0, lineDelta1);
  645. float delta = normal.Dot(pointDelta) / normal.Dot(normal);
  646. nearestPoint = point - normal * delta;
  647. }
  648. // Given a point and a plane (defined by a coplanar point and a normal), compute the closest point
  649. // in the plane. (The plane is unbounded.)
  650. void NearestPointInPlane(const Vector3d &point,
  651. const Vector3d &planePoint,
  652. const Vector3d &planeNormal)
  653. {
  654. Vector3d &nearestPoint = *this;
  655. Vector3d pointDelta = point - planePoint;
  656. float delta = planeNormal.Dot(pointDelta) / planeNormal.Dot(planeNormal);
  657. nearestPoint = point - planeNormal * delta;
  658. }
  659. // Given a point and a triangle (defined by three points), compute the closest point
  660. // in the triangle. Clamp the point so it's confined to the area of the triangle.
  661. void NearestPointInTriangle(const Vector3d &point,
  662. const Vector3d &triangle0,
  663. const Vector3d &triangle1,
  664. const Vector3d &triangle2)
  665. {
  666. static const Vector3d zeroVector(0, 0, 0);
  667. Vector3d &nearestPoint = *this;
  668. Vector3d lineDelta0 = triangle1 - triangle0;
  669. Vector3d lineDelta1 = triangle2 - triangle0;
  670. // Handle degenerate triangles
  671. if ((lineDelta0 == zeroVector) || (lineDelta1 == zeroVector))
  672. {
  673. nearestPoint.NearestPointInLineSegment(point, triangle1, triangle2);
  674. }
  675. else if (lineDelta0 == lineDelta1)
  676. {
  677. nearestPoint.NearestPointInLineSegment(point, triangle0, triangle1);
  678. }
  679. else
  680. {
  681. Vector3d axis[3];
  682. axis[0].NearestPointInLine(triangle0, triangle1, triangle2);
  683. axis[1].NearestPointInLine(triangle1, triangle0, triangle2);
  684. axis[2].NearestPointInLine(triangle2, triangle0, triangle1);
  685. float axisDot[3];
  686. axisDot[0] = (triangle0 - axis[0]).Dot(point - axis[0]);
  687. axisDot[1] = (triangle1 - axis[1]).Dot(point - axis[1]);
  688. axisDot[2] = (triangle2 - axis[2]).Dot(point - axis[2]);
  689. bool bForce = true;
  690. float bestMagnitude2 = 0;
  691. float closeMagnitude2;
  692. Vector3d closePoint;
  693. if (axisDot[0] < 0)
  694. {
  695. closePoint.NearestPointInLineSegment(point, triangle1, triangle2);
  696. closeMagnitude2 = point.Distance2(closePoint);
  697. if (bForce || (bestMagnitude2 > closeMagnitude2))
  698. {
  699. bForce = false;
  700. bestMagnitude2 = closeMagnitude2;
  701. nearestPoint = closePoint;
  702. }
  703. }
  704. if (axisDot[1] < 0)
  705. {
  706. closePoint.NearestPointInLineSegment(point, triangle0, triangle2);
  707. closeMagnitude2 = point.Distance2(closePoint);
  708. if (bForce || (bestMagnitude2 > closeMagnitude2))
  709. {
  710. bForce = false;
  711. bestMagnitude2 = closeMagnitude2;
  712. nearestPoint = closePoint;
  713. }
  714. }
  715. if (axisDot[2] < 0)
  716. {
  717. closePoint.NearestPointInLineSegment(point, triangle0, triangle1);
  718. closeMagnitude2 = point.Distance2(closePoint);
  719. if (bForce || (bestMagnitude2 > closeMagnitude2))
  720. {
  721. bForce = false;
  722. bestMagnitude2 = closeMagnitude2;
  723. nearestPoint = closePoint;
  724. }
  725. }
  726. // If bForce is true at this point, it means the nearest point lies
  727. // inside the triangle; use the nearest-point-on-a-plane equation
  728. if (bForce)
  729. {
  730. Vector3d normal;
  731. // Get the normal of the polygon (doesn't have to be a unit vector)
  732. normal.Cross(lineDelta0, lineDelta1);
  733. Vector3d pointDelta = point - triangle0;
  734. float delta = normal.Dot(pointDelta) / normal.Dot(normal);
  735. nearestPoint = point - normal * delta;
  736. }
  737. }
  738. }
  739. //private:
  740. float x;
  741. float y;
  742. float z;
  743. };
  744. class Vector2d
  745. {
  746. public:
  747. Vector2d(void){}; // null constructor, does not inialize point.
  748. Vector2d(const Vector2d &a) // constructor copies existing vector.
  749. {
  750. x = a.x;
  751. y = a.y;
  752. };
  753. Vector2d(const float *t)
  754. {
  755. x = t[0];
  756. y = t[1];
  757. };
  758. Vector2d(float a, float b) // construct with initial point.
  759. {
  760. x = a;
  761. y = b;
  762. };
  763. const float *Ptr() const { return &x; }
  764. float *Ptr() { return &x; }
  765. Vector2d &operator+=(const Vector2d &a) // += operator.
  766. {
  767. x += a.x;
  768. y += a.y;
  769. return *this;
  770. };
  771. Vector2d &operator-=(const Vector2d &a)
  772. {
  773. x -= a.x;
  774. y -= a.y;
  775. return *this;
  776. };
  777. Vector2d &operator*=(const Vector2d &a)
  778. {
  779. x *= a.x;
  780. y *= a.y;
  781. return *this;
  782. };
  783. Vector2d &operator/=(const Vector2d &a)
  784. {
  785. x /= a.x;
  786. y /= a.y;
  787. return *this;
  788. };
  789. bool operator==(const Vector2d &a) const
  790. {
  791. if (a.x == x && a.y == y) return true;
  792. return false;
  793. };
  794. bool operator!=(const Vector2d &a) const
  795. {
  796. if (a.x != x || a.y != y) return true;
  797. return false;
  798. };
  799. Vector2d operator+(Vector2d a) const
  800. {
  801. a.x += x;
  802. a.y += y;
  803. return a;
  804. };
  805. Vector2d operator-(Vector2d a) const
  806. {
  807. a.x = x - a.x;
  808. a.y = y - a.y;
  809. return a;
  810. };
  811. Vector2d operator-(void) const
  812. {
  813. return negative();
  814. };
  815. Vector2d operator*(Vector2d a) const
  816. {
  817. a.x *= x;
  818. a.y *= y;
  819. return a;
  820. };
  821. Vector2d operator*(float c) const
  822. {
  823. Vector2d a;
  824. a.x = x * c;
  825. a.y = y * c;
  826. return a;
  827. };
  828. Vector2d operator/(Vector2d a) const
  829. {
  830. a.x = x / a.x;
  831. a.y = y / a.y;
  832. return a;
  833. };
  834. float Dot(const Vector2d &a) const // computes dot product.
  835. {
  836. return (x * a.x + y * a.y);
  837. };
  838. float GetX(void) const { return x; };
  839. float GetY(void) const { return y; };
  840. void SetX(float t) { x = t; };
  841. void SetY(float t) { y = t; };
  842. void Set(float a, float b)
  843. {
  844. x = a;
  845. y = b;
  846. };
  847. void Zero(void)
  848. {
  849. x = y = 0;
  850. };
  851. Vector2d negative(void) const
  852. {
  853. Vector2d result;
  854. result.x = -x;
  855. result.y = -y;
  856. return result;
  857. }
  858. float magnitude(void) const
  859. {
  860. return (float)sqrtf(x * x + y * y);
  861. }
  862. float fastmagnitude(void) const
  863. {
  864. return (float)sqrtf(x * x + y * y);
  865. }
  866. float fastermagnitude(void) const
  867. {
  868. return (float)sqrtf(x * x + y * y);
  869. }
  870. void Reflection(Vector2d &a, Vector2d &b); // compute reflection vector.
  871. float Length(void) const // length of vector.
  872. {
  873. return float(sqrtf(x * x + y * y));
  874. };
  875. float FastLength(void) const // length of vector.
  876. {
  877. return float(sqrtf(x * x + y * y));
  878. };
  879. float FasterLength(void) const // length of vector.
  880. {
  881. return float(sqrtf(x * x + y * y));
  882. };
  883. float Length2(void) // squared distance, prior to square root.
  884. {
  885. return x * x + y * y;
  886. }
  887. float Distance(const Vector2d &a) const // distance between two points.
  888. {
  889. float dx = a.x - x;
  890. float dy = a.y - y;
  891. float d = dx * dx + dy * dy;
  892. return sqrtf(d);
  893. };
  894. float FastDistance(const Vector2d &a) const // distance between two points.
  895. {
  896. float dx = a.x - x;
  897. float dy = a.y - y;
  898. float d = dx * dx + dy * dy;
  899. return sqrtf(d);
  900. };
  901. float FasterDistance(const Vector2d &a) const // distance between two points.
  902. {
  903. float dx = a.x - x;
  904. float dy = a.y - y;
  905. float d = dx * dx + dy * dy;
  906. return sqrtf(d);
  907. };
  908. float Distance2(Vector2d &a) // squared distance.
  909. {
  910. float dx = a.x - x;
  911. float dy = a.y - y;
  912. return dx * dx + dy * dy;
  913. };
  914. void Lerp(const Vector2d &from, const Vector2d &to, float slerp)
  915. {
  916. x = ((to.x - from.x) * slerp) + from.x;
  917. y = ((to.y - from.y) * slerp) + from.y;
  918. };
  919. void Cross(const Vector2d &a, const Vector2d &b) // cross two vectors result in this one.
  920. {
  921. x = a.y * b.x - a.x * b.y;
  922. y = a.x * b.x - a.x * b.x;
  923. };
  924. float Normalize(void) // normalize to a unit vector, returns distance.
  925. {
  926. float l = Length();
  927. if (l != 0)
  928. {
  929. l = float(1) / l;
  930. x *= l;
  931. y *= l;
  932. }
  933. else
  934. {
  935. x = y = 0;
  936. }
  937. return l;
  938. };
  939. float FastNormalize(void) // normalize to a unit vector, returns distance.
  940. {
  941. float l = FastLength();
  942. if (l != 0)
  943. {
  944. l = float(1) / l;
  945. x *= l;
  946. y *= l;
  947. }
  948. else
  949. {
  950. x = y = 0;
  951. }
  952. return l;
  953. };
  954. float FasterNormalize(void) // normalize to a unit vector, returns distance.
  955. {
  956. float l = FasterLength();
  957. if (l != 0)
  958. {
  959. l = float(1) / l;
  960. x *= l;
  961. y *= l;
  962. }
  963. else
  964. {
  965. x = y = 0;
  966. }
  967. return l;
  968. };
  969. float x;
  970. float y;
  971. };
  972. class Line
  973. {
  974. public:
  975. Line(const Vector3d &from, const Vector3d &to)
  976. {
  977. mP1 = from;
  978. mP2 = to;
  979. };
  980. // JWR Test for the intersection of two lines.
  981. bool Intersect(const Line &src, Vector3d &sect);
  982. private:
  983. Vector3d mP1;
  984. Vector3d mP2;
  985. };
  986. typedef std::vector<Vector3d> Vector3dVector;
  987. typedef std::vector<Vector2d> Vector2dVector;
  988. inline Vector3d operator*(float s, const Vector3d &v)
  989. {
  990. Vector3d Scaled(v.x * s, v.y * s, v.z * s);
  991. return (Scaled);
  992. }
  993. inline Vector2d operator*(float s, const Vector2d &v)
  994. {
  995. Vector2d Scaled(v.x * s, v.y * s);
  996. return (Scaled);
  997. }
  998. } // namespace ConvexDecomposition
  999. #endif