Matrix3x3.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. © Siemens AG, 2018
  3. Author: Berkay Alp Cakal (berkay_alp.cakal.ct@siemens.com)
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. <http://www.apache.org/licenses/LICENSE-2.0>.
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. // Added vector(1x3) * (3x3)Matrix and vector(3x1) * vector(1x3)
  15. // UoK , 2019, Odysseas Doumas (od79@kent.ac.uk / odydoum@gmail.com)
  16. using System.Collections.Generic;
  17. using UnityEngine;
  18. namespace RosSharp
  19. {
  20. public class Matrix3x3
  21. {
  22. public float[][] elements;
  23. public Matrix3x3(float x = 0)
  24. {
  25. elements = new float[][] { new float[3], new float[3], new float[3] };
  26. for (int i = 0; i < 3; i++)
  27. for (int j = 0; j < 3; j++)
  28. elements[i][j] = x;
  29. }
  30. public Matrix3x3(float[] elements)
  31. {
  32. if (elements.Length == 3) // elements = [xx, yy, zz]
  33. this.elements = new float[][] { new float[] { elements[0], 0, 0 },
  34. new float[] { 0, elements[1], 0 },
  35. new float[] { 0, 0, elements[2] } };
  36. if (elements.Length == 6) // elements = [xx, xy, xz, yy, yz, zz]
  37. this.elements = new float[][] { new float[] { elements[0], elements[1], elements[2] },
  38. new float[] { elements[1], elements[3], elements[4] },
  39. new float[] { elements[2], elements[4], elements[5] } };
  40. if (elements.Length == 9) // elements = [xx, xy, xz, yx, yy, yz, zx, zy, zz]
  41. this.elements = new float[][] { new float[] { elements[0], elements[1], elements[2] },
  42. new float[] { elements[3], elements[4], elements[5] },
  43. new float[] { elements[6], elements[7], elements[8] } };
  44. }
  45. public Matrix3x3(float[][] elements)
  46. {
  47. this.elements = new float[][] { new float[3], new float[3], new float[3] };
  48. for (int i = 0; i < 3; i++)
  49. for (int j = 0; j < 3; j++)
  50. this.elements[i][j] = elements[i][j];
  51. }
  52. public Matrix3x3(Vector3[] vectors)
  53. {
  54. elements = new float[][] { new float[] { vectors[0].x, vectors[0].y, vectors[0].z },
  55. new float[] { vectors[1].x, vectors[1].y, vectors[1].z },
  56. new float[] { vectors[2].x, vectors[2].y, vectors[2].z } };
  57. }
  58. public float[] this[int i]
  59. {
  60. get { return elements[i]; }
  61. set { elements[i] = value; }
  62. }
  63. public static Matrix3x3 operator +(Matrix3x3 A, Matrix3x3 B)
  64. {
  65. Matrix3x3 result = new Matrix3x3();
  66. for (int i = 0; i < 3; i++)
  67. for (int j = 0; j < 3; j++)
  68. result[i][j] = A[i][j] + B[i][j];
  69. return result;
  70. }
  71. public static Matrix3x3 operator +(Matrix3x3 A, float x)
  72. {
  73. Matrix3x3 result = new Matrix3x3();
  74. for (int i = 0; i < 3; i++)
  75. for (int j = 0; j < 3; j++)
  76. result[i][j] = A[i][j] + x;
  77. return result;
  78. }
  79. public static Matrix3x3 operator -(Matrix3x3 A, Matrix3x3 B)
  80. {
  81. Matrix3x3 result = new Matrix3x3();
  82. for (int i = 0; i < 3; i++)
  83. for (int j = 0; j < 3; j++)
  84. result[i][j] = A[i][j] - B[i][j];
  85. return result;
  86. }
  87. public static Matrix3x3 operator -(Matrix3x3 A, float x)
  88. {
  89. Matrix3x3 result = new Matrix3x3();
  90. for (int i = 0; i < 3; i++)
  91. for (int j = 0; j < 3; j++)
  92. result[i][j] = A[i][j] - x;
  93. return result;
  94. }
  95. public static Matrix3x3 operator *(Matrix3x3 A, float x)
  96. {
  97. Matrix3x3 result = new Matrix3x3();
  98. for (int i = 0; i < 3; i++)
  99. for (int j = 0; j < 3; j++)
  100. result[i][j] = A[i][j] * x;
  101. return result;
  102. }
  103. public static Matrix3x3 operator *(Matrix3x3 A, Matrix3x3 B)
  104. {
  105. Matrix3x3 result = new Matrix3x3();
  106. for (int i = 0; i < 3; i++)
  107. for (int j = 0; j < 3; j++)
  108. for (int k = 0; k < 3; k++)
  109. result[i][j] += A[i][k] * B[k][j];
  110. return result;
  111. }
  112. public static Vector3 operator *(Matrix3x3 A, Vector3 B)
  113. {
  114. Vector3 row0 = new Vector3(A[0][0], A[0][1], A[0][2]);
  115. Vector3 row1 = new Vector3(A[1][0], A[1][1], A[1][2]);
  116. Vector3 row2 = new Vector3(A[2][0], A[2][1], A[2][2]);
  117. return new Vector3(Vector3.Dot(row0, B), Vector3.Dot(row1, B), Vector3.Dot(row2, B));
  118. }
  119. public static Matrix3x3 operator *(Vector3 A, Matrix3x3 B)
  120. {
  121. Matrix3x3 result = new Matrix3x3();
  122. for (int i = 0; i < 3; i++)
  123. for (int j = 0; j < 3; j++)
  124. result[i][j] += A[i] * B[i][j];
  125. return result;
  126. }
  127. public static Matrix3x3 VectorMult(Vector3 A, Vector3 B)
  128. {
  129. Matrix3x3 result = new Matrix3x3();
  130. for (int i = 0; i < 3; i++)
  131. for (int j = 0; j < 3; j++)
  132. result[i][j] += A[j] * B[i];
  133. return result;
  134. }
  135. public float Determinant()
  136. {
  137. float result = 0.0f;
  138. for (int i = 0; i < 3; i++)
  139. result += (elements[0][i] * (elements[1][(i + 1) % 3] * elements[2][(i + 2) % 3] - elements[1][(i + 2) % 3] * elements[2][(i + 1) % 3]));
  140. return result;
  141. }
  142. public float Trace()
  143. {
  144. return (elements[0][0] + elements[1][1] + elements[2][2]);
  145. }
  146. public bool IsDiagonal()
  147. {
  148. for (int i = 0; i < 3; i++)
  149. for (int j = 0; j < 3; j++)
  150. if (i != j && elements[i][j] != 0)
  151. return false;
  152. return true;
  153. }
  154. public Matrix3x3 Transpose()
  155. {
  156. return new Matrix3x3(new float[]
  157. { elements[0][0], elements[1][0], elements[2][0],
  158. elements[0][1], elements[1][1], elements[2][1],
  159. elements[0][2], elements[1][2], elements[2][2] });
  160. }
  161. public void DiagonalizeRealSymmetric(out Vector3 EigenvaluesOut, out Vector3[] EigenvectorsOut)
  162. {
  163. if (IsDiagonal())
  164. {
  165. EigenvaluesOut = new Vector3(elements[0][0], elements[1][1], elements[2][2]);
  166. EigenvectorsOut = new Vector3[] { new Vector3(1, 0, 0 ),
  167. new Vector3(0, 1, 0 ),
  168. new Vector3(0, 0, 1 ) };
  169. return;
  170. }
  171. EigenvaluesOut = Eigenvalues();
  172. EigenvectorsOut = Eigenvectors(new float[] { EigenvaluesOut[0], EigenvaluesOut[1], EigenvaluesOut[2] });
  173. }
  174. public Vector3 Eigenvalues()
  175. {
  176. /* Smith, Oliver K. (April 1961), "Eigenvalues of a symmetric 3 × 3 matrix."
  177. * Communications of the ACM, 4 (4): 168, doi:10.1145/355578.366316 */
  178. Matrix3x3 matrix3x3 = this;
  179. float traceA = matrix3x3.Trace();
  180. float q = traceA / 3;
  181. float p1 = matrix3x3[0][1] * matrix3x3[0][1] + matrix3x3[0][2] * matrix3x3[0][2] + matrix3x3[1][2] * matrix3x3[1][2];
  182. float p2 = (matrix3x3[0][0] - q) * (matrix3x3[0][0] - q) + (matrix3x3[1][1] - q) * (matrix3x3[1][1] - q) + (matrix3x3[2][2] - q) * (matrix3x3[2][2] - q) + 2 * p1;
  183. float p = Mathf.Sqrt(p2 / 6);
  184. Matrix3x3 B = (matrix3x3 * (1f / p)) + (new Matrix3x3(new float[] { -q / p, -q / p, -q / p }));
  185. float angle = Mathf.Clamp(B.Determinant() / 2f, -1, 1);
  186. float theta = Mathf.Acos(angle) / 3;
  187. Vector3 beta = new Vector3();
  188. Vector3 alpha = new Vector3();
  189. for (int k = 0; k < 3; k++)
  190. {
  191. beta[k] = 2f * Mathf.Cos(theta + (2 * Mathf.PI * k) / 3);
  192. alpha[k] = p * beta[k] + q;
  193. }
  194. return alpha;
  195. }
  196. private Vector3[] Eigenvectors(float[] eigenvalues_unsorted)
  197. {
  198. float[] eigenvalues = (float[])eigenvalues_unsorted.Clone();
  199. System.Array.Sort(eigenvalues);
  200. Vector3 eigenvector0 = GetEigenvector0(eigenvalues);
  201. Vector3 eigenvector1 = GetEigenvector1(eigenvalues, eigenvector0);
  202. Vector3 eigenvector2 = GetEigenvector2(eigenvalues, eigenvector0, eigenvector1);
  203. List<float> values = new List<float> { eigenvalues[0], eigenvalues[1], eigenvalues[2] };
  204. List<float[]> vectors = new List<float[]>(){
  205. new float[] { eigenvector0[0], eigenvector0[1], eigenvector0[2] },
  206. new float[] { eigenvector1[0], eigenvector1[1], eigenvector1[2] },
  207. new float[] { eigenvector2[0], eigenvector2[1], eigenvector2[2] }
  208. };
  209. List<float> values_unsorted = new List<float>();
  210. List<float[]> vectors_unsorted = new List<float[]>();
  211. for (int i = 0; i < 3; i++)
  212. {
  213. int idx = values.IndexOf(eigenvalues_unsorted[i]);
  214. values_unsorted.Add(values[idx]);
  215. vectors_unsorted.Add(vectors[idx]);
  216. values.RemoveAt(idx);
  217. vectors.RemoveAt(idx);
  218. }
  219. return new Vector3[] { new Vector3(vectors_unsorted[0][0], vectors_unsorted[0][1], vectors_unsorted[0][2]),
  220. new Vector3(vectors_unsorted[1][0], vectors_unsorted[1][1], vectors_unsorted[1][2]),
  221. new Vector3(vectors_unsorted[2][0], vectors_unsorted[2][1], vectors_unsorted[2][2]) };
  222. }
  223. private Vector3 GetEigenvector0(float[] eigenvalues)
  224. {
  225. if (IsTwoEigenvaluesEqual(eigenvalues))
  226. return new Vector3(1, 0, 0);
  227. Vector3 eigenvector0;
  228. Vector3 row0 = new Vector3(elements[0][0] - eigenvalues[0], elements[0][1], elements[0][2]);
  229. Vector3 row1 = new Vector3(elements[1][0], elements[1][1] - eigenvalues[0], elements[1][2]);
  230. Vector3 row2 = new Vector3(elements[2][0], elements[2][1], elements[2][2] - eigenvalues[0]);
  231. Vector3 cross_r0r1 = Vector3.Cross(row0, row1);
  232. Vector3 cross_r0r2 = Vector3.Cross(row0, row2);
  233. Vector3 cross_r1r2 = Vector3.Cross(row1, row2);
  234. float dot0 = Vector3.Dot(cross_r0r1, cross_r0r1);
  235. float dot1 = Vector3.Dot(cross_r0r2, cross_r0r2);
  236. float dot2 = Vector3.Dot(cross_r1r2, cross_r1r2);
  237. float dmax = dot0;
  238. int imax = 0;
  239. if (dot1 > dmax) { dmax = dot1; imax = 1; }
  240. if (dot2 > dmax) { imax = 2; }
  241. if (imax == 0)
  242. eigenvector0 = new Vector3(cross_r0r1[0] / Mathf.Sqrt(dot0), cross_r0r1[1] / Mathf.Sqrt(dot0), cross_r0r1[2] / Mathf.Sqrt(dot0));
  243. else if (imax == 1)
  244. eigenvector0 = new Vector3(cross_r0r2[0] / Mathf.Sqrt(dot1), cross_r0r2[1] / Mathf.Sqrt(dot1), cross_r0r2[2] / Mathf.Sqrt(dot1));
  245. else
  246. eigenvector0 = new Vector3(cross_r1r2[0] / Mathf.Sqrt(dot2), cross_r1r2[1] / Mathf.Sqrt(dot2), cross_r1r2[2] / Mathf.Sqrt(dot2));
  247. return eigenvector0;
  248. }
  249. private Vector3 GetEigenvector1(float[] eigenvalues, Vector3 eigenvector0)
  250. {
  251. Matrix3x3 inertiaTensor = this;
  252. if (IsTwoEigenvaluesEqual(eigenvalues))
  253. return new Vector3(0, 1, 0);
  254. Vector3 eigenvector1 = Vector3.zero;
  255. Vector3[] UV = CalculateOrthogonalComplement(eigenvector0);
  256. Vector3 AU = inertiaTensor * UV[0];
  257. Vector3 AV = inertiaTensor * UV[1];
  258. float m00 = Vector3.Dot(UV[0], AU) - eigenvalues[1];
  259. float m01 = Vector3.Dot(UV[0], AV);
  260. float m11 = Vector3.Dot(UV[1], AV) - eigenvalues[1];
  261. if (Mathf.Abs(m00) > Mathf.Abs(m11))
  262. {
  263. float maxAbscomp = Mathf.Max(Mathf.Abs(m00), Mathf.Abs(m01));
  264. if (maxAbscomp > 0)
  265. {
  266. if (Mathf.Abs(m00) >= Mathf.Abs(m01)) { m01 /= m00; m00 = 1 / Mathf.Sqrt(1 + m01 * m01); m01 *= m00; }
  267. else { m00 /= m01; m01 = 1 / Mathf.Sqrt(1 + m00 * m00); m00 *= m01; }
  268. eigenvector1 = (UV[0] * m01) + (UV[1] * -m00);
  269. }
  270. else
  271. eigenvector1 = UV[0];
  272. }
  273. else
  274. {
  275. float maxAbscomp = Mathf.Max(Mathf.Abs(m11), Mathf.Abs(m01));
  276. if (maxAbscomp > 0)
  277. {
  278. if (Mathf.Abs(m11) >= Mathf.Abs(m01)) { m01 /= m11; m11 = 1 / Mathf.Sqrt(1 + m01 * m01); m01 *= m11; }
  279. else { m11 /= m01; m01 = 1 / Mathf.Sqrt(1 + m11 * m11); m11 *= m01; }
  280. eigenvector1 = (UV[0] * m11) + (UV[1] * -m00);
  281. }
  282. else
  283. eigenvector1 = UV[0];
  284. }
  285. return eigenvector1;
  286. }
  287. private Vector3 GetEigenvector2(float[] eigenvalues, Vector3 eigenvector0, Vector3 eigenvector1)
  288. {
  289. if (IsTwoEigenvaluesEqual(eigenvalues))
  290. return new Vector3(0, 0, 1);
  291. return Vector3.Cross(eigenvector0, eigenvector1);
  292. }
  293. private Vector3[] CalculateOrthogonalComplement(Vector3 W)
  294. {
  295. Vector3 U; Vector3 V;
  296. float invLength = 0;
  297. if (Mathf.Abs(W[0]) > Mathf.Abs(W[1]))
  298. {
  299. invLength = 1 / Mathf.Sqrt(W[0] * W[0] + W[2] * W[2]);
  300. U = new Vector3(-W[2] * invLength, 0, W[0] * invLength);
  301. }
  302. else
  303. {
  304. invLength = 1 / Mathf.Sqrt(W[1] * W[1] + W[2] * W[2]);
  305. U = new Vector3(0, W[2] * invLength, -W[1] * invLength);
  306. }
  307. V = Vector3.Cross(W, U);
  308. return new Vector3[] { U, V };
  309. }
  310. private bool IsTwoEigenvaluesEqual(float[] eigenvalues)
  311. {
  312. return (eigenvalues[0] == eigenvalues[1] || eigenvalues[1] == eigenvalues[2] || eigenvalues[0] == eigenvalues[2]);
  313. }
  314. }
  315. }