[5420] | 1 | /* |
---|
| 2 | orxonox - the future of 3D-vertical-scrollers |
---|
| 3 | |
---|
| 4 | Copyright (C) 2004 orx |
---|
| 5 | |
---|
| 6 | This program is free software; you can redistribute it and/or modify |
---|
| 7 | it under the terms of the GNU General Public License as published by |
---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 9 | any later version. |
---|
| 10 | |
---|
| 11 | ### File Specific: |
---|
| 12 | main-programmer: Christian Meyer |
---|
| 13 | co-programmer: ... |
---|
| 14 | */ |
---|
| 15 | |
---|
[4578] | 16 | /*! |
---|
[5008] | 17 | * @file vector.h |
---|
| 18 | * A basic 3D math framework |
---|
| 19 | * |
---|
| 20 | * Contains classes to handle vectors, lines, rotations and planes |
---|
[4578] | 21 | */ |
---|
[2043] | 22 | |
---|
[3224] | 23 | #ifndef _VECTOR_H |
---|
| 24 | #define _VECTOR_H |
---|
[2043] | 25 | |
---|
| 26 | #include <math.h> |
---|
[3860] | 27 | #include "compiler.h" |
---|
[4545] | 28 | #include "abstract_model.h" |
---|
[3449] | 29 | //! PI the circle-constant |
---|
[2043] | 30 | #define PI 3.14159265359f |
---|
| 31 | |
---|
[2190] | 32 | //! 3D Vector |
---|
[2043] | 33 | /** |
---|
[4578] | 34 | Class to handle 3D Vectors |
---|
[2043] | 35 | */ |
---|
| 36 | class Vector { |
---|
| 37 | |
---|
| 38 | |
---|
[4476] | 39 | public: |
---|
[2043] | 40 | Vector (float x, float y, float z) : x(x), y(y), z(z) {} //!< assignment constructor |
---|
| 41 | Vector () : x(0), y(0), z(0) {} |
---|
| 42 | ~Vector () {} |
---|
| 43 | |
---|
[5052] | 44 | /** @param v: the Vecor to compare with this one @returns true, if the Vecors are the same, false otherwise */ |
---|
| 45 | inline bool operator== (const Vector& v) const { return (this->x==v.x&&this->y==v.y&&this->z==v.z)?true:false; }; |
---|
[4836] | 46 | /** @param index The index of the "array" @returns the x/y/z coordinate */ |
---|
[4562] | 47 | inline float operator[] (float index) const {if( index == 0) return this->x; if( index == 1) return this->y; if( index == 2) return this->z; } |
---|
[4992] | 48 | /** @param v The vector to add @returns the addition between two vectors (this + v) */ |
---|
[4476] | 49 | inline Vector operator+ (const Vector& v) const { return Vector(x + v.x, y + v.y, z + v.z); }; |
---|
[4992] | 50 | /** @param v The vector to add @returns the addition between two vectors (this + v) */ |
---|
[4609] | 51 | inline Vector operator+ (const sVec3D& v) const { return Vector(x + v[0], y + v[1], z + v[2]); }; |
---|
[4836] | 52 | /** @param v The vector to add @returns the addition between two vectors (this += v) */ |
---|
[4476] | 53 | inline const Vector& operator+= (const Vector& v) { this->x += v.x; this->y += v.y; this->z += v.z; return *this; }; |
---|
[4836] | 54 | /** @param v The vector to substract @returns the substraction between two vectors (this - v) */ |
---|
[4609] | 55 | inline const Vector& operator+= (const sVec3D& v) { this->x += v[0]; this->y += v[1]; this->z += v[2]; return *this; }; |
---|
[4836] | 56 | /** @param v The vector to substract @returns the substraction between two vectors (this - v) */ |
---|
[3819] | 57 | inline Vector operator- (const Vector& v) const { return Vector(x - v.x, y - v.y, z - v.z); } |
---|
[4836] | 58 | /** @param v The vector to substract @returns the substraction between two vectors (this - v) */ |
---|
[4609] | 59 | inline Vector operator- (const sVec3D& v) const { return Vector(x - v[0], y - v[1], z - v[2]); } |
---|
[4836] | 60 | /** @param v The vector to substract @returns the substraction between two vectors (this -= v) */ |
---|
[4476] | 61 | inline const Vector& operator-= (const Vector& v) { this->x -= v.x; this->y -= v.y; this->z -= v.z; return *this; }; |
---|
[4836] | 62 | /** @param v The vector to substract @returns the substraction between two vectors (this -= v) */ |
---|
[4609] | 63 | inline const Vector& operator-= (const sVec3D& v) { this->x -= v[0]; this->y -= v[1]; this->z -= v[2]; return *this; }; |
---|
[4836] | 64 | /** @param v the second vector @returns The dotProduct between two vector (this (dot) v) */ |
---|
[4476] | 65 | inline float operator* (const Vector& v) const { return x * v.x + y * v.y + z * v.z; }; |
---|
[4836] | 66 | /** @todo strange */ |
---|
[4476] | 67 | inline const Vector& operator*= (const Vector& v) { this->x *= v.x; this->y *= v.y; this->z *= v.z; return *this; }; |
---|
[4836] | 68 | /** @param f a factor to multiply the vector with @returns the vector multiplied by f (this * f) */ |
---|
[4476] | 69 | inline Vector operator* (float f) const { return Vector(x * f, y * f, z * f); }; |
---|
[4836] | 70 | /** @param f a factor to multiply the vector with @returns the vector multiplied by f (this *= f) */ |
---|
[4476] | 71 | inline const Vector& operator*= (float f) { this->x *= f; this->y *= f; this->z *= f; return *this; }; |
---|
[4836] | 72 | /** @param f a factor to divide the vector with @returns the vector divided by f (this / f) */ |
---|
[4997] | 73 | inline Vector operator/ (float f) const { return (unlikely(f == 0.0))?Vector(0,0,0):Vector(this->x / f, this->y / f, this->z / f); }; |
---|
[4836] | 74 | /** @param f a factor to divide the vector with @returns the vector divided by f (this /= f) */ |
---|
[4476] | 75 | 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; }; |
---|
[4992] | 76 | /** copy constructor @todo (i do not know it this is faster) @param v the vector to assign to this vector. @returns the vector v */ |
---|
[4476] | 77 | inline const Vector& operator= (const Vector& v) { this->x = v.x; this->y = v.y; this->z = v.z; return *this; }; |
---|
[4992] | 78 | /** copy constructor* @param v the sVec3D to assign to this vector. @returns the vector v */ |
---|
[4545] | 79 | inline const Vector& operator= (const sVec3D& v) { this->x = v[0]; this->y = v[1]; this->z = v[2]; } |
---|
[4836] | 80 | /** @param v: the other vector \return the dot product of the vectors */ |
---|
[4476] | 81 | float dot (const Vector& v) const { return x*v.x+y*v.y+z*v.z; }; |
---|
[4836] | 82 | /** @param v: the corss-product partner @returns the cross-product between this and v (this (x) v) */ |
---|
[3966] | 83 | 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 ); } |
---|
[4992] | 84 | /** scales the this vector with v* @param v the vector to scale this with */ |
---|
[4476] | 85 | void scale(const Vector& v) { x *= v.x; y *= v.y; z *= v.z; }; |
---|
[4836] | 86 | /** @returns the length of the vector */ |
---|
[3819] | 87 | inline float len() const { return sqrt (x*x+y*y+z*z); } |
---|
[4992] | 88 | /** normalizes the vector */ |
---|
[5053] | 89 | inline void normalize() { float l = len(); if( unlikely(l == 0.0))return; this->x=this->x/l; this->y=this->y/l; this->z=this->z/l; }; |
---|
[4372] | 90 | Vector getNormalized() const; |
---|
[2551] | 91 | Vector abs(); |
---|
[3541] | 92 | |
---|
[3966] | 93 | void debug() const; |
---|
[4476] | 94 | |
---|
| 95 | public: |
---|
| 96 | float x; //!< The x Coordinate of the Vector. |
---|
| 97 | float y; //!< The y Coordinate of the Vector. |
---|
| 98 | float z; //!< The z Coordinate of the Vector. |
---|
[2043] | 99 | }; |
---|
| 100 | |
---|
[4476] | 101 | /** |
---|
[4836] | 102 | * calculate the angle between two vectors in radiances |
---|
| 103 | * @param v1: a vector |
---|
| 104 | * @param v2: another vector |
---|
| 105 | * @return the angle between the vectors in radians |
---|
[4476] | 106 | */ |
---|
| 107 | inline float angleDeg (const Vector& v1, const Vector& v2) { return acos( v1 * v2 / (v1.len() * v2.len())); }; |
---|
| 108 | /** |
---|
[4836] | 109 | * calculate the angle between two vectors in degrees |
---|
| 110 | * @param v1: a vector |
---|
| 111 | * @param v2: another vector |
---|
| 112 | * @return the angle between the vectors in degrees |
---|
[4476] | 113 | */ |
---|
| 114 | inline float angleRad (const Vector& v1, const Vector& v2) { return acos( v1 * v2 / (v1.len() * v2.len())) * 180/M_PI; }; |
---|
[2043] | 115 | |
---|
[5008] | 116 | /** an easy way to create a Random Vector @param sideLength the length of the Vector (x not sqrt(x^2...)) */ |
---|
| 117 | #define VECTOR_RAND(sideLength) (Vector((float)rand()/RAND_MAX -.5, (float)rand()/RAND_MAX -.5, (float)rand()/RAND_MAX -.5) * sideLength) |
---|
[4476] | 118 | |
---|
[5008] | 119 | |
---|
[2190] | 120 | //! Quaternion |
---|
[2043] | 121 | /** |
---|
[4476] | 122 | Class to handle 3-dimensional rotation efficiently |
---|
[2190] | 123 | */ |
---|
| 124 | class Quaternion |
---|
| 125 | { |
---|
| 126 | public: |
---|
[4994] | 127 | /** creates a Default quaternion (multiplicational identity Quaternion)*/ |
---|
[3822] | 128 | inline Quaternion () { w = 1; v = Vector(0,0,0); } |
---|
[4994] | 129 | /** creates a Quaternion looking into the direction v @param v: the direction @param f: the value */ |
---|
[3971] | 130 | inline Quaternion (const Vector& v, float f) { this->w = f; this->v = v; } |
---|
[3541] | 131 | Quaternion (float m[4][4]); |
---|
[4994] | 132 | /** turns a rotation along an axis into a Quaternion @param angle: the amount of radians to rotate @param axis: the axis to rotate around */ |
---|
[3822] | 133 | inline Quaternion (float angle, const Vector& axis) { w = cos(angle/2); v = axis * sin(angle/2); } |
---|
[3541] | 134 | Quaternion (const Vector& dir, const Vector& up); |
---|
| 135 | Quaternion (float roll, float pitch, float yaw); |
---|
[5052] | 136 | |
---|
| 137 | /** @param q: the Quaternion to compare with this one. @returns true if the Quaternions are the same, false otherwise */ |
---|
| 138 | inline bool operator== (const Quaternion& q) const { return (unlikely(this->v==q.v&&this->w==q.w))?true:false; }; |
---|
[4997] | 139 | /** @param f: a real value @return a Quaternion containing the quotient */ |
---|
| 140 | inline Quaternion operator/ (const float& f) const { return (unlikely(f==0.0)) ? Quaternion() : Quaternion(this->v/f, this->w/f); }; |
---|
[4836] | 141 | /** @param f: the value to divide by @returns the quaternion devided by f (this /= f) */ |
---|
[4477] | 142 | inline const Quaternion& operator/= (const float& f) {*this = *this / f; return *this;} |
---|
[4997] | 143 | /** @param f: a real value @return a Quaternion containing the product */ |
---|
| 144 | inline Quaternion operator* (const float& f) const { return Quaternion(this->v*f, this->w*f); }; |
---|
[4836] | 145 | /** @param f: the value to multiply by @returns the quaternion multiplied by f (this *= f) */ |
---|
[4477] | 146 | inline const Quaternion& operator*= (const float& f) {*this = *this * f; return *this;} |
---|
[4999] | 147 | /** @param q: another Quaternion to rotate this by @return a quaternion that represents the first one rotated by the second one (WARUNING: this operation is not commutative! e.g. (A*B) != (B*A)) */ |
---|
| 148 | Quaternion operator* (const Quaternion& q) const { return Quaternion(Vector(this->w*q.v.x + this->v.x*q.w + this->v.y*q.v.z - this->v.z*q.v.y, |
---|
| 149 | this->w*q.v.y + this->v.y*q.w + this->v.z*q.v.x - this->v.x*q.v.z, |
---|
| 150 | this->w*q.v.z + this->v.z*q.w + this->v.x*q.v.y - this->v.y*q.v.x), |
---|
[5006] | 151 | this->w*q.w - this->v.x*q.v.x - this->v.y*q.v.y - this->v.z*q.v.z); }; |
---|
[4836] | 152 | /** @param q: the Quaternion to multiply by @returns the quaternion multiplied by q (this *= q) */ |
---|
[4997] | 153 | inline const Quaternion& operator*= (const Quaternion& q) {*this = *this * q; return *this; }; |
---|
| 154 | /** @param q the Quaternion by which to devide @returns the division from this by q (this / q) */ |
---|
| 155 | inline Quaternion operator/ (const Quaternion& q) const { return *this * q.inverse(); }; |
---|
| 156 | /** @param q the Quaternion by which to devide @returns the division from this by q (this /= q) */ |
---|
| 157 | inline const Quaternion& operator/= (const Quaternion& q) { *this = *this * q.inverse(); return *this; }; |
---|
[4836] | 158 | /** @param q the Quaternion to add to this @returns the quaternion added with q (this + q) */ |
---|
[4477] | 159 | inline Quaternion operator+ (const Quaternion& q) const { return Quaternion(q.v + v, q.w + w); }; |
---|
[4836] | 160 | /** @param q the Quaternion to add to this @returns the quaternion added with q (this += q) */ |
---|
[4477] | 161 | inline const Quaternion& operator+= (const Quaternion& q) { this->v += q.v; this->w += q.w; return *this; }; |
---|
[4836] | 162 | /** @param q the Quaternion to substrace from this @returns the quaternion substracted by q (this - q) */ |
---|
[3822] | 163 | inline Quaternion operator- (const Quaternion& q) const { return Quaternion(q.v - v, q.w - w); } |
---|
[4836] | 164 | /** @param q the Quaternion to substrace from this @returns the quaternion substracted by q (this -= q) */ |
---|
[4477] | 165 | inline const Quaternion& operator-= (const Quaternion& q) { this->v -= q.v; this->w -= q.w; return *this; }; |
---|
[4994] | 166 | /** copy constructor @param q: the Quaternion to set this to. @returns the Quaternion q (or this) */ |
---|
[3966] | 167 | inline Quaternion operator= (const Quaternion& q) {this->v = q.v; this->w = q.w; return *this;} |
---|
[4994] | 168 | /** conjugates this Quaternion @returns the conjugate */ |
---|
[4998] | 169 | inline Quaternion conjugate () const { return Quaternion(Vector(-v.x, -v.y, -v.z), this->w); }; |
---|
[4997] | 170 | /** @returns the norm of The Quaternion */ |
---|
[5000] | 171 | inline float norm () const { return sqrt(w*w + v.x*v.x + v.y*v.y + v.z*v.z); }; |
---|
[4997] | 172 | /** @returns the inverted Quaterntion of this */ |
---|
[5000] | 173 | inline Quaternion inverse () const { return conjugate() / (w*w + v.x*v.x + v.y*v.y + v.z*v.z); }; |
---|
[5419] | 174 | /** @returns the dot Product of a Quaternion */ |
---|
| 175 | inline float dot (const Quaternion& q) const { return v.x*q.v.x + v.y*q.v.y + v.z*q.v.z + w*q.w; }; |
---|
| 176 | /** @retuns the Distance between two Quaternions */ |
---|
| 177 | inline float distance(const Quaternion& q) const { return 2*acos(fabsf(this->dot(q))); }; |
---|
[4997] | 178 | /** @param v: the Vector @return a new Vector representing v rotated by the Quaternion */ |
---|
| 179 | inline Vector apply (const Vector& v) const { return (*this * Quaternion(v, 0) * conjugate()).v; }; |
---|
[3541] | 180 | void matrix (float m[4][4]) const; |
---|
[4998] | 181 | /** @returns the normalized Quaternion (|this|) */ |
---|
| 182 | inline Quaternion getNormalized() const { float n = this->norm(); return Quaternion(this->v/n, this->w/n); }; |
---|
| 183 | /** normalizes the current Quaternion */ |
---|
| 184 | inline void normalize() { float n = this->norm(); this->v /= n; this->w/=n; }; |
---|
[4578] | 185 | |
---|
[4998] | 186 | /** @returns the rotational axis of this Quaternion */ |
---|
| 187 | inline Vector getSpacialAxis() const { return this->v / sin(acos(w));/*sqrt(v.x*v.x + v.y*v.y + v.z+v.z);*/ }; |
---|
| 188 | /** @returns the rotational angle of this Quaternion around getSpacialAxis() !! IN DEGREE !! */ |
---|
[5435] | 189 | inline float getSpacialAxisAngle() const { return 360.0 / M_PI * acos( this->w ); }; |
---|
[4998] | 190 | |
---|
| 191 | static Quaternion quatSlerp(const Quaternion& from, const Quaternion& to, float t); |
---|
| 192 | |
---|
[3541] | 193 | void debug(); |
---|
[5000] | 194 | void debug2(); |
---|
[4477] | 195 | |
---|
[4998] | 196 | |
---|
[4477] | 197 | public: |
---|
| 198 | Vector v; //!< Imaginary Vector |
---|
| 199 | float w; //!< Real part of the number |
---|
| 200 | |
---|
[2190] | 201 | }; |
---|
| 202 | |
---|
[3971] | 203 | |
---|
| 204 | |
---|
| 205 | |
---|
[2190] | 206 | //! 3D rotation (OBSOLETE) |
---|
| 207 | /** |
---|
[2043] | 208 | Class to handle 3-dimensional rotations. |
---|
| 209 | Can create a rotation from several inputs, currently stores rotation using a 3x3 Matrix |
---|
| 210 | */ |
---|
| 211 | class Rotation { |
---|
| 212 | public: |
---|
[4578] | 213 | |
---|
[2043] | 214 | float m[9]; //!< 3x3 Rotation Matrix |
---|
[4578] | 215 | |
---|
[2043] | 216 | Rotation ( const Vector& v); |
---|
| 217 | Rotation ( const Vector& axis, float angle); |
---|
| 218 | Rotation ( float pitch, float yaw, float roll); |
---|
| 219 | Rotation (); |
---|
| 220 | ~Rotation () {} |
---|
[4578] | 221 | |
---|
[2190] | 222 | Rotation operator* (const Rotation& r); |
---|
[4578] | 223 | |
---|
[2190] | 224 | void glmatrix (float* buffer); |
---|
[2043] | 225 | }; |
---|
[2551] | 226 | |
---|
[2043] | 227 | //!< Apply a rotation to a vector |
---|
[3228] | 228 | Vector rotateVector( const Vector& v, const Rotation& r); |
---|
[2043] | 229 | |
---|
| 230 | //! 3D line |
---|
| 231 | /** |
---|
| 232 | Class to store Lines in 3-dimensional space |
---|
| 233 | |
---|
| 234 | Supports line-to-line distance measurements and rotation |
---|
| 235 | */ |
---|
| 236 | class Line |
---|
| 237 | { |
---|
| 238 | public: |
---|
[4578] | 239 | |
---|
[2043] | 240 | Vector r; //!< Offset |
---|
| 241 | Vector a; //!< Direction |
---|
[4578] | 242 | |
---|
[2043] | 243 | Line ( Vector r, Vector a) : r(r), a(a) {} //!< assignment constructor |
---|
| 244 | Line () : r(Vector(0,0,0)), a(Vector (1,1,1)) {} |
---|
| 245 | ~Line () {} |
---|
[4578] | 246 | |
---|
[2043] | 247 | float distance (const Line& l) const; |
---|
[3228] | 248 | float distancePoint (const Vector& v) const; |
---|
[4578] | 249 | float distancePoint (const sVec3D& v) const; |
---|
[2043] | 250 | Vector* footpoints (const Line& l) const; |
---|
| 251 | float len () const; |
---|
[4578] | 252 | |
---|
[2043] | 253 | void rotate(const Rotation& rot); |
---|
| 254 | }; |
---|
| 255 | |
---|
| 256 | //! 3D plane |
---|
| 257 | /** |
---|
| 258 | Class to handle planes in 3-dimensional space |
---|
[4578] | 259 | |
---|
[2043] | 260 | Critical for polygon-based collision detection |
---|
| 261 | */ |
---|
| 262 | class Plane |
---|
| 263 | { |
---|
| 264 | public: |
---|
[4578] | 265 | |
---|
[2043] | 266 | Vector n; //!< Normal vector |
---|
| 267 | float k; //!< Offset constant |
---|
[4578] | 268 | |
---|
[2043] | 269 | Plane (Vector a, Vector b, Vector c); |
---|
| 270 | Plane (Vector norm, Vector p); |
---|
[4611] | 271 | Plane (Vector norm, sVec3D p); |
---|
[2043] | 272 | Plane (Vector n, float k) : n(n), k(k) {} //!< assignment constructor |
---|
| 273 | Plane () : n(Vector(1,1,1)), k(0) {} |
---|
| 274 | ~Plane () {} |
---|
[4578] | 275 | |
---|
[3228] | 276 | Vector intersectLine (const Line& l) const; |
---|
| 277 | float distancePoint (const Vector& p) const; |
---|
[4585] | 278 | float distancePoint (const sVec3D& p) const; |
---|
[3228] | 279 | float locatePoint (const Vector& p) const; |
---|
[2043] | 280 | }; |
---|
| 281 | |
---|
[4845] | 282 | |
---|
| 283 | |
---|
| 284 | //! A class that represents a rectangle, this is needed for SpatialSeparation |
---|
| 285 | class Rectangle |
---|
| 286 | { |
---|
| 287 | |
---|
| 288 | public: |
---|
[5215] | 289 | Rectangle() { this->center = Vector(); } |
---|
| 290 | Rectangle(const Vector ¢er, float len) { this->center = Vector(center.x, center.y, center.z); this->axis[0] = len; this->axis[1] = len; } |
---|
[4845] | 291 | virtual ~Rectangle() {} |
---|
| 292 | |
---|
| 293 | /** \brief sets the center of the rectangle to a defined vector @param center the new center */ |
---|
[5215] | 294 | inline void setCenter(const Vector ¢er) { this->center = center;} |
---|
[4845] | 295 | /** \brief sets the center of the rectangle to a defined vector @param x coord of the center @param y coord of the center @param z coord of the center */ |
---|
[5215] | 296 | inline void setCenter(float x, float y, float z) { this->center.x = x; this->center.y = y; this->center.z = z; } |
---|
[4845] | 297 | /** \brief returns the center of the rectangle to a defined vector @returns center the new center */ |
---|
[5215] | 298 | inline const Vector& getCenter() const { return this->center; } |
---|
[4845] | 299 | |
---|
| 300 | /** \brief sets both axis of the rectangle to a defined vector @param unityLength the new center */ |
---|
| 301 | inline void setAxis(float unityLength) { this->axis[0] = unityLength; this->axis[1] = unityLength; } |
---|
| 302 | /** \brief sets both axis of the rectangle to a defined vector @param v1 the length of the x axis @param v2 the length of the z axis*/ |
---|
| 303 | inline void setAxis(float v1, float v2) { this->axis[0] = v1; this->axis[1] = v2; } |
---|
[4853] | 304 | /** \brief gets one axis length of the rectangle @returns the length of the axis 0 */ |
---|
| 305 | inline float getAxis() { return this-> axis[0]; } |
---|
[4845] | 306 | |
---|
| 307 | private: |
---|
[5215] | 308 | Vector center; |
---|
[4851] | 309 | float axis[2]; |
---|
[4845] | 310 | }; |
---|
[4851] | 311 | |
---|
| 312 | |
---|
[3224] | 313 | #endif /* _VECTOR_H */ |
---|
[4997] | 314 | |
---|