SmallVector3T.h
Go to the documentation of this file.
1 
7 #ifndef _VECTOR3DEFAULT_MAL_DEFAULT_
8 #define _VECTOR3DEFAULT_MAL_DEFAULT_
9 
10 namespace CD_Matrix
11 {
12 
13 template<typename T>
14 struct Matrix3x3T;
15 
16 template<typename T, bool normOptimization>
17 struct Norm
18 {
19 protected:
20  T norm_;
22 
23 public:
24  Norm() : normOutdated_(true) {}
25 
26  inline void setNorm(const T & n)
27  {
28  norm_ = n;
29  normOutdated_ = false;
30  }
31 
32  inline void outdateNorm()
33  {
34  normOutdated_ = true;
35  }
36 
37  inline bool normOutdated() const
38  {
39  return normOutdated_;
40  }
41 
42  inline T getNorm() const
43  {
44  return norm_;
45  }
46 };
47 
48 template<typename T>
49 struct Norm<T, false>
50 {
51 public:
52  inline void setNorm(const T & n) {}
53 
54  inline T getNorm() const
55  {
56  return 0;
57  }
58 
59  inline void outdateNorm() {}
60 
61  inline bool normOutdated() const
62  {
63  return true;
64  }
65 };
66 
68 template<typename T, bool normOptimization>
69 struct Vector3T : protected Norm<T, normOptimization>
70 {
71 public:
72  T m_x, m_y, m_z;
73 
75 
76 public:
77  template<class Archive>
78  void serialize(Archive & ar, const unsigned int /*version*/)
79  {
80  ar & m_x;
81  ar & m_y;
82  ar & m_z;
83 
85  }
86 
89  explicit Vector3T() : m_x(0.0), m_y(0.0), m_z(0.0) {}
90 
91  explicit Vector3T(const T & x, const T & y, const T & z) : m_x(x), m_y(y), m_z(z) {}
92 
93  explicit Vector3T(const T * const p) : m_x(p[0]), m_y(p[1]), m_z(p[2]) {}
94 
95  void Set(const T & x, const T & y, const T & z)
96  {
97  this->m_x = x;
98  this->m_y = y;
99  this->m_z = z;
100 
102  }
103 
104  void Set(const T * const p)
105  {
106  this->m_x = p[0];
107  this->m_y = p[1];
108  this->m_z = p[2];
109 
111  }
112 
114  template<bool b>
115  inline Vector3T(const struct Vector3T<T, b> & v) : m_x(v.m_x), m_y(v.m_y), m_z(v.m_z)
116  {
117  }
118 
120  template<bool b>
121  inline Vector3T & operator=(const struct Vector3T<T, b> & v)
122  {
123  m_x = v.m_x;
124  m_y = v.m_y;
125  m_z = v.m_z;
126 
128  return *this;
129  }
130 
132  inline Vector3T operator-() const
133  {
134  return Vector3T(-m_x, -m_y, -m_z);
135  }
136 
138  inline T & operator[](unsigned i)
139  {
141  return ((i == 0) ? m_x : (i == 1) ? m_y : m_z);
142  }
143 
145  inline const T & operator[](unsigned i) const
146  {
147  return ((i == 0) ? m_x : (i == 1) ? m_y : m_z);
148  }
149 
151  inline T & operator()(unsigned i)
152  {
154  return ((i == 0) ? m_x : (i == 1) ? m_y : m_z);
155  }
156 
158  template<bool b>
159  inline bool operator==(const struct Vector3T<T, b> & v) const
160  {
161  return ((v.m_x == m_x) && (v.m_y == m_y) && (v.m_z == m_z));
162  }
163 
165  template<bool b>
166  inline Vector3T operator+(const struct Vector3T<T, b> & v) const
167  {
168  Vector3T vr;
169  vr.m_x = m_x + v.m_x;
170  vr.m_y = m_y + v.m_y;
171  vr.m_z = m_z + v.m_z;
172  return vr;
173  }
174 
176  template<bool b>
177  inline Vector3T operator-(const struct Vector3T<T, b> & v) const
178  {
179  Vector3T vr;
180  vr.m_x = m_x - v.m_x;
181  vr.m_y = m_y - v.m_y;
182  vr.m_z = m_z - v.m_z;
183  return vr;
184  }
185 
187  template<bool b>
188  inline void operator+=(const struct Vector3T<T, b> & v)
189  {
191 
192  m_x += v.m_x;
193  m_y += v.m_y;
194  m_z += v.m_z;
195  }
196 
198  template<bool b>
199  inline void operator-=(const struct Vector3T<T, b> & v)
200  {
202 
203  m_x -= v.m_x;
204  m_y -= v.m_y;
205  m_z -= v.m_z;
206  }
207 
209  inline Vector3T operator*(const T & t) const
210  {
211  Vector3T vr;
212  vr.m_x = m_x * t;
213  vr.m_y = m_y * t;
214  vr.m_z = m_z * t;
215  return vr;
216  }
217 
219  template<bool b>
220  inline T operator*(const Vector3T<T, b> & v) const
221  {
222  return m_x * v.m_x + m_y * v.m_y + m_z * v.m_z;
223  }
224 
226  inline Vector3T operator*(const Matrix3x3T<T> & m) const
227  {
228 
229  Vector3T vr(m_x * m[0] + m_y * m[3] + m_z * m[6], m_x * m[1] + m_y * m[4] + m_z * m[7],
230  m_x * m[2] + m_y * m[5] + m_z * m[8]);
231  return vr;
232  }
233 
234  inline void operator*=(const Matrix3x3T<T> m)
235  {
237 
238  m_x = m_x * m[0] + m_y * m[3] + m_z * m[6];
239  m_y = m_x * m[1] + m_y * m[4] + m_z * m[7];
240  m_z = m_x * m[2] + m_y * m[5] + m_z * m[8];
241  }
242 
244  inline Vector3T operator/(const T & t) const
245  {
246  Vector3T vr;
247  vr.m_x = m_x / t;
248  vr.m_y = m_y / t;
249  vr.m_z = m_z / t;
250  return vr;
251  }
252 
254  inline void operator*=(const T & t)
255  {
257  m_x = m_x * t;
258  m_y = m_y * t;
259  m_z = m_z * t;
260  }
261 
263  inline void operator/=(const T & t)
264  {
266  m_x = m_x / t;
267  m_y = m_y / t;
268  m_z = m_z / t;
269  }
270 
272  inline void normalize()
273  {
275  T in = static_cast<T>(1.0 / sqrt(m_x * m_x + m_y * m_y + m_z * m_z));
276  m_x *= in;
277  m_y *= in;
278  m_z *= in;
279  }
280 
282  inline Vector3T normalized() const
283  {
284  T in = static_cast<T>(1.0 / sqrt(m_x * m_x + m_y * m_y + m_z * m_z));
285 
286  return Vector3T(m_x * in, m_y * in, m_z * in);
287  }
288 
290  inline T norm() const
291  {
292  return static_cast<T>(sqrt(m_x * m_x + m_y * m_y + m_z * m_z));
293  }
294 
296  inline T optimizedNorm()
297  {
298  if(normOptimization)
299  {
300  if(Norm<T, normOptimization>::normOutdated()) setNorm(static_cast<T>(sqrt(m_x * m_x + m_y * m_y + m_z * m_z)));
302  }
303  else
304  return static_cast<T>(sqrt(m_x * m_x + m_y * m_y + m_z * m_z));
305  }
306 
308  inline T normsquared() const
309  {
310  return (m_x * m_x + m_y * m_y + m_z * m_z);
311  }
312 
313  inline bool IsZero() const
314  {
315  return ((m_x == 0) && (m_y == 0) && (m_z == 0));
316  }
317 
319  template<bool b>
320  inline Vector3T operator^(const Vector3T<T, b> & v2) const
321  {
322  struct Vector3T vr;
323  vr.m_x = m_y * v2.m_z - v2.m_y * m_z;
324  vr.m_y = m_z * v2.m_x - v2.m_z * m_x;
325  vr.m_z = m_x * v2.m_y - v2.m_x * m_y;
326  return vr;
327  }
328 
329  inline friend std::ostream & operator<<(std::ostream & os, Vector3T<T, normOptimization> const & v)
330  {
331  os << v.m_x << " " << v.m_y << " " << v.m_z << " ";
332  return os;
333  }
334 };
335 } // namespace CD_Matrix
336 
337 #endif /* _VECTOR3D_PATTERNGENERATOR_JRL_ */
CD_Matrix::Norm< T, false >::normOutdated
bool normOutdated() const
Definition: SmallVector3T.h:61
CD_Matrix::Vector3T::Vector
Vector3T< T, normOptimization > Vector
Definition: SmallVector3T.h:74
CD_Matrix::Vector3T::operator/
Vector3T operator/(const T &t) const
Definition: SmallVector3T.h:244
CD_Matrix::Vector3T::IsZero
bool IsZero() const
Definition: SmallVector3T.h:313
CD_Matrix::Vector3T::Vector3T
Vector3T(const T *const p)
Definition: SmallVector3T.h:93
CD_Matrix::Vector3T::operator+=
void operator+=(const struct Vector3T< T, b > &v)
Definition: SmallVector3T.h:188
CD_Matrix::Vector3T::m_z
T m_z
Definition: SmallVector3T.h:72
CD_Matrix::Vector3T::Set
void Set(const T *const p)
Definition: SmallVector3T.h:104
CD_Matrix::Vector3T::Vector3T
Vector3T(const T &x, const T &y, const T &z)
Definition: SmallVector3T.h:91
CD_Matrix::Norm::outdateNorm
void outdateNorm()
Definition: SmallVector3T.h:32
CD_Matrix::Vector3T::operator<<
friend std::ostream & operator<<(std::ostream &os, Vector3T< T, normOptimization > const &v)
Definition: SmallVector3T.h:329
CD_Matrix::Vector3T::operator[]
const T & operator[](unsigned i) const
Definition: SmallVector3T.h:145
CD_Matrix::Vector3T::operator*
Vector3T operator*(const T &t) const
Definition: SmallVector3T.h:209
CD_Matrix::Vector3T::normsquared
T normsquared() const
Definition: SmallVector3T.h:308
CD_Matrix::Vector3T::m_x
T m_x
Definition: SmallVector3T.h:72
CD_Matrix::Vector3T::norm
T norm() const
Definition: SmallVector3T.h:290
CD_Matrix::Vector3T::operator=
Vector3T & operator=(const struct Vector3T< T, b > &v)
Definition: SmallVector3T.h:121
CD_Matrix::Norm::normOutdated
bool normOutdated() const
Definition: SmallVector3T.h:37
CD_Matrix::Vector3T::operator==
bool operator==(const struct Vector3T< T, b > &v) const
Definition: SmallVector3T.h:159
CD_Matrix::Vector3T::operator^
Vector3T operator^(const Vector3T< T, b > &v2) const
Definition: SmallVector3T.h:320
CD_Matrix::Vector3T::operator-
Vector3T operator-(const struct Vector3T< T, b > &v) const
Definition: SmallVector3T.h:177
CD_Matrix::Norm::getNorm
T getNorm() const
Definition: SmallVector3T.h:42
CD_Matrix::Vector3T::operator*
T operator*(const Vector3T< T, b > &v) const
Definition: SmallVector3T.h:220
CD_Matrix::Norm< T, false >::setNorm
void setNorm(const T &n)
Definition: SmallVector3T.h:52
CD_Matrix::Vector3T::Set
void Set(const T &x, const T &y, const T &z)
Definition: SmallVector3T.h:95
CD_Matrix::Vector3T::operator-=
void operator-=(const struct Vector3T< T, b > &v)
Definition: SmallVector3T.h:199
CD_Matrix::Vector3T::operator*
Vector3T operator*(const Matrix3x3T< T > &m) const
Definition: SmallVector3T.h:226
CD_Matrix::Vector3T::normalize
void normalize()
Definition: SmallVector3T.h:272
CD_Matrix::Norm::norm_
T norm_
Definition: SmallVector3T.h:20
CD_Matrix::Vector3T::m_y
T m_y
Definition: SmallVector3T.h:72
CD_Matrix::Norm::setNorm
void setNorm(const T &n)
Definition: SmallVector3T.h:26
CD_Matrix::Vector3T::normalized
Vector3T normalized() const
Definition: SmallVector3T.h:282
CD_Matrix::Norm< T, false >::outdateNorm
void outdateNorm()
Definition: SmallVector3T.h:59
CD_Matrix::Vector3T::operator[]
T & operator[](unsigned i)
Definition: SmallVector3T.h:138
CD_Matrix
Definition: QuaternionT.h:6
CD_Matrix::Norm< T, false >::getNorm
T getNorm() const
Definition: SmallVector3T.h:54
CD_Matrix::Vector3T::operator+
Vector3T operator+(const struct Vector3T< T, b > &v) const
Definition: SmallVector3T.h:166
CD_Matrix::Vector3T::operator()
T & operator()(unsigned i)
Definition: SmallVector3T.h:151
CD_Matrix::Vector3T::Vector3T
Vector3T()
Definition: SmallVector3T.h:89
CD_Matrix::Vector3T::operator/=
void operator/=(const T &t)
Definition: SmallVector3T.h:263
CD_Matrix::Vector3T
Definition: SmallMatrix3x3T.h:14
CD_Matrix::Vector3T::operator-
Vector3T operator-() const
Definition: SmallVector3T.h:132
CD_Matrix::Norm::Norm
Norm()
Definition: SmallVector3T.h:24
CD_Matrix::Vector3T::optimizedNorm
T optimizedNorm()
Definition: SmallVector3T.h:296
CD_Matrix::Norm::normOutdated_
bool normOutdated_
Definition: SmallVector3T.h:21
CD_Matrix::Vector3T::operator*=
void operator*=(const Matrix3x3T< T > m)
Definition: SmallVector3T.h:234
CD_Matrix::Matrix3x3T
Definition: SmallMatrix3x3T.h:20
CD_Matrix::Vector3T::operator*=
void operator*=(const T &t)
Definition: SmallVector3T.h:254
CD_Matrix::Norm
Definition: SmallVector3T.h:17
CD_Matrix::Vector3T::serialize
void serialize(Archive &ar, const unsigned int)
Definition: SmallVector3T.h:78
CD_Matrix::Vector3T::Vector3T
Vector3T(const struct Vector3T< T, b > &v)
Definition: SmallVector3T.h:115