Subversion Repositories gelsvn

Rev

Rev 43 | Rev 89 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 43 Rev 45
Line 27... Line 27...
27
 
27
 
28
      The template contains no virtual functions which is important
28
      The template contains no virtual functions which is important
29
      since they add overhead.
29
      since they add overhead.
30
  */
30
  */
31
 
31
 
32
  template <class T, class V, int N> 
32
  template <class T, class V, unsigned int N> 
33
    class ArithVec
33
    class ArithVec
34
    {
34
    {
35
 
35
 
36
    protected:
36
    protected:
37
 
37
 
Line 90... Line 90...
90
	
90
	
91
      /// A more meaningful name for vector type
91
      /// A more meaningful name for vector type
92
      typedef V VectorType;
92
      typedef V VectorType;
93
 
93
 
94
      /// Return dimension of vector
94
      /// Return dimension of vector
95
      static int get_dim() {return N;}
95
      static unsigned int get_dim() {return N;}
96
	
96
	
97
      /// Set all coordinates of a 2D vector.
97
      /// Set all coordinates of a 2D vector.
98
      void set(T _a, T _b)
98
      void set(T _a, T _b)
99
	{
99
	{
100
	  assert(N==2);
100
	  assert(N==2);
Line 120... Line 120...
120
	  data[2] = _c;
120
	  data[2] = _c;
121
	  data[3] = _d;
121
	  data[3] = _d;
122
	}
122
	}
123
 
123
 
124
      /// Const index operator
124
      /// Const index operator
125
      const T& operator [] ( int i ) const
125
      const T& operator [] ( unsigned int i ) const
126
	{
126
	{
127
	  assert(i<N);
127
	  assert(i<N);
128
	  return data[i];
128
	  return data[i];
129
	}
129
	}
130
 
130
 
131
      /// Non-const index operator
131
      /// Non-const index operator
132
      T& operator [] ( int i ) 
132
      T& operator [] ( unsigned int i ) 
133
	{
133
	{
134
	  assert(i<N);
134
	  assert(i<N);
135
	  return data[i];
135
	  return data[i];
136
	}
136
	}
137
 
137
 
Line 154... Line 154...
154
	{
154
	{
155
	  return std::inner_product(data, &data[N], v.get(), true,
155
	  return std::inner_product(data, &data[N], v.get(), true,
156
				    std::logical_and<bool>(), std::equal_to<T>());
156
				    std::logical_and<bool>(), std::equal_to<T>());
157
	}
157
	}
158
 
158
 
159
#define for_all_i(expr) for(int i=0;i<N;i++) {expr;}
159
#define for_all_i(expr) for(unsigned int i=0;i<N;i++) {expr;}
160
      /// Equality wrt scalar. True if all coords are equal to scalar
160
      /// Equality wrt scalar. True if all coords are equal to scalar
161
      bool operator==(T k) const 
161
      bool operator==(T k) const 
162
	{ 
162
	{ 
163
	  for_all_i(if (data[i] != k) return false)
163
	  for_all_i(if (data[i] != k) return false)
164
	    return true;
164
	    return true;
Line 361... Line 361...
361
	  return *std::max_element(data, &data[N]);
361
	  return *std::max_element(data, &data[N]);
362
	}
362
	}
363
 
363
 
364
    };
364
    };
365
 
365
 
366
  template <class T, class V, int N> 
366
  template <class T, class V, unsigned int N> 
367
    inline std::ostream& operator<<(std::ostream&os, const ArithVec<T,V,N>& v)
367
    inline std::ostream& operator<<(std::ostream&os, const ArithVec<T,V,N>& v)
368
    {
368
    {
369
      os << "[ ";
369
      os << "[ ";
370
      for(int i=0;i<N;i++) os << v[i] << " ";
370
      for(unsigned int i=0;i<N;i++) os << v[i] << " ";
371
      os << "]";
371
      os << "]";
372
      return os;
372
      return os;
373
    }
373
    }
374
 
374
 
375
  /// Get from operator for ArithVec descendants. 
375
  /// Get from operator for ArithVec descendants. 
376
  template <class T,class V, int N>
376
  template <class T,class V, unsigned int N>
377
    inline std::istream& operator>>(std::istream&is, ArithVec<T,V,N>& v)
377
    inline std::istream& operator>>(std::istream&is, ArithVec<T,V,N>& v)
378
    {
378
    {
379
      for(int i=0;i<N;i++) is>>v[i];
379
      for(unsigned int i=0;i<N;i++) is>>v[i];
380
      return is;
380
      return is;
381
    }
381
    }
382
 
382
 
383
 
383
 
384
  /** Dot product for two vectors. The `*' operator is 
384
  /** Dot product for two vectors. The `*' operator is 
385
      reserved for coordinatewise	multiplication of vectors. */
385
      reserved for coordinatewise	multiplication of vectors. */
386
  template <class T,class V, int N>
386
  template <class T,class V, unsigned int N>
387
    inline T dot(const ArithVec<T,V,N>& v0, const ArithVec<T,V,N>& v1)
387
    inline T dot(const ArithVec<T,V,N>& v0, const ArithVec<T,V,N>& v1)
388
    {
388
    {
389
      return std::inner_product(v0.get(), v0.get() + N, v1.get(), T(0));
389
      return std::inner_product(v0.get(), v0.get() + N, v1.get(), T(0));
390
    }
390
    }
391
 
391
 
392
  /** Compute the sqr length by taking dot product of vector with itself. */
392
  /** Compute the sqr length by taking dot product of vector with itself. */
393
  template <class T,class V, int N>
393
  template <class T,class V, unsigned int N>
394
    inline T sqr_length(const ArithVec<T,V,N>& v)
394
    inline T sqr_length(const ArithVec<T,V,N>& v)
395
    {
395
    {
396
      return dot(v,v);
396
      return dot(v,v);
397
    }
397
    }
398
 
398
 
Line 410... Line 410...
410
      be automatically converted, we will be disappointed. It seems that 
410
      be automatically converted, we will be disappointed. It seems that 
411
      a float * ArithVec<float,Vec3f,3> function is not found if the
411
      a float * ArithVec<float,Vec3f,3> function is not found if the
412
      left operand is really a double.
412
      left operand is really a double.
413
  */
413
  */
414
 
414
 
415
  template<class T, class V, int N>
415
  template<class T, class V, unsigned int N>
416
    inline const V operator * (double k, const ArithVec<T,V,N>& v) 
416
    inline const V operator * (double k, const ArithVec<T,V,N>& v) 
417
    {
417
    {
418
      return v * k;
418
      return v * k;
419
    }
419
    }
420
 
420
 
421
  /** Multiply float onto vector. See the note in the documentation
421
  /** Multiply float onto vector. See the note in the documentation
422
      regarding multiplication of a double onto a vector. */
422
      regarding multiplication of a double onto a vector. */
423
  template<class T, class V, int N>
423
  template<class T, class V, unsigned int N>
424
    inline const V operator * (float k, const ArithVec<T,V,N>& v) 
424
    inline const V operator * (float k, const ArithVec<T,V,N>& v) 
425
    {
425
    {
426
      return v * k;
426
      return v * k;
427
    }
427
    }
428
 
428
 
429
  /** Multiply int onto vector. See the note in the documentation
429
  /** Multiply unsigned int onto vector. See the note in the documentation
430
      regarding multiplication of a double onto a vector. */
430
      regarding multiplication of a double onto a vector. */
431
  template<class T, class V, int N>
431
  template<class T, class V, unsigned int N>
432
    inline const V operator * (int k, const ArithVec<T,V,N>& v) 
432
    inline const V operator * (int k, const ArithVec<T,V,N>& v) 
433
    {
433
    {
434
      return v * k;
434
      return v * k;
435
    }
435
    }
436
 
436
 
437
  /** Returns the vector containing for each coordinate the smallest
437
  /** Returns the vector containing for each coordinate the smallest
438
      value from two vectors. */
438
      value from two vectors. */
439
  template <class T,class V, int N>
439
  template <class T,class V, unsigned int N>
440
    inline V v_min(const ArithVec<T,V,N>& v0, const ArithVec<T,V,N>& v1)
440
    inline V v_min(const ArithVec<T,V,N>& v0, const ArithVec<T,V,N>& v1)
441
    {
441
    {
442
      V v;
442
      V v;
443
      std::transform(v0.get(), v0.get() + N, v1.get(), v.get(), std::ptr_fun(s_min<T>));
443
      std::transform(v0.get(), v0.get() + N, v1.get(), v.get(), std::ptr_fun(s_min<T>));
444
      return v;
444
      return v;
445
    }
445
    }
446
 
446
 
447
  /** Returns the vector containing for each coordinate the largest 
447
  /** Returns the vector containing for each coordinate the largest 
448
      value from two vectors. */
448
      value from two vectors. */
449
  template <class T,class V, int N>
449
  template <class T,class V, unsigned int N>
450
    inline V v_max(const ArithVec<T,V,N>& v0, const ArithVec<T,V,N>& v1)
450
    inline V v_max(const ArithVec<T,V,N>& v0, const ArithVec<T,V,N>& v1)
451
    {
451
    {
452
      V v;
452
      V v;
453
      std::transform(v0.get(), v0.get() + N, v1.get(), v.get(), std::ptr_fun(s_max<T>));
453
      std::transform(v0.get(), v0.get() + N, v1.get(), v.get(), std::ptr_fun(s_max<T>));
454
      return v;
454
      return v;