[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 | |
---|
[9110] | 26 | #include <cmath> |
---|
[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 | |
---|
[2190] | 36 | //! 3D Vector |
---|
[2043] | 37 | /** |
---|
[4578] | 38 | Class to handle 3D Vectors |
---|
[2043] | 39 | */ |
---|
| 40 | class Vector { |
---|
[4476] | 41 | public: |
---|
[2043] | 42 | Vector (float x, float y, float z) : x(x), y(y), z(z) {} //!< assignment constructor |
---|
| 43 | Vector () : x(0), y(0), z(0) {} |
---|
| 44 | |
---|
[5052] | 45 | /** @param v: the Vecor to compare with this one @returns true, if the Vecors are the same, false otherwise */ |
---|
| 46 | inline bool operator== (const Vector& v) const { return (this->x==v.x&&this->y==v.y&&this->z==v.z)?true:false; }; |
---|
[8490] | 47 | /** @param v: the Vecor to compare with this one @returns true, if the Vecors are different, false otherwise */ |
---|
[8894] | 48 | inline bool operator!= (const Vector& v) const { return (this->x!=v.x||this->y!=v.y||this->z!=v.z)?true:false; }; |
---|
[10314] | 49 | inline bool operator> (const Vector& v) const { return (this->len() > v.len()); } |
---|
| 50 | inline bool operator< (const Vector& v) const { return (this->len() < v.len()); } |
---|
| 51 | |
---|
[4836] | 52 | /** @param index The index of the "array" @returns the x/y/z coordinate */ |
---|
[10660] | 53 | inline float operator[] (float index) const {if( index == 0) return this->x; if( index == 1) return this->y; if( index == 2) return this->z; else return this->x; } |
---|
[4992] | 54 | /** @param v The vector to add @returns the addition between two vectors (this + v) */ |
---|
[4476] | 55 | inline Vector operator+ (const Vector& v) const { return Vector(x + v.x, y + v.y, z + v.z); }; |
---|
[4992] | 56 | /** @param v The vector to add @returns the addition between two vectors (this + v) */ |
---|
[4609] | 57 | inline Vector operator+ (const sVec3D& v) const { return Vector(x + v[0], y + v[1], z + v[2]); }; |
---|
[4836] | 58 | /** @param v The vector to add @returns the addition between two vectors (this += v) */ |
---|
[4476] | 59 | inline const Vector& operator+= (const Vector& v) { this->x += v.x; this->y += v.y; this->z += v.z; return *this; }; |
---|
[4836] | 60 | /** @param v The vector to substract @returns the substraction between two vectors (this - v) */ |
---|
[4609] | 61 | inline const Vector& operator+= (const sVec3D& v) { this->x += v[0]; this->y += v[1]; this->z += v[2]; return *this; }; |
---|
[4836] | 62 | /** @param v The vector to substract @returns the substraction between two vectors (this - v) */ |
---|
[3819] | 63 | inline Vector operator- (const Vector& v) const { return Vector(x - v.x, y - v.y, z - v.z); } |
---|
[4836] | 64 | /** @param v The vector to substract @returns the substraction between two vectors (this - v) */ |
---|
[4609] | 65 | inline Vector operator- (const sVec3D& v) const { return Vector(x - v[0], y - v[1], z - v[2]); } |
---|
[10655] | 66 | /** @returns the substraction between (0, 0, 0) and this (null-this) */ |
---|
| 67 | inline Vector operator- () const { return Vector(-x, -y, -z); } |
---|
[4836] | 68 | /** @param v The vector to substract @returns the substraction between two vectors (this -= v) */ |
---|
[4476] | 69 | inline const Vector& operator-= (const Vector& v) { this->x -= v.x; this->y -= v.y; this->z -= v.z; return *this; }; |
---|
[4836] | 70 | /** @param v The vector to substract @returns the substraction between two vectors (this -= v) */ |
---|
[4609] | 71 | inline const Vector& operator-= (const sVec3D& v) { this->x -= v[0]; this->y -= v[1]; this->z -= v[2]; return *this; }; |
---|
[4836] | 72 | /** @param v the second vector @returns The dotProduct between two vector (this (dot) v) */ |
---|
[4476] | 73 | inline float operator* (const Vector& v) const { return x * v.x + y * v.y + z * v.z; }; |
---|
[4836] | 74 | /** @todo strange */ |
---|
[4476] | 75 | inline const Vector& operator*= (const Vector& v) { this->x *= v.x; this->y *= v.y; this->z *= v.z; return *this; }; |
---|
[4836] | 76 | /** @param f a factor to multiply the vector with @returns the vector multiplied by f (this * f) */ |
---|
[4476] | 77 | inline Vector operator* (float f) const { return Vector(x * f, y * f, z * f); }; |
---|
[4836] | 78 | /** @param f a factor to multiply the vector with @returns the vector multiplied by f (this *= f) */ |
---|
[4476] | 79 | inline const Vector& operator*= (float f) { this->x *= f; this->y *= f; this->z *= f; return *this; }; |
---|
[4836] | 80 | /** @param f a factor to divide the vector with @returns the vector divided by f (this / f) */ |
---|
[4997] | 81 | 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] | 82 | /** @param f a factor to divide the vector with @returns the vector divided by f (this /= f) */ |
---|
[4476] | 83 | 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] | 84 | /** 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] | 85 | inline const Vector& operator= (const Vector& v) { this->x = v.x; this->y = v.y; this->z = v.z; return *this; }; |
---|
[4992] | 86 | /** copy constructor* @param v the sVec3D to assign to this vector. @returns the vector v */ |
---|
[8145] | 87 | inline const Vector& operator= (const sVec3D& v) { this->x = v[0]; this->y = v[1]; this->z = v[2]; return *this; } |
---|
| 88 | inline const Vector& operator= (const float* v) { this->x = v[0]; this->y = v[1]; this->z = v[2]; return *this; } |
---|
[10314] | 89 | |
---|
| 90 | /** this operator can be used to assign values to the vector */ |
---|
| 91 | inline void operator() (float x, float y, float z) { this->x = x; this->y = y; this->z = z;} |
---|
| 92 | |
---|
| 93 | |
---|
| 94 | |
---|
| 95 | |
---|
[4836] | 96 | /** @param v: the other vector \return the dot product of the vectors */ |
---|
[4476] | 97 | float dot (const Vector& v) const { return x*v.x+y*v.y+z*v.z; }; |
---|
[4836] | 98 | /** @param v: the corss-product partner @returns the cross-product between this and v (this (x) v) */ |
---|
[3966] | 99 | 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] | 100 | /** scales the this vector with v* @param v the vector to scale this with */ |
---|
[4476] | 101 | void scale(const Vector& v) { x *= v.x; y *= v.y; z *= v.z; }; |
---|
[4836] | 102 | /** @returns the length of the vector */ |
---|
[9110] | 103 | inline float len() const { return sqrt (x*x + y*y + z*z); } |
---|
[4992] | 104 | /** normalizes the vector */ |
---|
[5053] | 105 | 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] | 106 | Vector getNormalized() const; |
---|
[2551] | 107 | Vector abs(); |
---|
[8293] | 108 | |
---|
[7191] | 109 | /** @param toVec nears this Vector to... @param t how much to advance (0.0: not at all; 1.0: fully) */ |
---|
| 110 | inline void slerpTo(const Vector& toVec, float t) { *this + (toVec - *this) * t; }; |
---|
[3541] | 111 | |
---|
[8035] | 112 | /** @returns a Null Vector */ |
---|
| 113 | inline static const Vector& nullVector() { static Vector nullVector; return nullVector; } |
---|
[3966] | 114 | void debug() const; |
---|
[4476] | 115 | |
---|
| 116 | public: |
---|
| 117 | float x; //!< The x Coordinate of the Vector. |
---|
| 118 | float y; //!< The y Coordinate of the Vector. |
---|
| 119 | float z; //!< The z Coordinate of the Vector. |
---|
[2043] | 120 | }; |
---|
| 121 | |
---|
[4476] | 122 | /** |
---|
[4836] | 123 | * calculate the angle between two vectors in radiances |
---|
| 124 | * @param v1: a vector |
---|
| 125 | * @param v2: another vector |
---|
| 126 | * @return the angle between the vectors in radians |
---|
[4476] | 127 | */ |
---|
[10376] | 128 | inline float angleRad (const Vector& v1, const Vector& v2) { return acos( v1 * v2 / (v1.len() * v2.len())); }; |
---|
[4476] | 129 | /** |
---|
[4836] | 130 | * calculate the angle between two vectors in degrees |
---|
| 131 | * @param v1: a vector |
---|
| 132 | * @param v2: another vector |
---|
| 133 | * @return the angle between the vectors in degrees |
---|
[4476] | 134 | */ |
---|
[10376] | 135 | inline float angleDeg (const Vector& v1, const Vector& v2) { return acos( v1 * v2 / (v1.len() * v2.len())) * 180/M_PI; }; |
---|
[2043] | 136 | |
---|
[5008] | 137 | /** an easy way to create a Random Vector @param sideLength the length of the Vector (x not sqrt(x^2...)) */ |
---|
| 138 | #define VECTOR_RAND(sideLength) (Vector((float)rand()/RAND_MAX -.5, (float)rand()/RAND_MAX -.5, (float)rand()/RAND_MAX -.5) * sideLength) |
---|
[4476] | 139 | |
---|
[5982] | 140 | #endif /* __VECTOR_H_ */ |
---|
[4997] | 141 | |
---|