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 ************************************************************************/
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
) {}
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
)
51 inline const Vec
<T
> set(T x
, T y
, T z
)
53 m_x
= x
; m_y
= y
; m_z
= z
;
56 inline void get(T v
[3]) const
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
;
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
);
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
);
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();
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();
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
;
220 inline const Vec
<T
> rotate(T angle
, const Vec
<T
> &v
)
223 RotateGL(angle
, v
, 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]);
239 void draw(const Vec
<T
> &pos
, GLfloat scale
= 1.0) const
242 glTranslatef(pos
.x(), pos
.y(), pos
.z());
243 glScalef(scale
, scale
, scale
);
250 glPushAttrib(GL_ALL_ATTRIB_BITS
);
251 //glDisable(GL_DEPTH_TEST);
252 glDisable(GL_LIGHTING
);
254 glVertex3f(x(), y(), z());
264 typedef Vec
<float> Vec3f
;
265 typedef Vec
<double> Vec3d
;