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 | |
---|
16 | /*! |
---|
17 | * @file vector.h |
---|
18 | * A basic 3D math framework |
---|
19 | * |
---|
20 | * Contains classes to handle vectors, lines, rotations and planes |
---|
21 | */ |
---|
22 | |
---|
23 | #ifndef __VECTOR_H_ |
---|
24 | #define __VECTOR_H_ |
---|
25 | |
---|
26 | #include <math.h> |
---|
27 | #include "compiler.h" |
---|
28 | //! PI the circle-constant |
---|
29 | #define PI 3.14159265359f |
---|
30 | |
---|
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 | |
---|
41 | //! 3D Vector |
---|
42 | /** |
---|
43 | Class to handle 3D Vectors |
---|
44 | */ |
---|
45 | class Vector { |
---|
46 | public: |
---|
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 | |
---|
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; }; |
---|
53 | /** @param index The index of the "array" @returns the x/y/z coordinate */ |
---|
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; } |
---|
55 | /** @param v The vector to add @returns the addition between two vectors (this + v) */ |
---|
56 | inline Vector operator+ (const Vector& v) const { return Vector(x + v.x, y + v.y, z + v.z); }; |
---|
57 | /** @param v The vector to add @returns the addition between two vectors (this + v) */ |
---|
58 | inline Vector operator+ (const sVec3D& v) const { return Vector(x + v[0], y + v[1], z + v[2]); }; |
---|
59 | /** @param v The vector to add @returns the addition between two vectors (this += v) */ |
---|
60 | inline const Vector& operator+= (const Vector& v) { this->x += v.x; this->y += v.y; this->z += v.z; return *this; }; |
---|
61 | /** @param v The vector to substract @returns the substraction between two vectors (this - v) */ |
---|
62 | inline const Vector& operator+= (const sVec3D& v) { this->x += v[0]; this->y += v[1]; this->z += v[2]; return *this; }; |
---|
63 | /** @param v The vector to substract @returns the substraction between two vectors (this - v) */ |
---|
64 | inline Vector operator- (const Vector& v) const { return Vector(x - v.x, y - v.y, z - v.z); } |
---|
65 | /** @param v The vector to substract @returns the substraction between two vectors (this - v) */ |
---|
66 | inline Vector operator- (const sVec3D& v) const { return Vector(x - v[0], y - v[1], z - v[2]); } |
---|
67 | /** @param v The vector to substract @returns the substraction between two vectors (this -= v) */ |
---|
68 | inline const Vector& operator-= (const Vector& v) { this->x -= v.x; this->y -= v.y; this->z -= v.z; return *this; }; |
---|
69 | /** @param v The vector to substract @returns the substraction between two vectors (this -= v) */ |
---|
70 | inline const Vector& operator-= (const sVec3D& v) { this->x -= v[0]; this->y -= v[1]; this->z -= v[2]; return *this; }; |
---|
71 | /** @param v the second vector @returns The dotProduct between two vector (this (dot) v) */ |
---|
72 | inline float operator* (const Vector& v) const { return x * v.x + y * v.y + z * v.z; }; |
---|
73 | /** @todo strange */ |
---|
74 | inline const Vector& operator*= (const Vector& v) { this->x *= v.x; this->y *= v.y; this->z *= v.z; return *this; }; |
---|
75 | /** @param f a factor to multiply the vector with @returns the vector multiplied by f (this * f) */ |
---|
76 | inline Vector operator* (float f) const { return Vector(x * f, y * f, z * f); }; |
---|
77 | /** @param f a factor to multiply the vector with @returns the vector multiplied by f (this *= f) */ |
---|
78 | inline const Vector& operator*= (float f) { this->x *= f; this->y *= f; this->z *= f; return *this; }; |
---|
79 | /** @param f a factor to divide the vector with @returns the vector divided by f (this / f) */ |
---|
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); }; |
---|
81 | /** @param f a factor to divide the vector with @returns the vector divided by f (this /= f) */ |
---|
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; }; |
---|
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 */ |
---|
84 | inline const Vector& operator= (const Vector& v) { this->x = v.x; this->y = v.y; this->z = v.z; return *this; }; |
---|
85 | /** copy constructor* @param v the sVec3D to assign to this vector. @returns the vector v */ |
---|
86 | inline const Vector& operator= (const sVec3D& v) { this->x = v[0]; this->y = v[1]; this->z = v[2]; } |
---|
87 | /** @param v: the other vector \return the dot product of the vectors */ |
---|
88 | float dot (const Vector& v) const { return x*v.x+y*v.y+z*v.z; }; |
---|
89 | /** @param v: the corss-product partner @returns the cross-product between this and v (this (x) v) */ |
---|
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 ); } |
---|
91 | /** scales the this vector with v* @param v the vector to scale this with */ |
---|
92 | void scale(const Vector& v) { x *= v.x; y *= v.y; z *= v.z; }; |
---|
93 | /** @returns the length of the vector */ |
---|
94 | inline float len() const { return sqrt (x*x+y*y+z*z); } |
---|
95 | /** normalizes the vector */ |
---|
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; }; |
---|
97 | Vector getNormalized() const; |
---|
98 | Vector abs(); |
---|
99 | |
---|
100 | void debug() const; |
---|
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. |
---|
106 | }; |
---|
107 | |
---|
108 | /** |
---|
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 |
---|
113 | */ |
---|
114 | inline float angleDeg (const Vector& v1, const Vector& v2) { return acos( v1 * v2 / (v1.len() * v2.len())); }; |
---|
115 | /** |
---|
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 |
---|
120 | */ |
---|
121 | inline float angleRad (const Vector& v1, const Vector& v2) { return acos( v1 * v2 / (v1.len() * v2.len())) * 180/M_PI; }; |
---|
122 | |
---|
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) |
---|
125 | |
---|
126 | |
---|
127 | //! 3D rotation (OBSOLETE) |
---|
128 | /** |
---|
129 | Class to handle 3-dimensional rotations. |
---|
130 | Can create a rotation from several inputs, currently stores rotation using a 3x3 Matrix |
---|
131 | */ |
---|
132 | class Rotation { |
---|
133 | public: |
---|
134 | |
---|
135 | float m[9]; //!< 3x3 Rotation Matrix |
---|
136 | |
---|
137 | Rotation ( const Vector& v); |
---|
138 | Rotation ( const Vector& axis, float angle); |
---|
139 | Rotation ( float pitch, float yaw, float roll); |
---|
140 | Rotation (); |
---|
141 | ~Rotation () {} |
---|
142 | |
---|
143 | Rotation operator* (const Rotation& r); |
---|
144 | |
---|
145 | void glmatrix (float* buffer); |
---|
146 | }; |
---|
147 | |
---|
148 | //!< Apply a rotation to a vector |
---|
149 | Vector rotateVector( const Vector& v, const Rotation& r); |
---|
150 | |
---|
151 | //! 3D line |
---|
152 | /** |
---|
153 | Class to store Lines in 3-dimensional space |
---|
154 | |
---|
155 | Supports line-to-line distance measurements and rotation |
---|
156 | */ |
---|
157 | class Line |
---|
158 | { |
---|
159 | public: |
---|
160 | |
---|
161 | Vector r; //!< Offset |
---|
162 | Vector a; //!< Direction |
---|
163 | |
---|
164 | Line ( Vector r, Vector a) : r(r), a(a) {} //!< assignment constructor |
---|
165 | Line () : r(Vector(0,0,0)), a(Vector (1,1,1)) {} |
---|
166 | ~Line () {} |
---|
167 | |
---|
168 | float distance (const Line& l) const; |
---|
169 | float distancePoint (const Vector& v) const; |
---|
170 | float distancePoint (const sVec3D& v) const; |
---|
171 | Vector* footpoints (const Line& l) const; |
---|
172 | float len () const; |
---|
173 | |
---|
174 | void rotate(const Rotation& rot); |
---|
175 | }; |
---|
176 | |
---|
177 | //! 3D plane |
---|
178 | /** |
---|
179 | Class to handle planes in 3-dimensional space |
---|
180 | |
---|
181 | Critical for polygon-based collision detection |
---|
182 | */ |
---|
183 | class Plane |
---|
184 | { |
---|
185 | public: |
---|
186 | |
---|
187 | Vector n; //!< Normal vector |
---|
188 | float k; //!< Offset constant |
---|
189 | |
---|
190 | Plane (const Vector& a, const Vector& b, const Vector& c); |
---|
191 | Plane (const Vector& norm, const Vector& p); |
---|
192 | Plane (const Vector& norm, const sVec3D& p); |
---|
193 | Plane (const Vector& n, float k) : n(n), k(k) {} //!< assignment constructor |
---|
194 | Plane () : n(Vector(1,1,1)), k(0) {} |
---|
195 | ~Plane () {} |
---|
196 | |
---|
197 | Vector intersectLine (const Line& l) const; |
---|
198 | float distancePoint (const Vector& p) const; |
---|
199 | float distancePoint (const sVec3D& p) const; |
---|
200 | float locatePoint (const Vector& p) const; |
---|
201 | }; |
---|
202 | |
---|
203 | |
---|
204 | |
---|
205 | //! A class that represents a rectangle, this is needed for SpatialSeparation |
---|
206 | class Rectangle |
---|
207 | { |
---|
208 | |
---|
209 | public: |
---|
210 | Rectangle() { this->center = Vector(); } |
---|
211 | Rectangle(const Vector ¢er, float len) { this->center = Vector(center.x, center.y, center.z); this->axis[0] = len; this->axis[1] = len; } |
---|
212 | virtual ~Rectangle() {} |
---|
213 | |
---|
214 | /** \brief sets the center of the rectangle to a defined vector @param center the new center */ |
---|
215 | inline void setCenter(const Vector ¢er) { this->center = center;} |
---|
216 | /** \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 */ |
---|
217 | inline void setCenter(float x, float y, float z) { this->center.x = x; this->center.y = y; this->center.z = z; } |
---|
218 | /** \brief returns the center of the rectangle to a defined vector @returns center the new center */ |
---|
219 | inline const Vector& getCenter() const { return this->center; } |
---|
220 | |
---|
221 | /** \brief sets both axis of the rectangle to a defined vector @param unityLength the new center */ |
---|
222 | inline void setAxis(float unityLength) { this->axis[0] = unityLength; this->axis[1] = unityLength; } |
---|
223 | /** \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*/ |
---|
224 | inline void setAxis(float v1, float v2) { this->axis[0] = v1; this->axis[1] = v2; } |
---|
225 | /** \brief gets one axis length of the rectangle @returns the length of the axis 0 */ |
---|
226 | inline float getAxis() { return this-> axis[0]; } |
---|
227 | |
---|
228 | private: |
---|
229 | Vector center; |
---|
230 | float axis[2]; |
---|
231 | }; |
---|
232 | |
---|
233 | |
---|
234 | #endif /* __VECTOR_H_ */ |
---|
235 | |
---|