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