9 #if !defined(__LeapMath_h__) 10 #define __LeapMath_h__ 24 static const float PI = 3.1415926536f;
74 Vector(
float _x,
float _y,
float _z) :
75 x(_x),
y(_y),
z(_z) {}
84 x(vector.
x),
y(vector.
y),
z(vector.
z) {}
93 static Vector s_zero(0, 0, 0);
104 static Vector s_xAxis(1, 0, 0);
114 static Vector s_yAxis(0, 1, 0);
124 static Vector s_zAxis(0, 0, 1);
135 static Vector s_left(-1, 0, 0);
154 static Vector s_down(0, -1, 0);
173 static Vector s_forward(0, 0, -1);
198 return std::sqrt(
x*
x +
y*
y +
z*
z);
224 return std::sqrt((
x - other.
x)*(
x - other.
x) +
225 (
y - other.
y)*(
y - other.
y) +
226 (
z - other.
z)*(
z - other.
z));
249 if (denom <= EPSILON) {
252 float val = this->
dot(other) / std::sqrt(denom);
255 }
else if (val <= -1.0
f) {
258 return std::acos(val);
278 return std::atan2(
y, -
z);
298 return std::atan2(
x, -
z);
323 return std::atan2(
x, -
y);
341 return (
x * other.
x) + (
y * other.
y) + (
z * other.
z);
362 (
z * other.
x) - (
x * other.
z),
363 (
x * other.
y) - (
y * other.
x));
380 if (denom <= EPSILON) {
383 denom = 1.0f / std::sqrt(denom);
384 return Vector(
x * denom,
y * denom,
z * denom);
426 return Vector(
x * scalar,
y * scalar,
z * scalar);
436 return Vector(
x / scalar,
y / scalar,
z / scalar);
447 return Vector(vector.
x * scalar, vector.
y * scalar, vector.
z * scalar);
500 std::stringstream result;
501 result <<
"(" <<
x <<
", " <<
y <<
", " <<
z <<
")";
519 return x == other.
x &&
y == other.
y &&
z == other.
z;
528 return x != other.
x ||
y != other.
y ||
z != other.
z;
539 return (x <= FLT_MAX && x >= -FLT_MAX) &&
540 (y <= FLT_MAX && y >= -FLT_MAX) &&
541 (z <= FLT_MAX && z >= -FLT_MAX);
554 return index < 3 ? (&
x)[index] : 0.0
f;
574 template<
typename Vector3Type>
576 return Vector3Type(
x,
y,
z);
588 template<
typename Vector4Type>
590 return Vector4Type(
x,
y,
z,
w);
623 return m_array[index];
638 operator const float* ()
const {
684 xBasis(other.xBasis),
685 yBasis(other.yBasis),
686 zBasis(other.zBasis),
687 origin(other.origin) {
736 setRotation(axis, angleRadians);
751 : origin(translation) {
752 setRotation(axis, angleRadians);
782 const float s = std::sin(angleRadians);
783 const float c = std::cos(angleRadians);
784 const float C = (1-c);
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 );
804 return xBasis*in.
x + yBasis*in.
y + zBasis*in.
z + origin;
818 return xBasis*in.
x + yBasis*in.
y + zBasis*in.
z;
835 Vector(xBasis[1], yBasis[1], zBasis[1]),
836 Vector(xBasis[2], yBasis[2], zBasis[2]));
855 transformDirection(other.
yBasis),
856 transformDirection(other.
zBasis),
857 transformPoint(other.
origin));
868 return (*
this) = (*this) * other;
879 return xBasis == other.
xBasis &&
892 return xBasis != other.
xBasis ||
907 template<
typename Matrix3x3Type>
909 return Matrix3x3Type(xBasis.x, xBasis.y, xBasis.z,
910 yBasis.x, yBasis.y, yBasis.z,
911 zBasis.x, zBasis.y, zBasis.z);
921 template<
typename Matrix4x4Type>
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);
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;
958 toArray3x3((
float*)output);
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;
988 toArray4x4((
float*)output);
997 std::stringstream result;
998 result <<
"xBasis:" << xBasis.toString() <<
" yBasis:" << yBasis.toString()
999 <<
" zBasis:" << zBasis.toString() <<
" origin:" << origin.toString();
1000 return result.str();
1050 #endif // __LeapMath_h__ Vector(const Vector &vector)
Vector operator+(const Vector &other) const
Vector & operator+=(const Vector &other)
static const Vector & forward()
Vector cross(const Vector &other) const
Vector transformDirection(const Vector &in) const
std::string toString() const
FloatArray toArray3x3() const
FloatArray toArray4x4() const
bool operator!=(const Vector &other) const
Vector operator-(const Vector &other) const
const float * toFloatPointer() const
bool operator==(const Matrix &other) const
Vector & operator-=(const Vector &other)
static const Vector & up()
static const float DEG_TO_RAD
Matrix operator*(const Matrix &other) const
const Vector4Type toVector4(float w=0.0f) const
T * toArray3x3(T *output) const
const Matrix4x4Type toMatrix4x4() const
static const Vector & zAxis()
static const float RAD_TO_DEG
Vector operator/(float scalar) const
Matrix & operator*=(const Matrix &other)
float magnitudeSquared() const
Matrix(const Vector &axis, float angleRadians)
Vector operator*(float scalar) const
Vector normalized() const
static const Vector & backward()
Matrix(const Vector &_xBasis, const Vector &_yBasis, const Vector &_zBasis, const Vector &_origin)
float angleTo(const Vector &other) const
Matrix(const Matrix &other)
static const Vector & yAxis()
float dot(const Vector &other) const
static const Vector & left()
static const Vector & xAxis()
Matrix(const Vector &axis, float angleRadians, const Vector &translation)
Matrix(const Vector &_xBasis, const Vector &_yBasis, const Vector &_zBasis)
TFSIMD_FORCE_INLINE const tfScalar & w() const
static const float EPSILON
void setRotation(const Vector &axis, float angleRadians)
static const Vector & right()
Vector & operator/=(float scalar)
const Vector3Type toVector3() const
std::string toString() const
friend std::ostream & operator<<(std::ostream &out, const Matrix &matrix)
bool operator!=(const Matrix &other) const
const Matrix3x3Type toMatrix3x3() const
friend Vector operator*(float scalar, const Vector &vector)
static const Matrix & identity()
float operator[](unsigned int index) const
static const Vector & zero()
T * toArray4x4(T *output) const
friend std::ostream & operator<<(std::ostream &out, const Vector &vector)
bool operator==(const Vector &other) const
static const Vector & down()
float distanceTo(const Vector &other) const
Vector & operator*=(float scalar)
Matrix rigidInverse() const
Vector transformPoint(const Vector &in) const
Vector(float _x, float _y, float _z)