Fixed some transforms, move to 3d and quaternion math in progress...
[ne.git] / src / base / vec.h
blob0b94f7b422ec7acbda3171e135918ccd4f1cd40d
1 /************************************************************************
2 This file is part of NE.
4 NE is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 NE is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with NE. If not, see <http://www.gnu.org/licenses/>.
16 ************************************************************************/
18 #ifndef VEC_H_
19 #define VEC_H_
21 #include <GL/gl.h>
22 #include <ode/ode.h>
24 //namespace Base {
26 template<typename T>
27 class Vec
29 public:
30 Vec(T v=0) : m_x(v), m_y(v), m_z(v) {}
31 Vec(T x, T y, T z) : m_x(x), m_y(y), m_z(z) {}
33 ////////////////
34 // Conversion //
35 ////////////////
37 inline static const Vec<T> FromODE(const dReal *v)
39 return Vec<T>(v[0], v[1], v[2]);
42 ///////////////////////
43 // Set/Get functions //
44 ///////////////////////
46 inline const Vec<T> set(T v)
48 m_x = m_y = m_z = v;
49 return *this;
51 inline const Vec<T> set(T x, T y, T z)
53 m_x = x; m_y = y; m_z = z;
54 return *this;
56 inline void get(T v[3]) const
58 v[0] = m_x;
59 v[1] = m_y;
60 v[2] = m_z;
63 inline T x() const { return m_x; }
64 inline T y() const { return m_y; }
65 inline T z() const { return m_z; }
67 inline const Vec<T> operator - () const
69 return Vec<T>(-m_x, -m_y, -m_z);
72 ///////////////////////
73 // Equality overload //
74 ///////////////////////
76 inline const Vec<T> operator = (const Vec<T> &q)
78 return set(q.m_x,q.m_y,q.m_z);
80 inline bool operator == (const Vec<T> &q)
82 return m_x==q.m_x && m_y==q.m_y && m_z==q.m_z;
85 /////////////////////
86 // Vector overload //
87 /////////////////////
89 inline const Vec<T> operator + (const Vec<T> &v) const
91 return Vec<T>(m_x+v.m_x, m_y+v.m_y, m_z+v.m_z);
93 inline const Vec<T> operator += (const Vec<T> &v)
95 return set(m_x+v.m_x, m_y+v.m_y, m_z+v.m_z);
98 inline const Vec<T> operator - (const Vec<T> &v) const
100 return Vec<T>(m_x-v.m_x, m_y-v.m_y, m_z-v.m_z);
102 inline const Vec<T> operator -= (const Vec<T> &v)
104 return set(m_x-v.m_x, m_y-v.m_y, m_z-v.m_z);
107 inline const Vec<T> operator * (const Vec<T> &v) const
109 return Vec<T>(m_x*v.m_x, m_y*v.m_y, m_z*v.m_z);
111 inline const Vec<T> operator *= (const Vec<T> &v)
113 return set(m_x*v.m_x, m_y*v.m_y, m_z*v.m_z);
116 inline const Vec<T> operator / (const Vec<T> &v) const
118 return Vec<T>(m_x/v.m_x, m_y/v.m_y, m_z/v.m_z);
120 inline const Vec<T> operator /= (const Vec<T> &v)
122 return set(m_x/v.m_x, m_y/v.m_y, m_z/v.m_z);
125 /////////////////////
126 // Scalar overload //
127 /////////////////////
129 inline const Vec<T> operator + (T v) const
131 return *this + Vec<T>(v);
133 inline const Vec<T> operator += (T v)
135 return *this += Vec<T>(v);
138 inline const Vec<T> operator - (T v) const
140 return *this - Vec<T>(v);
142 inline const Vec<T> &operator -= (T v)
144 return *this -= Vec<T>(v);
147 inline const Vec<T> operator * (T v) const
149 return *this * Vec<T>(v);
151 inline const Vec<T> operator *= (T v)
153 return *this *= Vec<T>(v);
156 inline const Vec<T> operator / (T v) const
158 return *this / Vec<T>(v);
160 inline const Vec<T> operator /= (T v)
162 return *this /= Vec<T>(v);
165 /////////////
166 // 3D Math //
167 /////////////
169 inline T length() const
171 return sqrt(dot(*this));
174 inline const Vec<T> unit() const
176 return *this / (T)length();
179 inline T dot(const Vec<T> &v) const
181 return m_x*v.m_x + m_y*v.m_y + m_z*v.m_z;
184 inline const Vec<T> cross(const Vec<T> &v) const
186 return Vec<T>(m_y * v.m_z - m_z * v.m_y,
187 m_z * v.m_x - m_x * v.m_z,
188 m_x * v.m_y - m_y * v.m_x);
191 // returns a rotation matrix (for OpenGL) that
192 // specifies a rotation of angle around vector v
193 inline static void RotateGL(T angle, const Vec<T> &v, GLfloat *r)
195 GLfloat c = cos(angle);
196 GLfloat s = sin(angle);
197 GLfloat t = 1.0 - c;
199 r[ 0] = t * v.x() * v.x() + c;
200 r[ 1] = t * v.x() * v.y() + s * v.z();
201 r[ 2] = t * v.x() * v.z() + s * v.y();
202 r[ 3] = 0.0;
204 r[ 4] = t * v.x() * v.y() - s * v.z();
205 r[ 5] = t * v.y() * v.y() + c;
206 r[ 6] = t * v.y() * v.z() + s * v.x();
207 r[ 7] = 0.0;
209 r[ 8] = t * v.x() * v.z() + s * v.y();
210 r[ 9] = t * v.y() * v.z() + s * v.x();
211 r[10] = t * v.z() * v.z() + c;
212 r[11] = 0.0;
214 r[12] = 0.0;
215 r[13] = 0.0;
216 r[14] = 0.0;
217 r[15] = 1.0;
220 inline const Vec<T> rotate(T angle, const Vec<T> &v)
222 GLfloat r[16];
223 RotateGL(angle, v, r);
224 return rotate(r);
227 // rotate via an OpenGL matrix
228 inline const Vec<T> rotate(const T *r)
230 return Vec<T>(m_x*r[0] + m_y*r[4] + m_z*r[8],
231 m_x*r[1] + m_y*r[5] + m_z*r[9],
232 m_x*r[2] + m_y*r[6] + m_z*r[10]);
235 ///////////////
236 // Debugging //
237 ///////////////
239 void draw(const Vec<T> &pos, GLfloat scale = 1.0) const
241 glPushMatrix();
242 glTranslatef(pos.x(), pos.y(), pos.z());
243 glScalef(scale, scale, scale);
244 draw();
245 glPopMatrix();
248 void draw() const
250 glPushAttrib(GL_ALL_ATTRIB_BITS);
251 //glDisable(GL_DEPTH_TEST);
252 glDisable(GL_LIGHTING);
253 glBegin(GL_LINES);
254 glVertex3f(x(), y(), z());
255 glVertex3f(0, 0, 0);
256 glEnd();
257 glPopAttrib();
260 protected:
261 T m_x, m_y, m_z;
264 typedef Vec<float> Vec3f;
265 typedef Vec<double> Vec3d;
269 #endif /*VEC_H_*/