SVector.h
Go to the documentation of this file.
1 
2 #pragma once
3 
4 #include <ionCore.h>
5 
6 
7 template <typename T, u32 Size>
9 {
10 
11 private:
12 
13  SVectorBase & operator = (SVectorBase const &);
14 
15 protected:
16 
18  {}
19 
20 public:
21 
22  static u32 const Dimension = Size;
24  mutable T OutOfBounds;
25 
28  virtual T const operator[] (int i) const
29  {
30  return (i >= 0 && i < Dimension ? Values[i] : OutOfBounds = 0);
31  }
32 
35  virtual T & operator[] (int i)
36  {
37  return (i >= 0 && i < Dimension ? Values[i] : OutOfBounds = 0);
38  }
39 
41  void reset()
42  {
43  for (u32 i = 0; i < Dimension; ++ i)
44  Values[i] = 0;
45  }
46 
48  void set(T const in)
49  {
50  for (u32 i = 0; i < Dimension; ++ i)
51  Values[i] = in;
52  }
53 
55  void set(T const in[])
56  {
57  for (u32 i = 0; i < Dimension; ++ i)
58  Values[i] = in[i];
59  }
60 
62  template <typename U, u32 otherDimension>
63  void set(SVectorBase<U, otherDimension> const & other)
64  {
65  for (u32 i = 0; i < Dimension; ++ i)
66  Values[i] = (T) other[i];
67  }
68 
70  T const Length() const
71  {
72  T sum = 0;
73  for (u32 i = 0; i < Dimension; ++ i)
74  sum += Sq(Values[i]);
75  return (T) sqrt(sum);
76  }
77 
79  friend T const Length(SVectorBase<T, Dimension> const & vec)
80  {
81  return vec.Length();
82  }
83 
85  T const LengthSq() const
86  {
87  T sum = 0;
88  for (u32 i = 0; i < Dimension; ++ i)
89  sum += Sq(Values[i]);
90  return sum;
91  }
92 
94  friend T const LengthSq(SVectorBase<T, Dimension> const & vec)
95  {
96  return vec.LengthSq();
97  }
98 
100  T const * GetValuePointer() const
101  {
102  return Values;
103  }
104 
107  {
108  return Values;
109  }
110 
111 };
112 
113 template <typename T, int Size, typename Implementation>
114 class SVector : public SVectorBase<T, Size>
115 {
116 
117 private:
118 
119  SVector & operator = (SVector const &);
121  typedef SVectorBase<T, Size> Other;
122 
123  Implementation & Clone()
124  {
125  return * static_cast<Implementation *>(this);
126  }
127 
128  Implementation const & Clone() const
129  {
130  return * static_cast<Implementation const *>(this);
131  }
132 
133 protected:
134 
136  {}
137 
138 public:
139 
144 
146  // Dot Product //
148 
149  T const DotProduct(Other const & other) const
150  {
151  T sum = 0;
152  for (u32 i = 0; i < Dimension; ++ i)
153  sum += Values[i] * other[i];
154  return sum;
155  }
156 
157  T const Dot(Other const & other) const
158  {
159  return DotProduct(other);
160  }
161 
162  friend T const Dot(Type const & lhs, Other const & rhs)
163  {
164  return lhs.Dot(rhs);
165  }
166 
168  // Distance //
170 
171  T const GetDistanceFrom(Other const & v) const
172  {
173  return (* this - v).Length();
174  }
175 
176  friend T const Distance(Type const & lhs, Other const & rhs)
177  {
178  return (lhs - rhs).Length();
179  }
180 
181  T const GetDistanceSqFrom(Other const & v) const
182  {
183  return (* this - v).LengthSq();
184  }
185 
186  friend T const DistanceSq(Type const & lhs, Other const & rhs)
187  {
188  return (lhs - rhs).LengthSq();
189  }
190 
192  // Interpolation //
194 
195  template <typename Real>
196  Implementation const GetInterpolated(Other const & v, Real const d)
197  {
198  Real inv = (Real) 1 - d;
199  Implementation ret;
200  for (u32 i = 0; i < Dimension; ++ i)
201  ret[i] = (T) ((Real) v[i] * inv + (Real) Values[i] * d);
202  return ret;
203  }
204 
205  template <typename Real>
206  friend Implementation const Interpolate(Type const & lhs, Other const & rhs, Real const d)
207  {
208  return lhs.GetInterpolated(rhs, d);
209  }
210 
212  // Normalization //
214 
215  Implementation const GetNormalized() const
216  {
217  Implementation ret = Clone();
218  ret.Normalize();
219  return ret;
220  }
221 
223  Type & Normalize()
224  {
225  T const len = Length();
226 
227  for (u32 i = 0; i < Dimension; ++ i)
228  Values[i] /= len;
229 
230  return * this;
231  }
232 
233  friend Implementation const Normalize(Implementation const & v)
234  {
235  Implementation ret = v;
236  ret.Normalize();
237  return ret;
238  }
239 
241  // Basic Arithmetic //
243 
244  Implementation const operator + (Other const & v) const
245  {
246  Implementation ret;
247  for (u32 i = 0; i < Dimension; ++ i)
248  ret[i] = Values[i] + v[i];
249  return ret;
250  }
251 
252  Implementation const operator - (Other const & v) const
253  {
254  Implementation ret;
255  for (u32 i = 0; i < Dimension; ++ i)
256  ret[i] = Values[i] - v[i];
257  return ret;
258  }
259 
260  Implementation const operator * (Other const & v) const
261  {
262 
263  Implementation ret;
264  for (u32 i = 0; i < Dimension; ++ i)
265  ret[i] = Values[i] * v[i];
266  return ret;
267  }
268 
269  Implementation const operator / (Other const & v) const
270  {
271 
272  Implementation ret;
273  for (u32 i = 0; i < Dimension; ++ i)
274  ret[i] = Values[i] / v[i];
275  return ret;
276  }
277 
279  // Scalar Arithmetic //
281 
282  Implementation const operator + (T const s) const
283  {
284  Implementation ret;
285  for (u32 i = 0; i < Dimension; ++ i)
286  ret[i] = Values[i] + s;
287  return ret;
288  }
289 
290  Implementation const operator - (T const s) const
291  {
292  Implementation ret;
293  for (u32 i = 0; i < Dimension; ++ i)
294  ret[i] = Values[i] - s;
295  return ret;
296  }
297 
298  Implementation const operator * (T const s) const
299  {
300  Implementation ret;
301  for (u32 i = 0; i < Dimension; ++ i)
302  ret[i] = Values[i] * s;
303  return ret;
304  }
305 
306  Implementation const operator / (T const s) const
307  {
308  Implementation ret;
309  for (u32 i = 0; i < Dimension; ++ i)
310  ret[i] = Values[i] / s;
311  return ret;
312  }
313 
314  friend Implementation const operator + (T const lhs, Type const & rhs)
315  {
316  Implementation ret;
317  for (u32 i = 0; i < Dimension; ++ i)
318  ret[i] = lhs + rhs.Values[i];
319  return ret;
320  }
321 
322  friend Implementation const operator - (T const lhs, Type const & rhs)
323  {
324  Implementation ret;
325  for (u32 i = 0; i < Dimension; ++ i)
326  ret[i] = lhs - rhs.Values[i];
327  return ret;
328  }
329 
330  friend Implementation const operator * (T const lhs, Type const & rhs)
331  {
332  Implementation ret;
333  for (u32 i = 0; i < Dimension; ++ i)
334  ret[i] = lhs * rhs.Values[i];
335  return ret;
336  }
337 
338  friend Implementation const operator / (T const lhs, Type const & rhs)
339  {
340  Implementation ret;
341  for (u32 i = 0; i < Dimension; ++ i)
342  ret[i] = lhs / rhs.Values[i];
343  return ret;
344  }
345 
347  // Basic Arithmetic Assignment //
349 
350  Type & operator += (Other const & v)
351  {
352  for (u32 i = 0; i < Dimension; ++ i)
353  Values[i] += v[i];
354 
355  return * this;
356  }
357 
358  Type & operator -= (Other const & v)
359  {
360  for (u32 i = 0; i < Dimension; ++ i)
361  Values[i] -= v[i];
362 
363  return * this;
364  }
365 
366  Type & operator *= (Other const & v)
367  {
368  for (u32 i = 0; i < Dimension; ++ i)
369  Values[i] *= v[i];
370 
371  return * this;
372  }
373 
374  Type & operator /= (Other const & v)
375  {
376  for (u32 i = 0; i < Dimension; ++ i)
377  Values[i] /= v[i];
378 
379  return * this;
380  }
381 
383  // Scalar Arithmetic Assignment //
385 
386  Type & operator += (T const s)
387  {
388  for (u32 i = 0; i < Dimension; ++ i)
389  Values[i] += s;
390 
391  return * this;
392  }
393 
394  Type & operator -= (T const s)
395  {
396  for (u32 i = 0; i < Dimension; ++ i)
397  Values[i] -= s;
398 
399  return * this;
400  }
401 
402  Type & operator *= (T const s)
403  {
404  for (u32 i = 0; i < Dimension; ++ i)
405  Values[i] *= s;
406 
407  return * this;
408  }
409 
410  Type & operator /= (T const s)
411  {
412  for (u32 i = 0; i < Dimension; ++ i)
413  Values[i] /= s;
414 
415  return * this;
416  }
417 
419  // Negation //
421 
422  Implementation const operator - () const
423  {
424  Implementation ret;
425  for (u32 i = 0; i < Dimension; ++ i)
426  ret[i] = - Values[i];
427  return ret;
428  }
429 
431  // Inequality operator (for sorting) //
433 
434  bool operator < (Other const & v) const
435  {
436  for (int i = 0; i < Dimension; ++ i)
437  {
438  if (Values[i] < v.Values[i])
439  return true;
440  else if (Values[i] > v.Values[i])
441  return false;
442  }
443  return false;
444  }
445 
447  // Equality operators //
449 
450  bool operator == (Other const & v) const
451  {
452  return Equals(v);
453  }
454 
455  bool operator != (Other const & v) const
456  {
457  return ! Equals(v);
458  }
459 
460  bool Equals(Other const & v, T const Epsilon = RoundingError<T>::Value()) const
461  {
462  bool result = true;
463  for (u32 i = 0; i < Dimension; ++ i)
464  result &= ::Equals(Values[i], v[i], Epsilon);
465 
466  return result;
467  }
468 
470  // Extrema //
472 
473  friend Implementation const ComponentMinimum(Type const & lhs, Other const & rhs)
474  {
475  Implementation ret;
476  for (u32 i = 0; i < Dimension; ++ i)
477  ret[i] = Minimum(lhs[i], rhs[i]);
478 
479  return ret;
480  }
481 
482  friend Implementation const ComponentMaximum(Type const & lhs, Other const & rhs)
483  {
484  Implementation ret;
485  for (u32 i = 0; i < Dimension; ++ i)
486  ret[i] = Maximum(lhs[i], rhs[i]);
487 
488  return ret;
489  }
490 
491 };
friend Implementation const Normalize(Implementation const &v)
Definition: SVector.h:233
friend Implementation const ComponentMinimum(Type const &lhs, Other const &rhs)
Definition: SVector.h:473
T Sq(T const &a)
Definition: ionUtils.h:60
static u32 const Dimension
Definition: SVector.h:22
T Values[Dimension]
Definition: SVector.h:23
T const LengthSq() const
Squared-length of vector (computationally fast)
Definition: SVector.h:85
friend Implementation const Interpolate(Type const &lhs, Other const &rhs, Real const d)
Definition: SVector.h:206
SVector()
Definition: SVector.h:135
friend T const Dot(Type const &lhs, Other const &rhs)
Definition: SVector.h:162
void reset()
Sets all values to 0.
Definition: SVector.h:41
virtual T const operator[](int i) const
Definition: SVector.h:28
friend T const Length(SVectorBase< T, Dimension > const &vec)
Length of vector.
Definition: SVector.h:79
SVectorBase()
Definition: SVector.h:17
T const GetDistanceFrom(Other const &v) const
Definition: SVector.h:171
T const GetDistanceSqFrom(Other const &v) const
Definition: SVector.h:181
Definition: SVector.h:8
Uint32 u32
Definition: ionTypes.h:89
T const Length() const
Length of vector.
Definition: SVector.h:70
Type & Normalize()
Normalize this vector.
Definition: SVector.h:223
T const DotProduct(Other const &other) const
Definition: SVector.h:149
friend T const DistanceSq(Type const &lhs, Other const &rhs)
Definition: SVector.h:186
Implementation const GetInterpolated(Other const &v, Real const d)
Definition: SVector.h:196
Implementation const GetNormalized() const
Definition: SVector.h:215
T OutOfBounds
Definition: SVector.h:24
Definition: SVector.h:114
T * GetValuePointer()
Raw pointer access to vector values.
Definition: SVector.h:106
friend Implementation const ComponentMaximum(Type const &lhs, Other const &rhs)
Definition: SVector.h:482
T const * GetValuePointer() const
Raw pointer access to vector values.
Definition: SVector.h:100
T const Dot(Other const &other) const
Definition: SVector.h:157
bool Equals(Other const &v, T const Epsilon=RoundingError< T >::Value()) const
Definition: SVector.h:460
friend T const LengthSq(SVectorBase< T, Dimension > const &vec)
Squared-length of vector (computationally fast)
Definition: SVector.h:94
Helper template to enable template-dependant epsilon values.
Definition: ionComparison.h:30
friend T const Distance(Type const &lhs, Other const &rhs)
Definition: SVector.h:176