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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 <assert.h>
  12. #include "Vector3d.h"
  13. #include "Matrix.h"
  14. #include "utils/math.h"
  15. bool equalEpsilon(vec_t a, vec_t b) {
  16. vec_t epsilon = FLT_EPSILON;
  17. if (fabs(a - b) <= (fmax(fabs(a), fabs(b)) * epsilon))
  18. return true;
  19. return false;
  20. }
  21. inline vec_t square(vec_t a)
  22. {
  23. return a * a;
  24. }
  25. int helIntersectionLineAndPolygon(vec3_t intersect,
  26. vec3_t p1, vec3_t p2,
  27. vec3_t *polygon)
  28. {
  29. assert(polygon != NULL);
  30. // vec3_t normal, a, b;
  31. Vector3d a, b, normal, pA, pB;
  32. vec_t d, denominator, mu;
  33. pA = Vector3d(p1);
  34. pB = Vector3d(p2);
  35. // Find normal
  36. a = Vector3d(polygon[1]) - Vector3d(polygon[0]);
  37. b = Vector3d(polygon[2]) - Vector3d(polygon[0]);
  38. normal = Vector3d::cross(a, b);
  39. normal.normalize();
  40. // find D
  41. //d = (normal[0] * polygon[0][0] -
  42. // normal[1] * polygon[0][1] -
  43. // normal[2] * polygon[0][2]);
  44. d = (normal.mVec[0] * polygon[0][0] -
  45. normal.mVec[1] * polygon[0][1] -
  46. normal.mVec[2] * polygon[0][2]);
  47. // line segment parallel to plane?
  48. a = pB - pA;
  49. //denominator = (normal[0] * a[0] +
  50. // normal[1] * a[1] +
  51. // normal[2] * a[2]);
  52. denominator = Vector3d::dot(normal, a);
  53. if (denominator > 0.0)
  54. return 0;
  55. // Line segment contains intercept point?
  56. //mu = - ((d + normal[0] * p1[0] + normal[1] * p1[1] + normal[2] * p1[2]) /
  57. // denominator);
  58. mu = -((d + Vector3d::dot(normal, pA)) / denominator);
  59. if (mu < 0.0 || mu > 1.0)
  60. return 0;
  61. //intersect[0] = p1[0] + mu * a[0];
  62. //intersect[1] = p1[1] + mu * a[1];
  63. //intersect[2] = p1[2] + mu * a[2];
  64. b = pA + (a * mu);
  65. intersect[0] = b.mVec[0];
  66. intersect[1] = b.mVec[1];
  67. intersect[2] = b.mVec[2];
  68. // See if the intercept is bound by polygon by winding number
  69. // assume convex polygons here for sure
  70. double theta = Vector3d::dot(b - Vector3d(polygon[0]), normal); // b = intersect
  71. if (theta >= 90.0) // Yeah I know
  72. return 0;
  73. return 1;
  74. }
  75. vec_t helDist3v(vec3_t a, vec3_t b)
  76. {
  77. return (sqrtf( ((b[0] - a[0]) * (b[0] - a[0])) +
  78. ((b[1] - a[1]) * (b[1] - a[1])) +
  79. ((b[2] - a[2]) * (b[2] - a[2]))));
  80. }
  81. void helMidpoint3v(vec3_t a, vec3_t b, vec3_t mid)
  82. {
  83. mid[0] = (a[0] + b[0]) / 2.0f;
  84. mid[1] = (a[1] + b[1]) / 2.0f;
  85. mid[2] = (a[2] + b[2]) / 2.0f;
  86. }
  87. vec_t helNorm4v(vec4_t v)
  88. {
  89. return (sqrtf(v[0]*v[0] + v[1]*v[1] + v[2]*v[2] + v[3]*v[3]));
  90. }
  91. vec_t helNorm3v(vec3_t v)
  92. {
  93. return (sqrtf(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]));
  94. }
  95. vec_t helNorm2v(vec2_t v)
  96. {
  97. return (sqrtf(v[0]*v[0] + v[1]*v[1]));
  98. }
  99. vec_t helRandomNum(vec_t from, vec_t to)
  100. {
  101. return from + ((to - from) * rand() / (RAND_MAX + 1.0f));
  102. }