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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
/* 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;
namespace OpenSim.Region.PhysicsModule.ConvexDecompositionDotNet
{
public class Quaternion : float4
{
public Quaternion()
{
x = y = z = 0.0f;
w = 1.0f;
}
public Quaternion(float3 v, float t)
{
v = float3.normalize(v);
w = (float)Math.Cos(t / 2.0f);
v = v * (float)Math.Sin(t / 2.0f);
x = v.x;
y = v.y;
z = v.z;
}
public Quaternion(float _x, float _y, float _z, float _w)
{
x = _x;
y = _y;
z = _z;
w = _w;
}
public float angle()
{
return (float)Math.Acos(w) * 2.0f;
}
public float3 axis()
{
float3 a = new float3(x, y, z);
if (Math.Abs(angle()) < 0.0000001f)
return new float3(1f, 0f, 0f);
return a * (1 / (float)Math.Sin(angle() / 2.0f));
}
public float3 xdir()
{
return new float3(1 - 2 * (y * y + z * z), 2 * (x * y + w * z), 2 * (x * z - w * y));
}
public float3 ydir()
{
return new float3(2 * (x * y - w * z), 1 - 2 * (x * x + z * z), 2 * (y * z + w * x));
}
public float3 zdir()
{
return new float3(2 * (x * z + w * y), 2 * (y * z - w * x), 1 - 2 * (x * x + y * y));
}
public float3x3 getmatrix()
{
return new float3x3(xdir(), ydir(), zdir());
}
public static implicit operator float3x3(Quaternion q)
{
return q.getmatrix();
}
public static Quaternion operator *(Quaternion a, Quaternion b)
{
Quaternion c = new Quaternion();
c.w = a.w * b.w - a.x * b.x - a.y * b.y - a.z * b.z;
c.x = a.w * b.x + a.x * b.w + a.y * b.z - a.z * b.y;
c.y = a.w * b.y - a.x * b.z + a.y * b.w + a.z * b.x;
c.z = a.w * b.z + a.x * b.y - a.y * b.x + a.z * b.w;
return c;
}
public static float3 operator *(Quaternion q, float3 v)
{
// The following is equivalent to:
//return (q.getmatrix() * v);
float qx2 = q.x * q.x;
float qy2 = q.y * q.y;
float qz2 = q.z * q.z;
float qxqy = q.x * q.y;
float qxqz = q.x * q.z;
float qxqw = q.x * q.w;
float qyqz = q.y * q.z;
float qyqw = q.y * q.w;
float qzqw = q.z * q.w;
return new float3((1 - 2 * (qy2 + qz2)) * v.x + (2 * (qxqy - qzqw)) * v.y + (2 * (qxqz + qyqw)) * v.z, (2 * (qxqy + qzqw)) * v.x + (1 - 2 * (qx2 + qz2)) * v.y + (2 * (qyqz - qxqw)) * v.z, (2 * (qxqz - qyqw)) * v.x + (2 * (qyqz + qxqw)) * v.y + (1 - 2 * (qx2 + qy2)) * v.z);
}
public static Quaternion operator +(Quaternion a, Quaternion b)
{
return new Quaternion(a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w);
}
public static Quaternion operator *(Quaternion a, float b)
{
return new Quaternion(a.x *b, a.y *b, a.z *b, a.w *b);
}
public static Quaternion normalize(Quaternion a)
{
float m = (float)Math.Sqrt(a.w * a.w + a.x * a.x + a.y * a.y + a.z * a.z);
if (m < 0.000000001f)
{
a.w = 1;
a.x = a.y = a.z = 0;
return a;
}
return a * (1f / m);
}
public static float dot(Quaternion a, Quaternion b)
{
return (a.w * b.w + a.x * b.x + a.y * b.y + a.z * b.z);
}
public static Quaternion slerp(Quaternion a, Quaternion b, float interp)
{
if (dot(a, b) < 0.0)
{
a.w = -a.w;
a.x = -a.x;
a.y = -a.y;
a.z = -a.z;
}
float d = dot(a, b);
if (d >= 1.0)
{
return a;
}
float theta = (float)Math.Acos(d);
if (theta == 0.0f)
{
return (a);
}
return a * ((float)Math.Sin(theta - interp * theta) / (float)Math.Sin(theta)) + b * ((float)Math.Sin(interp * theta) / (float)Math.Sin(theta));
}
public static Quaternion Interpolate(Quaternion q0, Quaternion q1, float alpha)
{
return slerp(q0, q1, alpha);
}
public static Quaternion Inverse(Quaternion q)
{
return new Quaternion(-q.x, -q.y, -q.z, q.w);
}
public static Quaternion YawPitchRoll(float yaw, float pitch, float roll)
{
roll *= (3.14159264f / 180.0f);
yaw *= (3.14159264f / 180.0f);
pitch *= (3.14159264f / 180.0f);
return new Quaternion(new float3(0.0f, 0.0f, 1.0f), yaw) * new Quaternion(new float3(1.0f, 0.0f, 0.0f), pitch) * new Quaternion(new float3(0.0f, 1.0f, 0.0f), roll);
}
public static float Yaw(Quaternion q)
{
float3 v = q.ydir();
return (v.y == 0.0 && v.x == 0.0) ? 0.0f : (float)Math.Atan2(-v.x, v.y) * (180.0f / 3.14159264f);
}
public static float Pitch(Quaternion q)
{
float3 v = q.ydir();
return (float)Math.Atan2(v.z, Math.Sqrt(v.x * v.x + v.y * v.y)) * (180.0f / 3.14159264f);
}
public static float Roll(Quaternion q)
{
q = new Quaternion(new float3(0.0f, 0.0f, 1.0f), -Yaw(q) * (3.14159264f / 180.0f)) * q;
q = new Quaternion(new float3(1.0f, 0.0f, 0.0f), -Pitch(q) * (3.14159264f / 180.0f)) * q;
return (float)Math.Atan2(-q.xdir().z, q.xdir().x) * (180.0f / 3.14159264f);
}
}
}
|