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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 helDist3v(vec3_t a, vec3_t b)
  74. {
  75. return (sqrtf( ((b[0] - a[0]) * (b[0] - a[0])) +
  76. ((b[1] - a[1]) * (b[1] - a[1])) +
  77. ((b[2] - a[2]) * (b[2] - a[2]))));
  78. }
  79. void helMidpoint3v(vec3_t a, vec3_t b, vec3_t mid)
  80. {
  81. mid[0] = (a[0] + b[0]) / 2.0f;
  82. mid[1] = (a[1] + b[1]) / 2.0f;
  83. mid[2] = (a[2] + b[2]) / 2.0f;
  84. }
  85. vec_t helNorm4v(vec4_t v)
  86. {
  87. return (sqrtf(v[0]*v[0] + v[1]*v[1] + v[2]*v[2] + v[3]*v[3]));
  88. }
  89. vec_t helNorm3v(vec3_t v)
  90. {
  91. return (sqrtf(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]));
  92. }
  93. vec_t helNorm2v(vec2_t v)
  94. {
  95. return (sqrtf(v[0]*v[0] + v[1]*v[1]));
  96. }
  97. vec_t helRandomNum(vec_t from, vec_t to)
  98. {
  99. return from + ((to - from) * rand() / (RAND_MAX + 1.0f));
  100. }