2010-09-09 19:41:14 +00:00
|
|
|
/********************************************************************************
|
|
|
|
* ReactPhysics3D physics library, http://code.google.com/p/reactphysics3d/ *
|
|
|
|
* Copyright (c) 2010 Daniel Chappuis *
|
|
|
|
*********************************************************************************
|
|
|
|
* *
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy *
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal *
|
|
|
|
* in the Software without restriction, including without limitation the rights *
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell *
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is *
|
|
|
|
* furnished to do so, subject to the following conditions: *
|
|
|
|
* *
|
|
|
|
* The above copyright notice and this permission notice shall be included in *
|
|
|
|
* all copies or substantial portions of the Software. *
|
|
|
|
* *
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, *
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN *
|
|
|
|
* THE SOFTWARE. *
|
|
|
|
********************************************************************************/
|
2009-02-04 14:51:42 +00:00
|
|
|
|
|
|
|
#ifndef VECTOR3D_H
|
|
|
|
#define VECTOR3D_H
|
|
|
|
|
|
|
|
// Libraries
|
|
|
|
#include <cmath>
|
|
|
|
#include "exceptions.h"
|
2009-12-23 10:07:16 +00:00
|
|
|
#include "mathematics_functions.h"
|
2009-02-04 14:51:42 +00:00
|
|
|
|
2010-08-18 21:04:50 +00:00
|
|
|
// TODO : Remove the methods getX(), getY(), getZ(), setX(), setY(), setZ() and replace the attributes
|
|
|
|
// x, y and z by an array values[3]
|
|
|
|
|
2010-04-14 21:53:06 +00:00
|
|
|
|
2009-02-04 14:51:42 +00:00
|
|
|
// ReactPhysics3D namespace
|
|
|
|
namespace reactphysics3d {
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------
|
|
|
|
Class Vector3D :
|
|
|
|
This classrepresents 3 dimensionnal vector in space.
|
|
|
|
-------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
class Vector3D {
|
|
|
|
private :
|
|
|
|
double x; // X component of the vector
|
|
|
|
double y; // Y component of the vector
|
|
|
|
double z; // Z component of the vector
|
|
|
|
|
|
|
|
public :
|
|
|
|
Vector3D(); // Constructor of the class Vector3D
|
|
|
|
Vector3D(double x, double y, double z); // Constructor with arguments
|
|
|
|
Vector3D(const Vector3D& vector); // Copy-constructor
|
|
|
|
virtual ~Vector3D(); // Destructor
|
2010-08-18 21:04:50 +00:00
|
|
|
double getValue(int index) const throw(std::invalid_argument); // Get a component of the vector
|
|
|
|
void setValue(int index, double value) throw(std::invalid_argument); // Set a component of the vector
|
2009-02-04 14:51:42 +00:00
|
|
|
double getX() const; // Get the x component of the vector
|
|
|
|
double getY() const; // Get the y component of the vector
|
|
|
|
double getZ() const; // Get the z component of the vector
|
|
|
|
void setX(double x); // Set the x component of the vector
|
|
|
|
void setY(double y); // Set the y component of the vector
|
|
|
|
void setZ(double z); // Set the z component of the vector
|
|
|
|
void setAllValues(double x, double y, double z); // Set all the values of the vector
|
|
|
|
double length() const; // Return the lenght of the vector
|
|
|
|
Vector3D getUnit() const throw(MathematicsException); // Return the corresponding unit vector
|
2010-03-28 21:55:15 +00:00
|
|
|
bool isUnit() const; // Return true if the vector is unit and false otherwise
|
|
|
|
bool isZero() const; // Return true if the current vector is the zero vector
|
2009-11-29 10:54:35 +00:00
|
|
|
Vector3D getOpposite() const; // Return the vector in the opposite direction
|
2010-03-28 21:55:15 +00:00
|
|
|
Vector3D getOneOrthogonalVector() const; // Return one unit orthogonal vectors of the current vector
|
2009-02-04 14:51:42 +00:00
|
|
|
double scalarProduct(const Vector3D& vector) const; // Scalar product of two vectors
|
|
|
|
Vector3D crossProduct(const Vector3D& vector) const; // Cross product of two vectors
|
2009-12-22 16:23:09 +00:00
|
|
|
bool isParallelWith(const Vector3D& vector) const; // Return true if two vectors are parallel
|
2009-02-04 14:51:42 +00:00
|
|
|
|
|
|
|
// --- Overloaded operators --- //
|
|
|
|
Vector3D operator+(const Vector3D& vector) const; // Overloaded operator for addition
|
|
|
|
Vector3D operator-(const Vector3D& vector) const ; // Overloaded operator for substraction
|
|
|
|
Vector3D operator*(double number) const; // Overloaded operator for multiplication with a number
|
|
|
|
Vector3D& operator=(const Vector3D& vector); // Overloaded operator for the assignement to a Vector
|
|
|
|
bool operator==(const Vector3D& vector) const; // Overloaded operator for the equality condition
|
|
|
|
};
|
|
|
|
|
|
|
|
// Get the x component of the vector (inline)
|
|
|
|
inline double Vector3D::getX() const {
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the y component of the vector (inline)
|
|
|
|
inline double Vector3D::getY() const {
|
|
|
|
return y;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the z component of the vector (inline)
|
|
|
|
inline double Vector3D::getZ() const {
|
|
|
|
return z;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the x component of the vector (inline)
|
|
|
|
inline void Vector3D::setX(double x) {
|
|
|
|
this->x = x;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the y component of the vector (inline)
|
|
|
|
inline void Vector3D::setY(double y) {
|
|
|
|
this->y = y;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the z component of the vector (inline)
|
|
|
|
inline void Vector3D::setZ(double z) {
|
|
|
|
this->z = z;
|
|
|
|
}
|
|
|
|
|
2010-08-18 21:04:50 +00:00
|
|
|
// Get a component of the vector
|
|
|
|
inline double Vector3D::getValue(int index) const throw(std::invalid_argument) {
|
|
|
|
switch(index) {
|
|
|
|
case 0: return x;
|
|
|
|
case 1: return y;
|
|
|
|
case 2: return z;
|
|
|
|
default: // Throw an exception because of the wrong argument
|
|
|
|
throw std::invalid_argument("The argument is outside the bounds of the Vector3D");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set a component of the vector
|
|
|
|
inline void Vector3D::setValue(int index, double value) throw(std::invalid_argument) {
|
|
|
|
switch(index) {
|
|
|
|
case 0: x = value;
|
|
|
|
case 1: y = value;
|
|
|
|
case 2: z = value;
|
|
|
|
default: // Throw an exception because of the wrong argument
|
|
|
|
throw std::invalid_argument("The argument is outside the bounds of the Vector3D");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-04 14:51:42 +00:00
|
|
|
// Set all the values of the vector (inline)
|
|
|
|
inline void Vector3D::setAllValues(double x, double y, double z) {
|
|
|
|
this->x = x;
|
|
|
|
this->y = y;
|
|
|
|
this->z = z;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the length of the vector (inline)
|
|
|
|
inline double Vector3D::length() const {
|
|
|
|
// Compute and return the length of the vector
|
|
|
|
return sqrt(x*x + y*y + z*z);
|
|
|
|
}
|
|
|
|
|
2009-11-29 10:54:35 +00:00
|
|
|
// Return the vector in the opposite direction
|
|
|
|
inline Vector3D Vector3D::getOpposite() const {
|
|
|
|
return (Vector3D(0.0, 0.0, 0.0) - *this);
|
|
|
|
}
|
|
|
|
|
2009-02-04 14:51:42 +00:00
|
|
|
// Scalar product of two vectors (inline)
|
|
|
|
inline double Vector3D::scalarProduct(const Vector3D& vector) const {
|
|
|
|
// Compute and return the result of the scalar product
|
|
|
|
return (x * vector.x + y * vector.y + z * vector.z);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cross product of two vectors (inline)
|
|
|
|
inline Vector3D Vector3D::crossProduct(const Vector3D& vector) const {
|
|
|
|
// Compute and return the cross product
|
|
|
|
return Vector3D(y * vector.z - z * vector.y, z * vector.x - x * vector.z , x * vector.y - y * vector.x);
|
|
|
|
}
|
|
|
|
|
2009-12-22 16:23:09 +00:00
|
|
|
// Return true if two vectors are parallel
|
|
|
|
inline bool Vector3D::isParallelWith(const Vector3D& vector) const {
|
2010-06-16 21:04:36 +00:00
|
|
|
double scalarProd = this->scalarProduct(vector);
|
|
|
|
return approxEqual(std::abs(scalarProd), length() * vector.length());
|
2009-12-22 16:23:09 +00:00
|
|
|
}
|
|
|
|
|
2010-03-28 21:55:15 +00:00
|
|
|
// Return true if the vector is unit and false otherwise
|
|
|
|
inline bool Vector3D::isUnit() const {
|
|
|
|
return approxEqual(x*x+y*y+z*z, 1.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return true if the vector is the zero vector
|
|
|
|
inline bool Vector3D::isZero() const {
|
|
|
|
return approxEqual(x*x+y*y+z*z, 0.0);
|
|
|
|
}
|
|
|
|
|
2009-02-04 14:51:42 +00:00
|
|
|
// Overloaded operator for multiplication between a number and a Vector3D (inline)
|
|
|
|
inline Vector3D operator * (double number, const Vector3D& vector) {
|
|
|
|
// Compute and return the result vector
|
|
|
|
return vector * number;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Overloaded operator for the equality condition
|
|
|
|
inline bool Vector3D::operator == (const Vector3D& vector) const {
|
|
|
|
return (x == vector.x && y == vector.y && z == vector.z);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // End of the ReactPhysics3D namespace
|
|
|
|
|
|
|
|
#endif
|