Allolib  1.0
C++ Components For Interactive Multimedia
al_Vec.hpp
1 #ifndef INCLUDE_AL_VEC_HPP
2 #define INCLUDE_AL_VEC_HPP
3 
4 /* Allocore --
5  Multimedia / virtual environment application class library
6 
7  Copyright (C) 2009. AlloSphere Research Group, Media Arts & Technology, UCSB.
8  Copyright (C) 2012. The Regents of the University of California.
9  All rights reserved.
10 
11  Redistribution and use in source and binary forms, with or without
12  modification, are permitted provided that the following conditions are met:
13 
14  Redistributions of source code must retain the above copyright notice,
15  this list of conditions and the following disclaimer.
16 
17  Redistributions in binary form must reproduce the above copyright
18  notice, this list of conditions and the following disclaimer in the
19  documentation and/or other materials provided with the distribution.
20 
21  Neither the name of the University of California nor the names of its
22  contributors may be used to endorse or promote products derived from
23  this software without specific prior written permission.
24 
25  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
29  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  POSSIBILITY OF SUCH DAMAGE.
36 
37 
38  File description:
39  Generic fixed-size n-vector
40 
41  File author(s):
42  Lance Putnam, 2010, putnam.lance@gmail.com
43  Graham Wakefield, 2010, grrrwaaa@gmail.com
44 */
45 
46 #include <stdio.h>
47 #include <cmath>
48 #include <ostream>
49 #include "al/math/al_Constants.hpp"
50 
51 namespace al {
52 
53 template <int N, class T>
54 class Vec;
55 
58 typedef Vec<2, int> Vec2i;
61 typedef Vec<3, int> Vec3i;
64 typedef Vec<4, int> Vec4i;
65 
66 // Forward iterates from 0 to n-1. Current index is 'i'.
67 #define IT(n) for (int i = 0; i < (n); ++i)
68 
69 template <int N, class T>
70 struct VecElems {
71  T x, y, z, w;
72  private:
73  T data[N - 4];
74 };
75 
76 template <class T>
77 struct VecElems<0, T> {
78  static T x;
79 };
80 template <class T>
81 T VecElems<0, T>::x = 0;
82 
83 template <class T>
84 struct VecElems<1, T> {
85  T x;
86 };
87 template <class T>
88 struct VecElems<2, T> {
89  T x, y;
90 };
91 template <class T>
92 struct VecElems<3, T> {
93  T x, y, z;
94 
96  Vec<3, T> cross(const Vec<3, T>& b) const {
97  return Vec<3, T>(y * b.z - z * b.y, z * b.x - x * b.z, x * b.y - y * b.x);
98  }
99 };
100 template <class T>
101 struct VecElems<4, T> {
102  T x, y, z, w;
103 
104  Vec<4, T> cross(const Vec<4, T>& b) const {
105  return Vec<3, T>(y * b.z - z * b.y, z * b.x - x * b.z, x * b.y - y * b.x);
106  }
107 };
108 
110 
116 template <int N, class T>
117 class Vec : public VecElems<N, T> {
118  public:
119  using VecElems<N, T>::x;
120 
121  typedef T value_type;
122 
124  Vec(const T& v = T()) { set(v); }
125 
128  Vec(const T& v1, const T& v2) { set(v1, v2); }
129 
133  Vec(const T& v1, const T& v2, const T& v3) { set(v1, v2, v3); }
134 
139  Vec(const T& v1, const T& v2, const T& v3, const T& v4) {
140  set(v1, v2, v3, v4);
141  }
142 
144  template <int N2, class T2>
145  Vec(const Vec<N2, T2>& v) {
146  set(v);
147  }
148 
151  template <class Tv, class Ts>
152  Vec(const Vec<N - 1, Tv>& v, const Ts& s) {
153  set(v, s);
154  }
155 
158  template <class T2>
159  Vec(const T2* v, int stride = 1) {
160  set(v, stride);
161  }
162 
163  //--------------------------------------------------------------------------
164  // Memory Operations
165 
167  static int size() { return N; }
168 
170  static Vec& pun(T* src) { return *(Vec*)(src); }
171  static const Vec& pun(const T* src) { return *(const Vec*)(src); }
172 
174  template <class V>
175  V& as() {
176  return *(V*)(elems());
177  }
178 
179  template <class V>
180  const V& as() const {
181  return *(const V*)(elems());
182  }
183 
185  const T* elems() const { return &x; }
186 
188  T* elems() { return &x; }
189 
190  T* begin() { return elems(); }
191  const T* begin() const { return elems(); }
192  T* end() { return elems() + N; }
193  const T* end() const { return elems() + N; }
194 
196  T& operator[](size_t i) { return elems()[i]; }
197 
199  const T& operator[](size_t i) const { return elems()[i]; }
200 
202  bool operator==(const Vec& v) const {
203  IT(N) {
204  if ((*this)[i] != v[i]) return false;
205  }
206  return true;
207  }
208 
210  bool operator==(const T& v) const {
211  IT(N) {
212  if ((*this)[i] != v) return false;
213  }
214  return true;
215  }
216 
218  bool operator!=(const Vec& v) const { return !(*this == v); }
219 
221  bool operator!=(const T& v) const { return !(*this == v); }
222 
224  Vec<2, T> get(int i0, int i1) const {
225  return Vec<2, T>((*this)[i0], (*this)[i1]);
226  }
227 
229  Vec<3, T> get(int i0, int i1, int i2) const {
230  return Vec<3, T>((*this)[i0], (*this)[i1], (*this)[i2]);
231  }
232 
234  Vec<4, T> get(int i0, int i1, int i2, int i3) const {
235  return Vec<4, T>((*this)[i0], (*this)[i1], (*this)[i2], (*this)[i3]);
236  }
237 
239  template <int M>
240  Vec<M, T> sub(int begin = 0) const {
241  return Vec<M, T>(elems() + begin);
242  }
243 
244  template <int M>
245  Vec<M, T>& sub(int begin = 0) {
246  return *(Vec<M, T>*)(elems() + begin);
247  }
248 
249  //--------------------------------------------------------------------------
250  // Basic Arithmetic Operations
251 
252  Vec& operator=(const Vec& v) { return set(v); }
253  Vec& operator=(const T& v) { return set(v); }
254  Vec& operator+=(const Vec& v) {
255  IT(N)(*this)[i] += v[i];
256  return *this;
257  }
258  Vec& operator+=(const T& v) {
259  IT(N)(*this)[i] += v;
260  return *this;
261  }
262  Vec& operator-=(const Vec& v) {
263  IT(N)(*this)[i] -= v[i];
264  return *this;
265  }
266  Vec& operator-=(const T& v) {
267  IT(N)(*this)[i] -= v;
268  return *this;
269  }
270  Vec& operator*=(const Vec& v) {
271  IT(N)(*this)[i] *= v[i];
272  return *this;
273  }
274  Vec& operator*=(const T& v) {
275  IT(N)(*this)[i] *= v;
276  return *this;
277  }
278  Vec& operator/=(const Vec& v) {
279  IT(N)(*this)[i] /= v[i];
280  return *this;
281  }
282  Vec& operator/=(const T& v) {
283  IT(N)(*this)[i] /= v;
284  return *this;
285  }
286 
287  Vec operator+(const Vec& v) const { return Vec(*this) += v; }
288  Vec operator+(const T& v) const { return Vec(*this) += v; }
289  Vec operator-(const Vec& v) const { return Vec(*this) -= v; }
290  Vec operator-(const T& v) const { return Vec(*this) -= v; }
291  Vec operator*(const Vec& v) const { return Vec(*this) *= v; }
292  Vec operator*(const T& v) const { return Vec(*this) *= v; }
293  Vec operator/(const Vec& v) const { return Vec(*this) /= v; }
294  Vec operator/(const T& v) const { return Vec(*this) /= v; }
295  Vec operator-() const { return Vec(*this).negate(); }
296  bool operator>(const Vec& v) const { return magSqr() > v.magSqr(); }
297  bool operator<(const Vec& v) const { return magSqr() < v.magSqr(); }
298 
300  template <class T2>
301  Vec& set(const Vec<N, T2>& v) {
302  IT(N) { (*this)[i] = T(v[i]); }
303  return *this;
304  }
305 
307  template <int N2, class T2>
308  Vec& set(const Vec<N2, T2>& v) {
309  IT(N < N2 ? N : N2) { (*this)[i] = T(v[i]); }
310  return *this;
311  }
312 
314  template <class Tv, class Ts>
315  Vec& set(const Vec<N - 1, Tv>& v, const Ts& s) {
316  (*this)[N - 1] = s;
317  return set(v);
318  }
319 
321  Vec& set(const T& v) {
322  IT(N) { (*this)[i] = v; }
323  return *this;
324  }
325 
327  template <class T2>
328  Vec& set(const T2* v) {
329  IT(N) { (*this)[i] = T(v[i]); }
330  return *this;
331  }
332 
334  template <class T2>
335  Vec& set(const T2* v, int stride) {
336  IT(N) { (*this)[i] = T(v[i * stride]); }
337  return *this;
338  }
339 
341  Vec& set(const T& v1, const T& v2) { return set(v1, v2, v1, v1, v1, v1); }
342 
344  Vec& set(const T& v1, const T& v2, const T& v3) {
345  return set(v1, v2, v3, v1, v1, v1);
346  }
347 
349  Vec& set(const T& v1, const T& v2, const T& v3, const T& v4) {
350  return set(v1, v2, v3, v4, v1, v1);
351  }
352 
354  Vec& set(const T& v1, const T& v2, const T& v3, const T& v4, const T& v5) {
355  return set(v1, v2, v3, v4, v5, v1);
356  }
357 
359  Vec& set(const T& v1, const T& v2, const T& v3, const T& v4, const T& v5,
360  const T& v6) {
361  switch (N) {
362  default:
363  (*this)[5] = v6;
364  case 5:
365  (*this)[4] = v5;
366  case 4:
367  (*this)[3] = v4;
368  case 3:
369  (*this)[2] = v3;
370  case 2:
371  (*this)[1] = v2;
372  case 1:
373  (*this)[0] = v1;
374  }
375  return *this;
376  }
377 
379  Vec& zero() { return set(T(0)); }
380 
381  //--------------------------------------------------------------------------
382  // Linear Operations
383 
385 
388  template <int Dimension>
389  Vec by(T shift) const {
390  Vec res(*this);
391  res[Dimension] += shift;
392  return res;
393  }
394 
396  Vec byx(T shift) const { return by<0>(shift); }
397 
399  Vec byy(T shift) const { return by<1>(shift); }
400 
402  Vec byz(T shift) const { return by<2>(shift); }
403 
405  template <class U>
406  T dot(const Vec<N, U>& v) const {
407  T r = (*this)[0] * v[0];
408  for (int i = 1; i < N; ++i) {
409  r += (*this)[i] * v[i];
410  }
411  return r;
412  }
413 
415  T mag() const { return std::sqrt(magSqr()); }
416 
418  T magSqr() const { return dot(*this); }
419 
421 
424  T norm(const T& p) const {
425  T r = std::pow(std::abs((*this)[0]), p);
426  for (int i = 1; i < N; ++i) {
427  r += std::pow(std::abs((*this)[i]), p);
428  }
429  return std::pow(r, T(1) / p);
430  }
431 
433  T norm1() const { return sumAbs(); }
434 
436  T norm2() const { return mag(); }
437 
439  T product() const {
440  T r = (*this)[0];
441  for (int i = 1; i < N; ++i) {
442  r *= (*this)[i];
443  }
444  return r;
445  }
446 
448  T sum() const {
449  T r = (*this)[0];
450  for (int i = 1; i < N; ++i) {
451  r += (*this)[i];
452  }
453  return r;
454  }
455 
457  T sumAbs() const {
458  T r = std::abs((*this)[0]);
459  for (int i = 1; i < N; ++i) {
460  r += std::abs((*this)[i]);
461  }
462  return r;
463  }
464 
466  Vec& lerp(const Vec& target, T amt) {
467  return (*this) += (target - (*this)) * amt;
468  }
469 
471  Vec& mag(T v);
472 
474  Vec& negate() {
475  IT(N) { (*this)[i] = -(*this)[i]; }
476  return *this;
477  }
478 
480 
483  Vec& normalize(T scale = T(1));
484 
486 
489  Vec normalized(T scale = T(1)) const { return Vec(*this).normalize(scale); }
490 
492  Vec projection(const Vec& u) const { return dot(u) * u; }
493 
495 
498  Vec rejection(const Vec& u) const { return (*this) - projection(u); }
499 
501  Vec& reflect(const Vec& u) { return (*this) -= ((T(2) * dot(u)) * u); }
502 
504 
508  Vec& rotate(double angle, int dim1 = 0, int dim2 = 1) {
509  double a = cos(angle);
510  double b = sin(angle);
511  T t = (*this)[dim1];
512  T u = (*this)[dim2];
513  (*this)[dim1] = t * a - u * b;
514  (*this)[dim2] = t * b + u * a;
515  return *this;
516  }
517 
519  void print(FILE* out = stdout) const;
520 };
521 
522 // -----------------------------------------------------------------------------
523 // The following are functions that either cannot be defined as class methods
524 // (due to syntax rules or specialization) or simply are not object oriented.
525 
526 // Non-member binary arithmetic operations
527 template <int N, class T>
528 inline Vec<N, T> operator+(const T& s, const Vec<N, T>& v) {
529  return v + s;
530 }
531 
532 template <int N, class T>
533 inline Vec<N, T> operator-(const T& s, const Vec<N, T>& v) {
534  return -v + s;
535 }
536 
537 template <int N, class T>
538 inline Vec<N, T> operator*(const T& s, const Vec<N, T>& v) {
539  return v * s;
540 }
541 
542 template <int N, class T>
543 inline Vec<N, T> operator/(const T& s, const Vec<N, T>& v) {
544  Vec<N, T> r;
545  IT(N) { r[i] = s / v[i]; }
546  return r;
547 }
548 
549 // Specialized vector functions
550 
552 template <int N, class T>
553 inline T abs(const Vec<N, T>& v) {
554  return v.mag();
555 }
556 
558 template <int N1, class T1, int N2, class T2>
559 inline Vec<N1 + N2, T1> concat(const Vec<N1, T1>& a, const Vec<N2, T2>& b) {
561  r.set(a.elems());
562  for (int i = 0; i < N2; ++i) r[i + N1] = T1(b[i]);
563  return r;
564 }
565 
567 template <int M, int N, class T>
568 inline Vec<M, T> sub(const Vec<N, T>& v, int begin = 0) {
569  return v.template sub<M>(begin);
570 }
571 
573 template <class T>
574 inline void cross(Vec<3, T>& r, const Vec<3, T>& a, const Vec<3, T>& b) {
575  // r[0] = a[1]*b[2] - a[2]*b[1];
576  // r[1] = a[2]*b[0] - a[0]*b[2];
577  // r[2] = a[0]*b[1] - a[1]*b[0];
578  r = a.cross(b);
579 }
580 
582 template <class T>
583 inline Vec<3, T> cross(const Vec<3, T>& a, const Vec<3, T>& b) {
584  Vec<3, T> r;
585  cross(r, a, b);
586  return r;
587 }
588 
589 template <class T>
590 inline void cross(Vec<3, T>& r, const Vec<4, T>& a, const Vec<4, T>& b) {
591  // r[0] = a[1]*b[2] - a[2]*b[1];
592  // r[1] = a[2]*b[0] - a[0]*b[2];
593  // r[2] = a[0]*b[1] - a[1]*b[0];
594  r = a.cross(b);
595 }
596 
598 template <class T>
599 inline Vec<3, T> cross(const Vec<4, T>& a, const Vec<4, T>& b) {
600  Vec<3, T> r;
601  cross(r, a, b);
602  return r;
603 }
604 
606 
611 template <class T>
612 void rotate(Vec<3, T>& vec, const Vec<3, T>& normal, double cosAng,
613  double sinAng) {
614  T c = cosAng;
615  T s = sinAng;
616 
617  // Rodrigues' rotation formula:
618  vec = vec * c + cross(normal, vec) * s +
619  normal * (normal.dot(vec) * (T(1) - c));
620 }
621 
623 
627 template <class T>
628 void rotate(Vec<3, T>& vec, const Vec<3, T>& normal, double angle) {
629  rotate(vec, normal, cos(angle), sin(angle));
630 }
631 
633 template <int N, class T>
634 inline T angle(const Vec<N, T>& a, const Vec<N, T>& b) {
635  T cosAng = a.dot(b) / sqrt(a.magSqr() * b.magSqr());
636  if (cosAng >= T(1)) {
637  return T(0);
638  } else if (cosAng <= T(-1)) {
639  return T(M_PI);
640  }
641  return std::acos(cosAng);
642 }
643 
650 template <int N, class T>
651 inline void centroid3(Vec<N, T>& c, const Vec<N, T>& p1, const Vec<N, T>& p2,
652  const Vec<N, T>& p3) {
653  static const T _1_3 = T(1) / T(3);
654  c = (p1 + p2 + p3) * _1_3;
655 }
656 
658 template <int N, class T>
660  const Vec<N, T>& p) {
661  auto ab = b - a;
662  auto dot = (p - a).dot(ab); // projection of ap onto ab
663  auto magAB = ab.magSqr();
664  auto frac =
665  dot / magAB; // normalized distance along ab from a to the closest point
666 
667  // check if p projection is beyond endpoints of ab
668  if (frac < 0.) return a;
669  if (frac > 1.) return b;
670 
671  return a + ab * frac;
672 }
673 
675 template <int N, class T, class U>
676 inline T dist(const Vec<N, T>& a, const Vec<N, U>& b) {
677  return (a - b).mag();
678 }
679 
680 template <int N, class T>
681 inline Vec<N, T> lerp(const Vec<N, T>& input, const Vec<N, T>& target, T amt) {
682  return Vec<N, T>(input).lerp(target, amt);
683 }
684 
691 template <class T>
692 inline void normal(Vec<3, T>& n, const Vec<3, T>& p1, const Vec<3, T>& p2,
693  const Vec<3, T>& p3) {
694  cross(n, p2 - p1, p3 - p1);
695  n.normalize();
696 }
697 
699 template <int N, class T>
700 inline Vec<N, T> min(const Vec<N, T>& a, const Vec<N, T>& b) {
701  Vec<N, T> r;
702  IT(N) { r[i] = a[i] > b[i] ? b[i] : a[i]; }
703  return r;
704 }
705 
707 template <int N, class T>
708 inline Vec<N, T> max(const Vec<N, T>& a, const Vec<N, T>& b) {
709  Vec<N, T> r;
710  IT(N) { r[i] = a[i] < b[i] ? b[i] : a[i]; }
711  return r;
712 }
713 
714 // Implementation --------------------------------------------------------------
715 
716 template <int N, class T>
718  T m = mag();
719  if (m > T(1e-20)) {
720  (*this) *= (v / m);
721  } else {
722  set(T(0));
723  (*this)[0] = v;
724  }
725  return *this;
726 }
727 
728 template <int N, class T>
730  return mag(scale);
731 }
732 
733 template <typename T>
734 const char* typeString();
735 #define TypeString(A) \
736  template <> \
737  inline const char* typeString<A>() { \
738  return #A; \
739  }
740 TypeString(char) TypeString(unsigned char) TypeString(int)
741  TypeString(unsigned int) TypeString(float) TypeString(double)
742  TypeString(long double)
743 #undef TypeString
744 
745  template <int N, class T>
746  void Vec<N, T>::print(FILE* out) const {
747  fprintf(out, "{");
748  if (size()) {
749  fprintf(out, "%g", (double)((*this)[0]));
750  for (int i = 1; i < N; ++i) fprintf(out, ", %g", (double)((*this)[i]));
751  }
752  fprintf(out, "}");
753 }
754 
755 // Pretty-printing by Matt:
756 //
757 template <int N, class T>
758 std::ostream& operator<<(std::ostream& out, const Vec<N, T>& v) {
759  // out << "Vec<" << N << "," << typeString<T>() << "> = {";
760  // if(v.size()){
761  // out << v[0];
762  // for (int i = 1; i < N; ++i)
763  // out << ", " << v[i];
764  // }
765  // out << "}" << std::endl;
766  out << "{";
767  if (v.size()) {
768  out << v[0];
769  for (int i = 1; i < N; ++i) out << ", " << v[i];
770  }
771  out << "}";
772  return out;
773 }
774 
775 #undef IT
776 } // namespace al
777 #endif
Fixed-size n-vector.
Definition: al_Vec.hpp:117
T sumAbs() const
Returns sum of absolute value of elements.
Definition: al_Vec.hpp:457
Vec & set(const T &v1, const T &v2, const T &v3)
Set first 3 elements.
Definition: al_Vec.hpp:344
Vec & lerp(const Vec &target, T amt)
Linearly interpolate towards some target.
Definition: al_Vec.hpp:466
bool operator!=(const T &v) const
Return true if all elements are not equal to value, false otherwise.
Definition: al_Vec.hpp:221
T & operator[](size_t i)
Set element at index with no bounds checking.
Definition: al_Vec.hpp:196
T norm2() const
Return 2-norm of elements.
Definition: al_Vec.hpp:436
Vec & set(const T &v1, const T &v2, const T &v3, const T &v4, const T &v5, const T &v6)
Set first 6 elements.
Definition: al_Vec.hpp:359
Vec< 4, T > get(int i0, int i1, int i2, int i3) const
Get a vector comprised of indexed elements.
Definition: al_Vec.hpp:234
Vec & normalize(T scale=T(1))
Set magnitude to one without changing direction.
Definition: al_Vec.hpp:729
const T * elems() const
Get read-only pointer to elements.
Definition: al_Vec.hpp:185
Vec & set(const T &v1, const T &v2)
Set first 2 elements.
Definition: al_Vec.hpp:341
T norm(const T &p) const
Returns p-norm of elements.
Definition: al_Vec.hpp:424
T magSqr() const
Returns magnitude squared.
Definition: al_Vec.hpp:418
Vec byz(T shift) const
Returns a nearby vector along z.
Definition: al_Vec.hpp:402
Vec & set(const Vec< N, T2 > &v)
Set elements from another vector.
Definition: al_Vec.hpp:301
Vec(const Vec< N - 1, Tv > &v, const Ts &s)
Definition: al_Vec.hpp:152
Vec & set(const T2 *v, int stride)
Set elements from strided raw C-pointer.
Definition: al_Vec.hpp:335
Vec & set(const Vec< N2, T2 > &v)
Set elements from another vector.
Definition: al_Vec.hpp:308
Vec & zero()
Set all elements to zero.
Definition: al_Vec.hpp:379
Vec(const T &v1, const T &v2, const T &v3, const T &v4)
Definition: al_Vec.hpp:139
Vec byy(T shift) const
Returns a nearby vector along y.
Definition: al_Vec.hpp:399
Vec & set(const T2 *v)
Set elements from raw C-pointer.
Definition: al_Vec.hpp:328
Vec & set(const T &v)
Set all elements to the same value.
Definition: al_Vec.hpp:321
Vec & reflect(const Vec &u)
Relect vector around a unit vector.
Definition: al_Vec.hpp:501
const T & operator[](size_t i) const
Get element at index with no bounds checking.
Definition: al_Vec.hpp:199
Vec normalized(T scale=T(1)) const
Return closest vector lying on unit sphere.
Definition: al_Vec.hpp:489
Vec & set(const T &v1, const T &v2, const T &v3, const T &v4)
Set first 4 elements.
Definition: al_Vec.hpp:349
bool operator==(const T &v) const
Return true if all elements are equal to value, false otherwise.
Definition: al_Vec.hpp:210
T norm1() const
Return 1-norm of elements.
Definition: al_Vec.hpp:433
static int size()
Returns number of elements.
Definition: al_Vec.hpp:167
Vec & set(const Vec< N - 1, Tv > &v, const Ts &s)
Set elements from another vector and scalar.
Definition: al_Vec.hpp:315
void print(FILE *out=stdout) const
debug printing
Definition: al_Vec.hpp:746
T sum() const
Returns sum of elements.
Definition: al_Vec.hpp:448
static Vec & pun(T *src)
Returns C array type punned into a vector.
Definition: al_Vec.hpp:170
Vec(const T &v=T())
Definition: al_Vec.hpp:124
Vec by(T shift) const
Returns a nearby vector along some dimension.
Definition: al_Vec.hpp:389
Vec< 2, T > get(int i0, int i1) const
Get a vector comprised of indexed elements.
Definition: al_Vec.hpp:224
Vec & negate()
Negates all elements.
Definition: al_Vec.hpp:474
Vec & mag(T v)
Set magnitude (preserving direction)
Definition: al_Vec.hpp:717
T dot(const Vec< N, U > &v) const
Returns dot (inner) product between vectors.
Definition: al_Vec.hpp:406
Vec rejection(const Vec &u) const
Get rejection of vector from a unit vector.
Definition: al_Vec.hpp:498
Vec(const Vec< N2, T2 > &v)
Definition: al_Vec.hpp:145
Vec(const T2 *v, int stride=1)
Definition: al_Vec.hpp:159
bool operator!=(const Vec &v) const
Return true if objects are not element-wise equal, false otherwise.
Definition: al_Vec.hpp:218
Vec< 3, T > get(int i0, int i1, int i2) const
Get a vector comprised of indexed elements.
Definition: al_Vec.hpp:229
T * elems()
Get read-write pointer to elements.
Definition: al_Vec.hpp:188
Vec & set(const T &v1, const T &v2, const T &v3, const T &v4, const T &v5)
Set first 5 elements.
Definition: al_Vec.hpp:354
T mag() const
Returns magnitude.
Definition: al_Vec.hpp:415
Vec byx(T shift) const
Returns a nearby vector along x.
Definition: al_Vec.hpp:396
Vec< M, T > sub(int begin=0) const
Get a subvector.
Definition: al_Vec.hpp:240
bool operator==(const Vec &v) const
Return true if objects are element-wise equal, false otherwise.
Definition: al_Vec.hpp:202
Vec projection(const Vec &u) const
Get projection of vector onto a unit vector.
Definition: al_Vec.hpp:492
T product() const
Returns product of elements.
Definition: al_Vec.hpp:439
Vec & rotate(double angle, int dim1=0, int dim2=1)
Rotate vector on a global plane.
Definition: al_Vec.hpp:508
V & as()
Get reference to self as another type.
Definition: al_Vec.hpp:175
Vec(const T &v1, const T &v2, const T &v3)
Definition: al_Vec.hpp:133
Vec(const T &v1, const T &v2)
Definition: al_Vec.hpp:128
T min(const T &v1, const T &v2, const T &v3)
T max(const T &v1, const T &v2, const T &v3)
Definition: al_App.hpp:23
Vec< N, T > closestPointOnLineSegment(const Vec< N, T > &a, const Vec< N, T > &b, const Vec< N, T > &p)
Get closest point on line segment ab to point p.
Definition: al_Vec.hpp:659
Vec< N1+N2, T1 > concat(const Vec< N1, T1 > &a, const Vec< N2, T2 > &b)
Returns concatenation of two vectors.
Definition: al_Vec.hpp:559
Vec< M, T > sub(const Vec< N, T > &v, int begin=0)
Get a subvector.
Definition: al_Vec.hpp:568
Vec< 3, double > Vec3d
double 3-vector
Definition: al_Vec.hpp:60
Vec< 2, double > Vec2d
double 2-vector
Definition: al_Vec.hpp:57
void normal(Vec< 3, T > &n, const Vec< 3, T > &p1, const Vec< 3, T > &p2, const Vec< 3, T > &p3)
Definition: al_Vec.hpp:692
Vec< 4, float > Vec4f
float 4-vector
Definition: al_Vec.hpp:62
Vec< 4, double > Vec4d
double 4-vector
Definition: al_Vec.hpp:63
Vec< 4, int > Vec4i
integer 4-vector
Definition: al_Vec.hpp:64
T angle(const Vec< N, T > &a, const Vec< N, T > &b)
Returns angle, in interval [0, pi], between two vectors.
Definition: al_Vec.hpp:634
void centroid3(Vec< N, T > &c, const Vec< N, T > &p1, const Vec< N, T > &p2, const Vec< N, T > &p3)
Definition: al_Vec.hpp:651
Vec< 3, float > Vec3f
float 3-vector
Definition: al_Vec.hpp:59
Vec< 2, int > Vec2i
integer 2-vector
Definition: al_Vec.hpp:58
T dist(const Vec< N, T > &a, const Vec< N, U > &b)
Returns distance between two vectors.
Definition: al_Vec.hpp:676
Vec< 2, float > Vec2f
float 2-vector
Definition: al_Vec.hpp:54
void cross(Vec< 3, T > &r, const Vec< 3, T > &a, const Vec< 3, T > &b)
Sets r to cross product, a x b.
Definition: al_Vec.hpp:574
Vec< 3, int > Vec3i
integer 3-vector
Definition: al_Vec.hpp:61
Vec< 3, T > cross(const Vec< 3, T > &b) const
Returns cross product of this x b.
Definition: al_Vec.hpp:96