mMatrix.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "core/strings/stringFunctions.h"
  23. #include "core/frameAllocator.h"
  24. #include "math/mMatrix.h"
  25. #include "console/console.h"
  26. #include "console/enginePrimitives.h"
  27. #include "console/engineTypes.h"
  28. const MatrixF MatrixF::Identity( true );
  29. // idx(i,j) is index to element in column i, row j
  30. void MatrixF::transposeTo(F32 *matrix) const
  31. {
  32. matrix[idx(0,0)] = m[idx(0,0)];
  33. matrix[idx(0,1)] = m[idx(1,0)];
  34. matrix[idx(0,2)] = m[idx(2,0)];
  35. matrix[idx(0,3)] = m[idx(3,0)];
  36. matrix[idx(1,0)] = m[idx(0,1)];
  37. matrix[idx(1,1)] = m[idx(1,1)];
  38. matrix[idx(1,2)] = m[idx(2,1)];
  39. matrix[idx(1,3)] = m[idx(3,1)];
  40. matrix[idx(2,0)] = m[idx(0,2)];
  41. matrix[idx(2,1)] = m[idx(1,2)];
  42. matrix[idx(2,2)] = m[idx(2,2)];
  43. matrix[idx(2,3)] = m[idx(3,2)];
  44. matrix[idx(3,0)] = m[idx(0,3)];
  45. matrix[idx(3,1)] = m[idx(1,3)];
  46. matrix[idx(3,2)] = m[idx(2,3)];
  47. matrix[idx(3,3)] = m[idx(3,3)];
  48. }
  49. bool MatrixF::isAffine() const
  50. {
  51. // An affine transform is defined by the following structure
  52. //
  53. // [ X X X P ]
  54. // [ X X X P ]
  55. // [ X X X P ]
  56. // [ 0 0 0 1 ]
  57. //
  58. // Where X is an orthonormal 3x3 submatrix and P is an arbitrary translation
  59. // We'll check in the following order:
  60. // 1: [3][3] must be 1
  61. // 2: Shear portion must be zero
  62. // 3: Dot products of rows and columns must be zero
  63. // 4: Length of rows and columns must be 1
  64. //
  65. if (m[idx(3,3)] != 1.0f)
  66. return false;
  67. if (m[idx(0,3)] != 0.0f ||
  68. m[idx(1,3)] != 0.0f ||
  69. m[idx(2,3)] != 0.0f)
  70. return false;
  71. Point3F one, two, three;
  72. getColumn(0, &one);
  73. getColumn(1, &two);
  74. getColumn(2, &three);
  75. if (mDot(one, two) > 0.0001f ||
  76. mDot(one, three) > 0.0001f ||
  77. mDot(two, three) > 0.0001f)
  78. return false;
  79. if (mFabs(1.0f - one.lenSquared()) > 0.0001f ||
  80. mFabs(1.0f - two.lenSquared()) > 0.0001f ||
  81. mFabs(1.0f - three.lenSquared()) > 0.0001f)
  82. return false;
  83. getRow(0, &one);
  84. getRow(1, &two);
  85. getRow(2, &three);
  86. if (mDot(one, two) > 0.0001f ||
  87. mDot(one, three) > 0.0001f ||
  88. mDot(two, three) > 0.0001f)
  89. return false;
  90. if (mFabs(1.0f - one.lenSquared()) > 0.0001f ||
  91. mFabs(1.0f - two.lenSquared()) > 0.0001f ||
  92. mFabs(1.0f - three.lenSquared()) > 0.0001f)
  93. return false;
  94. // We're ok.
  95. return true;
  96. }
  97. // Perform inverse on full 4x4 matrix. Used in special cases only, so not at all optimized.
  98. bool MatrixF::fullInverse()
  99. {
  100. Point4F a,b,c,d;
  101. getRow(0,&a);
  102. getRow(1,&b);
  103. getRow(2,&c);
  104. getRow(3,&d);
  105. // det = a0*b1*c2*d3 - a0*b1*c3*d2 - a0*c1*b2*d3 + a0*c1*b3*d2 + a0*d1*b2*c3 - a0*d1*b3*c2 -
  106. // b0*a1*c2*d3 + b0*a1*c3*d2 + b0*c1*a2*d3 - b0*c1*a3*d2 - b0*d1*a2*c3 + b0*d1*a3*c2 +
  107. // c0*a1*b2*d3 - c0*a1*b3*d2 - c0*b1*a2*d3 + c0*b1*a3*d2 + c0*d1*a2*b3 - c0*d1*a3*b2 -
  108. // d0*a1*b2*c3 + d0*a1*b3*c2 + d0*b1*a2*c3 - d0*b1*a3*c2 - d0*c1*a2*b3 + d0*c1*a3*b2
  109. F32 det = a.x*b.y*c.z*d.w - a.x*b.y*c.w*d.z - a.x*c.y*b.z*d.w + a.x*c.y*b.w*d.z + a.x*d.y*b.z*c.w - a.x*d.y*b.w*c.z
  110. - b.x*a.y*c.z*d.w + b.x*a.y*c.w*d.z + b.x*c.y*a.z*d.w - b.x*c.y*a.w*d.z - b.x*d.y*a.z*c.w + b.x*d.y*a.w*c.z
  111. + c.x*a.y*b.z*d.w - c.x*a.y*b.w*d.z - c.x*b.y*a.z*d.w + c.x*b.y*a.w*d.z + c.x*d.y*a.z*b.w - c.x*d.y*a.w*b.z
  112. - d.x*a.y*b.z*c.w + d.x*a.y*b.w*c.z + d.x*b.y*a.z*c.w - d.x*b.y*a.w*c.z - d.x*c.y*a.z*b.w + d.x*c.y*a.w*b.z;
  113. if (mFabs(det)<0.00001f)
  114. return false;
  115. Point4F aa,bb,cc,dd;
  116. aa.x = b.y*c.z*d.w - b.y*c.w*d.z - c.y*b.z*d.w + c.y*b.w*d.z + d.y*b.z*c.w - d.y*b.w*c.z;
  117. aa.y = -a.y*c.z*d.w + a.y*c.w*d.z + c.y*a.z*d.w - c.y*a.w*d.z - d.y*a.z*c.w + d.y*a.w*c.z;
  118. aa.z = a.y*b.z*d.w - a.y*b.w*d.z - b.y*a.z*d.w + b.y*a.w*d.z + d.y*a.z*b.w - d.y*a.w*b.z;
  119. aa.w = -a.y*b.z*c.w + a.y*b.w*c.z + b.y*a.z*c.w - b.y*a.w*c.z - c.y*a.z*b.w + c.y*a.w*b.z;
  120. bb.x = -b.x*c.z*d.w + b.x*c.w*d.z + c.x*b.z*d.w - c.x*b.w*d.z - d.x*b.z*c.w + d.x*b.w*c.z;
  121. bb.y = a.x*c.z*d.w - a.x*c.w*d.z - c.x*a.z*d.w + c.x*a.w*d.z + d.x*a.z*c.w - d.x*a.w*c.z;
  122. bb.z = -a.x*b.z*d.w + a.x*b.w*d.z + b.x*a.z*d.w - b.x*a.w*d.z - d.x*a.z*b.w + d.x*a.w*b.z;
  123. bb.w = a.x*b.z*c.w - a.x*b.w*c.z - b.x*a.z*c.w + b.x*a.w*c.z + c.x*a.z*b.w - c.x*a.w*b.z;
  124. cc.x = b.x*c.y*d.w - b.x*c.w*d.y - c.x*b.y*d.w + c.x*b.w*d.y + d.x*b.y*c.w - d.x*b.w*c.y;
  125. cc.y = -a.x*c.y*d.w + a.x*c.w*d.y + c.x*a.y*d.w - c.x*a.w*d.y - d.x*a.y*c.w + d.x*a.w*c.y;
  126. cc.z = a.x*b.y*d.w - a.x*b.w*d.y - b.x*a.y*d.w + b.x*a.w*d.y + d.x*a.y*b.w - d.x*a.w*b.y;
  127. cc.w = -a.x*b.y*c.w + a.x*b.w*c.y + b.x*a.y*c.w - b.x*a.w*c.y - c.x*a.y*b.w + c.x*a.w*b.y;
  128. dd.x = -b.x*c.y*d.z + b.x*c.z*d.y + c.x*b.y*d.z - c.x*b.z*d.y - d.x*b.y*c.z + d.x*b.z*c.y;
  129. dd.y = a.x*c.y*d.z - a.x*c.z*d.y - c.x*a.y*d.z + c.x*a.z*d.y + d.x*a.y*c.z - d.x*a.z*c.y;
  130. dd.z = -a.x*b.y*d.z + a.x*b.z*d.y + b.x*a.y*d.z - b.x*a.z*d.y - d.x*a.y*b.z + d.x*a.z*b.y;
  131. dd.w = a.x*b.y*c.z - a.x*b.z*c.y - b.x*a.y*c.z + b.x*a.z*c.y + c.x*a.y*b.z - c.x*a.z*b.y;
  132. setRow(0,aa);
  133. setRow(1,bb);
  134. setRow(2,cc);
  135. setRow(3,dd);
  136. mul(1.0f/det);
  137. return true;
  138. }
  139. void MatrixF::reverseProjection()
  140. {
  141. m[idx(0, 2)] = m[idx(0, 3)] - m[idx(0, 2)];
  142. m[idx(1, 2)] = m[idx(1, 3)] - m[idx(1, 2)];
  143. m[idx(2, 2)] = m[idx(2, 3)] - m[idx(2, 2)];
  144. m[idx(3, 2)] = m[idx(3, 3)] - m[idx(3, 2)];
  145. }
  146. EulerF MatrixF::toEuler() const
  147. {
  148. const F32 * mat = m;
  149. EulerF r;
  150. r.x = mAsin(mClampF(mat[MatrixF::idx(2,1)], -1.0, 1.0));
  151. if(mCos(r.x) != 0.f)
  152. {
  153. r.y = mAtan2(-mat[MatrixF::idx(2,0)], mat[MatrixF::idx(2,2)]);
  154. r.z = mAtan2(-mat[MatrixF::idx(0,1)], mat[MatrixF::idx(1,1)]);
  155. }
  156. else
  157. {
  158. r.y = 0.f;
  159. r.z = mAtan2(mat[MatrixF::idx(1,0)], mat[MatrixF::idx(0,0)]);
  160. }
  161. return r;
  162. }
  163. void MatrixF::dumpMatrix(const char *caption /* =NULL */) const
  164. {
  165. U32 size = (caption == NULL)? 0 : dStrlen(caption);
  166. FrameTemp<char> spacer(size+1);
  167. char *spacerRef = spacer;
  168. dMemset(spacerRef, ' ', size);
  169. spacerRef[size] = 0;
  170. Con::printf("%s = | %-8.4f %-8.4f %-8.4f %-8.4f |", caption, m[idx(0,0)], m[idx(0, 1)], m[idx(0, 2)], m[idx(0, 3)]);
  171. Con::printf("%s | %-8.4f %-8.4f %-8.4f %-8.4f |", spacerRef, m[idx(1,0)], m[idx(1, 1)], m[idx(1, 2)], m[idx(1, 3)]);
  172. Con::printf("%s | %-8.4f %-8.4f %-8.4f %-8.4f |", spacerRef, m[idx(2,0)], m[idx(2, 1)], m[idx(2, 2)], m[idx(2, 3)]);
  173. Con::printf("%s | %-8.4f %-8.4f %-8.4f %-8.4f |", spacerRef, m[idx(3,0)], m[idx(3, 1)], m[idx(3, 2)], m[idx(3, 3)]);
  174. }
  175. EngineFieldTable::Field MatrixFEngineExport::getMatrixField()
  176. {
  177. typedef MatrixF ThisType;
  178. return _FIELD_AS(F32, m, m, 16, "");
  179. }
  180. //------------------------------------
  181. // Templatized matrix class to replace MATRIXF above
  182. //------------------------------------
  183. template<typename DATA_TYPE, U32 rows, U32 cols>
  184. const Matrix<DATA_TYPE, rows, cols> Matrix<DATA_TYPE, rows, cols>::Identity = []() {
  185. Matrix<DATA_TYPE, rows, cols> identity(true);
  186. return identity;
  187. }();
  188. template<typename DATA_TYPE, U32 rows, U32 cols>
  189. Matrix<DATA_TYPE, rows, cols>::Matrix(const EulerF& e)
  190. {
  191. set(e);
  192. }
  193. template<typename DATA_TYPE, U32 rows, U32 cols>
  194. Matrix<DATA_TYPE, rows, cols>& Matrix<DATA_TYPE, rows, cols>::set(const EulerF& e)
  195. {
  196. // when the template refactor is done, euler will be able to be setup in different ways
  197. AssertFatal(rows >= 3 && cols >= 3, "EulerF can only initialize 3x3 or more");
  198. static_assert(std::is_same<DATA_TYPE, float>::value, "Can only initialize eulers with floats for now");
  199. F32 cosPitch, sinPitch;
  200. mSinCos(e.x, sinPitch, cosPitch);
  201. F32 cosYaw, sinYaw;
  202. mSinCos(e.y, sinYaw, cosYaw);
  203. F32 cosRoll, sinRoll;
  204. mSinCos(e.z, sinRoll, cosRoll);
  205. enum {
  206. AXIS_X = (1 << 0),
  207. AXIS_Y = (1 << 1),
  208. AXIS_Z = (1 << 2)
  209. };
  210. U32 axis = 0;
  211. if (e.x != 0.0f) axis |= AXIS_X;
  212. if (e.y != 0.0f) axis |= AXIS_Y;
  213. if (e.z != 0.0f) axis |= AXIS_Z;
  214. switch (axis) {
  215. case 0:
  216. (*this) = Matrix<DATA_TYPE, rows, cols>(true);
  217. break;
  218. case AXIS_X:
  219. (*this)(0, 0) = 1.0f; (*this)(1, 0) = 0.0f; (*this)(2, 0) = 0.0f;
  220. (*this)(0, 1) = 0.0f; (*this)(1, 1) = cosPitch; (*this)(2, 1) = -sinPitch;
  221. (*this)(0, 2) = 0.0f; (*this)(1, 2) = sinPitch; (*this)(2, 2) = cosPitch;
  222. break;
  223. case AXIS_Y:
  224. (*this)(0, 0) = cosYaw; (*this)(1, 0) = 0.0f; (*this)(2, 0) = sinYaw;
  225. (*this)(0, 1) = 0.0f; (*this)(1, 1) = 1.0f; (*this)(2, 1) = 0.0f;
  226. (*this)(0, 2) = -sinYaw; (*this)(1, 2) = 0.0f; (*this)(2, 2) = cosYaw;
  227. break;
  228. case AXIS_Z:
  229. (*this)(0, 0) = cosRoll; (*this)(1, 0) = -sinRoll; (*this)(2, 0) = 0.0f;
  230. (*this)(0, 1) = sinRoll; (*this)(1, 1) = cosRoll; (*this)(2, 1) = 0.0f;
  231. (*this)(0, 2) = 0.0f; (*this)(1, 2) = 0.0f; (*this)(2, 2) = 0.0f;
  232. break;
  233. default:
  234. F32 r1 = cosYaw * cosRoll;
  235. F32 r2 = cosYaw * sinRoll;
  236. F32 r3 = sinYaw * cosRoll;
  237. F32 r4 = sinYaw * sinRoll;
  238. // the matrix looks like this:
  239. // r1 - (r4 * sin(x)) r2 + (r3 * sin(x)) -cos(x) * sin(y)
  240. // -cos(x) * sin(z) cos(x) * cos(z) sin(x)
  241. // r3 + (r2 * sin(x)) r4 - (r1 * sin(x)) cos(x) * cos(y)
  242. //
  243. // where:
  244. // r1 = cos(y) * cos(z)
  245. // r2 = cos(y) * sin(z)
  246. // r3 = sin(y) * cos(z)
  247. // r4 = sin(y) * sin(z)
  248. // init the euler 3x3 rotation matrix.
  249. (*this)(0, 0) = r1 - (r4 * sinPitch); (*this)(1, 0) = -cosPitch * sinRoll; (*this)(2, 0) = r3 + (r2 * sinPitch);
  250. (*this)(0, 1) = r2 + (r3 * sinPitch); (*this)(1, 1) = cosPitch * cosRoll; (*this)(2, 1) = r4 - (r1 * sinPitch);
  251. (*this)(0, 2) = -cosPitch * sinYaw; (*this)(1, 2) = sinPitch; (*this)(2, 2) = cosPitch * cosYaw;
  252. break;
  253. }
  254. if (rows == 4) {
  255. (*this)(3, 0) = 0.0f;
  256. (*this)(3, 1) = 0.0f;
  257. (*this)(3, 2) = 0.0f;
  258. }
  259. if (cols == 4) {
  260. (*this)(0, 3) = 0.0f;
  261. (*this)(1, 3) = 0.0f;
  262. (*this)(2, 3) = 0.0f;
  263. }
  264. if (rows == 4 && cols == 4) {
  265. (*this)(3, 3) = 1.0f;
  266. }
  267. return(*this);
  268. }
  269. template<typename DATA_TYPE, U32 rows, U32 cols>
  270. Matrix<DATA_TYPE, rows, cols>::Matrix(const EulerF& e, const Point3F p)
  271. {
  272. set(e, p);
  273. }
  274. template<typename DATA_TYPE, U32 rows, U32 cols>
  275. Matrix<DATA_TYPE, rows, cols>& Matrix<DATA_TYPE, rows, cols>::set(const EulerF& e, const Point3F p)
  276. {
  277. AssertFatal(rows >= 3 && cols >= 4, "Euler and Point can only initialize 3x4 or more");
  278. // call set euler, this already sets the last row if it exists.
  279. set(e);
  280. // does this need to multiply with the result of the euler? or are we just setting position.
  281. (*this)(0, 3) = p.x;
  282. (*this)(1, 3) = p.y;
  283. (*this)(2, 3) = p.z;
  284. return (*this);
  285. }
  286. template<typename DATA_TYPE, U32 rows, U32 cols>
  287. Matrix<DATA_TYPE, rows, cols>& Matrix<DATA_TYPE, rows, cols>::inverse()
  288. {
  289. // TODO: insert return statement here
  290. AssertFatal(rows == cols, "Can only perform inverse on square matrices.");
  291. const U32 size = rows;
  292. // Create augmented matrix [this | I]
  293. Matrix<DATA_TYPE, size, 2 * size> augmentedMatrix;
  294. Matrix<DATA_TYPE, size, size> resultMatrix;
  295. for (U32 i = 0; i < size; i++) {
  296. for (U32 j = 0; j < size; j++) {
  297. augmentedMatrix(i, j) = (*this)(i, j);
  298. augmentedMatrix(i, j + size) = (i == j) ? static_cast<DATA_TYPE>(1) : static_cast<DATA_TYPE>(0);
  299. }
  300. }
  301. // Apply gauss-joran elimination
  302. for (U32 i = 0; i < size; i++) {
  303. U32 pivotRow = i;
  304. for (U32 k = i + 1; k < size; k++) {
  305. // use std::abs until the templated math functions are in place.
  306. if (std::abs(augmentedMatrix(k, i)) > std::abs(augmentedMatrix(pivotRow, i))) {
  307. pivotRow = k;
  308. }
  309. }
  310. // Swap if needed.
  311. if (i != pivotRow) {
  312. for (U32 j = 0; j < 2 * size; j++) {
  313. std::swap(augmentedMatrix(i, j), augmentedMatrix(pivotRow, j));
  314. }
  315. }
  316. // Early out if pivot is 0, return a new empty matrix.
  317. if (augmentedMatrix(i, i) == static_cast<DATA_TYPE>(0)) {
  318. return Matrix<DATA_TYPE, rows, cols>();
  319. }
  320. DATA_TYPE pivotVal = augmentedMatrix(i, i);
  321. // scale the pivot
  322. for (U32 j = 0; j < 2 * size; ++j) {
  323. augmentedMatrix(i, j) /= pivotVal;
  324. }
  325. // Eliminate the current column in all other rows
  326. for (std::size_t k = 0; k < size; k++) {
  327. if (k != i) {
  328. DATA_TYPE factor = augmentedMatrix(k, i);
  329. for (std::size_t j = 0; j < 2 * size; ++j) {
  330. augmentedMatrix(k, j) -= factor * augmentedMatrix(i, j);
  331. }
  332. }
  333. }
  334. }
  335. for (U32 i = 0; i < size; i++) {
  336. for (U32 j = 0; j < size; j++) {
  337. resultMatrix(i, j) = augmentedMatrix(i, j + size);
  338. }
  339. }
  340. return resultMatrix;
  341. }
  342. template<typename DATA_TYPE, U32 rows, U32 cols>
  343. void Matrix<DATA_TYPE, rows, cols>::invert()
  344. {
  345. (*this) = inverse();
  346. }
  347. template<typename DATA_TYPE, U32 rows, U32 cols>
  348. Matrix<DATA_TYPE, rows, cols>& Matrix<DATA_TYPE, rows, cols>::setCrossProduct(const Point3F& p)
  349. {
  350. AssertFatal(rows == 4 && cols == 4, "Cross product only supported on 4x4 for now");
  351. (*this)(0, 0) = 0;
  352. (*this)(0, 1) = -p.z;
  353. (*this)(0, 2) = p.y;
  354. (*this)(0, 3) = 0;
  355. (*this)(1, 0) = p.z;
  356. (*this)(1, 1) = 0;
  357. (*this)(1, 2) = -p.x;
  358. (*this)(1, 3) = 0;
  359. (*this)(2, 0) = -p.y;
  360. (*this)(2, 1) = p.x;
  361. (*this)(2, 2) = 0;
  362. (*this)(2, 3) = 0;
  363. (*this)(3, 0) = 0;
  364. (*this)(3, 1) = 0;
  365. (*this)(3, 2) = 0;
  366. (*this)(3, 3) = 1;
  367. return (*this);
  368. }
  369. template<typename DATA_TYPE, U32 rows, U32 cols>
  370. Matrix<DATA_TYPE, rows, cols>& Matrix<DATA_TYPE, rows, cols>::setTensorProduct(const Point3F& p, const Point3F& q)
  371. {
  372. AssertFatal(rows == 4 && cols == 4, "Tensor product only supported on 4x4 for now");
  373. (*this)(0, 0) = p.x * q.x;
  374. (*this)(0, 1) = p.x * q.y;
  375. (*this)(0, 2) = p.x * q.z;
  376. (*this)(0, 3) = 0;
  377. (*this)(1, 0) = p.y * q.x;
  378. (*this)(1, 1) = p.y * q.y;
  379. (*this)(1, 2) = p.y * q.z;
  380. (*this)(1, 3) = 0;
  381. (*this)(2, 0) = p.z * q.x;
  382. (*this)(2, 1) = p.z * q.y;
  383. (*this)(2, 2) = p.z * q.z;
  384. (*this)(2, 3) = 0;
  385. (*this)(3, 0) = 0;
  386. (*this)(3, 1) = 0;
  387. (*this)(3, 2) = 0;
  388. (*this)(3, 3) = 1;
  389. return (*this);
  390. }
  391. template<typename DATA_TYPE, U32 rows, U32 cols>
  392. bool Matrix<DATA_TYPE, rows, cols>::isAffine() const
  393. {
  394. if ((*this)(rows - 1, cols - 1) != 1.0f) {
  395. return false;
  396. }
  397. for (U32 col = 0; col < cols - 1; ++col) {
  398. if ((*this)(rows - 1, col) != 0.0f) {
  399. return false;
  400. }
  401. }
  402. Point3F one, two, three;
  403. getColumn(0, &one);
  404. getColumn(1, &two);
  405. getColumn(2, &three);
  406. // check columns
  407. {
  408. if (mDot(one, two) > 0.0001f ||
  409. mDot(one, three) > 0.0001f ||
  410. mDot(two, three) > 0.0001f)
  411. return false;
  412. if (mFabs(1.0f - one.lenSquared()) > 0.0001f ||
  413. mFabs(1.0f - two.lenSquared()) > 0.0001f ||
  414. mFabs(1.0f - three.lenSquared()) > 0.0001f)
  415. return false;
  416. }
  417. getRow(0, &one);
  418. getRow(1, &two);
  419. getRow(2, &three);
  420. // check rows
  421. {
  422. if (mDot(one, two) > 0.0001f ||
  423. mDot(one, three) > 0.0001f ||
  424. mDot(two, three) > 0.0001f)
  425. return false;
  426. if (mFabs(1.0f - one.lenSquared()) > 0.0001f ||
  427. mFabs(1.0f - two.lenSquared()) > 0.0001f ||
  428. mFabs(1.0f - three.lenSquared()) > 0.0001f)
  429. return false;
  430. }
  431. return true;
  432. }
  433. template<typename DATA_TYPE, U32 rows, U32 cols>
  434. EulerF Matrix<DATA_TYPE, rows, cols>::toEuler() const
  435. {
  436. AssertFatal(rows >= 3 && cols >= 3, "Euler rotations require at least a 3x3 matrix.");
  437. // Extract rotation matrix components
  438. const DATA_TYPE m00 = (*this)(0, 0);
  439. const DATA_TYPE m01 = (*this)(0, 1);
  440. const DATA_TYPE m02 = (*this)(0, 2);
  441. const DATA_TYPE m10 = (*this)(1, 0);
  442. const DATA_TYPE m11 = (*this)(1, 1);
  443. const DATA_TYPE m21 = (*this)(2, 1);
  444. const DATA_TYPE m22 = (*this)(2, 2);
  445. // like all others assume float for now.
  446. EulerF r;
  447. r.x = mAsin(mClampF(m21, -1.0, 1.0));
  448. if (mCos(r.x) != 0.0f) {
  449. r.y = mAtan2(-m02, m22); // yaw
  450. r.z = mAtan2(-m10, m11); // roll
  451. }
  452. else {
  453. r.y = 0.0f;
  454. r.z = mAtan2(m01, m00); // this rolls when pitch is +90 degrees
  455. }
  456. return r;
  457. }
  458. template<typename DATA_TYPE, U32 rows, U32 cols>
  459. void Matrix<DATA_TYPE, rows, cols>::dumpMatrix(const char* caption) const
  460. {
  461. U32 size = (caption == NULL) ? 0 : dStrlen(caption);
  462. FrameTemp<char> spacer(size + 1);
  463. char* spacerRef = spacer;
  464. // is_floating_point should return true for floats and doubles.
  465. const char* formatSpec = std::is_floating_point_v<DATA_TYPE> ? " %-8.4f" : " %d";
  466. dMemset(spacerRef, ' ', size);
  467. // null terminate.
  468. spacerRef[size] = '\0';
  469. /*Con::printf("%s = | %-8.4f %-8.4f %-8.4f %-8.4f |", caption, m[idx(0, 0)], m[idx(0, 1)], m[idx(0, 2)], m[idx(0, 3)]);
  470. Con::printf("%s | %-8.4f %-8.4f %-8.4f %-8.4f |", spacerRef, m[idx(1, 0)], m[idx(1, 1)], m[idx(1, 2)], m[idx(1, 3)]);
  471. Con::printf("%s | %-8.4f %-8.4f %-8.4f %-8.4f |", spacerRef, m[idx(2, 0)], m[idx(2, 1)], m[idx(2, 2)], m[idx(2, 3)]);
  472. Con::printf("%s | %-8.4f %-8.4f %-8.4f %-8.4f |", spacerRef, m[idx(3, 0)], m[idx(3, 1)], m[idx(3, 2)], m[idx(3, 3)]);*/
  473. StringBuilder str;
  474. str.format("%s = |", caption);
  475. for (U32 i = 0; i < rows; i++) {
  476. if (i > 0) {
  477. str.append(spacerRef);
  478. }
  479. for (U32 j = 0; j < cols; j++) {
  480. str.format(formatSpec, (*this)(i, j));
  481. }
  482. str.append(" |\n");
  483. }
  484. Con::printf("%s", str.end().c_str());
  485. }