FastUIDraw
vecN.hpp
Go to the documentation of this file.
1 /*!
2  * \file vecN.hpp
3  * \brief file vecN.hpp
4  *
5  * Adapted from: vecN.hpp of WRATH:
6  *
7  * Copyright 2013 by Nomovok Ltd.
8  * Contact: info@nomovok.com
9  * This Source Code Form is subject to the
10  * terms of the Mozilla Public License, v. 2.0.
11  * If a copy of the MPL was not distributed with
12  * this file, You can obtain one at
13  * http://mozilla.org/MPL/2.0/.
14  *
15  * \author Kevin Rogovin <kevin.rogovin@nomovok.com>
16  * \author Kevin Rogovin <kevin.rogovin@gmail.com>
17  *
18  */
19 
20 #ifndef FASTUIDRAW_VECN_HPP
21 #define FASTUIDRAW_VECN_HPP
22 
23 #include <fastuidraw/util/util.hpp>
24 #include <fastuidraw/util/math.hpp>
25 
26 namespace fastuidraw
27 {
28 /*!\addtogroup Utility
29  * @{
30  */
31 
32 /*!
33  * \brief
34  * vecN is a simple static array class with no virtual
35  * functions and no memory overhead. Supports runtim array
36  * index checking and STL style iterators via pointer iterators.
37  *
38  * \param T typename with a constructor that takes no arguments.
39  * \param N size of array
40  */
41 template<typename T, size_t N>
42 class vecN
43 {
44 public:
45  enum
46  {
47  /*!
48  * Enumeration value for length of array.
49  */
51  };
52 
53  /*!
54  * \brief
55  * STL compliant typedef
56  */
57  typedef T* pointer;
58 
59  /*!
60  * \brief
61  * STL compliant typedef
62  */
63  typedef const T* const_pointer;
64 
65  /*!
66  * \brief
67  * STL compliant typedef
68  */
69  typedef T& reference;
70 
71  /*!
72  * \brief
73  * STL compliant typedef
74  */
75  typedef const T& const_reference;
76 
77  /*!
78  * \brief
79  * STL compliant typedef
80  */
81  typedef T value_type;
82 
83  /*!
84  * \brief
85  * STL compliant typedef
86  */
87  typedef size_t size_type;
88 
89  /*!
90  * \brief
91  * STL compliant typedef
92  */
93  typedef ptrdiff_t difference_type;
94 
95  /*!
96  * \brief
97  * iterator typedef to pointer
98  */
99  typedef pointer iterator;
100 
101  /*!
102  * \brief
103  * iterator typedef to const_pointer
104  */
105  typedef const_pointer const_iterator;
106 
107  /*!
108  * Ctor, no intiliaztion on POD types.
109  */
110  vecN(void)
111  {}
112 
113  /*!
114  * Ctor.
115  * Calls T::operator= on each element of the array.
116  * \param value constant reference to a T value.
117  */
118  explicit
119  vecN(const T &value)
120  {
121  for(size_type i = 0; i < N; ++i)
122  {
123  operator[](i) = T(value);
124  }
125  }
126 
127  /*!
128  * Copy constructor from array of different size.
129  * Calls T::operator= on each array element, if M<N then
130  * for each element beyond M, T::operator=
131  * is called with the parameter value.
132  * \tparam M size of other array.
133  * \param obj constant reference to copy elements from.
134  * \param value constant reference for value to use beyond index M
135  */
136  template<size_t M>
137  explicit
138  vecN(const vecN<T, M> &obj, const T &value = T())
139  {
140  size_type i;
141 
142  for(i = 0; i < t_min(N, M); ++i)
143  {
144  operator[](i) = obj[i];
145  }
146 
147  for(; i < N; ++i)
148  {
149  operator[](i) = value;
150  }
151  }
152 
153  /*!
154  * Ctor
155  * Calls T::operator= on each array element, if M<N then
156  * for each element beyond M, T::operator=
157  * is called with the parameter value.
158  * \tparam M size of other array.
159  * \param obj constant reference to copy elements from.
160  * \param value constant reference for value to use beyond index M
161  */
162  template<typename S, size_type M>
163  explicit
164  vecN(const vecN<S, M> &obj, const T &value = T())
165  {
166  size_type i;
167 
168  for(i = 0; i < t_min(N, M); ++i)
169  {
170  operator[](i) = T(obj[i]);
171  }
172 
173  for(; i < N; ++i)
174  {
175  operator[](i) = value;
176  }
177  }
178 
179  /*!
180  * Copy constructor from array of different size specifying the range
181  * Copies every stride'th value stored at obj starting at index start
182  * to this. For elements of this which are not assigned in this
183  * fashion, they are assigned as default_value.
184  * \tparam M size of other array.
185  * \param obj constant reference to copy elements from.
186  * \param start first index of M to use
187  * \param stride stride of copying
188  * \param default_value
189  */
190  template<size_t M>
191  vecN(const vecN<T, M> &obj,
192  size_type start, size_type stride = 1,
193  const T &default_value = T())
194  {
195  size_type i,j;
196 
197  for(i = 0, j = start; i < N && j < M; ++i, j += stride)
198  {
199  operator[](i) = obj[i];
200  }
201 
202  for(; i < N; ++i)
203  {
204  operator[](i) = default_value;
205  }
206  }
207 
208  /*!
209  * Conveniance ctor, will fail to compile unless N=2.
210  * \param px value to which to assing the return value of x().
211  * \param py value to which to assing the return value of y().
212  */
213  vecN(const T &px, const T &py)
214  {
215  FASTUIDRAWstatic_assert(N == 2);
216  operator[](0) = px;
217  operator[](1) = py;
218  }
219 
220  /*!
221  * Conveniance ctor, will fail to compile unless N=3.
222  * \param px value to which to assing the return value of x().
223  * \param py value to which to assing the return value of y().
224  * \param pz value to which to assing the return value of z().
225  */
226  vecN(const T &px, const T &py, const T &pz)
227  {
228  FASTUIDRAWstatic_assert(N == 3);
229  operator[](0) = px;
230  operator[](1) = py;
231  operator[](2) = pz;
232  }
233 
234  /*!
235  * Conveniance ctor, will fail to compile unless N=4.
236  * \param px value to which to assing the return value of x().
237  * \param py value to which to assing the return value of y().
238  * \param pz value to which to assing the return value of z().
239  * \param pw value to which to assing the return value of w().
240  */
241  vecN(const T &px, const T &py, const T &pz, const T &pw)
242  {
243  FASTUIDRAWstatic_assert(N == 4);
244  operator[](0) = px;
245  operator[](1) = py;
246  operator[](2) = pz;
247  operator[](3) = pw;
248  }
249 
250  /*!
251  * Conveniance ctor, will fail to compile unless N=5
252  * \param p0 value to which to assing the return value of operator[](0)
253  * \param p1 value to which to assing the return value of operator[](1)
254  * \param p2 value to which to assing the return value of operator[](2)
255  * \param p3 value to which to assing the return value of operator[](3)
256  * \param p4 value to which to assing the return value of operator[](4)
257  */
258  vecN(const T &p0, const T &p1, const T &p2,
259  const T &p3, const T &p4)
260  {
261  FASTUIDRAWstatic_assert(N == 5);
262  operator[](0) = p0;
263  operator[](1) = p1;
264  operator[](2) = p2;
265  operator[](3) = p3;
266  operator[](4) = p4;
267  }
268 
269  /*!
270  * Conveniance ctor, will fail to compile unless N=6
271  * \param p0 value to which to assing the return value of operator[](0)
272  * \param p1 value to which to assing the return value of operator[](1)
273  * \param p2 value to which to assing the return value of operator[](2)
274  * \param p3 value to which to assing the return value of operator[](3)
275  * \param p4 value to which to assing the return value of operator[](4)
276  * \param p5 value to which to assing the return value of operator[](5)
277  */
278  vecN(const T &p0, const T &p1, const T &p2,
279  const T &p3, const T &p4, const T &p5)
280  {
281  FASTUIDRAWstatic_assert(N == 6);
282  operator[](0) = p0;
283  operator[](1) = p1;
284  operator[](2) = p2;
285  operator[](3) = p3;
286  operator[](4) = p4;
287  operator[](5) = p5;
288  }
289 
290  /*!
291  * Conveniance ctor, will fail to compile unless N=7
292  * \param p0 value to which to assing the return value of operator[](0)
293  * \param p1 value to which to assing the return value of operator[](1)
294  * \param p2 value to which to assing the return value of operator[](2)
295  * \param p3 value to which to assing the return value of operator[](3)
296  * \param p4 value to which to assing the return value of operator[](4)
297  * \param p5 value to which to assing the return value of operator[](5)
298  * \param p6 value to which to assing the return value of operator[](6)
299  */
300  vecN(const T &p0, const T &p1, const T &p2,
301  const T &p3, const T &p4, const T &p5,
302  const T &p6)
303  {
304  FASTUIDRAWstatic_assert(N == 7);
305  operator[](0) = p0;
306  operator[](1) = p1;
307  operator[](2) = p2;
308  operator[](3) = p3;
309  operator[](4) = p4;
310  operator[](5) = p5;
311  operator[](6) = p6;
312  }
313 
314  /*!
315  * Conveniance ctor, will fail to compile unless N=8
316  * \param p0 value to which to assing the return value of operator[](0)
317  * \param p1 value to which to assing the return value of operator[](1)
318  * \param p2 value to which to assing the return value of operator[](2)
319  * \param p3 value to which to assing the return value of operator[](3)
320  * \param p4 value to which to assing the return value of operator[](4)
321  * \param p5 value to which to assing the return value of operator[](5)
322  * \param p6 value to which to assing the return value of operator[](6)
323  * \param p7 value to which to assing the return value of operator[](7)
324  */
325  vecN(const T &p0, const T &p1, const T &p2,
326  const T &p3, const T &p4, const T &p5,
327  const T &p6, const T &p7)
328  {
329  FASTUIDRAWstatic_assert(N == 8);
330  operator[](0) = p0;
331  operator[](1) = p1;
332  operator[](2) = p2;
333  operator[](3) = p3;
334  operator[](4) = p4;
335  operator[](5) = p5;
336  operator[](6) = p6;
337  operator[](7) = p7;
338  }
339 
340  /*!
341  * Conveniance ctor, will fail to compile unless N=9
342  * \param p0 value to which to assing the return value of operator[](0)
343  * \param p1 value to which to assing the return value of operator[](1)
344  * \param p2 value to which to assing the return value of operator[](2)
345  * \param p3 value to which to assing the return value of operator[](3)
346  * \param p4 value to which to assing the return value of operator[](4)
347  * \param p5 value to which to assing the return value of operator[](5)
348  * \param p6 value to which to assing the return value of operator[](6)
349  * \param p7 value to which to assing the return value of operator[](7)
350  * \param p8 value to which to assing the return value of operator[](8)
351  */
352  vecN(const T &p0, const T &p1, const T &p2,
353  const T &p3, const T &p4, const T &p5,
354  const T &p6, const T &p7, const T &p8)
355  {
356  FASTUIDRAWstatic_assert(N == 9);
357  operator[](0) = p0;
358  operator[](1) = p1;
359  operator[](2) = p2;
360  operator[](3) = p3;
361  operator[](4) = p4;
362  operator[](5) = p5;
363  operator[](6) = p6;
364  operator[](7) = p7;
365  operator[](8) = p8;
366  }
367 
368  /*!
369  * Conveniance function.
370  * \param p gives valeus for array indices 0 to N-2 inclusive
371  * \param d gives value for array index N-1
372  */
373  vecN(const vecN<T, N-1> &p, const T &d)
374  {
376  for(size_type i = 0 ; i < N - 1; ++i)
377  {
378  operator[](i) = p[i];
379  }
380  operator[](N - 1) = d;
381  }
382 
383  /*!
384  * Swap operation
385  * \param obj object with which to swap
386  */
387  void
388  swap(vecN &obj)
389  {
390  for(size_type i = 0; i < N; ++i)
391  {
392  m_data[i].swap(obj.m_data[i]);
393  }
394  }
395 
396  /*!
397  * Returns a C-style pointer to the array.
398  */
399  T*
400  c_ptr(void) { return m_data; }
401 
402  /*!
403  * Returns a constant C-style pointer to the array.
404  */
405  const T*
406  c_ptr(void) const { return m_data; }
407 
408  /*!
409  * Return a constant refernce to the j'th element.
410  * \param j index of element to return.
411  */
412  const_reference
413  operator[](size_type j) const
414  {
415  FASTUIDRAWassert(j < N);
416  return c_ptr()[j];
417  }
418 
419  /*!
420  * Return a refernce to the j'th element.
421  * \param j index of element to return.
422  */
423  reference
424  operator[](size_type j)
425  {
426  FASTUIDRAWassert(j < N);
427  return c_ptr()[j];
428  }
429 
430  /*!
431  * Conveniance readability member function,
432  * equivalent to operator[](0).
433  */
434  reference
435  x(void) { FASTUIDRAWstatic_assert(N >= 1); return c_ptr()[0]; }
436 
437  /*!
438  * Conveniance readability member function,
439  * equivalent to operator[](1). Fails to compile
440  * if N is not atleast 2.
441  */
442  reference
443  y(void) { FASTUIDRAWstatic_assert(N >= 2); return c_ptr()[1]; }
444 
445  /*!
446  * Conveniance readability member function,
447  * equivalent to operator[](2). Fails to compile
448  * if N is not atleast 3.
449  */
450  reference
451  z(void) { FASTUIDRAWstatic_assert(N >= 3); return c_ptr()[2]; }
452 
453  /*!
454  * Conveniance readability member function,
455  * equivalent to operator[](3). Fails to compile
456  * if N is not atleast 4.
457  */
458  reference
459  w(void) { FASTUIDRAWstatic_assert(N >= 4); return c_ptr()[3]; }
460 
461  /*!
462  * Conveniance readability member function,
463  * equivalent to operator[](0).
464  */
465  const_reference
466  x(void) const { FASTUIDRAWstatic_assert(N >= 1); return c_ptr()[0]; }
467 
468  /*!
469  * Conveniance readability member function,
470  * equivalent to operator[](1). Fails to compile
471  * if N is not atleast 2.
472  */
473  const_reference
474  y(void) const { FASTUIDRAWstatic_assert(N >= 2); return c_ptr()[1]; }
475 
476  /*!
477  * Conveniance readability member function,
478  * equivalent to operator[](2). Fails to compile
479  * if N is not atleast 3.
480  */
481  const_reference
482  z(void) const { FASTUIDRAWstatic_assert(N >= 3); return c_ptr()[2]; }
483 
484  /*!
485  * Conveniance readability member function,
486  * equivalent to operator[](3). Fails to compile
487  * if N is not atleast 4.
488  */
489  const_reference
490  w(void) const { FASTUIDRAWstatic_assert(N >= 4); return c_ptr()[3]; }
491 
492  /*!
493  * Assignment operator, performs operator=(T&, const T&)
494  * on each element.
495  * \param obj constant reference to a same sized array.
496  */
497  const vecN&
498  operator=(const vecN &obj)
499  {
500  if (&obj != this)
501  {
502  for(size_type i = 0; i < N; ++i)
503  {
504  operator[](i) = obj[i];
505  }
506  }
507  return *this;
508  }
509 
510  /*!
511  * Set all values of array, performs operator=(T&, const T&)
512  * on each element of the array agains obj.
513  * \param obj Value to set all objects as.
514  */
515  const vecN&
516  fill(const T &obj)
517  {
518  for(size_type i = 0; i < N; ++i)
519  {
520  operator[](i) = obj;
521  }
522 
523  return *this;
524  }
525 
526  /*!
527  * Component-wise negation operator.
528  * returns the componenet-wise negation
529  */
530  vecN
531  operator-(void) const
532  {
533  vecN retval;
534  for(size_type i = 0; i < N; ++i)
535  {
536  retval[i] = -operator[](i);
537  }
538  return retval;
539  }
540 
541  /*!
542  * Compoenent wise addition operator
543  * returns the componenet wise addition of two
544  * arrays.
545  * \param obj right hand side of + operator
546  */
547  vecN
548  operator+(const vecN &obj) const
549  {
550  vecN retval(*this);
551  retval += obj;
552  return retval;
553  }
554 
555  /*!
556  * Component-wise subtraction operator
557  * returns the componenet-wise subtraction of two
558  * arrays.
559  * \param obj right hand side of - operator
560  */
561  vecN
562  operator-(const vecN &obj) const
563  {
564  vecN retval(*this);
565  retval -= obj;
566  return retval;
567  }
568 
569  /*!
570  * Component-wise multiplication operator,
571  * returns the componenet-wise multiplication of
572  * two arrays.
573  * \param obj right hand side of * operator
574  */
575  vecN
576  operator*(const vecN &obj) const
577  {
578  vecN retval(*this);
579  retval *= obj;
580  return retval;
581  }
582 
583  /*!
584  * Component-wise division operator,
585  * returns the componenet-wise division of
586  * two arrays.
587  * \param obj right hand side of / operator
588  */
589  vecN
590  operator/(const vecN &obj) const
591  {
592  vecN retval(*this);
593  retval /= obj;
594  return retval;
595  }
596 
597  /*!
598  * Component-wise modulas operator
599  * returns the componenet-wise modulas of
600  * two arrays.
601  * \param obj right hand side of % operator
602  */
603  vecN
604  operator%(const vecN &obj) const
605  {
606  vecN retval(*this);
607  retval %= obj;
608  return retval;
609  }
610 
611  /*!
612  * Component-wise multiplication operator against a singleton
613  * \param obj right hand side of * operator
614  */
615  vecN
616  operator*(const T &obj) const
617  {
618  vecN retval(*this);
619  retval *= obj;
620  return retval;
621  }
622 
623  /*!
624  * Component-wise division against a singleton
625  * \param obj right hand side of / operator
626  */
627  vecN
628  operator/(const T &obj) const
629  {
630  vecN retval(*this);
631  retval /= obj;
632  return retval;
633  }
634 
635  /*!
636  * Component-wise modulas against a singleton
637  * \param obj right hand side of % operator
638  */
639  vecN
640  operator%(const T &obj) const
641  {
642  vecN retval(*this);
643  retval %= obj;
644  return retval;
645  }
646 
647  /*!
648  * Component-wise addition increment operator against an
649  * array of possibly different size, if M is smaller
650  * then only those indexes less than M are affected.
651  * \param obj right hand side of += operator
652  */
653  template<size_t M>
654  void
655  operator+=(const vecN<T, M> &obj)
656  {
657  for(size_type i = 0;i < t_min(M, N); ++i)
658  {
659  operator[](i) += obj[i];
660  }
661  }
662 
663  /*!
664  * Component-wise subtraction increment operator against an
665  * array of possibly different size, if M is smaller
666  * then only those indexes less than M are affected.
667  * \param obj right hand side of -= operator
668  */
669  template<size_t M>
670  void
671  operator-=(const vecN<T, M> &obj)
672  {
673  for(size_type i = 0; i < t_min(M, N); ++i)
674  {
675  operator[](i) -= obj[i];
676  }
677  }
678 
679  /*!
680  * Component-wise multiplication increment operator against an
681  * array of possibly different size, if M is smaller
682  * then only those indexes less than M are affected.
683  * \param obj right hand side of *= operator
684  */
685  template<size_t M>
686  void
687  operator*=(const vecN<T, M> &obj)
688  {
689  for(size_type i = 0; i < t_min(N, M); ++i)
690  {
691  operator[](i) *= obj[i];
692  }
693  }
694 
695  /*!
696  * Component-wise division increment operator against an
697  * array of possibly different size, if M is smaller
698  * then only those indexes less than M are affected.
699  * \param obj right hand side of /= operator
700  */
701  template<size_t M>
702  void
703  operator/=(const vecN<T, M> &obj)
704  {
705  for(size_type i = 0; i < t_min(M, N); ++i)
706  {
707  operator[](i) /= obj[i];
708  }
709  }
710 
711  /*!
712  * Component-wise modulas increment operator against an
713  * array of possibly different size, if M is smaller
714  * then only those indexes less than M are affected.
715  * \param obj right hand side of /= operator
716  */
717  template<size_t M>
718  void
719  operator%=(const vecN<T, M> &obj)
720  {
721  for(size_type i = 0; i < t_min(M, N); ++i)
722  {
723  operator[](i) %= obj[i];
724  }
725  }
726 
727  /*!
728  * Increment multiply operator against a singleton,
729  * i.e. increment multiple each element of this against
730  * the passed T value.
731  * \param obj right hind side of operator*=
732  */
733  void
734  operator*=(const T &obj)
735  {
736  for(size_type i = 0; i < N; ++i)
737  {
738  operator[](i) *= obj;
739  }
740  }
741 
742  /*!
743  * Increment divide operator against a singleton,
744  * i.e. increment divide each element of this against
745  * the passed T value.
746  * \param obj right hind side of operator/=
747  */
748  void
749  operator/=(const T &obj)
750  {
751  for(size_type i = 0; i < N; ++i)
752  {
753  operator[](i) /= obj;
754  }
755  }
756 
757  /*!
758  * Increment divide operator against a singleton,
759  * i.e. increment divide each element of this against
760  * the passed T value.
761  * \param obj right hind side of operator/=
762  */
763  void
764  operator%=(const T &obj)
765  {
766  for(size_type i = 0; i < N; ++i)
767  {
768  operator[](i) %= obj;
769  }
770  }
771 
772  /*!
773  * Component-wise multiplication against a singleton
774  * \param obj left hand side of * operator
775  * \param vec right hand side of * operator
776  */
777  friend
778  vecN operator*(const T &obj, const vecN &vec)
779  {
780  vecN retval(obj);
781  retval *= vec;
782  return retval;
783  }
784 
785  /*!
786  * Component-wise divition against a singleton
787  * \param obj left hand side of / operator
788  * \param vec right hand side of / operator
789  */
790  friend
791  vecN operator/(const T &obj, const vecN &vec)
792  {
793  vecN retval(obj);
794  retval /= vec;
795  return retval;
796  }
797 
798  /*!
799  * Performs inner product against another vecN.
800  * uses operator+=(T&, const T&) and operator*(T, T)
801  * \param obj vecN to perform inner product against
802  */
803  T
804  dot(const vecN &obj) const
805  {
806  T retval(operator[](0) * obj[0]);
807 
808  for(size_type i = 1; i < N; ++i)
809  {
810  retval += operator[](i) * obj[i];
811  }
812  return retval;
813  }
814 
815  /*!
816  * Conveninace function, equivalent to
817  * \code dot(*this) \endcode
818  */
819  T
820  magnitudeSq(void) const
821  {
822  return dot(*this);
823  }
824 
825  /*!
826  * Conveninace function, equivalent to
827  * \code t_sqrt(dot(*this)) \endcode
828  */
829  T
830  magnitude(void) const
831  {
832  return t_sqrt(magnitudeSq());
833  }
834 
835  /*!
836  * Computes the sum of t_abs() of each
837  * of the elements of the vecN.
838  */
839  T
840  L1norm(void) const
841  {
842  T retval(t_abs(operator[](0)));
843 
844  for(size_type i = 1; i < N; ++i)
845  {
846  retval += t_abs(operator[](i));
847  }
848  return retval;
849  }
850 
851  /*!
852  * Conveniance function, increments
853  * this vecN by dood*mult by doing so
854  * on each component individually,
855  * slighly more efficient than
856  * operator+=(dood*mult).
857  */
858  void
859  AddMult(const vecN &dood, const T &mult)
860  {
861  for(size_type i = 0; i < N; ++i)
862  {
863  operator[](i) += mult * dood[i];
864  }
865  }
866 
867  /*!
868  * Conveniance geometryic function; if dot(*this, referencePt)
869  * is negative, negates the elements of this
870  * \param referencePt refrence point to face forward to.
871  */
872  void
873  face_forward(const vecN &referencePt)
874  {
875  T val;
876 
877  val = dot(*this, referencePt);
878  if (val < T(0))
879  {
880  for(size_type i = 0; i < N; ++i)
881  {
882  operator[](i) = -operator[](i);
883  }
884  }
885  }
886 
887  /*!
888  * Equality operator by checking each array index individually.
889  * \param obj vecN to which to compare.
890  */
891  bool
892  operator==(const vecN &obj) const
893  {
894  if (this == &obj)
895  {
896  return true;
897  }
898  for(size_type i = 0; i < N; ++i)
899  {
900  if (obj[i] != operator[](i))
901  {
902  return false;
903  }
904  }
905  return true;
906  }
907 
908  /*!
909  * Provided as a conveniance, equivalent to
910  * \code
911  * !operator==(obj)
912  * \endcode
913  * \param obj vecN to which to compare.
914  */
915  bool
916  operator!=(const vecN &obj) const
917  {
918  return !operator==(obj);
919  }
920 
921  /*!
922  * Returns lexographical operator< by performing
923  * on individual elements.
924  * \param obj vecN to which to compare.
925  */
926  bool
927  operator<(const vecN &obj) const
928  {
929  if (this == &obj)
930  {
931  return false;
932  }
933  for(size_type i = 0; i < N; ++i)
934  {
935  if (operator[](i) < obj[i])
936  {
937  return true;
938  }
939  if (obj[i] < operator[](i))
940  {
941  return false;
942  }
943  }
944  return false;
945  }
946 
947  /*!
948  * Provided as a conveniance, equivalent to
949  * \code
950  * operator<(obj) || operator==(obj);
951  * \endcode
952  * \param obj vecN to which to compare.
953  */
954  bool
955  operator<=(const vecN &obj) const
956  {
957  return operator<(obj) || operator==(obj);
958  }
959 
960  /*!
961  * Provided as a conveniance, equivalent to
962  * \code
963  * obj.operator<(*this)
964  * \endcode
965  * \param obj vecN to which to compare.
966  */
967  bool
968  operator>(const vecN &obj) const
969  {
970  return obj.operator<(*this);
971  }
972 
973  /*!
974  * Provided as a conveniance, equivalent to
975  * \code
976  * !operator<(obj)
977  * \endcode
978  * \param obj vecN to which to compare.
979  */
980  bool
981  operator>=(const vecN &obj) const
982  {
983  return !operator<(obj);
984  }
985 
986  /*!
987  * Normalize this vecN up to a tolerance,
988  * equivalent to
989  * \code
990  * operator/=(t_sqrt(t_max(tol, magnitudeSq())))
991  * \endcode
992  * \param tol tolerance to avoid dividing by too small values
993  */
994  void
995  normalize(T tol)
996  {
997  T denom;
998  denom = magnitudeSq();
999  denom = t_sqrt(t_max(denom, tol));
1000  (*this) /= denom;
1001  }
1002 
1003  /*!
1004  * Normalize this vecN to a default tolerancee,
1005  * equivalent to
1006  * \code
1007  * normalize( T(0.00001*0.00001)
1008  * \endcode
1009  */
1010  void
1012  {
1013  normalize(T(0.00001*0.00001) );
1014  }
1015 
1016  /*!
1017  * Returns the vector that would be made
1018  * by calling normalize(void).
1019  */
1020  vecN
1021  unit_vector(void) const
1022  {
1023  vecN retval(*this);
1024  retval.normalize();
1025  return retval;
1026  }
1027 
1028  /*!
1029  * Returns the vector that would be made
1030  * by calling normalize(T).
1031  */
1032  vecN
1033  unit_vector(T tol) const
1034  {
1035  vecN retval(*this);
1036  retval.normalize(tol);
1037  return retval;
1038  }
1039 
1040  /*!
1041  * Conveniance function only for N = 2, equivalent to
1042  * \code
1043  * t_atan(y(), x())
1044  * \endcode
1045  */
1046  T
1047  atan(void) const
1048  {
1049  FASTUIDRAWstatic_assert(N == 2);
1050  return t_atan2(y(), x());
1051  }
1052 
1053  /*!
1054  * STL compliand size function, note that it is static
1055  * since the size of the array is determined by the template
1056  * parameter N.
1057  */
1058  static
1059  size_t
1060  size(void) { return static_cast<size_type>(N); }
1061 
1062  /*!
1063  * STL compliant iterator function.
1064  */
1065  iterator
1066  begin(void) { return iterator(c_ptr()); }
1067 
1068  /*!
1069  * STL compliant iterator function.
1070  */
1071  const_iterator
1072  begin(void) const { return const_iterator(c_ptr()); }
1073 
1074  /*!
1075  * STL compliant iterator function.
1076  */
1077  iterator
1078  end(void) { return iterator(c_ptr() + static_cast<difference_type>(size()) ); }
1079 
1080  /*!
1081  * STL compliant iterator function.
1082  */
1083  const_iterator
1084  end(void) const { return const_iterator(c_ptr() + static_cast<difference_type>(size()) ); }
1085 
1086  /*!
1087  * STL compliant back() function.
1088  */
1089  reference
1090  back(void) { return (*this)[size() - 1]; }
1091 
1092  /*!
1093  * STL compliant back() function.
1094  */
1095  const_reference
1096  back(void) const { return (*this)[size() - 1]; }
1097 
1098  /*!
1099  * STL compliant front() function.
1100  */
1101  reference
1102  front(void) { return (*this)[0]; }
1103 
1104  /*!
1105  * STL compliant front() function.
1106  */
1107  const_reference
1108  front(void) const { return (*this)[0]; }
1109 
1110 private:
1111  T m_data[N];
1112 };
1113 
1114 /*!
1115  * conveniance function, equivalent to
1116  * \code
1117  * a.dot(b)
1118  * \endcode
1119  * \param a object to perform dot
1120  * \param b object passed as parameter to dot.
1121  */
1122 template<typename T, size_t N>
1123 T
1124 dot(const vecN<T, N> &a, const vecN<T, N> &b)
1125 {
1126  return a.dot(b);
1127 }
1128 
1129 /*!
1130  * conveniance function, equivalent to
1131  * \code
1132  * in.magnitudeSq()
1133  * \endcode
1134  * \param in object to perform magnitudeSq
1135  */
1136 template<typename T, size_t N>
1137 inline
1138 T
1140 {
1141  return in.magnitudeSq();
1142 }
1143 
1144 /*!
1145  * conveniance function, equivalent to
1146  * \code
1147  * in.magnitude()
1148  * \endcode
1149  * \param in object to perform magnitude
1150  */
1151 template<typename T, size_t N>
1152 inline
1153 T
1155 {
1156  return in.magnitude();
1157 }
1158 
1159 /*!
1160  * conveniance function to compare magnitude squares, equivalent to
1161  * \code
1162  * a.magnitudeSq()<b.magnitudeSq();
1163  * \endcode
1164  * \param a left hand side of comparison
1165  * \param b right hand side of comparison
1166  */
1167 template<typename T, size_t N>
1168 bool
1170  const vecN<T, N> &b)
1171 {
1172  return a.magnitudeSq()<b.magnitudeSq();
1173 }
1174 
1175  /*!
1176  * unvecN extracts from a type T \ref vecN::array_size
1177  * and \ref vecN::value_type if T is a vecN<S, N> for
1178  * some type S and N.
1179  */
1180  template<typename T>
1181  class unvecN
1182  {
1183  public:
1184  enum
1185  {
1186  /*!
1187  * If T is the type vecN<S, N> for some type S,
1188  * then is the value of N, otherwise is 1.
1189  */
1191  };
1192 
1193  /*!
1194  * If T is the type vecN<S, N> for some type S,
1195  * then is the type S, otherwise is the type T.
1196  */
1197  typedef T type;
1198  };
1199 
1200  ///@cond
1201  template<typename T, size_t N>
1202  class unvecN<vecN<T, N> >
1203  {
1204  public:
1205  enum
1206  {
1207  array_size = N,
1208  };
1209  typedef T type;
1210  };
1211 
1212  template<typename T, size_t N>
1213  class unvecN<vecN<T, N> const >
1214  {
1215  public:
1216  enum
1217  {
1218  array_size = N,
1219  };
1220  typedef T const type;
1221  };
1222  ///@endcond
1223 
1224  /*!
1225  * Conveniance typedef
1226  */
1228  /*!
1229  * Conveniance typedef
1230  */
1232  /*!
1233  * Conveniance typedef
1234  */
1236  /*!
1237  * Conveniance typedef
1238  */
1240 
1241  /*!
1242  * Conveniance typedef
1243  */
1245  /*!
1246  * Conveniance typedef
1247  */
1249  /*!
1250  * Conveniance typedef
1251  */
1253  /*!
1254  * Conveniance typedef
1255  */
1257 
1258  /*!
1259  * Conveniance typedef
1260  */
1262  /*!
1263  * Conveniance typedef
1264  */
1266  /*!
1267  * Conveniance typedef
1268  */
1270  /*!
1271  * Conveniance typedef
1272  */
1274 
1275  /*!
1276  * Conveniance typedef
1277  */
1279  /*!
1280  * Conveniance typedef
1281  */
1283  /*!
1284  * Conveniance typedef
1285  */
1287  /*!
1288  * Conveniance typedef
1289  */
1291 
1292  /*!
1293  * Conveniance typedef
1294  */
1296  /*!
1297  * Conveniance typedef
1298  */
1300  /*!
1301  * Conveniance typedef
1302  */
1304  /*!
1305  * Conveniance typedef
1306  */
1308 
1309  /*!
1310  * Conveniance typedef
1311  */
1313  /*!
1314  * Conveniance typedef
1315  */
1317  /*!
1318  * Conveniance typedef
1319  */
1321  /*!
1322  * Conveniance typedef
1323  */
1325 
1326  /*!
1327  * Conveniance typedef
1328  */
1330  /*!
1331  * Conveniance typedef
1332  */
1334  /*!
1335  * Conveniance typedef
1336  */
1338  /*!
1339  * Conveniance typedef
1340  */
1342 
1343  /*!
1344  * Conveniance typedef
1345  */
1347  /*!
1348  * Conveniance typedef
1349  */
1351  /*!
1352  * Conveniance typedef
1353  */
1355  /*!
1356  * Conveniance typedef
1357  */
1359 
1360  /*!
1361  * Conveniance typedef
1362  */
1364  /*!
1365  * Conveniance typedef
1366  */
1368  /*!
1369  * Conveniance typedef
1370  */
1372  /*!
1373  * Conveniance typedef
1374  */
1376 
1377  /*!
1378  * Conveniance typedef
1379  */
1381  /*!
1382  * Conveniance typedef
1383  */
1385  /*!
1386  * Conveniance typedef
1387  */
1389  /*!
1390  * Conveniance typedef
1391  */
1393 
1394  /*!
1395  * Conveniance typedef
1396  */
1398  /*!
1399  * Conveniance typedef
1400  */
1402  /*!
1403  * Conveniance typedef
1404  */
1406  /*!
1407  * Conveniance typedef
1408  */
1410 
1411  /*!
1412  * Conveniance typedef
1413  */
1415  /*!
1416  * Conveniance typedef
1417  */
1419  /*!
1420  * Conveniance typedef
1421  */
1423  /*!
1424  * Conveniance typedef
1425  */
1427 
1428 /*!
1429  * Pack 4 float values into a uvec4
1430  * values via pack_float().
1431  * \param x x-value
1432  * \param y y-value
1433  * \param z z-value
1434  * \param w w-value
1435  */
1436 inline
1437 uvec4
1438 pack_vec4(float x, float y, float z, float w)
1439 {
1440  uvec4 return_value;
1441  return_value.x() = pack_float(x);
1442  return_value.y() = pack_float(y);
1443  return_value.z() = pack_float(z);
1444  return_value.w() = pack_float(w);
1445  return return_value;
1446 }
1447 
1448 /*!
1449  * Pack a \ref vec4 into a uvec4
1450  * values via pack_float().
1451  * \param v value to pack
1452  */
1453 inline
1454 uvec4
1455 pack_vec4(const vec4 &v)
1456 {
1457  return pack_vec4(v[0], v[1], v[2], v[3]);
1458 }
1459 
1460  /*!
1461  * Compute the area of a triangle using Heron's rule.
1462  */
1463  template<typename T, size_t N>
1464  T
1466  const vecN<T, N> &p1,
1467  const vecN<T, N> &p2)
1468  {
1469  T d, d0, d1, d2;
1470  d0 = p0.magnitude();
1471  d1 = p1.magnitude();
1472  d2 = p2.magnitude();
1473  d = (d0 + d1 + d2) / T(2);
1474  return t_sqrt(d * (d - d0) * (d - d1) * (d - d2));
1475  }
1476 
1477  /*!
1478  * Compute the cross produces of two vectors
1479  */
1480  template<typename T>
1481  vecN<T, 3>
1483  {
1484  return vecN<T, 3>(a.y() * b.z() - a.z() * b.y(),
1485  a.z() * b.x() - a.x() * b.z(),
1486  a.x() * b.y() - a.y() * b.x());
1487  }
1488 
1489 /*! @} */
1490 
1491 } //namespace
1492 
1493 #endif
vecN< uint64_t, 4 > u64vec4
Definition: vecN.hpp:1426
vecN(const T &px, const T &py, const T &pz, const T &pw)
Definition: vecN.hpp:241
vecN< int8_t, 1 > i8vec1
Definition: vecN.hpp:1295
T * c_ptr(void)
Definition: vecN.hpp:400
vecN(const T &p0, const T &p1, const T &p2, const T &p3, const T &p4, const T &p5, const T &p6, const T &p7, const T &p8)
Definition: vecN.hpp:352
vecN< uint16_t, 1 > u16vec1
Definition: vecN.hpp:1380
T * pointer
STL compliant typedef.
Definition: vecN.hpp:57
const_reference back(void) const
Definition: vecN.hpp:1096
vecN< int8_t, 3 > i8vec3
Definition: vecN.hpp:1303
void operator/=(const T &obj)
Definition: vecN.hpp:749
friend vecN operator/(const T &obj, const vecN &vec)
Definition: vecN.hpp:791
const T & const_reference
STL compliant typedef.
Definition: vecN.hpp:75
vecN< uint16_t, 2 > u16vec2
Definition: vecN.hpp:1384
void operator*=(const T &obj)
Definition: vecN.hpp:734
vecN< uint32_t, 2 > u32vec2
Definition: vecN.hpp:1401
T L1norm(void) const
Definition: vecN.hpp:840
vecN< int32_t, 1 > ivec1
Definition: vecN.hpp:1261
float t_atan2(float y, float x)
Definition: math.hpp:124
T magnitude(void) const
Definition: vecN.hpp:830
vecN operator-(void) const
Definition: vecN.hpp:531
all classes and functions of FastUIDraw are in the namespace fastuidraw.
Definition: colorstop.hpp:28
vecN operator+(const vecN &obj) const
Definition: vecN.hpp:548
void normalize(T tol)
Definition: vecN.hpp:995
const_reference y(void) const
Definition: vecN.hpp:474
T magnitudeSq(void) const
Definition: vecN.hpp:820
const T * c_ptr(void) const
Definition: vecN.hpp:406
vecN(const vecN< T, M > &obj, size_type start, size_type stride=1, const T &default_value=T())
Definition: vecN.hpp:191
vecN< int16_t, 1 > i16vec1
Definition: vecN.hpp:1312
vecN(const T &p0, const T &p1, const T &p2, const T &p3, const T &p4, const T &p5, const T &p6)
Definition: vecN.hpp:300
pointer iterator
iterator typedef to pointer
Definition: vecN.hpp:99
bool operator<=(const vecN &obj) const
Definition: vecN.hpp:955
const vecN & fill(const T &obj)
Definition: vecN.hpp:516
vecN(const T &p0, const T &p1, const T &p2, const T &p3, const T &p4, const T &p5)
Definition: vecN.hpp:278
void operator/=(const vecN< T, M > &obj)
Definition: vecN.hpp:703
const vecN & operator=(const vecN &obj)
Definition: vecN.hpp:498
const_reference w(void) const
Definition: vecN.hpp:490
vecN unit_vector(void) const
Definition: vecN.hpp:1021
vecN< int32_t, 3 > i32vec3
Definition: vecN.hpp:1337
vecN< double, 2 > dvec2
Definition: vecN.hpp:1248
iterator end(void)
Definition: vecN.hpp:1078
vecN is a simple static array class with no virtual functions and no memory overhead. Supports runtim array index checking and STL style iterators via pointer iterators.
Definition: vecN.hpp:42
bool operator>(const vecN &obj) const
Definition: vecN.hpp:968
vecN< uint8_t, 2 > u8vec2
Definition: vecN.hpp:1367
vecN< uint32_t, 3 > uvec3
Definition: vecN.hpp:1286
vecN< uint64_t, 1 > u64vec1
Definition: vecN.hpp:1414
bool magnitude_compare(const vecN< T, N > &a, const vecN< T, N > &b)
Definition: vecN.hpp:1169
const_reference x(void) const
Definition: vecN.hpp:466
vecN(const T &value)
Definition: vecN.hpp:119
vecN< T, 3 > cross_product(const vecN< T, 3 > &a, const vecN< T, 3 > &b)
Definition: vecN.hpp:1482
vecN< int32_t, 1 > i32vec1
Definition: vecN.hpp:1329
vecN< uint32_t, 4 > uvec4
Definition: vecN.hpp:1290
vecN< int64_t, 2 > i64vec2
Definition: vecN.hpp:1350
reference operator[](size_type j)
Definition: vecN.hpp:424
vecN< int64_t, 4 > i64vec4
Definition: vecN.hpp:1358
iterator begin(void)
Definition: vecN.hpp:1066
reference z(void)
Definition: vecN.hpp:451
vecN< double, 1 > dvec1
Definition: vecN.hpp:1244
bool operator<(const vecN &obj) const
Definition: vecN.hpp:927
ptrdiff_t difference_type
STL compliant typedef.
Definition: vecN.hpp:93
const_pointer const_iterator
iterator typedef to const_pointer
Definition: vecN.hpp:105
vecN< float, 2 > vec2
Definition: vecN.hpp:1231
vecN< int32_t, 4 > ivec4
Definition: vecN.hpp:1273
vecN< uint32_t, 2 > uvec2
Definition: vecN.hpp:1282
vecN< uint64_t, 2 > u64vec2
Definition: vecN.hpp:1418
#define FASTUIDRAWstatic_assert(X)
Definition: util.hpp:118
vecN(const vecN< S, M > &obj, const T &value=T())
Definition: vecN.hpp:164
const T & t_max(const T &a, const T &b)
Definition: math.hpp:54
vecN< int16_t, 3 > i16vec3
Definition: vecN.hpp:1320
reference back(void)
Definition: vecN.hpp:1090
file math.hpp
vecN operator*(const vecN &obj) const
Definition: vecN.hpp:576
vecN unit_vector(T tol) const
Definition: vecN.hpp:1033
vecN operator/(const T &obj) const
Definition: vecN.hpp:628
vecN operator/(const vecN &obj) const
Definition: vecN.hpp:590
vecN(const T &p0, const T &p1, const T &p2, const T &p3, const T &p4, const T &p5, const T &p6, const T &p7)
Definition: vecN.hpp:325
vecN< int32_t, 2 > i32vec2
Definition: vecN.hpp:1333
T triangle_area(const vecN< T, N > &p0, const vecN< T, N > &p1, const vecN< T, N > &p2)
Definition: vecN.hpp:1465
vecN< uint32_t, 1 > u32vec1
Definition: vecN.hpp:1397
bool operator==(const vecN &obj) const
Definition: vecN.hpp:892
file util.hpp
void face_forward(const vecN &referencePt)
Definition: vecN.hpp:873
vecN< uint8_t, 4 > u8vec4
Definition: vecN.hpp:1375
uvec4 pack_vec4(float x, float y, float z, float w)
Definition: vecN.hpp:1438
vecN< float, 1 > vec1
Definition: vecN.hpp:1227
reference y(void)
Definition: vecN.hpp:443
vecN< int32_t, 3 > ivec3
Definition: vecN.hpp:1269
const T & t_min(const T &a, const T &b)
Definition: math.hpp:43
uint32_t pack_float(float f)
Definition: util.hpp:360
bool operator!=(const vecN &obj) const
Definition: vecN.hpp:916
vecN< uint32_t, 3 > u32vec3
Definition: vecN.hpp:1405
vecN operator%(const T &obj) const
Definition: vecN.hpp:640
const T * const_pointer
STL compliant typedef.
Definition: vecN.hpp:63
T dot(const vecN &obj) const
Definition: vecN.hpp:804
vecN(const vecN< T, N-1 > &p, const T &d)
Definition: vecN.hpp:373
float t_sqrt(float x)
Definition: math.hpp:96
void operator*=(const vecN< T, M > &obj)
Definition: vecN.hpp:687
reference w(void)
Definition: vecN.hpp:459
bool operator>=(const vecN &obj) const
Definition: vecN.hpp:981
vecN< uint16_t, 4 > u16vec4
Definition: vecN.hpp:1392
vecN< uint64_t, 3 > u64vec3
Definition: vecN.hpp:1422
vecN operator-(const vecN &obj) const
Definition: vecN.hpp:562
const_reference operator[](size_type j) const
Definition: vecN.hpp:413
vecN(const T &px, const T &py, const T &pz)
Definition: vecN.hpp:226
vecN< int16_t, 4 > i16vec4
Definition: vecN.hpp:1324
vecN< double, 4 > dvec4
Definition: vecN.hpp:1256
vecN(const vecN< T, M > &obj, const T &value=T())
Definition: vecN.hpp:138
const_iterator end(void) const
Definition: vecN.hpp:1084
vecN< uint16_t, 3 > u16vec3
Definition: vecN.hpp:1388
vecN< uint8_t, 1 > u8vec1
Definition: vecN.hpp:1363
vecN< float, 4 > vec4
Definition: vecN.hpp:1239
friend vecN operator*(const T &obj, const vecN &vec)
Definition: vecN.hpp:778
void operator-=(const vecN< T, M > &obj)
Definition: vecN.hpp:671
vecN< int8_t, 2 > i8vec2
Definition: vecN.hpp:1299
vecN< int32_t, 2 > ivec2
Definition: vecN.hpp:1265
static size_t size(void)
Definition: vecN.hpp:1060
const_reference front(void) const
Definition: vecN.hpp:1108
T & reference
STL compliant typedef.
Definition: vecN.hpp:69
T value_type
STL compliant typedef.
Definition: vecN.hpp:81
size_t size_type
STL compliant typedef.
Definition: vecN.hpp:87
reference x(void)
Definition: vecN.hpp:435
const_reference z(void) const
Definition: vecN.hpp:482
void operator+=(const vecN< T, M > &obj)
Definition: vecN.hpp:655
void operator%=(const T &obj)
Definition: vecN.hpp:764
void AddMult(const vecN &dood, const T &mult)
Definition: vecN.hpp:859
vecN< int64_t, 3 > i64vec3
Definition: vecN.hpp:1354
void swap(vecN &obj)
Definition: vecN.hpp:388
vecN(const T &p0, const T &p1, const T &p2, const T &p3, const T &p4)
Definition: vecN.hpp:258
vecN< uint32_t, 4 > u32vec4
Definition: vecN.hpp:1409
void normalize(void)
Definition: vecN.hpp:1011
vecN< int32_t, 4 > i32vec4
Definition: vecN.hpp:1341
vecN< int8_t, 4 > i8vec4
Definition: vecN.hpp:1307
vecN< int64_t, 1 > i64vec1
Definition: vecN.hpp:1346
int t_abs(int x)
Definition: math.hpp:243
vecN(const T &px, const T &py)
Definition: vecN.hpp:213
vecN< int16_t, 2 > i16vec2
Definition: vecN.hpp:1316
vecN< uint32_t, 1 > uvec1
Definition: vecN.hpp:1278
#define FASTUIDRAWassert(X)
Definition: util.hpp:99
const_iterator begin(void) const
Definition: vecN.hpp:1072
reference front(void)
Definition: vecN.hpp:1102
T atan(void) const
Definition: vecN.hpp:1047
vecN< double, 3 > dvec3
Definition: vecN.hpp:1252
vecN< float, 3 > vec3
Definition: vecN.hpp:1235
vecN operator%(const vecN &obj) const
Definition: vecN.hpp:604
vecN operator*(const T &obj) const
Definition: vecN.hpp:616
vecN< uint8_t, 3 > u8vec3
Definition: vecN.hpp:1371
void operator%=(const vecN< T, M > &obj)
Definition: vecN.hpp:719