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.

math.cpp 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*!
  2. *
  3. * \file src/utils/math.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 "Vector3d.h"
  12. #include "Matrix.h"
  13. #include "utils/math.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. inline vec_t square(vec_t a)
  21. {
  22. return a * a;
  23. }
  24. int helIntersectionLineAndPolygon(vec3_t intersect,
  25. vec3_t p1, vec3_t p2,
  26. vec3_t *polygon)
  27. {
  28. // vec3_t normal, a, b;
  29. Vector3d a, b, normal, pA, pB;
  30. vec_t d, denominator, mu;
  31. pA = Vector3d(p1);
  32. pB = Vector3d(p2);
  33. // Find normal
  34. //mtkVectorSubtract(polygon[1], polygon[0], a);
  35. a = Vector3d(polygon[1]) - Vector3d(polygon[0]);
  36. //mtkVectorSubtract(polygon[2], polygon[0], b);
  37. b = Vector3d(polygon[2]) - Vector3d(polygon[0]);
  38. normal = Vector3d::cross(a, b);
  39. //mtkVectorCrossProduct(a, b, normal);
  40. normal.normalize();
  41. //mtkVectorNormalize(normal, normal);
  42. // find D
  43. //d = (normal[0] * polygon[0][0] -
  44. // normal[1] * polygon[0][1] -
  45. // normal[2] * polygon[0][2]);
  46. d = (normal.mVec[0] * polygon[0][0] -
  47. normal.mVec[1] * polygon[0][1] -
  48. normal.mVec[2] * polygon[0][2]);
  49. // line segment parallel to plane?
  50. //mtkVectorSubtract(p2, p1, a); // cache p2 - p1 => a
  51. a = pB - pA;
  52. //denominator = (normal[0] * a[0] +
  53. // normal[1] * a[1] +
  54. // normal[2] * a[2]);
  55. denominator = Vector3d::dot(normal, a);
  56. if (denominator > 0.0)
  57. return 0;
  58. // Line segment contains intercept point?
  59. //mu = - ((d + normal[0] * p1[0] + normal[1] * p1[1] + normal[2] * p1[2]) /
  60. // denominator);
  61. mu = -((d + Vector3d::dot(normal, pA)) / denominator);
  62. if (mu < 0.0 || mu > 1.0)
  63. return 0;
  64. //intersect[0] = p1[0] + mu * a[0];
  65. //intersect[1] = p1[1] + mu * a[1];
  66. //intersect[2] = p1[2] + mu * a[2];
  67. b = pA + (a * mu);
  68. intersect[0] = b.mVec[0];
  69. intersect[1] = b.mVec[1];
  70. intersect[2] = b.mVec[2];
  71. // See if the intercept is bound by polygon by winding number
  72. #ifdef WINDING_NUMBERS_TRIANGLE
  73. mtkVectorSubtract(polygon[0], intersect, a);
  74. mtkVectorNormalize(a, a);
  75. mtkVectorSubtract(polygon[1], intersect, b);
  76. mtkVectorNormalize(b, b);
  77. mtkVectorSubtract(polygon[2], intersect, c);
  78. mtkVectorNormalize(c, c);
  79. t0 = mtkVectorDotProduct(a, b);
  80. t1 = mtkVectorDotProduct(b, c);
  81. t2 = mtkVectorDotProduct(c, a);
  82. total = HEL_RAD_TO_DEG(acos(t0) + acos(t1) + acos(t2));
  83. if (total - 360 < 0.0)
  84. return 0;
  85. #else // assume convex polygons here for sure
  86. //mtkVectorSubtract(intersect, polygon[0], a);
  87. //theta = mtkVectorDotProduct(a, normal);
  88. double theta = Vector3d::dot(b - Vector3d(polygon[0]), normal); // b = intersect
  89. if (theta >= 90.0) // Yeah I know
  90. return 0;
  91. #endif
  92. return 1;
  93. }
  94. vec_t helDistToSphereFromPlane3v(vec3_t center, vec_t radius, vec4_t plane)
  95. {
  96. vec_t d;
  97. d = (plane[0] * center[0] +
  98. plane[1] * center[1] +
  99. plane[2] * center[2] +
  100. plane[3]);
  101. if (d <= -radius)
  102. return 0;
  103. return d + radius;
  104. }
  105. vec_t helDistToBboxFromPlane3v(vec3_t min, vec3_t max, vec4_t plane)
  106. {
  107. vec3_t center;
  108. vec_t d, radius;
  109. helMidpoint3v(min, max, center);
  110. d = (plane[0] * center[0] +
  111. plane[1] * center[1] +
  112. plane[2] * center[2] +
  113. plane[3]);
  114. radius = helDist3v(max, center);
  115. if (d <= -radius)
  116. return 0;
  117. return d + radius;
  118. }
  119. vec_t helDist3v(vec3_t a, vec3_t b)
  120. {
  121. return (sqrtf( ((b[0] - a[0]) * (b[0] - a[0])) +
  122. ((b[1] - a[1]) * (b[1] - a[1])) +
  123. ((b[2] - a[2]) * (b[2] - a[2]))));
  124. }
  125. void helMidpoint3v(vec3_t a, vec3_t b, vec3_t mid)
  126. {
  127. mid[0] = (a[0] + b[0]) / 2.0f;
  128. mid[1] = (a[1] + b[1]) / 2.0f;
  129. mid[2] = (a[2] + b[2]) / 2.0f;
  130. }
  131. vec_t helNorm4v(vec4_t v)
  132. {
  133. return (sqrtf(v[0]*v[0] + v[1]*v[1] + v[2]*v[2] + v[3]*v[3]));
  134. }
  135. vec_t helNorm3v(vec3_t v)
  136. {
  137. return (sqrtf(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]));
  138. }
  139. vec_t helNorm2v(vec2_t v)
  140. {
  141. return (sqrtf(v[0]*v[0] + v[1]*v[1]));
  142. }
  143. vec_t helRandomNum(vec_t from, vec_t to)
  144. {
  145. return from + ((to - from) * rand() / (RAND_MAX + 1.0f));
  146. }