Subversion Repositories gelsvn

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
667 khor 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
 
11
#ifndef __CGLA__ARITHVECFLOAT_H__
12
#define __CGLA__ARITHVECFLOAT_H__
13
 
14
#include "ArithVec.h"
15
 
16
namespace CGLA {
17
 
18
    template<class T, class V, unsigned int N>
19
    class ArithVecFloat: public ArithVec<T,V,N>
20
    {
21
    public:
22
 
23
        ArithVecFloat() 
24
        {
25
#ifndef NDEBUG
26
            std::fill_n(this->data.begin(), N, CGLA_INIT_VALUE);
27
#endif
28
        }
29
 
30
        ArithVecFloat(T a): 
31
        ArithVec<T,V,N>(a) {}
32
 
33
        ArithVecFloat(T a, T b): 
34
        ArithVec<T,V,N>(a,b) {}
35
 
36
        ArithVecFloat(T a, T b, T c): 
37
        ArithVec<T,V,N>(a,b,c) {}
38
 
39
        ArithVecFloat(T a, T b, T c, T d): 
40
        ArithVec<T,V,N>(a,b,c,d) {}
41
 
42
        /// Compute Euclidean length.
43
        T length() const 
44
        {
45
            return sqrt(sqr_length(*this));
46
        }
47
 
48
        /// Normalize vector.
49
        void normalize() 
50
        {
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);
60
        }
61
 
62
    };
63
 
64
    /// Returns length of vector
65
    template<class T, class V, unsigned int N>
66
    inline T length(const ArithVecFloat<T,V,N>& v) 
67
    {
68
        return v.length();
69
    }
70
 
71
 
72
    /// Returns normalized vector
73
    template<class T, class V, unsigned int N>
74
    inline V normalize(const ArithVecFloat<T,V,N>& v) 
75
    {
76
        return v/v.length();
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
 
89
}
90
 
91
#endif
92