Tanoda
LeapTransform.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9namespace Leap {
10 using System;
11
18 public struct LeapTransform {
24 this(translation, rotation, Vector.Ones) {
25 }
26
32 this() {
33 _scale = scale;
34 // these are non-trival setters.
35 this.translation = translation;
36 this.rotation = rotation; // Calls validateBasis
37 }
38
44 return _xBasisScaled * point.x + _yBasisScaled * point.y + _zBasisScaled * point.z + translation;
45 }
46
51 public Vector TransformDirection(Vector direction) {
52 return _xBasis * direction.x + _yBasis * direction.y + _zBasis * direction.z;
53 }
54
59 public Vector TransformVelocity(Vector velocity) {
60 return _xBasisScaled * velocity.x + _yBasisScaled * velocity.y + _zBasisScaled * velocity.z;
61 }
62
75 if (_quaternionDirty)
76 throw new InvalidOperationException("Calling TransformQuaternion after Basis vectors have been modified.");
77
78 if (_flip) {
79 // Mirror the axis of rotation across the flip axis.
80 rhs.x *= _flipAxes.x;
81 rhs.y *= _flipAxes.y;
82 rhs.z *= _flipAxes.z;
83 }
84
85 LeapQuaternion t = _quaternion.Multiply(rhs);
86 return t;
87 }
88
93 public void MirrorX() {
94 _xBasis = -_xBasis;
95 _xBasisScaled = -_xBasisScaled;
96
97 _flip = true;
98 _flipAxes.y = -_flipAxes.y;
99 _flipAxes.z = -_flipAxes.z;
100 }
101
106 public void MirrorZ() {
107 _zBasis = -_zBasis;
108 _zBasisScaled = -_zBasisScaled;
109
110 _flip = true;
111 _flipAxes.x = -_flipAxes.x;
112 _flipAxes.y = -_flipAxes.y;
113 }
114
124 public Vector xBasis {
125 get { return _xBasis; }
126 set {
127 _xBasis = value;
128 _xBasisScaled = value * scale.x;
129 _quaternionDirty = true;
130 }
131 }
132
142 public Vector yBasis {
143 get { return _yBasis; }
144 set {
145 _yBasis = value;
146 _yBasisScaled = value * scale.y;
147 _quaternionDirty = true;
148 }
149 }
150
160 public Vector zBasis {
161 get { return _zBasis; }
162 set {
163 _zBasis = value;
164 _zBasisScaled = value * scale.z;
165 _quaternionDirty = true;
166 }
167 }
168
174 get { return _translation; }
175 set {
176 _translation = value;
177 }
178 }
179
185 public Vector scale {
186 get { return _scale; }
187 set {
188 _scale = value;
189 _xBasisScaled = _xBasis * scale.x;
190 _yBasisScaled = _yBasis * scale.y;
191 _zBasisScaled = _zBasis * scale.z;
192 }
193 }
194
205 get {
206 if (_quaternionDirty)
207 throw new InvalidOperationException("Requesting rotation after Basis vectors have been modified.");
208 return _quaternion;
209 }
210 set {
211 _quaternion = value;
212
213 float d = value.MagnitudeSquared;
214 float s = 2.0f / d;
215 float xs = value.x * s, ys = value.y * s, zs = value.z * s;
216 float wx = value.w * xs, wy = value.w * ys, wz = value.w * zs;
217 float xx = value.x * xs, xy = value.x * ys, xz = value.x * zs;
218 float yy = value.y * ys, yz = value.y * zs, zz = value.z * zs;
219
220 _xBasis = new Vector(1.0f - (yy + zz), xy + wz, xz - wy);
221 _yBasis = new Vector(xy - wz, 1.0f - (xx + zz), yz + wx);
222 _zBasis = new Vector(xz + wy, yz - wx, 1.0f - (xx + yy));
223
224 _xBasisScaled = _xBasis * scale.x;
225 _yBasisScaled = _yBasis * scale.y;
226 _zBasisScaled = _zBasis * scale.z;
227
228 _quaternionDirty = false;
229 _flip = false;
230 _flipAxes = new Vector(1.0f, 1.0f, 1.0f);
231 }
232 }
233
239
240 private Vector _translation;
241 private Vector _scale;
242 private LeapQuaternion _quaternion;
243 private bool _quaternionDirty;
244 private bool _flip;
245 private Vector _flipAxes;
246 private Vector _xBasis;
247 private Vector _yBasis;
248 private Vector _zBasis;
249 private Vector _xBasisScaled;
250 private Vector _yBasisScaled;
251 private Vector _zBasisScaled;
252 }
253}
The LeapQuaternion struct represents a rotation in three-dimensional space.
LeapQuaternion Multiply(LeapQuaternion rhs)
Concatenates the rotation described by this quaternion with the one provided and returns the result.
float MagnitudeSquared
The square of the magnitude, or length, of this quaternion.
static readonly LeapQuaternion Identity
The identity quaternion.
The LeapTransform class represents a transform in three dimensional space.
Vector TransformDirection(Vector direction)
Transforms the specified direction vector, applying rotation only.
Vector TransformPoint(Vector point)
Transforms the specified position vector, applying translation, rotation and scale.
static readonly LeapTransform Identity
The identity transform.
void MirrorX()
Mirrors this transform's rotation and scale across the x-axis. Translation is not affected.
LeapTransform(Vector translation, LeapQuaternion rotation)
Constructs a new transform from the specified translation and rotation.
Vector TransformVelocity(Vector velocity)
Transforms the specified velocity vector, applying rotation and scale.
Vector zBasis
The z-basis of the transform.
LeapQuaternion rotation
The rotational component of the transform.
void MirrorZ()
Mirrors this transform's rotation and scale across the z-axis. Translation is not affected.
Vector xBasis
The x-basis of the transform.
LeapTransform(Vector translation, LeapQuaternion rotation, Vector scale)
Constructs a new transform from the specified translation, rotation and scale.
Vector yBasis
The y-basis of the transform.
Vector translation
The translation component of the transform.
Vector scale
The scale factors of the transform. Scale is kept separate from translation.
LeapQuaternion TransformQuaternion(LeapQuaternion rhs)
Transforms the specified quaternion. Multiplies the quaternion representing the rotational part of th...
The Vector struct represents a three-component mathematical vector or point such as a direction or po...
Definition: Vector.cs:36
static readonly Vector Zero
The zero vector: (0, 0, 0)
Definition: Vector.cs:313
static readonly Vector Ones
The ones vector: (1, 1, 1)
Definition: Vector.cs:318
float y
Definition: Vector.cs:219
float x
Definition: Vector.cs:218
float z
Definition: Vector.cs:220