BsMath.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "Math/BsMath.h"
  4. #include "Math/BsVector2.h"
  5. #include "Math/BsVector3.h"
  6. #include "Math/BsVector4.h"
  7. #include "Math/BsQuaternion.h"
  8. namespace bs
  9. {
  10. const float Math::LOG2 = std::log(2.0f);
  11. Radian Math::acos(float val)
  12. {
  13. if (-1.0f < val)
  14. {
  15. if (val < 1.0f)
  16. return Radian(std::acos(val));
  17. else
  18. return Radian(0.0f);
  19. }
  20. else
  21. {
  22. return Radian(PI);
  23. }
  24. }
  25. Radian Math::asin(float val)
  26. {
  27. if (-1.0f < val)
  28. {
  29. if (val < 1.0f)
  30. return Radian(std::asin(val));
  31. else
  32. return Radian(HALF_PI);
  33. }
  34. else
  35. {
  36. return Radian(-HALF_PI);
  37. }
  38. }
  39. float Math::sign(float val)
  40. {
  41. if (val > 0.0f)
  42. return 1.0f;
  43. if (val < 0.0f)
  44. return -1.0f;
  45. return 0.0f;
  46. }
  47. float Math::invSqrt(float val)
  48. {
  49. return 1.0f/sqrt(val);
  50. }
  51. float Math::fastSin0(float val)
  52. {
  53. float angleSqr = val*val;
  54. float result = 7.61e-03f;
  55. result *= angleSqr;
  56. result -= 1.6605e-01f;
  57. result *= angleSqr;
  58. result += 1.0f;
  59. result *= val;
  60. return result;
  61. }
  62. float Math::fastSin1(float val)
  63. {
  64. float angleSqr = val*val;
  65. float result = -2.39e-08f;
  66. result *= angleSqr;
  67. result += 2.7526e-06f;
  68. result *= angleSqr;
  69. result -= 1.98409e-04f;
  70. result *= angleSqr;
  71. result += 8.3333315e-03f;
  72. result *= angleSqr;
  73. result -= 1.666666664e-01f;
  74. result *= angleSqr;
  75. result += 1.0f;
  76. result *= val;
  77. return result;
  78. }
  79. float Math::fastCos0(float val)
  80. {
  81. float angleSqr = val*val;
  82. float result = 3.705e-02f;
  83. result *= angleSqr;
  84. result -= 4.967e-01f;
  85. result *= angleSqr;
  86. result += 1.0f;
  87. return result;
  88. }
  89. float Math::fastCos1(float val)
  90. {
  91. float angleSqr = val*val;
  92. float result = -2.605e-07f;
  93. result *= angleSqr;
  94. result += 2.47609e-05f;
  95. result *= angleSqr;
  96. result -= 1.3888397e-03f;
  97. result *= angleSqr;
  98. result += 4.16666418e-02f;
  99. result *= angleSqr;
  100. result -= 4.999999963e-01f;
  101. result *= angleSqr;
  102. result += 1.0f;
  103. return result;
  104. }
  105. float Math::fastTan0(float val)
  106. {
  107. float angleSqr = val*val;
  108. float result = 2.033e-01f;
  109. result *= angleSqr;
  110. result += 3.1755e-01f;
  111. result *= angleSqr;
  112. result += 1.0f;
  113. result *= val;
  114. return result;
  115. }
  116. float Math::fastTan1(float val)
  117. {
  118. float angleSqr = val*val;
  119. float result = 9.5168091e-03f;
  120. result *= angleSqr;
  121. result += 2.900525e-03f;
  122. result *= angleSqr;
  123. result += 2.45650893e-02f;
  124. result *= angleSqr;
  125. result += 5.33740603e-02f;
  126. result *= angleSqr;
  127. result += 1.333923995e-01f;
  128. result *= angleSqr;
  129. result += 3.333314036e-01f;
  130. result *= angleSqr;
  131. result += 1.0f;
  132. result *= val;
  133. return result;
  134. }
  135. float Math::fastASin0(float val)
  136. {
  137. float root = sqrt(abs(1.0f - val));
  138. float result = -0.0187293f;
  139. result *= val;
  140. result += 0.0742610f;
  141. result *= val;
  142. result -= 0.2121144f;
  143. result *= val;
  144. result += 1.5707288f;
  145. result = HALF_PI - root*result;
  146. return result;
  147. }
  148. float Math::fastASin1(float val)
  149. {
  150. float root = sqrt(abs(1.0f - val));
  151. float result = -0.0012624911f;
  152. result *= val;
  153. result += 0.0066700901f;
  154. result *= val;
  155. result -= 0.0170881256f;
  156. result *= val;
  157. result += 0.0308918810f;
  158. result *= val;
  159. result -= 0.0501743046f;
  160. result *= val;
  161. result += 0.0889789874f;
  162. result *= val;
  163. result -= 0.2145988016f;
  164. result *= val;
  165. result += 1.5707963050f;
  166. result = HALF_PI - root*result;
  167. return result;
  168. }
  169. float Math::fastACos0(float val)
  170. {
  171. float root = sqrt(abs(1.0f - val));
  172. float result = -0.0187293f;
  173. result *= val;
  174. result += 0.0742610f;
  175. result *= val;
  176. result -= 0.2121144f;
  177. result *= val;
  178. result += 1.5707288f;
  179. result *= root;
  180. return result;
  181. }
  182. float Math::fastACos1(float val)
  183. {
  184. float root = sqrt(abs(1.0f - val));
  185. float result = -0.0012624911f;
  186. result *= val;
  187. result += 0.0066700901f;
  188. result *= val;
  189. result -= 0.0170881256f;
  190. result *= val;
  191. result += 0.0308918810f;
  192. result *= val;
  193. result -= 0.0501743046f;
  194. result *= val;
  195. result += 0.0889789874f;
  196. result *= val;
  197. result -= 0.2145988016f;
  198. result *= val;
  199. result += 1.5707963050f;
  200. result *= root;
  201. return result;
  202. }
  203. float Math::fastATan0(float val)
  204. {
  205. float valueSqr = val*val;
  206. float result = 0.0208351f;
  207. result *= valueSqr;
  208. result -= 0.085133f;
  209. result *= valueSqr;
  210. result += 0.180141f;
  211. result *= valueSqr;
  212. result -= 0.3302995f;
  213. result *= valueSqr;
  214. result += 0.999866f;
  215. result *= val;
  216. return result;
  217. }
  218. float Math::fastATan1(float val)
  219. {
  220. float valueSqr = val*val;
  221. float result = 0.0028662257f;
  222. result *= valueSqr;
  223. result -= 0.0161657367f;
  224. result *= valueSqr;
  225. result += 0.0429096138f;
  226. result *= valueSqr;
  227. result -= 0.0752896400f;
  228. result *= valueSqr;
  229. result += 0.1065626393f;
  230. result *= valueSqr;
  231. result -= 0.1420889944f;
  232. result *= valueSqr;
  233. result += 0.1999355085f;
  234. result *= valueSqr;
  235. result -= 0.3333314528f;
  236. result *= valueSqr;
  237. result += 1.0f;
  238. result *= val;
  239. return result;
  240. }
  241. inline bool Math::approxEquals(const Vector2& a, const Vector2& b, float tolerance)
  242. {
  243. return fabs(b.x - a.x) <= tolerance && fabs(b.y - a.y) <= tolerance;
  244. }
  245. inline bool Math::approxEquals(const Vector3& a, const Vector3& b, float tolerance)
  246. {
  247. return fabs(b.x - a.x) <= tolerance && fabs(b.y - a.y) <= tolerance && fabs(b.z - a.z) <= tolerance;
  248. }
  249. inline bool Math::approxEquals(const Vector4& a, const Vector4& b, float tolerance)
  250. {
  251. return fabs(b.x - a.x) <= tolerance && fabs(b.y - a.y) <= tolerance && fabs(b.z - a.z) <= tolerance &&
  252. fabs(b.w - a.w) <= tolerance;
  253. }
  254. inline bool Math::approxEquals(const Quaternion& a, const Quaternion& b, float tolerance)
  255. {
  256. return fabs(b.x - a.x) <= tolerance && fabs(b.y - a.y) <= tolerance && fabs(b.z - a.z) <= tolerance &&
  257. fabs(b.w - a.w) <= tolerance;
  258. }
  259. Vector3 Math::calculateTriTangent(const Vector3& position1, const Vector3& position2,
  260. const Vector3& position3, float u1, float v1, float u2, float v2, float u3, float v3)
  261. {
  262. Vector3 side0 = position1 - position2;
  263. Vector3 side1 = position3 - position1;
  264. // Calculate face normal
  265. Vector3 normal = side1.cross(side0);
  266. normal.normalize();
  267. // Now we use a formula to calculate the tangent.
  268. float deltaV0 = v1 - v2;
  269. float deltaV1 = v3 - v1;
  270. Vector3 tangent = deltaV1 * side0 - deltaV0 * side1;
  271. tangent.normalize();
  272. // Calculate binormal
  273. float deltaU0 = u1 - u2;
  274. float deltaU1 = u3 - u1;
  275. Vector3 binormal = deltaU1 * side0 - deltaU0 * side1;
  276. binormal.normalize();
  277. // Now, we take the cross product of the tangents to get a vector which
  278. // should point in the same direction as our normal calculated above.
  279. // If it points in the opposite direction (the dot product between the normals is less than zero),
  280. // then we need to reverse the s and t tangents.
  281. // This is because the triangle has been mirrored when going from tangent space to object space.
  282. // reverse tangents if necessary.
  283. Vector3 tangentCross = tangent.cross(binormal);
  284. if (tangentCross.dot(normal) < 0.0f)
  285. {
  286. tangent = -tangent;
  287. binormal = -binormal;
  288. }
  289. return tangent;
  290. }
  291. }