lbm_reference
Vec.h
Go to the documentation of this file.
1 //! \file Vec.h
2 //! \date Jan 4, 2009
3 //! \author Florian Rathgeber
4 
5 #ifndef VEC_H_
6 #define VEC_H_
7 
8 #include <vector>
9 
10 //! Three component vector of type specified by template parameter.
11 
12 //! \tparam T Type of the vector elements
13 
14 template< typename T >
15 class Vec3 {
16 
17 public:
18 
19  // ============================ //
20  // Constructors and destructors //
21  // ============================ //
22 
23  //! Default constructor
24 
25  //! Creates a zero length vector
26 
27  Vec3() : data_( 0 ) {}
28 
29  //! Copy constructor
30 
31  //! \param[in] v Vector to copy from
32 
33  Vec3( const Vec3& v ) : data_( v.data_ ) {}
34 
35  //! Constructor to set all three components
36 
37  //! \param[in] x First component
38  //! \param[in] y Second component
39  //! \param[in] z Third component
40 
41  Vec3( T x, T y, T z ) : data_( 3 ) {
42  data_[0] = x;
43  data_[1] = y;
44  data_[2] = z;
45  }
46 
47  //! Destructor
48 
49  //! Clears the vector
50 
51  virtual ~Vec3() {
52  data_.clear();
53  }
54 
55  // ================ //
56  // Access operators //
57  // ================ //
58 
59  //! Non-constant access operator
60 
61  //! \param[in] i Vector component to return
62  //! \return Reference to specified component of template type
63 
64  inline T& operator[]( int i ) { return data_[i]; }
65 
66  //! Constant access operator
67 
68  //! \param[in] i Vector component to return
69  //! \return Const reference to specified component of template type
70 
71  inline const T& operator[]( int i ) const { return data_[i]; }
72 
73  // ====================== //
74  // Mathematical operators //
75  // ====================== //
76 
77  //! Addition operator
78 
79  //! \param[in] v Vector to add to this vector
80  //! \return Vector sum of this vector and given vector
81 
82  inline Vec3<T> operator+( const Vec3<T>& v ) const {
83  return Vec3<T>( data_[0] + v.data_[0], data_[1] + v.data_[1], data_[2] + v.data_[2] );
84  }
85 
86  //! In-place addition operator
87 
88  //! \param[in] v Vector to add to this vector in-place
89 
90  inline void operator+=( const Vec3<T>& v ) {
91  data_[0] += v.data_[0];
92  data_[1] += v.data_[1];
93  data_[2] += v.data_[2];
94  }
95 
96  //! Inner product operator
97 
98  //! \param[in] v Vector to scalar multiply with this vector
99  //! \return Scalar product of this and given vector
100 
101  inline T operator*( const Vec3<T>& v ) const {
102  return data_[0] * v.data_[0] + data_[1] * v.data_[1] + data_[2] * v.data_[2];
103  }
104 
105  //! Multiplication with scalar operator
106 
107  //! \param[in] a Scalar to multiply this vector with
108  //! \return Vector scaled by given scalar
109 
110  inline Vec3<T> operator*( const T a ) const {
111  return Vec3<T>(data_[0] * a + data_[1] * a + data_[2] * a);
112  }
113 
114  //! In-place multiplication with scalar operator
115 
116  //! \param[in] a Scalar to multiply this vector with in-place
117 
118  inline void operator*=( const T a ) const {
119  data_[0] *= a;
120  data_[1] *= a;
121  data_[2] *= a;
122  }
123 
124 protected:
125 
126  // ============ //
127  // Data members //
128  // ============ //
129 
130  //! The stl vector to hold the data
131 
132  std::vector<T> data_;
133 };
134 
135 #endif /* VEC_H_ */