OpenVDB  1.1.0
Vec3.h
Go to the documentation of this file.
1 
2 //
3 // Copyright (c) 2012-2013 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 //
30 
31 #ifndef OPENVDB_MATH_VEC3_HAS_BEEN_INCLUDED
32 #define OPENVDB_MATH_VEC3_HAS_BEEN_INCLUDED
33 
34 #include <cmath>
35 #include <openvdb/Exceptions.h>
36 #include "Math.h"
37 #include "Tuple.h"
38 
39 namespace openvdb {
41 namespace OPENVDB_VERSION_NAME {
42 namespace math {
43 
44 template<typename T> class Mat3;
45 
46 template<typename T>
47 class Vec3: public Tuple<3, T>
48 {
49 public:
50  typedef T value_type;
51  typedef T ValueType;
52 
54  Vec3() {}
55 
57  explicit Vec3(T val) { this->mm[0] = this->mm[1] = this->mm[2] = val; }
58 
60  Vec3(T x, T y, T z)
61  {
62  this->mm[0] = x;
63  this->mm[1] = y;
64  this->mm[2] = z;
65  }
66 
68  template <typename Source>
69  Vec3(Source *a)
70  {
71  this->mm[0] = a[0];
72  this->mm[1] = a[1];
73  this->mm[2] = a[2];
74  }
75 
77  template<typename Source>
78  explicit Vec3(const Tuple<3, Source> &v)
79  {
80  this->mm[0] = static_cast<T>(v[0]);
81  this->mm[1] = static_cast<T>(v[1]);
82  this->mm[2] = static_cast<T>(v[2]);
83  }
84 
85  template<typename Other>
86  Vec3(const Vec3<Other>& v)
87  {
88  this->mm[0] = static_cast<T>(v[0]);
89  this->mm[1] = static_cast<T>(v[1]);
90  this->mm[2] = static_cast<T>(v[2]);
91  }
92 
94  T& x() { return this->mm[0]; }
95  T& y() { return this->mm[1]; }
96  T& z() { return this->mm[2]; }
97 
99  T x() const { return this->mm[0]; }
100  T y() const { return this->mm[1]; }
101  T z() const { return this->mm[2]; }
102 
103  T* asPointer() { return this->mm; }
104  const T* asPointer() const { return this->mm; }
105 
107  T& operator()(int i) { return this->mm[i]; }
108 
110  T operator()(int i) const { return this->mm[i]; }
111 
114  const Vec3<T>& init(T x=0, T y=0, T z=0)
115  {
116  this->mm[0] = x; this->mm[1] = y; this->mm[2] = z;
117  return *this;
118  }
119 
120 
122  const Vec3<T>& setZero()
123  {
124  this->mm[0] = 0; this->mm[1] = 0; this->mm[2] = 0;
125  return *this;
126  }
127 
129  template<typename Source>
130  const Vec3<T>& operator=(const Vec3<Source> &v)
131  {
132  // note: don't static_cast because that suppresses warnings
133  this->mm[0] = ValueType(v[0]);
134  this->mm[1] = ValueType(v[1]);
135  this->mm[2] = ValueType(v[2]);
136 
137  return *this;
138  }
139 
141  bool eq(const Vec3<T> &v, T eps = static_cast<T>(1.0e-7)) const
142  {
143  return isRelOrApproxEqual(this->mm[0], v.mm[0], eps, eps) &&
144  isRelOrApproxEqual(this->mm[1], v.mm[1], eps, eps) &&
145  isRelOrApproxEqual(this->mm[2], v.mm[2], eps, eps);
146  }
147 
148 
150  Vec3<T> operator-() const { return Vec3<T>(-this->mm[0], -this->mm[1], -this->mm[2]); }
151 
154  template <typename T0, typename T1>
155  const Vec3<T>& add(const Vec3<T0> &v1, const Vec3<T1> &v2)
156  {
157  this->mm[0] = v1[0] + v2[0];
158  this->mm[1] = v1[1] + v2[1];
159  this->mm[2] = v1[2] + v2[2];
160 
161  return *this;
162  }
163 
166  template <typename T0, typename T1>
167  const Vec3<T>& sub(const Vec3<T0> &v1, const Vec3<T1> &v2)
168  {
169  this->mm[0] = v1[0] - v2[0];
170  this->mm[1] = v1[1] - v2[1];
171  this->mm[2] = v1[2] - v2[2];
172 
173  return *this;
174  }
175 
178  template <typename T0, typename T1>
179  const Vec3<T>& scale(T0 scale, const Vec3<T1> &v)
180  {
181  this->mm[0] = scale * v[0];
182  this->mm[1] = scale * v[1];
183  this->mm[2] = scale * v[2];
184 
185  return *this;
186  }
187 
188  template <typename T0, typename T1>
189  const Vec3<T> &div(T0 scale, const Vec3<T1> &v)
190  {
191  this->mm[0] = v[0] / scale;
192  this->mm[1] = v[1] / scale;
193  this->mm[2] = v[2] / scale;
194 
195  return *this;
196  }
197 
199  T dot(const Vec3<T> &v) const
200  {
201  return
202  this->mm[0]*v.mm[0] +
203  this->mm[1]*v.mm[1] +
204  this->mm[2]*v.mm[2];
205  }
206 
208  T length() const
209  {
210  return static_cast<T>(sqrt(double(
211  this->mm[0]*this->mm[0] +
212  this->mm[1]*this->mm[1] +
213  this->mm[2]*this->mm[2])));
214  }
215 
216 
219  T lengthSqr() const
220  {
221  return
222  this->mm[0]*this->mm[0] +
223  this->mm[1]*this->mm[1] +
224  this->mm[2]*this->mm[2];
225  }
226 
228  Vec3<T> cross(const Vec3<T> &v) const
229  {
230  return Vec3<T>(this->mm[1]*v.mm[2] - this->mm[2]*v.mm[1],
231  this->mm[2]*v.mm[0] - this->mm[0]*v.mm[2],
232  this->mm[0]*v.mm[1] - this->mm[1]*v.mm[0]);
233  }
234 
235 
237  const Vec3<T>& cross(const Vec3<T> &v1, const Vec3<T> &v2)
238  {
239  // assert(this!=&v1);
240  // assert(this!=&v2);
241  this->mm[0] = v1.mm[1]*v2.mm[2] - v1.mm[2]*v2.mm[1];
242  this->mm[1] = v1.mm[2]*v2.mm[0] - v1.mm[0]*v2.mm[2];
243  this->mm[2] = v1.mm[0]*v2.mm[1] - v1.mm[1]*v2.mm[0];
244  return *this;
245  }
246 
248  template <typename S>
249  const Vec3<T> &operator*=(S scalar)
250  {
251  this->mm[0] *= scalar;
252  this->mm[1] *= scalar;
253  this->mm[2] *= scalar;
254  return *this;
255  }
256 
258  template <typename S>
259  const Vec3<T> &operator*=(const Vec3<S> &v1)
260  {
261  this->mm[0] *= v1[0];
262  this->mm[1] *= v1[1];
263  this->mm[2] *= v1[2];
264  return *this;
265  }
266 
268  template <typename S>
269  const Vec3<T> &operator/=(S scalar)
270  {
271  this->mm[0] /= scalar;
272  this->mm[1] /= scalar;
273  this->mm[2] /= scalar;
274  return *this;
275  }
276 
278  template <typename S>
279  const Vec3<T> &operator/=(const Vec3<S> &v1)
280  {
281  this->mm[0] /= v1[0];
282  this->mm[1] /= v1[1];
283  this->mm[2] /= v1[2];
284  return *this;
285  }
286 
288  template <typename S>
289  const Vec3<T> &operator+=(S scalar)
290  {
291  this->mm[0] += scalar;
292  this->mm[1] += scalar;
293  this->mm[2] += scalar;
294  return *this;
295  }
296 
298  template <typename S>
299  const Vec3<T> &operator+=(const Vec3<S> &v1)
300  {
301  this->mm[0] += v1[0];
302  this->mm[1] += v1[1];
303  this->mm[2] += v1[2];
304  return *this;
305  }
306 
308  template <typename S>
309  const Vec3<T> &operator-=(S scalar)
310  {
311  this->mm[0] -= scalar;
312  this->mm[1] -= scalar;
313  this->mm[2] -= scalar;
314  return *this;
315  }
316 
318  template <typename S>
319  const Vec3<T> &operator-=(const Vec3<S> &v1)
320  {
321  this->mm[0] -= v1[0];
322  this->mm[1] -= v1[1];
323  this->mm[2] -= v1[2];
324  return *this;
325  }
326 
328  bool normalize(T eps = T(1.0e-7))
329  {
330  T d = length();
331  if (isApproxEqual(d, T(0), eps)) {
332  return false;
333  }
334  *this *= (T(1) / d);
335  return true;
336  }
337 
338 
340  Vec3<T> unit(T eps=0) const
341  {
342  T d;
343  return unit(eps, d);
344  }
345 
347  Vec3<T> unit(T eps, T& len) const
348  {
349  len = length();
350  if (isApproxEqual(len, T(0), eps)) {
351  OPENVDB_THROW(ArithmeticError, "Normalizing null 3-vector");
352  }
353  return *this / len;
354  }
355 
356  // Number of cols, rows, elements
357  static unsigned numRows() { return 1; }
358  static unsigned numColumns() { return 3; }
359  static unsigned numElements() { return 3; }
360 
363  T component(const Vec3<T> &onto, T eps=1.0e-7) const
364  {
365  T l = onto.length();
366  if (isApproxEqual(l, T(0), eps)) return 0;
367 
368  return dot(onto)*(T(1)/l);
369  }
370 
373  Vec3<T> projection(const Vec3<T> &onto, T eps=1.0e-7) const
374  {
375  T l = onto.lengthSqr();
376  if (isApproxEqual(l, T(0), eps)) return Vec3::zero();
377 
378  return onto*(dot(onto)*(T(1)/l));
379  }
380 
384  Vec3<T> getArbPerpendicular() const
385  {
386  Vec3<T> u;
387  T l;
388 
389  if ( fabs(this->mm[0]) >= fabs(this->mm[1]) ) {
390  // v.x or v.z is the largest magnitude component, swap them
391  l = this->mm[0]*this->mm[0] + this->mm[2]*this->mm[2];
392  l = static_cast<T>(T(1)/sqrt(double(l)));
393  u.mm[0] = -this->mm[2]*l;
394  u.mm[1] = (T)0.0;
395  u.mm[2] = +this->mm[0]*l;
396  } else {
397  // W.y or W.z is the largest magnitude component, swap them
398  l = this->mm[1]*this->mm[1] + this->mm[2]*this->mm[2];
399  l = static_cast<T>(T(1)/sqrt(double(l)));
400  u.mm[0] = (T)0.0;
401  u.mm[1] = +this->mm[2]*l;
402  u.mm[2] = -this->mm[1]*l;
403  }
404 
405  return u;
406  }
407 
409  bool isNan() const { return isnan(this->mm[0]) || isnan(this->mm[1]) || isnan(this->mm[2]); }
410 
412  bool isInfinite() const
413  {
414  return isinf(this->mm[0]) || isinf(this->mm[1]) || isinf(this->mm[2]);
415  }
416 
418  bool isFinite() const
419  {
420  return finite(this->mm[0]) && finite(this->mm[1]) && finite(this->mm[2]);
421  }
422 
424  static Vec3<T> zero() { return Vec3<T>(0, 0, 0); }
425 };
426 
427 
429 template <typename T0, typename T1>
430 inline bool operator==(const Vec3<T0> &v0, const Vec3<T1> &v1)
431 {
432  return isExactlyEqual(v0[0], v1[0]) && isExactlyEqual(v0[1], v1[1])
433  && isExactlyEqual(v0[2], v1[2]);
434 }
435 
437 template <typename T0, typename T1>
438 inline bool operator!=(const Vec3<T0> &v0, const Vec3<T1> &v1) { return !(v0==v1); }
439 
441 template <typename S, typename T>
442 inline Vec3<typename promote<S, T>::type> operator*(S scalar, const Vec3<T> &v) { return v*scalar; }
443 
445 template <typename S, typename T>
447 {
449  result *= scalar;
450  return result;
451 }
452 
454 template <typename T0, typename T1>
456 {
457  Vec3<typename promote<T0, T1>::type> result(v0[0] * v1[0], v0[1] * v1[1], v0[2] * v1[2]);
458  return result;
459 }
460 
461 
463 template <typename S, typename T>
465 {
466  return Vec3<typename promote<S, T>::type>(scalar/v[0], scalar/v[1], scalar/v[2]);
467 }
468 
470 template <typename S, typename T>
472 {
474  result /= scalar;
475  return result;
476 }
477 
479 template <typename T0, typename T1>
481 {
482  Vec3<typename promote<T0, T1>::type> result(v0[0] / v1[0], v0[1] / v1[1], v0[2] / v1[2]);
483  return result;
484 }
485 
487 template <typename T0, typename T1>
489 {
491  result += v1;
492  return result;
493 }
494 
496 template <typename S, typename T>
498 {
500  result += scalar;
501  return result;
502 }
503 
505 template <typename T0, typename T1>
507 {
509  result -= v1;
510  return result;
511 }
512 
514 template <typename S, typename T>
516 {
518  result -= scalar;
519  return result;
520 }
521 
524 template <typename T>
525 inline T angle(const Vec3<T> &v1, const Vec3<T> &v2)
526 {
527  Vec3<T> c = v1.cross(v2);
528  return static_cast<T>(atan2(c.length(), v1.dot(v2)));
529 }
530 
531 template <typename T>
532 inline bool
533 isApproxEqual(const Vec3<T>& a, const Vec3<T>& b)
534 {
535  return a.eq(b);
536 }
537 template <typename T>
538 inline bool
539 isApproxEqual(const Vec3<T>& a, const Vec3<T>& b, const Vec3<T>& eps)
540 {
541  return isApproxEqual(a.x(), b.x(), eps.x()) &&
542  isApproxEqual(a.y(), b.y(), eps.y()) &&
543  isApproxEqual(a.z(), b.z(), eps.z());
544 }
545 
548 template <typename T>
549 inline void orthonormalize(Vec3<T> &v1, Vec3<T> &v2, Vec3<T> &v3)
550 {
551  // If the input vectors are v0, v1, and v2, then the Gram-Schmidt
552  // orthonormalization produces vectors u0, u1, and u2 as follows,
553  //
554  // u0 = v0/|v0|
555  // u1 = (v1-(u0*v1)u0)/|v1-(u0*v1)u0|
556  // u2 = (v2-(u0*v2)u0-(u1*v2)u1)/|v2-(u0*v2)u0-(u1*v2)u1|
557  //
558  // where |A| indicates length of vector A and A*B indicates dot
559  // product of vectors A and B.
560 
561  // compute u0
562  v1.normalize();
563 
564  // compute u1
565  T d0 = v1.dot(v2);
566  v2 -= v1*d0;
567  v2.normalize();
568 
569  // compute u2
570  T d1 = v2.dot(v3);
571  d0 = v1.dot(v3);
572  v3 -= v1*d0 + v2*d1;
573  v3.normalize();
574 }
575 
580 
582 template <typename T>
583 inline Vec3<T> minComponent(const Vec3<T> &v1, const Vec3<T> &v2)
584 {
585  return Vec3<T>(
586  std::min(v1.x(), v2.x()),
587  std::min(v1.y(), v2.y()),
588  std::min(v1.z(), v2.z()));
589 }
590 
592 template <typename T>
593 inline Vec3<T> maxComponent(const Vec3<T> &v1, const Vec3<T> &v2)
594 {
595  return Vec3<T>(
596  std::max(v1.x(), v2.x()),
597  std::max(v1.y(), v2.y()),
598  std::max(v1.z(), v2.z()));
599 }
600 
601 typedef Vec3<int> Vec3i;
605 
606 #if DWREAL_IS_DOUBLE == 1
607 typedef Vec3d Vec3f;
608 #else
609 typedef Vec3s Vec3f;
610 #endif // DWREAL_IS_DOUBLE
611 
612 } // namespace math
613 } // namespace OPENVDB_VERSION_NAME
614 } // namespace openvdb
615 
616 #endif // OPENVDB_MATH_VEC3_HAS_BEEN_INCLUDED
617 
618 // Copyright (c) 2012-2013 DreamWorks Animation LLC
619 // All rights reserved. This software is distributed under the
620 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )