cd_vector.h 26 KB

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