math.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  1. /*
  2. * Copyright 2011-2017 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bx#license-bsd-2-clause
  4. */
  5. #include <bx/math.h>
  6. #include <math.h>
  7. namespace bx
  8. {
  9. const float kPi = 3.1415926535897932384626433832795f;
  10. const float kPi2 = 6.2831853071795864769252867665590f;
  11. const float kInvPi = 1.0f/kPi;
  12. const float kPiHalf = 1.5707963267948966192313216916398f;
  13. const float kSqrt2 = 1.4142135623730950488016887242097f;
  14. const float kInvLogNat2 = 1.4426950408889634073599246810019f;
  15. #if BX_COMPILER_MSVC
  16. const float kHuge = float(HUGE_VAL);
  17. #else
  18. const float kHuge = HUGE_VALF;
  19. #endif // BX_COMPILER_MSVC
  20. float fabs(float _a)
  21. {
  22. return ::fabsf(_a);
  23. }
  24. float fsin(float _a)
  25. {
  26. return ::sinf(_a);
  27. }
  28. float fasin(float _a)
  29. {
  30. return ::asinf(_a);
  31. }
  32. float fcos(float _a)
  33. {
  34. return ::cosf(_a);
  35. }
  36. float ftan(float _a)
  37. {
  38. return ::tanf(_a);
  39. }
  40. float facos(float _a)
  41. {
  42. return ::acosf(_a);
  43. }
  44. float fatan2(float _y, float _x)
  45. {
  46. return ::atan2f(_y, _x);
  47. }
  48. float fpow(float _a, float _b)
  49. {
  50. return ::powf(_a, _b);
  51. }
  52. float flog(float _a)
  53. {
  54. return ::logf(_a);
  55. }
  56. float fsqrt(float _a)
  57. {
  58. return ::sqrtf(_a);
  59. }
  60. float ffloor(float _f)
  61. {
  62. return ::floorf(_f);
  63. }
  64. float fceil(float _f)
  65. {
  66. return ::ceilf(_f);
  67. }
  68. float fmod(float _a, float _b)
  69. {
  70. return ::fmodf(_a, _b);
  71. }
  72. void mtxLookAtImpl(float* _result, const float* _eye, const float* _view, const float* _up)
  73. {
  74. float up[3] = { 0.0f, 1.0f, 0.0f };
  75. if (NULL != _up)
  76. {
  77. up[0] = _up[0];
  78. up[1] = _up[1];
  79. up[2] = _up[2];
  80. }
  81. float tmp[4];
  82. vec3Cross(tmp, up, _view);
  83. float right[4];
  84. vec3Norm(right, tmp);
  85. vec3Cross(up, _view, right);
  86. memSet(_result, 0, sizeof(float)*16);
  87. _result[ 0] = right[0];
  88. _result[ 1] = up[0];
  89. _result[ 2] = _view[0];
  90. _result[ 4] = right[1];
  91. _result[ 5] = up[1];
  92. _result[ 6] = _view[1];
  93. _result[ 8] = right[2];
  94. _result[ 9] = up[2];
  95. _result[10] = _view[2];
  96. _result[12] = -vec3Dot(right, _eye);
  97. _result[13] = -vec3Dot(up, _eye);
  98. _result[14] = -vec3Dot(_view, _eye);
  99. _result[15] = 1.0f;
  100. }
  101. void mtxLookAtLh(float* _result, const float* _eye, const float* _at, const float* _up)
  102. {
  103. float tmp[4];
  104. vec3Sub(tmp, _at, _eye);
  105. float view[4];
  106. vec3Norm(view, tmp);
  107. mtxLookAtImpl(_result, _eye, view, _up);
  108. }
  109. void mtxLookAtRh(float* _result, const float* _eye, const float* _at, const float* _up)
  110. {
  111. float tmp[4];
  112. vec3Sub(tmp, _eye, _at);
  113. float view[4];
  114. vec3Norm(view, tmp);
  115. mtxLookAtImpl(_result, _eye, view, _up);
  116. }
  117. void mtxLookAt(float* _result, const float* _eye, const float* _at, const float* _up)
  118. {
  119. mtxLookAtLh(_result, _eye, _at, _up);
  120. }
  121. template<Handness::Enum HandnessT>
  122. void mtxProjXYWH(float* _result, float _x, float _y, float _width, float _height, float _near, float _far, bool _oglNdc)
  123. {
  124. const float diff = _far-_near;
  125. const float aa = _oglNdc ? ( _far+_near)/diff : _far/diff;
  126. const float bb = _oglNdc ? (2.0f*_far*_near)/diff : _near*aa;
  127. memSet(_result, 0, sizeof(float)*16);
  128. _result[ 0] = _width;
  129. _result[ 5] = _height;
  130. _result[ 8] = (Handness::Right == HandnessT) ? _x : -_x;
  131. _result[ 9] = (Handness::Right == HandnessT) ? _y : -_y;
  132. _result[10] = (Handness::Right == HandnessT) ? -aa : aa;
  133. _result[11] = (Handness::Right == HandnessT) ? -1.0f : 1.0f;
  134. _result[14] = -bb;
  135. }
  136. template<Handness::Enum HandnessT>
  137. void mtxProjImpl(float* _result, float _ut, float _dt, float _lt, float _rt, float _near, float _far, bool _oglNdc)
  138. {
  139. const float invDiffRl = 1.0f/(_rt - _lt);
  140. const float invDiffUd = 1.0f/(_ut - _dt);
  141. const float width = 2.0f*_near * invDiffRl;
  142. const float height = 2.0f*_near * invDiffUd;
  143. const float xx = (_rt + _lt) * invDiffRl;
  144. const float yy = (_ut + _dt) * invDiffUd;
  145. mtxProjXYWH<HandnessT>(_result, xx, yy, width, height, _near, _far, _oglNdc);
  146. }
  147. template<Handness::Enum HandnessT>
  148. void mtxProjImpl(float* _result, const float _fov[4], float _near, float _far, bool _oglNdc)
  149. {
  150. mtxProjImpl<HandnessT>(_result, _fov[0], _fov[1], _fov[2], _fov[3], _near, _far, _oglNdc);
  151. }
  152. template<Handness::Enum HandnessT>
  153. void mtxProjImpl(float* _result, float _fovy, float _aspect, float _near, float _far, bool _oglNdc)
  154. {
  155. const float height = 1.0f/ftan(toRad(_fovy)*0.5f);
  156. const float width = height * 1.0f/_aspect;
  157. mtxProjXYWH<HandnessT>(_result, 0.0f, 0.0f, width, height, _near, _far, _oglNdc);
  158. }
  159. void mtxProj(float* _result, float _ut, float _dt, float _lt, float _rt, float _near, float _far, bool _oglNdc)
  160. {
  161. mtxProjImpl<Handness::Left>(_result, _ut, _dt, _lt, _rt, _near, _far, _oglNdc);
  162. }
  163. void mtxProj(float* _result, const float _fov[4], float _near, float _far, bool _oglNdc)
  164. {
  165. mtxProjImpl<Handness::Left>(_result, _fov, _near, _far, _oglNdc);
  166. }
  167. void mtxProj(float* _result, float _fovy, float _aspect, float _near, float _far, bool _oglNdc)
  168. {
  169. mtxProjImpl<Handness::Left>(_result, _fovy, _aspect, _near, _far, _oglNdc);
  170. }
  171. void mtxProjLh(float* _result, float _ut, float _dt, float _lt, float _rt, float _near, float _far, bool _oglNdc)
  172. {
  173. mtxProjImpl<Handness::Left>(_result, _ut, _dt, _lt, _rt, _near, _far, _oglNdc);
  174. }
  175. void mtxProjLh(float* _result, const float _fov[4], float _near, float _far, bool _oglNdc)
  176. {
  177. mtxProjImpl<Handness::Left>(_result, _fov, _near, _far, _oglNdc);
  178. }
  179. void mtxProjLh(float* _result, float _fovy, float _aspect, float _near, float _far, bool _oglNdc)
  180. {
  181. mtxProjImpl<Handness::Left>(_result, _fovy, _aspect, _near, _far, _oglNdc);
  182. }
  183. void mtxProjRh(float* _result, float _ut, float _dt, float _lt, float _rt, float _near, float _far, bool _oglNdc)
  184. {
  185. mtxProjImpl<Handness::Right>(_result, _ut, _dt, _lt, _rt, _near, _far, _oglNdc);
  186. }
  187. void mtxProjRh(float* _result, const float _fov[4], float _near, float _far, bool _oglNdc)
  188. {
  189. mtxProjImpl<Handness::Right>(_result, _fov, _near, _far, _oglNdc);
  190. }
  191. void mtxProjRh(float* _result, float _fovy, float _aspect, float _near, float _far, bool _oglNdc)
  192. {
  193. mtxProjImpl<Handness::Right>(_result, _fovy, _aspect, _near, _far, _oglNdc);
  194. }
  195. template<NearFar::Enum NearFarT, Handness::Enum HandnessT>
  196. void mtxProjInfXYWH(float* _result, float _x, float _y, float _width, float _height, float _near, bool _oglNdc)
  197. {
  198. float aa;
  199. float bb;
  200. if (BX_ENABLED(NearFar::Reverse == NearFarT) )
  201. {
  202. aa = _oglNdc ? -1.0f : 0.0f;
  203. bb = _oglNdc ? -2.0f*_near : -_near;
  204. }
  205. else
  206. {
  207. aa = 1.0f;
  208. bb = _oglNdc ? 2.0f*_near : _near;
  209. }
  210. memSet(_result, 0, sizeof(float)*16);
  211. _result[ 0] = _width;
  212. _result[ 5] = _height;
  213. _result[ 8] = (Handness::Right == HandnessT) ? _x : -_x;
  214. _result[ 9] = (Handness::Right == HandnessT) ? _y : -_y;
  215. _result[10] = (Handness::Right == HandnessT) ? -aa : aa;
  216. _result[11] = (Handness::Right == HandnessT) ? -1.0f : 1.0f;
  217. _result[14] = -bb;
  218. }
  219. template<NearFar::Enum NearFarT, Handness::Enum HandnessT>
  220. void mtxProjInfImpl(float* _result, float _ut, float _dt, float _lt, float _rt, float _near, bool _oglNdc)
  221. {
  222. const float invDiffRl = 1.0f/(_rt - _lt);
  223. const float invDiffUd = 1.0f/(_ut - _dt);
  224. const float width = 2.0f*_near * invDiffRl;
  225. const float height = 2.0f*_near * invDiffUd;
  226. const float xx = (_rt + _lt) * invDiffRl;
  227. const float yy = (_ut + _dt) * invDiffUd;
  228. mtxProjInfXYWH<NearFarT,HandnessT>(_result, xx, yy, width, height, _near, _oglNdc);
  229. }
  230. template<NearFar::Enum NearFarT, Handness::Enum HandnessT>
  231. void mtxProjInfImpl(float* _result, const float _fov[4], float _near, bool _oglNdc)
  232. {
  233. mtxProjInfImpl<NearFarT,HandnessT>(_result, _fov[0], _fov[1], _fov[2], _fov[3], _near, _oglNdc);
  234. }
  235. template<NearFar::Enum NearFarT, Handness::Enum HandnessT>
  236. void mtxProjInfImpl(float* _result, float _fovy, float _aspect, float _near, bool _oglNdc)
  237. {
  238. const float height = 1.0f/ftan(toRad(_fovy)*0.5f);
  239. const float width = height * 1.0f/_aspect;
  240. mtxProjInfXYWH<NearFarT,HandnessT>(_result, 0.0f, 0.0f, width, height, _near, _oglNdc);
  241. }
  242. void mtxProjInf(float* _result, const float _fov[4], float _near, bool _oglNdc)
  243. {
  244. mtxProjInfImpl<NearFar::Default,Handness::Left>(_result, _fov, _near, _oglNdc);
  245. }
  246. void mtxProjInf(float* _result, float _ut, float _dt, float _lt, float _rt, float _near, bool _oglNdc)
  247. {
  248. mtxProjInfImpl<NearFar::Default,Handness::Left>(_result, _ut, _dt, _lt, _rt, _near, _oglNdc);
  249. }
  250. void mtxProjInf(float* _result, float _fovy, float _aspect, float _near, bool _oglNdc)
  251. {
  252. mtxProjInfImpl<NearFar::Default,Handness::Left>(_result, _fovy, _aspect, _near, _oglNdc);
  253. }
  254. void mtxProjInfLh(float* _result, float _ut, float _dt, float _lt, float _rt, float _near, bool _oglNdc)
  255. {
  256. mtxProjInfImpl<NearFar::Default,Handness::Left>(_result, _ut, _dt, _lt, _rt, _near, _oglNdc);
  257. }
  258. void mtxProjInfLh(float* _result, const float _fov[4], float _near, bool _oglNdc)
  259. {
  260. mtxProjInfImpl<NearFar::Default,Handness::Left>(_result, _fov, _near, _oglNdc);
  261. }
  262. void mtxProjInfLh(float* _result, float _fovy, float _aspect, float _near, bool _oglNdc)
  263. {
  264. mtxProjInfImpl<NearFar::Default,Handness::Left>(_result, _fovy, _aspect, _near, _oglNdc);
  265. }
  266. void mtxProjInfRh(float* _result, float _ut, float _dt, float _lt, float _rt, float _near, bool _oglNdc)
  267. {
  268. mtxProjInfImpl<NearFar::Default,Handness::Right>(_result, _ut, _dt, _lt, _rt, _near, _oglNdc);
  269. }
  270. void mtxProjInfRh(float* _result, const float _fov[4], float _near, bool _oglNdc)
  271. {
  272. mtxProjInfImpl<NearFar::Default,Handness::Right>(_result, _fov, _near, _oglNdc);
  273. }
  274. void mtxProjInfRh(float* _result, float _fovy, float _aspect, float _near, bool _oglNdc)
  275. {
  276. mtxProjInfImpl<NearFar::Default,Handness::Right>(_result, _fovy, _aspect, _near, _oglNdc);
  277. }
  278. void mtxProjRevInfLh(float* _result, float _ut, float _dt, float _lt, float _rt, float _near, bool _oglNdc)
  279. {
  280. mtxProjInfImpl<NearFar::Reverse,Handness::Left>(_result, _ut, _dt, _lt, _rt, _near, _oglNdc);
  281. }
  282. void mtxProjRevInfLh(float* _result, const float _fov[4], float _near, bool _oglNdc)
  283. {
  284. mtxProjInfImpl<NearFar::Reverse,Handness::Left>(_result, _fov, _near, _oglNdc);
  285. }
  286. void mtxProjRevInfLh(float* _result, float _fovy, float _aspect, float _near, bool _oglNdc)
  287. {
  288. mtxProjInfImpl<NearFar::Reverse,Handness::Left>(_result, _fovy, _aspect, _near, _oglNdc);
  289. }
  290. void mtxProjRevInfRh(float* _result, float _ut, float _dt, float _lt, float _rt, float _near, bool _oglNdc)
  291. {
  292. mtxProjInfImpl<NearFar::Reverse,Handness::Right>(_result, _ut, _dt, _lt, _rt, _near, _oglNdc);
  293. }
  294. void mtxProjRevInfRh(float* _result, const float _fov[4], float _near, bool _oglNdc)
  295. {
  296. mtxProjInfImpl<NearFar::Reverse,Handness::Right>(_result, _fov, _near, _oglNdc);
  297. }
  298. void mtxProjRevInfRh(float* _result, float _fovy, float _aspect, float _near, bool _oglNdc)
  299. {
  300. mtxProjInfImpl<NearFar::Reverse,Handness::Right>(_result, _fovy, _aspect, _near, _oglNdc);
  301. }
  302. template<Handness::Enum HandnessT>
  303. void mtxOrthoImpl(float* _result, float _left, float _right, float _bottom, float _top, float _near, float _far, float _offset, bool _oglNdc)
  304. {
  305. const float aa = 2.0f/(_right - _left);
  306. const float bb = 2.0f/(_top - _bottom);
  307. const float cc = (_oglNdc ? 2.0f : 1.0f) / (_far - _near);
  308. const float dd = (_left + _right )/(_left - _right);
  309. const float ee = (_top + _bottom)/(_bottom - _top );
  310. const float ff = _oglNdc
  311. ? (_near + _far)/(_near - _far)
  312. : _near /(_near - _far)
  313. ;
  314. memSet(_result, 0, sizeof(float)*16);
  315. _result[ 0] = aa;
  316. _result[ 5] = bb;
  317. _result[10] = (Handness::Right == HandnessT) ? -cc : cc;
  318. _result[12] = dd + _offset;
  319. _result[13] = ee;
  320. _result[14] = ff;
  321. _result[15] = 1.0f;
  322. }
  323. void mtxOrtho(float* _result, float _left, float _right, float _bottom, float _top, float _near, float _far, float _offset, bool _oglNdc)
  324. {
  325. mtxOrthoImpl<Handness::Left>(_result, _left, _right, _bottom, _top, _near, _far, _offset, _oglNdc);
  326. }
  327. void mtxOrthoLh(float* _result, float _left, float _right, float _bottom, float _top, float _near, float _far, float _offset, bool _oglNdc)
  328. {
  329. mtxOrthoImpl<Handness::Left>(_result, _left, _right, _bottom, _top, _near, _far, _offset, _oglNdc);
  330. }
  331. void mtxOrthoRh(float* _result, float _left, float _right, float _bottom, float _top, float _near, float _far, float _offset, bool _oglNdc)
  332. {
  333. mtxOrthoImpl<Handness::Right>(_result, _left, _right, _bottom, _top, _near, _far, _offset, _oglNdc);
  334. }
  335. void mtxRotateX(float* _result, float _ax)
  336. {
  337. const float sx = fsin(_ax);
  338. const float cx = fcos(_ax);
  339. memSet(_result, 0, sizeof(float)*16);
  340. _result[ 0] = 1.0f;
  341. _result[ 5] = cx;
  342. _result[ 6] = -sx;
  343. _result[ 9] = sx;
  344. _result[10] = cx;
  345. _result[15] = 1.0f;
  346. }
  347. void mtxRotateY(float* _result, float _ay)
  348. {
  349. const float sy = fsin(_ay);
  350. const float cy = fcos(_ay);
  351. memSet(_result, 0, sizeof(float)*16);
  352. _result[ 0] = cy;
  353. _result[ 2] = sy;
  354. _result[ 5] = 1.0f;
  355. _result[ 8] = -sy;
  356. _result[10] = cy;
  357. _result[15] = 1.0f;
  358. }
  359. void mtxRotateZ(float* _result, float _az)
  360. {
  361. const float sz = fsin(_az);
  362. const float cz = fcos(_az);
  363. memSet(_result, 0, sizeof(float)*16);
  364. _result[ 0] = cz;
  365. _result[ 1] = -sz;
  366. _result[ 4] = sz;
  367. _result[ 5] = cz;
  368. _result[10] = 1.0f;
  369. _result[15] = 1.0f;
  370. }
  371. void mtxRotateXY(float* _result, float _ax, float _ay)
  372. {
  373. const float sx = fsin(_ax);
  374. const float cx = fcos(_ax);
  375. const float sy = fsin(_ay);
  376. const float cy = fcos(_ay);
  377. memSet(_result, 0, sizeof(float)*16);
  378. _result[ 0] = cy;
  379. _result[ 2] = sy;
  380. _result[ 4] = sx*sy;
  381. _result[ 5] = cx;
  382. _result[ 6] = -sx*cy;
  383. _result[ 8] = -cx*sy;
  384. _result[ 9] = sx;
  385. _result[10] = cx*cy;
  386. _result[15] = 1.0f;
  387. }
  388. void mtxRotateXYZ(float* _result, float _ax, float _ay, float _az)
  389. {
  390. const float sx = fsin(_ax);
  391. const float cx = fcos(_ax);
  392. const float sy = fsin(_ay);
  393. const float cy = fcos(_ay);
  394. const float sz = fsin(_az);
  395. const float cz = fcos(_az);
  396. memSet(_result, 0, sizeof(float)*16);
  397. _result[ 0] = cy*cz;
  398. _result[ 1] = -cy*sz;
  399. _result[ 2] = sy;
  400. _result[ 4] = cz*sx*sy + cx*sz;
  401. _result[ 5] = cx*cz - sx*sy*sz;
  402. _result[ 6] = -cy*sx;
  403. _result[ 8] = -cx*cz*sy + sx*sz;
  404. _result[ 9] = cz*sx + cx*sy*sz;
  405. _result[10] = cx*cy;
  406. _result[15] = 1.0f;
  407. }
  408. void mtxRotateZYX(float* _result, float _ax, float _ay, float _az)
  409. {
  410. const float sx = fsin(_ax);
  411. const float cx = fcos(_ax);
  412. const float sy = fsin(_ay);
  413. const float cy = fcos(_ay);
  414. const float sz = fsin(_az);
  415. const float cz = fcos(_az);
  416. memSet(_result, 0, sizeof(float)*16);
  417. _result[ 0] = cy*cz;
  418. _result[ 1] = cz*sx*sy-cx*sz;
  419. _result[ 2] = cx*cz*sy+sx*sz;
  420. _result[ 4] = cy*sz;
  421. _result[ 5] = cx*cz + sx*sy*sz;
  422. _result[ 6] = -cz*sx + cx*sy*sz;
  423. _result[ 8] = -sy;
  424. _result[ 9] = cy*sx;
  425. _result[10] = cx*cy;
  426. _result[15] = 1.0f;
  427. };
  428. void mtxSRT(float* _result, float _sx, float _sy, float _sz, float _ax, float _ay, float _az, float _tx, float _ty, float _tz)
  429. {
  430. const float sx = fsin(_ax);
  431. const float cx = fcos(_ax);
  432. const float sy = fsin(_ay);
  433. const float cy = fcos(_ay);
  434. const float sz = fsin(_az);
  435. const float cz = fcos(_az);
  436. const float sxsz = sx*sz;
  437. const float cycz = cy*cz;
  438. _result[ 0] = _sx * (cycz - sxsz*sy);
  439. _result[ 1] = _sx * -cx*sz;
  440. _result[ 2] = _sx * (cz*sy + cy*sxsz);
  441. _result[ 3] = 0.0f;
  442. _result[ 4] = _sy * (cz*sx*sy + cy*sz);
  443. _result[ 5] = _sy * cx*cz;
  444. _result[ 6] = _sy * (sy*sz -cycz*sx);
  445. _result[ 7] = 0.0f;
  446. _result[ 8] = _sz * -cx*sy;
  447. _result[ 9] = _sz * sx;
  448. _result[10] = _sz * cx*cy;
  449. _result[11] = 0.0f;
  450. _result[12] = _tx;
  451. _result[13] = _ty;
  452. _result[14] = _tz;
  453. _result[15] = 1.0f;
  454. }
  455. void mtx3Inverse(float* _result, const float* _a)
  456. {
  457. float xx = _a[0];
  458. float xy = _a[1];
  459. float xz = _a[2];
  460. float yx = _a[3];
  461. float yy = _a[4];
  462. float yz = _a[5];
  463. float zx = _a[6];
  464. float zy = _a[7];
  465. float zz = _a[8];
  466. float det = 0.0f;
  467. det += xx * (yy*zz - yz*zy);
  468. det -= xy * (yx*zz - yz*zx);
  469. det += xz * (yx*zy - yy*zx);
  470. float invDet = 1.0f/det;
  471. _result[0] = +(yy*zz - yz*zy) * invDet;
  472. _result[1] = -(xy*zz - xz*zy) * invDet;
  473. _result[2] = +(xy*yz - xz*yy) * invDet;
  474. _result[3] = -(yx*zz - yz*zx) * invDet;
  475. _result[4] = +(xx*zz - xz*zx) * invDet;
  476. _result[5] = -(xx*yz - xz*yx) * invDet;
  477. _result[6] = +(yx*zy - yy*zx) * invDet;
  478. _result[7] = -(xx*zy - xy*zx) * invDet;
  479. _result[8] = +(xx*yy - xy*yx) * invDet;
  480. }
  481. void mtxInverse(float* _result, const float* _a)
  482. {
  483. float xx = _a[ 0];
  484. float xy = _a[ 1];
  485. float xz = _a[ 2];
  486. float xw = _a[ 3];
  487. float yx = _a[ 4];
  488. float yy = _a[ 5];
  489. float yz = _a[ 6];
  490. float yw = _a[ 7];
  491. float zx = _a[ 8];
  492. float zy = _a[ 9];
  493. float zz = _a[10];
  494. float zw = _a[11];
  495. float wx = _a[12];
  496. float wy = _a[13];
  497. float wz = _a[14];
  498. float ww = _a[15];
  499. float det = 0.0f;
  500. det += xx * (yy*(zz*ww - zw*wz) - yz*(zy*ww - zw*wy) + yw*(zy*wz - zz*wy) );
  501. det -= xy * (yx*(zz*ww - zw*wz) - yz*(zx*ww - zw*wx) + yw*(zx*wz - zz*wx) );
  502. det += xz * (yx*(zy*ww - zw*wy) - yy*(zx*ww - zw*wx) + yw*(zx*wy - zy*wx) );
  503. det -= xw * (yx*(zy*wz - zz*wy) - yy*(zx*wz - zz*wx) + yz*(zx*wy - zy*wx) );
  504. float invDet = 1.0f/det;
  505. _result[ 0] = +(yy*(zz*ww - wz*zw) - yz*(zy*ww - wy*zw) + yw*(zy*wz - wy*zz) ) * invDet;
  506. _result[ 1] = -(xy*(zz*ww - wz*zw) - xz*(zy*ww - wy*zw) + xw*(zy*wz - wy*zz) ) * invDet;
  507. _result[ 2] = +(xy*(yz*ww - wz*yw) - xz*(yy*ww - wy*yw) + xw*(yy*wz - wy*yz) ) * invDet;
  508. _result[ 3] = -(xy*(yz*zw - zz*yw) - xz*(yy*zw - zy*yw) + xw*(yy*zz - zy*yz) ) * invDet;
  509. _result[ 4] = -(yx*(zz*ww - wz*zw) - yz*(zx*ww - wx*zw) + yw*(zx*wz - wx*zz) ) * invDet;
  510. _result[ 5] = +(xx*(zz*ww - wz*zw) - xz*(zx*ww - wx*zw) + xw*(zx*wz - wx*zz) ) * invDet;
  511. _result[ 6] = -(xx*(yz*ww - wz*yw) - xz*(yx*ww - wx*yw) + xw*(yx*wz - wx*yz) ) * invDet;
  512. _result[ 7] = +(xx*(yz*zw - zz*yw) - xz*(yx*zw - zx*yw) + xw*(yx*zz - zx*yz) ) * invDet;
  513. _result[ 8] = +(yx*(zy*ww - wy*zw) - yy*(zx*ww - wx*zw) + yw*(zx*wy - wx*zy) ) * invDet;
  514. _result[ 9] = -(xx*(zy*ww - wy*zw) - xy*(zx*ww - wx*zw) + xw*(zx*wy - wx*zy) ) * invDet;
  515. _result[10] = +(xx*(yy*ww - wy*yw) - xy*(yx*ww - wx*yw) + xw*(yx*wy - wx*yy) ) * invDet;
  516. _result[11] = -(xx*(yy*zw - zy*yw) - xy*(yx*zw - zx*yw) + xw*(yx*zy - zx*yy) ) * invDet;
  517. _result[12] = -(yx*(zy*wz - wy*zz) - yy*(zx*wz - wx*zz) + yz*(zx*wy - wx*zy) ) * invDet;
  518. _result[13] = +(xx*(zy*wz - wy*zz) - xy*(zx*wz - wx*zz) + xz*(zx*wy - wx*zy) ) * invDet;
  519. _result[14] = -(xx*(yy*wz - wy*yz) - xy*(yx*wz - wx*yz) + xz*(yx*wy - wx*yy) ) * invDet;
  520. _result[15] = +(xx*(yy*zz - zy*yz) - xy*(yx*zz - zx*yz) + xz*(yx*zy - zx*yy) ) * invDet;
  521. }
  522. void calcLinearFit2D(float _result[2], const void* _points, uint32_t _stride, uint32_t _numPoints)
  523. {
  524. float sumX = 0.0f;
  525. float sumY = 0.0f;
  526. float sumXX = 0.0f;
  527. float sumXY = 0.0f;
  528. const uint8_t* ptr = (const uint8_t*)_points;
  529. for (uint32_t ii = 0; ii < _numPoints; ++ii, ptr += _stride)
  530. {
  531. const float* point = (const float*)ptr;
  532. float xx = point[0];
  533. float yy = point[1];
  534. sumX += xx;
  535. sumY += yy;
  536. sumXX += xx*xx;
  537. sumXY += xx*yy;
  538. }
  539. // [ sum(x^2) sum(x) ] [ A ] = [ sum(x*y) ]
  540. // [ sum(x) numPoints ] [ B ] [ sum(y) ]
  541. float det = (sumXX*_numPoints - sumX*sumX);
  542. float invDet = 1.0f/det;
  543. _result[0] = (-sumX * sumY + _numPoints * sumXY) * invDet;
  544. _result[1] = (sumXX * sumY - sumX * sumXY) * invDet;
  545. }
  546. void calcLinearFit3D(float _result[3], const void* _points, uint32_t _stride, uint32_t _numPoints)
  547. {
  548. float sumX = 0.0f;
  549. float sumY = 0.0f;
  550. float sumZ = 0.0f;
  551. float sumXX = 0.0f;
  552. float sumXY = 0.0f;
  553. float sumXZ = 0.0f;
  554. float sumYY = 0.0f;
  555. float sumYZ = 0.0f;
  556. const uint8_t* ptr = (const uint8_t*)_points;
  557. for (uint32_t ii = 0; ii < _numPoints; ++ii, ptr += _stride)
  558. {
  559. const float* point = (const float*)ptr;
  560. float xx = point[0];
  561. float yy = point[1];
  562. float zz = point[2];
  563. sumX += xx;
  564. sumY += yy;
  565. sumZ += zz;
  566. sumXX += xx*xx;
  567. sumXY += xx*yy;
  568. sumXZ += xx*zz;
  569. sumYY += yy*yy;
  570. sumYZ += yy*zz;
  571. }
  572. // [ sum(x^2) sum(x*y) sum(x) ] [ A ] [ sum(x*z) ]
  573. // [ sum(x*y) sum(y^2) sum(y) ] [ B ] = [ sum(y*z) ]
  574. // [ sum(x) sum(y) numPoints ] [ C ] [ sum(z) ]
  575. float mtx[9] =
  576. {
  577. sumXX, sumXY, sumX,
  578. sumXY, sumYY, sumY,
  579. sumX, sumY, float(_numPoints),
  580. };
  581. float invMtx[9];
  582. mtx3Inverse(invMtx, mtx);
  583. _result[0] = invMtx[0]*sumXZ + invMtx[1]*sumYZ + invMtx[2]*sumZ;
  584. _result[1] = invMtx[3]*sumXZ + invMtx[4]*sumYZ + invMtx[5]*sumZ;
  585. _result[2] = invMtx[6]*sumXZ + invMtx[7]*sumYZ + invMtx[8]*sumZ;
  586. }
  587. void rgbToHsv(float _hsv[3], const float _rgb[3])
  588. {
  589. const float rr = _rgb[0];
  590. const float gg = _rgb[1];
  591. const float bb = _rgb[2];
  592. const float s0 = fstep(bb, gg);
  593. const float px = flerp(bb, gg, s0);
  594. const float py = flerp(gg, bb, s0);
  595. const float pz = flerp(-1.0f, 0.0f, s0);
  596. const float pw = flerp(2.0f/3.0f, -1.0f/3.0f, s0);
  597. const float s1 = fstep(px, rr);
  598. const float qx = flerp(px, rr, s1);
  599. const float qy = py;
  600. const float qz = flerp(pw, pz, s1);
  601. const float qw = flerp(rr, px, s1);
  602. const float dd = qx - fmin(qw, qy);
  603. const float ee = 1.0e-10f;
  604. _hsv[0] = fabs(qz + (qw - qy) / (6.0f * dd + ee) );
  605. _hsv[1] = dd / (qx + ee);
  606. _hsv[2] = qx;
  607. }
  608. void hsvToRgb(float _rgb[3], const float _hsv[3])
  609. {
  610. const float hh = _hsv[0];
  611. const float ss = _hsv[1];
  612. const float vv = _hsv[2];
  613. const float px = fabs(ffract(hh + 1.0f ) * 6.0f - 3.0f);
  614. const float py = fabs(ffract(hh + 2.0f/3.0f) * 6.0f - 3.0f);
  615. const float pz = fabs(ffract(hh + 1.0f/3.0f) * 6.0f - 3.0f);
  616. _rgb[0] = vv * flerp(1.0f, fsaturate(px - 1.0f), ss);
  617. _rgb[1] = vv * flerp(1.0f, fsaturate(py - 1.0f), ss);
  618. _rgb[2] = vv * flerp(1.0f, fsaturate(pz - 1.0f), ss);
  619. }
  620. } // namespace bx