Tanoda
Matrix.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
23 public struct Matrix {
24
28 public static Matrix operator *(Matrix m1, Matrix m2) {
29 return m1._operator_mul(m2);
30 }
31
35 public float[] ToArray3x3(float[] output) {
36 output[0] = xBasis.x;
37 output[1] = xBasis.y;
38 output[2] = xBasis.z;
39 output[3] = yBasis.x;
40 output[4] = yBasis.y;
41 output[5] = yBasis.z;
42 output[6] = zBasis.x;
43 output[7] = zBasis.y;
44 output[8] = zBasis.z;
45 return output;
46 }
47
51 public double[] ToArray3x3(double[] output) {
52 output[0] = xBasis.x;
53 output[1] = xBasis.y;
54 output[2] = xBasis.z;
55 output[3] = yBasis.x;
56 output[4] = yBasis.y;
57 output[5] = yBasis.z;
58 output[6] = zBasis.x;
59 output[7] = zBasis.y;
60 output[8] = zBasis.z;
61 return output;
62 }
63
67 public float[] ToArray3x3() {
68 return ToArray3x3(new float[9]);
69 }
70
74 public float[] ToArray4x4(float[] output) {
75 output[0] = xBasis.x;
76 output[1] = xBasis.y;
77 output[2] = xBasis.z;
78 output[3] = 0.0f;
79 output[4] = yBasis.x;
80 output[5] = yBasis.y;
81 output[6] = yBasis.z;
82 output[7] = 0.0f;
83 output[8] = zBasis.x;
84 output[9] = zBasis.y;
85 output[10] = zBasis.z;
86 output[11] = 0.0f;
87 output[12] = origin.x;
88 output[13] = origin.y;
89 output[14] = origin.z;
90 output[15] = 1.0f;
91 return output;
92 }
93
97 public double[] ToArray4x4(double[] output) {
98 output[0] = xBasis.x;
99 output[1] = xBasis.y;
100 output[2] = xBasis.z;
101 output[3] = 0.0f;
102 output[4] = yBasis.x;
103 output[5] = yBasis.y;
104 output[6] = yBasis.z;
105 output[7] = 0.0f;
106 output[8] = zBasis.x;
107 output[9] = zBasis.y;
108 output[10] = zBasis.z;
109 output[11] = 0.0f;
110 output[12] = origin.x;
111 output[13] = origin.y;
112 output[14] = origin.z;
113 output[15] = 1.0f;
114 return output;
115 }
116
120 public float[] ToArray4x4() {
121 return ToArray4x4(new float[16]);
122 }
123
128 public Matrix(Matrix other) :
129 this() {
130 xBasis = other.xBasis;
131 yBasis = other.yBasis;
132 zBasis = other.zBasis;
133 origin = other.origin;
134 }
135
141 this() {
142 this.xBasis = xBasis;
143 this.yBasis = yBasis;
144 this.zBasis = zBasis;
145 this.origin = Vector.Zero;
146 }
147
153 this() {
154 this.xBasis = xBasis;
155 this.yBasis = yBasis;
156 this.zBasis = zBasis;
157 this.origin = origin;
158 }
159
164 public Matrix(Vector axis, float angleRadians) :
165 this() {
170 SetRotation(axis, angleRadians);
171 }
172
178 public Matrix(Vector axis, float angleRadians, Vector translation) :
179 this() {
183 origin = translation;
184 this.SetRotation(axis, angleRadians);
185 }
186
187 public Matrix(float m00,
188 float m01,
189 float m02,
190 float m10,
191 float m11,
192 float m12,
193 float m20,
194 float m21,
195 float m22) :
196 this() {
197 xBasis = new Vector(m00, m01, m02);
198 yBasis = new Vector(m10, m11, m12);
199 zBasis = new Vector(m20, m21, m22);
201 }
202
203 public Matrix(float m00,
204 float m01,
205 float m02,
206 float m10,
207 float m11,
208 float m12,
209 float m20,
210 float m21,
211 float m22,
212 float m30,
213 float m31,
214 float m32) :
215 this() {
216 xBasis = new Vector(m00, m01, m02);
217 yBasis = new Vector(m10, m11, m12);
218 zBasis = new Vector(m20, m21, m22);
219 origin = new Vector(m30, m31, m32);
220 }
221
230 public void SetRotation(Vector axis, float angleRadians) {
231 Vector n = axis.Normalized;
232 float s = (float)Math.Sin(angleRadians);
233 float c = (float)Math.Cos(angleRadians);
234 float C = (1 - c);
235
236 xBasis = new Vector(n[0] * n[0] * C + c, n[0] * n[1] * C - n[2] * s, n[0] * n[2] * C + n[1] * s);
237 yBasis = new Vector(n[1] * n[0] * C + n[2] * s, n[1] * n[1] * C + c, n[1] * n[2] * C - n[0] * s);
238 zBasis = new Vector(n[2] * n[0] * C - n[1] * s, n[2] * n[1] * C + n[0] * s, n[2] * n[2] * C + c);
239 }
240
250 return xBasis * point.x + yBasis * point.y + zBasis * point.z + origin;
251 }
252
258 public Vector TransformDirection(Vector direction) {
259 return xBasis * direction.x + yBasis * direction.y + zBasis * direction.z;
260 }
261
272 Matrix rotInverse = new Matrix(new Vector(xBasis[0], yBasis[0], zBasis[0]),
273 new Vector(xBasis[1], yBasis[1], zBasis[1]),
274 new Vector(xBasis[2], yBasis[2], zBasis[2]));
275 rotInverse.origin = rotInverse.TransformDirection(-origin);
276 return rotInverse;
277 }
278
284 private Matrix _operator_mul(Matrix other) {
285 return new Matrix(TransformDirection(other.xBasis),
288 TransformPoint(other.origin));
289 }
290
295 public bool Equals(Matrix other) {
296 return xBasis == other.xBasis &&
297 yBasis == other.yBasis &&
298 zBasis == other.zBasis &&
299 origin == other.origin;
300 }
301
305 public override string ToString() {
306 return string.Format("xBasis: {0} yBasis: {1} zBasis: {2} origin: {3}", xBasis, yBasis, zBasis, origin);
307 }
308
313 public Vector xBasis { get; set; }
314
319 public Vector yBasis { get; set; }
320
325 public Vector zBasis { get; set; }
326
331 public Vector origin { get; set; }
332
337 public static readonly Matrix Identity = new Matrix(Vector.XAxis, Vector.YAxis, Vector.ZAxis, Vector.Zero);
338 }
339}
Es.InkPainter.Math Math
Definition: PaintTest.cs:7
The Matrix struct represents a transformation matrix.
Definition: Matrix.cs:23
Matrix(Matrix other)
Constructs a copy of the specified Matrix object.
Definition: Matrix.cs:128
Vector yBasis
The basis vector for the y-axis.
Definition: Matrix.cs:319
void SetRotation(Vector axis, float angleRadians)
Sets this transformation matrix to represent a rotation around the specified vector.
Definition: Matrix.cs:230
Vector zBasis
The basis vector for the z-axis.
Definition: Matrix.cs:325
bool Equals(Matrix other)
Compare Matrix equality component-wise.
Definition: Matrix.cs:295
static Matrix operator*(Matrix m1, Matrix m2)
Multiply two matrices.
Definition: Matrix.cs:28
Matrix(Vector xBasis, Vector yBasis, Vector zBasis)
Constructs a transformation matrix from the specified basis vectors.
Definition: Matrix.cs:140
static readonly Matrix Identity
Returns the identity matrix specifying no translation, rotation, and scale.
Definition: Matrix.cs:337
Vector xBasis
The basis vector for the x-axis.
Definition: Matrix.cs:313
float[] ToArray4x4(float[] output)
Copy this matrix to the specified array of 16 float values in row-major order.
Definition: Matrix.cs:74
Matrix RigidInverse()
Performs a matrix inverse if the matrix consists entirely of rigid transformations (translations and ...
Definition: Matrix.cs:271
Vector TransformDirection(Vector direction)
Transforms a vector with this matrix by transforming its rotation and scale only.
Definition: Matrix.cs:258
Vector TransformPoint(Vector point)
Transforms a vector with this matrix by transforming its rotation, scale, and translation.
Definition: Matrix.cs:249
Matrix(Vector xBasis, Vector yBasis, Vector zBasis, Vector origin)
Constructs a transformation matrix from the specified basis and translation vectors.
Definition: Matrix.cs:152
float[] ToArray3x3(float[] output)
Copy this matrix to the specified array of 9 float values in row-major order.
Definition: Matrix.cs:35
Vector origin
The translation factors for all three axes.
Definition: Matrix.cs:331
Matrix(Vector axis, float angleRadians, Vector translation)
Constructs a transformation matrix specifying a rotation around the specified vector and a translatio...
Definition: Matrix.cs:178
double[] ToArray3x3(double[] output)
Copy this matrix to the specified array containing 9 double values in row-major order.
Definition: Matrix.cs:51
Matrix(float m00, float m01, float m02, float m10, float m11, float m12, float m20, float m21, float m22, float m30, float m31, float m32)
Definition: Matrix.cs:203
Matrix(float m00, float m01, float m02, float m10, float m11, float m12, float m20, float m21, float m22)
Definition: Matrix.cs:187
float[] ToArray4x4()
Convert this matrix to an array containing 16 float values in row-major order.
Definition: Matrix.cs:120
float[] ToArray3x3()
Convert this matrix to an array containing 9 float values in row-major order.
Definition: Matrix.cs:67
double[] ToArray4x4(double[] output)
Copy this matrix to the specified array of 16 double values in row-major order.
Definition: Matrix.cs:97
override string ToString()
Write the matrix to a string in a human readable format.
Definition: Matrix.cs:305
Matrix(Vector axis, float angleRadians)
Constructs a transformation matrix specifying a rotation around the specified vector.
Definition: Matrix.cs:164
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
Vector Normalized
A normalized copy of this vector.
Definition: Vector.cs:299
static readonly Vector ZAxis
The z-axis unit vector: (0, 0, 1)
Definition: Vector.cs:333
float y
Definition: Vector.cs:219
float x
Definition: Vector.cs:218
static readonly Vector YAxis
The y-axis unit vector: (0, 1, 0)
Definition: Vector.cs:328
static readonly Vector XAxis
The x-axis unit vector: (1, 0, 0)
Definition: Vector.cs:323
float z
Definition: Vector.cs:220