math.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  1. /*
  2. * Copyright 2011-2019 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bx#license-bsd-2-clause
  4. */
  5. #include "bx_p.h"
  6. #include <bx/math.h>
  7. #include <bx/uint32_t.h>
  8. namespace bx
  9. {
  10. const float kInfinity = bitsToFloat(UINT32_C(0x7f800000) );
  11. namespace
  12. {
  13. constexpr float kSinC2 = -0.16666667163372039794921875f;
  14. constexpr float kSinC4 = 8.333347737789154052734375e-3f;
  15. constexpr float kSinC6 = -1.9842604524455964565277099609375e-4f;
  16. constexpr float kSinC8 = 2.760012648650445044040679931640625e-6f;
  17. constexpr float kSinC10 = -2.50293279435709337121807038784027099609375e-8f;
  18. constexpr float kCosC2 = -0.5f;
  19. constexpr float kCosC4 = 4.166664183139801025390625e-2f;
  20. constexpr float kCosC6 = -1.388833043165504932403564453125e-3f;
  21. constexpr float kCosC8 = 2.47562347794882953166961669921875e-5f;
  22. constexpr float kCosC10 = -2.59630184018533327616751194000244140625e-7f;
  23. } // namespace
  24. BX_CONST_FUNC float cos(float _a)
  25. {
  26. const float scaled = _a * 2.0f*kInvPi;
  27. const float real = floor(scaled);
  28. const float xx = _a - real * kPiHalf;
  29. const int32_t bits = int32_t(real) & 3;
  30. float c0, c2, c4, c6, c8, c10;
  31. if (bits == 0
  32. || bits == 2)
  33. {
  34. c0 = 1.0f;
  35. c2 = kCosC2;
  36. c4 = kCosC4;
  37. c6 = kCosC6;
  38. c8 = kCosC8;
  39. c10 = kCosC10;
  40. }
  41. else
  42. {
  43. c0 = xx;
  44. c2 = kSinC2;
  45. c4 = kSinC4;
  46. c6 = kSinC6;
  47. c8 = kSinC8;
  48. c10 = kSinC10;
  49. }
  50. const float xsq = square(xx);
  51. const float tmp0 = mad(c10, xsq, c8 );
  52. const float tmp1 = mad(tmp0, xsq, c6 );
  53. const float tmp2 = mad(tmp1, xsq, c4 );
  54. const float tmp3 = mad(tmp2, xsq, c2 );
  55. const float tmp4 = mad(tmp3, xsq, 1.0);
  56. const float result = tmp4 * c0;
  57. return bits == 1 || bits == 2
  58. ? -result
  59. : result
  60. ;
  61. }
  62. namespace
  63. {
  64. constexpr float kAcosC0 = 1.5707288f;
  65. constexpr float kAcosC1 = -0.2121144f;
  66. constexpr float kAcosC2 = 0.0742610f;
  67. constexpr float kAcosC3 = -0.0187293f;
  68. } // namespace
  69. BX_CONST_FUNC float acos(float _a)
  70. {
  71. const float absa = abs(_a);
  72. const float tmp0 = mad(kAcosC3, absa, kAcosC2);
  73. const float tmp1 = mad(tmp0, absa, kAcosC1);
  74. const float tmp2 = mad(tmp1, absa, kAcosC0);
  75. const float tmp3 = tmp2 * sqrt(1.0f - absa);
  76. const float negate = float(_a < 0.0f);
  77. const float tmp4 = tmp3 - 2.0f*negate*tmp3;
  78. const float result = negate*kPi + tmp4;
  79. return result;
  80. }
  81. namespace
  82. {
  83. constexpr float kAtan2C0 = -0.013480470f;
  84. constexpr float kAtan2C1 = 0.057477314f;
  85. constexpr float kAtan2C2 = -0.121239071f;
  86. constexpr float kAtan2C3 = 0.195635925f;
  87. constexpr float kAtan2C4 = -0.332994597f;
  88. constexpr float kAtan2C5 = 0.999995630f;
  89. } // namespace
  90. BX_CONST_FUNC float atan2(float _y, float _x)
  91. {
  92. const float ax = abs(_x);
  93. const float ay = abs(_y);
  94. const float maxaxy = max(ax, ay);
  95. const float minaxy = min(ax, ay);
  96. if (maxaxy == 0.0f)
  97. {
  98. return 0.0f*sign(_y);
  99. }
  100. const float mxy = minaxy / maxaxy;
  101. const float mxysq = square(mxy);
  102. const float tmp0 = mad(kAtan2C0, mxysq, kAtan2C1);
  103. const float tmp1 = mad(tmp0, mxysq, kAtan2C2);
  104. const float tmp2 = mad(tmp1, mxysq, kAtan2C3);
  105. const float tmp3 = mad(tmp2, mxysq, kAtan2C4);
  106. const float tmp4 = mad(tmp3, mxysq, kAtan2C5);
  107. const float tmp5 = tmp4 * mxy;
  108. const float tmp6 = ay > ax ? kPiHalf - tmp5 : tmp5;
  109. const float tmp7 = _x < 0.0f ? kPi - tmp6 : tmp6;
  110. const float result = sign(_y)*tmp7;
  111. return result;
  112. }
  113. BX_CONST_FUNC float ldexp(float _a, int32_t _b)
  114. {
  115. const uint32_t ftob = floatToBits(_a);
  116. const uint32_t masked = uint32_and(ftob, UINT32_C(0xff800000) );
  117. const uint32_t expsign0 = uint32_sra(masked, 23);
  118. const uint32_t tmp = uint32_iadd(expsign0, _b);
  119. const uint32_t expsign1 = uint32_sll(tmp, 23);
  120. const uint32_t mantissa = uint32_and(ftob, UINT32_C(0x007fffff) );
  121. const uint32_t bits = uint32_or(mantissa, expsign1);
  122. const float result = bitsToFloat(bits);
  123. return result;
  124. }
  125. float frexp(float _a, int32_t* _outExp)
  126. {
  127. const uint32_t ftob = floatToBits(_a);
  128. const uint32_t masked0 = uint32_and(ftob, UINT32_C(0x7f800000) );
  129. const uint32_t exp0 = uint32_srl(masked0, 23);
  130. const uint32_t masked1 = uint32_and(ftob, UINT32_C(0x807fffff) );
  131. const uint32_t bits = uint32_or(masked1, UINT32_C(0x3f000000) );
  132. const float result = bitsToFloat(bits);
  133. *_outExp = int32_t(exp0 - 0x7e);
  134. return result;
  135. }
  136. namespace
  137. {
  138. constexpr float kExpC0 = 1.66666666666666019037e-01f;
  139. constexpr float kExpC1 = -2.77777777770155933842e-03f;
  140. constexpr float kExpC2 = 6.61375632143793436117e-05f;
  141. constexpr float kExpC3 = -1.65339022054652515390e-06f;
  142. constexpr float kExpC4 = 4.13813679705723846039e-08f;
  143. } // namespace
  144. BX_CONST_FUNC float exp(float _a)
  145. {
  146. if (abs(_a) <= kNearZero)
  147. {
  148. return _a + 1.0f;
  149. }
  150. const float kk = round(_a*kInvLogNat2);
  151. const float hi = _a - kk*kLogNat2Hi;
  152. const float lo = kk*kLogNat2Lo;
  153. const float hml = hi - lo;
  154. const float hmlsq = square(hml);
  155. const float tmp0 = mad(kExpC4, hmlsq, kExpC3);
  156. const float tmp1 = mad(tmp0, hmlsq, kExpC2);
  157. const float tmp2 = mad(tmp1, hmlsq, kExpC1);
  158. const float tmp3 = mad(tmp2, hmlsq, kExpC0);
  159. const float tmp4 = hml - hmlsq * tmp3;
  160. const float tmp5 = hml*tmp4/(2.0f-tmp4);
  161. const float tmp6 = 1.0f - ( (lo - tmp5) - hi);
  162. const float result = ldexp(tmp6, int32_t(kk) );
  163. return result;
  164. }
  165. namespace
  166. {
  167. constexpr float kLogC0 = 6.666666666666735130e-01f;
  168. constexpr float kLogC1 = 3.999999999940941908e-01f;
  169. constexpr float kLogC2 = 2.857142874366239149e-01f;
  170. constexpr float kLogC3 = 2.222219843214978396e-01f;
  171. constexpr float kLogC4 = 1.818357216161805012e-01f;
  172. constexpr float kLogC5 = 1.531383769920937332e-01f;
  173. constexpr float kLogC6 = 1.479819860511658591e-01f;
  174. } // namespace
  175. BX_CONST_FUNC float log(float _a)
  176. {
  177. int32_t exp;
  178. float ff = frexp(_a, &exp);
  179. if (ff < kSqrt2*0.5f)
  180. {
  181. ff *= 2.0f;
  182. --exp;
  183. }
  184. ff -= 1.0f;
  185. const float kk = float(exp);
  186. const float hi = kk*kLogNat2Hi;
  187. const float lo = kk*kLogNat2Lo;
  188. const float ss = ff / (2.0f + ff);
  189. const float s2 = square(ss);
  190. const float s4 = square(s2);
  191. const float tmp0 = mad(kLogC6, s4, kLogC4);
  192. const float tmp1 = mad(tmp0, s4, kLogC2);
  193. const float tmp2 = mad(tmp1, s4, kLogC0);
  194. const float t1 = s2*tmp2;
  195. const float tmp3 = mad(kLogC5, s4, kLogC3);
  196. const float tmp4 = mad(tmp3, s4, kLogC1);
  197. const float t2 = s4*tmp4;
  198. const float t12 = t1 + t2;
  199. const float hfsq = 0.5f*square(ff);
  200. const float result = hi - ( (hfsq - (ss*(hfsq+t12) + lo) ) - ff);
  201. return result;
  202. }
  203. void mtxLookAt(float* _result, const Vec3& _eye, const Vec3& _at, const Vec3& _up, Handness::Enum _handness)
  204. {
  205. const Vec3 view = normalize(
  206. Handness::Right == _handness
  207. ? sub(_eye, _at)
  208. : sub(_at, _eye)
  209. );
  210. const Vec3 uxv = cross(_up, view);
  211. const Vec3 right = normalize(uxv);
  212. const Vec3 up = cross(view, right);
  213. memSet(_result, 0, sizeof(float)*16);
  214. _result[ 0] = right.x;
  215. _result[ 1] = up.x;
  216. _result[ 2] = view.x;
  217. _result[ 4] = right.y;
  218. _result[ 5] = up.y;
  219. _result[ 6] = view.y;
  220. _result[ 8] = right.z;
  221. _result[ 9] = up.z;
  222. _result[10] = view.z;
  223. _result[12] = -dot(right, _eye);
  224. _result[13] = -dot(up, _eye);
  225. _result[14] = -dot(view, _eye);
  226. _result[15] = 1.0f;
  227. }
  228. static void mtxProjXYWH(float* _result, float _x, float _y, float _width, float _height, float _near, float _far, bool _homogeneousNdc, Handness::Enum _handness)
  229. {
  230. const float diff = _far-_near;
  231. const float aa = _homogeneousNdc ? ( _far+_near)/diff : _far/diff;
  232. const float bb = _homogeneousNdc ? (2.0f*_far*_near)/diff : _near*aa;
  233. memSet(_result, 0, sizeof(float)*16);
  234. _result[ 0] = _width;
  235. _result[ 5] = _height;
  236. _result[ 8] = (Handness::Right == _handness) ? _x : -_x;
  237. _result[ 9] = (Handness::Right == _handness) ? _y : -_y;
  238. _result[10] = (Handness::Right == _handness) ? -aa : aa;
  239. _result[11] = (Handness::Right == _handness) ? -1.0f : 1.0f;
  240. _result[14] = -bb;
  241. }
  242. void mtxProj(float* _result, float _ut, float _dt, float _lt, float _rt, float _near, float _far, bool _homogeneousNdc, Handness::Enum _handness)
  243. {
  244. const float invDiffRl = 1.0f/(_rt - _lt);
  245. const float invDiffUd = 1.0f/(_ut - _dt);
  246. const float width = 2.0f*_near * invDiffRl;
  247. const float height = 2.0f*_near * invDiffUd;
  248. const float xx = (_rt + _lt) * invDiffRl;
  249. const float yy = (_ut + _dt) * invDiffUd;
  250. mtxProjXYWH(_result, xx, yy, width, height, _near, _far, _homogeneousNdc, _handness);
  251. }
  252. void mtxProj(float* _result, const float _fov[4], float _near, float _far, bool _homogeneousNdc, Handness::Enum _handness)
  253. {
  254. mtxProj(_result, _fov[0], _fov[1], _fov[2], _fov[3], _near, _far, _homogeneousNdc, _handness);
  255. }
  256. void mtxProj(float* _result, float _fovy, float _aspect, float _near, float _far, bool _homogeneousNdc, Handness::Enum _handness)
  257. {
  258. const float height = 1.0f/tan(toRad(_fovy)*0.5f);
  259. const float width = height * 1.0f/_aspect;
  260. mtxProjXYWH(_result, 0.0f, 0.0f, width, height, _near, _far, _homogeneousNdc, _handness);
  261. }
  262. static void mtxProjInfXYWH(float* _result, float _x, float _y, float _width, float _height, float _near, bool _homogeneousNdc, Handness::Enum _handness, NearFar::Enum _nearFar)
  263. {
  264. float aa;
  265. float bb;
  266. if (NearFar::Reverse == _nearFar)
  267. {
  268. aa = _homogeneousNdc ? -1.0f : 0.0f;
  269. bb = _homogeneousNdc ? -2.0f*_near : -_near;
  270. }
  271. else
  272. {
  273. aa = 1.0f;
  274. bb = _homogeneousNdc ? 2.0f*_near : _near;
  275. }
  276. memSet(_result, 0, sizeof(float)*16);
  277. _result[ 0] = _width;
  278. _result[ 5] = _height;
  279. _result[ 8] = (Handness::Right == _handness) ? _x : -_x;
  280. _result[ 9] = (Handness::Right == _handness) ? _y : -_y;
  281. _result[10] = (Handness::Right == _handness) ? -aa : aa;
  282. _result[11] = (Handness::Right == _handness) ? -1.0f : 1.0f;
  283. _result[14] = -bb;
  284. }
  285. void mtxProjInf(float* _result, float _ut, float _dt, float _lt, float _rt, float _near, bool _homogeneousNdc, Handness::Enum _handness, NearFar::Enum _nearFar)
  286. {
  287. const float invDiffRl = 1.0f/(_rt - _lt);
  288. const float invDiffUd = 1.0f/(_ut - _dt);
  289. const float width = 2.0f*_near * invDiffRl;
  290. const float height = 2.0f*_near * invDiffUd;
  291. const float xx = (_rt + _lt) * invDiffRl;
  292. const float yy = (_ut + _dt) * invDiffUd;
  293. mtxProjInfXYWH(_result, xx, yy, width, height, _near, _homogeneousNdc, _handness, _nearFar);
  294. }
  295. void mtxProjInf(float* _result, const float _fov[4], float _near, bool _homogeneousNdc, Handness::Enum _handness, NearFar::Enum _nearFar)
  296. {
  297. mtxProjInf(_result, _fov[0], _fov[1], _fov[2], _fov[3], _near, _homogeneousNdc, _handness, _nearFar);
  298. }
  299. void mtxProjInf(float* _result, float _fovy, float _aspect, float _near, bool _homogeneousNdc, Handness::Enum _handness, NearFar::Enum _nearFar)
  300. {
  301. const float height = 1.0f/tan(toRad(_fovy)*0.5f);
  302. const float width = height * 1.0f/_aspect;
  303. mtxProjInfXYWH(_result, 0.0f, 0.0f, width, height, _near, _homogeneousNdc, _handness, _nearFar);
  304. }
  305. void mtxOrtho(float* _result, float _left, float _right, float _bottom, float _top, float _near, float _far, float _offset, bool _homogeneousNdc, Handness::Enum _handness)
  306. {
  307. const float aa = 2.0f/(_right - _left);
  308. const float bb = 2.0f/(_top - _bottom);
  309. const float cc = (_homogeneousNdc ? 2.0f : 1.0f) / (_far - _near);
  310. const float dd = (_left + _right )/(_left - _right);
  311. const float ee = (_top + _bottom)/(_bottom - _top );
  312. const float ff = _homogeneousNdc
  313. ? (_near + _far)/(_near - _far)
  314. : _near /(_near - _far)
  315. ;
  316. memSet(_result, 0, sizeof(float)*16);
  317. _result[ 0] = aa;
  318. _result[ 5] = bb;
  319. _result[10] = Handness::Right == _handness ? -cc : cc;
  320. _result[12] = dd + _offset;
  321. _result[13] = ee;
  322. _result[14] = ff;
  323. _result[15] = 1.0f;
  324. }
  325. void mtxRotateX(float* _result, float _ax)
  326. {
  327. const float sx = sin(_ax);
  328. const float cx = cos(_ax);
  329. memSet(_result, 0, sizeof(float)*16);
  330. _result[ 0] = 1.0f;
  331. _result[ 5] = cx;
  332. _result[ 6] = -sx;
  333. _result[ 9] = sx;
  334. _result[10] = cx;
  335. _result[15] = 1.0f;
  336. }
  337. void mtxRotateY(float* _result, float _ay)
  338. {
  339. const float sy = sin(_ay);
  340. const float cy = cos(_ay);
  341. memSet(_result, 0, sizeof(float)*16);
  342. _result[ 0] = cy;
  343. _result[ 2] = sy;
  344. _result[ 5] = 1.0f;
  345. _result[ 8] = -sy;
  346. _result[10] = cy;
  347. _result[15] = 1.0f;
  348. }
  349. void mtxRotateZ(float* _result, float _az)
  350. {
  351. const float sz = sin(_az);
  352. const float cz = cos(_az);
  353. memSet(_result, 0, sizeof(float)*16);
  354. _result[ 0] = cz;
  355. _result[ 1] = -sz;
  356. _result[ 4] = sz;
  357. _result[ 5] = cz;
  358. _result[10] = 1.0f;
  359. _result[15] = 1.0f;
  360. }
  361. void mtxRotateXY(float* _result, float _ax, float _ay)
  362. {
  363. const float sx = sin(_ax);
  364. const float cx = cos(_ax);
  365. const float sy = sin(_ay);
  366. const float cy = cos(_ay);
  367. memSet(_result, 0, sizeof(float)*16);
  368. _result[ 0] = cy;
  369. _result[ 2] = sy;
  370. _result[ 4] = sx*sy;
  371. _result[ 5] = cx;
  372. _result[ 6] = -sx*cy;
  373. _result[ 8] = -cx*sy;
  374. _result[ 9] = sx;
  375. _result[10] = cx*cy;
  376. _result[15] = 1.0f;
  377. }
  378. void mtxRotateXYZ(float* _result, float _ax, float _ay, float _az)
  379. {
  380. const float sx = sin(_ax);
  381. const float cx = cos(_ax);
  382. const float sy = sin(_ay);
  383. const float cy = cos(_ay);
  384. const float sz = sin(_az);
  385. const float cz = cos(_az);
  386. memSet(_result, 0, sizeof(float)*16);
  387. _result[ 0] = cy*cz;
  388. _result[ 1] = -cy*sz;
  389. _result[ 2] = sy;
  390. _result[ 4] = cz*sx*sy + cx*sz;
  391. _result[ 5] = cx*cz - sx*sy*sz;
  392. _result[ 6] = -cy*sx;
  393. _result[ 8] = -cx*cz*sy + sx*sz;
  394. _result[ 9] = cz*sx + cx*sy*sz;
  395. _result[10] = cx*cy;
  396. _result[15] = 1.0f;
  397. }
  398. void mtxRotateZYX(float* _result, float _ax, float _ay, float _az)
  399. {
  400. const float sx = sin(_ax);
  401. const float cx = cos(_ax);
  402. const float sy = sin(_ay);
  403. const float cy = cos(_ay);
  404. const float sz = sin(_az);
  405. const float cz = cos(_az);
  406. memSet(_result, 0, sizeof(float)*16);
  407. _result[ 0] = cy*cz;
  408. _result[ 1] = cz*sx*sy-cx*sz;
  409. _result[ 2] = cx*cz*sy+sx*sz;
  410. _result[ 4] = cy*sz;
  411. _result[ 5] = cx*cz + sx*sy*sz;
  412. _result[ 6] = -cz*sx + cx*sy*sz;
  413. _result[ 8] = -sy;
  414. _result[ 9] = cy*sx;
  415. _result[10] = cx*cy;
  416. _result[15] = 1.0f;
  417. };
  418. void mtxSRT(float* _result, float _sx, float _sy, float _sz, float _ax, float _ay, float _az, float _tx, float _ty, float _tz)
  419. {
  420. const float sx = sin(_ax);
  421. const float cx = cos(_ax);
  422. const float sy = sin(_ay);
  423. const float cy = cos(_ay);
  424. const float sz = sin(_az);
  425. const float cz = cos(_az);
  426. const float sxsz = sx*sz;
  427. const float cycz = cy*cz;
  428. _result[ 0] = _sx * (cycz - sxsz*sy);
  429. _result[ 1] = _sx * -cx*sz;
  430. _result[ 2] = _sx * (cz*sy + cy*sxsz);
  431. _result[ 3] = 0.0f;
  432. _result[ 4] = _sy * (cz*sx*sy + cy*sz);
  433. _result[ 5] = _sy * cx*cz;
  434. _result[ 6] = _sy * (sy*sz -cycz*sx);
  435. _result[ 7] = 0.0f;
  436. _result[ 8] = _sz * -cx*sy;
  437. _result[ 9] = _sz * sx;
  438. _result[10] = _sz * cx*cy;
  439. _result[11] = 0.0f;
  440. _result[12] = _tx;
  441. _result[13] = _ty;
  442. _result[14] = _tz;
  443. _result[15] = 1.0f;
  444. }
  445. void mtx3Inverse(float* _result, const float* _a)
  446. {
  447. const float xx = _a[0];
  448. const float xy = _a[1];
  449. const float xz = _a[2];
  450. const float yx = _a[3];
  451. const float yy = _a[4];
  452. const float yz = _a[5];
  453. const float zx = _a[6];
  454. const float zy = _a[7];
  455. const float zz = _a[8];
  456. float det = 0.0f;
  457. det += xx * (yy*zz - yz*zy);
  458. det -= xy * (yx*zz - yz*zx);
  459. det += xz * (yx*zy - yy*zx);
  460. float invDet = 1.0f/det;
  461. _result[0] = +(yy*zz - yz*zy) * invDet;
  462. _result[1] = -(xy*zz - xz*zy) * invDet;
  463. _result[2] = +(xy*yz - xz*yy) * invDet;
  464. _result[3] = -(yx*zz - yz*zx) * invDet;
  465. _result[4] = +(xx*zz - xz*zx) * invDet;
  466. _result[5] = -(xx*yz - xz*yx) * invDet;
  467. _result[6] = +(yx*zy - yy*zx) * invDet;
  468. _result[7] = -(xx*zy - xy*zx) * invDet;
  469. _result[8] = +(xx*yy - xy*yx) * invDet;
  470. }
  471. void mtxInverse(float* _result, const float* _a)
  472. {
  473. const float xx = _a[ 0];
  474. const float xy = _a[ 1];
  475. const float xz = _a[ 2];
  476. const float xw = _a[ 3];
  477. const float yx = _a[ 4];
  478. const float yy = _a[ 5];
  479. const float yz = _a[ 6];
  480. const float yw = _a[ 7];
  481. const float zx = _a[ 8];
  482. const float zy = _a[ 9];
  483. const float zz = _a[10];
  484. const float zw = _a[11];
  485. const float wx = _a[12];
  486. const float wy = _a[13];
  487. const float wz = _a[14];
  488. const float ww = _a[15];
  489. float det = 0.0f;
  490. det += xx * (yy*(zz*ww - zw*wz) - yz*(zy*ww - zw*wy) + yw*(zy*wz - zz*wy) );
  491. det -= xy * (yx*(zz*ww - zw*wz) - yz*(zx*ww - zw*wx) + yw*(zx*wz - zz*wx) );
  492. det += xz * (yx*(zy*ww - zw*wy) - yy*(zx*ww - zw*wx) + yw*(zx*wy - zy*wx) );
  493. det -= xw * (yx*(zy*wz - zz*wy) - yy*(zx*wz - zz*wx) + yz*(zx*wy - zy*wx) );
  494. float invDet = 1.0f/det;
  495. _result[ 0] = +(yy*(zz*ww - wz*zw) - yz*(zy*ww - wy*zw) + yw*(zy*wz - wy*zz) ) * invDet;
  496. _result[ 1] = -(xy*(zz*ww - wz*zw) - xz*(zy*ww - wy*zw) + xw*(zy*wz - wy*zz) ) * invDet;
  497. _result[ 2] = +(xy*(yz*ww - wz*yw) - xz*(yy*ww - wy*yw) + xw*(yy*wz - wy*yz) ) * invDet;
  498. _result[ 3] = -(xy*(yz*zw - zz*yw) - xz*(yy*zw - zy*yw) + xw*(yy*zz - zy*yz) ) * invDet;
  499. _result[ 4] = -(yx*(zz*ww - wz*zw) - yz*(zx*ww - wx*zw) + yw*(zx*wz - wx*zz) ) * invDet;
  500. _result[ 5] = +(xx*(zz*ww - wz*zw) - xz*(zx*ww - wx*zw) + xw*(zx*wz - wx*zz) ) * invDet;
  501. _result[ 6] = -(xx*(yz*ww - wz*yw) - xz*(yx*ww - wx*yw) + xw*(yx*wz - wx*yz) ) * invDet;
  502. _result[ 7] = +(xx*(yz*zw - zz*yw) - xz*(yx*zw - zx*yw) + xw*(yx*zz - zx*yz) ) * invDet;
  503. _result[ 8] = +(yx*(zy*ww - wy*zw) - yy*(zx*ww - wx*zw) + yw*(zx*wy - wx*zy) ) * invDet;
  504. _result[ 9] = -(xx*(zy*ww - wy*zw) - xy*(zx*ww - wx*zw) + xw*(zx*wy - wx*zy) ) * invDet;
  505. _result[10] = +(xx*(yy*ww - wy*yw) - xy*(yx*ww - wx*yw) + xw*(yx*wy - wx*yy) ) * invDet;
  506. _result[11] = -(xx*(yy*zw - zy*yw) - xy*(yx*zw - zx*yw) + xw*(yx*zy - zx*yy) ) * invDet;
  507. _result[12] = -(yx*(zy*wz - wy*zz) - yy*(zx*wz - wx*zz) + yz*(zx*wy - wx*zy) ) * invDet;
  508. _result[13] = +(xx*(zy*wz - wy*zz) - xy*(zx*wz - wx*zz) + xz*(zx*wy - wx*zy) ) * invDet;
  509. _result[14] = -(xx*(yy*wz - wy*yz) - xy*(yx*wz - wx*yz) + xz*(yx*wy - wx*yy) ) * invDet;
  510. _result[15] = +(xx*(yy*zz - zy*yz) - xy*(yx*zz - zx*yz) + xz*(yx*zy - zx*yy) ) * invDet;
  511. }
  512. void mtx3Cofactor(float* _result, const float* _a)
  513. {
  514. const float xx = _a[0];
  515. const float xy = _a[1];
  516. const float xz = _a[2];
  517. const float yx = _a[3];
  518. const float yy = _a[4];
  519. const float yz = _a[5];
  520. const float zx = _a[6];
  521. const float zy = _a[7];
  522. const float zz = _a[8];
  523. _result[0] = +(yy*zz - yz * zy);
  524. _result[1] = -(yx*zz - yz * zx);
  525. _result[2] = +(yx*zy - yy * zx);
  526. _result[3] = -(xy*zz - xz * zy);
  527. _result[4] = +(xx*zz - xz * zx);
  528. _result[5] = -(xx*zy - xy * zx);
  529. _result[6] = +(xy*yz - xz * yy);
  530. _result[7] = -(xx*yz - xz * yx);
  531. _result[8] = +(xx*yy - xy * yx);
  532. }
  533. void mtxCofactor(float* _result, const float* _a)
  534. {
  535. const float xx = _a[0];
  536. const float xy = _a[1];
  537. const float xz = _a[2];
  538. const float xw = _a[3];
  539. const float yx = _a[4];
  540. const float yy = _a[5];
  541. const float yz = _a[6];
  542. const float yw = _a[7];
  543. const float zx = _a[8];
  544. const float zy = _a[9];
  545. const float zz = _a[10];
  546. const float zw = _a[11];
  547. const float wx = _a[12];
  548. const float wy = _a[13];
  549. const float wz = _a[14];
  550. const float ww = _a[15];
  551. _result[ 0] = +(yy*(zz*ww - wz * zw) - yz * (zy*ww - wy * zw) + yw * (zy*wz - wy * zz) );
  552. _result[ 1] = -(yx*(zz*ww - wz * zw) - yz * (zx*ww - wx * zw) + yw * (zx*wz - wx * zz) );
  553. _result[ 2] = +(yx*(zy*ww - wy * zw) - yy * (zx*ww - wx * zw) + yw * (zx*wy - wx * zy) );
  554. _result[ 3] = -(yx*(zy*wz - wy * zz) - yy * (zx*wz - wx * zz) + yz * (zx*wy - wx * zy) );
  555. _result[ 4] = -(xy*(zz*ww - wz * zw) - xz * (zy*ww - wy * zw) + xw * (zy*wz - wy * zz) );
  556. _result[ 5] = +(xx*(zz*ww - wz * zw) - xz * (zx*ww - wx * zw) + xw * (zx*wz - wx * zz) );
  557. _result[ 6] = -(xx*(zy*ww - wy * zw) - xy * (zx*ww - wx * zw) + xw * (zx*wy - wx * zy) );
  558. _result[ 7] = +(xx*(zy*wz - wy * zz) - xy * (zx*wz - wx * zz) + xz * (zx*wy - wx * zy) );
  559. _result[ 8] = +(xy*(yz*ww - wz * yw) - xz * (yy*ww - wy * yw) + xw * (yy*wz - wy * yz) );
  560. _result[ 9] = -(xx*(yz*ww - wz * yw) - xz * (yx*ww - wx * yw) + xw * (yx*wz - wx * yz) );
  561. _result[10] = +(xx*(yy*ww - wy * yw) - xy * (yx*ww - wx * yw) + xw * (yx*wy - wx * yy) );
  562. _result[11] = -(xx*(yy*wz - wy * yz) - xy * (yx*wz - wx * yz) + xz * (yx*wy - wx * yy) );
  563. _result[12] = -(xy*(yz*zw - zz * yw) - xz * (yy*zw - zy * yw) + xw * (yy*zz - zy * yz) );
  564. _result[13] = +(xx*(yz*zw - zz * yw) - xz * (yx*zw - zx * yw) + xw * (yx*zz - zx * yz) );
  565. _result[14] = -(xx*(yy*zw - zy * yw) - xy * (yx*zw - zx * yw) + xw * (yx*zy - zx * yy) );
  566. _result[15] = +(xx*(yy*zz - zy * yz) - xy * (yx*zz - zx * yz) + xz * (yx*zy - zx * yy) );
  567. }
  568. void calcLinearFit2D(float _result[2], const void* _points, uint32_t _stride, uint32_t _numPoints)
  569. {
  570. float sumX = 0.0f;
  571. float sumY = 0.0f;
  572. float sumXX = 0.0f;
  573. float sumXY = 0.0f;
  574. const uint8_t* ptr = (const uint8_t*)_points;
  575. for (uint32_t ii = 0; ii < _numPoints; ++ii, ptr += _stride)
  576. {
  577. const float* point = (const float*)ptr;
  578. float xx = point[0];
  579. float yy = point[1];
  580. sumX += xx;
  581. sumY += yy;
  582. sumXX += xx*xx;
  583. sumXY += xx*yy;
  584. }
  585. // [ sum(x^2) sum(x) ] [ A ] = [ sum(x*y) ]
  586. // [ sum(x) numPoints ] [ B ] [ sum(y) ]
  587. float det = (sumXX*_numPoints - sumX*sumX);
  588. float invDet = 1.0f/det;
  589. _result[0] = (-sumX * sumY + _numPoints * sumXY) * invDet;
  590. _result[1] = (sumXX * sumY - sumX * sumXY) * invDet;
  591. }
  592. void calcLinearFit3D(float _result[3], const void* _points, uint32_t _stride, uint32_t _numPoints)
  593. {
  594. float sumX = 0.0f;
  595. float sumY = 0.0f;
  596. float sumZ = 0.0f;
  597. float sumXX = 0.0f;
  598. float sumXY = 0.0f;
  599. float sumXZ = 0.0f;
  600. float sumYY = 0.0f;
  601. float sumYZ = 0.0f;
  602. const uint8_t* ptr = (const uint8_t*)_points;
  603. for (uint32_t ii = 0; ii < _numPoints; ++ii, ptr += _stride)
  604. {
  605. const float* point = (const float*)ptr;
  606. float xx = point[0];
  607. float yy = point[1];
  608. float zz = point[2];
  609. sumX += xx;
  610. sumY += yy;
  611. sumZ += zz;
  612. sumXX += xx*xx;
  613. sumXY += xx*yy;
  614. sumXZ += xx*zz;
  615. sumYY += yy*yy;
  616. sumYZ += yy*zz;
  617. }
  618. // [ sum(x^2) sum(x*y) sum(x) ] [ A ] [ sum(x*z) ]
  619. // [ sum(x*y) sum(y^2) sum(y) ] [ B ] = [ sum(y*z) ]
  620. // [ sum(x) sum(y) numPoints ] [ C ] [ sum(z) ]
  621. float mtx[9] =
  622. {
  623. sumXX, sumXY, sumX,
  624. sumXY, sumYY, sumY,
  625. sumX, sumY, float(_numPoints),
  626. };
  627. float invMtx[9];
  628. mtx3Inverse(invMtx, mtx);
  629. _result[0] = invMtx[0]*sumXZ + invMtx[1]*sumYZ + invMtx[2]*sumZ;
  630. _result[1] = invMtx[3]*sumXZ + invMtx[4]*sumYZ + invMtx[5]*sumZ;
  631. _result[2] = invMtx[6]*sumXZ + invMtx[7]*sumYZ + invMtx[8]*sumZ;
  632. }
  633. void rgbToHsv(float _hsv[3], const float _rgb[3])
  634. {
  635. const float rr = _rgb[0];
  636. const float gg = _rgb[1];
  637. const float bb = _rgb[2];
  638. const float s0 = step(bb, gg);
  639. const float px = lerp(bb, gg, s0);
  640. const float py = lerp(gg, bb, s0);
  641. const float pz = lerp(-1.0f, 0.0f, s0);
  642. const float pw = lerp(2.0f/3.0f, -1.0f/3.0f, s0);
  643. const float s1 = step(px, rr);
  644. const float qx = lerp(px, rr, s1);
  645. const float qy = py;
  646. const float qz = lerp(pw, pz, s1);
  647. const float qw = lerp(rr, px, s1);
  648. const float dd = qx - min(qw, qy);
  649. const float ee = 1.0e-10f;
  650. _hsv[0] = abs(qz + (qw - qy) / (6.0f * dd + ee) );
  651. _hsv[1] = dd / (qx + ee);
  652. _hsv[2] = qx;
  653. }
  654. void hsvToRgb(float _rgb[3], const float _hsv[3])
  655. {
  656. const float hh = _hsv[0];
  657. const float ss = _hsv[1];
  658. const float vv = _hsv[2];
  659. const float px = abs(fract(hh + 1.0f ) * 6.0f - 3.0f);
  660. const float py = abs(fract(hh + 2.0f/3.0f) * 6.0f - 3.0f);
  661. const float pz = abs(fract(hh + 1.0f/3.0f) * 6.0f - 3.0f);
  662. _rgb[0] = vv * lerp(1.0f, clamp(px - 1.0f, 0.0f, 1.0f), ss);
  663. _rgb[1] = vv * lerp(1.0f, clamp(py - 1.0f, 0.0f, 1.0f), ss);
  664. _rgb[2] = vv * lerp(1.0f, clamp(pz - 1.0f, 0.0f, 1.0f), ss);
  665. }
  666. } // namespace bx