BsMath.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. //__________________________ Banshee Project - A modern game development toolkit _________________________________//
  2. //_____________________________________ www.banshee-project.com __________________________________________________//
  3. //________________________ Copyright (c) 2014 Marko Pintera. All rights reserved. ________________________________//
  4. #include "BsMath.h"
  5. #include "BsVector2.h"
  6. #include "BsVector3.h"
  7. #include "BsVector4.h"
  8. #include "BsRay.h"
  9. #include "BsSphere.h"
  10. #include "BsAABox.h"
  11. #include "BsPlane.h"
  12. namespace BansheeEngine
  13. {
  14. const float Math::POS_INFINITY = std::numeric_limits<float>::infinity();
  15. const float Math::NEG_INFINITY = -std::numeric_limits<float>::infinity();
  16. const float Math::PI = (float)4.0f * std::atan(1.0f);
  17. const float Math::TWO_PI = (float)(2.0f * PI);
  18. const float Math::HALF_PI = (float)(0.5f * PI);
  19. const float Math::DEG2RAD = PI / 180.0f;
  20. const float Math::RAD2DEG = 180.0f / PI;
  21. const float Math::LOG2 = std::log(2.0f);
  22. Radian Math::acos(float val)
  23. {
  24. if (-1.0f < val)
  25. {
  26. if (val < 1.0f)
  27. return Radian(std::acos(val));
  28. else
  29. return Radian(0.0f);
  30. }
  31. else
  32. {
  33. return Radian(PI);
  34. }
  35. }
  36. Radian Math::asin(float val)
  37. {
  38. if (-1.0f < val)
  39. {
  40. if (val < 1.0f)
  41. return Radian(std::asin(val));
  42. else
  43. return Radian(HALF_PI);
  44. }
  45. else
  46. {
  47. return Radian(-HALF_PI);
  48. }
  49. }
  50. float Math::sign(float val)
  51. {
  52. if (val > 0.0f)
  53. return 1.0f;
  54. if (val < 0.0f)
  55. return -1.0f;
  56. return 0.0f;
  57. }
  58. float Math::invSqrt(float val)
  59. {
  60. return 1.0f/sqrt(val);
  61. }
  62. float Math::fastSin0(float val)
  63. {
  64. float angleSqr = val*val;
  65. float result = 7.61e-03f;
  66. result *= angleSqr;
  67. result -= 1.6605e-01f;
  68. result *= angleSqr;
  69. result += 1.0f;
  70. result *= val;
  71. return result;
  72. }
  73. float Math::fastSin1(float val)
  74. {
  75. float angleSqr = val*val;
  76. float result = -2.39e-08f;
  77. result *= angleSqr;
  78. result += 2.7526e-06f;
  79. result *= angleSqr;
  80. result -= 1.98409e-04f;
  81. result *= angleSqr;
  82. result += 8.3333315e-03f;
  83. result *= angleSqr;
  84. result -= 1.666666664e-01f;
  85. result *= angleSqr;
  86. result += 1.0f;
  87. result *= val;
  88. return result;
  89. }
  90. float Math::fastCos0(float val)
  91. {
  92. float angleSqr = val*val;
  93. float result = 3.705e-02f;
  94. result *= angleSqr;
  95. result -= 4.967e-01f;
  96. result *= angleSqr;
  97. result += 1.0f;
  98. return result;
  99. }
  100. float Math::fastCos1(float val)
  101. {
  102. float angleSqr = val*val;
  103. float result = -2.605e-07f;
  104. result *= angleSqr;
  105. result += 2.47609e-05f;
  106. result *= angleSqr;
  107. result -= 1.3888397e-03f;
  108. result *= angleSqr;
  109. result += 4.16666418e-02f;
  110. result *= angleSqr;
  111. result -= 4.999999963e-01f;
  112. result *= angleSqr;
  113. result += 1.0f;
  114. return result;
  115. }
  116. float Math::fastTan0(float val)
  117. {
  118. float angleSqr = val*val;
  119. float result = 2.033e-01f;
  120. result *= angleSqr;
  121. result += 3.1755e-01f;
  122. result *= angleSqr;
  123. result += 1.0f;
  124. result *= val;
  125. return result;
  126. }
  127. float Math::fastTan1(float val)
  128. {
  129. float angleSqr = val*val;
  130. float result = 9.5168091e-03f;
  131. result *= angleSqr;
  132. result += 2.900525e-03f;
  133. result *= angleSqr;
  134. result += 2.45650893e-02f;
  135. result *= angleSqr;
  136. result += 5.33740603e-02f;
  137. result *= angleSqr;
  138. result += 1.333923995e-01f;
  139. result *= angleSqr;
  140. result += 3.333314036e-01f;
  141. result *= angleSqr;
  142. result += 1.0f;
  143. result *= val;
  144. return result;
  145. }
  146. float Math::fastASin0(float val)
  147. {
  148. float root = sqrt(abs(1.0f - val));
  149. float result = -0.0187293f;
  150. result *= val;
  151. result += 0.0742610f;
  152. result *= val;
  153. result -= 0.2121144f;
  154. result *= val;
  155. result += 1.5707288f;
  156. result = HALF_PI - root*result;
  157. return result;
  158. }
  159. float Math::fastASin1(float val)
  160. {
  161. float root = sqrt(abs(1.0f - val));
  162. float result = -0.0012624911f;
  163. result *= val;
  164. result += 0.0066700901f;
  165. result *= val;
  166. result -= 0.0170881256f;
  167. result *= val;
  168. result += 0.0308918810f;
  169. result *= val;
  170. result -= 0.0501743046f;
  171. result *= val;
  172. result += 0.0889789874f;
  173. result *= val;
  174. result -= 0.2145988016f;
  175. result *= val;
  176. result += 1.5707963050f;
  177. result = HALF_PI - root*result;
  178. return result;
  179. }
  180. float Math::fastACos0(float val)
  181. {
  182. float root = sqrt(abs(1.0f - val));
  183. float result = -0.0187293f;
  184. result *= val;
  185. result += 0.0742610f;
  186. result *= val;
  187. result -= 0.2121144f;
  188. result *= val;
  189. result += 1.5707288f;
  190. result *= root;
  191. return result;
  192. }
  193. float Math::fastACos1(float val)
  194. {
  195. float root = sqrt(abs(1.0f - val));
  196. float result = -0.0012624911f;
  197. result *= val;
  198. result += 0.0066700901f;
  199. result *= val;
  200. result -= 0.0170881256f;
  201. result *= val;
  202. result += 0.0308918810f;
  203. result *= val;
  204. result -= 0.0501743046f;
  205. result *= val;
  206. result += 0.0889789874f;
  207. result *= val;
  208. result -= 0.2145988016f;
  209. result *= val;
  210. result += 1.5707963050f;
  211. result *= root;
  212. return result;
  213. }
  214. float Math::fastATan0(float val)
  215. {
  216. float valueSqr = val*val;
  217. float result = 0.0208351f;
  218. result *= valueSqr;
  219. result -= 0.085133f;
  220. result *= valueSqr;
  221. result += 0.180141f;
  222. result *= valueSqr;
  223. result -= 0.3302995f;
  224. result *= valueSqr;
  225. result += 0.999866f;
  226. result *= val;
  227. return result;
  228. }
  229. float Math::fastATan1(float val)
  230. {
  231. float valueSqr = val*val;
  232. float result = 0.0028662257f;
  233. result *= valueSqr;
  234. result -= 0.0161657367f;
  235. result *= valueSqr;
  236. result += 0.0429096138f;
  237. result *= valueSqr;
  238. result -= 0.0752896400f;
  239. result *= valueSqr;
  240. result += 0.1065626393f;
  241. result *= valueSqr;
  242. result -= 0.1420889944f;
  243. result *= valueSqr;
  244. result += 0.1999355085f;
  245. result *= valueSqr;
  246. result -= 0.3333314528f;
  247. result *= valueSqr;
  248. result += 1.0f;
  249. result *= val;
  250. return result;
  251. }
  252. bool Math::approxEquals(float a, float b, float tolerance)
  253. {
  254. if (fabs(b-a) <= tolerance)
  255. return true;
  256. else
  257. return false;
  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. }