SmallVector4T.h
Go to the documentation of this file.
1 
9 #ifndef _VECTOR4D_MAL_DEFAULT_
10 #define _VECTOR4D_MAL_DEFAULT_
11 
12 namespace CD_Matrix
13 {
14 template<typename T>
15 struct Matrix4x4T;
16 
18 template<typename T>
19 class Vector4T
20 {
21 public:
22  T m_x, m_y, m_z, m_w;
23 
27  {
28  m_x = 0.0;
29  m_y = 0.0;
30  m_z = 0.0;
31  m_w = 0.0;
32  }
33 
34  explicit Vector4T<T>(const T & x, const T & y, const T & z, const T & w) : m_x(x), m_y(y), m_z(z), m_w(w) {}
35 
37  inline Vector4T<T> operator=(const Vector4T<T> & v)
38  {
39  m_x = v.m_x;
40  m_y = v.m_y;
41  m_z = v.m_z;
42  m_w = v.m_w;
43  return *this;
44  }
45 
47  inline Vector4T<T> operator-() const
48  {
49  return Vector4T<T>(-m_x, -m_y, -m_z, -m_w);
50  }
51 
53  inline T & operator[](unsigned i)
54  {
55  return ((i == 0) ? m_x : (i == 1) ? m_y : (i == 2) ? m_z : m_w);
56  }
57 
59  inline const T & operator[](unsigned i) const
60  {
61  return ((i == 0) ? m_x : (i == 1) ? m_y : (i == 2) ? m_z : m_w);
62  }
63 
65  inline T & operator()(unsigned i)
66  {
67  return ((i == 0) ? m_x : (i == 1) ? m_y : (i == 2) ? m_z : m_w);
68  }
69 
71  inline bool operator==(const Vector4T<T> & v) const
72  {
73  return ((v.m_x == m_x) && (v.m_y == m_y) && (v.m_z == m_z) && (v.m_w == m_w));
74  }
75 
77  inline Vector4T<T> operator+(const Vector4T<T> & v) const
78  {
79  Vector4T<T> vr;
80  vr.m_x = m_x + v.m_x;
81  vr.m_y = m_y + v.m_y;
82  vr.m_z = m_z + v.m_z;
83  vr.m_w = m_w + v.m_w;
84  return vr;
85  }
86 
88  inline Vector4T<T> operator-(const Vector4T<T> & v) const
89  {
90  Vector4T<T> vr;
91  vr.m_x = m_x - v.m_x;
92  vr.m_y = m_y - v.m_y;
93  vr.m_z = m_z - v.m_z;
94  vr.m_w = m_w - v.m_w;
95  return vr;
96  }
97 
99  inline void operator+=(const Vector4T<T> & v)
100  {
101  m_x += v.m_x;
102  m_y += v.m_y;
103  m_z += v.m_z;
104  m_w += v.m_w;
105  }
106 
108  inline void operator-=(const Vector4T<T> & v)
109  {
110  m_x -= v.m_x;
111  m_y -= v.m_y;
112  m_z -= v.m_z;
113  m_w -= v.m_w;
114  }
115 
117  inline Vector4T<T> operator*(const T & t) const
118  {
119  Vector4T<T> vr;
120  vr.m_x = m_x * t;
121  vr.m_y = m_y * t;
122  vr.m_z = m_z * t;
123  vr.m_w = m_w * t;
124  return vr;
125  }
126 
128  inline Vector4T<T> operator/(const T & t) const
129  {
130  Vector4T<T> vr;
131  vr.m_x = m_x / t;
132  vr.m_y = m_y / t;
133  vr.m_z = m_z / t;
134  vr.m_w = m_w / t;
135  return vr;
136  }
137 
139  inline void operator*=(const T & t)
140  {
141  m_x = m_x / t;
142  m_y = m_y / t;
143  m_z = m_z / t;
144  m_w = m_w / t;
145  }
146 
148  inline void operator/=(const T & t)
149  {
150  m_x = m_x / t;
151  m_y = m_y / t;
152  m_z = m_z / t;
153  m_w = m_w / t;
154  }
155 
157  inline void normalize()
158  {
159  T in = static_cast<T>(1 / sqrt(m_x * m_x + m_y * m_y + m_z * m_z + m_w * m_w));
160  m_x *= in;
161  m_y *= in;
162  m_z *= in;
163  m_w *= in;
164  }
165 
167  inline T norm() const
168  {
169  return static_cast<T>(sqrt(m_x * m_x + m_y * m_y + m_z * m_z + m_w * m_w));
170  }
171 
173  inline T normsquared() const
174  {
175  return (m_x * m_x + m_y * m_y + m_z * m_z + m_w * m_w);
176  }
177 
178  inline friend std::ostream & operator<<(std::ostream & os, Vector4T<T> const & v)
179  {
180  os << v.m_x << " " << v.m_y << " " << v.m_z << " " << v.m_w;
181  return os;
182  }
183 };
184 } // namespace CD_Matrix
185 
186 #endif /* _VECTOR4D_PATTERNGENERATOR_JRL_ */
CD_Matrix::Vector4T::operator[]
const T & operator[](unsigned i) const
Array operator.
Definition: SmallVector4T.h:59
CD_Matrix::Vector4T::m_z
T m_z
Definition: SmallVector4T.h:22
CD_Matrix::Vector4T::operator[]
T & operator[](unsigned i)
Array operator.
Definition: SmallVector4T.h:53
CD_Matrix::Vector4T::normsquared
T normsquared() const
Get the norm squared.
Definition: SmallVector4T.h:173
CD_Matrix::Vector4T::operator*
Vector4T< T > operator*(const T &t) const
Binary operator *.
Definition: SmallVector4T.h:117
CD_Matrix::Vector4T::m_y
T m_y
Definition: SmallVector4T.h:22
CD_Matrix::Vector4T
Template to handle 3 dimensional vector.
Definition: SmallVector4T.h:19
CD_Matrix::Vector4T::normalize
void normalize()
Normalize .
Definition: SmallVector4T.h:157
CD_Matrix::Vector4T::operator-=
void operator-=(const Vector4T< T > &v)
Binary operator -=.
Definition: SmallVector4T.h:108
CD_Matrix::Vector4T::operator-
Vector4T< T > operator-(const Vector4T< T > &v) const
Binary operator -.
Definition: SmallVector4T.h:88
CD_Matrix::Vector4T::operator-
Vector4T< T > operator-() const
Unary operator -.
Definition: SmallVector4T.h:47
CD_Matrix::Vector4T::operator=
Vector4T< T > operator=(const Vector4T< T > &v)
Assignement operator.
Definition: SmallVector4T.h:37
CD_Matrix::Vector4T::operator==
bool operator==(const Vector4T< T > &v) const
Binary operator ==.
Definition: SmallVector4T.h:71
CD_Matrix::Vector4T::operator/=
void operator/=(const T &t)
Binary operator /=.
Definition: SmallVector4T.h:148
CD_Matrix::Vector4T::m_w
T m_w
Definition: SmallVector4T.h:22
CD_Matrix::Vector4T::Vector4T
Vector4T()
Basic constructor: all the field are set to zero.
Definition: SmallVector4T.h:26
CD_Matrix::Vector4T::m_x
T m_x
Definition: SmallVector4T.h:22
CD_Matrix
Definition: QuaternionT.h:6
CD_Matrix::Vector4T::operator<<
friend std::ostream & operator<<(std::ostream &os, Vector4T< T > const &v)
Definition: SmallVector4T.h:178
CD_Matrix::Vector4T::operator+=
void operator+=(const Vector4T< T > &v)
Binary operator +=.
Definition: SmallVector4T.h:99
CD_Matrix::Vector4T::operator/
Vector4T< T > operator/(const T &t) const
Binary operator /.
Definition: SmallVector4T.h:128
CD_Matrix::Vector4T::norm
T norm() const
Get the norm.
Definition: SmallVector4T.h:167
CD_Matrix::Vector4T::operator+
Vector4T< T > operator+(const Vector4T< T > &v) const
Binary operator +.
Definition: SmallVector4T.h:77
CD_Matrix::Vector4T::operator()
T & operator()(unsigned i)
Array operator.
Definition: SmallVector4T.h:65
CD_Matrix::Vector4T::operator*=
void operator*=(const T &t)
Binary operator *=.
Definition: SmallVector4T.h:139