leap-motion 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,1036 @@
1
+ /******************************************************************************\
2
+ * Copyright (C) 2012-2013 Leap Motion, Inc. All rights reserved. *
3
+ * Leap Motion proprietary and confidential. Not for distribution. *
4
+ * Use subject to the terms of the Leap Motion SDK Agreement available at *
5
+ * https://developer.leapmotion.com/sdk_agreement, or another agreement *
6
+ * between Leap Motion and you, your company or other organization. *
7
+ \******************************************************************************/
8
+
9
+ #if !defined(__LeapMath_h__)
10
+ #define __LeapMath_h__
11
+
12
+ #include <cmath>
13
+ #include <iostream>
14
+ #include <sstream>
15
+ #include <float.h>
16
+
17
+ namespace Leap {
18
+
19
+ /**
20
+ * The constant pi as a single precision floating point number.
21
+ * @since 1.0
22
+ */
23
+ static const float PI = 3.1415926536f;
24
+ /**
25
+ * The constant ratio to convert an angle measure from degrees to radians.
26
+ * Multiply a value in degrees by this constant to convert to radians.
27
+ * @since 1.0
28
+ */
29
+ static const float DEG_TO_RAD = 0.0174532925f;
30
+ /**
31
+ * The constant ratio to convert an angle measure from radians to degrees.
32
+ * Multiply a value in radians by this constant to convert to degrees.
33
+ * @since 1.0
34
+ */
35
+ static const float RAD_TO_DEG = 57.295779513f;
36
+
37
+ /**
38
+ * The Vector struct represents a three-component mathematical vector or point
39
+ * such as a direction or position in three-dimensional space.
40
+ *
41
+ * The Leap Motion software employs a right-handed Cartesian coordinate system.
42
+ * Values given are in units of real-world millimeters. The origin is centered
43
+ * at the center of the Leap Motion Controller. The x- and z-axes lie in the horizontal
44
+ * plane, with the x-axis running parallel to the long edge of the device.
45
+ * The y-axis is vertical, with positive values increasing upwards (in contrast
46
+ * to the downward orientation of most computer graphics coordinate systems).
47
+ * The z-axis has positive values increasing away from the computer screen.
48
+ *
49
+ * \image html images/Leap_Axes.png
50
+ * @since 1.0
51
+ */
52
+ struct Vector {
53
+ /**
54
+ * Creates a new Vector with all components set to zero.
55
+ * @since 1.0
56
+ */
57
+ Vector() :
58
+ x(0), y(0), z(0) {}
59
+
60
+ /**
61
+ * Creates a new Vector with the specified component values.
62
+ *
63
+ * \include Vector_Constructor_1.txt
64
+ * @since 1.0
65
+ */
66
+ Vector(float _x, float _y, float _z) :
67
+ x(_x), y(_y), z(_z) {}
68
+
69
+ /**
70
+ * Copies the specified Vector.
71
+ *
72
+ * \include Vector_Constructor_2.txt
73
+ * @since 1.0
74
+ */
75
+ Vector(const Vector& vector) :
76
+ x(vector.x), y(vector.y), z(vector.z) {}
77
+
78
+ /**
79
+ * The zero vector: (0, 0, 0)
80
+ *
81
+ * \include Vector_Zero.txt
82
+ * @since 1.0
83
+ */
84
+ static const Vector& zero() {
85
+ static Vector s_zero(0, 0, 0);
86
+ return s_zero;
87
+ }
88
+
89
+ /**
90
+ * The x-axis unit vector: (1, 0, 0)
91
+ *
92
+ * \include Vector_XAxis.txt
93
+ * @since 1.0
94
+ */
95
+ static const Vector& xAxis() {
96
+ static Vector s_xAxis(1, 0, 0);
97
+ return s_xAxis;
98
+ }
99
+ /**
100
+ * The y-axis unit vector: (0, 1, 0)
101
+ *
102
+ * \include Vector_YAxis.txt
103
+ * @since 1.0
104
+ */
105
+ static const Vector& yAxis() {
106
+ static Vector s_yAxis(0, 1, 0);
107
+ return s_yAxis;
108
+ }
109
+ /**
110
+ * The z-axis unit vector: (0, 0, 1)
111
+ *
112
+ * \include Vector_ZAxis.txt
113
+ * @since 1.0
114
+ */
115
+ static const Vector& zAxis() {
116
+ static Vector s_zAxis(0, 0, 1);
117
+ return s_zAxis;
118
+ }
119
+
120
+ /**
121
+ * The unit vector pointing left along the negative x-axis: (-1, 0, 0)
122
+ *
123
+ * \include Vector_Left.txt
124
+ * @since 1.0
125
+ */
126
+ static const Vector& left() {
127
+ static Vector s_left(-1, 0, 0);
128
+ return s_left;
129
+ }
130
+ /**
131
+ * The unit vector pointing right along the positive x-axis: (1, 0, 0)
132
+ *
133
+ * \include Vector_Right.txt
134
+ * @since 1.0
135
+ */
136
+ static const Vector& right() {
137
+ return xAxis();
138
+ }
139
+ /**
140
+ * The unit vector pointing down along the negative y-axis: (0, -1, 0)
141
+ *
142
+ * \include Vector_Down.txt
143
+ * @since 1.0
144
+ */
145
+ static const Vector& down() {
146
+ static Vector s_down(0, -1, 0);
147
+ return s_down;
148
+ }
149
+ /**
150
+ * The unit vector pointing up along the positive y-axis: (0, 1, 0)
151
+ *
152
+ * \include Vector_Up.txt
153
+ * @since 1.0
154
+ */
155
+ static const Vector& up() {
156
+ return yAxis();
157
+ }
158
+ /**
159
+ * The unit vector pointing forward along the negative z-axis: (0, 0, -1)
160
+ *
161
+ * \include Vector_Forward.txt
162
+ * @since 1.0
163
+ */
164
+ static const Vector& forward() {
165
+ static Vector s_forward(0, 0, -1);
166
+ return s_forward;
167
+ }
168
+ /**
169
+ * The unit vector pointing backward along the positive z-axis: (0, 0, 1)
170
+ *
171
+ * \include Vector_Backward.txt
172
+ * @since 1.0
173
+ */
174
+ static const Vector& backward() {
175
+ return zAxis();
176
+ }
177
+
178
+ /**
179
+ * The magnitude, or length, of this vector.
180
+ *
181
+ * The magnitude is the L2 norm, or Euclidean distance between the origin and
182
+ * the point represented by the (x, y, z) components of this Vector object.
183
+ *
184
+ * \include Vector_Magnitude.txt
185
+ *
186
+ * @returns The length of this vector.
187
+ * @since 1.0
188
+ */
189
+ float magnitude() const {
190
+ return std::sqrt(x*x + y*y + z*z);
191
+ }
192
+
193
+ /**
194
+ * The square of the magnitude, or length, of this vector.
195
+ *
196
+ * \include Vector_Magnitude_Squared.txt
197
+ *
198
+ * @returns The square of the length of this vector.
199
+ * @since 1.0
200
+ */
201
+ float magnitudeSquared() const {
202
+ return x*x + y*y + z*z;
203
+ }
204
+
205
+ /**
206
+ * The distance between the point represented by this Vector
207
+ * object and a point represented by the specified Vector object.
208
+ *
209
+ * \include Vector_DistanceTo.txt
210
+ *
211
+ * @param other A Vector object.
212
+ * @returns The distance from this point to the specified point.
213
+ * @since 1.0
214
+ */
215
+ float distanceTo(const Vector& other) const {
216
+ return std::sqrt((x - other.x)*(x - other.x) +
217
+ (y - other.y)*(y - other.y) +
218
+ (z - other.z)*(z - other.z));
219
+ }
220
+
221
+ /**
222
+ * The angle between this vector and the specified vector in radians.
223
+ *
224
+ * The angle is measured in the plane formed by the two vectors. The
225
+ * angle returned is always the smaller of the two conjugate angles.
226
+ * Thus <tt>A.angleTo(B) == B.angleTo(A)</tt> and is always a positive
227
+ * value less than or equal to pi radians (180 degrees).
228
+ *
229
+ * If either vector has zero length, then this function returns zero.
230
+ *
231
+ * \image html images/Math_AngleTo.png
232
+ *
233
+ * \include Vector_AngleTo.txt
234
+ *
235
+ * @param other A Vector object.
236
+ * @returns The angle between this vector and the specified vector in radians.
237
+ * @since 1.0
238
+ */
239
+ float angleTo(const Vector& other) const {
240
+ float denom = this->magnitudeSquared() * other.magnitudeSquared();
241
+ if (denom <= 0.0f) {
242
+ return 0.0f;
243
+ }
244
+ return std::acos(this->dot(other) / std::sqrt(denom));
245
+ }
246
+
247
+ /**
248
+ * The pitch angle in radians.
249
+ *
250
+ * Pitch is the angle between the negative z-axis and the projection of
251
+ * the vector onto the y-z plane. In other words, pitch represents rotation
252
+ * around the x-axis.
253
+ * If the vector points upward, the returned angle is between 0 and pi radians
254
+ * (180 degrees); if it points downward, the angle is between 0 and -pi radians.
255
+ *
256
+ * \image html images/Math_Pitch_Angle.png
257
+ *
258
+ * \include Vector_Pitch.txt
259
+ *
260
+ * @returns The angle of this vector above or below the horizon (x-z plane).
261
+ * @since 1.0
262
+ */
263
+ float pitch() const {
264
+ return std::atan2(y, -z);
265
+ }
266
+
267
+ /**
268
+ * The yaw angle in radians.
269
+ *
270
+ * Yaw is the angle between the negative z-axis and the projection of
271
+ * the vector onto the x-z plane. In other words, yaw represents rotation
272
+ * around the y-axis. If the vector points to the right of the negative z-axis,
273
+ * then the returned angle is between 0 and pi radians (180 degrees);
274
+ * if it points to the left, the angle is between 0 and -pi radians.
275
+ *
276
+ * \image html images/Math_Yaw_Angle.png
277
+ *
278
+ * \include Vector_Yaw.txt
279
+ *
280
+ * @returns The angle of this vector to the right or left of the negative z-axis.
281
+ * @since 1.0
282
+ */
283
+ float yaw() const {
284
+ return std::atan2(x, -z);
285
+ }
286
+
287
+ /**
288
+ * The roll angle in radians.
289
+ *
290
+ * Roll is the angle between the y-axis and the projection of
291
+ * the vector onto the x-y plane. In other words, roll represents rotation
292
+ * around the z-axis. If the vector points to the left of the y-axis,
293
+ * then the returned angle is between 0 and pi radians (180 degrees);
294
+ * if it points to the right, the angle is between 0 and -pi radians.
295
+ *
296
+ * \image html images/Math_Roll_Angle.png
297
+ *
298
+ * Use this function to get roll angle of the plane to which this vector is a
299
+ * normal. For example, if this vector represents the normal to the palm,
300
+ * then this function returns the tilt or roll of the palm plane compared
301
+ * to the horizontal (x-z) plane.
302
+ *
303
+ * \include Vector_Roll.txt
304
+ *
305
+ * @returns The angle of this vector to the right or left of the y-axis.
306
+ * @since 1.0
307
+ */
308
+ float roll() const {
309
+ return std::atan2(x, -y);
310
+ }
311
+
312
+ /**
313
+ * The dot product of this vector with another vector.
314
+ *
315
+ * The dot product is the magnitude of the projection of this vector
316
+ * onto the specified vector.
317
+ *
318
+ * \image html images/Math_Dot.png
319
+ *
320
+ * \include Vector_Dot.txt
321
+ *
322
+ * @param other A Vector object.
323
+ * @returns The dot product of this vector and the specified vector.
324
+ * @since 1.0
325
+ */
326
+ float dot(const Vector& other) const {
327
+ return (x * other.x) + (y * other.y) + (z * other.z);
328
+ }
329
+
330
+ /**
331
+ * The cross product of this vector and the specified vector.
332
+ *
333
+ * The cross product is a vector orthogonal to both original vectors.
334
+ * It has a magnitude equal to the area of a parallelogram having the
335
+ * two vectors as sides. The direction of the returned vector is
336
+ * determined by the right-hand rule. Thus <tt>A.cross(B) == -B.cross(A).</tt>
337
+ *
338
+ * \image html images/Math_Cross.png
339
+ *
340
+ * \include Vector_Cross.txt
341
+ *
342
+ * @param other A Vector object.
343
+ * @returns The cross product of this vector and the specified vector.
344
+ * @since 1.0
345
+ */
346
+ Vector cross(const Vector& other) const {
347
+ return Vector((y * other.z) - (z * other.y),
348
+ (z * other.x) - (x * other.z),
349
+ (x * other.y) - (y * other.x));
350
+ }
351
+
352
+ /**
353
+ * A normalized copy of this vector.
354
+ *
355
+ * A normalized vector has the same direction as the original vector,
356
+ * but with a length of one.
357
+ *
358
+ * \include Vector_Normalized.txt
359
+ *
360
+ * @returns A Vector object with a length of one, pointing in the same
361
+ * direction as this Vector object.
362
+ * @since 1.0
363
+ */
364
+ Vector normalized() const {
365
+ float denom = this->magnitudeSquared();
366
+ if (denom <= 0.0f) {
367
+ return Vector::zero();
368
+ }
369
+ denom = 1.0f / std::sqrt(denom);
370
+ return Vector(x * denom, y * denom, z * denom);
371
+ }
372
+
373
+ /**
374
+ * A copy of this vector pointing in the opposite direction.
375
+ *
376
+ * \include Vector_Negate.txt
377
+ *
378
+ * @returns A Vector object with all components negated.
379
+ * @since 1.0
380
+ */
381
+ Vector operator-() const {
382
+ return Vector(-x, -y, -z);
383
+ }
384
+
385
+ /**
386
+ * Add vectors component-wise.
387
+ *
388
+ * \include Vector_Plus.txt
389
+ * @since 1.0
390
+ */
391
+ Vector operator+(const Vector& other) const {
392
+ return Vector(x + other.x, y + other.y, z + other.z);
393
+ }
394
+
395
+ /**
396
+ * Subtract vectors component-wise.
397
+ *
398
+ * \include Vector_Minus.txt
399
+ * @since 1.0
400
+ */
401
+ Vector operator-(const Vector& other) const {
402
+ return Vector(x - other.x, y - other.y, z - other.z);
403
+ }
404
+
405
+ /**
406
+ * Multiply vector by a scalar.
407
+ *
408
+ * \include Vector_Times.txt
409
+ * @since 1.0
410
+ */
411
+ Vector operator*(float scalar) const {
412
+ return Vector(x * scalar, y * scalar, z * scalar);
413
+ }
414
+
415
+ /**
416
+ * Divide vector by a scalar.
417
+ *
418
+ * \include Vector_Divide.txt
419
+ * @since 1.0
420
+ */
421
+ Vector operator/(float scalar) const {
422
+ return Vector(x / scalar, y / scalar, z / scalar);
423
+ }
424
+
425
+ #if !defined(SWIG)
426
+ /**
427
+ * Multiply vector by a scalar on the left-hand side (C++ only).
428
+ *
429
+ * \include Vector_Left_Times.txt
430
+ * @since 1.0
431
+ */
432
+ friend Vector operator*(float scalar, const Vector& vector) {
433
+ return Vector(vector.x * scalar, vector.y * scalar, vector.z * scalar);
434
+ }
435
+ #endif
436
+
437
+ /**
438
+ * Add vectors component-wise and assign the sum.
439
+ * @since 1.0
440
+ */
441
+ Vector& operator+=(const Vector& other) {
442
+ x += other.x;
443
+ y += other.y;
444
+ z += other.z;
445
+ return *this;
446
+ }
447
+
448
+ /**
449
+ * Subtract vectors component-wise and assign the difference.
450
+ * @since 1.0
451
+ */
452
+ Vector& operator-=(const Vector& other) {
453
+ x -= other.x;
454
+ y -= other.y;
455
+ z -= other.z;
456
+ return *this;
457
+ }
458
+
459
+ /**
460
+ * Multiply vector by a scalar and assign the product.
461
+ * @since 1.0
462
+ */
463
+ Vector& operator*=(float scalar) {
464
+ x *= scalar;
465
+ y *= scalar;
466
+ z *= scalar;
467
+ return *this;
468
+ }
469
+
470
+ /**
471
+ * Divide vector by a scalar and assign the quotient.
472
+ * @since 1.0
473
+ */
474
+ Vector& operator/=(float scalar) {
475
+ x /= scalar;
476
+ y /= scalar;
477
+ z /= scalar;
478
+ return *this;
479
+ }
480
+
481
+ /**
482
+ * Returns a string containing this vector in a human readable format: (x, y, z).
483
+ * @since 1.0
484
+ */
485
+ std::string toString() const {
486
+ std::stringstream result;
487
+ result << "(" << x << ", " << y << ", " << z << ")";
488
+ return result.str();
489
+ }
490
+ /**
491
+ * Writes the vector to the output stream using a human readable format: (x, y, z).
492
+ * @since 1.0
493
+ */
494
+ friend std::ostream& operator<<(std::ostream& out, const Vector& vector) {
495
+ return out << vector.toString();
496
+ }
497
+
498
+ /**
499
+ * Compare Vector equality component-wise.
500
+ *
501
+ * \include Vector_Equals.txt
502
+ * @since 1.0
503
+ */
504
+ bool operator==(const Vector& other) const {
505
+ return x == other.x && y == other.y && z == other.z;
506
+ }
507
+ /**
508
+ * Compare Vector inequality component-wise.
509
+ *
510
+ * \include Vector_NotEqual.txt
511
+ * @since 1.0
512
+ */
513
+ bool operator!=(const Vector& other) const {
514
+ return x != other.x || y != other.y || z != other.z;
515
+ }
516
+
517
+ /**
518
+ * Returns true if all of the vector's components are finite. If any
519
+ * component is NaN or infinite, then this returns false.
520
+ *
521
+ * \include Vector_IsValid.txt
522
+ * @since 1.0
523
+ */
524
+ bool isValid() const {
525
+ return (x <= FLT_MAX && x >= -FLT_MAX) &&
526
+ (y <= FLT_MAX && y >= -FLT_MAX) &&
527
+ (z <= FLT_MAX && z >= -FLT_MAX);
528
+ }
529
+
530
+ /**
531
+ * Index vector components numerically.
532
+ * Index 0 is x, index 1 is y, and index 2 is z.
533
+ * @returns The x, y, or z component of this Vector, if the specified index
534
+ * value is at least 0 and at most 2; otherwise, returns zero.
535
+ *
536
+ * \include Vector_Index.txt
537
+ * @since 1.0
538
+ */
539
+ float operator[](unsigned int index) const {
540
+ return index < 3 ? (&x)[index] : 0.0f;
541
+ }
542
+
543
+ /**
544
+ * Cast the vector to a float array.
545
+ *
546
+ * \include Vector_ToFloatPointer.txt
547
+ * @since 1.0
548
+ */
549
+ const float* toFloatPointer() const {
550
+ return &x; /* Note: Assumes x, y, z are aligned in memory. */
551
+ }
552
+
553
+ /**
554
+ * Convert a Leap::Vector to another 3-component Vector type.
555
+ *
556
+ * The specified type must define a constructor that takes the x, y, and z
557
+ * components as separate parameters.
558
+ * @since 1.0
559
+ */
560
+ template<typename Vector3Type>
561
+ const Vector3Type toVector3() const {
562
+ return Vector3Type(x, y, z);
563
+ }
564
+
565
+ /**
566
+ * Convert a Leap::Vector to another 4-component Vector type.
567
+ *
568
+ * The specified type must define a constructor that takes the x, y, z, and w
569
+ * components as separate parameters. (The homogeneous coordinate, w, is set
570
+ * to zero by default, but you should typically set it to one for vectors
571
+ * representing a position.)
572
+ * @since 1.0
573
+ */
574
+ template<typename Vector4Type>
575
+ const Vector4Type toVector4(float w=0.0f) const {
576
+ return Vector4Type(x, y, z, w);
577
+ }
578
+
579
+ /**
580
+ * The horizontal component.
581
+ * @since 1.0
582
+ */
583
+ float x;
584
+ /**
585
+ * The vertical component.
586
+ * @since 1.0
587
+ */
588
+ float y;
589
+ /**
590
+ * The depth component.
591
+ * @since 1.0
592
+ */
593
+ float z;
594
+ };
595
+
596
+
597
+ /**
598
+ * The FloatArray struct is used to allow the returning of native float arrays
599
+ * without requiring dynamic memory allocation. It represents a matrix
600
+ * with a size up to 4x4.
601
+ * @since 1.0
602
+ */
603
+ struct FloatArray {
604
+ /**
605
+ * Access the elements of the float array exactly like a native array.
606
+ * @since 1.0
607
+ */
608
+ float& operator[] (unsigned int index) {
609
+ return m_array[index];
610
+ }
611
+
612
+ /**
613
+ * Use the Float Array anywhere a float pointer can be used.
614
+ * @since 1.0
615
+ */
616
+ operator float* () {
617
+ return m_array;
618
+ }
619
+
620
+ /**
621
+ * Use the Float Array anywhere a const float pointer can be used.
622
+ * @since 1.0
623
+ */
624
+ operator const float* () const {
625
+ return m_array;
626
+ }
627
+
628
+ /**
629
+ * An array containing up to 16 entries of the matrix.
630
+ * @since 1.0
631
+ */
632
+ float m_array[16];
633
+ };
634
+
635
+ /**
636
+ * The Matrix struct represents a transformation matrix.
637
+ *
638
+ * To use this struct to transform a Vector, construct a matrix containing the
639
+ * desired transformation and then use the Matrix::transformPoint() or
640
+ * Matrix::transformDirection() functions to apply the transform.
641
+ *
642
+ * Transforms can be combined by multiplying two or more transform matrices using
643
+ * the * operator.
644
+ * @since 1.0
645
+ */
646
+ struct Matrix
647
+ {
648
+ /**
649
+ * Constructs an identity transformation matrix.
650
+ *
651
+ * \include Matrix_Matrix.txt
652
+ *
653
+ * @since 1.0
654
+ */
655
+ Matrix() :
656
+ xBasis(1, 0, 0),
657
+ yBasis(0, 1, 0),
658
+ zBasis(0, 0, 1),
659
+ origin(0, 0, 0) {
660
+ }
661
+
662
+ /**
663
+ * Constructs a copy of the specified Matrix object.
664
+ *
665
+ * \include Matrix_Matrix_copy.txt
666
+ *
667
+ * @since 1.0
668
+ */
669
+ Matrix(const Matrix& other) :
670
+ xBasis(other.xBasis),
671
+ yBasis(other.yBasis),
672
+ zBasis(other.zBasis),
673
+ origin(other.origin) {
674
+ }
675
+
676
+ /**
677
+ * Constructs a transformation matrix from the specified basis vectors.
678
+ *
679
+ * \include Matrix_Matrix_basis.txt
680
+ *
681
+ * @param _xBasis A Vector specifying rotation and scale factors for the x-axis.
682
+ * @param _yBasis A Vector specifying rotation and scale factors for the y-axis.
683
+ * @param _zBasis A Vector specifying rotation and scale factors for the z-axis.
684
+ * @since 1.0
685
+ */
686
+ Matrix(const Vector& _xBasis, const Vector& _yBasis, const Vector& _zBasis) :
687
+ xBasis(_xBasis),
688
+ yBasis(_yBasis),
689
+ zBasis(_zBasis),
690
+ origin(0, 0, 0) {
691
+ }
692
+
693
+ /**
694
+ * Constructs a transformation matrix from the specified basis and translation vectors.
695
+ *
696
+ * \include Matrix_Matrix_basis_origin.txt
697
+ *
698
+ * @param _xBasis A Vector specifying rotation and scale factors for the x-axis.
699
+ * @param _yBasis A Vector specifying rotation and scale factors for the y-axis.
700
+ * @param _zBasis A Vector specifying rotation and scale factors for the z-axis.
701
+ * @param _origin A Vector specifying translation factors on all three axes.
702
+ * @since 1.0
703
+ */
704
+ Matrix(const Vector& _xBasis, const Vector& _yBasis, const Vector& _zBasis, const Vector& _origin) :
705
+ xBasis(_xBasis),
706
+ yBasis(_yBasis),
707
+ zBasis(_zBasis),
708
+ origin(_origin) {
709
+ }
710
+
711
+ /**
712
+ * Constructs a transformation matrix specifying a rotation around the specified vector.
713
+ *
714
+ * \include Matrix_Matrix_rotation.txt
715
+ *
716
+ * @param axis A Vector specifying the axis of rotation.
717
+ * @param angleRadians The amount of rotation in radians.
718
+ * @since 1.0
719
+ */
720
+ Matrix(const Vector& axis, float angleRadians) :
721
+ origin(0, 0, 0) {
722
+ setRotation(axis, angleRadians);
723
+ }
724
+
725
+ /**
726
+ * Constructs a transformation matrix specifying a rotation around the specified vector
727
+ * and a translation by the specified vector.
728
+ *
729
+ * \include Matrix_Matrix_rotation_translation.txt
730
+ *
731
+ * @param axis A Vector specifying the axis of rotation.
732
+ * @param angleRadians The angle of rotation in radians.
733
+ * @param translation A Vector representing the translation part of the transform.
734
+ * @since 1.0
735
+ */
736
+ Matrix(const Vector& axis, float angleRadians, const Vector& translation)
737
+ : origin(translation) {
738
+ setRotation(axis, angleRadians);
739
+ }
740
+
741
+ /**
742
+ * Returns the identity matrix specifying no translation, rotation, and scale.
743
+ *
744
+ * \include Matrix_identity.txt
745
+ *
746
+ * @returns The identity matrix.
747
+ * @since 1.0
748
+ */
749
+ static const Matrix& identity() {
750
+ static Matrix s_identity;
751
+ return s_identity;
752
+ }
753
+
754
+ /**
755
+ * Sets this transformation matrix to represent a rotation around the specified vector.
756
+ *
757
+ * \include Matrix_setRotation.txt
758
+ *
759
+ * This function erases any previous rotation and scale transforms applied
760
+ * to this matrix, but does not affect translation.
761
+ *
762
+ * @param axis A Vector specifying the axis of rotation.
763
+ * @param angleRadians The amount of rotation in radians.
764
+ * @since 1.0
765
+ */
766
+ void setRotation(const Vector& axis, float angleRadians) {
767
+ const Vector n = axis.normalized();
768
+ const float s = std::sin(angleRadians);
769
+ const float c = std::cos(angleRadians);
770
+ const float C = (1-c);
771
+
772
+ xBasis = Vector(n[0]*n[0]*C + c, n[0]*n[1]*C - n[2]*s, n[0]*n[2]*C + n[1]*s);
773
+ yBasis = Vector(n[1]*n[0]*C + n[2]*s, n[1]*n[1]*C + c, n[1]*n[2]*C - n[0]*s);
774
+ zBasis = Vector(n[2]*n[0]*C - n[1]*s, n[2]*n[1]*C + n[0]*s, n[2]*n[2]*C + c );
775
+ }
776
+
777
+ /**
778
+ * Transforms a vector with this matrix by transforming its rotation,
779
+ * scale, and translation.
780
+ *
781
+ * \include Matrix_transformPoint.txt
782
+ *
783
+ * Translation is applied after rotation and scale.
784
+ *
785
+ * @param in The Vector to transform.
786
+ * @returns A new Vector representing the transformed original.
787
+ * @since 1.0
788
+ */
789
+ Vector transformPoint(const Vector& in) const {
790
+ return xBasis*in.x + yBasis*in.y + zBasis*in.z + origin;
791
+ }
792
+
793
+ /**
794
+ * Transforms a vector with this matrix by transforming its rotation and
795
+ * scale only.
796
+ *
797
+ * \include Matrix_transformDirection.txt
798
+ *
799
+ * @param in The Vector to transform.
800
+ * @returns A new Vector representing the transformed original.
801
+ * @since 1.0
802
+ */
803
+ Vector transformDirection(const Vector& in) const {
804
+ return xBasis*in.x + yBasis*in.y + zBasis*in.z;
805
+ }
806
+
807
+ /**
808
+ * Performs a matrix inverse if the matrix consists entirely of rigid
809
+ * transformations (translations and rotations). If the matrix is not rigid,
810
+ * this operation will not represent an inverse.
811
+ *
812
+ * \include Matrix_rigidInverse.txt
813
+ *
814
+ * Note that all matricies that are directly returned by the API are rigid.
815
+ *
816
+ * @returns The rigid inverse of the matrix.
817
+ * @since 1.0
818
+ */
819
+ Matrix rigidInverse() const {
820
+ Matrix rotInverse = Matrix(Vector(xBasis[0], yBasis[0], zBasis[0]),
821
+ Vector(xBasis[1], yBasis[1], zBasis[1]),
822
+ Vector(xBasis[2], yBasis[2], zBasis[2]));
823
+ rotInverse.origin = rotInverse.transformDirection( -origin );
824
+ return rotInverse;
825
+ }
826
+
827
+ /**
828
+ * Multiply transform matrices.
829
+ *
830
+ * Combines two transformations into a single equivalent transformation.
831
+ *
832
+ * \include Matrix_operator_times.txt
833
+ *
834
+ * @param other A Matrix to multiply on the right hand side.
835
+ * @returns A new Matrix representing the transformation equivalent to
836
+ * applying the other transformation followed by this transformation.
837
+ * @since 1.0
838
+ */
839
+ Matrix operator*(const Matrix& other) const {
840
+ return Matrix(transformDirection(other.xBasis),
841
+ transformDirection(other.yBasis),
842
+ transformDirection(other.zBasis),
843
+ transformPoint(other.origin));
844
+ }
845
+
846
+ /**
847
+ * Multiply transform matrices and assign the product.
848
+ *
849
+ * \include Matrix_operator_times_equal.txt
850
+ *
851
+ * @since 1.0
852
+ */
853
+ Matrix& operator*=(const Matrix& other) {
854
+ return (*this) = (*this) * other;
855
+ }
856
+
857
+ /**
858
+ * Compare Matrix equality component-wise.
859
+ *
860
+ * \include Matrix_operator_equals.txt
861
+ *
862
+ * @since 1.0
863
+ */
864
+ bool operator==(const Matrix& other) const {
865
+ return xBasis == other.xBasis &&
866
+ yBasis == other.yBasis &&
867
+ zBasis == other.zBasis &&
868
+ origin == other.origin;
869
+ }
870
+ /**
871
+ * Compare Matrix inequality component-wise.
872
+ *
873
+ * \include Matrix_operator_not_equals.txt
874
+ *
875
+ * @since 1.0
876
+ */
877
+ bool operator!=(const Matrix& other) const {
878
+ return xBasis != other.xBasis ||
879
+ yBasis != other.yBasis ||
880
+ zBasis != other.zBasis ||
881
+ origin != other.origin;
882
+ }
883
+
884
+ /**
885
+ * Convert a Leap::Matrix object to another 3x3 matrix type.
886
+ *
887
+ * The new type must define a constructor function that takes each matrix
888
+ * element as a parameter in row-major order.
889
+ *
890
+ * Translation factors are discarded.
891
+ * @since 1.0
892
+ */
893
+ template<typename Matrix3x3Type>
894
+ const Matrix3x3Type toMatrix3x3() const {
895
+ return Matrix3x3Type(xBasis.x, xBasis.y, xBasis.z,
896
+ yBasis.x, yBasis.y, yBasis.z,
897
+ zBasis.x, zBasis.y, zBasis.z);
898
+ }
899
+
900
+ /**
901
+ * Convert a Leap::Matrix object to another 4x4 matrix type.
902
+ *
903
+ * The new type must define a constructor function that takes each matrix
904
+ * element as a parameter in row-major order.
905
+ * @since 1.0
906
+ */
907
+ template<typename Matrix4x4Type>
908
+ const Matrix4x4Type toMatrix4x4() const {
909
+ return Matrix4x4Type(xBasis.x, xBasis.y, xBasis.z, 0.0f,
910
+ yBasis.x, yBasis.y, yBasis.z, 0.0f,
911
+ zBasis.x, zBasis.y, zBasis.z, 0.0f,
912
+ origin.x, origin.y, origin.z, 1.0f);
913
+ }
914
+
915
+ /**
916
+ * Writes the 3x3 Matrix object to a 9 element row-major float or
917
+ * double array.
918
+ *
919
+ * Translation factors are discarded.
920
+ *
921
+ * Returns a pointer to the same data.
922
+ * @since 1.0
923
+ */
924
+ template<typename T>
925
+ T* toArray3x3(T* output) const {
926
+ output[0] = xBasis.x; output[1] = xBasis.y; output[2] = xBasis.z;
927
+ output[3] = yBasis.x; output[4] = yBasis.y; output[5] = yBasis.z;
928
+ output[6] = zBasis.x; output[7] = zBasis.y; output[8] = zBasis.z;
929
+ return output;
930
+ }
931
+
932
+ /**
933
+ * Convert a 3x3 Matrix object to a 9 element row-major float array.
934
+ *
935
+ * Translation factors are discarded.
936
+ *
937
+ * \include Matrix_toArray3x3.txt
938
+ *
939
+ * Returns a FloatArray struct to avoid dynamic memory allocation.
940
+ * @since 1.0
941
+ */
942
+ FloatArray toArray3x3() const {
943
+ FloatArray output;
944
+ toArray3x3((float*)output);
945
+ return output;
946
+ }
947
+
948
+ /**
949
+ * Writes the 4x4 Matrix object to a 16 element row-major float
950
+ * or double array.
951
+ *
952
+ * Returns a pointer to the same data.
953
+ * @since 1.0
954
+ */
955
+ template<typename T>
956
+ T* toArray4x4(T* output) const {
957
+ output[0] = xBasis.x; output[1] = xBasis.y; output[2] = xBasis.z; output[3] = 0.0f;
958
+ output[4] = yBasis.x; output[5] = yBasis.y; output[6] = yBasis.z; output[7] = 0.0f;
959
+ output[8] = zBasis.x; output[9] = zBasis.y; output[10] = zBasis.z; output[11] = 0.0f;
960
+ output[12] = origin.x; output[13] = origin.y; output[14] = origin.z; output[15] = 1.0f;
961
+ return output;
962
+ }
963
+
964
+ /**
965
+ * Convert a 4x4 Matrix object to a 16 element row-major float array.
966
+ *
967
+ * \include Matrix_toArray4x4.txt
968
+ *
969
+ * Returns a FloatArray struct to avoid dynamic memory allocation.
970
+ * @since 1.0
971
+ */
972
+ FloatArray toArray4x4() const {
973
+ FloatArray output;
974
+ toArray4x4((float*)output);
975
+ return output;
976
+ }
977
+
978
+ /**
979
+ * Write the matrix to a string in a human readable format.
980
+ * @since 1.0
981
+ */
982
+ std::string toString() const {
983
+ std::stringstream result;
984
+ result << "xBasis:" << xBasis.toString() << " yBasis:" << yBasis.toString()
985
+ << " zBasis:" << zBasis.toString() << " origin:" << origin.toString();
986
+ return result.str();
987
+ }
988
+
989
+ /**
990
+ * Write the matrix to an output stream in a human readable format.
991
+ *
992
+ * \include Matrix_operator_stream.txt
993
+ *
994
+ * @since 1.0
995
+ */
996
+ friend std::ostream& operator<<(std::ostream& out, const Matrix& matrix) {
997
+ return out << matrix.toString();
998
+ }
999
+
1000
+ /**
1001
+ * The rotation and scale factors for the x-axis.
1002
+ *
1003
+ * \include Matrix_xBasis.txt
1004
+ *
1005
+ * @since 1.0
1006
+ */
1007
+ Vector xBasis;
1008
+ /**
1009
+ * The rotation and scale factors for the y-axis.
1010
+ *
1011
+ * \include Matrix_yBasis.txt
1012
+ *
1013
+ * @since 1.0
1014
+ */
1015
+ Vector yBasis;
1016
+ /**
1017
+ * The rotation and scale factors for the z-axis.
1018
+ *
1019
+ * \include Matrix_zBasis.txt
1020
+ *
1021
+ * @since 1.0
1022
+ */
1023
+ Vector zBasis;
1024
+ /**
1025
+ * The translation factors for all three axes.
1026
+ *
1027
+ * \include Matrix_origin.txt
1028
+ *
1029
+ * @since 1.0
1030
+ */
1031
+ Vector origin;
1032
+ };
1033
+
1034
+ }; // namespace Leap
1035
+
1036
+ #endif // __LeapMath_h__