math.cpp 21 KB

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