1
0

float3x3.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /* The MIT License
  2. *
  3. * Copyright (c) 2010 Intel Corporation.
  4. * All rights reserved.
  5. *
  6. * Based on the convexdecomposition library from
  7. * <http://codesuppository.googlecode.com> by John W. Ratcliff and Stan Melax.
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Diagnostics;
  30. namespace OpenSim.Region.PhysicsModules.ConvexDecompositionDotNet
  31. {
  32. public class float3x3
  33. {
  34. public float3 x = new float3();
  35. public float3 y = new float3();
  36. public float3 z = new float3();
  37. public float3x3()
  38. {
  39. }
  40. public float3x3(float xx, float xy, float xz, float yx, float yy, float yz, float zx, float zy, float zz)
  41. {
  42. x = new float3(xx, xy, xz);
  43. y = new float3(yx, yy, yz);
  44. z = new float3(zx, zy, zz);
  45. }
  46. public float3x3(float3 _x, float3 _y, float3 _z)
  47. {
  48. x = new float3(_x);
  49. y = new float3(_y);
  50. z = new float3(_z);
  51. }
  52. public float3 this[int i]
  53. {
  54. get
  55. {
  56. switch (i)
  57. {
  58. case 0: return x;
  59. case 1: return y;
  60. case 2: return z;
  61. }
  62. throw new ArgumentOutOfRangeException();
  63. }
  64. }
  65. public float this[int i, int j]
  66. {
  67. get
  68. {
  69. switch (i)
  70. {
  71. case 0:
  72. switch (j)
  73. {
  74. case 0: return x.x;
  75. case 1: return x.y;
  76. case 2: return x.z;
  77. }
  78. break;
  79. case 1:
  80. switch (j)
  81. {
  82. case 0: return y.x;
  83. case 1: return y.y;
  84. case 2: return y.z;
  85. }
  86. break;
  87. case 2:
  88. switch (j)
  89. {
  90. case 0: return z.x;
  91. case 1: return z.y;
  92. case 2: return z.z;
  93. }
  94. break;
  95. }
  96. throw new ArgumentOutOfRangeException();
  97. }
  98. set
  99. {
  100. switch (i)
  101. {
  102. case 0:
  103. switch (j)
  104. {
  105. case 0: x.x = value; return;
  106. case 1: x.y = value; return;
  107. case 2: x.z = value; return;
  108. }
  109. break;
  110. case 1:
  111. switch (j)
  112. {
  113. case 0: y.x = value; return;
  114. case 1: y.y = value; return;
  115. case 2: y.z = value; return;
  116. }
  117. break;
  118. case 2:
  119. switch (j)
  120. {
  121. case 0: z.x = value; return;
  122. case 1: z.y = value; return;
  123. case 2: z.z = value; return;
  124. }
  125. break;
  126. }
  127. throw new ArgumentOutOfRangeException();
  128. }
  129. }
  130. public static float3x3 Transpose(float3x3 m)
  131. {
  132. return new float3x3(new float3(m.x.x, m.y.x, m.z.x), new float3(m.x.y, m.y.y, m.z.y), new float3(m.x.z, m.y.z, m.z.z));
  133. }
  134. public static float3x3 operator *(float3x3 a, float3x3 b)
  135. {
  136. return new float3x3(a.x * b, a.y * b, a.z * b);
  137. }
  138. public static float3x3 operator *(float3x3 a, float s)
  139. {
  140. return new float3x3(a.x * s, a.y * s, a.z * s);
  141. }
  142. public static float3x3 operator /(float3x3 a, float s)
  143. {
  144. float t = 1f / s;
  145. return new float3x3(a.x * t, a.y * t, a.z * t);
  146. }
  147. public static float3x3 operator +(float3x3 a, float3x3 b)
  148. {
  149. return new float3x3(a.x + b.x, a.y + b.y, a.z + b.z);
  150. }
  151. public static float3x3 operator -(float3x3 a, float3x3 b)
  152. {
  153. return new float3x3(a.x - b.x, a.y - b.y, a.z - b.z);
  154. }
  155. public static float Determinant(float3x3 m)
  156. {
  157. return m.x.x * m.y.y * m.z.z + m.y.x * m.z.y * m.x.z + m.z.x * m.x.y * m.y.z - m.x.x * m.z.y * m.y.z - m.y.x * m.x.y * m.z.z - m.z.x * m.y.y * m.x.z;
  158. }
  159. public static float3x3 Inverse(float3x3 a)
  160. {
  161. float3x3 b = new float3x3();
  162. float d = Determinant(a);
  163. Debug.Assert(d != 0);
  164. for (int i = 0; i < 3; i++)
  165. {
  166. for (int j = 0; j < 3; j++)
  167. {
  168. int i1 = (i + 1) % 3;
  169. int i2 = (i + 2) % 3;
  170. int j1 = (j + 1) % 3;
  171. int j2 = (j + 2) % 3;
  172. // reverse indexs i&j to take transpose
  173. b[i, j] = (a[i1][j1] * a[i2][j2] - a[i1][j2] * a[i2][j1]) / d;
  174. }
  175. }
  176. return b;
  177. }
  178. }
  179. }