HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Vec3.h
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2012-2017 DreamWorks Animation LLC
4 //
5 // All rights reserved. This software is distributed under the
6 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
7 //
8 // Redistributions of source code must retain the above copyright
9 // and license notice and the following restrictions and disclaimer.
10 //
11 // * Neither the name of DreamWorks Animation nor the names of
12 // its contributors may be used to endorse or promote products derived
13 // from this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY INDIRECT, INCIDENTAL,
20 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 // IN NO EVENT SHALL THE COPYRIGHT HOLDERS' AND CONTRIBUTORS' AGGREGATE
27 // LIABILITY FOR ALL CLAIMS REGARDLESS OF THEIR BASIS EXCEED US$250.00.
28 //
29 ///////////////////////////////////////////////////////////////////////////
30 
31 #ifndef OPENVDB_MATH_VEC3_HAS_BEEN_INCLUDED
32 #define OPENVDB_MATH_VEC3_HAS_BEEN_INCLUDED
33 
34 #include <openvdb/Exceptions.h>
35 #include "Math.h"
36 #include "Tuple.h"
37 #include <cmath>
38 #include <type_traits>
39 
40 
41 namespace openvdb {
43 namespace OPENVDB_VERSION_NAME {
44 namespace math {
45 
46 template<typename T> class Mat3;
47 
48 template<typename T>
49 class Vec3: public Tuple<3, T>
50 {
51 public:
52  typedef T value_type;
53  typedef T ValueType;
54 
55  /// Trivial constructor, the vector is NOT initialized
56  Vec3() {}
57 
58  /// @brief Construct a vector all of whose components have the given value.
59  explicit Vec3(T val) { this->mm[0] = this->mm[1] = this->mm[2] = val; }
60 
61  /// Constructor with three arguments, e.g. Vec3d v(1,2,3);
62  Vec3(T x, T y, T z)
63  {
64  this->mm[0] = x;
65  this->mm[1] = y;
66  this->mm[2] = z;
67  }
68 
69  /// Constructor with array argument, e.g. double a[3]; Vec3d v(a);
70  template <typename Source>
71  Vec3(Source *a)
72  {
73  this->mm[0] = a[0];
74  this->mm[1] = a[1];
75  this->mm[2] = a[2];
76  }
77 
78  /// @brief Construct a Vec3 from a 3-Tuple with a possibly different value type.
79  /// @details Type conversion warnings are suppressed.
80  template<typename Source>
81  explicit Vec3(const Tuple<3, Source> &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  /// @brief Construct a vector all of whose components have the given value,
89  /// which may be of an arithmetic type different from this vector's value type.
90  /// @details Type conversion warnings are suppressed.
91  template<typename Other>
92  explicit Vec3(Other val,
93  typename std::enable_if<std::is_arithmetic<Other>::value, Conversion>::type = Conversion{})
94  {
95  this->mm[0] = this->mm[1] = this->mm[2] = static_cast<T>(val);
96  }
97 
98  /// @brief Construct a Vec3 from another Vec3 with a possibly different value type.
99  /// @details Type conversion warnings are suppressed.
100  template<typename Other>
102  {
103  this->mm[0] = static_cast<T>(v[0]);
104  this->mm[1] = static_cast<T>(v[1]);
105  this->mm[2] = static_cast<T>(v[2]);
106  }
107 
108  /// Reference to the component, e.g. v.x() = 4.5f;
109  T& x() { return this->mm[0]; }
110  T& y() { return this->mm[1]; }
111  T& z() { return this->mm[2]; }
112 
113  /// Get the component, e.g. float f = v.y();
114  T x() const { return this->mm[0]; }
115  T y() const { return this->mm[1]; }
116  T z() const { return this->mm[2]; }
117 
118  T* asPointer() { return this->mm; }
119  const T* asPointer() const { return this->mm; }
120 
121  /// Alternative indexed reference to the elements
122  T& operator()(int i) { return this->mm[i]; }
123 
124  /// Alternative indexed constant reference to the elements,
125  T operator()(int i) const { return this->mm[i]; }
126 
127  /// "this" vector gets initialized to [x, y, z],
128  /// calling v.init(); has same effect as calling v = Vec3::zero();
129  const Vec3<T>& init(T x=0, T y=0, T z=0)
130  {
131  this->mm[0] = x; this->mm[1] = y; this->mm[2] = z;
132  return *this;
133  }
134 
135 
136  /// Set "this" vector to zero
137  const Vec3<T>& setZero()
138  {
139  this->mm[0] = 0; this->mm[1] = 0; this->mm[2] = 0;
140  return *this;
141  }
142 
143  /// @brief Assignment operator
144  /// @details Type conversion warnings are not suppressed.
145  template<typename Source>
147  {
148  // note: don't static_cast because that suppresses warnings
149  this->mm[0] = v[0];
150  this->mm[1] = v[1];
151  this->mm[2] = v[2];
152 
153  return *this;
154  }
155 
156  /// Test if "this" vector is equivalent to vector v with tolerance of eps
157  bool eq(const Vec3<T> &v, T eps = static_cast<T>(1.0e-7)) const
158  {
159  return isRelOrApproxEqual(this->mm[0], v.mm[0], eps, eps) &&
160  isRelOrApproxEqual(this->mm[1], v.mm[1], eps, eps) &&
161  isRelOrApproxEqual(this->mm[2], v.mm[2], eps, eps);
162  }
163 
164 
165  /// Negation operator, for e.g. v1 = -v2;
166  Vec3<T> operator-() const { return Vec3<T>(-this->mm[0], -this->mm[1], -this->mm[2]); }
167 
168  /// this = v1 + v2
169  /// "this", v1 and v2 need not be distinct objects, e.g. v.add(v1,v);
170  template <typename T0, typename T1>
171  const Vec3<T>& add(const Vec3<T0> &v1, const Vec3<T1> &v2)
172  {
173  this->mm[0] = v1[0] + v2[0];
174  this->mm[1] = v1[1] + v2[1];
175  this->mm[2] = v1[2] + v2[2];
176 
177  return *this;
178  }
179 
180  /// this = v1 - v2
181  /// "this", v1 and v2 need not be distinct objects, e.g. v.sub(v1,v);
182  template <typename T0, typename T1>
183  const Vec3<T>& sub(const Vec3<T0> &v1, const Vec3<T1> &v2)
184  {
185  this->mm[0] = v1[0] - v2[0];
186  this->mm[1] = v1[1] - v2[1];
187  this->mm[2] = v1[2] - v2[2];
188 
189  return *this;
190  }
191 
192  /// this = scalar*v, v need not be a distinct object from "this",
193  /// e.g. v.scale(1.5,v1);
194  template <typename T0, typename T1>
195  const Vec3<T>& scale(T0 scale, const Vec3<T1> &v)
196  {
197  this->mm[0] = scale * v[0];
198  this->mm[1] = scale * v[1];
199  this->mm[2] = scale * v[2];
200 
201  return *this;
202  }
203 
204  template <typename T0, typename T1>
205  const Vec3<T> &div(T0 scale, const Vec3<T1> &v)
206  {
207  this->mm[0] = v[0] / scale;
208  this->mm[1] = v[1] / scale;
209  this->mm[2] = v[2] / scale;
210 
211  return *this;
212  }
213 
214  /// Dot product
215  T dot(const Vec3<T> &v) const
216  {
217  return
218  this->mm[0]*v.mm[0] +
219  this->mm[1]*v.mm[1] +
220  this->mm[2]*v.mm[2];
221  }
222 
223  /// Length of the vector
224  T length() const
225  {
226  return static_cast<T>(sqrt(double(
227  this->mm[0]*this->mm[0] +
228  this->mm[1]*this->mm[1] +
229  this->mm[2]*this->mm[2])));
230  }
231 
232 
233  /// Squared length of the vector, much faster than length() as it
234  /// does not involve square root
235  T lengthSqr() const
236  {
237  return
238  this->mm[0]*this->mm[0] +
239  this->mm[1]*this->mm[1] +
240  this->mm[2]*this->mm[2];
241  }
242 
243  /// Return the cross product of "this" vector and v;
244  Vec3<T> cross(const Vec3<T> &v) const
245  {
246  return Vec3<T>(this->mm[1]*v.mm[2] - this->mm[2]*v.mm[1],
247  this->mm[2]*v.mm[0] - this->mm[0]*v.mm[2],
248  this->mm[0]*v.mm[1] - this->mm[1]*v.mm[0]);
249  }
250 
251 
252  /// this = v1 cross v2, v1 and v2 must be distinct objects than "this"
253  const Vec3<T>& cross(const Vec3<T> &v1, const Vec3<T> &v2)
254  {
255  // assert(this!=&v1);
256  // assert(this!=&v2);
257  this->mm[0] = v1.mm[1]*v2.mm[2] - v1.mm[2]*v2.mm[1];
258  this->mm[1] = v1.mm[2]*v2.mm[0] - v1.mm[0]*v2.mm[2];
259  this->mm[2] = v1.mm[0]*v2.mm[1] - v1.mm[1]*v2.mm[0];
260  return *this;
261  }
262 
263  /// Returns v, where \f$v_i *= scalar\f$ for \f$i \in [0, 2]\f$
264  template <typename S>
265  const Vec3<T> &operator*=(S scalar)
266  {
267  this->mm[0] = static_cast<T>(this->mm[0] * scalar);
268  this->mm[1] = static_cast<T>(this->mm[1] * scalar);
269  this->mm[2] = static_cast<T>(this->mm[2] * scalar);
270  return *this;
271  }
272 
273  /// Returns v0, where \f$v0_i *= v1_i\f$ for \f$i \in [0, 2]\f$
274  template <typename S>
275  const Vec3<T> &operator*=(const Vec3<S> &v1)
276  {
277  this->mm[0] *= v1[0];
278  this->mm[1] *= v1[1];
279  this->mm[2] *= v1[2];
280  return *this;
281  }
282 
283  /// Returns v, where \f$v_i /= scalar\f$ for \f$i \in [0, 2]\f$
284  template <typename S>
285  const Vec3<T> &operator/=(S scalar)
286  {
287  this->mm[0] /= scalar;
288  this->mm[1] /= scalar;
289  this->mm[2] /= scalar;
290  return *this;
291  }
292 
293  /// Returns v0, where \f$v0_i /= v1_i\f$ for \f$i \in [0, 2]\f$
294  template <typename S>
295  const Vec3<T> &operator/=(const Vec3<S> &v1)
296  {
297  this->mm[0] /= v1[0];
298  this->mm[1] /= v1[1];
299  this->mm[2] /= v1[2];
300  return *this;
301  }
302 
303  /// Returns v, where \f$v_i += scalar\f$ for \f$i \in [0, 2]\f$
304  template <typename S>
305  const Vec3<T> &operator+=(S scalar)
306  {
307  this->mm[0] = static_cast<T>(this->mm[0] + scalar);
308  this->mm[1] = static_cast<T>(this->mm[1] + scalar);
309  this->mm[2] = static_cast<T>(this->mm[2] + scalar);
310  return *this;
311  }
312 
313  /// Returns v0, where \f$v0_i += v1_i\f$ for \f$i \in [0, 2]\f$
314  template <typename S>
315  const Vec3<T> &operator+=(const Vec3<S> &v1)
316  {
317  this->mm[0] += v1[0];
318  this->mm[1] += v1[1];
319  this->mm[2] += v1[2];
320  return *this;
321  }
322 
323  /// Returns v, where \f$v_i += scalar\f$ for \f$i \in [0, 2]\f$
324  template <typename S>
325  const Vec3<T> &operator-=(S scalar)
326  {
327  this->mm[0] -= scalar;
328  this->mm[1] -= scalar;
329  this->mm[2] -= scalar;
330  return *this;
331  }
332 
333  /// Returns v0, where \f$v0_i -= v1_i\f$ for \f$i \in [0, 2]\f$
334  template <typename S>
335  const Vec3<T> &operator-=(const Vec3<S> &v1)
336  {
337  this->mm[0] -= v1[0];
338  this->mm[1] -= v1[1];
339  this->mm[2] -= v1[2];
340  return *this;
341  }
342 
343  /// Return a reference to itself after the exponent has been
344  /// applied to all the vector components.
345  inline const Vec3<T>& exp()
346  {
347  this->mm[0] = std::exp(this->mm[0]);
348  this->mm[1] = std::exp(this->mm[1]);
349  this->mm[2] = std::exp(this->mm[2]);
350  return *this;
351  }
352 
353  /// Return a reference to itself after log has been
354  /// applied to all the vector components.
355  inline const Vec3<T>& log()
356  {
357  this->mm[0] = std::log(this->mm[0]);
358  this->mm[1] = std::log(this->mm[1]);
359  this->mm[2] = std::log(this->mm[2]);
360  return *this;
361  }
362 
363  /// Return the sum of all the vector components.
364  inline T sum() const
365  {
366  return this->mm[0] + this->mm[1] + this->mm[2];
367  }
368 
369  /// Return the product of all the vector components.
370  inline T product() const
371  {
372  return this->mm[0] * this->mm[1] * this->mm[2];
373  }
374 
375  /// this = normalized this
376  bool normalize(T eps = T(1.0e-7))
377  {
378  T d = length();
379  if (isApproxEqual(d, T(0), eps)) {
380  return false;
381  }
382  *this *= (T(1) / d);
383  return true;
384  }
385 
386 
387  /// return normalized this, throws if null vector
388  Vec3<T> unit(T eps=0) const
389  {
390  T d;
391  return unit(eps, d);
392  }
393 
394  /// return normalized this and length, throws if null vector
395  Vec3<T> unit(T eps, T& len) const
396  {
397  len = length();
398  if (isApproxEqual(len, T(0), eps)) {
399  OPENVDB_THROW(ArithmeticError, "Normalizing null 3-vector");
400  }
401  return *this / len;
402  }
403 
404  /// return normalized this, or (1, 0, 0) if this is null vector
406  {
407  T l2 = lengthSqr();
408  return l2 ? *this / static_cast<T>(sqrt(l2)) : Vec3<T>(1, 0 ,0);
409  }
410 
411  // Number of cols, rows, elements
412  static unsigned numRows() { return 1; }
413  static unsigned numColumns() { return 3; }
414  static unsigned numElements() { return 3; }
415 
416  /// Returns the scalar component of v in the direction of onto, onto need
417  /// not be unit. e.g double c = Vec3d::component(v1,v2);
418  T component(const Vec3<T> &onto, T eps = static_cast<T>(1.0e-7)) const
419  {
420  T l = onto.length();
421  if (isApproxEqual(l, T(0), eps)) return 0;
422 
423  return dot(onto)*(T(1)/l);
424  }
425 
426  /// Return the projection of v onto the vector, onto need not be unit
427  /// e.g. Vec3d a = vprojection(n);
428  Vec3<T> projection(const Vec3<T> &onto, T eps = static_cast<T>(1.0e-7)) const
429  {
430  T l = onto.lengthSqr();
431  if (isApproxEqual(l, T(0), eps)) return Vec3::zero();
432 
433  return onto*(dot(onto)*(T(1)/l));
434  }
435 
436  /// Return an arbitrary unit vector perpendicular to v
437  /// Vector this must be a unit vector
438  /// e.g. v = v.normalize(); Vec3d n = v.getArbPerpendicular();
440  {
441  Vec3<T> u;
442  T l;
443 
444  if ( fabs(this->mm[0]) >= fabs(this->mm[1]) ) {
445  // v.x or v.z is the largest magnitude component, swap them
446  l = this->mm[0]*this->mm[0] + this->mm[2]*this->mm[2];
447  l = static_cast<T>(T(1)/sqrt(double(l)));
448  u.mm[0] = -this->mm[2]*l;
449  u.mm[1] = T(0);
450  u.mm[2] = +this->mm[0]*l;
451  } else {
452  // W.y or W.z is the largest magnitude component, swap them
453  l = this->mm[1]*this->mm[1] + this->mm[2]*this->mm[2];
454  l = static_cast<T>(T(1)/sqrt(double(l)));
455  u.mm[0] = T(0);
456  u.mm[1] = +this->mm[2]*l;
457  u.mm[2] = -this->mm[1]*l;
458  }
459 
460  return u;
461  }
462 
463  /// True if a Nan is present in vector
464  bool isNan() const { return isnan(this->mm[0]) || isnan(this->mm[1]) || isnan(this->mm[2]); }
465 
466  /// True if an Inf is present in vector
467  bool isInfinite() const
468  {
469  return isinf(this->mm[0]) || isinf(this->mm[1]) || isinf(this->mm[2]);
470  }
471 
472  /// True if all no Nan or Inf values present
473  bool isFinite() const
474  {
475  return finite(this->mm[0]) && finite(this->mm[1]) && finite(this->mm[2]);
476  }
477 
478  /// Return a vector with the components of this in ascending order
479  Vec3<T> sorted() const
480  {
481  Vec3<T> r(*this);
482  if( r.mm[0] > r.mm[1] ) std::swap(r.mm[0], r.mm[1]);
483  if( r.mm[1] > r.mm[2] ) std::swap(r.mm[1], r.mm[2]);
484  if( r.mm[0] > r.mm[1] ) std::swap(r.mm[0], r.mm[1]);
485  return r;
486  }
487 
488  /// Return the vector (z, y, x)
490  {
491  return Vec3<T>(this->mm[2], this->mm[1], this->mm[0]);
492  }
493 
494  /// Predefined constants, e.g. Vec3d v = Vec3d::xNegAxis();
495  static Vec3<T> zero() { return Vec3<T>(0, 0, 0); }
496  static Vec3<T> ones() { return Vec3<T>(1, 1, 1); }
497 };
498 
499 
500 /// Equality operator, does exact floating point comparisons
501 template <typename T0, typename T1>
502 inline bool operator==(const Vec3<T0> &v0, const Vec3<T1> &v1)
503 {
504  return isExactlyEqual(v0[0], v1[0]) && isExactlyEqual(v0[1], v1[1])
505  && isExactlyEqual(v0[2], v1[2]);
506 }
507 
508 /// Inequality operator, does exact floating point comparisons
509 template <typename T0, typename T1>
510 inline bool operator!=(const Vec3<T0> &v0, const Vec3<T1> &v1) { return !(v0==v1); }
511 
512 /// Returns V, where \f$V_i = v_i * scalar\f$ for \f$i \in [0, 2]\f$
513 template <typename S, typename T>
514 inline Vec3<typename promote<S, T>::type> operator*(S scalar, const Vec3<T> &v) { return v*scalar; }
515 
516 /// Returns V, where \f$V_i = v_i * scalar\f$ for \f$i \in [0, 2]\f$
517 template <typename S, typename T>
519 {
521  result *= scalar;
522  return result;
523 }
524 
525 /// Returns V, where \f$V_i = v0_i * v1_i\f$ for \f$i \in [0, 2]\f$
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 
534 /// Returns V, where \f$V_i = scalar / v_i\f$ for \f$i \in [0, 2]\f$
535 template <typename S, typename T>
537 {
538  return Vec3<typename promote<S, T>::type>(scalar/v[0], scalar/v[1], scalar/v[2]);
539 }
540 
541 /// Returns V, where \f$V_i = v_i / scalar\f$ for \f$i \in [0, 2]\f$
542 template <typename S, typename T>
544 {
546  result /= scalar;
547  return result;
548 }
549 
550 /// Returns V, where \f$V_i = v0_i / v1_i\f$ for \f$i \in [0, 2]\f$
551 template <typename T0, typename T1>
553 {
554  Vec3<typename promote<T0, T1>::type> result(v0[0] / v1[0], v0[1] / v1[1], v0[2] / v1[2]);
555  return result;
556 }
557 
558 /// Returns V, where \f$V_i = v0_i + v1_i\f$ for \f$i \in [0, 2]\f$
559 template <typename T0, typename T1>
561 {
563  result += v1;
564  return result;
565 }
566 
567 /// Returns V, where \f$V_i = v_i + scalar\f$ for \f$i \in [0, 2]\f$
568 template <typename S, typename T>
570 {
572  result += scalar;
573  return result;
574 }
575 
576 /// Returns V, where \f$V_i = v0_i - v1_i\f$ for \f$i \in [0, 2]\f$
577 template <typename T0, typename T1>
579 {
581  result -= v1;
582  return result;
583 }
584 
585 /// Returns V, where \f$V_i = v_i - scalar\f$ for \f$i \in [0, 2]\f$
586 template <typename S, typename T>
588 {
590  result -= scalar;
591  return result;
592 }
593 
594 /// Angle between two vectors, the result is between [0, pi],
595 /// e.g. double a = Vec3d::angle(v1,v2);
596 template <typename T>
597 inline T angle(const Vec3<T> &v1, const Vec3<T> &v2)
598 {
599  Vec3<T> c = v1.cross(v2);
600  return static_cast<T>(atan2(c.length(), v1.dot(v2)));
601 }
602 
603 template <typename T>
604 inline bool
605 isApproxEqual(const Vec3<T>& a, const Vec3<T>& b)
606 {
607  return a.eq(b);
608 }
609 template <typename T>
610 inline bool
611 isApproxEqual(const Vec3<T>& a, const Vec3<T>& b, const Vec3<T>& eps)
612 {
613  return isApproxEqual(a.x(), b.x(), eps.x()) &&
614  isApproxEqual(a.y(), b.y(), eps.y()) &&
615  isApproxEqual(a.z(), b.z(), eps.z());
616 }
617 
618 template<typename T>
619 inline bool
620 isFinite(const Vec3<T>& v)
621 {
622  return isFinite(v[0]) && isFinite(v[1]) && isFinite(v[2]);
623 }
624 
625 /// Return @c true if all components are exactly equal to zero.
626 template<typename T>
627 inline bool
628 isZero(const Vec3<T>& v)
629 {
630  return isZero(v[0]) && isZero(v[1]) && isZero(v[2]);
631 }
632 
633 template<typename T>
634 inline Vec3<T>
635 Abs(const Vec3<T>& v)
636 {
637  return Vec3<T>(Abs(v[0]), Abs(v[1]), Abs(v[2]));
638 }
639 
640 /// Orthonormalize vectors v1, v2 and v3 and store back the resulting
641 /// basis e.g. Vec3d::orthonormalize(v1,v2,v3);
642 template <typename T>
644 {
645  // If the input vectors are v0, v1, and v2, then the Gram-Schmidt
646  // orthonormalization produces vectors u0, u1, and u2 as follows,
647  //
648  // u0 = v0/|v0|
649  // u1 = (v1-(u0*v1)u0)/|v1-(u0*v1)u0|
650  // u2 = (v2-(u0*v2)u0-(u1*v2)u1)/|v2-(u0*v2)u0-(u1*v2)u1|
651  //
652  // where |A| indicates length of vector A and A*B indicates dot
653  // product of vectors A and B.
654 
655  // compute u0
656  v1.normalize();
657 
658  // compute u1
659  T d0 = v1.dot(v2);
660  v2 -= v1*d0;
661  v2.normalize();
662 
663  // compute u2
664  T d1 = v2.dot(v3);
665  d0 = v1.dot(v3);
666  v3 -= v1*d0 + v2*d1;
667  v3.normalize();
668 }
669 
670 /// @remark We are switching to a more explicit name because the semantics
671 /// are different from std::min/max. In that case, the function returns a
672 /// reference to one of the objects based on a comparator. Here, we must
673 /// fabricate a new object which might not match either of the inputs.
674 
675 /// Return component-wise minimum of the two vectors.
676 template <typename T>
677 inline Vec3<T> minComponent(const Vec3<T> &v1, const Vec3<T> &v2)
678 {
679  return Vec3<T>(
680  std::min(v1.x(), v2.x()),
681  std::min(v1.y(), v2.y()),
682  std::min(v1.z(), v2.z()));
683 }
684 
685 /// Return component-wise maximum of the two vectors.
686 template <typename T>
687 inline Vec3<T> maxComponent(const Vec3<T> &v1, const Vec3<T> &v2)
688 {
689  return Vec3<T>(
690  std::max(v1.x(), v2.x()),
691  std::max(v1.y(), v2.y()),
692  std::max(v1.z(), v2.z()));
693 }
694 
695 /// @brief Return a vector with the exponent applied to each of
696 /// the components of the input vector.
697 template <typename T>
698 inline Vec3<T> Exp(Vec3<T> v) { return v.exp(); }
699 
700 /// @brief Return a vector with log applied to each of
701 /// the components of the input vector.
702 template <typename T>
703 inline Vec3<T> Log(Vec3<T> v) { return v.log(); }
704 
709 
710 } // namespace math
711 } // namespace OPENVDB_VERSION_NAME
712 } // namespace openvdb
713 
714 #endif // OPENVDB_MATH_VEC3_HAS_BEEN_INCLUDED
715 
716 // Copyright (c) 2012-2017 DreamWorks Animation LLC
717 // All rights reserved. This software is distributed under the
718 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
Vec2< T > minComponent(const Vec2< T > &v1, const Vec2< T > &v2)
Return component-wise minimum of the two vectors.
Definition: Vec2.h:553
SYS_API double atan2(double y, double x)
bool isExactlyEqual(const T0 &a, const T1 &b)
Return true if a is exactly equal to b.
Definition: Math.h:407
bool normalize(T eps=T(1.0e-7))
this = normalized this
Definition: Vec3.h:376
const hboost::disable_if_c< VecTraits< T >::IsVec, T >::type & min(const T &a, const T &b)
Definition: Composite.h:128
void swap(UT::ArraySet< Key, MULTI, MAX_LOAD_FACTOR_256, Clearer, Hash, KeyEqual > &a, UT::ArraySet< Key, MULTI, MAX_LOAD_FACTOR_256, Clearer, Hash, KeyEqual > &b)
Definition: UT_ArraySet.h:1561
const GLdouble * v
Definition: glcorearb.h:836
void orthonormalize(Vec2< T > &v1, Vec2< T > &v2)
Definition: Vec2.h:525
Vec3< typename promote< T, Coord::ValueType >::type > operator-(const Vec3< T > &v0, const Coord &v1)
Allow a Coord to be subtracted from a Vec3.
Definition: Coord.h:527
Vec3(const Vec3< Other > &v)
Construct a Vec3 from another Vec3 with a possibly different value type.
Definition: Vec3.h:101
GLdouble GLdouble GLdouble z
Definition: glcorearb.h:847
Mat3< typename promote< T0, T1 >::type > operator*(const Mat3< T0 > &m0, const Mat3< T1 > &m1)
Matrix multiplication.
Definition: Mat3.h:654
T sum() const
Return the sum of all the vector components.
Definition: Vec3.h:364
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1221
Vec3< T > projection(const Vec3< T > &onto, T eps=static_cast< T >(1.0e-7)) const
Definition: Vec3.h:428
Vec2< T > Log(Vec2< T > v)
Return a vector with log applied to each of the components of the input vector.
Definition: Vec2.h:577
Dummy class for tag dispatch of conversion constructors.
Definition: Tuple.h:47
Vec3< T > unitSafe() const
return normalized this, or (1, 0, 0) if this is null vector
Definition: Vec3.h:405
GLint y
Definition: glcorearb.h:102
GLfloat GLfloat GLfloat v2
Definition: glcorearb.h:817
GLfloat GLfloat GLfloat GLfloat v3
Definition: glcorearb.h:818
const Vec3< T > & operator+=(S scalar)
Returns v, where for .
Definition: Vec3.h:305
static Vec3< T > zero()
Predefined constants, e.g. Vec3d v = Vec3d::xNegAxis();.
Definition: Vec3.h:495
T dot(const Vec3< T > &v) const
Dot product.
Definition: Vec3.h:215
T product() const
Return the product of all the vector components.
Definition: Vec3.h:370
png_uint_32 i
Definition: png.h:2877
Vec2< typename promote< S, T >::type > operator/(S scalar, const Vec2< T > &v)
Returns V, where for .
Definition: Vec2.h:419
T x() const
Get the component, e.g. float f = v.y();.
Definition: Vec3.h:114
const hboost::disable_if_c< VecTraits< T >::IsVec, T >::type & max(const T &a, const T &b)
Definition: Composite.h:132
T operator()(int i) const
Alternative indexed constant reference to the elements,.
Definition: Vec3.h:125
SYS_API double log(double x)
const Vec3< T > & setZero()
Set "this" vector to zero.
Definition: Vec3.h:137
Vec3(Source *a)
Constructor with array argument, e.g. double a[3]; Vec3d v(a);.
Definition: Vec3.h:71
bool isNan() const
True if a Nan is present in vector.
Definition: Vec3.h:464
const Vec3< T > & scale(T0 scale, const Vec3< T1 > &v)
Definition: Vec3.h:195
Vec3< T > reversed() const
Return the vector (z, y, x)
Definition: Vec3.h:489
T component(const Vec3< T > &onto, T eps=static_cast< T >(1.0e-7)) const
Definition: Vec3.h:418
Coord Abs(const Coord &xyz)
Definition: Coord.h:254
bool isFinite(const Type &x)
Return true if x is finite.
Definition: Math.h:363
#define OPENVDB_VERSION_NAME
Definition: version.h:43
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:253
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
const Vec3< T > & div(T0 scale, const Vec3< T1 > &v)
Definition: Vec3.h:205
SYS_API double exp(double x)
Vec3< T > cross(const Vec3< T > &v) const
Return the cross product of "this" vector and v;.
Definition: Vec3.h:244
const Vec3< T > & operator+=(const Vec3< S > &v1)
Returns v0, where for .
Definition: Vec3.h:315
T angle(const Vec2< T > &v1, const Vec2< T > &v2)
Definition: Vec2.h:480
bool isRelOrApproxEqual(const Type &a, const Type &b, const Type &absTol, const Type &relTol)
Definition: Math.h:417
Vec3< T > sorted() const
Return a vector with the components of this in ascending order.
Definition: Vec3.h:479
Vec3< T > unit(T eps=0) const
return normalized this, throws if null vector
Definition: Vec3.h:388
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1221
Vec3< T > unit(T eps, T &len) const
return normalized this and length, throws if null vector
Definition: Vec3.h:395
bool isApproxEqual(const Type &a, const Type &b)
Return true if a is equal to b to within the default floating-point comparison tolerance.
Definition: Math.h:370
GLfloat v0
Definition: glcorearb.h:815
Vec3< T > getArbPerpendicular() const
Definition: Vec3.h:439
bool operator!=(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Inequality operator, does exact floating point comparisons.
Definition: Vec3.h:510
bool isInfinite() const
True if an Inf is present in vector.
Definition: Vec3.h:467
GLsizei const GLfloat * value
Definition: glcorearb.h:823
const Vec3< T > & operator/=(const Vec3< S > &v1)
Returns v0, where for .
Definition: Vec3.h:295
bool isFinite() const
True if all no Nan or Inf values present.
Definition: Vec3.h:473
Vec3< T > operator-() const
Negation operator, for e.g. v1 = -v2;.
Definition: Vec3.h:166
const Vec3< T > & operator/=(S scalar)
Returns v, where for .
Definition: Vec3.h:285
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:157
const Vec3< T > & operator=(const Vec3< Source > &v)
Assignment operator.
Definition: Vec3.h:146
const Vec3< T > & add(const Vec3< T0 > &v1, const Vec3< T1 > &v2)
Definition: Vec3.h:171
T & x()
Reference to the component, e.g. v.x() = 4.5f;.
Definition: Vec3.h:109
Vec3(T x, T y, T z)
Constructor with three arguments, e.g. Vec3d v(1,2,3);.
Definition: Vec3.h:62
Vec3(const Tuple< 3, Source > &v)
Construct a Vec3 from a 3-Tuple with a possibly different value type.
Definition: Vec3.h:81
const Vec3< T > & init(T x=0, T y=0, T z=0)
Definition: Vec3.h:129
Vec3(T val)
Construct a vector all of whose components have the given value.
Definition: Vec3.h:59
GLint GLenum GLint x
Definition: glcorearb.h:408
GLfloat GLfloat v1
Definition: glcorearb.h:816
GLuint GLfloat * val
Definition: glcorearb.h:1607
Vec2< T > maxComponent(const Vec2< T > &v1, const Vec2< T > &v2)
Return component-wise maximum of the two vectors.
Definition: Vec2.h:562
Vec3()
Trivial constructor, the vector is NOT initialized.
Definition: Vec3.h:56
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glcorearb.h:107
Vec3< typename promote< T, typename Coord::ValueType >::type > operator+(const Vec3< T > &v0, const Coord &v1)
Allow a Coord to be added to or subtracted from a Vec3.
Definition: Coord.h:501
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:92
GLboolean r
Definition: glcorearb.h:1221
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71
const Vec3< T > & operator-=(const Vec3< S > &v1)
Returns v0, where for .
Definition: Vec3.h:335
const Vec3< T > & operator*=(S scalar)
Returns v, where for .
Definition: Vec3.h:265
const Vec3< T > & operator*=(const Vec3< S > &v1)
Returns v0, where for .
Definition: Vec3.h:275
const Vec3< T > & operator-=(S scalar)
Returns v, where for .
Definition: Vec3.h:325
bool operator==(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Equality operator, does exact floating point comparisons.
Definition: Vec3.h:502
T length() const
Length of the vector.
Definition: Vec3.h:224
bool isZero(const Type &x)
Return true if x is exactly equal to zero.
Definition: Math.h:324
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:101
const Vec3< T > & sub(const Vec3< T0 > &v1, const Vec3< T1 > &v2)
Definition: Vec3.h:183
Type Exp(const Type &x)
Return .
Definition: Math.h:676
T & operator()(int i)
Alternative indexed reference to the elements.
Definition: Vec3.h:122