aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/Physics/ConvexDecompositionDotNet/float3x3.cs
blob: 76cf06347a20c3bccb569687916763d8d16c2de6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/* The MIT License
 * 
 * Copyright (c) 2010 Intel Corporation.
 * All rights reserved.
 *
 * Based on the convexdecomposition library from 
 * <http://codesuppository.googlecode.com> by John W. Ratcliff and Stan Melax.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

using System;
using System.Collections.Generic;
using System.Diagnostics;

namespace OpenSim.Region.Physics.ConvexDecompositionDotNet
{
    public class float3x3
    {
        public float3 x = new float3();
        public float3 y = new float3();
        public float3 z = new float3();

        public float3x3()
        {
        }

        public float3x3(float xx, float xy, float xz, float yx, float yy, float yz, float zx, float zy, float zz)
        {
            x = new float3(xx, xy, xz);
            y = new float3(yx, yy, yz);
            z = new float3(zx, zy, zz);
        }

        public float3x3(float3 _x, float3 _y, float3 _z)
        {
            x = new float3(_x);
            y = new float3(_y);
            z = new float3(_z);
        }

        public float3 this[int i]
        {
            get
            {
                switch (i)
                {
                    case 0: return x;
                    case 1: return y;
                    case 2: return z;
                }
                throw new ArgumentOutOfRangeException();
            }
        }

        public float this[int i, int j]
        {
            get
            {
                switch (i)
                {
                    case 0:
                        switch (j)
                        {
                            case 0: return x.x;
                            case 1: return x.y;
                            case 2: return x.z;
                        }
                        break;
                    case 1:
                        switch (j)
                        {
                            case 0: return y.x;
                            case 1: return y.y;
                            case 2: return y.z;
                        }
                        break;
                    case 2:
                        switch (j)
                        {
                            case 0: return z.x;
                            case 1: return z.y;
                            case 2: return z.z;
                        }
                        break;
                }
                throw new ArgumentOutOfRangeException();
            }
            set
            {
                switch (i)
                {
                    case 0:
                        switch (j)
                        {
                            case 0: x.x = value; return;
                            case 1: x.y = value; return;
                            case 2: x.z = value; return;
                        }
                        break;
                    case 1:
                        switch (j)
                        {
                            case 0: y.x = value; return;
                            case 1: y.y = value; return;
                            case 2: y.z = value; return;
                        }
                        break;
                    case 2:
                        switch (j)
                        {
                            case 0: z.x = value; return;
                            case 1: z.y = value; return;
                            case 2: z.z = value; return;
                        }
                        break;
                }
                throw new ArgumentOutOfRangeException();
            }
        }

        public static float3x3 Transpose(float3x3 m)
        {
            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));
        }

        public static float3x3 operator *(float3x3 a, float3x3 b)
        {
            return new float3x3(a.x * b, a.y * b, a.z * b);
        }

        public static float3x3 operator *(float3x3 a, float s)
        {
            return new float3x3(a.x * s, a.y * s, a.z * s);
        }

        public static float3x3 operator /(float3x3 a, float s)
        {
            float t = 1f / s;
            return new float3x3(a.x * t, a.y * t, a.z * t);
        }

        public static float3x3 operator +(float3x3 a, float3x3 b)
        {
            return new float3x3(a.x + b.x, a.y + b.y, a.z + b.z);
        }

        public static float3x3 operator -(float3x3 a, float3x3 b)
        {
            return new float3x3(a.x - b.x, a.y - b.y, a.z - b.z);
        }

        public static float Determinant(float3x3 m)
        {
            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;
        }

        public static float3x3 Inverse(float3x3 a)
        {
            float3x3 b = new float3x3();
            float d = Determinant(a);
            Debug.Assert(d != 0);
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    int i1 = (i + 1) % 3;
                    int i2 = (i + 2) % 3;
                    int j1 = (j + 1) % 3;
                    int j2 = (j + 2) % 3;

                    // reverse indexs i&j to take transpose
                    b[i, j] = (a[i1][j1] * a[i2][j2] - a[i1][j2] * a[i2][j1]) / d;
                }
            }
            return b;
        }
    }
}