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 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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) <= (fmax(fabs(a), fabs(b)) * 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. a = Vector3d(polygon[1]) - Vector3d(polygon[0]);
  35. b = Vector3d(polygon[2]) - Vector3d(polygon[0]);
  36. normal = Vector3d::cross(a, b);
  37. normal.normalize();
  38. // find D
  39. //d = (normal[0] * polygon[0][0] -
  40. // normal[1] * polygon[0][1] -
  41. // normal[2] * polygon[0][2]);
  42. d = (normal.mVec[0] * polygon[0][0] -
  43. normal.mVec[1] * polygon[0][1] -
  44. normal.mVec[2] * polygon[0][2]);
  45. // line segment parallel to plane?
  46. a = pB - pA;
  47. //denominator = (normal[0] * a[0] +
  48. // normal[1] * a[1] +
  49. // normal[2] * a[2]);
  50. denominator = Vector3d::dot(normal, a);
  51. if (denominator > 0.0)
  52. return 0;
  53. // Line segment contains intercept point?
  54. //mu = - ((d + normal[0] * p1[0] + normal[1] * p1[1] + normal[2] * p1[2]) /
  55. // denominator);
  56. mu = -((d + Vector3d::dot(normal, pA)) / denominator);
  57. if (mu < 0.0 || mu > 1.0)
  58. return 0;
  59. //intersect[0] = p1[0] + mu * a[0];
  60. //intersect[1] = p1[1] + mu * a[1];
  61. //intersect[2] = p1[2] + mu * a[2];
  62. b = pA + (a * mu);
  63. intersect[0] = b.mVec[0];
  64. intersect[1] = b.mVec[1];
  65. intersect[2] = b.mVec[2];
  66. // See if the intercept is bound by polygon by winding number
  67. // assume convex polygons here for sure
  68. double theta = Vector3d::dot(b - Vector3d(polygon[0]), normal); // b = intersect
  69. if (theta >= 90.0) // Yeah I know
  70. return 0;
  71. return 1;
  72. }
  73. vec_t helDistToSphereFromPlane3v(vec3_t center, vec_t radius, vec4_t plane)
  74. {
  75. vec_t d;
  76. d = (plane[0] * center[0] +
  77. plane[1] * center[1] +
  78. plane[2] * center[2] +
  79. plane[3]);
  80. if (d <= -radius)
  81. return 0;
  82. return d + radius;
  83. }
  84. vec_t helDistToBboxFromPlane3v(vec3_t min, vec3_t max, vec4_t plane)
  85. {
  86. vec3_t center;
  87. vec_t d, radius;
  88. helMidpoint3v(min, max, center);
  89. d = (plane[0] * center[0] +
  90. plane[1] * center[1] +
  91. plane[2] * center[2] +
  92. plane[3]);
  93. radius = helDist3v(max, center);
  94. if (d <= -radius)
  95. return 0;
  96. return d + radius;
  97. }
  98. vec_t helDist3v(vec3_t a, vec3_t b)
  99. {
  100. return (sqrtf( ((b[0] - a[0]) * (b[0] - a[0])) +
  101. ((b[1] - a[1]) * (b[1] - a[1])) +
  102. ((b[2] - a[2]) * (b[2] - a[2]))));
  103. }
  104. void helMidpoint3v(vec3_t a, vec3_t b, vec3_t mid)
  105. {
  106. mid[0] = (a[0] + b[0]) / 2.0f;
  107. mid[1] = (a[1] + b[1]) / 2.0f;
  108. mid[2] = (a[2] + b[2]) / 2.0f;
  109. }
  110. vec_t helNorm4v(vec4_t v)
  111. {
  112. return (sqrtf(v[0]*v[0] + v[1]*v[1] + v[2]*v[2] + v[3]*v[3]));
  113. }
  114. vec_t helNorm3v(vec3_t v)
  115. {
  116. return (sqrtf(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]));
  117. }
  118. vec_t helNorm2v(vec2_t v)
  119. {
  120. return (sqrtf(v[0]*v[0] + v[1]*v[1]));
  121. }
  122. vec_t helRandomNum(vec_t from, vec_t to)
  123. {
  124. return from + ((to - from) * rand() / (RAND_MAX + 1.0f));
  125. }