mMatrix.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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. // row-major for now, since torque says it uses that
  183. // but in future could cut down on transpose calls if
  184. // we switch to column major.
  185. //------------------------------------
  186. template<typename DATA_TYPE, U32 rows, U32 cols>
  187. const Matrix<DATA_TYPE, rows, cols> Matrix<DATA_TYPE, rows, cols>::Identity = []() {
  188. Matrix<DATA_TYPE, rows, cols> identity(true);
  189. return identity;
  190. }();
  191. template<typename DATA_TYPE, U32 rows, U32 cols>
  192. Matrix<DATA_TYPE, rows, cols>::Matrix(const EulerF& e)
  193. {
  194. set(e);
  195. }
  196. template<typename DATA_TYPE, U32 rows, U32 cols>
  197. Matrix<DATA_TYPE, rows, cols>& Matrix<DATA_TYPE, rows, cols>::set(const EulerF& e)
  198. {
  199. // when the template refactor is done, euler will be able to be setup in different ways
  200. AssertFatal(rows >= 3 && cols >= 3, "EulerF can only initialize 3x3 or more");
  201. static_assert(std::is_same<DATA_TYPE, float>::value, "Can only initialize eulers with floats for now");
  202. F32 cosPitch, sinPitch;
  203. mSinCos(e.x, sinPitch, cosPitch);
  204. F32 cosYaw, sinYaw;
  205. mSinCos(e.y, sinYaw, cosYaw);
  206. F32 cosRoll, sinRoll;
  207. mSinCos(e.z, sinRoll, cosRoll);
  208. enum {
  209. AXIS_X = (1 << 0),
  210. AXIS_Y = (1 << 1),
  211. AXIS_Z = (1 << 2)
  212. };
  213. U32 axis = 0;
  214. if (e.x != 0.0f) axis |= AXIS_X;
  215. if (e.y != 0.0f) axis |= AXIS_Y;
  216. if (e.z != 0.0f) axis |= AXIS_Z;
  217. switch (axis) {
  218. case 0:
  219. (*this) = Matrix<DATA_TYPE, rows, cols>(true);
  220. break;
  221. case AXIS_X:
  222. (*this)(0, 0) = 1.0f; (*this)(1, 0) = 0.0f; (*this)(2, 0) = 0.0f;
  223. (*this)(0, 1) = 0.0f; (*this)(1, 1) = cosPitch; (*this)(2, 1) = -sinPitch;
  224. (*this)(0, 2) = 0.0f; (*this)(1, 2) = sinPitch; (*this)(2, 2) = cosPitch;
  225. break;
  226. case AXIS_Y:
  227. (*this)(0, 0) = cosYaw; (*this)(1, 0) = 0.0f; (*this)(2, 0) = sinYaw;
  228. (*this)(0, 1) = 0.0f; (*this)(1, 1) = 1.0f; (*this)(2, 1) = 0.0f;
  229. (*this)(0, 2) = -sinYaw; (*this)(1, 2) = 0.0f; (*this)(2, 2) = cosYaw;
  230. break;
  231. case AXIS_Z:
  232. (*this)(0, 0) = cosRoll; (*this)(1, 0) = -sinRoll; (*this)(2, 0) = 0.0f;
  233. (*this)(0, 1) = sinRoll; (*this)(1, 1) = cosRoll; (*this)(2, 1) = 0.0f;
  234. (*this)(0, 2) = 0.0f; (*this)(1, 2) = 0.0f; (*this)(2, 2) = 0.0f;
  235. break;
  236. default:
  237. F32 r1 = cosYaw * cosRoll;
  238. F32 r2 = cosYaw * sinRoll;
  239. F32 r3 = sinYaw * cosRoll;
  240. F32 r4 = sinYaw * sinRoll;
  241. // the matrix looks like this:
  242. // r1 - (r4 * sin(x)) r2 + (r3 * sin(x)) -cos(x) * sin(y)
  243. // -cos(x) * sin(z) cos(x) * cos(z) sin(x)
  244. // r3 + (r2 * sin(x)) r4 - (r1 * sin(x)) cos(x) * cos(y)
  245. //
  246. // where:
  247. // r1 = cos(y) * cos(z)
  248. // r2 = cos(y) * sin(z)
  249. // r3 = sin(y) * cos(z)
  250. // r4 = sin(y) * sin(z)
  251. // init the euler 3x3 rotation matrix.
  252. (*this)(0, 0) = r1 - (r4 * sinPitch); (*this)(1, 0) = -cosPitch * sinRoll; (*this)(2, 0) = r3 + (r2 * sinPitch);
  253. (*this)(0, 1) = r2 + (r3 * sinPitch); (*this)(1, 1) = cosPitch * cosRoll; (*this)(2, 1) = r4 - (r1 * sinPitch);
  254. (*this)(0, 2) = -cosPitch * sinYaw; (*this)(1, 2) = sinPitch; (*this)(2, 2) = cosPitch * cosYaw;
  255. break;
  256. }
  257. if (rows == 4) {
  258. (*this)(3, 0) = 0.0f;
  259. (*this)(3, 1) = 0.0f;
  260. (*this)(3, 2) = 0.0f;
  261. }
  262. if (cols == 4) {
  263. (*this)(0, 3) = 0.0f;
  264. (*this)(1, 3) = 0.0f;
  265. (*this)(2, 3) = 0.0f;
  266. }
  267. if (rows == 4 && cols == 4) {
  268. (*this)(3, 3) = 1.0f;
  269. }
  270. return(*this);
  271. }
  272. template<typename DATA_TYPE, U32 rows, U32 cols>
  273. Matrix<DATA_TYPE, rows, cols>::Matrix(const EulerF& e, const Point3F p)
  274. {
  275. set(e, p);
  276. }
  277. template<typename DATA_TYPE, U32 rows, U32 cols>
  278. Matrix<DATA_TYPE, rows, cols>& Matrix<DATA_TYPE, rows, cols>::set(const EulerF& e, const Point3F p)
  279. {
  280. AssertFatal(rows >= 3 && cols >= 4, "Euler and Point can only initialize 3x4 or more");
  281. // call set euler, this already sets the last row if it exists.
  282. set(e);
  283. // does this need to multiply with the result of the euler? or are we just setting position.
  284. (*this)(0, 3) = p.x;
  285. (*this)(1, 3) = p.y;
  286. (*this)(2, 3) = p.z;
  287. return (*this);
  288. }
  289. template<typename DATA_TYPE, U32 rows, U32 cols>
  290. Matrix<DATA_TYPE, rows, cols>& Matrix<DATA_TYPE, rows, cols>::inverse()
  291. {
  292. // TODO: insert return statement here
  293. AssertFatal(rows == cols, "Can only perform inverse on square matrices.");
  294. const U32 size = rows;
  295. // Create augmented matrix [this | I]
  296. Matrix<DATA_TYPE, size, 2 * size> augmentedMatrix;
  297. Matrix<DATA_TYPE, size, size> resultMatrix;
  298. for (U32 i = 0; i < size; i++) {
  299. for (U32 j = 0; j < size; j++) {
  300. augmentedMatrix(i, j) = (*this)(i, j);
  301. augmentedMatrix(i, j + size) = (i == j) ? static_cast<DATA_TYPE>(1) : static_cast<DATA_TYPE>(0);
  302. }
  303. }
  304. // Apply gauss-joran elimination
  305. for (U32 i = 0; i < size; i++) {
  306. U32 pivotRow = i;
  307. for (U32 k = i + 1; k < size; k++) {
  308. // use std::abs until the templated math functions are in place.
  309. if (std::abs(augmentedMatrix(k, i)) > std::abs(augmentedMatrix(pivotRow, i))) {
  310. pivotRow = k;
  311. }
  312. }
  313. // Swap if needed.
  314. if (i != pivotRow) {
  315. for (U32 j = 0; j < 2 * size; j++) {
  316. std::swap(augmentedMatrix(i, j), augmentedMatrix(pivotRow, j));
  317. }
  318. }
  319. // Early out if pivot is 0, return a new empty matrix.
  320. if (augmentedMatrix(i, i) == static_cast<DATA_TYPE>(0)) {
  321. return Matrix<DATA_TYPE, rows, cols>();
  322. }
  323. DATA_TYPE pivotVal = augmentedMatrix(i, i);
  324. // scale the pivot
  325. for (U32 j = 0; j < 2 * size; ++j) {
  326. augmentedMatrix(i, j) /= pivotVal;
  327. }
  328. // Eliminate the current column in all other rows
  329. for (std::size_t k = 0; k < size; k++) {
  330. if (k != i) {
  331. DATA_TYPE factor = augmentedMatrix(k, i);
  332. for (std::size_t j = 0; j < 2 * size; ++j) {
  333. augmentedMatrix(k, j) -= factor * augmentedMatrix(i, j);
  334. }
  335. }
  336. }
  337. }
  338. for (U32 i = 0; i < size; i++) {
  339. for (U32 j = 0; j < size; j++) {
  340. resultMatrix(i, j) = augmentedMatrix(i, j + size);
  341. }
  342. }
  343. return resultMatrix;
  344. }
  345. template<typename DATA_TYPE, U32 rows, U32 cols>
  346. void Matrix<DATA_TYPE, rows, cols>::invert()
  347. {
  348. (*this) = inverse();
  349. }
  350. template<typename DATA_TYPE, U32 rows, U32 cols>
  351. Matrix<DATA_TYPE, rows, cols>& Matrix<DATA_TYPE, rows, cols>::setCrossProduct(const Point3F& p)
  352. {
  353. AssertFatal(rows == 4 && cols == 4, "Cross product only supported on 4x4 for now");
  354. (*this)(0, 0) = 0;
  355. (*this)(0, 1) = -p.z;
  356. (*this)(0, 2) = p.y;
  357. (*this)(0, 3) = 0;
  358. (*this)(1, 0) = p.z;
  359. (*this)(1, 1) = 0;
  360. (*this)(1, 2) = -p.x;
  361. (*this)(1, 3) = 0;
  362. (*this)(2, 0) = -p.y;
  363. (*this)(2, 1) = p.x;
  364. (*this)(2, 2) = 0;
  365. (*this)(2, 3) = 0;
  366. (*this)(3, 0) = 0;
  367. (*this)(3, 1) = 0;
  368. (*this)(3, 2) = 0;
  369. (*this)(3, 3) = 1;
  370. return (*this);
  371. }
  372. template<typename DATA_TYPE, U32 rows, U32 cols>
  373. Matrix<DATA_TYPE, rows, cols>& Matrix<DATA_TYPE, rows, cols>::setTensorProduct(const Point3F& p, const Point3F& q)
  374. {
  375. AssertFatal(rows == 4 && cols == 4, "Tensor product only supported on 4x4 for now");
  376. (*this)(0, 0) = p.x * q.x;
  377. (*this)(0, 1) = p.x * q.y;
  378. (*this)(0, 2) = p.x * q.z;
  379. (*this)(0, 3) = 0;
  380. (*this)(1, 0) = p.y * q.x;
  381. (*this)(1, 1) = p.y * q.y;
  382. (*this)(1, 2) = p.y * q.z;
  383. (*this)(1, 3) = 0;
  384. (*this)(2, 0) = p.z * q.x;
  385. (*this)(2, 1) = p.z * q.y;
  386. (*this)(2, 2) = p.z * q.z;
  387. (*this)(2, 3) = 0;
  388. (*this)(3, 0) = 0;
  389. (*this)(3, 1) = 0;
  390. (*this)(3, 2) = 0;
  391. (*this)(3, 3) = 1;
  392. return (*this);
  393. }
  394. template<typename DATA_TYPE, U32 rows, U32 cols>
  395. bool Matrix<DATA_TYPE, rows, cols>::isAffine() const
  396. {
  397. if ((*this)(rows - 1, cols - 1) != 1.0f) {
  398. return false;
  399. }
  400. for (U32 col = 0; col < cols - 1; ++col) {
  401. if ((*this)(rows - 1, col) != 0.0f) {
  402. return false;
  403. }
  404. }
  405. Point3F one, two, three;
  406. getColumn(0, &one);
  407. getColumn(1, &two);
  408. getColumn(2, &three);
  409. // check columns
  410. {
  411. if (mDot(one, two) > 0.0001f ||
  412. mDot(one, three) > 0.0001f ||
  413. mDot(two, three) > 0.0001f)
  414. return false;
  415. if (mFabs(1.0f - one.lenSquared()) > 0.0001f ||
  416. mFabs(1.0f - two.lenSquared()) > 0.0001f ||
  417. mFabs(1.0f - three.lenSquared()) > 0.0001f)
  418. return false;
  419. }
  420. getRow(0, &one);
  421. getRow(1, &two);
  422. getRow(2, &three);
  423. // check rows
  424. {
  425. if (mDot(one, two) > 0.0001f ||
  426. mDot(one, three) > 0.0001f ||
  427. mDot(two, three) > 0.0001f)
  428. return false;
  429. if (mFabs(1.0f - one.lenSquared()) > 0.0001f ||
  430. mFabs(1.0f - two.lenSquared()) > 0.0001f ||
  431. mFabs(1.0f - three.lenSquared()) > 0.0001f)
  432. return false;
  433. }
  434. return true;
  435. }
  436. template<typename DATA_TYPE, U32 rows, U32 cols>
  437. EulerF Matrix<DATA_TYPE, rows, cols>::toEuler() const
  438. {
  439. AssertFatal(rows >= 3 && cols >= 3, "Euler rotations require at least a 3x3 matrix.");
  440. // Extract rotation matrix components
  441. const DATA_TYPE m00 = (*this)(0, 0);
  442. const DATA_TYPE m01 = (*this)(0, 1);
  443. const DATA_TYPE m02 = (*this)(0, 2);
  444. const DATA_TYPE m10 = (*this)(1, 0);
  445. const DATA_TYPE m11 = (*this)(1, 1);
  446. const DATA_TYPE m21 = (*this)(2, 1);
  447. const DATA_TYPE m22 = (*this)(2, 2);
  448. // like all others assume float for now.
  449. EulerF r;
  450. r.x = mAsin(mClampF(m21, -1.0, 1.0));
  451. if (mCos(r.x) != 0.0f) {
  452. r.y = mAtan2(-m02, m22); // yaw
  453. r.z = mAtan2(-m10, m11); // roll
  454. }
  455. else {
  456. r.y = 0.0f;
  457. r.z = mAtan2(m01, m00); // this rolls when pitch is +90 degrees
  458. }
  459. return r;
  460. }
  461. template<typename DATA_TYPE, U32 rows, U32 cols>
  462. void Matrix<DATA_TYPE, rows, cols>::dumpMatrix(const char* caption) const
  463. {
  464. U32 size = (caption == NULL) ? 0 : dStrlen(caption);
  465. FrameTemp<char> spacer(size + 1);
  466. char* spacerRef = spacer;
  467. // is_floating_point should return true for floats and doubles.
  468. const char* formatSpec = std::is_floating_point_v<DATA_TYPE> ? " %-8.4f" : " %d";
  469. dMemset(spacerRef, ' ', size);
  470. // null terminate.
  471. spacerRef[size] = '\0';
  472. /*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)]);
  473. 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)]);
  474. 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)]);
  475. 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)]);*/
  476. StringBuilder str;
  477. str.format("%s = |", caption);
  478. for (U32 i = 0; i < rows; i++) {
  479. if (i > 0) {
  480. str.append(spacerRef);
  481. }
  482. for (U32 j = 0; j < cols; j++) {
  483. str.format(formatSpec, (*this)(i, j));
  484. }
  485. str.append(" |\n");
  486. }
  487. Con::printf("%s", str.end().c_str());
  488. }