OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
float3x3.cs
Go to the documentation of this file.
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 
28 using System;
29 using System.Collections.Generic;
30 using System.Diagnostics;
31 
32 namespace OpenSim.Region.PhysicsModules.ConvexDecompositionDotNet
33 {
34  public class float3x3
35  {
36  public float3 x = new float3();
37  public float3 y = new float3();
38  public float3 z = new float3();
39 
40  public float3x3()
41  {
42  }
43 
44  public float3x3(float xx, float xy, float xz, float yx, float yy, float yz, float zx, float zy, float zz)
45  {
46  x = new float3(xx, xy, xz);
47  y = new float3(yx, yy, yz);
48  z = new float3(zx, zy, zz);
49  }
50 
51  public float3x3(float3 _x, float3 _y, float3 _z)
52  {
53  x = new float3(_x);
54  y = new float3(_y);
55  z = new float3(_z);
56  }
57 
58  public float3 this[int i]
59  {
60  get
61  {
62  switch (i)
63  {
64  case 0: return x;
65  case 1: return y;
66  case 2: return z;
67  }
68  throw new ArgumentOutOfRangeException();
69  }
70  }
71 
72  public float this[int i, int j]
73  {
74  get
75  {
76  switch (i)
77  {
78  case 0:
79  switch (j)
80  {
81  case 0: return x.x;
82  case 1: return x.y;
83  case 2: return x.z;
84  }
85  break;
86  case 1:
87  switch (j)
88  {
89  case 0: return y.x;
90  case 1: return y.y;
91  case 2: return y.z;
92  }
93  break;
94  case 2:
95  switch (j)
96  {
97  case 0: return z.x;
98  case 1: return z.y;
99  case 2: return z.z;
100  }
101  break;
102  }
103  throw new ArgumentOutOfRangeException();
104  }
105  set
106  {
107  switch (i)
108  {
109  case 0:
110  switch (j)
111  {
112  case 0: x.x = value; return;
113  case 1: x.y = value; return;
114  case 2: x.z = value; return;
115  }
116  break;
117  case 1:
118  switch (j)
119  {
120  case 0: y.x = value; return;
121  case 1: y.y = value; return;
122  case 2: y.z = value; return;
123  }
124  break;
125  case 2:
126  switch (j)
127  {
128  case 0: z.x = value; return;
129  case 1: z.y = value; return;
130  case 2: z.z = value; return;
131  }
132  break;
133  }
134  throw new ArgumentOutOfRangeException();
135  }
136  }
137 
138  public static float3x3 Transpose(float3x3 m)
139  {
140  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));
141  }
142 
143  public static float3x3 operator *(float3x3 a, float3x3 b)
144  {
145  return new float3x3(a.x * b, a.y * b, a.z * b);
146  }
147 
148  public static float3x3 operator *(float3x3 a, float s)
149  {
150  return new float3x3(a.x * s, a.y * s, a.z * s);
151  }
152 
153  public static float3x3 operator /(float3x3 a, float s)
154  {
155  float t = 1f / s;
156  return new float3x3(a.x * t, a.y * t, a.z * t);
157  }
158 
159  public static float3x3 operator +(float3x3 a, float3x3 b)
160  {
161  return new float3x3(a.x + b.x, a.y + b.y, a.z + b.z);
162  }
163 
164  public static float3x3 operator -(float3x3 a, float3x3 b)
165  {
166  return new float3x3(a.x - b.x, a.y - b.y, a.z - b.z);
167  }
168 
169  public static float Determinant(float3x3 m)
170  {
171  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;
172  }
173 
174  public static float3x3 Inverse(float3x3 a)
175  {
176  float3x3 b = new float3x3();
177  float d = Determinant(a);
178  Debug.Assert(d != 0);
179  for (int i = 0; i < 3; i++)
180  {
181  for (int j = 0; j < 3; j++)
182  {
183  int i1 = (i + 1) % 3;
184  int i2 = (i + 2) % 3;
185  int j1 = (j + 1) % 3;
186  int j2 = (j + 2) % 3;
187 
188  // reverse indexs i&j to take transpose
189  b[i, j] = (a[i1][j1] * a[i2][j2] - a[i1][j2] * a[i2][j1]) / d;
190  }
191  }
192  return b;
193  }
194  }
195 }
float3x3(float xx, float xy, float xz, float yx, float yy, float yz, float zx, float zy, float zz)
Definition: float3x3.cs:44