LeapMath.h
Go to the documentation of this file.
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 #include <algorithm>
17 
18 namespace Leap {
19 
24 static const float PI = 3.1415926536f;
30 static const float DEG_TO_RAD = 0.0174532925f;
36 static const float RAD_TO_DEG = 57.295779513f;
37 
43 static const float EPSILON = 1.192092896e-07f;
44 
60 struct Vector {
65  Vector() :
66  x(0), y(0), z(0) {}
67 
74  Vector(float _x, float _y, float _z) :
75  x(_x), y(_y), z(_z) {}
76 
83  Vector(const Vector& vector) :
84  x(vector.x), y(vector.y), z(vector.z) {}
85 
92  static const Vector& zero() {
93  static Vector s_zero(0, 0, 0);
94  return s_zero;
95  }
96 
103  static const Vector& xAxis() {
104  static Vector s_xAxis(1, 0, 0);
105  return s_xAxis;
106  }
113  static const Vector& yAxis() {
114  static Vector s_yAxis(0, 1, 0);
115  return s_yAxis;
116  }
123  static const Vector& zAxis() {
124  static Vector s_zAxis(0, 0, 1);
125  return s_zAxis;
126  }
127 
134  static const Vector& left() {
135  static Vector s_left(-1, 0, 0);
136  return s_left;
137  }
144  static const Vector& right() {
145  return xAxis();
146  }
153  static const Vector& down() {
154  static Vector s_down(0, -1, 0);
155  return s_down;
156  }
163  static const Vector& up() {
164  return yAxis();
165  }
172  static const Vector& forward() {
173  static Vector s_forward(0, 0, -1);
174  return s_forward;
175  }
182  static const Vector& backward() {
183  return zAxis();
184  }
185 
197  float magnitude() const {
198  return std::sqrt(x*x + y*y + z*z);
199  }
200 
209  float magnitudeSquared() const {
210  return x*x + y*y + z*z;
211  }
212 
223  float distanceTo(const Vector& other) const {
224  return std::sqrt((x - other.x)*(x - other.x) +
225  (y - other.y)*(y - other.y) +
226  (z - other.z)*(z - other.z));
227  }
228 
247  float angleTo(const Vector& other) const {
248  float denom = this->magnitudeSquared() * other.magnitudeSquared();
249  if (denom <= EPSILON) {
250  return 0.0f;
251  }
252  float val = this->dot(other) / std::sqrt(denom);
253  if (val >= 1.0f) {
254  return 0.0f;
255  } else if (val <= -1.0f) {
256  return PI;
257  }
258  return std::acos(val);
259  }
260 
277  float pitch() const {
278  return std::atan2(y, -z);
279  }
280 
297  float yaw() const {
298  return std::atan2(x, -z);
299  }
300 
322  float roll() const {
323  return std::atan2(x, -y);
324  }
325 
340  float dot(const Vector& other) const {
341  return (x * other.x) + (y * other.y) + (z * other.z);
342  }
343 
360  Vector cross(const Vector& other) const {
361  return Vector((y * other.z) - (z * other.y),
362  (z * other.x) - (x * other.z),
363  (x * other.y) - (y * other.x));
364  }
365 
378  Vector normalized() const {
379  float denom = this->magnitudeSquared();
380  if (denom <= EPSILON) {
381  return Vector::zero();
382  }
383  denom = 1.0f / std::sqrt(denom);
384  return Vector(x * denom, y * denom, z * denom);
385  }
386 
395  Vector operator-() const {
396  return Vector(-x, -y, -z);
397  }
398 
405  Vector operator+(const Vector& other) const {
406  return Vector(x + other.x, y + other.y, z + other.z);
407  }
408 
415  Vector operator-(const Vector& other) const {
416  return Vector(x - other.x, y - other.y, z - other.z);
417  }
418 
425  Vector operator*(float scalar) const {
426  return Vector(x * scalar, y * scalar, z * scalar);
427  }
428 
435  Vector operator/(float scalar) const {
436  return Vector(x / scalar, y / scalar, z / scalar);
437  }
438 
439 #if !defined(SWIG)
440 
446  friend Vector operator*(float scalar, const Vector& vector) {
447  return Vector(vector.x * scalar, vector.y * scalar, vector.z * scalar);
448  }
449 #endif
450 
455  Vector& operator+=(const Vector& other) {
456  x += other.x;
457  y += other.y;
458  z += other.z;
459  return *this;
460  }
461 
466  Vector& operator-=(const Vector& other) {
467  x -= other.x;
468  y -= other.y;
469  z -= other.z;
470  return *this;
471  }
472 
477  Vector& operator*=(float scalar) {
478  x *= scalar;
479  y *= scalar;
480  z *= scalar;
481  return *this;
482  }
483 
488  Vector& operator/=(float scalar) {
489  x /= scalar;
490  y /= scalar;
491  z /= scalar;
492  return *this;
493  }
494 
499  std::string toString() const {
500  std::stringstream result;
501  result << "(" << x << ", " << y << ", " << z << ")";
502  return result.str();
503  }
508  friend std::ostream& operator<<(std::ostream& out, const Vector& vector) {
509  return out << vector.toString();
510  }
511 
518  bool operator==(const Vector& other) const {
519  return x == other.x && y == other.y && z == other.z;
520  }
527  bool operator!=(const Vector& other) const {
528  return x != other.x || y != other.y || z != other.z;
529  }
530 
538  bool isValid() const {
539  return (x <= FLT_MAX && x >= -FLT_MAX) &&
540  (y <= FLT_MAX && y >= -FLT_MAX) &&
541  (z <= FLT_MAX && z >= -FLT_MAX);
542  }
543 
553  float operator[](unsigned int index) const {
554  return index < 3 ? (&x)[index] : 0.0f;
555  }
556 
563  const float* toFloatPointer() const {
564  return &x; /* Note: Assumes x, y, z are aligned in memory. */
565  }
566 
574  template<typename Vector3Type>
575  const Vector3Type toVector3() const {
576  return Vector3Type(x, y, z);
577  }
578 
588  template<typename Vector4Type>
589  const Vector4Type toVector4(float w=0.0f) const {
590  return Vector4Type(x, y, z, w);
591  }
592 
597  float x;
602  float y;
607  float z;
608 };
609 
610 
617 struct FloatArray {
622  float& operator[] (unsigned int index) {
623  return m_array[index];
624  }
625 
630  operator float* () {
631  return m_array;
632  }
633 
638  operator const float* () const {
639  return m_array;
640  }
641 
646  float m_array[16];
647 };
648 
660 struct Matrix
661 {
669  Matrix() :
670  xBasis(1, 0, 0),
671  yBasis(0, 1, 0),
672  zBasis(0, 0, 1),
673  origin(0, 0, 0) {
674  }
675 
683  Matrix(const Matrix& other) :
684  xBasis(other.xBasis),
685  yBasis(other.yBasis),
686  zBasis(other.zBasis),
687  origin(other.origin) {
688  }
689 
700  Matrix(const Vector& _xBasis, const Vector& _yBasis, const Vector& _zBasis) :
701  xBasis(_xBasis),
702  yBasis(_yBasis),
703  zBasis(_zBasis),
704  origin(0, 0, 0) {
705  }
706 
718  Matrix(const Vector& _xBasis, const Vector& _yBasis, const Vector& _zBasis, const Vector& _origin) :
719  xBasis(_xBasis),
720  yBasis(_yBasis),
721  zBasis(_zBasis),
722  origin(_origin) {
723  }
724 
734  Matrix(const Vector& axis, float angleRadians) :
735  origin(0, 0, 0) {
736  setRotation(axis, angleRadians);
737  }
738 
750  Matrix(const Vector& axis, float angleRadians, const Vector& translation)
751  : origin(translation) {
752  setRotation(axis, angleRadians);
753  }
754 
763  static const Matrix& identity() {
764  static Matrix s_identity;
765  return s_identity;
766  }
767 
780  void setRotation(const Vector& axis, float angleRadians) {
781  const Vector n = axis.normalized();
782  const float s = std::sin(angleRadians);
783  const float c = std::cos(angleRadians);
784  const float C = (1-c);
785 
786  xBasis = Vector(n[0]*n[0]*C + c, n[0]*n[1]*C - n[2]*s, n[0]*n[2]*C + n[1]*s);
787  yBasis = Vector(n[1]*n[0]*C + n[2]*s, n[1]*n[1]*C + c, n[1]*n[2]*C - n[0]*s);
788  zBasis = Vector(n[2]*n[0]*C - n[1]*s, n[2]*n[1]*C + n[0]*s, n[2]*n[2]*C + c );
789  }
790 
803  Vector transformPoint(const Vector& in) const {
804  return xBasis*in.x + yBasis*in.y + zBasis*in.z + origin;
805  }
806 
817  Vector transformDirection(const Vector& in) const {
818  return xBasis*in.x + yBasis*in.y + zBasis*in.z;
819  }
820 
834  Matrix rotInverse = Matrix(Vector(xBasis[0], yBasis[0], zBasis[0]),
835  Vector(xBasis[1], yBasis[1], zBasis[1]),
836  Vector(xBasis[2], yBasis[2], zBasis[2]));
837  rotInverse.origin = rotInverse.transformDirection( -origin );
838  return rotInverse;
839  }
840 
853  Matrix operator*(const Matrix& other) const {
854  return Matrix(transformDirection(other.xBasis),
855  transformDirection(other.yBasis),
856  transformDirection(other.zBasis),
857  transformPoint(other.origin));
858  }
859 
867  Matrix& operator*=(const Matrix& other) {
868  return (*this) = (*this) * other;
869  }
870 
878  bool operator==(const Matrix& other) const {
879  return xBasis == other.xBasis &&
880  yBasis == other.yBasis &&
881  zBasis == other.zBasis &&
882  origin == other.origin;
883  }
891  bool operator!=(const Matrix& other) const {
892  return xBasis != other.xBasis ||
893  yBasis != other.yBasis ||
894  zBasis != other.zBasis ||
895  origin != other.origin;
896  }
897 
907  template<typename Matrix3x3Type>
908  const Matrix3x3Type toMatrix3x3() const {
909  return Matrix3x3Type(xBasis.x, xBasis.y, xBasis.z,
910  yBasis.x, yBasis.y, yBasis.z,
911  zBasis.x, zBasis.y, zBasis.z);
912  }
913 
921  template<typename Matrix4x4Type>
922  const Matrix4x4Type toMatrix4x4() const {
923  return Matrix4x4Type(xBasis.x, xBasis.y, xBasis.z, 0.0f,
924  yBasis.x, yBasis.y, yBasis.z, 0.0f,
925  zBasis.x, zBasis.y, zBasis.z, 0.0f,
926  origin.x, origin.y, origin.z, 1.0f);
927  }
928 
938  template<typename T>
939  T* toArray3x3(T* output) const {
940  output[0] = xBasis.x; output[1] = xBasis.y; output[2] = xBasis.z;
941  output[3] = yBasis.x; output[4] = yBasis.y; output[5] = yBasis.z;
942  output[6] = zBasis.x; output[7] = zBasis.y; output[8] = zBasis.z;
943  return output;
944  }
945 
957  FloatArray output;
958  toArray3x3((float*)output);
959  return output;
960  }
961 
969  template<typename T>
970  T* toArray4x4(T* output) const {
971  output[0] = xBasis.x; output[1] = xBasis.y; output[2] = xBasis.z; output[3] = 0.0f;
972  output[4] = yBasis.x; output[5] = yBasis.y; output[6] = yBasis.z; output[7] = 0.0f;
973  output[8] = zBasis.x; output[9] = zBasis.y; output[10] = zBasis.z; output[11] = 0.0f;
974  output[12] = origin.x; output[13] = origin.y; output[14] = origin.z; output[15] = 1.0f;
975  return output;
976  }
977 
987  FloatArray output;
988  toArray4x4((float*)output);
989  return output;
990  }
991 
996  std::string toString() const {
997  std::stringstream result;
998  result << "xBasis:" << xBasis.toString() << " yBasis:" << yBasis.toString()
999  << " zBasis:" << zBasis.toString() << " origin:" << origin.toString();
1000  return result.str();
1001  }
1002 
1010  friend std::ostream& operator<<(std::ostream& out, const Matrix& matrix) {
1011  return out << matrix.toString();
1012  }
1013 
1046 };
1047 
1048 }; // namespace Leap
1049 
1050 #endif // __LeapMath_h__
Vector(const Vector &vector)
Definition: LeapMath.h:83
Vector operator+(const Vector &other) const
Definition: LeapMath.h:405
Vector xBasis
Definition: LeapMath.h:1021
Vector & operator+=(const Vector &other)
Definition: LeapMath.h:455
float magnitude() const
Definition: LeapMath.h:197
f
static const Vector & forward()
Definition: LeapMath.h:172
Vector cross(const Vector &other) const
Definition: LeapMath.h:360
Vector transformDirection(const Vector &in) const
Definition: LeapMath.h:817
std::string toString() const
Definition: LeapMath.h:996
XmlRpcServer s
FloatArray toArray3x3() const
Definition: LeapMath.h:956
FloatArray toArray4x4() const
Definition: LeapMath.h:986
bool operator!=(const Vector &other) const
Definition: LeapMath.h:527
Vector operator-(const Vector &other) const
Definition: LeapMath.h:415
const float * toFloatPointer() const
Definition: LeapMath.h:563
bool operator==(const Matrix &other) const
Definition: LeapMath.h:878
Vector & operator-=(const Vector &other)
Definition: LeapMath.h:466
static const Vector & up()
Definition: LeapMath.h:163
float yaw() const
Definition: LeapMath.h:297
static const float DEG_TO_RAD
Definition: LeapMath.h:30
Matrix operator*(const Matrix &other) const
Definition: LeapMath.h:853
const Vector4Type toVector4(float w=0.0f) const
Definition: LeapMath.h:589
Definition: Leap.h:48
T * toArray3x3(T *output) const
Definition: LeapMath.h:939
const Matrix4x4Type toMatrix4x4() const
Definition: LeapMath.h:922
static const Vector & zAxis()
Definition: LeapMath.h:123
static const float RAD_TO_DEG
Definition: LeapMath.h:36
Vector operator/(float scalar) const
Definition: LeapMath.h:435
Matrix & operator*=(const Matrix &other)
Definition: LeapMath.h:867
Vector yBasis
Definition: LeapMath.h:1029
float magnitudeSquared() const
Definition: LeapMath.h:209
Matrix(const Vector &axis, float angleRadians)
Definition: LeapMath.h:734
Vector operator*(float scalar) const
Definition: LeapMath.h:425
float roll() const
Definition: LeapMath.h:322
static const float PI
Definition: LeapMath.h:24
Vector normalized() const
Definition: LeapMath.h:378
static const Vector & backward()
Definition: LeapMath.h:182
Matrix(const Vector &_xBasis, const Vector &_yBasis, const Vector &_zBasis, const Vector &_origin)
Definition: LeapMath.h:718
float angleTo(const Vector &other) const
Definition: LeapMath.h:247
Matrix(const Matrix &other)
Definition: LeapMath.h:683
static const Vector & yAxis()
Definition: LeapMath.h:113
Vector origin
Definition: LeapMath.h:1045
float dot(const Vector &other) const
Definition: LeapMath.h:340
Vector zBasis
Definition: LeapMath.h:1037
static const Vector & left()
Definition: LeapMath.h:134
static const Vector & xAxis()
Definition: LeapMath.h:103
float pitch() const
Definition: LeapMath.h:277
Matrix(const Vector &axis, float angleRadians, const Vector &translation)
Definition: LeapMath.h:750
Matrix(const Vector &_xBasis, const Vector &_yBasis, const Vector &_zBasis)
Definition: LeapMath.h:700
TFSIMD_FORCE_INLINE const tfScalar & w() const
static const float EPSILON
Definition: LeapMath.h:43
void setRotation(const Vector &axis, float angleRadians)
Definition: LeapMath.h:780
static const Vector & right()
Definition: LeapMath.h:144
Vector & operator/=(float scalar)
Definition: LeapMath.h:488
const Vector3Type toVector3() const
Definition: LeapMath.h:575
std::string toString() const
Definition: LeapMath.h:499
friend std::ostream & operator<<(std::ostream &out, const Matrix &matrix)
Definition: LeapMath.h:1010
bool operator!=(const Matrix &other) const
Definition: LeapMath.h:891
const Matrix3x3Type toMatrix3x3() const
Definition: LeapMath.h:908
friend Vector operator*(float scalar, const Vector &vector)
Definition: LeapMath.h:446
static const Matrix & identity()
Definition: LeapMath.h:763
float operator[](unsigned int index) const
Definition: LeapMath.h:553
static const Vector & zero()
Definition: LeapMath.h:92
T * toArray4x4(T *output) const
Definition: LeapMath.h:970
friend std::ostream & operator<<(std::ostream &out, const Vector &vector)
Definition: LeapMath.h:508
Vector operator-() const
Definition: LeapMath.h:395
bool operator==(const Vector &other) const
Definition: LeapMath.h:518
static const Vector & down()
Definition: LeapMath.h:153
float distanceTo(const Vector &other) const
Definition: LeapMath.h:223
Vector & operator*=(float scalar)
Definition: LeapMath.h:477
bool isValid() const
Definition: LeapMath.h:538
Matrix rigidInverse() const
Definition: LeapMath.h:833
Vector transformPoint(const Vector &in) const
Definition: LeapMath.h:803
Vector(float _x, float _y, float _z)
Definition: LeapMath.h:74


leap_motion
Author(s): Florian Lier , Mirza Shah , Isaac IY Saito
autogenerated on Tue Jun 2 2020 03:58:01