Changeset 4476 in orxonox.OLD for orxonox/trunk/src/lib/math
- Timestamp:
- Jun 2, 2005, 4:03:50 AM (19 years ago)
- Location:
- orxonox/trunk/src/lib/math
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
orxonox/trunk/src/lib/math/vector.cc
r4372 r4476 25 25 26 26 /** 27 \brief add two vectors 28 \param v: the other vector 29 \return the sum of both vectors 30 */ 31 32 //Vector Vector::operator+ (const Vector& v) const 33 34 35 /** 36 \brief subtract a vector from another 37 \param v: the other vector 38 \return the difference between the vectors 39 */ 40 //Vector Vector::operator- (const Vector& v) const 41 42 43 /** 44 \brief calculate the dot product of two vectors 45 \param v: the other vector 46 \return the dot product of the vectors 47 */ 48 //float Vector::operator* (const Vector& v) const 49 50 51 /** 52 \brief multiply a vector with a float 53 \param f: the factor 54 \return the vector multipied by f 55 */ 56 //Vector Vector::operator* (float f) const 57 58 59 /** 60 \brief divide a vector with a float 61 \param f: the divisor 62 \return the vector divided by f 63 */ 64 Vector Vector::operator/ (float f) const 65 { 66 if( unlikely(f == 0.0)) 67 { 68 // Prevent divide by zero 69 return Vector (0,0,0); 70 } 71 return Vector(x / f, y / f, z / f); 72 } 73 74 /** 75 \brief calculate the dot product of two vectors 76 \param v: the other vector 77 \return the dot product of the vectors 78 */ 79 float Vector::dot (const Vector& v) const 80 { 81 return x*v.x+y*v.y+z*v.z; 82 } 83 84 /** 85 \brief calculate the cross product of two vectors 86 \param v: the other vector 87 \return the cross product of the vectors 88 */ 89 //Vector Vector::cross (const Vector& v) const 90 91 92 /** 93 \brief normalizes the vector to lenght 1.0 94 */ 95 //void Vector::normalize () 96 97 98 /** 99 \brief returns the voctor normalized to length 1.0 100 */ 101 27 \brief returns the this-vector normalized to length 1.0 28 */ 102 29 Vector Vector::getNormalized() const 103 30 { … … 114 41 return *this / l; 115 42 } 116 117 /**118 \brief scales this Vector with Vector v.119 \param v the vector to scale this vector with120 */121 void Vector::scale(const Vector& v)122 {123 x *= v.x;124 y *= v.y;125 z *= v.z;126 }127 128 129 /**130 \brief calculates the lenght of the vector131 \return the lenght of the vector132 */133 //float Vector::len () const134 135 136 /**137 \brief Vector is looking in the positive direction on all axes after this138 */139 Vector Vector::abs()140 {141 Vector v(fabs(x), fabs(y), fabs(z));142 return v;143 }144 145 /**146 \brief calculate the angle between two vectors in radiances147 \param v1: a vector148 \param v2: another vector149 \return the angle between the vectors in radians150 */151 float angleRad (const Vector& v1, const Vector& v2)152 {153 return acos( v1 * v2 / (v1.len() * v2.len()));154 }155 156 157 /**158 \brief calculate the angle between two vectors in degrees159 \param v1: a vector160 \param v2: another vector161 \return the angle between the vectors in degrees162 */163 float angleDeg (const Vector& v1, const Vector& v2)164 {165 float f;166 f = acos( v1 * v2 / (v1.len() * v2.len()));167 return f * 180 / PI;168 }169 170 43 171 44 /** -
orxonox/trunk/src/lib/math/vector.h
r4372 r4476 20 20 class Vector { 21 21 22 public: 23 24 float x; //!< The x Coordinate of the Vector. 25 float y; //!< The y Coordinate of the Vector. 26 float z; //!< The z Coordinate of the Vector. 27 22 23 public: 28 24 Vector (float x, float y, float z) : x(x), y(y), z(z) {} //!< assignment constructor 29 25 Vector () : x(0), y(0), z(0) {} 30 26 ~Vector () {} 31 27 32 inline Vector operator+ (const Vector& v) const { return Vector(x + v.x, y + v.y, z + v.z); } 33 inline const Vector& operator+= (const Vector& v) {this->x += v.x; this->y += v.y; this->z += v.z; return *this;} 28 /** \param v The vector to add \returns the addition between two vectors (this + v) */ 29 inline Vector operator+ (const Vector& v) const { return Vector(x + v.x, y + v.y, z + v.z); }; 30 /** \param v The vector to add \returns the addition between two vectors (this += v) */ 31 inline const Vector& operator+= (const Vector& v) { this->x += v.x; this->y += v.y; this->z += v.z; return *this; }; 32 /** \param v The vector to substract \returns the substraction between two vectors (this - v) */ 34 33 inline Vector operator- (const Vector& v) const { return Vector(x - v.x, y - v.y, z - v.z); } 35 inline const Vector& operator-= (const Vector& v) {this->x -= v.x; this->y -= v.y; this->z -= v.z; return *this;} 36 inline float operator* (const Vector& v) const { return x * v.x + y * v.y + z * v.z; } 37 inline const Vector& operator*= (const Vector& v) {this->x *= v.x; this->y *= v.y; this->z *= v.z; return *this;} 38 inline Vector operator* (float f) const { return Vector(x * f, y * f, z * f); } 39 inline const Vector& operator*= (float f) {this->x *= f; this->y *= f; this->z *= f; return *this;} 40 Vector operator/ (float f) const; 41 inline const Vector& operator/= (float f) {this->x /= f; this->y /= f; this->z /= f; return *this;} 42 inline const Vector& operator= (const Vector& v) {this->x = v.x; this->y = v.y; this->z = v.z; return *this;} 43 float dot (const Vector& v) const; 34 /** \param v The vector to substract \returns the substraction between two vectors (this -= v) */ 35 inline const Vector& operator-= (const Vector& v) { this->x -= v.x; this->y -= v.y; this->z -= v.z; return *this; }; 36 /** \param v the second vector \returns The dotProduct between two vector (this (dot) v) */ 37 inline float operator* (const Vector& v) const { return x * v.x + y * v.y + z * v.z; }; 38 /** \todo strange */ 39 inline const Vector& operator*= (const Vector& v) { this->x *= v.x; this->y *= v.y; this->z *= v.z; return *this; }; 40 /** \param f a factor to multiply the vector with \returns the vector multiplied by f (this * f) */ 41 inline Vector operator* (float f) const { return Vector(x * f, y * f, z * f); }; 42 /** \param f a factor to multiply the vector with \returns the vector multiplied by f (this *= f) */ 43 inline const Vector& operator*= (float f) { this->x *= f; this->y *= f; this->z *= f; return *this; }; 44 /** \param f a factor to divide the vector with \returns the vector divided by f (this / f) */ 45 inline Vector operator/ (float f) const {if (unlikely(f == 0.0)) return Vector(0,0,0); else return Vector(this->x / f, this->y / f, this->z / f); }; 46 /** \param f a factor to divide the vector with \returns the vector divided by f (this /= f) */ 47 inline const Vector& operator/= (float f) {if (unlikely(f == 0.0)) {this->x=0;this->y=0;this->z=0;} else {this->x /= f; this->y /= f; this->z /= f;} return *this; }; 48 /** \brief copy constructor \todo (i do not know it this is faster) \param v the vector to assign to this vector. \returns the vector v */ 49 inline const Vector& operator= (const Vector& v) { this->x = v.x; this->y = v.y; this->z = v.z; return *this; }; 50 /** \param v: the other vector \return the dot product of the vectors */ 51 float dot (const Vector& v) const { return x*v.x+y*v.y+z*v.z; }; 52 /** \param v: the corss-product partner \returns the cross-product between this and v (this (x) v) */ 44 53 inline Vector cross (const Vector& v) const { return Vector(y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x ); } 45 void scale(const Vector& v); 54 /** \param scales the this vector with v */ 55 void scale(const Vector& v) { x *= v.x; y *= v.y; z *= v.z; }; 56 /** \returns the length of the vector */ 46 57 inline float len() const { return sqrt (x*x+y*y+z*z); } 47 inline void normalize() { 58 /** \brief normalizes the vector */ 59 inline void normalize() { 48 60 float l = len(); 49 61 if( unlikely(l == 0.0)) … … 60 72 61 73 void debug() const; 62 }; 63 64 float angleDeg (const Vector& v1, const Vector& v2); 65 float angleRad (const Vector& v1, const Vector& v2); 74 75 public: 76 float x; //!< The x Coordinate of the Vector. 77 float y; //!< The y Coordinate of the Vector. 78 float z; //!< The z Coordinate of the Vector. 79 }; 80 81 /** 82 \brief calculate the angle between two vectors in radiances 83 \param v1: a vector 84 \param v2: another vector 85 \return the angle between the vectors in radians 86 */ 87 inline float angleDeg (const Vector& v1, const Vector& v2) { return acos( v1 * v2 / (v1.len() * v2.len())); }; 88 /** 89 \brief calculate the angle between two vectors in degrees 90 \param v1: a vector 91 \param v2: another vector 92 \return the angle between the vectors in degrees 93 */ 94 inline float angleRad (const Vector& v1, const Vector& v2) { return acos( v1 * v2 / (v1.len() * v2.len())) * 180/M_PI; }; 95 66 96 67 97 //! Quaternion 68 98 /** 69 99 Class to handle 3-dimensional rotation efficiently 70 100 */ 71 101 class Quaternion 72 102 { 73 103 public: 74 Vector v;//!< Imaginary Vector75 float w; //!< Real part of the number104 Vector v; //!< Imaginary Vector 105 float w; //!< Real part of the number 76 106 77 107 inline Quaternion () { w = 1; v = Vector(0,0,0); }
Note: See TracChangeset
for help on using the changeset viewer.