2011-08-18 21:02:48 +00:00
|
|
|
/********************************************************************************
|
|
|
|
* ReactPhysics3D physics library, http://code.google.com/p/reactphysics3d/ *
|
2011-11-13 17:49:03 +00:00
|
|
|
* Copyright (c) 2010-2012 Daniel Chappuis *
|
2011-08-18 21:02:48 +00:00
|
|
|
*********************************************************************************
|
|
|
|
* *
|
2011-11-13 17:49:03 +00:00
|
|
|
* This software is provided 'as-is', without any express or implied warranty. *
|
|
|
|
* In no event will the authors be held liable for any damages arising from the *
|
|
|
|
* use of this software. *
|
2011-08-18 21:02:48 +00:00
|
|
|
* *
|
2011-11-13 17:49:03 +00:00
|
|
|
* Permission is granted to anyone to use this software for any purpose, *
|
|
|
|
* including commercial applications, and to alter it and redistribute it *
|
|
|
|
* freely, subject to the following restrictions: *
|
|
|
|
* *
|
|
|
|
* 1. The origin of this software must not be misrepresented; you must not claim *
|
|
|
|
* that you wrote the original software. If you use this software in a *
|
|
|
|
* product, an acknowledgment in the product documentation would be *
|
|
|
|
* appreciated but is not required. *
|
|
|
|
* *
|
|
|
|
* 2. Altered source versions must be plainly marked as such, and must not be *
|
|
|
|
* misrepresented as being the original software. *
|
|
|
|
* *
|
|
|
|
* 3. This notice may not be removed or altered from any source distribution. *
|
2011-08-18 21:02:48 +00:00
|
|
|
* *
|
|
|
|
********************************************************************************/
|
|
|
|
|
|
|
|
#ifndef VECTOR3_H
|
|
|
|
#define VECTOR3_H
|
|
|
|
|
|
|
|
// Libraries
|
|
|
|
#include <cmath>
|
2013-02-16 15:14:04 +00:00
|
|
|
#include <cassert>
|
2011-08-18 21:02:48 +00:00
|
|
|
#include "mathematics_functions.h"
|
2012-01-18 23:06:33 +00:00
|
|
|
#include "../decimal.h"
|
2011-08-18 21:02:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
// ReactPhysics3D namespace
|
|
|
|
namespace reactphysics3d {
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------
|
2013-02-27 21:10:10 +00:00
|
|
|
Structure Vector3 :
|
|
|
|
This class represents a 3D vector.
|
2011-08-18 21:02:48 +00:00
|
|
|
-------------------------------------------------------------------
|
|
|
|
*/
|
2013-02-27 21:10:10 +00:00
|
|
|
struct Vector3 {
|
2012-10-09 20:21:02 +00:00
|
|
|
|
2013-02-27 21:10:10 +00:00
|
|
|
public:
|
2012-10-09 20:21:02 +00:00
|
|
|
|
|
|
|
// -------------------- Attributes -------------------- //
|
|
|
|
|
|
|
|
// Values of the 3D vector
|
2013-02-27 21:10:10 +00:00
|
|
|
decimal x, y, z;
|
2012-10-09 20:21:02 +00:00
|
|
|
|
|
|
|
// -------------------- Methods -------------------- //
|
|
|
|
|
|
|
|
// Constructor of the class Vector3D
|
|
|
|
Vector3();
|
|
|
|
|
|
|
|
// Constructor with arguments
|
2013-02-27 21:10:10 +00:00
|
|
|
Vector3(decimal newX, decimal newY, decimal newZ);
|
2012-10-09 20:21:02 +00:00
|
|
|
|
|
|
|
// Copy-constructor
|
|
|
|
Vector3(const Vector3& vector);
|
|
|
|
|
|
|
|
// Destructor
|
|
|
|
~Vector3();
|
|
|
|
|
|
|
|
// Set all the values of the vector
|
2013-02-27 21:10:10 +00:00
|
|
|
void setAllValues(decimal newX, decimal newY, decimal newZ);
|
2012-10-09 20:21:02 +00:00
|
|
|
|
|
|
|
// Return the lenght of the vector
|
|
|
|
decimal length() const;
|
|
|
|
|
|
|
|
// Return the square of the length of the vector
|
|
|
|
decimal lengthSquare() const;
|
|
|
|
|
|
|
|
// Return the corresponding unit vector
|
|
|
|
Vector3 getUnit() const;
|
|
|
|
|
2013-01-27 09:38:41 +00:00
|
|
|
// Return one unit orthogonal vector of the current vector
|
|
|
|
Vector3 getOneUnitOrthogonalVector() const;
|
|
|
|
|
2012-10-09 20:21:02 +00:00
|
|
|
// Return true if the vector is unit and false otherwise
|
|
|
|
bool isUnit() const;
|
|
|
|
|
|
|
|
// Return true if the current vector is the zero vector
|
|
|
|
bool isZero() const;
|
|
|
|
|
|
|
|
// Dot product of two vectors
|
|
|
|
decimal dot(const Vector3& vector) const;
|
|
|
|
|
|
|
|
// Cross product of two vectors
|
|
|
|
Vector3 cross(const Vector3& vector) const;
|
|
|
|
|
2013-02-16 15:14:04 +00:00
|
|
|
// Normalize the vector
|
|
|
|
void normalize();
|
|
|
|
|
2012-10-09 20:21:02 +00:00
|
|
|
// Return the corresponding absolute value vector
|
|
|
|
Vector3 getAbsoluteVector() const;
|
|
|
|
|
|
|
|
// Return the axis with the minimal value
|
|
|
|
int getMinAxis() const;
|
|
|
|
|
|
|
|
// Return the axis with the maximal value
|
|
|
|
int getMaxAxis() const;
|
|
|
|
|
|
|
|
// Return true if two vectors are parallel
|
|
|
|
bool isParallelWith(const Vector3& vector) const;
|
|
|
|
|
|
|
|
// Overloaded operator for the equality condition
|
|
|
|
bool operator== (const Vector3& vector) const;
|
|
|
|
|
|
|
|
// Overloaded operator for the is different condition
|
|
|
|
bool operator!= (const Vector3& vector) const;
|
|
|
|
|
|
|
|
// Overloaded operator for addition with assignment
|
|
|
|
Vector3& operator+=(const Vector3& vector);
|
|
|
|
|
|
|
|
// Overloaded operator for substraction with assignment
|
|
|
|
Vector3& operator-=(const Vector3& vector);
|
|
|
|
|
|
|
|
// Overloaded operator for multiplication with a number with assignment
|
|
|
|
Vector3& operator*=(decimal number);
|
|
|
|
|
2013-02-16 15:14:04 +00:00
|
|
|
// Overloaded operator for division by a number with assignment
|
|
|
|
Vector3& operator/=(decimal number);
|
|
|
|
|
2012-10-09 20:21:02 +00:00
|
|
|
// Overloaded operator for value access
|
|
|
|
decimal& operator[] (int index);
|
|
|
|
|
|
|
|
// Overloaded operator for value access
|
|
|
|
const decimal& operator[] (int index) const;
|
|
|
|
|
|
|
|
// Overloaded operator
|
|
|
|
Vector3& operator=(const Vector3& vector);
|
|
|
|
|
|
|
|
// -------------------- Friends -------------------- //
|
|
|
|
|
2011-08-18 21:02:48 +00:00
|
|
|
friend Vector3 operator+(const Vector3& vector1, const Vector3& vector2);
|
|
|
|
friend Vector3 operator-(const Vector3& vector1, const Vector3& vector2);
|
|
|
|
friend Vector3 operator-(const Vector3& vector);
|
2012-01-18 23:06:33 +00:00
|
|
|
friend Vector3 operator*(const Vector3& vector, decimal number);
|
|
|
|
friend Vector3 operator*(decimal number, const Vector3& vector);
|
2013-01-27 09:38:41 +00:00
|
|
|
friend Vector3 operator/(const Vector3& vector, decimal number);
|
2011-08-18 21:02:48 +00:00
|
|
|
};
|
|
|
|
|
2013-02-27 21:10:10 +00:00
|
|
|
// Set all the values of the vector
|
|
|
|
inline void Vector3::setAllValues(decimal newX, decimal newY, decimal newZ) {
|
|
|
|
x = newX;
|
|
|
|
y = newY;
|
|
|
|
z = newZ;
|
2011-08-18 21:02:48 +00:00
|
|
|
}
|
|
|
|
|
2013-02-27 21:10:10 +00:00
|
|
|
// Return the length of the vector
|
2012-01-18 23:06:33 +00:00
|
|
|
inline decimal Vector3::length() const {
|
2013-02-27 21:10:10 +00:00
|
|
|
return sqrt(x*x + y*y + z*z);
|
2011-08-18 21:02:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return the square of the length of the vector
|
2012-01-18 23:06:33 +00:00
|
|
|
inline decimal Vector3::lengthSquare() const {
|
2013-02-27 21:10:10 +00:00
|
|
|
return x*x + y*y + z*z;
|
2011-08-18 21:02:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Scalar product of two vectors (inline)
|
2012-01-18 23:06:33 +00:00
|
|
|
inline decimal Vector3::dot(const Vector3& vector) const {
|
2013-02-27 21:10:10 +00:00
|
|
|
return (x*vector.x + y*vector.y + z*vector.z);
|
2011-08-18 21:02:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Cross product of two vectors (inline)
|
|
|
|
inline Vector3 Vector3::cross(const Vector3& vector) const {
|
2013-02-27 21:10:10 +00:00
|
|
|
return Vector3(y * vector.z - z * vector.y,
|
|
|
|
z * vector.x - x * vector.z,
|
|
|
|
x * vector.y - y * vector.x);
|
2011-08-18 21:02:48 +00:00
|
|
|
}
|
|
|
|
|
2013-02-16 15:14:04 +00:00
|
|
|
// Normalize the vector
|
|
|
|
inline void Vector3::normalize() {
|
|
|
|
decimal l = length();
|
2013-02-27 21:10:10 +00:00
|
|
|
assert(l > std::numeric_limits<decimal>::epsilon());
|
|
|
|
x /= l;
|
|
|
|
y /= l;
|
|
|
|
z /= l;
|
2013-02-16 15:14:04 +00:00
|
|
|
}
|
|
|
|
|
2011-08-18 21:02:48 +00:00
|
|
|
// Return the corresponding absolute value vector
|
|
|
|
inline Vector3 Vector3::getAbsoluteVector() const {
|
2013-02-27 21:10:10 +00:00
|
|
|
return Vector3(std::abs(x), std::abs(y), std::abs(z));
|
2011-08-18 21:02:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return true if two vectors are parallel
|
|
|
|
inline bool Vector3::isParallelWith(const Vector3& vector) const {
|
2012-01-18 23:06:33 +00:00
|
|
|
decimal scalarProd = this->dot(vector);
|
2011-08-18 21:02:48 +00:00
|
|
|
return approxEqual(std::abs(scalarProd), length() * vector.length());
|
2011-10-18 22:03:05 +00:00
|
|
|
}
|
|
|
|
|
2011-08-18 21:02:48 +00:00
|
|
|
|
|
|
|
// Return the axis with the minimal value
|
|
|
|
inline int Vector3::getMinAxis() const {
|
2013-02-27 21:10:10 +00:00
|
|
|
return (x < y ? (x < z ? 0 : 2) : (y < z ? 1 : 2));
|
2011-08-18 21:02:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return the axis with the maximal value
|
|
|
|
inline int Vector3::getMaxAxis() const {
|
2013-02-27 21:10:10 +00:00
|
|
|
return (x < y ? (y < z ? 2 : 1) : (x < z ? 2 : 0));
|
2011-08-18 21:02:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return true if the vector is unit and false otherwise
|
|
|
|
inline bool Vector3::isUnit() const {
|
2013-02-27 21:10:10 +00:00
|
|
|
return approxEqual(lengthSquare(), 1.0);
|
2011-08-18 21:02:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return true if the vector is the zero vector
|
|
|
|
inline bool Vector3::isZero() const {
|
2013-02-27 21:10:10 +00:00
|
|
|
return approxEqual(lengthSquare(), 0.0);
|
2011-08-18 21:02:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Overloaded operator for the equality condition
|
|
|
|
inline bool Vector3::operator== (const Vector3& vector) const {
|
2013-02-27 21:10:10 +00:00
|
|
|
return (x == vector.x && y == vector.y && z == vector.z);
|
2011-08-18 21:02:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Overloaded operator for the is different condition
|
|
|
|
inline bool Vector3::operator!= (const Vector3& vector) const {
|
|
|
|
return !(*this == vector);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Overloaded operator for addition with assignment
|
|
|
|
inline Vector3& Vector3::operator+=(const Vector3& vector) {
|
2013-02-27 21:10:10 +00:00
|
|
|
x += vector.x;
|
|
|
|
y += vector.y;
|
|
|
|
z += vector.z;
|
2011-08-18 21:02:48 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Overloaded operator for substraction with assignment
|
|
|
|
inline Vector3& Vector3::operator-=(const Vector3& vector) {
|
2013-02-27 21:10:10 +00:00
|
|
|
x -= vector.x;
|
|
|
|
y -= vector.y;
|
|
|
|
z -= vector.z;
|
2011-08-18 21:02:48 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Overloaded operator for multiplication with a number with assignment
|
2012-01-18 23:06:33 +00:00
|
|
|
inline Vector3& Vector3::operator*=(decimal number) {
|
2013-02-27 21:10:10 +00:00
|
|
|
x *= number;
|
|
|
|
y *= number;
|
|
|
|
z *= number;
|
2011-08-18 21:02:48 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2013-02-16 15:14:04 +00:00
|
|
|
// Overloaded operator for division by a number with assignment
|
|
|
|
inline Vector3& Vector3::operator/=(decimal number) {
|
2013-02-27 21:10:10 +00:00
|
|
|
assert(number > std::numeric_limits<decimal>::epsilon());
|
|
|
|
x /= number;
|
|
|
|
y /= number;
|
|
|
|
z /= number;
|
2013-02-16 15:14:04 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2011-08-18 21:02:48 +00:00
|
|
|
// Overloaded operator for value access
|
2012-01-18 23:06:33 +00:00
|
|
|
inline decimal& Vector3::operator[] (int index) {
|
2013-02-27 21:10:10 +00:00
|
|
|
return (&x)[index];
|
2011-08-18 21:02:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Overloaded operator for value access
|
2012-01-18 23:06:33 +00:00
|
|
|
inline const decimal& Vector3::operator[] (int index) const {
|
2013-02-27 21:10:10 +00:00
|
|
|
return (&x)[index];
|
2011-08-18 21:02:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Overloaded operator for addition
|
|
|
|
inline Vector3 operator+(const Vector3& vector1, const Vector3& vector2) {
|
2013-02-27 21:10:10 +00:00
|
|
|
return Vector3(vector1.x + vector2.x, vector1.y + vector2.y, vector1.z + vector2.z);
|
2011-08-18 21:02:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Overloaded operator for substraction
|
|
|
|
inline Vector3 operator-(const Vector3& vector1, const Vector3& vector2) {
|
2013-02-27 21:10:10 +00:00
|
|
|
return Vector3(vector1.x - vector2.x, vector1.y - vector2.y, vector1.z - vector2.z);
|
2011-08-18 21:02:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Overloaded operator for the negative of a vector
|
|
|
|
inline Vector3 operator-(const Vector3& vector) {
|
2013-02-27 21:10:10 +00:00
|
|
|
return Vector3(-vector.x, -vector.y, -vector.z);
|
2011-08-18 21:02:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Overloaded operator for multiplication with a number
|
2012-01-18 23:06:33 +00:00
|
|
|
inline Vector3 operator*(const Vector3& vector, decimal number) {
|
2013-02-27 21:10:10 +00:00
|
|
|
return Vector3(number * vector.x, number * vector.y, number * vector.z);
|
2011-08-18 21:02:48 +00:00
|
|
|
}
|
|
|
|
|
2013-01-27 09:38:41 +00:00
|
|
|
// Overloaded operator for division by a number
|
|
|
|
inline Vector3 operator/(const Vector3& vector, decimal number) {
|
2013-02-27 21:10:10 +00:00
|
|
|
assert(number > std::numeric_limits<decimal>::epsilon());
|
|
|
|
return Vector3(vector.x / number, vector.y / number, vector.z / number);
|
2013-01-27 09:38:41 +00:00
|
|
|
}
|
|
|
|
|
2011-08-18 21:02:48 +00:00
|
|
|
// Overloaded operator for multiplication with a number
|
2012-01-18 23:06:33 +00:00
|
|
|
inline Vector3 operator*(decimal number, const Vector3& vector) {
|
2011-08-18 21:02:48 +00:00
|
|
|
return vector * number;
|
|
|
|
}
|
|
|
|
|
2012-10-09 20:21:02 +00:00
|
|
|
// Assignment operator
|
|
|
|
inline Vector3& Vector3::operator=(const Vector3& vector) {
|
|
|
|
if (&vector != this) {
|
2013-02-27 21:10:10 +00:00
|
|
|
x = vector.x;
|
|
|
|
y = vector.y;
|
|
|
|
z = vector.z;
|
2012-10-09 20:21:02 +00:00
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2013-02-27 21:10:10 +00:00
|
|
|
}
|
2011-08-18 21:02:48 +00:00
|
|
|
|
|
|
|
#endif
|