Guest User

Untitled

a guest
Jan 24th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.16 KB | None | 0 0
  1. union {
  2. struct
  3. {
  4. float _11, _12, _13, _14;
  5. float _21, _22, _23, _24;
  6. float _31, _32, _33, _34;
  7. float _41, _42, _43, _44;
  8. };
  9. float m[4][4];
  10. float m2[16];
  11. };
  12.  
  13. inline void GetRotation(float& Yaw, float& Pitch, float& Roll) const
  14. {
  15. if (_11 == 1.0f)
  16. {
  17. Yaw = atan2f(_13, _34);
  18. Pitch = 0;
  19. Roll = 0;
  20.  
  21. }else if (_11 == -1.0f)
  22. {
  23. Yaw = atan2f(_13, _34);
  24. Pitch = 0;
  25. Roll = 0;
  26. }else
  27. {
  28.  
  29. Yaw = atan2(-_31,_11);
  30. Pitch = asin(_21);
  31. Roll = atan2(-_23,_22);
  32. }
  33. }
  34.  
  35. ┌ cosθ -sinθ 0 ┐
  36. Rz = │ sinθ cosθ 0 │
  37. └ 0 0 1 ┘
  38.  
  39. ┌ 1 0 0 ┐
  40. Rx = │ 0 cosθ -sinθ │
  41. └ 0 sinθ cosθ ┘
  42.  
  43. ┌ cosθ 0 sinθ ┐
  44. Ry = │ 0 1 0 │
  45. └ -sinθ 0 cosθ ┘
  46.  
  47. ┌ CyCz -CySz Sy ┐
  48. RxRyRz = │ SxSyCz + CxSz -SxSySz + CxCz -SxCy │
  49. └ -CxSyCz + SxSz CxSySz + SxCz CxCy ┘
  50.  
  51. x = atan2(-M[1][2], M[2][2])
  52.  
  53. cosY = sqrt(1 - M[0][2])
  54.  
  55. y = atan2(M[0][2], cosY)
  56.  
  57. ┌ Cy 0 Sy ┐
  58. RxRy = │ SxSy Cx -SxCy │
  59. └ -CxSy Sx CxCy ┘
  60.  
  61. M = RxRy * Rz
  62.  
  63. inverse(RxRy) * M = Rz
  64.  
  65. ┌ Cy SxSy -CxSy ┐┌M00 M01 M02┐ ┌ cosZ -sinZ 0 ┐
  66. │ 0 Cx Sx ││M10 M11 M12│ = │ sinZ cosZ 0 │
  67. └ Sy -SxCy CxCy ┘└M20 M21 M22┘ └ 0 0 1 ┘
  68.  
  69. sinZ = cosX * M[1][0] + sinX * M[2][0]
  70. cosZ = coxX * M[1][1] + sinX * M[2][1]
  71. z = atan2(sinZ, cosZ)
  72.  
  73. #include <iostream>
  74. #include <cmath>
  75.  
  76. class Vec4 {
  77. public:
  78. Vec4(float x, float y, float z, float w) :
  79. x(x), y(y), z(z), w(w) {}
  80.  
  81. float dot(const Vec4& other) const {
  82. return x * other.x +
  83. y * other.y +
  84. z * other.z +
  85. w * other.w;
  86. };
  87.  
  88. float x, y, z, w;
  89. };
  90.  
  91. class Mat4x4 {
  92. public:
  93. Mat4x4() {}
  94.  
  95. Mat4x4(float v00, float v01, float v02, float v03,
  96. float v10, float v11, float v12, float v13,
  97. float v20, float v21, float v22, float v23,
  98. float v30, float v31, float v32, float v33) {
  99. values[0] = v00;
  100. values[1] = v01;
  101. values[2] = v02;
  102. values[3] = v03;
  103. values[4] = v10;
  104. values[5] = v11;
  105. values[6] = v12;
  106. values[7] = v13;
  107. values[8] = v20;
  108. values[9] = v21;
  109. values[10] = v22;
  110. values[11] = v23;
  111. values[12] = v30;
  112. values[13] = v31;
  113. values[14] = v32;
  114. values[15] = v33;
  115. }
  116.  
  117. Vec4 row(const int row) const {
  118. return Vec4(
  119. values[row*4],
  120. values[row*4+1],
  121. values[row*4+2],
  122. values[row*4+3]
  123. );
  124. }
  125.  
  126. Vec4 column(const int column) const {
  127. return Vec4(
  128. values[column],
  129. values[column + 4],
  130. values[column + 8],
  131. values[column + 12]
  132. );
  133. }
  134.  
  135. Mat4x4 multiply(const Mat4x4& other) const {
  136. Mat4x4 result;
  137. for (int row = 0; row < 4; ++row) {
  138. for (int column = 0; column < 4; ++column) {
  139. result.values[row*4+column] = this->row(row).dot(other.column(column));
  140. }
  141. }
  142. return result;
  143. }
  144.  
  145. void extractEulerAngleXYZ(float& rotXangle, float& rotYangle, float& rotZangle) const {
  146. rotXangle = atan2(-row(1).z, row(2).z);
  147. float cosYangle = sqrt(pow(row(0).x, 2) + pow(row(0).y, 2));
  148. rotYangle = atan2(row(0).z, cosYangle);
  149. float sinXangle = sin(rotXangle);
  150. float cosXangle = cos(rotXangle);
  151. rotZangle = atan2(cosXangle * row(1).x + sinXangle * row(2).x, cosXangle * row(1).y + sinXangle * row(2).y);
  152. }
  153.  
  154. float values[16];
  155. };
  156.  
  157. float toRadians(float degrees) {
  158. return degrees * (M_PI / 180);
  159. }
  160.  
  161. float toDegrees(float radians) {
  162. return radians * (180 / M_PI);
  163. }
  164.  
  165. int main() {
  166. float rotXangle = toRadians(15);
  167. float rotYangle = toRadians(30);
  168. float rotZangle = toRadians(60);
  169.  
  170. Mat4x4 rotX(
  171. 1, 0, 0, 0,
  172. 0, cos(rotXangle), -sin(rotXangle), 0,
  173. 0, sin(rotXangle), cos(rotXangle), 0,
  174. 0, 0, 0, 1
  175. );
  176. Mat4x4 rotY(
  177. cos(rotYangle), 0, sin(rotYangle), 0,
  178. 0, 1, 0, 0,
  179. -sin(rotYangle), 0, cos(rotYangle), 0,
  180. 0, 0, 0, 1
  181. );
  182. Mat4x4 rotZ(
  183. cos(rotZangle), -sin(rotZangle), 0, 0,
  184. sin(rotZangle), cos(rotZangle), 0, 0,
  185. 0, 0, 1, 0,
  186. 0, 0, 0, 1
  187. );
  188.  
  189. Mat4x4 concatenatedRotationMatrix =
  190. rotX.multiply(rotY.multiply(rotZ));
  191.  
  192. float extractedXangle = 0, extractedYangle = 0, extractedZangle = 0;
  193. concatenatedRotationMatrix.extractEulerAngleXYZ(
  194. extractedXangle, extractedYangle, extractedZangle
  195. );
  196.  
  197. std::cout << toDegrees(extractedXangle) << ' ' <<
  198. toDegrees(extractedYangle) << ' ' <<
  199. toDegrees(extractedZangle) << std::endl;
  200.  
  201. return 0;
  202. }
Add Comment
Please, Sign In to add comment