File: vec.h

package info (click to toggle)
sofa-framework 1.0~beta4-9
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 88,688 kB
  • ctags: 27,205
  • sloc: cpp: 151,126; ansic: 2,387; xml: 581; sh: 417; makefile: 67
file content (494 lines) | stat: -rw-r--r-- 11,484 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/******* COPYRIGHT ************************************************
*                                                                 *
*                             FlowVR                              *
*                       Template Library                          *
*                                                                 *
*-----------------------------------------------------------------*
* COPYRIGHT (C) 20054 by                                          *
* Laboratoire Informatique et Distribution (UMR5132) and          *
* INRIA Project MOVI. ALL RIGHTS RESERVED.                        *
*                                                                 *
* This source is covered by the GNU LGPL, please refer to the     *
* COPYING file for further information.                           *
*                                                                 *
*-----------------------------------------------------------------*
*                                                                 *
*  Original Contributors:                                         *
*    Jeremie Allard,                                              *
*    Clement Menier.                                              *
*                                                                 * 
*******************************************************************
*                                                                 *
* File: include/ftl/vec.h                                         *
*                                                                 *
* Contacts: 20/09/2005 Clement Menier <clement.menier.fr>         *
*                                                                 *
******************************************************************/
#ifndef FTL_VEC_H
#define FTL_VEC_H

#include <math.h>
#include "fixed_array.h"
#include <boost/static_assert.hpp>

#include "type.h"

#include <iostream>

namespace ftl
{

template <int N, typename real=float>
class Vec : public fixed_array<real,N>
{
public:

  static Type::Type getType() { return (Type::Type)Type::vector(Type::get(real()),N); }

  /// Default constructor: sets all values to 0.
  Vec()
  {
    this->assign(0);
  }

/*
  Vec(real r1)
  {
    BOOST_STATIC_ASSERT(N == 1);
    this->elems[0]=r1;
  }
*/

  /// Specific constructor for 2-elements vectors.
  Vec(real r1, real r2)
  {
    BOOST_STATIC_ASSERT(N == 2);
    this->elems[0]=r1;
    this->elems[1]=r2;
  }

  /// Specific constructor for 3-elements vectors.
  Vec(real r1, real r2, real r3)
  {
    BOOST_STATIC_ASSERT(N == 3);
    this->elems[0]=r1;
    this->elems[1]=r2;
    this->elems[2]=r3;
  }

  /// Specific constructor for 4-elements vectors.
  Vec(real r1, real r2, real r3, real r4)
  {
    BOOST_STATIC_ASSERT(N == 4);
    this->elems[0]=r1;
    this->elems[1]=r2;
    this->elems[2]=r3;
    this->elems[3]=r4;
  }

  /// Specific constructor for 5-elements vectors.
  Vec(real r1, real r2, real r3, real r4, real r5)
  {
    BOOST_STATIC_ASSERT(N == 5);
    this->elems[0]=r1;
    this->elems[1]=r2;
    this->elems[2]=r3;
    this->elems[3]=r4;
    this->elems[4]=r5;
  }

  /// Specific constructor for 6-elements vectors (bounding-box).
  Vec(real r1, real r2, real r3, real r4, real r5, real r6)
  {
    BOOST_STATIC_ASSERT(N == 6);
    this->elems[0]=r1;
    this->elems[1]=r2;
    this->elems[2]=r3;
    this->elems[3]=r4;
    this->elems[4]=r5;
    this->elems[5]=r6;
  }

  /// Constructor from an N-1 elements vector and an additional value (added at the end).
  Vec(const Vec<N-1,real>& v, real r1)
  {
    BOOST_STATIC_ASSERT(N > 1);
    for(int i=0;i<N-1;i++)
      this->elems[i] = v[i];
    this->elems[N-1]=r1;
  }
  
  /// Constructor from an array of values.
  template<typename real2>
  explicit Vec(const real2* p)
  {
    std::copy(p, p+N, this->begin());
  }

  template<int M, typename real2>
  explicit Vec(const Vec<M, real2> &v)
  {
    std::copy(v.begin(), v.begin()+(N>M?M:N), this->begin());
  }

  /// Special access to first element.
  real& x() { BOOST_STATIC_ASSERT(N >= 1); return this->elems[0]; }
  /// Special access to second element.
  real& y() { BOOST_STATIC_ASSERT(N >= 2); return this->elems[1]; }
  /// Special access to third element.
  real& z() { BOOST_STATIC_ASSERT(N >= 3); return this->elems[2]; }
  /// Special access to fourth element.
  real& w() { BOOST_STATIC_ASSERT(N >= 4); return this->elems[3]; }

  /// Special const access to first element.
  const real& x() const { BOOST_STATIC_ASSERT(N >= 1); return this->elems[0]; }
  /// Special const access to second element.
  const real& y() const { BOOST_STATIC_ASSERT(N >= 2); return this->elems[1]; }
  /// Special const access to third element.
  const real& z() const { BOOST_STATIC_ASSERT(N >= 3); return this->elems[2]; }
  /// Special const access to fourth element.
  const real& w() const { BOOST_STATIC_ASSERT(N >= 4); return this->elems[3]; }
  
  /// Assignment operator from an array of values.
  void operator=(const real* p)
  {
    std::copy(p, p+N, this->begin());
  }

  /// Assignment from a vector with different dimensions.
template<int M, typename real2> void operator=(const Vec<M,real2>& v)
  {
    std::copy(v.begin(), v.begin()+(N>M?M:N), this->begin());
  }

  /// Sets every element to 0.
  void clear()
  {
    this->assign(0);
  }

  /// Sets every element to r.
  void fill(real r)
  {
    this->assign(r);
  }

  /// Access to i-th element.
  real& operator[](int i)
  {
    return this->elems[i];
  }

  /// Const access to i-th element.
  const real& operator[](int i) const
  {
    return this->elems[i];
  }

  /// Access to i-th element.
  real& operator()(int i)
  {
    return this->elems[i];
  }

  /// Const access to i-th element.
  const real& operator()(int i) const
  {
    return this->elems[i];
  }

  /// Cast into a const array of values.
  const real* ptr() const
  {
    return this->elems;
  }

  /// Cast into an array of values.
  real* ptr()
  {
    return this->elems;
  }

  // LINEAR ALGEBRA

  /// Multiplication by a scalar f.
  Vec<N,real> operator*(real f) const
  {
    Vec<N,real> r;
    for (int i=0;i<N;i++)
      r[i] = this->elems[i]*f;
    return r;
  }

  /// Scalar multiplication assignment operator.
  void operator*=(real f)
  {
    for (int i=0;i<N;i++)
      this->elems[i]*=f;
  }

  /// Division by a scalar f.
  Vec<N,real> operator/(real f) const
  {
    Vec<N,real> r;
    for (int i=0;i<N;i++)
      r[i] = this->elems[i]/f;
    return r;
  }

  /// On-place division by a scalar f.
  void operator/=(real f)
  {
    for (int i=0;i<N;i++)
      this->elems[i]/=f;
  }

  /// Dot product.
  real operator*(const Vec<N,real>& v) const
  {
    real r = this->elems[0]*v[0];
    for (int i=1;i<N;i++)
      r += this->elems[i]*v[i];
    return r;
  }

  /// Vector addition.
  Vec<N,real> operator+(const Vec<N,real>& v) const
  {
    Vec<N,real> r;
    for (int i=0;i<N;i++)
      r[i]=this->elems[i]+v[i];
    return r;
  }

  /// On-place vector addition.
  void operator+=(const Vec<N,real>& v)
  {
    for (int i=0;i<N;i++)
      this->elems[i]+=v[i];
  }

  /// Vector subtraction.
  Vec<N,real> operator-(const Vec<N,real>& v) const
  {
    Vec<N,real> r;
    for (int i=0;i<N;i++)
      r[i]=this->elems[i]-v[i];
    return r;
  }
  
  /// On-place vector subtraction.
  void operator-=(const Vec<N,real>& v)
  {
    for (int i=0;i<N;i++)
      this->elems[i]-=v[i];
  }

  /// Vector negation.
  Vec<N,real> operator-() const
  {
    Vec<N,real> r;
    for (int i=0;i<N;i++)
      r[i]=-this->elems[i];
    return r;
  }

  /// Squared norm.
  real norm2() const
  {
    real r = this->elems[0]*this->elems[0];
    for (int i=1;i<N;i++)
      r += this->elems[i]*this->elems[i];
    return r;
  }

  /// Euclidean norm.
  real norm() const
  {
    return sqrt(norm2());
  }

  /// Normalize the vector.
  void normalize()
  {
    real r = norm();
    if (r>1e-10)
    for (int i=0;i<N;i++)
      this->elems[i]/=r;
  }

};

/// Cross product for 3-elements vectors.
template<typename real>
inline Vec<3,real> cross(const Vec<3,real>& a, const Vec<3,real>& b)
{
  return Vec<3,real>(a.y()*b.z() - a.z()*b.y(),
		     a.z()*b.x() - a.x()*b.z(),
		     a.x()*b.y() - a.y()*b.x());
}

/// Dot product (alias for operator*)
template<int N,typename real>
inline real dot(const Vec<N,real>& a, const Vec<N,real>& b)
{
  return a*b;
}

// Definition of VecXY (X = 2,3,4) and Y = (b = byte, i = int, f = float, d = double).
typedef Vec<2,unsigned char> Vec2b;
typedef Vec<2,int> Vec2i;
typedef Vec<2,float> Vec2f;
typedef Vec<2,double> Vec2d;

typedef Vec<3,unsigned char> Vec3b;
typedef Vec<3,int> Vec3i;
typedef Vec<3,float> Vec3f;
typedef Vec<3,double> Vec3d;

typedef Vec<4,unsigned char> Vec4b;
typedef Vec<4,int> Vec4i;
typedef Vec<4,float> Vec4f;
typedef Vec<4,double> Vec4d;

// Typed data

namespace Type
{

//template<int N, class real>
//inline Type get(const Vec<N,real>& v)
//{
//  return (Type)vector(get(v[0]),N);
//}

/// Assign a vector from a string
template<int N, class real>
bool assignString(Vec<N,real>& dest, const std::string& data)
{
  const char* src = data.c_str();
  const char* end = src+data.length();
  bool res = true;
  int n = 0;
  while (n < N && src < end)
  {
    if (*src==' ' || *src==',') ++src;
    else
    {
      const char* s0 = src;
      if (n==0 && *s0 == '{') ++s0;
      do
	++src;
      while (src < end && *src != ' ' && *src != ',');
      int s = src-s0;
      if (src==end && src[-1]=='}') --s;
      if (s>0)
      {
	std::string str (s0, s0+s);
#ifdef DEBUG
	std::cerr << "dest["<<n<<"]=\""<<str<<"\""<<std::endl;
#endif
	res &= assign(dest[n], buildString(s), s0);
	++n;
      }
    }
  }
  return res;
}

template <int N, typename real>
class Assign< Vec<N,real> >
{
public:
static bool do_assign(Vec<N,real>& dest, int type, const void* data)
{
  if (isString(type))
  {
    return assignString(dest, std::string((const char*) data, size(type)));
  }
  else if (isVector(type))
  {
    int eSize = elemSize(type);
    int eType = toSingle(type);
    int n = nx(type);
    if (n>N) n=N;
    bool res = true;
    for (int i=0;i<n;i++)
      res &= assign(dest[i],eType,((const char*)data)+eSize*i);
    return res;
  }
  else
  {
    // Default implementation: read one value and fill the vector with it
    real r;
    if (!assign(r,type,data)) return false;
    dest.fill(r);
    return true;
  }
}
};

} // namespace Type

} // namespace ftl

// iostream

template <int N, typename real>
std::ostream& operator<<(std::ostream& o, const ftl::Vec<N,real>& v)
{
  o << '<' << v[0];
  for (int i=1; i<N; i++)
    o << ',' << v[i];
  o << '>';
  return o;
}

template <int N, typename real>
std::ostream& operator<<(std::ostream& o, ftl::Vec<N,real>& v)
{
  o << '<' << v[0];
  for (int i=1; i<N; i++)
    o << ',' << v[i];
  o << '>';
  return o;
}

template <int N, typename real>
std::istream& operator>>(std::istream& in, ftl::Vec<N,real>& v)
{
  int c;
  c = in.peek();
  while (c==' ' || c=='\n' || c=='<')
  {
    in.get();
    c = in.peek();
  }
  in >> v[0];
  for (int i=1; i<N; i++)
  {
    c = in.peek();
    while (c==' ' || c==',')
    {
      in.get();
      c = in.peek();
    }
    in >> v[i];
  }
  c = in.peek();
  while (c==' ' || c=='\n' || c=='>')
  {
    in.get();
    c = in.peek();
  }
  return in;
}

/// Scalar multiplication operator.
template <int N, typename real>
ftl::Vec<N,real> operator*(real r, const ftl::Vec<N,real>& v)
{
  return v*r;
}

#endif