Guest User

Untitled

a guest
Sep 10th, 2017
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 31.98 KB | None | 0 0
  1. #pragma once
  2.  
  3. // -- constructors
  4. #ifndef INLINE
  5. #ifdef _MSC_VER
  6. #if (_MSC_VER >= 1200)
  7. #define INLINE __forceinline
  8. #else
  9. #define INLINE __inline
  10. #endif
  11. #endif
  12. #endif
  13.  
  14. #include "macros.h"
  15. #include "zstring.h"
  16.  
  17.  
  18. #pragma warning(disable : 4244)
  19. #pragma warning(disable : 4305)
  20.  
  21. /** Insert description.
  22. */
  23. INLINE zVEC2::zVEC2()
  24. {
  25.     //XCALL(0x0052ED60);
  26. }
  27.  
  28. /** Insert description.
  29. ????
  30. */
  31. /*
  32. INLINE zVEC2::zVEC2(float f)
  33. {
  34.     n[0] = f;
  35.     n[1] = f;
  36. }
  37. */
  38. /** Insert description.
  39. */
  40. INLINE zVEC2::zVEC2(float x, float y)
  41. {
  42.     XCALL(0x004B4DC0);
  43. }
  44.  
  45. /** Insert description.
  46. */
  47. INLINE zVEC2& zVEC2::operator+=(zVEC2 const& other)
  48. {
  49.     XCALL(0x005B02F0);
  50. }
  51.  
  52. /** Insert description.
  53. */
  54. INLINE zVEC2& zVEC2::operator-=(zVEC2 const& other)
  55. {
  56.     XCALL(0x00691020);
  57. }
  58.  
  59. /** Insert description.
  60. */
  61. INLINE zVEC2& zVEC2::operator*=(float fm)
  62. {
  63.     XCALL(0x00691040);
  64. }
  65.  
  66. /** Insert description.
  67. */
  68. INLINE zVEC2& zVEC2::operator/=(float value)
  69. {
  70.     XCALL(0x004B4DE0);
  71. }
  72.  
  73. /** Insert description.
  74. */
  75. /*
  76. INLINE zVEC2& zVEC2::Normalize()
  77. {
  78.     double norm = 1.0 / sqrt(Length2());
  79.     n[0] *= norm;
  80.     n[1] *= norm;
  81.     return *this;
  82. }
  83. */
  84.  
  85. /** Insert description.
  86. */
  87. /*
  88. INLINE BOOL zVEC2::IsEqualEps(zVEC2 const& other) const
  89. {
  90.     return (fabs(n[0] - other.n[0]) < 0.001)
  91.         && (fabs(n[1] - other.n[1]) < 0.001);
  92. }
  93. */
  94. /** Insert description.
  95. */
  96. /*
  97. INLINE zVEC2& zVEC2::Apply(float (*func)(float))
  98. {
  99.     n[0] = func(n[0]);
  100.     n[1] = func(n[1]);
  101.     return *this;
  102. }
  103. */
  104.  
  105. inline zSTRING zVEC2::GetDescription() const
  106. {
  107.     XCALL(0x00512A30);
  108. }
  109.  
  110. inline void zVEC2::SetByDescription(zSTRING const& value)
  111. {
  112.     XCALL(0x00512C70);
  113. }
  114.  
  115. /** Insert description.
  116. */
  117. inline float zVEC2::Length2()
  118. {
  119.     XCALL(0x004B4E00);
  120. }
  121.  
  122. /** Insert description.
  123. */
  124. inline float operator*(zVEC2 const& va, zVEC2 const& vb)
  125. {
  126.     XCALL(0x004B4E20);
  127. }
  128.  
  129. //.text:00514220 ; int __cdecl operator*(int, float, int)
  130.  
  131. /** Insert description.
  132. */
  133. INLINE zVEC2 operator+(zVEC2 const& va, zVEC2 const& vb)
  134. {
  135.     XCALL(0x0056CB80);
  136. }
  137.  
  138. /** Insert description.
  139. */
  140. INLINE zVEC2 operator-(zVEC2 const& va, zVEC2 const& vb)
  141. {
  142.     XCALL(0x006011C0);
  143. }
  144.  
  145. /** Insert description.
  146. */
  147. INLINE zVEC2 operator*(zVEC2 const& v, float fm)
  148. {
  149.     XCALL(0x00499480);
  150. }
  151.  
  152. /** Insert description.
  153. */
  154. INLINE zVEC2 operator/(zVEC2 const& v, float fd)
  155. {
  156.     return zVEC2(v[0] / fd, v[1] / fd);
  157. }
  158.  
  159. /** Insert description.
  160. */
  161. INLINE zVEC2 operator-(zVEC2 const& v)
  162. {
  163.     return zVEC2(-v[0], -v[1]);
  164. }
  165.  
  166. /** Insert description.
  167. */
  168. INLINE float Alg_SqrtInvApprox(float value)
  169. {
  170.     static float kf1 = 0.5;
  171.     static float kf2 = 1.5;
  172.     float sqrtInv;
  173.     _asm
  174.     {
  175.         fld     value
  176.         fmul    kf1
  177.         mov     eax, value
  178.         mov     ecx, 5F3759DFh
  179.         shr     eax, 1
  180.         sub     ecx, eax
  181.         mov     value, ecx
  182.         fmul    value
  183.         fld     value
  184.         fmul    value
  185.         fld     kf2
  186.         fmul    value
  187.         fxch    st(2)
  188.         fmulp   st(1), st
  189.         fsubp   st(1), st
  190.         fstp    sqrtInv
  191.     }
  192.     return sqrtInv;
  193. }
  194.  
  195. /** Insert description.
  196. */
  197. INLINE zVEC3::zVEC3()
  198. {
  199.     n[0] = 0;
  200.     n[1] = 0;
  201.     n[2] = 0;
  202. }
  203.  
  204. /** Insert description.
  205. */
  206. INLINE zVEC3::zVEC3(float f)
  207. {
  208.     n[0] = f;
  209.     n[1] = f;
  210.     n[2] = f;
  211. }
  212.  
  213. /** Insert description.
  214. */
  215. INLINE zVEC3::zVEC3(float x, float y, float z)
  216. {
  217.     n[0] = x;
  218.     n[1] = y;
  219.     n[2] = z;
  220. }
  221.  
  222. /** Insert description.
  223. */
  224. INLINE zVEC3& zVEC3::operator+=(zVEC3 const& other)
  225. {
  226.     n[0] += other.n[0];
  227.     n[1] += other.n[1];
  228.     n[2] += other.n[2];
  229.     return *this;
  230. }
  231.  
  232. /** Insert description.
  233. */
  234. INLINE zVEC3& zVEC3::operator-=(zVEC3 const& other)
  235. {
  236.     n[0] -= other.n[0];
  237.     n[1] -= other.n[1];
  238.     n[2] -= other.n[2];
  239.     return *this;
  240. }
  241.  
  242. /** Insert description.
  243. */
  244. INLINE zVEC3& zVEC3::operator*=(float fm)
  245. {
  246.     n[0] *= fm;
  247.     n[1] *= fm;
  248.     n[2] *= fm;
  249.     return *this;
  250. }
  251.  
  252. /** Insert description.
  253. */
  254. INLINE zVEC3& zVEC3::operator/=(float fm)
  255. {
  256.     n[0] /= fm;
  257.     n[1] /= fm;
  258.     n[2] /= fm;
  259.     return *this;
  260. }
  261.  
  262. /** Insert description.
  263. */
  264. INLINE float zVEC3::LengthApprox() const
  265. {
  266.     float tmp;
  267.     float v0 = fabs(n[0]);
  268.     float v1 = fabs(n[1]);
  269.     float v2 = fabs(n[2]);
  270.     if(v0 < v1)
  271.     {
  272.         tmp = v0;
  273.         v0 = v1;
  274.         v1 = tmp;
  275.     }
  276.     if(v0 < v2)
  277.     {
  278.         tmp = v0;
  279.         v0 = v2;
  280.         v2 = tmp;
  281.     }
  282.     v1 += v2;
  283.     return v0 - v0 * 0.0625 + v1 * 0.25 + v1 * 0.125;
  284. }
  285.  
  286. /** Insert description.
  287. */
  288. INLINE float zVEC3::Distance(const zVEC3& other) const
  289. {
  290.     zVEC3 d;
  291.     d[0] = n[0] - other[0];
  292.     d[1] = n[1] - other[1];
  293.     d[2] = n[2] - other[2];
  294.     return sqrt(d[0] * d[0] + d[1] * d[1] + d[2] * d[2]);
  295. }
  296.  
  297. /** Insert description.
  298. */
  299. INLINE float zVEC3::DistanceAbs(const zVEC3& other) const
  300. {
  301.     zVEC3 d;
  302.     d[0] = fabs(n[0] - other[0]);
  303.     d[1] = fabs(n[1] - other[1]);
  304.     d[2] = fabs(n[2] - other[2]);
  305.     return d[0] + d[1] + d[2];
  306. }
  307.  
  308. /** Insert description.
  309. */
  310. INLINE zVEC3& zVEC3::Normalize()
  311. {
  312.     double norm = 1.0 / sqrt(Length2());
  313.     n[0] *= norm;
  314.     n[1] *= norm;
  315.     n[2] *= norm;
  316.     return *this;
  317. }
  318.  
  319. /** Insert description.
  320. */
  321. INLINE zVEC3& zVEC3::NormalizeSafe()
  322. {
  323.     double length = sqrt(Length2());
  324.     double norm = 1.0 / (length ? length : 1.0e-6);
  325.     n[0] *= norm;
  326.     n[1] *= norm;
  327.     n[2] *= norm;
  328.     return *this;
  329. }
  330.  
  331. /** Insert description.
  332. */
  333. INLINE zVEC3& zVEC3::NormalizeApprox()
  334. {
  335.     double norm = Alg_SqrtInvApprox(Length2());
  336.     n[0] *= norm;
  337.     n[1] *= norm;
  338.     n[2] *= norm;
  339.     return *this;
  340. }
  341.  
  342. /** Insert description.
  343. */
  344. INLINE BOOL zVEC3::IsEqualEps(zVEC3 const& other) const
  345. {
  346.     return (fabs(n[0] - other.n[0]) < 0.001)
  347.         && (fabs(n[1] - other.n[1]) < 0.001)
  348.         && (fabs(n[2] - other.n[2]) < 0.001);
  349. }
  350.  
  351. INLINE zSTRING zVEC3::GetString() const
  352. {
  353.         XCALL(0x005375D0);
  354.  
  355. };
  356.  
  357. INLINE void zVEC3::SetByDescription(zSTRING const& value)
  358. {
  359.     n[0] = atof(value.PickWord(1, ""," "));
  360.     n[1] = atof(value.PickWord(2, ""," "));
  361.     n[2] = atof(value.PickWord(2, ""," "));
  362. }
  363.  
  364. /** Insert description.
  365. */
  366. INLINE zVEC3& zVEC3::Apply(float (*func)(float))
  367. {
  368.     n[0] = func(n[0]);
  369.     n[1] = func(n[1]);
  370.     n[2] = func(n[2]);
  371.     return *this;
  372. }
  373.  
  374. /** Insert description.
  375. */
  376. INLINE float zVEC3::MultAbs(const zVEC3& other) const
  377. {
  378.     return fabs(n[0] * other[0]) + fabs(n[1] * other[1]) + fabs(n[2] * other[2]);
  379. }
  380.  
  381. /** Insert description.
  382. */
  383. INLINE BOOL operator==(zVEC3 const& va, zVEC3 const& vb)
  384. {
  385.     return (va[0] == vb[0]) && (va[1] == vb[1]) && (va[2] == vb[2]);
  386. }
  387.  
  388. /** Insert description.
  389. */
  390. INLINE zVEC3 operator+(zVEC3 const& va, zVEC3 const& vb)
  391. {
  392.     return zVEC3(va[0] + vb[0], va[1] + vb[1], va[2] + vb[2]);
  393. }
  394.  
  395. /** Insert description.
  396. */
  397. INLINE zVEC3 operator-(zVEC3 const& va, zVEC3 const& vb)
  398. {
  399.     return zVEC3(va[0] - vb[0], va[1] - vb[1], va[2] - vb[2]);
  400. }
  401.  
  402. /** Insert description.
  403. */
  404. INLINE zVEC3 operator^(zVEC3 const& va, zVEC3 const& vb)
  405. {
  406.     return zVEC3(va[1] * vb[2] - va[2] * vb[1],
  407.         va[2] * vb[0] - va[0] * vb[2],
  408.         va[0] * vb[1] - va[1] * vb[0]);
  409. }
  410.  
  411. /** Insert description.
  412. */
  413. INLINE zVEC3 operator*(zVEC3 const& v, float fm)
  414. {
  415.     return zVEC3(v[0] * fm, v[1] * fm, v[2] * fm);
  416. }
  417.  
  418. /** Insert description.
  419. */
  420. INLINE zVEC3 operator/(zVEC3 const& v, float fd)
  421. {
  422.     return zVEC3(v[0] / fd, v[1] / fd, v[2] / fd);
  423. }
  424.  
  425. /** Insert description.
  426. */
  427. INLINE zVEC3 operator-(zVEC3 const& v)
  428. {
  429.     return zVEC3(-v[0], -v[1], -v[2]);
  430. }
  431.  
  432. INLINE zVEC3 String2Vec3(zSTRING const& str)
  433. {
  434.     float v1 = atof(str.PickWord(0, " "," "));
  435.     float v2 = atof(str.PickWord(1, " "," "));
  436.     float v3 = atof(str.PickWord(2, " "," "));
  437.     return zVEC3(v1, v2, v3);
  438. }
  439.  
  440. /* Get ALL coord*/
  441. //INLINE float *Get3V()
  442. //{
  443. //  return zVEC3::coord;
  444. //}
  445.  
  446. INLINE float zVEC3::GetX()
  447. {
  448.     return n[0];
  449. }
  450.  
  451. /* Get Y coord*/
  452. INLINE float zVEC3::GetY()
  453. {
  454.     return n[1];
  455. }
  456.  
  457. /* Get Z Coord */
  458. INLINE float zVEC3::GetZ()
  459. {
  460.     return n[2];
  461. }
  462.  
  463. /** Insert description.
  464. */
  465. INLINE zVEC4::zVEC4()
  466. {
  467.     n[0] = 0;
  468.     n[1] = 0;
  469.     n[2] = 0;
  470.     n[3] = 0;
  471. }
  472.  
  473. /** Insert description.
  474. */
  475. INLINE zVEC4::zVEC4(float f)
  476. {
  477.     n[0] = f;
  478.     n[1] = f;
  479.     n[2] = f;
  480.     n[3] = f;
  481. }
  482.  
  483. /** Insert description.
  484. */
  485. INLINE zVEC4::zVEC4(zVEC3 const& v, float t)
  486. {
  487.     n[0] = v[0];
  488.     n[1] = v[1];
  489.     n[2] = v[2];
  490.     n[3] = t;
  491. }
  492.  
  493. /** Insert description.
  494. */
  495. INLINE zVEC4::zVEC4(float x, float y, float z, float t)
  496. {
  497.     n[0] = x;
  498.     n[1] = y;
  499.     n[2] = z;
  500.     n[3] = t;
  501. }
  502.  
  503. /** Insert description.
  504. */
  505. INLINE zVEC4& zVEC4::operator+=(zVEC4 const& other)
  506. {
  507.     n[0] += other.n[0];
  508.     n[1] += other.n[1];
  509.     n[2] += other.n[2];
  510.     n[3] += other.n[3];
  511.     return *this;
  512. }
  513.  
  514. /** Insert description.
  515. */
  516. INLINE zVEC4& zVEC4::operator-=(zVEC4 const& other)
  517. {
  518.     n[0] -= other.n[0];
  519.     n[1] -= other.n[1];
  520.     n[2] -= other.n[2];
  521.     n[3] -= other.n[3];
  522.     return *this;
  523. }
  524.  
  525. /** Insert description.
  526. */
  527. INLINE zVEC4& zVEC4::operator*=(float fm)
  528. {
  529.     n[0] *= fm;
  530.     n[1] *= fm;
  531.     n[2] *= fm;
  532.     n[3] *= fm;
  533.     return *this;
  534. }
  535.  
  536. /** Insert description.
  537. */
  538. INLINE float zVEC4::LengthApprox() const
  539. {
  540.     float tmp;
  541.     float v0 = fabs(n[0]);
  542.     float v1 = fabs(n[1]);
  543.     float v2 = fabs(n[2]);
  544.     float v3 = fabs(n[3]);
  545.     if(v0 < v1)
  546.     {
  547.         tmp = v0;
  548.         v0 = v1;
  549.         v1 = tmp;
  550.     }
  551.     if(v2 < v3)
  552.     {
  553.         tmp = v2;
  554.         v2 = v3;
  555.         v3 = tmp;
  556.     }
  557.     if(v0 < v2)
  558.     {
  559.         tmp = v0;
  560.         v0 = v2;
  561.         v2 = tmp;
  562.     }
  563.     if(v1 < v3)
  564.     {
  565.         tmp = v1;
  566.         v1 = v3;
  567.         v3 = tmp;
  568.     }
  569.     if(v1 < v2)
  570.     {
  571.         tmp = v1;
  572.         v1 = v2;
  573.         v2 = tmp;
  574.     }
  575.     v1 += v2;
  576.     return v0 / 60.0 + v1 * 25.0 + v2 * 19.0 + v3 * 16.0;
  577. }
  578.  
  579. /** Insert description.
  580. */
  581. INLINE zVEC4& zVEC4::Normalize()
  582. {
  583.     double norm = 1.0 / sqrt(n[0] * n[0] + n[1] * n[1] + n[2] * n[2] + n[3] * n[3]);
  584.     n[0] *= norm;
  585.     n[1] *= norm;
  586.     n[2] *= norm;
  587.     n[3] *= norm;
  588.     return *this;
  589. }
  590.  
  591. /** Insert description.
  592. */
  593. INLINE zVEC4& zVEC4::Apply(float (*func)(float))
  594. {
  595.     n[0] = func(n[0]);
  596.     n[1] = func(n[1]);
  597.     n[2] = func(n[2]);
  598.     n[3] = func(n[3]);
  599.     return *this;
  600. }
  601.  
  602. /** Insert description.
  603. */
  604. INLINE BOOL operator==(zVEC4 const& va, zVEC4 const& vb)
  605. {
  606.     return (va[0] == vb[0]) && (va[1] == vb[1]) && (va[2] == vb[2]) && (va[3] == vb[3]);
  607. }
  608.  
  609. /** Insert description.
  610. */
  611. INLINE zVEC4 operator+(zVEC4 const& va, zVEC4 const& vb)
  612. {
  613.     return zVEC4(va[0] + vb[0], va[1] + vb[1], va[2] + vb[2], va[3] + vb[3]);
  614. }
  615.  
  616. /** Insert description.
  617. */
  618. INLINE zVEC4 operator-(zVEC4 const& va, zVEC4 const& vb)
  619. {
  620.     return zVEC4(va[0] - vb[0], va[1] - vb[1], va[2] - vb[2], va[3] - vb[3]);
  621. }
  622.  
  623. /** Insert description.
  624. */
  625. INLINE zVEC4 operator*(zVEC4 const& v, float f)
  626. {
  627.     return zVEC4(v[0] * f, v[1] * f, v[2] * f, v[3] * f);
  628. }
  629.  
  630. /** Insert description.
  631. */
  632. INLINE zVEC4 operator-(zVEC4 const& v)
  633. {
  634.     return zVEC4(-v[0], -v[1], -v[2], -v[3]);
  635. }
  636.  
  637. /** Insert description.
  638. */
  639. INLINE zMAT3::zMAT3(zVEC3 const& v0, zVEC3 const& v1, zVEC3 const& v2)
  640. {
  641.     row[0] = v0;
  642.     row[1] = v1;
  643.     row[2] = v2;
  644. }
  645.  
  646. /** Insert description.
  647. */
  648. INLINE zMAT3::zMAT3(float f)
  649. {
  650.     row[0] = f;
  651.     row[1] = f;
  652.     row[2] = f;
  653. }
  654.  
  655. /** Insert description.
  656. */
  657. INLINE zMAT3& zMAT3::operator+=(zMAT3 const& m)
  658. {
  659.     row[0] += m.row[0];
  660.     row[1] += m.row[1];
  661.     row[2] += m.row[2];
  662.     return *this;
  663. }
  664.  
  665. /** Insert description.
  666. */
  667. INLINE zMAT3& zMAT3::operator-=(zMAT3 const& m)
  668. {
  669.     row[0] -= m.row[0];
  670.     row[1] -= m.row[1];
  671.     row[2] -= m.row[2];
  672.     return *this;
  673. }
  674.  
  675. /** Insert description.
  676. */
  677. INLINE zMAT3& zMAT3::operator*=(float f)
  678. {
  679.     row[0] *= f;
  680.     row[1] *= f;
  681.     row[2] *= f;
  682.     return *this;
  683. }
  684.  
  685. /** Insert description.
  686. */
  687. INLINE zMAT3& zMAT3::operator/=(float f)
  688. {
  689.     row[0] /= f;
  690.     row[1] /= f;
  691.     row[2] /= f;
  692.     return *this;
  693. }
  694.  
  695. /** Insert description.
  696. */
  697. INLINE zMAT3 zMAT3::Transpose() const
  698. {
  699.     return zMAT3(zVEC3(row[0][0], row[1][0], row[2][0]),
  700.         zVEC3(row[0][1], row[1][1], row[2][1]),
  701.         zVEC3(row[0][2], row[1][2], row[2][2]));
  702. }
  703.  
  704. /** Insert description.
  705. */
  706. INLINE zMAT3 zMAT3::Inverse(float* pf)
  707. {
  708.     float kf = (row[1] ^ row[2]) * row[0];
  709.     if(pf)
  710.         *pf = kf;
  711.     kf = 1.0 / kf;
  712.     zVEC3 v0 = zVEC3(
  713.         row[2][2] * row[1][1] - row[2][1] * row[1][2],
  714.         row[2][1] * row[0][2] - row[2][2] * row[0][1],
  715.         row[1][2] * row[0][1] - row[1][1] * row[0][2]);
  716.     zVEC3 v1 = zVEC3(
  717.         row[2][0] * row[1][2] - row[2][1] * row[1][0],
  718.         row[2][2] * row[0][0] - row[2][0] * row[0][2],
  719.         row[1][0] * row[0][2] - row[1][2] * row[0][0]);
  720.     zVEC3 v2 = zVEC3(
  721.         row[2][1] * row[1][0] - row[2][0] * row[1][2],
  722.         row[2][0] * row[0][1] - row[2][1] * row[0][1],
  723.         row[1][1] * row[0][0] - row[1][0] * row[0][1]);
  724.     return kf * zMAT3(v0, v1, v2);
  725. }
  726.  
  727. /** Insert description.
  728. */
  729. INLINE void zMAT3::SetAtVector(zVEC3 const& v)
  730. {
  731.     row[0][2] = v[0];
  732.     row[1][2] = v[1];
  733.     row[2][2] = v[2];
  734. }
  735.  
  736. /** Insert description.
  737. */
  738. INLINE zVEC3 zMAT3::GetAtVector() const
  739. {
  740.     return zVEC3(row[0][2], row[1][2], row[2][2]);
  741. }
  742.  
  743. /** Insert description.
  744. */
  745. INLINE void zMAT3::SetUpVector(zVEC3 const& v)
  746. {
  747.     row[0][1] = v[0];
  748.     row[1][1] = v[1];
  749.     row[2][1] = v[2];
  750. }
  751.  
  752. /** Insert description.
  753. */
  754. INLINE zVEC3 zMAT3::GetUpVector() const
  755. {
  756.     return zVEC3(row[0][1], row[1][1], row[2][1]);
  757. }
  758.  
  759. /** Insert description.
  760. */
  761. INLINE void zMAT3::SetRightVector(zVEC3 const& v)
  762. {
  763.     row[0][0] = v[0];
  764.     row[1][0] = v[1];
  765.     row[2][0] = v[2];
  766. }
  767.  
  768. /** Insert description.
  769. */
  770. INLINE zVEC3 zMAT3::GetRightVector() const
  771. {
  772.     return zVEC3(row[0][0], row[1][0], row[2][0]);
  773. }
  774.  
  775. /** Insert description.
  776. */
  777. INLINE void zMAT3::MakeOrthonormal()
  778. {
  779.     zVEC3 v1 = GetAtVector();
  780.     v1.NormalizeSafe();
  781.     zVEC3 vec = v1 * -(v1 * GetUpVector());
  782.     zVEC3 v2 = vec + GetUpVector();
  783.     v2.NormalizeSafe();
  784.     vec = v2 ^ v1;
  785.     SetAtVector(v1);
  786.     SetUpVector(v2);
  787.     SetRightVector(vec);
  788. }
  789.  
  790. /** Insert description.
  791. */
  792. INLINE zMAT3& zMAT3::Apply(float (*func)(float))
  793. {
  794.     row[0].Apply(func);
  795.     row[1].Apply(func);
  796.     row[2].Apply(func);
  797.     return *this;
  798. }
  799.  
  800. /** Insert description.
  801. */
  802. INLINE zMAT3 operator-(zMAT3 const& m)
  803. {
  804.     return zMAT3(-m[0], -m[1], -m[2]);
  805. }
  806.  
  807. /** Insert description.
  808. */
  809. INLINE zMAT3 operator+(zMAT3 const& ma, zMAT3 const& mb)
  810. {
  811.     return zMAT3(ma[0] + mb[0], ma[1] + mb[1], ma[2] + mb[2]);
  812. }
  813.  
  814. /** Insert description.
  815. */
  816. INLINE zMAT3 operator-(zMAT3 const& ma, zMAT3 const& mb)
  817. {
  818.     return zMAT3(ma[0] - mb[0], ma[1] - mb[1], ma[2] - mb[2]);
  819. }
  820.  
  821. /** Insert description.
  822. */
  823. INLINE zMAT3 operator*(zMAT3 const& ma, zMAT3 const& mb)
  824. {
  825.     zVEC3 v0 = zVEC3(
  826.         mb[0][0] * ma[0][0] + mb[1][0] * ma[0][1] + mb[2][0] * ma[0][2],
  827.         mb[0][1] * ma[0][0] + mb[1][1] * ma[0][1] + mb[2][1] * ma[0][2],
  828.         mb[0][2] * ma[0][0] + mb[1][2] * ma[0][1] + mb[2][2] * ma[0][2]);
  829.     zVEC3 v1 = zVEC3(
  830.         mb[0][0] * ma[1][0] + mb[1][0] * ma[1][1] + mb[2][0] * ma[1][2],
  831.         mb[0][1] * ma[1][0] + mb[1][1] * ma[1][1] + mb[2][1] * ma[1][2],
  832.         mb[0][2] * ma[1][0] + mb[1][2] * ma[1][1] + mb[2][2] * ma[1][2]);
  833.     zVEC3 v2 = zVEC3(
  834.         mb[0][0] * ma[2][0] + mb[1][0] * ma[2][1] + mb[2][0] * ma[2][2],
  835.         mb[0][1] * ma[2][0] + mb[1][1] * ma[2][1] + mb[2][1] * ma[2][2],
  836.         mb[0][2] * ma[2][0] + mb[1][2] * ma[2][1] + mb[2][2] * ma[2][2]);
  837.     return zMAT3(v0, v1, v2);
  838. }
  839.  
  840. /** Insert description.
  841. */
  842. INLINE zVEC3 operator*(zMAT3 const& m, zVEC3 const& v)
  843. {
  844.     return zVEC3(m[0] * v, m[1] * v, m[2] * v);
  845. }
  846.  
  847. /** Insert description.
  848. */
  849. INLINE zVEC2 operator*(zMAT3 const& m, zVEC2 const& v)
  850. {
  851.     zVEC2 vr(m[0][0] * v[0] + m[0][1] * v[1] + m[0][2], m[1][0] * v[0] + m[1][1] * v[1] + m[1][2]);
  852.     float scale = 1.0 / (m[2][0] * v[0] + m[2][1] * v[1] + m[2][2]);
  853.     return vr * scale;
  854. }
  855.  
  856. /** Insert description.
  857. */
  858. INLINE zMAT3 operator*(zMAT3 const& m, float f)
  859. {
  860.     return zMAT3(m[0] * f, m[1] * f, m[2] * f);
  861. }
  862.  
  863. /** Insert description.
  864. */
  865. INLINE zMAT3 operator*(float f, zMAT3 const& m)
  866. {
  867.     return zMAT3(m[0] * f, m[1] * f, m[2] * f);
  868. }
  869.  
  870. /** Insert description.
  871. */
  872. INLINE zMAT3 operator/(zMAT3 const& m, float f)
  873. {
  874.     return zMAT3(m[0] / f, m[1] / f, m[2] / f);
  875. }
  876.  
  877. /** Insert description.
  878. */
  879. INLINE BOOL operator==(zMAT3 const& ma, zMAT3 const& mb)
  880. {
  881.     return (ma[0] == mb[0]) && (ma[1] == mb[1]) && (ma[2] == mb[2]);
  882. }
  883.  
  884. /** Insert description.
  885. */
  886. INLINE BOOL operator!=(zMAT3 const& ma, zMAT3 const& mb)
  887. {
  888.     return ((ma[0] == mb[0]) && (ma[1] == mb[1]) && (ma[2] == mb[2])) == FALSE;
  889. }
  890.  
  891. /** Insert description.
  892. */
  893. INLINE zMAT4::zMAT4(zVEC4 const& v0, zVEC4 const& v1, zVEC4 const& v2, zVEC4 const& v3)
  894. {
  895.     row[0] = v0;
  896.     row[1] = v1;
  897.     row[2] = v2;
  898.     row[3] = v3;
  899. }
  900.  
  901. /** Insert description.
  902. */
  903. INLINE zMAT4::zMAT4(float f)
  904. {
  905.     row[0] = f;
  906.     row[1] = f;
  907.     row[2] = f;
  908.     row[3] = f;
  909. }
  910.  
  911. /** Insert description.
  912. */
  913. INLINE zMAT4& zMAT4::operator+=(zMAT4 const& m)
  914. {
  915.     row[0] += m.row[0];
  916.     row[1] += m.row[1];
  917.     row[2] += m.row[2];
  918.     row[3] += m.row[3];
  919.     return *this;
  920. }
  921.  
  922. /** Insert description.
  923. */
  924. INLINE zMAT4& zMAT4::operator-=(zMAT4 const& m)
  925. {
  926.     row[0] -= m.row[0];
  927.     row[1] -= m.row[1];
  928.     row[2] -= m.row[2];
  929.     row[3] -= m.row[3];
  930.     return *this;
  931. }
  932.  
  933. /** Insert description.
  934. */
  935. INLINE zMAT4& zMAT4::operator*=(float f)
  936. {
  937.     row[0] *= f;
  938.     row[1] *= f;
  939.     row[2] *= f;
  940.     row[3] *= f;
  941.     return *this;
  942. }
  943.  
  944. /** Insert description.
  945. */
  946. INLINE zMAT4& zMAT4::operator/=(float f)
  947. {
  948.     row[0] *= 1.0 / f;
  949.     row[1] *= 1.0 / f;
  950.     row[2] *= 1.0 / f;
  951.     row[3] *= 1.0 / f;
  952.     return *this;
  953. }
  954.  
  955. /** Insert description.
  956. */
  957. INLINE zMAT4 zMAT4::Transpose() const
  958. {
  959.     return zMAT4(zVEC4(row[0][0], row[1][0], row[2][0], row[3][0]),
  960.         zVEC4(row[0][1], row[1][1], row[2][1], row[3][1]),
  961.         zVEC4(row[0][2], row[1][2], row[2][2], row[3][2]),
  962.         zVEC4(row[0][3], row[1][3], row[2][3], row[3][3]));
  963. }
  964.  
  965. /** Insert description.
  966. */
  967. INLINE zMAT4 zMAT4::Inverse() const
  968. {
  969.     XCALL(0x000515500);
  970. }
  971.  
  972. /** Insert description.
  973. */
  974. INLINE zMAT4 zMAT4::InverseLinTrafo() const
  975. {
  976.     /*
  977.     zMAT4 mat(zVEC4(row[0][0], row[1][0], row [2][0], 0),
  978.         zVEC4(row[0][1], row[1][1], row[2][1], 0),
  979.         zVEC4(row[0][2], row[1][2], row[2][2], 0),
  980.         zVEC4(row[3][0], row[3][1], row[3][2], row[3][3]));
  981.     zVEC3 vec(row[0][0] * -row[0][3] + row[1][0] * -row[1][3] + row[2][0] * -row[2][3],
  982.         row[0][1] * -row[0][3] + row[1][1] * -row[1][3] + row[2][1] * -row[2][3],
  983.         row[0][2] * -row[0][3] + row[1][2] * -row[1][3] + row[2][2] * -row[2][3]);
  984.     mat.SetTranslation(vec);
  985.     return mat;
  986.     */
  987.     XCALL(0x00515340);
  988. }
  989.  
  990. /** Insert description.
  991. */
  992. INLINE zMAT4& zMAT4::Translate(zVEC3 const& v)
  993. {
  994.     row[0][3] += v[0];
  995.     row[1][3] += v[1];
  996.     row[2][3] += v[2];
  997.     return (*this);
  998. }
  999.  
  1000. /** Insert description.
  1001. */
  1002. INLINE zMAT4& zMAT4::SetTranslation(zVEC3 const& v)
  1003. {
  1004.     row[0][3] = v[0];
  1005.     row[1][3] = v[1];
  1006.     row[2][3] = v[2];
  1007.     return (*this);
  1008. }
  1009.  
  1010. INLINE void zMAT4::LookAt(const zVEC3 target)
  1011. {
  1012.     zVEC3 f = GetTranslation() - target;
  1013.     f.Normalize();
  1014.     zVEC3 s = f.Cross(zVEC3(0, 1, 0));
  1015.     s.Normalize();
  1016.     zVEC3 v = s.Cross(f);
  1017.  
  1018.     f *= -1; //negate?
  1019.  
  1020.              //build V matrix;
  1021.     row[0][0] = s[0];
  1022.     row[1][0] = s[1];
  1023.     row[2][0] = s[2];
  1024.  
  1025.     row[0][1] = v[0];
  1026.     row[1][1] = v[1];
  1027.     row[2][1] = v[2];
  1028.  
  1029.     row[0][2] = f[0];
  1030.     row[1][2] = f[1];
  1031.     row[2][2] = f[2];
  1032. }
  1033. /** Insert description.
  1034. //.text:00408EE0 ; public: class zVEC3 __thiscall zMAT4::GetTranslation(void)const
  1035. */
  1036. INLINE zVEC3 zMAT4::GetTranslation() const
  1037. {
  1038.     XCALL(0x00408EE0);
  1039. }
  1040.  
  1041. /** Insert description.
  1042. */
  1043. INLINE void zMAT4::GetTranslation(zVEC3& v) const
  1044. {
  1045.     v[0] = row[0][3];
  1046.     v[1] = row[1][3];
  1047.     v[2] = row[2][3];
  1048. }
  1049.  
  1050. /** Insert description.
  1051. */
  1052. INLINE void zMAT4::SetAtVector(zVEC3 const& v)
  1053. {
  1054.     row[0][2] = v[0];
  1055.     row[1][2] = v[1];
  1056.     row[2][2] = v[2];
  1057. }
  1058.  
  1059. /** Insert description.
  1060. */
  1061. INLINE zVEC3 zMAT4::GetAtVector() const
  1062. {
  1063.     return zVEC3(row[0][2], row[1][2], row[2][2]);
  1064. }
  1065.  
  1066. /** Insert description.
  1067. */
  1068. INLINE void zMAT4::SetUpVector(zVEC3 const& v)
  1069. {
  1070.     row[0][1] = v[0];
  1071.     row[1][1] = v[1];
  1072.     row[2][1] = v[2];
  1073. }
  1074.  
  1075. /** Insert description.
  1076. */
  1077. INLINE zVEC3 zMAT4::GetUpVector() const
  1078. {
  1079.     return zVEC3(row[0][1], row[1][1], row[2][1]);
  1080. }
  1081.  
  1082. /** Insert description.
  1083. */
  1084. INLINE void zMAT4::SetRightVector(zVEC3 const& v)
  1085. {
  1086.     row[0][0] = v[0];
  1087.     row[1][0] = v[1];
  1088.     row[2][0] = v[2];
  1089. }
  1090.  
  1091. /** Insert description.
  1092. */
  1093. INLINE zVEC3 zMAT4::GetRightVector() const
  1094. {
  1095.     return zVEC3(row[0][0], row[1][0], row[2][0]);
  1096. }
  1097.  
  1098. /** Insert description.
  1099. */
  1100. INLINE zVEC3 zMAT4::ExtractScaling() const
  1101. {
  1102.     return zVEC3(sqrt(GetRightVector().Length2()), sqrt(GetUpVector().Length2()), sqrt(GetAtVector().Length2()));
  1103. }
  1104.  
  1105. /** Insert description.
  1106. */
  1107. INLINE zMAT3 zMAT4::ExtractRotation()
  1108. {
  1109.     zVEC3 vscale = ExtractScaling();
  1110.     zMAT4 m(zVEC4(1.0 / vscale[0], 0, 0, 0), zVEC4(0, 1.0 / vscale[1], 0, 0),
  1111.         zVEC4(0, 0, 1.0 / vscale[2], 0), zVEC4(0, 0, 0, 1.0));
  1112.     zMAT4 mr = *this * m;
  1113.     zMAT3 mrot(zVEC3(mr[0][0], mr[0][1], mr[0][2]), zVEC3(mr[1][0], mr[1][1], mr[1][2]), zVEC3(mr[2][0], mr[2][1], mr[2][2]));
  1114.     return mrot;
  1115. }
  1116.  
  1117. /** Insert description.
  1118. */
  1119. INLINE void zMAT4::MakeOrthonormal()
  1120. {
  1121.     zVEC3 v1 = GetAtVector();
  1122.     v1.NormalizeSafe();
  1123.     zVEC3 vec = v1 * -(v1 * GetUpVector());
  1124.     zVEC3 v2 = vec + GetUpVector();
  1125.     v2.NormalizeSafe();
  1126.     vec = v2 ^ v1;
  1127.     SetAtVector(v1);
  1128.     SetUpVector(v2);
  1129.     SetRightVector(vec);
  1130. }
  1131.  
  1132. /** Insert description.
  1133. */
  1134. INLINE zVEC3 zMAT4::Rotate(zVEC3 const& v) const
  1135. {
  1136.     return zVEC3(zVEC3(row[0][0], row[0][1], row[0][2]) * v,
  1137.         zVEC3(row[1][0], row[1][1], row[1][2]) * v,
  1138.         zVEC3(row[2][0], row[2][1], row[2][2]) * v);
  1139. }
  1140.  
  1141. /** Insert description.
  1142. .text:00517730 ; int __stdcall zMAT4__PostRotateX(float)
  1143. */
  1144. INLINE void zMAT4::PostRotateX(float angle)
  1145. {
  1146.     XCALL(0x00517730);
  1147. }
  1148.  
  1149. /** Insert description.
  1150. .text:00517780 ; int __stdcall zMAT4__PostRotateY(float)
  1151. */
  1152. INLINE void zMAT4::PostRotateY(float angle)
  1153. {
  1154.     XCALL(0x00517780);
  1155. }
  1156.  
  1157. /** Insert description.
  1158. .text:005177D0 ; int __stdcall zMAT4__PostRotateZ(float)
  1159. */
  1160. INLINE void zMAT4::PostRotateZ(float angle)
  1161. {
  1162.     XCALL(0x005177D0);
  1163. }
  1164.  
  1165. /** Insert description.
  1166. .text:00517820 ; public: void __thiscall zMAT4::PostScale(class zVEC3 const &)
  1167. */
  1168. INLINE void zMAT4::PostScale(zVEC3 const& scale)
  1169. {
  1170.     XCALL(0x00517820);
  1171. }
  1172.  
  1173. /** Insert description.
  1174. .text:00517840 ; public: void __thiscall zMAT4::PreScale(class zVEC3 const &)
  1175. */
  1176. INLINE void zMAT4::PreScale(zVEC3 const& scale)
  1177. {
  1178.     XCALL(0x00517840);
  1179. }
  1180.  
  1181. /** Insert description.
  1182. */
  1183. INLINE zVEC3 zMAT4::GetEulerAngles() const
  1184. {
  1185.     zCQuat qt;
  1186.     qt.Matrix4ToQuat(*this);
  1187.     zVEC3 veul;
  1188.     qt.QuatToEuler(veul);
  1189.     return veul;
  1190. }
  1191.  
  1192. /** Insert description.
  1193. */
  1194. INLINE void zMAT4::SetByEulerAngles(zVEC3 veul)
  1195. {
  1196.     zCQuat qt;
  1197.     qt.EulerToQuat(veul);
  1198.     qt.QuatToMatrix4(*this);
  1199. }
  1200.  
  1201. /** Insert description.
  1202. */
  1203. INLINE BOOL zMAT4::IsUpper3x3Orthonormal() const
  1204. {
  1205.     zVEC3 vright = GetRightVector();
  1206.     zVEC3 vat = GetAtVector();
  1207.     if(fabs(vright * vat) > 0.01)
  1208.         return FALSE;
  1209.     zVEC3 vup = GetUpVector();
  1210.     if(fabs(vup * vat) > 0.01)
  1211.         return FALSE;
  1212.     if(fabs(vright * vup) > 0.01)
  1213.         return FALSE;
  1214.     float dst = sqrt(vat.Length2());
  1215.     if((dst < 0.99) || (dst > 1.01))
  1216.         return FALSE;
  1217.     dst = sqrt(vup.Length2());
  1218.     if((dst < 0.99) || (dst > 1.01))
  1219.         return FALSE;
  1220.     dst = sqrt(vright.Length2());
  1221.     if((dst < 0.99) || (dst > 1.01))
  1222.         return FALSE;
  1223.     return TRUE;
  1224. }
  1225.  
  1226. /** Insert description.
  1227. */
  1228. INLINE zMAT4& zMAT4::Apply(float (*func)(float))
  1229. {
  1230.     row[0].Apply(func);
  1231.     row[1].Apply(func);
  1232.     row[2].Apply(func);
  1233.     row[3].Apply(func);
  1234.     return *this;
  1235. }
  1236.  
  1237. /** Insert description.
  1238. */
  1239. INLINE zMAT4 operator-(zMAT4 const& m)
  1240. {
  1241.     return zMAT4(-m[0], -m[1], -m[2], -m[3]);
  1242. }
  1243.  
  1244. /** Insert description.
  1245. */
  1246. INLINE zMAT4 operator+(zMAT4 const& ma, zMAT4 const& mb)
  1247. {
  1248.     return zMAT4(ma[0] + mb[0], ma[1] + mb[1], ma[2] + mb[2], ma[3] + mb[3]);
  1249. }
  1250.  
  1251. /** Insert description.
  1252. */
  1253. INLINE zMAT4 operator-(zMAT4 const& ma, zMAT4 const& mb)
  1254. {
  1255.     return zMAT4(ma[0] - mb[0], ma[1] - mb[1], ma[2] - mb[2], ma[3] - mb[3]);
  1256. }
  1257.  
  1258. /** Insert description.
  1259. */
  1260. INLINE zMAT4 operator*(zMAT4 const& ma, zMAT4 const& mb)
  1261. {
  1262.     zVEC4 v0 = zVEC4(
  1263.         mb[0][0]*ma[0][0] + mb[1][0]*ma[0][1] + mb[2][0]*ma[0][2] + mb[3][0]*ma[0][3],
  1264.         mb[0][1]*ma[0][0] + mb[1][1]*ma[0][1] + mb[2][1]*ma[0][2] + mb[3][1]*ma[0][3],
  1265.         mb[0][2]*ma[0][0] + mb[1][2]*ma[0][1] + mb[2][2]*ma[0][2] + mb[3][2]*ma[0][3],
  1266.         mb[0][3]*ma[0][0] + mb[1][3]*ma[0][1] + mb[2][3]*ma[0][2] + mb[3][3]*ma[0][3]);
  1267.     zVEC4 v1 = zVEC4(
  1268.         mb[0][0]*ma[1][0] + mb[1][0]*ma[1][1] + mb[2][0]*ma[1][2] + mb[3][0]*ma[1][3],
  1269.         mb[0][1]*ma[1][0] + mb[1][1]*ma[1][1] + mb[2][1]*ma[1][2] + mb[3][1]*ma[1][3],
  1270.         mb[0][2]*ma[1][0] + mb[1][2]*ma[1][1] + mb[2][2]*ma[1][2] + mb[3][2]*ma[1][3],
  1271.         mb[0][3]*ma[1][0] + mb[1][3]*ma[1][1] + mb[2][3]*ma[1][2] + mb[3][3]*ma[1][3]);
  1272.     zVEC4 v2 = zVEC4(
  1273.         mb[0][0]*ma[2][0] + mb[1][0]*ma[2][1] + mb[2][0]*ma[2][2] + mb[3][0]*ma[2][3],
  1274.         mb[0][1]*ma[2][0] + mb[1][1]*ma[2][1] + mb[2][1]*ma[2][2] + mb[3][1]*ma[2][3],
  1275.         mb[0][2]*ma[2][0] + mb[1][2]*ma[2][1] + mb[2][2]*ma[2][2] + mb[3][2]*ma[2][3],
  1276.         mb[0][3]*ma[2][0] + mb[1][3]*ma[2][1] + mb[2][3]*ma[2][2] + mb[3][3]*ma[2][3]);
  1277.     zVEC4 v3 = zVEC4(
  1278.         mb[0][0]*ma[3][0] + mb[1][0]*ma[3][1] + mb[2][0]*ma[3][2] + mb[3][0]*ma[3][3],
  1279.         mb[0][1]*ma[3][0] + mb[1][1]*ma[3][1] + mb[2][1]*ma[3][2] + mb[3][1]*ma[3][3],
  1280.         mb[0][2]*ma[3][0] + mb[1][2]*ma[3][1] + mb[2][2]*ma[3][2] + mb[3][2]*ma[3][3],
  1281.         mb[0][3]*ma[3][0] + mb[1][3]*ma[3][1] + mb[2][3]*ma[3][2] + mb[3][3]*ma[3][3]);
  1282.     return zMAT4(v0, v1, v2, v3);
  1283. }
  1284.  
  1285. /** Insert description.
  1286. */
  1287. INLINE zVEC3 operator*(zMAT4 const& m, zVEC3 const& v)
  1288. {
  1289.     return zVEC3(
  1290.         m[0][0] * v[0] + m[0][1] * v[1] + m[0][2] * v[2] + m[0][3],
  1291.         m[1][0] * v[0] + m[1][1] * v[1] + m[1][2] * v[2] + m[1][3],
  1292.         m[2][0] * v[0] + m[2][1] * v[1] + m[2][2] * v[2] + m[2][3]);
  1293. }
  1294.  
  1295. /** Insert description.
  1296. */
  1297. INLINE zMAT4 operator*(zMAT4 const& m, float f)
  1298. {
  1299.     return zMAT4(m[0] * f, m[1] * f, m[2] * f, m[3] * f);
  1300. }
  1301.  
  1302. /** Insert description.
  1303. */
  1304. INLINE zMAT4 operator*(float f, zMAT4 const& m)
  1305. {
  1306.     return zMAT4(m[0] * f, m[1] * f, m[2] * f,  m[3] * f);
  1307. }
  1308.  
  1309. /** Insert description.
  1310. */
  1311. INLINE zMAT4 operator/(zMAT4 const& m, float f)
  1312. {
  1313.     return zMAT4(m[0] * (1.0 / f), m[1] * (1.0 / f), m[2] * (1.0 / f),  m[3] * (1.0 / f));
  1314. }
  1315.  
  1316. /** Insert description.
  1317. */
  1318. INLINE BOOL operator==(zMAT4 const& ma, zMAT4 const& mb)
  1319. {
  1320.     return (ma[0] == mb[0]) && (ma[1] == mb[1]) && (ma[2] == mb[2]) && (ma[3] == mb[3]);
  1321. }
  1322.  
  1323. /** Insert description.
  1324. */
  1325. INLINE BOOL operator!=(zMAT4 const& ma, zMAT4 const& mb)
  1326. {
  1327.     return ((ma[0] == mb[0]) && (ma[1] == mb[1]) && (ma[2] == mb[2]) && (ma[3] == mb[3])) == FALSE;
  1328. }
  1329.  
  1330. /** Insert description.
  1331. */
  1332. INLINE void zCQuat::Unit()
  1333. {
  1334.     qt.Normalize();
  1335. }
  1336.  
  1337. /** Insert description.
  1338. */
  1339. INLINE void zCQuat::QuatToMatrix3(zMAT3& mat) const
  1340. {
  1341.     mat[0][0] = qt[0] * qt[0] + qt[3] * qt[3] - qt[1] * qt[1] - qt[2] * qt[2];
  1342.     mat[0][1] = (qt[0] * qt[1] + qt[2] * qt[3]) * 2;
  1343.     mat[0][2] = (qt[0] * qt[2] - qt[1] * qt[3]) * 2;
  1344.  
  1345.     mat[1][0] = (qt[0] * qt[1] - qt[2] * qt[3]) * 2;
  1346.     mat[1][1] = qt[3] * qt[3] - qt[0] * qt[0] + qt[1] * qt[1] - qt[2] * qt[2];
  1347.     mat[1][2] = (qt[1] * qt[2] + qt[0] * qt[3]) * 2;
  1348.  
  1349.     mat[2][0] = (qt[0] * qt[2] + qt[1] * qt[3]) * 2;
  1350.     mat[2][1] = (qt[1] * qt[2] - qt[0] * qt[3]) * 2;
  1351.     mat[2][2] = qt[3] * qt[3] - qt[0] * qt[0] - qt[1] * qt[1] + qt[2] * qt[2];
  1352. }
  1353.  
  1354. /** Insert description.
  1355. */
  1356. INLINE void zCQuat::QuatToMatrix4(zMAT4& mat) const
  1357. {
  1358.     mat[0][0] = qt[0] * qt[0] + qt[3] * qt[3] - qt[1] * qt[1] - qt[2] * qt[2];
  1359.     mat[0][1] = (qt[0] * qt[1] + qt[2] * qt[3]) * 2;
  1360.     mat[0][2] = (qt[0] * qt[2] - qt[1] * qt[3]) * 2;
  1361.  
  1362.     mat[1][0] = (qt[0] * qt[1] - qt[2] * qt[3]) * 2;
  1363.     mat[1][1] = qt[3] * qt[3] - qt[0] * qt[0] + qt[1] * qt[1] - qt[2] * qt[2];
  1364.     mat[1][2] = (qt[1] * qt[2] + qt[0] * qt[3]) * 2;
  1365.  
  1366.     mat[2][0] = (qt[0] * qt[2] + qt[1] * qt[3]) * 2;
  1367.     mat[2][1] = (qt[1] * qt[2] - qt[0] * qt[3]) * 2;
  1368.     mat[2][2] = qt[3] * qt[3] - qt[0] * qt[0] - qt[1] * qt[1] + qt[2] * qt[2];
  1369. }
  1370.  
  1371. /** Insert description.
  1372. */
  1373. INLINE void zCQuat::Matrix3ToQuat(zMAT3 const& mat)
  1374. {
  1375.     float dig = mat[0][0] + mat[1][1] + mat[2][2];
  1376.     if(dig > 0)
  1377.     {
  1378.         float kf = sqrt(dig + 1.0);
  1379.         qt[3] = kf * 0.5;
  1380.         kf = 0.5 / kf;
  1381.         qt[0] = (mat[1][2] - mat[2][1]) * kf;
  1382.         qt[1] = (mat[2][0] - mat[0][2]) * kf;
  1383.         qt[2] = (mat[0][1] - mat[1][0]) * kf;
  1384.         return;
  1385.     }
  1386.     int k = 0;
  1387.     if(mat[1][1] > mat[0][0])
  1388.         k = 1;
  1389.     if(mat[2][2] > mat[k][k])
  1390.         k = 2;
  1391.     int r[3] = { 1, 2, 0 };
  1392.     float kf = sqrt(mat[k][k] - (mat[r[r[k]]][r[r[k]]] + mat[r[k]][r[k]]) + 1.0);
  1393.     qt[k] = kf * 0.5;
  1394.     if(kf != 0)
  1395.         kf = 0.5 / kf;
  1396.     qt[3] = (mat[r[k]][r[r[k]]] - mat[r[r[k]]][r[k]]) * kf;
  1397.     qt[r[k]] = (mat[k][r[k]] + mat[r[k]][k]) * kf;
  1398.     qt[r[r[k]]] = (mat[k][r[r[k]]] + mat[r[r[k]]][k]) * kf;
  1399. }
  1400.  
  1401. /** Insert description.
  1402. */
  1403. INLINE void zCQuat::Matrix4ToQuat(zMAT4 const& mat)
  1404. {
  1405.     float dig = mat[0][0] + mat[1][1] + mat[2][2];
  1406.     if(dig > 0)
  1407.     {
  1408.         float kf = sqrt(dig + 1.0);
  1409.         qt[3] = kf * 0.5;
  1410.         kf = 0.5 / kf;
  1411.         qt[0] = (mat[1][2] - mat[2][1]) * kf;
  1412.         qt[1] = (mat[2][0] - mat[0][2]) * kf;
  1413.         qt[2] = (mat[0][1] - mat[1][0]) * kf;
  1414.         return;
  1415.     }
  1416.     int k = 0;
  1417.     if(mat[1][1] > mat[0][0])
  1418.         k = 1;
  1419.     if(mat[2][2] > mat[k][k])
  1420.         k = 2;
  1421.     int r[3] = { 1, 2, 0 };
  1422.     float kf = sqrt(mat[k][k] - (mat[r[r[k]]][r[r[k]]] + mat[r[k]][r[k]]) + 1.0);
  1423.     qt[k] = kf * 0.5;
  1424.     if(kf != 0)
  1425.         kf = 0.5 / kf;
  1426.     qt[3] = (mat[r[k]][r[r[k]]] - mat[r[r[k]]][r[k]]) * kf;
  1427.     qt[r[k]] = (mat[k][r[k]] + mat[r[k]][k]) * kf;
  1428.     qt[r[r[k]]] = (mat[k][r[r[k]]] + mat[r[r[k]]][k]) * kf;
  1429. }
  1430.  
  1431. /** Insert description.
  1432. */
  1433. INLINE void zCQuat::QuatToEuler(zVEC3& veu) const
  1434. {
  1435.     float angle = -(qt[0] * qt[2] - qt[1] * qt[3]) * 2;
  1436.     if(fabs(angle) > 1.0)
  1437.     {
  1438.         veu[0] = atan2f((qt[1] * qt[1] + qt[3] * qt[3]) * 2 - 1.0, -(qt[1] * qt[2] - qt[0] * qt[3]) * 2);
  1439.         veu[1] = (angle > 0) ? 3.141592653589793 / 2 : -3.141592653589793 / 2;
  1440.         veu[2] = 0;
  1441.     }
  1442.     else
  1443.     {
  1444.         veu[0] = atan2f((qt[2] * qt[2] + qt[3] * qt[3]) * 2 - 1.0, (qt[1] * qt[2] + qt[0] * qt[3]) * 2);
  1445.         veu[1] = asin(angle);
  1446.         veu[2] = atan2f((qt[1] * qt[1] + qt[3] * qt[3]) * 2 - 1.0, (qt[0] * qt[1] + qt[2] * qt[3]) * 2);
  1447.     }
  1448. }
  1449.  
  1450. /** Insert description.
  1451. */
  1452. INLINE void zCQuat::EulerToQuat(zVEC3 const& veu)
  1453. {
  1454.     float sn0 = sin(veu[0] * 0.5);
  1455.     float cs0 = cos(veu[0] * 0.5);
  1456.     float sn1 = sin(veu[1] * 0.5);
  1457.     float cs1 = cos(veu[1] * 0.5);
  1458.     float sn2 = sin(veu[2] * 0.5);
  1459.     float cs2 = cos(veu[2] * 0.5);
  1460.     qt[3] = sn0 * sn1 * sn2 + cs0 * cs1 * cs2;
  1461.     qt[0] = sn0 * cs1 * cs2 - cs0 * sn1 * sn2;
  1462.     qt[1] = cs0 * sn1 * cs2 + sn0 * cs1 * sn2;
  1463.     qt[2] = cs0 * cs1 * sn2 - sn0 * sn1 * cs2;
  1464. }
  1465.  
  1466. /** Insert description.
  1467. */
  1468. INLINE void zCQuat::QuatToAxisAngle(zVEC3& vAxis, float& fAngle) const
  1469. {
  1470.     float fSqrLength = qt[0] * qt[0] + qt[1] * qt[1] + qt[2] * qt[2];
  1471.     if(fSqrLength > 1.0e-6)
  1472.     {
  1473.         float fInvLength = 1.0 / fSqrLength;
  1474.         vAxis[0] = qt[0] * fInvLength;
  1475.         vAxis[1] = qt[1] * fInvLength;
  1476.         vAxis[2] = qt[2] * fInvLength;
  1477.         fAngle = 2.0 * acos(qt[3]);
  1478.     }
  1479.     else
  1480.     {
  1481.         // angle is 0 (mod 2*pi), so any axis will do
  1482.         vAxis[0] = 0.0;
  1483.         vAxis[1] = 0.0;
  1484.         vAxis[2] = 1.0;
  1485.         fAngle = 0.0;
  1486.     }
  1487. }
  1488.  
  1489. /** Insert description.
  1490. */
  1491. INLINE void zCQuat::QuatToAngAxis(zVEC3& vAxis, float& fAngle) const
  1492. {
  1493.     // The quaternion representing the rotation is
  1494.     // q = cos(A/2)+sin(A/2)*(x*i+y*j+z*k)
  1495.     float fSqrLength = qt[0] * qt[0] + qt[1] * qt[1] + qt[2] * qt[2];
  1496.     if(fSqrLength > 0.0)
  1497.     {
  1498.         fAngle = 2.0 * acos(qt[3]);
  1499.         float fInvLength = 1.0 / sqrt(fSqrLength);
  1500.         vAxis[0] = -qt[0] * fInvLength;
  1501.         vAxis[1] = -qt[1] * fInvLength;
  1502.         vAxis[2] = -qt[2] * fInvLength;
  1503.     }
  1504.     else
  1505.     {
  1506.         // angle is 0 (mod 2*pi), so any axis will do
  1507.         fAngle = 0.0;
  1508.         vAxis[0] = 0.0;
  1509.         vAxis[1] = 1.0;
  1510.         vAxis[2] = 0.0;
  1511.     }
  1512. }
  1513.  
  1514. /** Insert description.
  1515. */
  1516. INLINE void zCQuat::Slerp(float fT, const zCQuat& rkP, const zCQuat& rkQ)
  1517. {
  1518.     float fCos = 0.0f;
  1519.     for(int j = 0; j < 4; j++)
  1520.         fCos += rkP[j] * rkQ[j];
  1521.  
  1522.     float fAngle;
  1523.     if(-1.0f < fCos)
  1524.     {
  1525.         if(fCos < 1.0f)
  1526.             fAngle = acos(fCos);
  1527.         else
  1528.             fAngle = 0.0f;
  1529.     }
  1530.     else
  1531.     {
  1532.         static float fPi = 4.0f * atan(1.0f);
  1533.         fAngle = fPi;
  1534.     }
  1535.     if(fabs(fAngle) < 1e-3f)
  1536.     {
  1537.         for(int j = 0; j < 4; j++)
  1538.             qt[j] = rkP[j];
  1539.     }
  1540.     else
  1541.     {
  1542.         float fSin = sin(fAngle);
  1543.         float fInvSin = 1.0f / fSin;
  1544.         float fCoeff0 = sin((1.0f - fT) * fAngle) * fInvSin;
  1545.         float fCoeff1 = sin(fT * fAngle) * fInvSin;
  1546.         if(fCos < 0.0f)
  1547.             fCoeff0 = -fCoeff0;
  1548.         for(int j = 0; j < 4; j++)
  1549.             qt[j] = fCoeff0 * rkP[j] + fCoeff1 * rkQ[j];
  1550.     }
  1551. }
  1552.  
  1553. /** Insert description.
  1554. */
  1555. INLINE void zCQuat::Lerp(float fT, zCQuat const& rkP, zCQuat const& rkQ)
  1556. {
  1557.     zVEC4 vq = rkQ.qt * fT;
  1558.     zVEC4 vp = rkP.qt * (1.0 / fT);
  1559.     if(rkQ.qt * rkP.qt < 0)
  1560.         qt = vp - vq;
  1561.     else
  1562.         qt = vp + vq;
  1563. }
  1564.  
  1565. /** Insert description.
  1566. */
  1567. INLINE void zCQuat::Squad(float fT, zCQuat const& sQ0, zCQuat const& sQ1, zCQuat const& sQ2, zCQuat const& sQ3)
  1568. {
  1569.     zCQuat qt0, qt1;
  1570.     qt0.Slerp(fT, sQ0, sQ1);
  1571.     qt0.Unit();
  1572.     qt1.Slerp(fT, sQ2, sQ3);
  1573.     qt1.Unit();
  1574.     Slerp((1.0 - fT) * fT * 2, qt0, qt1);
  1575. }
Advertisement
Add Comment
Please, Sign In to add comment