wvector.h
1 /********************************************************************************
2  * WorldSim -- library for robot simulations *
3  * Copyright (C) 2008-2011 Gianluca Massera <emmegian@yahoo.it> *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the Free Software *
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
18  ********************************************************************************/
19 
20 #ifndef WVECTOR_H
21 #define WVECTOR_H
22 
23 #include "worldsimconfig.h"
24 #include "qglviewer/vec.h"
25 #include <cmath>
26 
27 namespace farsa {
28 
36 template <bool Shared>
37 struct FARSA_WSIM_TEMPLATE InternalData
38 {
39  real data[4];
40 };
41 
49 template <>
50 struct FARSA_WSIM_TEMPLATE InternalData<true>
51 {
52 };
53 
68 template <bool Shared = false>
69 class FARSA_WSIM_TEMPLATE wVectorT : protected InternalData<Shared> {
70 public:
72  wVectorT();
74  template <bool OtherShared>
77  wVectorT( const wVectorT& );
79  wVectorT( const real *ptr );
81  wVectorT( real *ptr );
83  wVectorT( real x, real y, real z, real w=1.0 );
84 
86  operator qglviewer::Vec() const;
87 
89  wVectorT<false> scale( real s ) const;
93  wVectorT& normalize();
94 
96  real& operator[]( int i );
98  const real& operator[]( int i ) const;
99 
101  wVectorT<false> operator-() const;
103  const wVectorT& operator+() const;
104 
106  template <bool OtherShared>
107  wVectorT<false> operator+( const wVectorT<OtherShared> &A ) const;
109  template <bool OtherShared>
110  wVectorT<false> operator-( const wVectorT<OtherShared> &A ) const;
112  wVectorT& operator=( const wVectorT &A );
114  template <bool OtherShared>
115  wVectorT& operator=( const wVectorT<OtherShared> &A );
117  template <bool OtherShared>
118  wVectorT& operator+=( const wVectorT<OtherShared> &A );
120  template <bool OtherShared>
121  wVectorT& operator-=( const wVectorT<OtherShared> &A );
122 
124  template <bool OtherShared>
125  bool operator==( const wVectorT<OtherShared> &A ) const;
126 
128  template <bool OtherShared>
129  real operator%( const wVectorT<OtherShared> &A ) const;
130 
132  template <bool OtherShared>
133  wVectorT<false> operator*( const wVectorT<OtherShared> &B ) const;
134 
136  template <bool OtherShared>
137  wVectorT<false> compProduct( const wVectorT<OtherShared> &A ) const;
138 
140  real norm() const;
141 
143  template <bool SharedA, bool SharedB>
144  static real distance( const wVectorT<SharedA> &A, const wVectorT<SharedB> &B );
145 
151  //template <bool OtherShared>
152  wVectorT& rotateAround( wVectorT<false> axis, real theta );
153 
155  static wVectorT<false> X();
157  static wVectorT<false> Y();
159  static wVectorT<false> Z();
160 
161  real &x;
162  real &y;
163  real &z;
164  real &w;
165 };
166 
168 typedef wVectorT<false> wVector;
169 
170 template <bool Shared>
172  return wVectorT<false>(1,0,0,0);
173 }
174 
175 template <bool Shared>
177  return wVectorT<false>(0,1,0,0);
178 }
179 
180 template <bool Shared>
182  return wVectorT<false>(0,0,1,0);
183 }
184 
185 template <>
186 inline wVectorT<false>::wVectorT() :
187  x(InternalData<false>::data[0]),
188  y(InternalData<false>::data[1]),
189  z(InternalData<false>::data[2]),
190  w(InternalData<false>::data[3])
191 {
192  x = 0.0;
193  y = 0.0;
194  z = 0.0;
195  w = 1.0;
196 }
197 
198 template <>
199 template <bool OtherShared>
200 inline wVectorT<false>::wVectorT( const wVectorT<OtherShared>& src ) :
201  x(InternalData<false>::data[0]),
202  y(InternalData<false>::data[1]),
203  z(InternalData<false>::data[2]),
204  w(InternalData<false>::data[3])
205 {
206  x = src.x;
207  y = src.y;
208  z = src.z;
209  w = src.w;
210 }
211 
212 template <>
213 inline wVectorT<false>::wVectorT( const wVectorT<false>& src ) :
214  x(InternalData<false>::data[0]),
215  y(InternalData<false>::data[1]),
216  z(InternalData<false>::data[2]),
217  w(InternalData<false>::data[3])
218 {
219  x = src.x;
220  y = src.y;
221  z = src.z;
222  w = src.w;
223 }
224 
225 template <>
226 inline wVectorT<false>::wVectorT(const real *ptr) :
227  x(InternalData<false>::data[0]),
228  y(InternalData<false>::data[1]),
229  z(InternalData<false>::data[2]),
230  w(InternalData<false>::data[3])
231 {
232  x = ptr[0];
233  y = ptr[1];
234  z = ptr[2];
235  w = 1.0;
236 }
237 
238 template <bool Shared>
239 inline wVectorT<Shared>::wVectorT(real *ptr) :
240  x(InternalData<Shared>::data[0]),
241  y(InternalData<Shared>::data[1]),
242  z(InternalData<Shared>::data[2]),
243  w(InternalData<Shared>::data[3])
244 {
245  x = ptr[0];
246  y = ptr[1];
247  z = ptr[2];
248  w = 1.0;
249 }
250 
251 template <>
252 inline wVectorT<true>::wVectorT(real *ptr) :
253  x(ptr[0]),
254  y(ptr[1]),
255  z(ptr[2]),
256  w(ptr[3])
257 {
258  w = 1.0;
259 }
260 
261 template <>
262 inline wVectorT<false>::wVectorT(real _x, real _y, real _z, real _w) :
263  x(InternalData<false>::data[0]),
264  y(InternalData<false>::data[1]),
265  z(InternalData<false>::data[2]),
266  w(InternalData<false>::data[3])
267 {
268  this->x = _x;
269  this->y = _y;
270  this->z = _z;
271  this->w = _w;
272 }
273 
274 template <bool Shared>
276  return qglviewer::Vec( x, y, z );
277 }
278 
279 template <bool Shared>
280 inline real& wVectorT<Shared>::operator[](int i) {
281  return (&x)[i];
282 }
283 
284 template <bool Shared>
285 inline const real& wVectorT<Shared>::operator[](int i) const {
286  return (&x)[i];
287 }
288 
289 template <bool Shared>
290 inline wVectorT<false> wVectorT<Shared>::scale(real scale) const {
291  return wVectorT<false>(x*scale, y*scale, z*scale, w);
292 }
293 
294 template <bool Shared>
296  wVectorT& self = (*this);
297  real n = sqrt( self%self );
298  self.x /= n;
299  self.y /= n;
300  self.z /= n;
301  self.w = 0.0;
302  return self;
303 }
304 
305 template <bool Shared>
307  return wVectorT<false>( -x, -y, -z, w );
308 }
309 
310 template <bool Shared>
312  return (*this);
313 }
314 
315 template <bool Shared>
316 template <bool OtherShared>
318  return wVectorT<false>(x+B.x, y+B.y, z+B.z, w);
319 }
320 
321 template <bool Shared>
322 template <bool OtherShared>
324  return wVectorT<false>(x-A.x, y-A.y, z-A.z, w);
325 }
326 
327 template <bool Shared>
329 {
330  x = A.x;
331  y = A.y;
332  z = A.z;
333  w = A.w;
334  return *this;
335 }
336 
337 template <bool Shared>
338 template <bool OtherShared>
340  x = A.x;
341  y = A.y;
342  z = A.z;
343  w = A.w;
344  return *this;
345 }
346 
347 template <bool Shared>
348 template <bool OtherShared>
350  x += A.x;
351  y += A.y;
352  z += A.z;
353  w = 1.0;
354  return *this;
355 }
356 
357 template <bool Shared>
358 template <bool OtherShared>
360  x -= A.x;
361  y -= A.y;
362  z -= A.z;
363  w = 1.0;
364  return *this;
365 }
366 
367 template <bool Shared>
368 template <bool OtherShared>
370  return ( x==A.x && y==A.y && z==A.z );
371 }
372 
373 template <bool Shared>
374 template <bool OtherShared>
376  return x*A.x + y*A.y + z*A.z;
377 }
378 
379 template <bool Shared>
380 template <bool OtherShared>
382  return wVectorT<false>(
383  y*B.z - z*B.y,
384  z*B.x - x*B.z,
385  x*B.y - y*B.x,
386  w);
387 }
388 
389 template <bool Shared>
390 template <bool OtherShared>
392  return wVectorT<false>(x*A.x, y*A.y, z*A.z, A.w);
393 }
394 
395 template <bool Shared>
396 inline real wVectorT<Shared>::norm() const {
397  return sqrt( x*x + y*y + z*z );
398 }
399 
400 template <bool Shared>
401 template <bool SharedA, bool SharedB>
403  return sqrt( (A.x-B.x)*(A.x-B.x) + (A.y-B.y)*(A.y-B.y) + (A.z-B.z)*(A.z-B.z) );
404 }
405 
406 template <bool Shared>
407 //template <bool OtherShared>
409  wVector q(0,0,0);
410  double costheta, sintheta;
411  axis.normalize();
412  costheta = cos(theta);
413  sintheta = sin(theta);
414  q.x += (costheta + (1 - costheta) * axis.x * axis.x) * x;
415  q.x += ((1 - costheta) * axis.x * axis.y - axis.z * sintheta) * y;
416  q.x += ((1 - costheta) * axis.x * axis.z + axis.y * sintheta) * z;
417 
418  q.y += ((1 - costheta) * axis.x * axis.y + axis.z * sintheta) * x;
419  q.y += (costheta + (1 - costheta) * axis.y * axis.y) * y;
420  q.y += ((1 - costheta) * axis.y * axis.z - axis.x * sintheta) * z;
421 
422  q.z += ((1 - costheta) * axis.x * axis.z - axis.y * sintheta) * x;
423  q.z += ((1 - costheta) * axis.y * axis.z + axis.x * sintheta) * y;
424  q.z += (costheta + (1 - costheta) * axis.z * axis.z) * z;
425  x = q.x;
426  y = q.y;
427  z = q.z;
428  return (*this);
429 }
430 } // end namespace farsa
431 
432 #endif