/Genetibase.MathX/Genetibase.MathX_x86/Genetibase.MathX.NuGenStructures/NuGenTrafo3F.cs

https://github.com/AnthonyNystrom/GenXSource · C# · 320 lines · 279 code · 38 blank · 3 comment · 2 complexity · 0863679df5dcc616ed890135f04a7be1 MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace Genetibase.MathX.NuGenStructures
  5. {
  6. /// <summary>
  7. /// NuGenTrafo3F.
  8. /// </summary>
  9. public struct NuGenTrafo3F
  10. {
  11. public static NuGenTrafo3F Identity = new NuGenTrafo3F(
  12. 1, 0, 0, 0,
  13. 0, 1, 0, 0,
  14. 0, 0, 1, 0,
  15. 0, 0, 0, 1
  16. );
  17. public NuGenTrafo3F(
  18. float x00, float x01, float x02, float x03,
  19. float x10, float x11, float x12, float x13,
  20. float x20, float x21, float x22, float x23,
  21. float x30, float x31, float x32, float x33
  22. )
  23. {
  24. _x = new float[16];
  25. _x[0] = x00; _x[1] = x01; _x[2] = x02; _x[3] = x03;
  26. _x[4] = x10; _x[5] = x11; _x[6] = x12; _x[7] = x13;
  27. _x[8] = x20; _x[9] = x21; _x[10] = x22; _x[11] = x23;
  28. _x[12] = x30; _x[13] = x31; _x[14] = x32; _x[15] = x33;
  29. }
  30. public NuGenTrafo3F(NuGenRot3F r)
  31. {
  32. this = r.ToNuGenTrafo3F();
  33. }
  34. public NuGenTrafo3F(NuGenTrafo2F t)
  35. {
  36. _x = new float[16];
  37. _x[0] = t[0, 0]; _x[1] = t[0, 1]; _x[2] = 0.0f; _x[3] = t[0, 2];
  38. _x[4] = t[1, 0]; _x[5] = t[1, 1]; _x[6] = 0.0f; _x[7] = t[1, 2];
  39. _x[8] = 0.0f; _x[9] = 0.0f; _x[10] = 1.0f; _x[11] = 0.0f;
  40. _x[12] = t[2, 0]; _x[13] = t[2, 1]; _x[14] = 0.0f; _x[15] = t[2, 2];
  41. }
  42. public static explicit operator NuGenTrafo3F(NuGenTrafo3D t)
  43. {
  44. return new NuGenTrafo3F(
  45. (float)t._x[0], (float)t._x[1], (float)t._x[2], (float)t._x[3],
  46. (float)t._x[4], (float)t._x[5], (float)t._x[6], (float)t._x[7],
  47. (float)t._x[8], (float)t._x[9], (float)t._x[10], (float)t._x[11],
  48. (float)t._x[12], (float)t._x[13], (float)t._x[14], (float)t._x[15]
  49. );
  50. }
  51. public float this[int i]
  52. {
  53. get
  54. {
  55. return _x[i];
  56. }
  57. set
  58. {
  59. _x[i] = value;
  60. }
  61. }
  62. public float this[int i, int j]
  63. {
  64. get
  65. {
  66. return _x[i * 4 + j];
  67. }
  68. set
  69. {
  70. _x[i * 4 + j] = value;
  71. }
  72. }
  73. public NuGenTrafo3F Transposed
  74. {
  75. get
  76. {
  77. return new NuGenTrafo3F(
  78. _x[0], _x[4], _x[8], _x[12],
  79. _x[1], _x[5], _x[9], _x[13],
  80. _x[2], _x[6], _x[10], _x[14],
  81. _x[3], _x[7], _x[11], _x[15]
  82. );
  83. }
  84. }
  85. public void Transpose()
  86. {
  87. this = this.Transposed;
  88. }
  89. public float Det
  90. {
  91. get
  92. {
  93. return NuGenVector.Det4x4(_x);
  94. }
  95. }
  96. public NuGenTrafo3F Adjoint
  97. {
  98. get
  99. {
  100. return new NuGenTrafo3F(
  101. NuGenVector.Det3x3(
  102. _x[5], _x[6], _x[7],
  103. _x[9], _x[10], _x[11],
  104. _x[13], _x[14], _x[15]), // 00
  105. -NuGenVector.Det3x3(
  106. _x[4], _x[6], _x[7],
  107. _x[8], _x[10], _x[11],
  108. _x[12], _x[14], _x[15]), // 01
  109. NuGenVector.Det3x3(
  110. _x[4], _x[5], _x[7],
  111. _x[8], _x[9], _x[11],
  112. _x[12], _x[13], _x[15]), // 02
  113. -NuGenVector.Det3x3(
  114. _x[4], _x[5], _x[6],
  115. _x[8], _x[9], _x[10],
  116. _x[12], _x[13], _x[14]), // 03
  117. NuGenVector.Det3x3(
  118. _x[1], _x[2], _x[3],
  119. _x[9], _x[10], _x[11],
  120. _x[13], _x[14], _x[15]), // 10
  121. -NuGenVector.Det3x3(
  122. _x[0], _x[2], _x[3],
  123. _x[8], _x[10], _x[11],
  124. _x[12], _x[14], _x[15]), // 11
  125. NuGenVector.Det3x3(
  126. _x[0], _x[1], _x[3],
  127. _x[8], _x[9], _x[11],
  128. _x[12], _x[13], _x[15]), // 12
  129. -NuGenVector.Det3x3(
  130. _x[0], _x[1], _x[2],
  131. _x[8], _x[9], _x[10],
  132. _x[12], _x[13], _x[14]), // 13
  133. NuGenVector.Det3x3(
  134. _x[1], _x[2], _x[3],
  135. _x[5], _x[6], _x[7],
  136. _x[13], _x[14], _x[15]), // 20
  137. -NuGenVector.Det3x3(
  138. _x[0], _x[2], _x[3],
  139. _x[4], _x[6], _x[7],
  140. _x[12], _x[14], _x[15]), // 21
  141. NuGenVector.Det3x3(
  142. _x[0], _x[1], _x[3],
  143. _x[4], _x[5], _x[7],
  144. _x[12], _x[13], _x[15]), // 22
  145. -NuGenVector.Det3x3(
  146. _x[0], _x[1], _x[2],
  147. _x[4], _x[5], _x[6],
  148. _x[12], _x[13], _x[14]), // 23
  149. NuGenVector.Det3x3(
  150. _x[1], _x[2], _x[3],
  151. _x[5], _x[6], _x[7],
  152. _x[9], _x[10], _x[11]), // 24
  153. -NuGenVector.Det3x3(
  154. _x[0], _x[2], _x[3],
  155. _x[4], _x[6], _x[7],
  156. _x[8], _x[10], _x[11]), // 25
  157. NuGenVector.Det3x3(
  158. _x[0], _x[1], _x[3],
  159. _x[4], _x[5], _x[7],
  160. _x[8], _x[9], _x[11]), // 26
  161. -NuGenVector.Det3x3(
  162. _x[0], _x[1], _x[2],
  163. _x[4], _x[5], _x[6],
  164. _x[8], _x[9], _x[10]) // 27
  165. );
  166. }
  167. }
  168. public NuGenTrafo3F Inverse
  169. {
  170. get
  171. {
  172. float d = Det;
  173. if (d == 0) throw new ArithmeticException("Singular Matrix");
  174. d = 1.0f / d;
  175. return Adjoint.Transposed * d;
  176. }
  177. }
  178. public void Invert()
  179. {
  180. this = Inverse;
  181. }
  182. public static NuGenTrafo3F operator +(NuGenTrafo3F t, float f)
  183. {
  184. return new NuGenTrafo3F(
  185. t._x[0] + f, t._x[1] + f, t._x[2] + f, t._x[3] + f,
  186. t._x[4] + f, t._x[5] + f, t._x[6] + f, t._x[7] + f,
  187. t._x[8] + f, t._x[9] + f, t._x[10] + f, t._x[11] + f,
  188. t._x[12] + f, t._x[13] + f, t._x[14] + f, t._x[15] + f
  189. );
  190. }
  191. public static NuGenTrafo3F operator -(NuGenTrafo3F t, float f)
  192. {
  193. return new NuGenTrafo3F(
  194. t._x[0] - f, t._x[1] - f, t._x[2] - f, t._x[3] - f,
  195. t._x[4] - f, t._x[5] - f, t._x[6] - f, t._x[7] - f,
  196. t._x[8] - f, t._x[9] - f, t._x[10] - f, t._x[11] - f,
  197. t._x[12] - f, t._x[13] - f, t._x[14] - f, t._x[15] - f
  198. );
  199. }
  200. public static NuGenTrafo3F operator *(NuGenTrafo3F t, float f)
  201. {
  202. return new NuGenTrafo3F(
  203. t._x[0] * f, t._x[1] * f, t._x[2] * f, t._x[3] * f,
  204. t._x[4] * f, t._x[5] * f, t._x[6] * f, t._x[7] * f,
  205. t._x[8] * f, t._x[9] * f, t._x[10] * f, t._x[11] * f,
  206. t._x[12] * f, t._x[13] * f, t._x[14] * f, t._x[15] * f
  207. );
  208. }
  209. public static NuGenTrafo3F operator /(NuGenTrafo3F t, float f)
  210. {
  211. return new NuGenTrafo3F(
  212. t._x[0] / f, t._x[1] / f, t._x[2] / f, t._x[3] / f,
  213. t._x[4] / f, t._x[5] / f, t._x[6] / f, t._x[7] / f,
  214. t._x[8] / f, t._x[9] / f, t._x[10] / f, t._x[11] / f,
  215. t._x[12] / f, t._x[13] / f, t._x[14] / f, t._x[15] / f
  216. );
  217. }
  218. public static NuGenPnt3F operator *(NuGenPnt3F p, NuGenTrafo3F t)
  219. {
  220. float f = 1.0f / (p[0] * t[0, 3] + p[1] * t[1, 3] + p[2] * t[2, 3] + t[3, 3]);
  221. return new NuGenPnt3F(
  222. (p[0] * t[0, 0] + p[1] * t[1, 0] + p[2] * t[2, 0] + t[3, 0]) * f,
  223. (p[0] * t[0, 1] + p[1] * t[1, 1] + p[2] * t[2, 1] + t[3, 1]) * f,
  224. (p[0] * t[0, 2] + p[1] * t[1, 2] + p[2] * t[2, 2] + t[3, 2]) * f
  225. );
  226. }
  227. public static NuGenVec3F operator *(NuGenVec3F v, NuGenTrafo3F t)
  228. {
  229. return new NuGenVec3F(
  230. v._x[0] * t._x[0] + v._x[1] * t._x[4] + v._x[2] * t._x[8],
  231. v._x[0] * t._x[1] + v._x[1] * t._x[5] + v._x[2] * t._x[9],
  232. v._x[0] * t._x[2] + v._x[1] * t._x[6] + v._x[2] * t._x[10]
  233. );
  234. }
  235. public static NuGenRay3F operator *(NuGenRay3F r, NuGenTrafo3F t)
  236. {
  237. return new NuGenRay3F(r.p * t, r.v * t);
  238. }
  239. public static NuGenBox3F operator *(NuGenBox3F b, NuGenTrafo3F t)
  240. {
  241. NuGenBox3F result = NuGenBox3F.Empty;
  242. result += b.LLL * t;
  243. result += b.LLU * t;
  244. result += b.LUL * t;
  245. result += b.LUU * t;
  246. result += b.ULL * t;
  247. result += b.ULU * t;
  248. result += b.UUL * t;
  249. result += b.UUU * t;
  250. return result;
  251. }
  252. public static NuGenTrafo3F operator *(NuGenTrafo3F a, NuGenTrafo3F b)
  253. {
  254. return new NuGenTrafo3F(
  255. a._x[0] * b._x[0] + a._x[1] * b._x[4] + a._x[2] * b._x[8] + a._x[3] * b._x[12],
  256. a._x[0] * b._x[1] + a._x[1] * b._x[5] + a._x[2] * b._x[9] + a._x[3] * b._x[13],
  257. a._x[0] * b._x[2] + a._x[1] * b._x[6] + a._x[2] * b._x[10] + a._x[3] * b._x[14],
  258. a._x[0] * b._x[3] + a._x[1] * b._x[7] + a._x[2] * b._x[11] + a._x[3] * b._x[15],
  259. a._x[4] * b._x[0] + a._x[5] * b._x[4] + a._x[6] * b._x[8] + a._x[7] * b._x[12],
  260. a._x[4] * b._x[1] + a._x[5] * b._x[5] + a._x[6] * b._x[9] + a._x[7] * b._x[13],
  261. a._x[4] * b._x[2] + a._x[5] * b._x[6] + a._x[6] * b._x[10] + a._x[7] * b._x[14],
  262. a._x[4] * b._x[3] + a._x[5] * b._x[7] + a._x[6] * b._x[11] + a._x[7] * b._x[15],
  263. a._x[8] * b._x[0] + a._x[9] * b._x[4] + a._x[10] * b._x[8] + a._x[11] * b._x[12],
  264. a._x[8] * b._x[1] + a._x[9] * b._x[5] + a._x[10] * b._x[9] + a._x[11] * b._x[13],
  265. a._x[8] * b._x[2] + a._x[9] * b._x[6] + a._x[10] * b._x[10] + a._x[11] * b._x[14],
  266. a._x[8] * b._x[3] + a._x[9] * b._x[7] + a._x[10] * b._x[11] + a._x[11] * b._x[15],
  267. a._x[12] * b._x[0] + a._x[13] * b._x[4] + a._x[14] * b._x[8] + a._x[15] * b._x[12],
  268. a._x[12] * b._x[1] + a._x[13] * b._x[5] + a._x[14] * b._x[9] + a._x[15] * b._x[13],
  269. a._x[12] * b._x[2] + a._x[13] * b._x[6] + a._x[14] * b._x[10] + a._x[15] * b._x[14],
  270. a._x[12] * b._x[3] + a._x[13] * b._x[7] + a._x[14] * b._x[11] + a._x[15] * b._x[15]
  271. );
  272. }
  273. internal float[] _x;
  274. public float[] Array
  275. {
  276. get
  277. {
  278. return _x;
  279. }
  280. }
  281. }
  282. }