local_transform.inl - Engine C API Reference

local_transform.inl
  1. #include "vector3.h"
  2. #include "quaternion.h"
  3. namespace stingray_plugin_foundation {
  4. // ----------------------------------------------------------------------
  5. // Matrix3x3
  6. // ----------------------------------------------------------------------
  7. inline Matrix3x3 matrix3x3_identity()
  8. {
  9. Matrix3x3 m;
  10. set_to_identity(m);
  11. return m;
  12. }
  13. inline void set_to_identity(Matrix3x3 &m)
  14. {
  15. m.x = vector3(1,0,0);
  16. m.y = vector3(0,1,0);
  17. m.z = vector3(0,0,1);
  18. }
  19. inline Matrix3x3 operator*(const Matrix3x3 &a, const Matrix3x3 &b)
  20. {
  21. Matrix3x3 m;
  22. m.x.x = a.x.x*b.x.x + a.x.y*b.y.x + a.x.z*b.z.x;
  23. m.x.y = a.x.x*b.x.y + a.x.y*b.y.y + a.x.z*b.z.y;
  24. m.x.z = a.x.x*b.x.z + a.x.y*b.y.z + a.x.z*b.z.z;
  25. m.y.x = a.y.x*b.x.x + a.y.y*b.y.x + a.y.z*b.z.x;
  26. m.y.y = a.y.x*b.x.y + a.y.y*b.y.y + a.y.z*b.z.y;
  27. m.y.z = a.y.x*b.x.z + a.y.y*b.y.z + a.y.z*b.z.z;
  28. m.z.x = a.z.x*b.x.x + a.z.y*b.y.x + a.z.z*b.z.x;
  29. m.z.y = a.z.x*b.x.y + a.z.y*b.y.y + a.z.z*b.z.y;
  30. m.z.z = a.z.x*b.x.z + a.z.y*b.y.z + a.z.z*b.z.z;
  31. return m;
  32. }
  33. inline Matrix3x3 matrix3x3(const Quaternion &q)
  34. {
  35. const float d = dot(q,q);
  36. float s = d ? 2.0f / d : 1.0f;
  37. const float xs = q.x * s;
  38. const float ys = q.y * s;
  39. const float zs = q.z * s;
  40. const float wx = q.w * xs;
  41. const float wy = q.w * ys;
  42. const float wz = q.w * zs;
  43. const float xx = q.x * xs;
  44. const float xy = q.x * ys;
  45. const float xz = q.x * zs;
  46. const float yy = q.y * ys;
  47. const float yz = q.y * zs;
  48. const float zz = q.z * zs;
  49. Matrix3x3 m;
  50. m.x = vector3((1.0f - yy - zz), (xy + wz), (xz - wy));
  51. m.y = vector3((xy - wz), (1.0f - xx - zz), (yz + wx));
  52. m.z = vector3((xz + wy), (yz - wx), (1.0f - xx - yy));
  53. return m;
  54. }
  55. inline Matrix3x3 matrix3x3(const Matrix4x4 &tm)
  56. {
  57. Matrix3x3 m;
  58. m.x = x_axis(tm);
  59. m.y = y_axis(tm);
  60. m.z = z_axis(tm);
  61. return m;
  62. }
  63. // ----------------------------------------------------------------------
  64. // LocalTransform
  65. // ----------------------------------------------------------------------
  66. inline LocalTransform local_transform(const Matrix3x3 &rot, const Vector3 &pos)
  67. {
  68. LocalTransform tm;
  69. tm.rot = rot;
  70. tm.pos = pos;
  71. tm.scale = vector3(1,1,1);
  72. tm.dummy = 0.0f;
  73. return tm;
  74. }
  75. inline LocalTransform local_transform(const Matrix3x3 &rot, const Vector3 &pos, const Vector3 &scale)
  76. {
  77. LocalTransform tm;
  78. tm.rot = rot;
  79. tm.pos = pos;
  80. tm.scale = scale;
  81. tm.dummy = 0.0f;
  82. return tm;
  83. }
  84. inline LocalTransform local_transform_identity()
  85. {
  86. LocalTransform tm;
  87. set_to_identity(tm);
  88. return tm;
  89. }
  90. inline void set_to_identity(LocalTransform &tm)
  91. {
  92. set_to_identity(tm.rot);
  93. tm.pos = vector3(0,0,0);
  94. tm.scale = vector3(1,1,1);
  95. tm.dummy = 0.0f;
  96. }
  97. inline Vector3 transform(const LocalTransform &tm, const Vector3 &p)
  98. {
  99. return tm.pos + p.x*tm.rot.x*tm.scale.x + p.y*tm.rot.y*tm.scale.y + p.z*tm.rot.z*tm.scale.z;
  100. }
  101. inline Vector3 transform_without_translation(const LocalTransform &tm, const Vector3 &p)
  102. {
  103. return p.x*tm.rot.x*tm.scale.x + p.y*tm.rot.y*tm.scale.y + p.z*tm.rot.z*tm.scale.z;
  104. }
  105. inline LocalTransform operator*(const LocalTransform &a, const LocalTransform &b)
  106. {
  107. Matrix3x3 a_rs = a.rot;
  108. Matrix3x3 b_rs = b.rot;
  109. a_rs.x = a_rs.x * a.scale.x;
  110. a_rs.y = a_rs.y * a.scale.y;
  111. a_rs.z = a_rs.z * a.scale.z;
  112. b_rs.x = b_rs.x * b.scale.x;
  113. b_rs.y = b_rs.y * b.scale.y;
  114. b_rs.z = b_rs.z * b.scale.z;
  115. LocalTransform res;
  116. res.rot = a_rs * b_rs;
  117. res.scale = vector3(length(res.rot.x), length(res.rot.y), length(res.rot.z));
  118. res.rot.x /= res.scale.x; res.rot.y /= res.scale.y; res.rot.z /= res.scale.z;
  119. res.pos = transform(b, a.pos);
  120. return res;
  121. }
  122. inline void operator*=(LocalTransform &a, const LocalTransform &b)
  123. {
  124. a = a*b;
  125. }
  126. inline LocalTransform relative(LocalTransform &child, const LocalTransform &parent)
  127. {
  128. Matrix4x4 p4i = inverse(matrix4x4(parent));
  129. Matrix4x4 rm = matrix4x4(child) * p4i;
  130. return local_transform(rm);
  131. }
  132. inline LocalTransform local_transform(const Matrix4x4 &m4)
  133. {
  134. LocalTransform tm;
  135. tm.rot.x = x_axis(m4);
  136. tm.rot.y = y_axis(m4);
  137. tm.rot.z = z_axis(m4);
  138. tm.scale = vector3( length(tm.rot.x), length(tm.rot.y), length(tm.rot.z) );
  139. if (tm.scale.x && tm.scale.y && tm.scale.z) {
  140. tm.rot.x /= tm.scale.x;
  141. tm.rot.y /= tm.scale.y;
  142. tm.rot.z /= tm.scale.z;
  143. } else
  144. tm.rot = matrix3x3_identity();
  145. tm.pos = translation(m4);
  146. tm.dummy = 0.0f;
  147. return tm;
  148. }
  149. inline Matrix4x4 matrix4x4(const LocalTransform &tm)
  150. {
  151. Matrix4x4 m4 = matrix4x4_identity();
  152. x_axis(m4) = tm.rot.x * tm.scale.x;
  153. y_axis(m4) = tm.rot.y * tm.scale.y;
  154. z_axis(m4) = tm.rot.z * tm.scale.z;
  155. translation(m4) = tm.pos;
  156. return m4;
  157. }
  158. }