CmMath.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  1. /*
  2. -----------------------------------------------------------------------------
  3. This source file is part of OGRE
  4. (Object-oriented Graphics Rendering Engine)
  5. For the latest info, see http://www.ogre3d.org/
  6. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. -----------------------------------------------------------------------------
  23. */
  24. #include "CmMath.h"
  25. #include "CmMathAsm.h"
  26. #include "CmVector2.h"
  27. #include "CmVector3.h"
  28. #include "CmVector4.h"
  29. #include "CmRay.h"
  30. #include "CmSphere.h"
  31. #include "CmAABox.h"
  32. #include "CmPlane.h"
  33. namespace CamelotFramework
  34. {
  35. const float Math::POS_INFINITY = std::numeric_limits<float>::infinity();
  36. const float Math::NEG_INFINITY = -std::numeric_limits<float>::infinity();
  37. const float Math::PI = float( 4.0 * atan( 1.0 ) );
  38. const float Math::TWO_PI = float( 2.0 * PI );
  39. const float Math::HALF_PI = float( 0.5 * PI );
  40. const float Math::fDeg2Rad = PI / float(180.0);
  41. const float Math::fRad2Deg = float(180.0) / PI;
  42. const float Math::LOG2 = log(float(2.0));
  43. int Math::mTrigTableSize;
  44. Math::AngleUnit Math::msAngleUnit;
  45. float Math::mTrigTableFactor;
  46. float *Math::mSinTable = NULL;
  47. float *Math::mTanTable = NULL;
  48. //-----------------------------------------------------------------------
  49. Math::Math( unsigned int trigTableSize )
  50. {
  51. msAngleUnit = AU_DEGREE;
  52. mTrigTableSize = trigTableSize;
  53. mTrigTableFactor = mTrigTableSize / Math::TWO_PI;
  54. mSinTable = (float*)malloc(sizeof(float) * mTrigTableSize);
  55. mTanTable = (float*)malloc(sizeof(float) * mTrigTableSize);
  56. buildTrigTables();
  57. }
  58. //-----------------------------------------------------------------------
  59. Math::~Math()
  60. {
  61. free(mSinTable);
  62. free(mTanTable);
  63. }
  64. //-----------------------------------------------------------------------
  65. void Math::buildTrigTables(void)
  66. {
  67. // Build trig lookup tables
  68. // Could get away with building only PI sized Sin table but simpler this
  69. // way. Who cares, it'll ony use an extra 8k of memory anyway and I like
  70. // simplicity.
  71. float angle;
  72. for (int i = 0; i < mTrigTableSize; ++i)
  73. {
  74. angle = Math::TWO_PI * i / mTrigTableSize;
  75. mSinTable[i] = sin(angle);
  76. mTanTable[i] = tan(angle);
  77. }
  78. }
  79. //-----------------------------------------------------------------------
  80. float Math::SinTable (float fValue)
  81. {
  82. // Convert range to index values, wrap if required
  83. int idx;
  84. if (fValue >= 0)
  85. {
  86. idx = int(fValue * mTrigTableFactor) % mTrigTableSize;
  87. }
  88. else
  89. {
  90. idx = mTrigTableSize - (int(-fValue * mTrigTableFactor) % mTrigTableSize) - 1;
  91. }
  92. return mSinTable[idx];
  93. }
  94. //-----------------------------------------------------------------------
  95. float Math::TanTable (float fValue)
  96. {
  97. // Convert range to index values, wrap if required
  98. int idx = int(fValue *= mTrigTableFactor) % mTrigTableSize;
  99. return mTanTable[idx];
  100. }
  101. //-----------------------------------------------------------------------
  102. Radian Math::ACos (float fValue)
  103. {
  104. if ( -1.0 < fValue )
  105. {
  106. if ( fValue < 1.0 )
  107. return Radian(acos(fValue));
  108. else
  109. return Radian(0.0);
  110. }
  111. else
  112. {
  113. return Radian(PI);
  114. }
  115. }
  116. //-----------------------------------------------------------------------
  117. Radian Math::ASin (float fValue)
  118. {
  119. if ( -1.0 < fValue )
  120. {
  121. if ( fValue < 1.0 )
  122. return Radian(asin(fValue));
  123. else
  124. return Radian(HALF_PI);
  125. }
  126. else
  127. {
  128. return Radian(-HALF_PI);
  129. }
  130. }
  131. //-----------------------------------------------------------------------
  132. float Math::Sign (float fValue)
  133. {
  134. if ( fValue > 0.0 )
  135. return 1.0;
  136. if ( fValue < 0.0 )
  137. return -1.0;
  138. return 0.0;
  139. }
  140. //-----------------------------------------------------------------------
  141. float Math::InvSqrt(float fValue)
  142. {
  143. return float(asm_rsq(fValue));
  144. }
  145. //-----------------------------------------------------------------------
  146. float Math::UnitRandom ()
  147. {
  148. return asm_rand() / asm_rand_max();
  149. }
  150. //-----------------------------------------------------------------------
  151. float Math::RangeRandom (float fLow, float fHigh)
  152. {
  153. return (fHigh-fLow)*UnitRandom() + fLow;
  154. }
  155. //-----------------------------------------------------------------------
  156. float Math::SymmetricRandom ()
  157. {
  158. return 2.0f * UnitRandom() - 1.0f;
  159. }
  160. //-----------------------------------------------------------------------
  161. void Math::setAngleUnit(Math::AngleUnit unit)
  162. {
  163. msAngleUnit = unit;
  164. }
  165. //-----------------------------------------------------------------------
  166. Math::AngleUnit Math::getAngleUnit(void)
  167. {
  168. return msAngleUnit;
  169. }
  170. //-----------------------------------------------------------------------
  171. float Math::AngleUnitsToRadians(float angleunits)
  172. {
  173. if (msAngleUnit == AU_DEGREE)
  174. return angleunits * fDeg2Rad;
  175. else
  176. return angleunits;
  177. }
  178. //-----------------------------------------------------------------------
  179. float Math::RadiansToAngleUnits(float radians)
  180. {
  181. if (msAngleUnit == AU_DEGREE)
  182. return radians * fRad2Deg;
  183. else
  184. return radians;
  185. }
  186. //-----------------------------------------------------------------------
  187. float Math::AngleUnitsToDegrees(float angleunits)
  188. {
  189. if (msAngleUnit == AU_RADIAN)
  190. return angleunits * fRad2Deg;
  191. else
  192. return angleunits;
  193. }
  194. //-----------------------------------------------------------------------
  195. float Math::DegreesToAngleUnits(float degrees)
  196. {
  197. if (msAngleUnit == AU_RADIAN)
  198. return degrees * fDeg2Rad;
  199. else
  200. return degrees;
  201. }
  202. //-----------------------------------------------------------------------
  203. bool Math::pointInTri2D(const Vector2& p, const Vector2& a,
  204. const Vector2& b, const Vector2& c)
  205. {
  206. // Winding must be consistent from all edges for point to be inside
  207. Vector2 v1, v2;
  208. float dot[3];
  209. bool zeroDot[3];
  210. v1 = b - a;
  211. v2 = p - a;
  212. // Note we don't care about normalisation here since sign is all we need
  213. // It means we don't have to worry about magnitude of cross products either
  214. dot[0] = v1.crossProduct(v2);
  215. zeroDot[0] = Math::RealEqual(dot[0], 0.0f, 1e-3f);
  216. v1 = c - b;
  217. v2 = p - b;
  218. dot[1] = v1.crossProduct(v2);
  219. zeroDot[1] = Math::RealEqual(dot[1], 0.0f, 1e-3f);
  220. // Compare signs (ignore colinear / coincident points)
  221. if(!zeroDot[0] && !zeroDot[1]
  222. && Math::Sign(dot[0]) != Math::Sign(dot[1]))
  223. {
  224. return false;
  225. }
  226. v1 = a - c;
  227. v2 = p - c;
  228. dot[2] = v1.crossProduct(v2);
  229. zeroDot[2] = Math::RealEqual(dot[2], 0.0f, 1e-3f);
  230. // Compare signs (ignore colinear / coincident points)
  231. if((!zeroDot[0] && !zeroDot[2]
  232. && Math::Sign(dot[0]) != Math::Sign(dot[2])) ||
  233. (!zeroDot[1] && !zeroDot[2]
  234. && Math::Sign(dot[1]) != Math::Sign(dot[2])))
  235. {
  236. return false;
  237. }
  238. return true;
  239. }
  240. //-----------------------------------------------------------------------
  241. bool Math::pointInTri3D(const Vector3& p, const Vector3& a,
  242. const Vector3& b, const Vector3& c, const Vector3& normal)
  243. {
  244. // Winding must be consistent from all edges for point to be inside
  245. Vector3 v1, v2;
  246. float dot[3];
  247. bool zeroDot[3];
  248. v1 = b - a;
  249. v2 = p - a;
  250. // Note we don't care about normalisation here since sign is all we need
  251. // It means we don't have to worry about magnitude of cross products either
  252. dot[0] = v1.crossProduct(v2).dotProduct(normal);
  253. zeroDot[0] = Math::RealEqual(dot[0], 0.0f, 1e-3f);
  254. v1 = c - b;
  255. v2 = p - b;
  256. dot[1] = v1.crossProduct(v2).dotProduct(normal);
  257. zeroDot[1] = Math::RealEqual(dot[1], 0.0f, 1e-3f);
  258. // Compare signs (ignore colinear / coincident points)
  259. if(!zeroDot[0] && !zeroDot[1]
  260. && Math::Sign(dot[0]) != Math::Sign(dot[1]))
  261. {
  262. return false;
  263. }
  264. v1 = a - c;
  265. v2 = p - c;
  266. dot[2] = v1.crossProduct(v2).dotProduct(normal);
  267. zeroDot[2] = Math::RealEqual(dot[2], 0.0f, 1e-3f);
  268. // Compare signs (ignore colinear / coincident points)
  269. if((!zeroDot[0] && !zeroDot[2]
  270. && Math::Sign(dot[0]) != Math::Sign(dot[2])) ||
  271. (!zeroDot[1] && !zeroDot[2]
  272. && Math::Sign(dot[1]) != Math::Sign(dot[2])))
  273. {
  274. return false;
  275. }
  276. return true;
  277. }
  278. //-----------------------------------------------------------------------
  279. bool Math::RealEqual( float a, float b, float tolerance )
  280. {
  281. if (fabs(b-a) <= tolerance)
  282. return true;
  283. else
  284. return false;
  285. }
  286. //-----------------------------------------------------------------------
  287. std::pair<bool, float> Math::intersects(const Ray& ray, const Plane& plane)
  288. {
  289. float denom = plane.normal.dotProduct(ray.getDirection());
  290. if (Math::Abs(denom) < std::numeric_limits<float>::epsilon())
  291. {
  292. // Parallel
  293. return std::pair<bool, float>(false, 0.0f);
  294. }
  295. else
  296. {
  297. float nom = plane.normal.dotProduct(ray.getOrigin()) + plane.d;
  298. float t = -(nom/denom);
  299. return std::pair<bool, float>(t >= 0, t);
  300. }
  301. }
  302. //-----------------------------------------------------------------------
  303. std::pair<bool, float> Math::intersects(const Ray& ray,
  304. const Vector<Plane>::type& planes, bool normalIsOutside)
  305. {
  306. List<Plane>::type planesList;
  307. for (Vector<Plane>::type::const_iterator i = planes.begin(); i != planes.end(); ++i)
  308. {
  309. planesList.push_back(*i);
  310. }
  311. return intersects(ray, planesList, normalIsOutside);
  312. }
  313. //-----------------------------------------------------------------------
  314. std::pair<bool, float> Math::intersects(const Ray& ray,
  315. const List<Plane>::type& planes, bool normalIsOutside)
  316. {
  317. List<Plane>::type::const_iterator planeit, planeitend;
  318. planeitend = planes.end();
  319. bool allInside = true;
  320. std::pair<bool, float> ret;
  321. std::pair<bool, float> end;
  322. ret.first = false;
  323. ret.second = 0.0f;
  324. end.first = false;
  325. end.second = 0;
  326. // derive side
  327. // NB we don't pass directly since that would require Plane::Side in
  328. // interface, which results in recursive includes since Math is so fundamental
  329. Plane::Side outside = normalIsOutside ? Plane::POSITIVE_SIDE : Plane::NEGATIVE_SIDE;
  330. for (planeit = planes.begin(); planeit != planeitend; ++planeit)
  331. {
  332. const Plane& plane = *planeit;
  333. // is origin outside?
  334. if (plane.getSide(ray.getOrigin()) == outside)
  335. {
  336. allInside = false;
  337. // Test single plane
  338. std::pair<bool, float> planeRes =
  339. ray.intersects(plane);
  340. if (planeRes.first)
  341. {
  342. // Ok, we intersected
  343. ret.first = true;
  344. // Use the most distant result since convex volume
  345. ret.second = std::max(ret.second, planeRes.second);
  346. }
  347. else
  348. {
  349. ret.first =false;
  350. ret.second=0.0f;
  351. return ret;
  352. }
  353. }
  354. else
  355. {
  356. std::pair<bool, float> planeRes =
  357. ray.intersects(plane);
  358. if (planeRes.first)
  359. {
  360. if( !end.first )
  361. {
  362. end.first = true;
  363. end.second = planeRes.second;
  364. }
  365. else
  366. {
  367. end.second = std::min( planeRes.second, end.second );
  368. }
  369. }
  370. }
  371. }
  372. if (allInside)
  373. {
  374. // Intersecting at 0 distance since inside the volume!
  375. ret.first = true;
  376. ret.second = 0.0f;
  377. return ret;
  378. }
  379. if( end.first )
  380. {
  381. if( end.second < ret.second )
  382. {
  383. ret.first = false;
  384. return ret;
  385. }
  386. }
  387. return ret;
  388. }
  389. //-----------------------------------------------------------------------
  390. std::pair<bool, float> Math::intersects(const Ray& ray, const Sphere& sphere,
  391. bool discardInside)
  392. {
  393. const Vector3& raydir = ray.getDirection();
  394. // Adjust ray origin relative to sphere center
  395. const Vector3& rayorig = ray.getOrigin() - sphere.getCenter();
  396. float radius = sphere.getRadius();
  397. // Check origin inside first
  398. if (rayorig.squaredLength() <= radius*radius && discardInside)
  399. {
  400. return std::pair<bool, float>(true, 0.0f);
  401. }
  402. // Mmm, quadratics
  403. // Build coeffs which can be used with std quadratic solver
  404. // ie t = (-b +/- sqrt(b*b + 4ac)) / 2a
  405. float a = raydir.dotProduct(raydir);
  406. float b = 2 * rayorig.dotProduct(raydir);
  407. float c = rayorig.dotProduct(rayorig) - radius*radius;
  408. // Calc determinant
  409. float d = (b*b) - (4 * a * c);
  410. if (d < 0)
  411. {
  412. // No intersection
  413. return std::pair<bool, float>(false, 0.0f);
  414. }
  415. else
  416. {
  417. // BTW, if d=0 there is one intersection, if d > 0 there are 2
  418. // But we only want the closest one, so that's ok, just use the
  419. // '-' version of the solver
  420. float t = ( -b - Math::Sqrt(d) ) / (2 * a);
  421. if (t < 0)
  422. t = ( -b + Math::Sqrt(d) ) / (2 * a);
  423. return std::pair<bool, float>(true, t);
  424. }
  425. }
  426. //-----------------------------------------------------------------------
  427. std::pair<bool, float> Math::intersects(const Ray& ray, const AABox& box)
  428. {
  429. float lowt = 0.0f;
  430. float t;
  431. bool hit = false;
  432. Vector3 hitpoint;
  433. const Vector3& min = box.getMin();
  434. const Vector3& max = box.getMax();
  435. const Vector3& rayorig = ray.getOrigin();
  436. const Vector3& raydir = ray.getDirection();
  437. // Check origin inside first
  438. if ( rayorig > min && rayorig < max )
  439. {
  440. return std::pair<bool, float>(true, 0.0f);
  441. }
  442. // Check each face in turn, only check closest 3
  443. // Min x
  444. if (rayorig.x <= min.x && raydir.x > 0)
  445. {
  446. t = (min.x - rayorig.x) / raydir.x;
  447. if (t >= 0)
  448. {
  449. // Substitute t back into ray and check bounds and dist
  450. hitpoint = rayorig + raydir * t;
  451. if (hitpoint.y >= min.y && hitpoint.y <= max.y &&
  452. hitpoint.z >= min.z && hitpoint.z <= max.z &&
  453. (!hit || t < lowt))
  454. {
  455. hit = true;
  456. lowt = t;
  457. }
  458. }
  459. }
  460. // Max x
  461. if (rayorig.x >= max.x && raydir.x < 0)
  462. {
  463. t = (max.x - rayorig.x) / raydir.x;
  464. if (t >= 0)
  465. {
  466. // Substitute t back into ray and check bounds and dist
  467. hitpoint = rayorig + raydir * t;
  468. if (hitpoint.y >= min.y && hitpoint.y <= max.y &&
  469. hitpoint.z >= min.z && hitpoint.z <= max.z &&
  470. (!hit || t < lowt))
  471. {
  472. hit = true;
  473. lowt = t;
  474. }
  475. }
  476. }
  477. // Min y
  478. if (rayorig.y <= min.y && raydir.y > 0)
  479. {
  480. t = (min.y - rayorig.y) / raydir.y;
  481. if (t >= 0)
  482. {
  483. // Substitute t back into ray and check bounds and dist
  484. hitpoint = rayorig + raydir * t;
  485. if (hitpoint.x >= min.x && hitpoint.x <= max.x &&
  486. hitpoint.z >= min.z && hitpoint.z <= max.z &&
  487. (!hit || t < lowt))
  488. {
  489. hit = true;
  490. lowt = t;
  491. }
  492. }
  493. }
  494. // Max y
  495. if (rayorig.y >= max.y && raydir.y < 0)
  496. {
  497. t = (max.y - rayorig.y) / raydir.y;
  498. if (t >= 0)
  499. {
  500. // Substitute t back into ray and check bounds and dist
  501. hitpoint = rayorig + raydir * t;
  502. if (hitpoint.x >= min.x && hitpoint.x <= max.x &&
  503. hitpoint.z >= min.z && hitpoint.z <= max.z &&
  504. (!hit || t < lowt))
  505. {
  506. hit = true;
  507. lowt = t;
  508. }
  509. }
  510. }
  511. // Min z
  512. if (rayorig.z <= min.z && raydir.z > 0)
  513. {
  514. t = (min.z - rayorig.z) / raydir.z;
  515. if (t >= 0)
  516. {
  517. // Substitute t back into ray and check bounds and dist
  518. hitpoint = rayorig + raydir * t;
  519. if (hitpoint.x >= min.x && hitpoint.x <= max.x &&
  520. hitpoint.y >= min.y && hitpoint.y <= max.y &&
  521. (!hit || t < lowt))
  522. {
  523. hit = true;
  524. lowt = t;
  525. }
  526. }
  527. }
  528. // Max z
  529. if (rayorig.z >= max.z && raydir.z < 0)
  530. {
  531. t = (max.z - rayorig.z) / raydir.z;
  532. if (t >= 0)
  533. {
  534. // Substitute t back into ray and check bounds and dist
  535. hitpoint = rayorig + raydir * t;
  536. if (hitpoint.x >= min.x && hitpoint.x <= max.x &&
  537. hitpoint.y >= min.y && hitpoint.y <= max.y &&
  538. (!hit || t < lowt))
  539. {
  540. hit = true;
  541. lowt = t;
  542. }
  543. }
  544. }
  545. return std::pair<bool, float>(hit, lowt);
  546. }
  547. //-----------------------------------------------------------------------
  548. bool Math::intersects(const Ray& ray, const AABox& box,
  549. float* d1, float* d2)
  550. {
  551. const Vector3& min = box.getMin();
  552. const Vector3& max = box.getMax();
  553. const Vector3& rayorig = ray.getOrigin();
  554. const Vector3& raydir = ray.getDirection();
  555. Vector3 absDir;
  556. absDir[0] = Math::Abs(raydir[0]);
  557. absDir[1] = Math::Abs(raydir[1]);
  558. absDir[2] = Math::Abs(raydir[2]);
  559. // Sort the axis, ensure check minimise floating error axis first
  560. int imax = 0, imid = 1, imin = 2;
  561. if (absDir[0] < absDir[2])
  562. {
  563. imax = 2;
  564. imin = 0;
  565. }
  566. if (absDir[1] < absDir[imin])
  567. {
  568. imid = imin;
  569. imin = 1;
  570. }
  571. else if (absDir[1] > absDir[imax])
  572. {
  573. imid = imax;
  574. imax = 1;
  575. }
  576. float start = 0, end = Math::POS_INFINITY;
  577. #define _CALC_AXIS(i) \
  578. do { \
  579. float denom = 1 / raydir[i]; \
  580. float newstart = (min[i] - rayorig[i]) * denom; \
  581. float newend = (max[i] - rayorig[i]) * denom; \
  582. if (newstart > newend) std::swap(newstart, newend); \
  583. if (newstart > end || newend < start) return false; \
  584. if (newstart > start) start = newstart; \
  585. if (newend < end) end = newend; \
  586. } while(0)
  587. // Check each axis in turn
  588. _CALC_AXIS(imax);
  589. if (absDir[imid] < std::numeric_limits<float>::epsilon())
  590. {
  591. // Parallel with middle and minimise axis, check bounds only
  592. if (rayorig[imid] < min[imid] || rayorig[imid] > max[imid] ||
  593. rayorig[imin] < min[imin] || rayorig[imin] > max[imin])
  594. return false;
  595. }
  596. else
  597. {
  598. _CALC_AXIS(imid);
  599. if (absDir[imin] < std::numeric_limits<float>::epsilon())
  600. {
  601. // Parallel with minimise axis, check bounds only
  602. if (rayorig[imin] < min[imin] || rayorig[imin] > max[imin])
  603. return false;
  604. }
  605. else
  606. {
  607. _CALC_AXIS(imin);
  608. }
  609. }
  610. #undef _CALC_AXIS
  611. if (d1) *d1 = start;
  612. if (d2) *d2 = end;
  613. return true;
  614. }
  615. //-----------------------------------------------------------------------
  616. std::pair<bool, float> Math::intersects(const Ray& ray, const Vector3& a,
  617. const Vector3& b, const Vector3& c, const Vector3& normal,
  618. bool positiveSide, bool negativeSide)
  619. {
  620. //
  621. // Calculate intersection with plane.
  622. //
  623. float t;
  624. {
  625. float denom = normal.dotProduct(ray.getDirection());
  626. // Check intersect side
  627. if (denom > + std::numeric_limits<float>::epsilon())
  628. {
  629. if (!negativeSide)
  630. return std::pair<bool, float>(false, 0.0f);
  631. }
  632. else if (denom < - std::numeric_limits<float>::epsilon())
  633. {
  634. if (!positiveSide)
  635. return std::pair<bool, float>(false, 0.0f);
  636. }
  637. else
  638. {
  639. // Parallel or triangle area is close to zero when
  640. // the plane normal not normalised.
  641. return std::pair<bool, float>(false, 0.0f);
  642. }
  643. t = normal.dotProduct(a - ray.getOrigin()) / denom;
  644. if (t < 0)
  645. {
  646. // Intersection is behind origin
  647. return std::pair<bool, float>(false, 0.0f);
  648. }
  649. }
  650. //
  651. // Calculate the largest area projection plane in X, Y or Z.
  652. //
  653. size_t i0, i1;
  654. {
  655. float n0 = Math::Abs(normal[0]);
  656. float n1 = Math::Abs(normal[1]);
  657. float n2 = Math::Abs(normal[2]);
  658. i0 = 1; i1 = 2;
  659. if (n1 > n2)
  660. {
  661. if (n1 > n0) i0 = 0;
  662. }
  663. else
  664. {
  665. if (n2 > n0) i1 = 0;
  666. }
  667. }
  668. //
  669. // Check the intersection point is inside the triangle.
  670. //
  671. {
  672. float u1 = b[i0] - a[i0];
  673. float v1 = b[i1] - a[i1];
  674. float u2 = c[i0] - a[i0];
  675. float v2 = c[i1] - a[i1];
  676. float u0 = t * ray.getDirection()[i0] + ray.getOrigin()[i0] - a[i0];
  677. float v0 = t * ray.getDirection()[i1] + ray.getOrigin()[i1] - a[i1];
  678. float alpha = u0 * v2 - u2 * v0;
  679. float beta = u1 * v0 - u0 * v1;
  680. float area = u1 * v2 - u2 * v1;
  681. // epsilon to avoid float precision error
  682. const float EPSILON = 1e-6f;
  683. float tolerance = - EPSILON * area;
  684. if (area > 0)
  685. {
  686. if (alpha < tolerance || beta < tolerance || alpha+beta > area-tolerance)
  687. return std::pair<bool, float>(false, 0.0f);
  688. }
  689. else
  690. {
  691. if (alpha > tolerance || beta > tolerance || alpha+beta < area-tolerance)
  692. return std::pair<bool, float>(false, 0.0f);
  693. }
  694. }
  695. return std::pair<bool, float>(true, t);
  696. }
  697. //-----------------------------------------------------------------------
  698. std::pair<bool, float> Math::intersects(const Ray& ray, const Vector3& a,
  699. const Vector3& b, const Vector3& c,
  700. bool positiveSide, bool negativeSide)
  701. {
  702. Vector3 normal = calculateBasicFaceNormalWithoutNormalize(a, b, c);
  703. return intersects(ray, a, b, c, normal, positiveSide, negativeSide);
  704. }
  705. //-----------------------------------------------------------------------
  706. bool Math::intersects(const Sphere& sphere, const AABox& box)
  707. {
  708. // Use splitting planes
  709. const Vector3& center = sphere.getCenter();
  710. float radius = sphere.getRadius();
  711. const Vector3& min = box.getMin();
  712. const Vector3& max = box.getMax();
  713. // Arvo's algorithm
  714. float s, d = 0;
  715. for (int i = 0; i < 3; ++i)
  716. {
  717. if (center.ptr()[i] < min.ptr()[i])
  718. {
  719. s = center.ptr()[i] - min.ptr()[i];
  720. d += s * s;
  721. }
  722. else if(center.ptr()[i] > max.ptr()[i])
  723. {
  724. s = center.ptr()[i] - max.ptr()[i];
  725. d += s * s;
  726. }
  727. }
  728. return d <= radius * radius;
  729. }
  730. //-----------------------------------------------------------------------
  731. bool Math::intersects(const Plane& plane, const AABox& box)
  732. {
  733. return (plane.getSide(box) == Plane::BOTH_SIDE);
  734. }
  735. //-----------------------------------------------------------------------
  736. bool Math::intersects(const Sphere& sphere, const Plane& plane)
  737. {
  738. return (
  739. Math::Abs(plane.getDistance(sphere.getCenter()))
  740. <= sphere.getRadius() );
  741. }
  742. //-----------------------------------------------------------------------
  743. Vector3 Math::calculateTangentSpaceVector(
  744. const Vector3& position1, const Vector3& position2, const Vector3& position3,
  745. float u1, float v1, float u2, float v2, float u3, float v3)
  746. {
  747. //side0 is the vector along one side of the triangle of vertices passed in,
  748. //and side1 is the vector along another side. Taking the cross product of these returns the normal.
  749. Vector3 side0 = position1 - position2;
  750. Vector3 side1 = position3 - position1;
  751. //Calculate face normal
  752. Vector3 normal = side1.crossProduct(side0);
  753. normal.normalize();
  754. //Now we use a formula to calculate the tangent.
  755. float deltaV0 = v1 - v2;
  756. float deltaV1 = v3 - v1;
  757. Vector3 tangent = deltaV1 * side0 - deltaV0 * side1;
  758. tangent.normalize();
  759. //Calculate binormal
  760. float deltaU0 = u1 - u2;
  761. float deltaU1 = u3 - u1;
  762. Vector3 binormal = deltaU1 * side0 - deltaU0 * side1;
  763. binormal.normalize();
  764. //Now, we take the cross product of the tangents to get a vector which
  765. //should point in the same direction as our normal calculated above.
  766. //If it points in the opposite direction (the dot product between the normals is less than zero),
  767. //then we need to reverse the s and t tangents.
  768. //This is because the triangle has been mirrored when going from tangent space to object space.
  769. //reverse tangents if necessary
  770. Vector3 tangentCross = tangent.crossProduct(binormal);
  771. if (tangentCross.dotProduct(normal) < 0.0f)
  772. {
  773. tangent = -tangent;
  774. binormal = -binormal;
  775. }
  776. return tangent;
  777. }
  778. //-----------------------------------------------------------------------
  779. Matrix4 Math::buildReflectionMatrix(const Plane& p)
  780. {
  781. return Matrix4(
  782. -2 * p.normal.x * p.normal.x + 1, -2 * p.normal.x * p.normal.y, -2 * p.normal.x * p.normal.z, -2 * p.normal.x * p.d,
  783. -2 * p.normal.y * p.normal.x, -2 * p.normal.y * p.normal.y + 1, -2 * p.normal.y * p.normal.z, -2 * p.normal.y * p.d,
  784. -2 * p.normal.z * p.normal.x, -2 * p.normal.z * p.normal.y, -2 * p.normal.z * p.normal.z + 1, -2 * p.normal.z * p.d,
  785. 0, 0, 0, 1);
  786. }
  787. //-----------------------------------------------------------------------
  788. Vector4 Math::calculateFaceNormal(const Vector3& v1, const Vector3& v2, const Vector3& v3)
  789. {
  790. Vector3 normal = calculateBasicFaceNormal(v1, v2, v3);
  791. // Now set up the w (distance of tri from origin
  792. return Vector4(normal.x, normal.y, normal.z, -(normal.dotProduct(v1)));
  793. }
  794. //-----------------------------------------------------------------------
  795. Vector3 Math::calculateBasicFaceNormal(const Vector3& v1, const Vector3& v2, const Vector3& v3)
  796. {
  797. Vector3 normal = (v2 - v1).crossProduct(v3 - v1);
  798. normal.normalize();
  799. return normal;
  800. }
  801. //-----------------------------------------------------------------------
  802. Vector4 Math::calculateFaceNormalWithoutNormalize(const Vector3& v1, const Vector3& v2, const Vector3& v3)
  803. {
  804. Vector3 normal = calculateBasicFaceNormalWithoutNormalize(v1, v2, v3);
  805. // Now set up the w (distance of tri from origin)
  806. return Vector4(normal.x, normal.y, normal.z, -(normal.dotProduct(v1)));
  807. }
  808. //-----------------------------------------------------------------------
  809. Vector3 Math::calculateBasicFaceNormalWithoutNormalize(const Vector3& v1, const Vector3& v2, const Vector3& v3)
  810. {
  811. Vector3 normal = (v2 - v1).crossProduct(v3 - v1);
  812. return normal;
  813. }
  814. //-----------------------------------------------------------------------
  815. float Math::gaussianDistribution(float x, float offset, float scale)
  816. {
  817. float nom = Math::Exp(
  818. -Math::Sqr(x - offset) / (2 * Math::Sqr(scale)));
  819. float denom = scale * Math::Sqrt(2 * Math::PI);
  820. return nom / denom;
  821. }
  822. //---------------------------------------------------------------------
  823. Matrix4 Math::makeViewMatrix(const Vector3& position, const Quaternion& orientation,
  824. const Matrix4* reflectMatrix)
  825. {
  826. Matrix4 viewMatrix;
  827. // View matrix is:
  828. //
  829. // [ Lx Uy Dz Tx ]
  830. // [ Lx Uy Dz Ty ]
  831. // [ Lx Uy Dz Tz ]
  832. // [ 0 0 0 1 ]
  833. //
  834. // Where T = -(Transposed(Rot) * Pos)
  835. // This is most efficiently done using 3x3 Matrices
  836. Matrix3 rot;
  837. orientation.ToRotationMatrix(rot);
  838. // Make the translation relative to new axes
  839. Matrix3 rotT = rot.transpose();
  840. Vector3 trans = -rotT * position;
  841. // Make final matrix
  842. viewMatrix = Matrix4::IDENTITY;
  843. viewMatrix = rotT; // fills upper 3x3
  844. viewMatrix[0][3] = trans.x;
  845. viewMatrix[1][3] = trans.y;
  846. viewMatrix[2][3] = trans.z;
  847. // Deal with reflections
  848. if (reflectMatrix)
  849. {
  850. viewMatrix = viewMatrix * (*reflectMatrix);
  851. }
  852. return viewMatrix;
  853. }
  854. //---------------------------------------------------------------------
  855. float Math::boundingRadiusFromAABB(const AABox& aabb)
  856. {
  857. Vector3 max = aabb.getMax();
  858. Vector3 min = aabb.getMin();
  859. Vector3 magnitude = max;
  860. magnitude.makeCeil(-max);
  861. magnitude.makeCeil(min);
  862. magnitude.makeCeil(-min);
  863. return magnitude.length();
  864. }
  865. }