Spelunx Cavern SDK
 
Loading...
Searching...
No Matches
OVRT_Utils.cs
Go to the documentation of this file.
1using UnityEngine;
2using Valve.VR;
3
4namespace Spelunx.Vive
5{
6 public static class OVRT_Utils
7 {
8 [System.Serializable]
9 public struct RigidTransform
10 {
11 public Vector3 pos;
12 public Quaternion rot;
13
15 {
16 get { return new RigidTransform(Vector3.zero, Quaternion.identity); }
17 }
18
19 public static RigidTransform FromLocal(Transform t)
20 {
21 return new RigidTransform(t.localPosition, t.localRotation);
22 }
23
24 public RigidTransform(Vector3 pos, Quaternion rot)
25 {
26 this.pos = pos;
27 this.rot = rot;
28 }
29
30 public RigidTransform(Transform t)
31 {
32 this.pos = t.position;
33 this.rot = t.rotation;
34 }
35
36 public RigidTransform(Transform from, Transform to)
37 {
38 var inv = Quaternion.Inverse(from.rotation);
39 rot = inv * to.rotation;
40 pos = inv * (to.position - from.position);
41 }
42
43 public RigidTransform(HmdMatrix34_t pose)
44 {
45 var m = Matrix4x4.identity;
46
47 m[0, 0] = pose.m0;
48 m[0, 1] = pose.m1;
49 m[0, 2] = -pose.m2;
50 m[0, 3] = pose.m3;
51
52 m[1, 0] = pose.m4;
53 m[1, 1] = pose.m5;
54 m[1, 2] = -pose.m6;
55 m[1, 3] = pose.m7;
56
57 m[2, 0] = -pose.m8;
58 m[2, 1] = -pose.m9;
59 m[2, 2] = pose.m10;
60 m[2, 3] = -pose.m11;
61
62 this.pos = m.GetPosition();
63 this.rot = m.GetRotation();
64 }
65
66 public RigidTransform(HmdMatrix44_t pose)
67 {
68 var m = Matrix4x4.identity;
69
70 m[0, 0] = pose.m0;
71 m[0, 1] = pose.m1;
72 m[0, 2] = -pose.m2;
73 m[0, 3] = pose.m3;
74
75 m[1, 0] = pose.m4;
76 m[1, 1] = pose.m5;
77 m[1, 2] = -pose.m6;
78 m[1, 3] = pose.m7;
79
80 m[2, 0] = -pose.m8;
81 m[2, 1] = -pose.m9;
82 m[2, 2] = pose.m10;
83 m[2, 3] = -pose.m11;
84
85 m[3, 0] = pose.m12;
86 m[3, 1] = pose.m13;
87 m[3, 2] = -pose.m14;
88 m[3, 3] = pose.m15;
89
90 this.pos = m.GetPosition();
91 this.rot = m.GetRotation();
92 }
93
94 public HmdMatrix44_t ToHmdMatrix44()
95 {
96 var m = Matrix4x4.TRS(pos, rot, Vector3.one);
97 var pose = new HmdMatrix44_t();
98
99 pose.m0 = m[0, 0];
100 pose.m1 = m[0, 1];
101 pose.m2 = -m[0, 2];
102 pose.m3 = m[0, 3];
103
104 pose.m4 = m[1, 0];
105 pose.m5 = m[1, 1];
106 pose.m6 = -m[1, 2];
107 pose.m7 = m[1, 3];
108
109 pose.m8 = -m[2, 0];
110 pose.m9 = -m[2, 1];
111 pose.m10 = m[2, 2];
112 pose.m11 = -m[2, 3];
113
114 pose.m12 = m[3, 0];
115 pose.m13 = m[3, 1];
116 pose.m14 = -m[3, 2];
117 pose.m15 = m[3, 3];
118
119 return pose;
120 }
121
122 public HmdMatrix34_t ToHmdMatrix34()
123 {
124 var m = Matrix4x4.TRS(pos, rot, Vector3.one);
125 var pose = new HmdMatrix34_t();
126
127 pose.m0 = m[0, 0];
128 pose.m1 = m[0, 1];
129 pose.m2 = -m[0, 2];
130 pose.m3 = m[0, 3];
131
132 pose.m4 = m[1, 0];
133 pose.m5 = m[1, 1];
134 pose.m6 = -m[1, 2];
135 pose.m7 = m[1, 3];
136
137 pose.m8 = -m[2, 0];
138 pose.m9 = -m[2, 1];
139 pose.m10 = m[2, 2];
140 pose.m11 = -m[2, 3];
141
142 return pose;
143 }
144
145 public override bool Equals(object o)
146 {
147 if (o is RigidTransform)
148 {
150 return pos == t.pos && rot == t.rot;
151 }
152 return false;
153 }
154
155
156
157 public override int GetHashCode()
158 {
159 return pos.GetHashCode() ^ rot.GetHashCode();
160 }
161
163 {
164 return a.pos == b.pos && a.rot == b.rot;
165 }
166
168 {
169 return a.pos != b.pos || a.rot != b.rot;
170 }
171
173 {
174 return new RigidTransform
175 {
176 rot = a.rot * b.rot,
177 pos = a.pos + a.rot * b.pos
178 };
179 }
180
181 public void Inverse()
182 {
183 rot = Quaternion.Inverse(rot);
184 pos = -(rot * pos);
185 }
186
188 {
189 var t = new RigidTransform(pos, rot);
190 t.Inverse();
191 return t;
192 }
193
195 {
196 rot = a.rot * b.rot;
197 pos = a.pos + a.rot * b.pos;
198 }
199
200 public Vector3 InverseTransformPoint(Vector3 point)
201 {
202 return Quaternion.Inverse(rot) * (point - pos);
203 }
204
205 public Vector3 TransformPoint(Vector3 point)
206 {
207 return pos + (rot * point);
208 }
209
210 public static Vector3 operator *(RigidTransform t, Vector3 v)
211 {
212 return t.TransformPoint(v);
213 }
214
216 {
217 return new RigidTransform(Vector3.Lerp(a.pos, b.pos, t), Quaternion.Slerp(a.rot, b.rot, t));
218 }
219
220 public void Interpolate(RigidTransform to, float t)
221 {
222 pos = OVRT_Utils.Lerp(pos, to.pos, t);
223 rot = OVRT_Utils.Slerp(rot, to.rot, t);
224 }
225 }
226
227 // this version does not clamp [0..1]
228 public static Quaternion Slerp(Quaternion A, Quaternion B, float t)
229 {
230 var cosom = Mathf.Clamp(A.x * B.x + A.y * B.y + A.z * B.z + A.w * B.w, -1.0f, 1.0f);
231 if (cosom < 0.0f)
232 {
233 B = new Quaternion(-B.x, -B.y, -B.z, -B.w);
234 cosom = -cosom;
235 }
236
237 float sclp, sclq;
238 if ((1.0f - cosom) > 0.0001f)
239 {
240 var omega = Mathf.Acos(cosom);
241 var sinom = Mathf.Sin(omega);
242 sclp = Mathf.Sin((1.0f - t) * omega) / sinom;
243 sclq = Mathf.Sin(t * omega) / sinom;
244 }
245 else
246 {
247 // "from" and "to" very close, so do linear interp
248 sclp = 1.0f - t;
249 sclq = t;
250 }
251
252 return new Quaternion(
253 sclp * A.x + sclq * B.x,
254 sclp * A.y + sclq * B.y,
255 sclp * A.z + sclq * B.z,
256 sclp * A.w + sclq * B.w);
257 }
258
259 public static Vector3 Lerp(Vector3 A, Vector3 B, float t)
260 {
261 return new Vector3(
262 Lerp(A.x, B.x, t),
263 Lerp(A.y, B.y, t),
264 Lerp(A.z, B.z, t));
265 }
266
267 public static float Lerp(float A, float B, float t)
268 {
269 return A + (B - A) * t;
270 }
271
272 public static double Lerp(double A, double B, double t)
273 {
274 return A + (B - A) * t;
275 }
276
277 private static float _copysign(float sizeval, float signval)
278 {
279 return Mathf.Sign(signval) == 1 ? Mathf.Abs(sizeval) : -Mathf.Abs(sizeval);
280 }
281
282 public static Quaternion GetRotation(this Matrix4x4 matrix)
283 {
284 Quaternion q = new Quaternion();
285 q.w = Mathf.Sqrt(Mathf.Max(0, 1 + matrix.m00 + matrix.m11 + matrix.m22)) / 2;
286 q.x = Mathf.Sqrt(Mathf.Max(0, 1 + matrix.m00 - matrix.m11 - matrix.m22)) / 2;
287 q.y = Mathf.Sqrt(Mathf.Max(0, 1 - matrix.m00 + matrix.m11 - matrix.m22)) / 2;
288 q.z = Mathf.Sqrt(Mathf.Max(0, 1 - matrix.m00 - matrix.m11 + matrix.m22)) / 2;
289 q.x = _copysign(q.x, matrix.m21 - matrix.m12);
290 q.y = _copysign(q.y, matrix.m02 - matrix.m20);
291 q.z = _copysign(q.z, matrix.m10 - matrix.m01);
292 return q;
293 }
294
295 public static Vector3 GetPosition(this Matrix4x4 matrix)
296 {
297 var x = matrix.m03;
298 var y = matrix.m13;
299 var z = matrix.m23;
300
301 return new Vector3(x, y, z);
302 }
303
304 public static Vector2 ToVector2(this VRControllerAxis_t axis)
305 {
306 return new Vector2(axis.x, axis.y);
307 }
308 }
309}
static Quaternion GetRotation(this Matrix4x4 matrix)
Definition: OVRT_Utils.cs:282
static Quaternion Slerp(Quaternion A, Quaternion B, float t)
Definition: OVRT_Utils.cs:228
static Vector3 Lerp(Vector3 A, Vector3 B, float t)
Definition: OVRT_Utils.cs:259
static float Lerp(float A, float B, float t)
Definition: OVRT_Utils.cs:267
static double Lerp(double A, double B, double t)
Definition: OVRT_Utils.cs:272
static Vector3 GetPosition(this Matrix4x4 matrix)
Definition: OVRT_Utils.cs:295
static Vector2 ToVector2(this VRControllerAxis_t axis)
Definition: OVRT_Utils.cs:304
override bool Equals(object o)
Definition: OVRT_Utils.cs:145
static RigidTransform identity
Definition: OVRT_Utils.cs:15
RigidTransform(Vector3 pos, Quaternion rot)
Definition: OVRT_Utils.cs:24
RigidTransform(HmdMatrix34_t pose)
Definition: OVRT_Utils.cs:43
static RigidTransform operator*(RigidTransform a, RigidTransform b)
Definition: OVRT_Utils.cs:172
Vector3 TransformPoint(Vector3 point)
Definition: OVRT_Utils.cs:205
RigidTransform(HmdMatrix44_t pose)
Definition: OVRT_Utils.cs:66
static bool operator!=(RigidTransform a, RigidTransform b)
Definition: OVRT_Utils.cs:167
void Multiply(RigidTransform a, RigidTransform b)
Definition: OVRT_Utils.cs:194
static RigidTransform Interpolate(RigidTransform a, RigidTransform b, float t)
Definition: OVRT_Utils.cs:215
void Interpolate(RigidTransform to, float t)
Definition: OVRT_Utils.cs:220
Vector3 InverseTransformPoint(Vector3 point)
Definition: OVRT_Utils.cs:200
RigidTransform(Transform from, Transform to)
Definition: OVRT_Utils.cs:36
static bool operator==(RigidTransform a, RigidTransform b)
Definition: OVRT_Utils.cs:162
static RigidTransform FromLocal(Transform t)
Definition: OVRT_Utils.cs:19