OpenVDB  9.0.1
Vec3.h
Go to the documentation of this file.
1 // Copyright Contributors to the OpenVDB Project
2 // SPDX-License-Identifier: MPL-2.0
3 
4 #ifndef OPENVDB_MATH_VEC3_HAS_BEEN_INCLUDED
5 #define OPENVDB_MATH_VEC3_HAS_BEEN_INCLUDED
6 
7 #include <openvdb/Exceptions.h>
8 #include "Math.h"
9 #include "Tuple.h"
10 #include <algorithm>
11 #include <cmath>
12 #include <type_traits>
13 
14 
15 namespace openvdb {
17 namespace OPENVDB_VERSION_NAME {
18 namespace math {
19 
20 template<typename T> class Mat3;
21 
22 template<typename T>
23 class Vec3: public Tuple<3, T>
24 {
25 public:
26  using value_type = T;
27  using ValueType = T;
28 
29  /// Trivial constructor, the vector is NOT initialized
30 #if OPENVDB_ABI_VERSION_NUMBER >= 8
31  /// @note destructor, copy constructor, assignment operator and
32  /// move constructor are left to be defined by the compiler (default)
33  Vec3() = default;
34 #else
35  Vec3() {}
36 #endif
37 
38  /// @brief Construct a vector all of whose components have the given value.
39  explicit Vec3(T val) { this->mm[0] = this->mm[1] = this->mm[2] = val; }
40 
41  /// Constructor with three arguments, e.g. Vec3d v(1,2,3);
42  Vec3(T x, T y, T z)
43  {
44  this->mm[0] = x;
45  this->mm[1] = y;
46  this->mm[2] = z;
47  }
48 
49  /// Constructor with array argument, e.g. double a[3]; Vec3d v(a);
50  template <typename Source>
51  Vec3(Source *a)
52  {
53  this->mm[0] = static_cast<T>(a[0]);
54  this->mm[1] = static_cast<T>(a[1]);
55  this->mm[2] = static_cast<T>(a[2]);
56  }
57 
58  /// @brief Construct a Vec3 from a 3-Tuple with a possibly different value type.
59  /// @details Type conversion warnings are suppressed.
60  template<typename Source>
61  explicit Vec3(const Tuple<3, Source> &v)
62  {
63  this->mm[0] = static_cast<T>(v[0]);
64  this->mm[1] = static_cast<T>(v[1]);
65  this->mm[2] = static_cast<T>(v[2]);
66  }
67 
68  /// @brief Construct a vector all of whose components have the given value,
69  /// which may be of an arithmetic type different from this vector's value type.
70  /// @details Type conversion warnings are suppressed.
71  template<typename Other>
72  explicit Vec3(Other val,
73  typename std::enable_if<std::is_arithmetic<Other>::value, Conversion>::type = Conversion{})
74  {
75  this->mm[0] = this->mm[1] = this->mm[2] = static_cast<T>(val);
76  }
77 
78  /// @brief Construct a Vec3 from another Vec3 with a possibly different value type.
79  /// @details Type conversion warnings are suppressed.
80  template<typename Other>
81  Vec3(const Vec3<Other>& v)
82  {
83  this->mm[0] = static_cast<T>(v[0]);
84  this->mm[1] = static_cast<T>(v[1]);
85  this->mm[2] = static_cast<T>(v[2]);
86  }
87 
88  /// Reference to the component, e.g. v.x() = 4.5f;
89  T& x() { return this->mm[0]; }
90  T& y() { return this->mm[1]; }
91  T& z() { return this->mm[2]; }
92 
93  /// Get the component, e.g. float f = v.y();
94  T x() const { return this->mm[0]; }
95  T y() const { return this->mm[1]; }
96  T z() const { return this->mm[2]; }
97 
98  T* asPointer() { return this->mm; }
99  const T* asPointer() const { return this->mm; }
100 
101  /// Alternative indexed reference to the elements
102  T& operator()(int i) { return this->mm[i]; }
103 
104  /// Alternative indexed constant reference to the elements,
105  T operator()(int i) const { return this->mm[i]; }
106 
107  /// "this" vector gets initialized to [x, y, z],
108  /// calling v.init(); has same effect as calling v = Vec3::zero();
109  const Vec3<T>& init(T x=0, T y=0, T z=0)
110  {
111  this->mm[0] = x; this->mm[1] = y; this->mm[2] = z;
112  return *this;
113  }
114 
115 
116  /// Set "this" vector to zero
117  const Vec3<T>& setZero()
118  {
119  this->mm[0] = 0; this->mm[1] = 0; this->mm[2] = 0;
120  return *this;
121  }
122 
123  /// @brief Assignment operator
124  /// @details Type conversion warnings are not suppressed.
125  template<typename Source>
126  const Vec3<T>& operator=(const Vec3<Source> &v)
127  {
128  // note: don't static_cast because that suppresses warnings
129  this->mm[0] = v[0];
130  this->mm[1] = v[1];
131  this->mm[2] = v[2];
132 
133  return *this;
134  }
135 
136  /// Test if "this" vector is equivalent to vector v with tolerance of eps
137  bool eq(const Vec3<T> &v, T eps = static_cast<T>(1.0e-7)) const
138  {
139  return isRelOrApproxEqual(this->mm[0], v.mm[0], eps, eps) &&
140  isRelOrApproxEqual(this->mm[1], v.mm[1], eps, eps) &&
141  isRelOrApproxEqual(this->mm[2], v.mm[2], eps, eps);
142  }
143 
144 
145  /// Negation operator, for e.g. v1 = -v2;
146  Vec3<T> operator-() const { return Vec3<T>(-this->mm[0], -this->mm[1], -this->mm[2]); }
147 
148  /// this = v1 + v2
149  /// "this", v1 and v2 need not be distinct objects, e.g. v.add(v1,v);
150  template <typename T0, typename T1>
151  const Vec3<T>& add(const Vec3<T0> &v1, const Vec3<T1> &v2)
152  {
153  this->mm[0] = v1[0] + v2[0];
154  this->mm[1] = v1[1] + v2[1];
155  this->mm[2] = v1[2] + v2[2];
156 
157  return *this;
158  }
159 
160  /// this = v1 - v2
161  /// "this", v1 and v2 need not be distinct objects, e.g. v.sub(v1,v);
162  template <typename T0, typename T1>
163  const Vec3<T>& sub(const Vec3<T0> &v1, const Vec3<T1> &v2)
164  {
165  this->mm[0] = v1[0] - v2[0];
166  this->mm[1] = v1[1] - v2[1];
167  this->mm[2] = v1[2] - v2[2];
168 
169  return *this;
170  }
171 
172  /// this = scalar*v, v need not be a distinct object from "this",
173  /// e.g. v.scale(1.5,v1);
174  template <typename T0, typename T1>
175  const Vec3<T>& scale(T0 scale, const Vec3<T1> &v)
176  {
177  this->mm[0] = scale * v[0];
178  this->mm[1] = scale * v[1];
179  this->mm[2] = scale * v[2];
180 
181  return *this;
182  }
183 
184  template <typename T0, typename T1>
185  const Vec3<T> &div(T0 scale, const Vec3<T1> &v)
186  {
187  this->mm[0] = v[0] / scale;
188  this->mm[1] = v[1] / scale;
189  this->mm[2] = v[2] / scale;
190 
191  return *this;
192  }
193 
194  /// Dot product
195  T dot(const Vec3<T> &v) const
196  {
197  return
198  this->mm[0]*v.mm[0] +
199  this->mm[1]*v.mm[1] +
200  this->mm[2]*v.mm[2];
201  }
202 
203  /// Length of the vector
204  T length() const
205  {
206  return static_cast<T>(sqrt(double(
207  this->mm[0]*this->mm[0] +
208  this->mm[1]*this->mm[1] +
209  this->mm[2]*this->mm[2])));
210  }
211 
212 
213  /// Squared length of the vector, much faster than length() as it
214  /// does not involve square root
215  T lengthSqr() const
216  {
217  return
218  this->mm[0]*this->mm[0] +
219  this->mm[1]*this->mm[1] +
220  this->mm[2]*this->mm[2];
221  }
222 
223  /// Return the cross product of "this" vector and v;
224  Vec3<T> cross(const Vec3<T> &v) const
225  {
226  return Vec3<T>(this->mm[1]*v.mm[2] - this->mm[2]*v.mm[1],
227  this->mm[2]*v.mm[0] - this->mm[0]*v.mm[2],
228  this->mm[0]*v.mm[1] - this->mm[1]*v.mm[0]);
229  }
230 
231 
232  /// this = v1 cross v2, v1 and v2 must be distinct objects than "this"
233  const Vec3<T>& cross(const Vec3<T> &v1, const Vec3<T> &v2)
234  {
235  // assert(this!=&v1);
236  // assert(this!=&v2);
237  this->mm[0] = v1.mm[1]*v2.mm[2] - v1.mm[2]*v2.mm[1];
238  this->mm[1] = v1.mm[2]*v2.mm[0] - v1.mm[0]*v2.mm[2];
239  this->mm[2] = v1.mm[0]*v2.mm[1] - v1.mm[1]*v2.mm[0];
240  return *this;
241  }
242 
243  /// Multiply each element of this vector by @a scalar.
244  template <typename S>
245  const Vec3<T> &operator*=(S scalar)
246  {
248  const auto value0 = this->mm[0] * scalar;
249  const auto value1 = this->mm[1] * scalar;
250  const auto value2 = this->mm[2] * scalar;
252  this->mm[0] = static_cast<T>(value0);
253  this->mm[1] = static_cast<T>(value1);
254  this->mm[2] = static_cast<T>(value2);
255  return *this;
256  }
257 
258  /// Multiply each element of this vector by the corresponding element of the given vector.
259  template <typename S>
260  const Vec3<T> &operator*=(const Vec3<S> &v1)
261  {
262  this->mm[0] *= v1[0];
263  this->mm[1] *= v1[1];
264  this->mm[2] *= v1[2];
265  return *this;
266  }
267 
268  /// Divide each element of this vector by @a scalar.
269  template <typename S>
270  const Vec3<T> &operator/=(S scalar)
271  {
272  this->mm[0] /= scalar;
273  this->mm[1] /= scalar;
274  this->mm[2] /= scalar;
275  return *this;
276  }
277 
278  /// Divide each element of this vector by the corresponding element of the given vector.
279  template <typename S>
280  const Vec3<T> &operator/=(const Vec3<S> &v1)
281  {
282  this->mm[0] /= v1[0];
283  this->mm[1] /= v1[1];
284  this->mm[2] /= v1[2];
285  return *this;
286  }
287 
288  /// Add @a scalar to each element of this vector.
289  template <typename S>
290  const Vec3<T> &operator+=(S scalar)
291  {
293  const auto value0 = this->mm[0] + scalar;
294  const auto value1 = this->mm[1] + scalar;
295  const auto value2 = this->mm[2] + scalar;
297  this->mm[0] = static_cast<T>(value0);
298  this->mm[1] = static_cast<T>(value1);
299  this->mm[2] = static_cast<T>(value2);
300  return *this;
301  }
302 
303  /// Add each element of the given vector to the corresponding element of this vector.
304  template <typename S>
305  const Vec3<T> &operator+=(const Vec3<S> &v1)
306  {
307  this->mm[0] += v1[0];
308  this->mm[1] += v1[1];
309  this->mm[2] += v1[2];
310  return *this;
311  }
312 
313  /// Subtract @a scalar from each element of this vector.
314  template <typename S>
315  const Vec3<T> &operator-=(S scalar)
316  {
317  this->mm[0] -= scalar;
318  this->mm[1] -= scalar;
319  this->mm[2] -= scalar;
320  return *this;
321  }
322 
323  /// Subtract each element of the given vector from the corresponding element of this vector.
324  template <typename S>
325  const Vec3<T> &operator-=(const Vec3<S> &v1)
326  {
327  this->mm[0] -= v1[0];
328  this->mm[1] -= v1[1];
329  this->mm[2] -= v1[2];
330  return *this;
331  }
332 
333  /// Return a reference to itself after the exponent has been
334  /// applied to all the vector components.
335  inline const Vec3<T>& exp()
336  {
337  this->mm[0] = std::exp(this->mm[0]);
338  this->mm[1] = std::exp(this->mm[1]);
339  this->mm[2] = std::exp(this->mm[2]);
340  return *this;
341  }
342 
343  /// Return a reference to itself after log has been
344  /// applied to all the vector components.
345  inline const Vec3<T>& log()
346  {
347  this->mm[0] = std::log(this->mm[0]);
348  this->mm[1] = std::log(this->mm[1]);
349  this->mm[2] = std::log(this->mm[2]);
350  return *this;
351  }
352 
353  /// Return the sum of all the vector components.
354  inline T sum() const
355  {
356  return this->mm[0] + this->mm[1] + this->mm[2];
357  }
358 
359  /// Return the product of all the vector components.
360  inline T product() const
361  {
362  return this->mm[0] * this->mm[1] * this->mm[2];
363  }
364 
365  /// this = normalized this
366  bool normalize(T eps = T(1.0e-7))
367  {
368  T d = length();
369  if (isApproxEqual(d, T(0), eps)) {
370  return false;
371  }
372  *this *= (T(1) / d);
373  return true;
374  }
375 
376 
377  /// return normalized this, throws if null vector
378  Vec3<T> unit(T eps=0) const
379  {
380  T d;
381  return unit(eps, d);
382  }
383 
384  /// return normalized this and length, throws if null vector
385  Vec3<T> unit(T eps, T& len) const
386  {
387  len = length();
388  if (isApproxEqual(len, T(0), eps)) {
389  OPENVDB_THROW(ArithmeticError, "Normalizing null 3-vector");
390  }
391  return *this / len;
392  }
393 
394  /// return normalized this, or (1, 0, 0) if this is null vector
396  {
397  T l2 = lengthSqr();
398  return l2 ? *this / static_cast<T>(sqrt(l2)) : Vec3<T>(1, 0 ,0);
399  }
400 
401  // Number of cols, rows, elements
402  static unsigned numRows() { return 1; }
403  static unsigned numColumns() { return 3; }
404  static unsigned numElements() { return 3; }
405 
406  /// Returns the scalar component of v in the direction of onto, onto need
407  /// not be unit. e.g double c = Vec3d::component(v1,v2);
408  T component(const Vec3<T> &onto, T eps = static_cast<T>(1.0e-7)) const
409  {
410  T l = onto.length();
411  if (isApproxEqual(l, T(0), eps)) return 0;
412 
413  return dot(onto)*(T(1)/l);
414  }
415 
416  /// Return the projection of v onto the vector, onto need not be unit
417  /// e.g. Vec3d a = vprojection(n);
418  Vec3<T> projection(const Vec3<T> &onto, T eps = static_cast<T>(1.0e-7)) const
419  {
420  T l = onto.lengthSqr();
421  if (isApproxEqual(l, T(0), eps)) return Vec3::zero();
422 
423  return onto*(dot(onto)*(T(1)/l));
424  }
425 
426  /// Return an arbitrary unit vector perpendicular to v
427  /// Vector this must be a unit vector
428  /// e.g. v = v.normalize(); Vec3d n = v.getArbPerpendicular();
430  {
431  Vec3<T> u;
432  T l;
433 
434  if ( fabs(this->mm[0]) >= fabs(this->mm[1]) ) {
435  // v.x or v.z is the largest magnitude component, swap them
436  l = this->mm[0]*this->mm[0] + this->mm[2]*this->mm[2];
437  l = static_cast<T>(T(1)/sqrt(double(l)));
438  u.mm[0] = -this->mm[2]*l;
439  u.mm[1] = T(0);
440  u.mm[2] = +this->mm[0]*l;
441  } else {
442  // W.y or W.z is the largest magnitude component, swap them
443  l = this->mm[1]*this->mm[1] + this->mm[2]*this->mm[2];
444  l = static_cast<T>(T(1)/sqrt(double(l)));
445  u.mm[0] = T(0);
446  u.mm[1] = +this->mm[2]*l;
447  u.mm[2] = -this->mm[1]*l;
448  }
449 
450  return u;
451  }
452 
453  /// Return a vector with the components of this in ascending order
454  Vec3<T> sorted() const
455  {
456  Vec3<T> r(*this);
457  if( r.mm[0] > r.mm[1] ) std::swap(r.mm[0], r.mm[1]);
458  if( r.mm[1] > r.mm[2] ) std::swap(r.mm[1], r.mm[2]);
459  if( r.mm[0] > r.mm[1] ) std::swap(r.mm[0], r.mm[1]);
460  return r;
461  }
462 
463  /// Return the vector (z, y, x)
465  {
466  return Vec3<T>(this->mm[2], this->mm[1], this->mm[0]);
467  }
468 
469  /// Predefined constants, e.g. Vec3d v = Vec3d::xNegAxis();
470  static Vec3<T> zero() { return Vec3<T>(0, 0, 0); }
471  static Vec3<T> ones() { return Vec3<T>(1, 1, 1); }
472 };
473 
474 
475 /// Equality operator, does exact floating point comparisons
476 template <typename T0, typename T1>
477 inline bool operator==(const Vec3<T0> &v0, const Vec3<T1> &v1)
478 {
479  return isExactlyEqual(v0[0], v1[0]) && isExactlyEqual(v0[1], v1[1])
480  && isExactlyEqual(v0[2], v1[2]);
481 }
482 
483 /// Inequality operator, does exact floating point comparisons
484 template <typename T0, typename T1>
485 inline bool operator!=(const Vec3<T0> &v0, const Vec3<T1> &v1) { return !(v0==v1); }
486 
487 /// Multiply each element of the given vector by @a scalar and return the result.
488 template <typename S, typename T>
489 inline Vec3<typename promote<S, T>::type> operator*(S scalar, const Vec3<T> &v) { return v*scalar; }
490 
491 /// Multiply each element of the given vector by @a scalar and return the result.
492 template <typename S, typename T>
494 {
496  result *= scalar;
497  return result;
498 }
499 
500 /// Multiply corresponding elements of @a v0 and @a v1 and return the result.
501 template <typename T0, typename T1>
503 {
504  Vec3<typename promote<T0, T1>::type> result(v0[0] * v1[0], v0[1] * v1[1], v0[2] * v1[2]);
505  return result;
506 }
507 
508 
509 /// Divide @a scalar by each element of the given vector and return the result.
510 template <typename S, typename T>
512 {
513  return Vec3<typename promote<S, T>::type>(scalar/v[0], scalar/v[1], scalar/v[2]);
514 }
515 
516 /// Divide each element of the given vector by @a scalar and return the result.
517 template <typename S, typename T>
519 {
521  result /= scalar;
522  return result;
523 }
524 
525 /// Divide corresponding elements of @a v0 and @a v1 and return the result.
526 template <typename T0, typename T1>
528 {
529  Vec3<typename promote<T0, T1>::type> result(v0[0] / v1[0], v0[1] / v1[1], v0[2] / v1[2]);
530  return result;
531 }
532 
533 /// Add corresponding elements of @a v0 and @a v1 and return the result.
534 template <typename T0, typename T1>
536 {
538  result += v1;
539  return result;
540 }
541 
542 /// Add @a scalar to each element of the given vector and return the result.
543 template <typename S, typename T>
545 {
547  result += scalar;
548  return result;
549 }
550 
551 /// Subtract corresponding elements of @a v0 and @a v1 and return the result.
552 template <typename T0, typename T1>
554 {
556  result -= v1;
557  return result;
558 }
559 
560 /// Subtract @a scalar from each element of the given vector and return the result.
561 template <typename S, typename T>
563 {
565  result -= scalar;
566  return result;
567 }
568 
569 /// Angle between two vectors, the result is between [0, pi],
570 /// e.g. double a = Vec3d::angle(v1,v2);
571 template <typename T>
572 inline T angle(const Vec3<T> &v1, const Vec3<T> &v2)
573 {
574  Vec3<T> c = v1.cross(v2);
575  return static_cast<T>(atan2(c.length(), v1.dot(v2)));
576 }
577 
578 template <typename T>
579 inline bool
580 isApproxEqual(const Vec3<T>& a, const Vec3<T>& b)
581 {
582  return a.eq(b);
583 }
584 template <typename T>
585 inline bool
586 isApproxEqual(const Vec3<T>& a, const Vec3<T>& b, const Vec3<T>& eps)
587 {
588  return isApproxEqual(a.x(), b.x(), eps.x()) &&
589  isApproxEqual(a.y(), b.y(), eps.y()) &&
590  isApproxEqual(a.z(), b.z(), eps.z());
591 }
592 
593 template<typename T>
594 inline Vec3<T>
595 Abs(const Vec3<T>& v)
596 {
597  return Vec3<T>(Abs(v[0]), Abs(v[1]), Abs(v[2]));
598 }
599 
600 /// Orthonormalize vectors v1, v2 and v3 and store back the resulting
601 /// basis e.g. Vec3d::orthonormalize(v1,v2,v3);
602 template <typename T>
603 inline void orthonormalize(Vec3<T> &v1, Vec3<T> &v2, Vec3<T> &v3)
604 {
605  // If the input vectors are v0, v1, and v2, then the Gram-Schmidt
606  // orthonormalization produces vectors u0, u1, and u2 as follows,
607  //
608  // u0 = v0/|v0|
609  // u1 = (v1-(u0*v1)u0)/|v1-(u0*v1)u0|
610  // u2 = (v2-(u0*v2)u0-(u1*v2)u1)/|v2-(u0*v2)u0-(u1*v2)u1|
611  //
612  // where |A| indicates length of vector A and A*B indicates dot
613  // product of vectors A and B.
614 
615  // compute u0
616  v1.normalize();
617 
618  // compute u1
619  T d0 = v1.dot(v2);
620  v2 -= v1*d0;
621  v2.normalize();
622 
623  // compute u2
624  T d1 = v2.dot(v3);
625  d0 = v1.dot(v3);
626  v3 -= v1*d0 + v2*d1;
627  v3.normalize();
628 }
629 
630 /// @remark We are switching to a more explicit name because the semantics
631 /// are different from std::min/max. In that case, the function returns a
632 /// reference to one of the objects based on a comparator. Here, we must
633 /// fabricate a new object which might not match either of the inputs.
634 
635 /// Return component-wise minimum of the two vectors.
636 template <typename T>
637 inline Vec3<T> minComponent(const Vec3<T> &v1, const Vec3<T> &v2)
638 {
639  return Vec3<T>(
640  std::min(v1.x(), v2.x()),
641  std::min(v1.y(), v2.y()),
642  std::min(v1.z(), v2.z()));
643 }
644 
645 /// Return component-wise maximum of the two vectors.
646 template <typename T>
647 inline Vec3<T> maxComponent(const Vec3<T> &v1, const Vec3<T> &v2)
648 {
649  return Vec3<T>(
650  std::max(v1.x(), v2.x()),
651  std::max(v1.y(), v2.y()),
652  std::max(v1.z(), v2.z()));
653 }
654 
655 /// @brief Return a vector with the exponent applied to each of
656 /// the components of the input vector.
657 template <typename T>
658 inline Vec3<T> Exp(Vec3<T> v) { return v.exp(); }
659 
660 /// @brief Return a vector with log applied to each of
661 /// the components of the input vector.
662 template <typename T>
663 inline Vec3<T> Log(Vec3<T> v) { return v.log(); }
664 
669 
670 #if OPENVDB_ABI_VERSION_NUMBER >= 8
675 #endif
676 
677 } // namespace math
678 } // namespace OPENVDB_VERSION_NAME
679 } // namespace openvdb
680 
681 #endif // OPENVDB_MATH_VEC3_HAS_BEEN_INCLUDED
Vec3< T > sorted() const
Return a vector with the components of this in ascending order.
Definition: Vec3.h:454
Vec3< T > Log(Vec3< T > v)
Return a vector with log applied to each of the components of the input vector.
Definition: Vec3.h:663
#define OPENVDB_NO_TYPE_CONVERSION_WARNING_BEGIN
Bracket code with OPENVDB_NO_TYPE_CONVERSION_WARNING_BEGIN/_END, to inhibit warnings about type conve...
Definition: Platform.h:206
Definition: Exceptions.h:56
const Vec3< T > & operator+=(const Vec3< S > &v1)
Add each element of the given vector to the corresponding element of this vector. ...
Definition: Vec3.h:305
Vec3< T > getArbPerpendicular() const
Definition: Vec3.h:429
bool isExactlyEqual(const T0 &a, const T1 &b)
Return true if a is exactly equal to b.
Definition: Math.h:444
Definition: Tuple.h:29
T z() const
Definition: Vec3.h:96
Vec3< typename promote< S, T >::type > operator-(const Vec3< T > &v, S scalar)
Subtract scalar from each element of the given vector and return the result.
Definition: Vec3.h:562
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:74
const Vec3< T > & operator+=(S scalar)
Add scalar to each element of this vector.
Definition: Vec3.h:290
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
const Vec3< T > & setZero()
Set "this" vector to zero.
Definition: Vec3.h:117
const Vec3< T > & operator*=(const Vec3< S > &v1)
Multiply each element of this vector by the corresponding element of the given vector.
Definition: Vec3.h:260
bool isApproxEqual(const Vec3< T > &a, const Vec3< T > &b, const Vec3< T > &eps)
Definition: Vec3.h:586
Vec3< T > cross(const Vec3< T > &v) const
Return the cross product of "this" vector and v;.
Definition: Vec3.h:224
T & z()
Definition: Vec3.h:91
T component(const Vec3< T > &onto, T eps=static_cast< T >(1.0e-7)) const
Definition: Vec3.h:408
T & y()
Definition: Vec3.h:90
bool eq(const Vec3< T > &v, T eps=static_cast< T >(1.0e-7)) const
Test if "this" vector is equivalent to vector v with tolerance of eps.
Definition: Vec3.h:137
T angle(const Vec3< T > &v1, const Vec3< T > &v2)
Definition: Vec3.h:572
Vec3< T > unit(T eps=0) const
return normalized this, throws if null vector
Definition: Vec3.h:378
Real value_type
Definition: Vec3.h:26
static unsigned numColumns()
Definition: Vec3.h:403
const Vec3< T > & scale(T0 scale, const Vec3< T1 > &v)
Definition: Vec3.h:175
Real ValueType
Definition: Vec3.h:27
#define OPENVDB_NO_TYPE_CONVERSION_WARNING_END
Definition: Platform.h:207
const std::enable_if<!VecTraits< T >::IsVec, T >::type & max(const T &a, const T &b)
Definition: Composite.h:107
Vec3< T > maxComponent(const Vec3< T > &v1, const Vec3< T > &v2)
Return component-wise maximum of the two vectors.
Definition: Vec3.h:647
T length() const
Length of the vector.
Definition: Vec3.h:204
static unsigned numElements()
Definition: Vec3.h:404
MatType unit(const MatType &mat, typename MatType::value_type eps=1.0e-8)
Return a copy of the given matrix with its upper 3×3 rows normalized.
Definition: Mat.h:670
Vec3< typename promote< S, T >::type > operator+(const Vec3< T > &v, S scalar)
Add scalar to each element of the given vector and return the result.
Definition: Vec3.h:544
Vec3< typename promote< T0, T1 >::type > operator/(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Divide corresponding elements of v0 and v1 and return the result.
Definition: Vec3.h:527
Vec3< T > projection(const Vec3< T > &onto, T eps=static_cast< T >(1.0e-7)) const
Definition: Vec3.h:418
const Vec3< T > & log()
Definition: Vec3.h:345
Vec3< T > operator-() const
Negation operator, for e.g. v1 = -v2;.
Definition: Vec3.h:146
Vec3< T > unit(T eps, T &len) const
return normalized this and length, throws if null vector
Definition: Vec3.h:385
T y() const
Definition: Vec3.h:95
const Vec3< T > & init(T x=0, T y=0, T z=0)
Definition: Vec3.h:109
Vec3(const Vec3< Other > &v)
Construct a Vec3 from another Vec3 with a possibly different value type.
Definition: Vec3.h:81
const Vec3< T > & exp()
Definition: Vec3.h:335
static unsigned numRows()
Definition: Vec3.h:402
Vec3< T > unitSafe() const
return normalized this, or (1, 0, 0) if this is null vector
Definition: Vec3.h:395
Vec3< T > reversed() const
Return the vector (z, y, x)
Definition: Vec3.h:464
const Vec3< T > & operator=(const Vec3< Source > &v)
Assignment operator.
Definition: Vec3.h:126
const Vec3< T > & cross(const Vec3< T > &v1, const Vec3< T > &v2)
this = v1 cross v2, v1 and v2 must be distinct objects than "this"
Definition: Vec3.h:233
T dot(const Vec3< T > &v) const
Dot product.
Definition: Vec3.h:195
Dummy class for tag dispatch of conversion constructors.
Definition: Tuple.h:23
Definition: Exceptions.h:13
Vec3< T > minComponent(const Vec3< T > &v1, const Vec3< T > &v2)
Return component-wise minimum of the two vectors.
Definition: Vec3.h:637
ValueT value
Definition: GridBuilder.h:1287
Vec3(T x, T y, T z)
Constructor with three arguments, e.g. Vec3d v(1,2,3);.
Definition: Vec3.h:42
T * asPointer()
Definition: Vec3.h:98
static Vec3< T > zero()
Predefined constants, e.g. Vec3d v = Vec3d::xNegAxis();.
Definition: Vec3.h:470
const Vec3< T > & operator-=(const Vec3< S > &v1)
Subtract each element of the given vector from the corresponding element of this vector.
Definition: Vec3.h:325
const Vec3< T > & add(const Vec3< T0 > &v1, const Vec3< T1 > &v2)
Definition: Vec3.h:151
MatType scale(const Vec3< typename MatType::value_type > &s)
Return a matrix that scales by s.
Definition: Mat.h:637
T sum() const
Return the sum of all the vector components.
Definition: Vec3.h:354
Definition: Mat.h:187
T & operator()(int i)
Alternative indexed reference to the elements.
Definition: Vec3.h:102
Vec3(Other val, typename std::enable_if< std::is_arithmetic< Other >::value, Conversion >::type=Conversion{})
Construct a vector all of whose components have the given value, which may be of an arithmetic type d...
Definition: Vec3.h:72
const Vec3< T > & operator/=(const Vec3< S > &v1)
Divide each element of this vector by the corresponding element of the given vector.
Definition: Vec3.h:280
const Vec3< T > & div(T0 scale, const Vec3< T1 > &v)
Definition: Vec3.h:185
const Vec3< T > & operator/=(S scalar)
Divide each element of this vector by scalar.
Definition: Vec3.h:270
T & x()
Reference to the component, e.g. v.x() = 4.5f;.
Definition: Vec3.h:89
bool isRelOrApproxEqual(const Type &a, const Type &b, const Type &absTol, const Type &relTol)
Definition: Math.h:454
bool normalize(T eps=T(1.0e-7))
this = normalized this
Definition: Vec3.h:366
T lengthSqr() const
Definition: Vec3.h:215
const Vec3< T > & sub(const Vec3< T0 > &v1, const Vec3< T1 > &v2)
Definition: Vec3.h:163
T x() const
Get the component, e.g. float f = v.y();.
Definition: Vec3.h:94
const Vec3< T > & operator-=(S scalar)
Subtract scalar from each element of this vector.
Definition: Vec3.h:315
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h.in:116
Vec3(const Tuple< 3, Source > &v)
Construct a Vec3 from a 3-Tuple with a possibly different value type.
Definition: Vec3.h:61
const std::enable_if<!VecTraits< T >::IsVec, T >::type & min(const T &a, const T &b)
Definition: Composite.h:103
bool operator==(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Equality operator, does exact floating point comparisons.
Definition: Vec3.h:477
bool operator!=(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Inequality operator, does exact floating point comparisons.
Definition: Vec3.h:485
static Vec3< T > ones()
Definition: Vec3.h:471
T product() const
Return the product of all the vector components.
Definition: Vec3.h:360
Vec3< T > Abs(const Vec3< T > &v)
Definition: Vec3.h:595
void orthonormalize(Vec3< T > &v1, Vec3< T > &v2, Vec3< T > &v3)
Definition: Vec3.h:603
Vec3(T val)
Construct a vector all of whose components have the given value.
Definition: Vec3.h:39
Vec3(Source *a)
Constructor with array argument, e.g. double a[3]; Vec3d v(a);.
Definition: Vec3.h:51
Vec3< T > Exp(Vec3< T > v)
Return a vector with the exponent applied to each of the components of the input vector.
Definition: Vec3.h:658
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h.in:202
const T * asPointer() const
Definition: Vec3.h:99
T operator()(int i) const
Alternative indexed constant reference to the elements,.
Definition: Vec3.h:105
#define OPENVDB_IS_POD(Type)
Definition: Math.h:55
Vec3< typename promote< T0, T1 >::type > operator*(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Multiply corresponding elements of v0 and v1 and return the result.
Definition: Vec3.h:502
const Vec3< T > & operator*=(S scalar)
Multiply each element of this vector by scalar.
Definition: Vec3.h:245