Open Source Tomb Raider Engine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MatMath.cpp 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*!
  2. *
  3. * \file src/MatMath.cpp
  4. * \brief Vector and Matrix math
  5. *
  6. * \author Mongoose
  7. */
  8. #include <stdlib.h>
  9. #include <math.h>
  10. #include <float.h>
  11. #include <MatMath.h>
  12. #include <Vector3d.h>
  13. #include <Matrix.h>
  14. bool equalEpsilon(vec_t a, vec_t b) {
  15. vec_t epsilon = FLT_EPSILON;
  16. if (fabs(a - b) <= (((fabs(b) > fabs(a)) ? fabs(b) : fabs(a)) * epsilon))
  17. return true;
  18. return false;
  19. }
  20. vec_t helIntersectionOfAbstractSpheres(vec3_t centerA, vec_t radiusA,
  21. vec3_t centerB, vec_t radiusB)
  22. {
  23. Vector3d a = Vector3d(centerA);
  24. Vector3d b = Vector3d(centerB);
  25. Vector3d d = a - b;
  26. vec_t dist, minDist;
  27. dist = Vector3d::dot(d, d);
  28. minDist = radiusA + radiusB;
  29. return (dist <= minDist * minDist);
  30. }
  31. inline vec_t square(vec_t a)
  32. {
  33. return a * a;
  34. }
  35. // Returns number of intersections and intersection position(s)
  36. // Got algorithm from http://astronomy.swin.edu.au/~pbourke/geometry/
  37. int helIntersectionOfAbstractSphereAndLine(vec3_t center, vec_t radius,
  38. vec3_t posA, vec3_t posB,
  39. vec3_t intersectionA,
  40. vec3_t intersectionB)
  41. {
  42. // float x , y , z;
  43. vec_t a, b, c, mu, i ;
  44. a = (square(posB[0] - posA[0]) +
  45. square(posB[1] - posA[1]) +
  46. square(posB[2] - posA[2]));
  47. b = (2 * ((posB[0] - posA[0]) * (posA[0] - center[0]) +
  48. (posB[1] - posA[1]) * (posA[1] - center[1]) +
  49. (posB[2] - posA[2]) * (posA[2] - center[2])));
  50. c = (square(center[0]) + square(center[1]) +
  51. square(center[2]) + square(posA[0]) +
  52. square(posA[1]) + square(posA[2]) -
  53. 2 * (center[0]*posA[0] + center[1]*posA[1] + center[2]*posA[2]) -
  54. square(radius));
  55. i = b * b - 4 * a * c;
  56. if (i < 0.0)
  57. {
  58. // No intersection
  59. return 0;
  60. }
  61. else if (i == 0.0)
  62. {
  63. // One intersection
  64. mu = -b/(2*a) ;
  65. intersectionA[0] = posA[0] + mu*(posB[0]-posA[0]);
  66. intersectionA[1] = posA[1] + mu*(posB[1]-posA[1]);
  67. intersectionA[2] = posA[2] + mu*(posB[2]-posA[2]);
  68. return 1;
  69. }
  70. else
  71. {
  72. // Two intersections
  73. // First intersection
  74. mu = (-b + sqrtf( square(b) - 4.0f*a*c)) / (2.0f*a);
  75. intersectionA[0] = posA[0] + mu*(posB[0]-posA[0]);
  76. intersectionA[1] = posA[1] + mu*(posB[1]-posA[1]);
  77. intersectionA[2] = posA[2] + mu*(posB[2]-posA[2]);
  78. // Second intersection
  79. mu = (-b - sqrtf(square(b) - 4.0f*a*c)) / (2.0f*a);
  80. intersectionB[0] = posA[0] + mu*(posB[0]-posA[0]);
  81. intersectionB[1] = posA[1] + mu*(posB[1]-posA[1]);
  82. intersectionB[2] = posA[2] + mu*(posB[2]-posA[2]);
  83. return 2;
  84. }
  85. }
  86. int helIntersectionLineAndPolygon(vec3_t intersect,
  87. vec3_t p1, vec3_t p2,
  88. vec3_t *polygon)
  89. {
  90. // vec3_t normal, a, b;
  91. Vector3d a, b, normal, pA, pB;
  92. vec_t d, denominator, mu;
  93. double theta;
  94. pA = Vector3d(p1);
  95. pB = Vector3d(p2);
  96. // Find normal
  97. //mtkVectorSubtract(polygon[1], polygon[0], a);
  98. a = Vector3d(polygon[1]) - Vector3d(polygon[0]);
  99. //mtkVectorSubtract(polygon[2], polygon[0], b);
  100. b = Vector3d(polygon[2]) - Vector3d(polygon[0]);
  101. normal = Vector3d::cross(a, b);
  102. //mtkVectorCrossProduct(a, b, normal);
  103. normal.normalize();
  104. //mtkVectorNormalize(normal, normal);
  105. // find D
  106. //d = (normal[0] * polygon[0][0] -
  107. // normal[1] * polygon[0][1] -
  108. // normal[2] * polygon[0][2]);
  109. d = (normal.mVec[0] * polygon[0][0] -
  110. normal.mVec[1] * polygon[0][1] -
  111. normal.mVec[2] * polygon[0][2]);
  112. // line segment parallel to plane?
  113. //mtkVectorSubtract(p2, p1, a); // cache p2 - p1 => a
  114. a = pB - pA;
  115. //denominator = (normal[0] * a[0] +
  116. // normal[1] * a[1] +
  117. // normal[2] * a[2]);
  118. denominator = Vector3d::dot(normal, a);
  119. if (denominator > 0.0)
  120. return 0;
  121. // Line segment contains intercept point?
  122. //mu = - ((d + normal[0] * p1[0] + normal[1] * p1[1] + normal[2] * p1[2]) /
  123. // denominator);
  124. mu = -((d + Vector3d::dot(normal, pA)) / denominator);
  125. if (mu < 0.0 || mu > 1.0)
  126. return 0;
  127. //intersect[0] = p1[0] + mu * a[0];
  128. //intersect[1] = p1[1] + mu * a[1];
  129. //intersect[2] = p1[2] + mu * a[2];
  130. b = pA + (a * mu);
  131. intersect[0] = b.mVec[0];
  132. intersect[1] = b.mVec[1];
  133. intersect[2] = b.mVec[2];
  134. // See if the intercept is bound by polygon by winding number
  135. #ifdef WINDING_NUMBERS_TRIANGLE
  136. mtkVectorSubtract(polygon[0], intersect, a);
  137. mtkVectorNormalize(a, a);
  138. mtkVectorSubtract(polygon[1], intersect, b);
  139. mtkVectorNormalize(b, b);
  140. mtkVectorSubtract(polygon[2], intersect, c);
  141. mtkVectorNormalize(c, c);
  142. t0 = mtkVectorDotProduct(a, b);
  143. t1 = mtkVectorDotProduct(b, c);
  144. t2 = mtkVectorDotProduct(c, a);
  145. total = HEL_RAD_TO_DEG(acos(t0) + acos(t1) + acos(t2));
  146. if (total - 360 < 0.0)
  147. return 0;
  148. #else // assume convex polygons here for sure
  149. //mtkVectorSubtract(intersect, polygon[0], a);
  150. //theta = mtkVectorDotProduct(a, normal);
  151. theta = Vector3d::dot(b - Vector3d(polygon[0]), normal); // b = intersect
  152. if (theta >= 90.0) // Yeah I know
  153. return 0;
  154. #endif
  155. return 1;
  156. }
  157. vec_t helDistToSphereFromPlane3v(vec3_t center, vec_t radius, vec4_t plane)
  158. {
  159. vec_t d;
  160. d = (plane[0] * center[0] +
  161. plane[1] * center[1] +
  162. plane[2] * center[2] +
  163. plane[3]);
  164. if (d <= -radius)
  165. return 0;
  166. return d + radius;
  167. }
  168. vec_t helDistToBboxFromPlane3v(vec3_t min, vec3_t max, vec4_t plane)
  169. {
  170. vec3_t center;
  171. vec_t d, radius;
  172. helMidpoint3v(min, max, center);
  173. d = (plane[0] * center[0] +
  174. plane[1] * center[1] +
  175. plane[2] * center[2] +
  176. plane[3]);
  177. radius = helDist3v(max, center);
  178. if (d <= -radius)
  179. return 0;
  180. return d + radius;
  181. }
  182. vec_t helDist3v(vec3_t a, vec3_t b)
  183. {
  184. return (sqrtf( ((b[0] - a[0]) * (b[0] - a[0])) +
  185. ((b[1] - a[1]) * (b[1] - a[1])) +
  186. ((b[2] - a[2]) * (b[2] - a[2]))));
  187. }
  188. void helMidpoint3v(vec3_t a, vec3_t b, vec3_t mid)
  189. {
  190. mid[0] = (a[0] + b[0]) / 2.0f;
  191. mid[1] = (a[1] + b[1]) / 2.0f;
  192. mid[2] = (a[2] + b[2]) / 2.0f;
  193. }
  194. vec_t helNorm4v(vec4_t v)
  195. {
  196. return (sqrtf(v[0]*v[0] + v[1]*v[1] + v[2]*v[2] + v[3]*v[3]));
  197. }
  198. vec_t helNorm3v(vec3_t v)
  199. {
  200. return (sqrtf(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]));
  201. }
  202. vec_t helNorm2v(vec2_t v)
  203. {
  204. return (sqrtf(v[0]*v[0] + v[1]*v[1]));
  205. }
  206. vec_t helRandomNum(vec_t from, vec_t to)
  207. {
  208. return from + ((to - from) * rand() / (RAND_MAX + 1.0f));
  209. }