Subversion Repositories gelsvn

Rev

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

Rev 125 Rev 595
Line -... Line 1...
-
 
1
/* ----------------------------------------------------------------------- *
-
 
2
 * This file is part of GEL, http://www.imm.dtu.dk/GEL
-
 
3
 * Copyright (C) the authors and DTU Informatics
-
 
4
 * For license and list of authors, see ../../doc/intro.pdf
-
 
5
 * ----------------------------------------------------------------------- */
-
 
6
 
-
 
7
/** @file ArithVecFloat.h
-
 
8
 * @brief Abstract 2D floating point vector class
-
 
9
 */
-
 
10
 
1
#ifndef __CGLA__ARITHVECFLOAT_H__
11
#ifndef __CGLA__ARITHVECFLOAT_H__
2
#define __CGLA__ARITHVECFLOAT_H__
12
#define __CGLA__ARITHVECFLOAT_H__
3
 
13
 
4
#include "ArithVec.h"
14
#include "ArithVec.h"
5
 
15
 
6
namespace CGLA {
16
namespace CGLA {
7
 
17
    
8
  template<class T, class V, unsigned int N>
18
    template<class T, class V, unsigned int N>
9
    class ArithVecFloat: public ArithVec<T,V,N>
19
    class ArithVecFloat: public ArithVec<T,V,N>
10
    {
20
    {
11
    public:
21
    public:
12
	  
22
        
13
      ArithVecFloat() 
23
        ArithVecFloat() 
14
	{
24
        {
15
#ifndef NDEBUG
25
#ifndef NDEBUG
16
	  std::fill_n(this->data, N, CGLA_INIT_VALUE);
26
            std::fill_n(this->data, N, CGLA_INIT_VALUE);
17
#endif
27
#endif
18
	}
28
        }
19
	  
29
        
20
      ArithVecFloat(T a): 
30
        ArithVecFloat(T a): 
21
	ArithVec<T,V,N>(a) {}
31
        ArithVec<T,V,N>(a) {}
22
 
32
        
23
      ArithVecFloat(T a, T b): 
33
        ArithVecFloat(T a, T b): 
24
	ArithVec<T,V,N>(a,b) {}
34
        ArithVec<T,V,N>(a,b) {}
25
 
35
        
26
      ArithVecFloat(T a, T b, T c): 
36
        ArithVecFloat(T a, T b, T c): 
27
	ArithVec<T,V,N>(a,b,c) {}
37
        ArithVec<T,V,N>(a,b,c) {}
28
 
38
        
29
      ArithVecFloat(T a, T b, T c, T d): 
39
        ArithVecFloat(T a, T b, T c, T d): 
30
	ArithVec<T,V,N>(a,b,c,d) {}
40
        ArithVec<T,V,N>(a,b,c,d) {}
31
 
41
        
32
      /// Compute Euclidean length.
42
        /// Compute Euclidean length.
33
      T length() const 
43
        T length() const 
34
	{
44
        {
35
	  return sqrt(sqr_length(*this));
45
            return sqrt(sqr_length(*this));
36
	}
46
        }
37
    
47
        
38
      /// Normalize vector.
48
        /// Normalize vector.
39
      void normalize() 
49
        void normalize() 
40
	{
50
        {
41
	  (*this) /= this->length();
51
            (*this) /= this->length();
-
 
52
        }
-
 
53
        
-
 
54
        /// Conditionally normalize vector. The condition being that the vector has non-zero length
-
 
55
        void cond_normalize() 
-
 
56
        {
-
 
57
            T sql = sqr_length(*this);
-
 
58
            if(sql > 0)
-
 
59
                (*this) /= sqrt(sql);
42
	}
60
        }
43
		
61
        
44
    };
62
    };
45
 
63
    
46
  /// Returns normalized vector
64
    /// Returns length of vector
47
  template<class T, class V, unsigned int N>
65
    template<class T, class V, unsigned int N>
48
    inline T length(const ArithVecFloat<T,V,N>& v) 
66
    inline T length(const ArithVecFloat<T,V,N>& v) 
49
    {
67
    {
50
      return v.length();
68
        return v.length();
51
    }
69
    }
52
	
70
	
53
	
71
	
54
  /// Returns normalized vector
72
    /// Returns normalized vector
55
  template<class T, class V, unsigned int N>
73
    template<class T, class V, unsigned int N>
56
    inline V normalize(const ArithVecFloat<T,V,N>& v) 
74
    inline V normalize(const ArithVecFloat<T,V,N>& v) 
57
    {
75
    {
58
      return v/v.length();
76
        return v/v.length();
59
    }
77
    }
-
 
78
    
-
 
79
    /// Returns normalized vector if the vector has non-zero length - otherwise the 0 vector.
-
 
80
    template<class T, class V, unsigned int N>
-
 
81
    inline V cond_normalize(const ArithVecFloat<T,V,N>& v) 
-
 
82
    {
-
 
83
        T sql = sqr_length(v);
-
 
84
        if(sql > 0)
-
 
85
            return v/sqrt(sql);
-
 
86
        return v*1.0;
-
 
87
    }
-
 
88
    
60
}
89
}
61
 
90
 
62
#endif
91
#endif
63
 
92