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

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