OpenVDB  4.0.1
Vec2.h
Go to the documentation of this file.
1 //
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 //
30 
31 #ifndef OPENVDB_MATH_VEC2_HAS_BEEN_INCLUDED
32 #define OPENVDB_MATH_VEC2_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 Mat2;
47 
48 template<typename T>
49 class Vec2: public Tuple<2, T>
50 {
51 public:
52  typedef T value_type;
53  typedef T ValueType;
54 
56  Vec2() {}
57 
59  explicit Vec2(T val) { this->mm[0] = this->mm[1] = val; }
60 
62  Vec2(T x, T y)
63  {
64  this->mm[0] = x;
65  this->mm[1] = y;
66  }
67 
69  template <typename Source>
70  Vec2(Source *a)
71  {
72  this->mm[0] = a[0];
73  this->mm[1] = a[1];
74  } // trivial
75 
77  template<typename Source>
78  explicit Vec2(const Tuple<2, Source> &t)
79  {
80  this->mm[0] = static_cast<T>(t[0]);
81  this->mm[1] = static_cast<T>(t[1]);
82  }
83 
87  template<typename Other>
88  explicit Vec2(Other val,
89  typename std::enable_if<std::is_arithmetic<Other>::value, Conversion>::type = Conversion{})
90  {
91  this->mm[0] = this->mm[1] = static_cast<T>(val);
92  }
93 
95  T& x() {return this->mm[0];}
96  T& y() {return this->mm[1];}
97 
99  T x() const {return this->mm[0];}
100  T y() const {return this->mm[1];}
101 
103  T& operator()(int i) {return this->mm[i];}
104 
106  T operator()(int i) const {return this->mm[i];}
107 
108  T* asPointer() {return this->mm;}
109  const T* asPointer() const {return this->mm;}
110 
113  const Vec2<T>& init(T x=0, T y=0)
114  {
115  this->mm[0] = x; this->mm[1] = y;
116  return *this;
117  }
118 
120  const Vec2<T>& setZero()
121  {
122  this->mm[0] = 0; this->mm[1] = 0;
123  return *this;
124  }
125 
127  template<typename Source>
128  const Vec2<T>& operator=(const Vec2<Source> &v)
129  {
130  // note: don't static_cast because that suppresses warnings
131  this->mm[0] = v[0];
132  this->mm[1] = v[1];
133 
134  return *this;
135  }
136 
138  bool operator==(const Vec2<T> &v) const
139  {
140  return (isExactlyEqual(this->mm[0], v.mm[0]) && isExactlyEqual(this->mm[1], v.mm[1]));
141  }
142 
144  bool operator!=(const Vec2<T> &v) const { return !(*this==v); }
145 
147  bool eq(const Vec2<T> &v, T eps = static_cast<T>(1.0e-7)) const
148  {
149  return isApproxEqual(this->mm[0], v.mm[0], eps) &&
150  isApproxEqual(this->mm[1], v.mm[1], eps);
151  } // trivial
152 
154  Vec2<T> operator-() const {return Vec2<T>(-this->mm[0], -this->mm[1]);}
155 
158  template <typename T0, typename T1>
159  const Vec2<T>& add(const Vec2<T0> &v1, const Vec2<T1> &v2)
160  {
161  this->mm[0] = v1[0] + v2[0];
162  this->mm[1] = v1[1] + v2[1];
163 
164  return *this;
165  }
166 
169  template <typename T0, typename T1>
170  const Vec2<T>& sub(const Vec2<T0> &v1, const Vec2<T1> &v2)
171  {
172  this->mm[0] = v1[0] - v2[0];
173  this->mm[1] = v1[1] - v2[1];
174 
175  return *this;
176  }
177 
180  template <typename T0, typename T1>
181  const Vec2<T>& scale(T0 scalar, const Vec2<T1> &v)
182  {
183  this->mm[0] = scalar * v[0];
184  this->mm[1] = scalar * v[1];
185 
186  return *this;
187  }
188 
189  template <typename T0, typename T1>
190  const Vec2<T> &div(T0 scalar, const Vec2<T1> &v)
191  {
192  this->mm[0] = v[0] / scalar;
193  this->mm[1] = v[1] / scalar;
194 
195  return *this;
196  }
197 
199  T dot(const Vec2<T> &v) const { return this->mm[0]*v[0] + this->mm[1]*v[1]; } // trivial
200 
202  T length() const
203  {
204  return static_cast<T>(sqrt(double(this->mm[0]*this->mm[0] + this->mm[1]*this->mm[1])));
205  }
206 
209  T lengthSqr() const { return (this->mm[0]*this->mm[0] + this->mm[1]*this->mm[1]); }
210 
213  inline const Vec2<T>& exp()
214  {
215  this->mm[0] = std::exp(this->mm[0]);
216  this->mm[1] = std::exp(this->mm[1]);
217  return *this;
218  }
219 
222  inline const Vec2<T>& log()
223  {
224  this->mm[0] = std::log(this->mm[0]);
225  this->mm[1] = std::log(this->mm[1]);
226  return *this;
227  }
228 
230  inline T sum() const
231  {
232  return this->mm[0] + this->mm[1];
233  }
234 
236  inline T product() const
237  {
238  return this->mm[0] * this->mm[1];
239  }
240 
242  bool normalize(T eps=1.0e-8)
243  {
244  T d = length();
245  if (isApproxEqual(d, T(0), eps)) {
246  return false;
247  }
248  *this *= (T(1) / d);
249  return true;
250  }
251 
253  Vec2<T> unit(T eps=0) const
254  {
255  T d;
256  return unit(eps, d);
257  }
258 
260  Vec2<T> unit(T eps, T& len) const
261  {
262  len = length();
263  if (isApproxEqual(len, T(0), eps)) {
264  OPENVDB_THROW(ArithmeticError, "Normalizing null 2-vector");
265  }
266  return *this / len;
267  }
268 
271  {
272  T l2 = lengthSqr();
273  return l2 ? *this/static_cast<T>(sqrt(l2)) : Vec2<T>(1,0);
274  }
275 
277  template <typename S>
278  const Vec2<T> &operator*=(S scalar)
279  {
280  this->mm[0] *= scalar;
281  this->mm[1] *= scalar;
282  return *this;
283  }
284 
286  template <typename S>
287  const Vec2<T> &operator*=(const Vec2<S> &v1)
288  {
289  this->mm[0] *= v1[0];
290  this->mm[1] *= v1[1];
291  return *this;
292  }
293 
295  template <typename S>
296  const Vec2<T> &operator/=(S scalar)
297  {
298  this->mm[0] /= scalar;
299  this->mm[1] /= scalar;
300  return *this;
301  }
302 
304  template <typename S>
305  const Vec2<T> &operator/=(const Vec2<S> &v1)
306  {
307  this->mm[0] /= v1[0];
308  this->mm[1] /= v1[1];
309  return *this;
310  }
311 
313  template <typename S>
314  const Vec2<T> &operator+=(S scalar)
315  {
316  this->mm[0] += scalar;
317  this->mm[1] += scalar;
318  return *this;
319  }
320 
322  template <typename S>
323  const Vec2<T> &operator+=(const Vec2<S> &v1)
324  {
325  this->mm[0] += v1[0];
326  this->mm[1] += v1[1];
327  return *this;
328  }
329 
331  template <typename S>
332  const Vec2<T> &operator-=(S scalar)
333  {
334  this->mm[0] -= scalar;
335  this->mm[1] -= scalar;
336  return *this;
337  }
338 
340  template <typename S>
341  const Vec2<T> &operator-=(const Vec2<S> &v1)
342  {
343  this->mm[0] -= v1[0];
344  this->mm[1] -= v1[1];
345  return *this;
346  }
347 
348  // Number of cols, rows, elements
349  static unsigned numRows() { return 1; }
350  static unsigned numColumns() { return 2; }
351  static unsigned numElements() { return 2; }
352 
355  T component(const Vec2<T> &onto, T eps=1.0e-8) const
356  {
357  T l = onto.length();
358  if (isApproxEqual(l, T(0), eps)) return 0;
359 
360  return dot(onto)*(T(1)/l);
361  }
362 
365  Vec2<T> projection(const Vec2<T> &onto, T eps=1.0e-8) const
366  {
367  T l = onto.lengthSqr();
368  if (isApproxEqual(l, T(0), eps)) return Vec2::zero();
369 
370  return onto*(dot(onto)*(T(1)/l));
371  }
372 
376  Vec2<T> getArbPerpendicular() const { return Vec2<T>(-this->mm[1], this->mm[0]); }
377 
379  bool isNan() const { return isnan(this->mm[0]) || isnan(this->mm[1]); }
380 
382  bool isInfinite() const { return isinf(this->mm[0]) || isinf(this->mm[1]); }
383 
385  bool isFinite() const { return finite(this->mm[0]) && finite(this->mm[1]); }
386 
388  static Vec2<T> zero() { return Vec2<T>(0, 0); }
389  static Vec2<T> ones() { return Vec2<T>(1, 1); }
390 };
391 
392 
394 template <typename S, typename T>
396 {
397  return v * scalar;
398 }
399 
401 template <typename S, typename T>
403 {
405  result *= scalar;
406  return result;
407 }
408 
410 template <typename T0, typename T1>
412 {
413  Vec2<typename promote<T0, T1>::type> result(v0[0] * v1[0], v0[1] * v1[1]);
414  return result;
415 }
416 
418 template <typename S, typename T>
420 {
421  return Vec2<typename promote<S, T>::type>(scalar/v[0], scalar/v[1]);
422 }
423 
425 template <typename S, typename T>
427 {
429  result /= scalar;
430  return result;
431 }
432 
434 template <typename T0, typename T1>
436 {
437  Vec2<typename promote<T0, T1>::type> result(v0[0] / v1[0], v0[1] / v1[1]);
438  return result;
439 }
440 
442 template <typename T0, typename T1>
444 {
446  result += v1;
447  return result;
448 }
449 
451 template <typename S, typename T>
453 {
455  result += scalar;
456  return result;
457 }
458 
460 template <typename T0, typename T1>
462 {
464  result -= v1;
465  return result;
466 }
467 
469 template <typename S, typename T>
471 {
473  result -= scalar;
474  return result;
475 }
476 
479 template <typename T>
480 inline T angle(const Vec2<T> &v1, const Vec2<T> &v2)
481 {
482  T c = v1.dot(v2);
483  return acos(c);
484 }
485 
486 template <typename T>
487 inline bool
488 isApproxEqual(const Vec2<T>& a, const Vec2<T>& b)
489 {
490  return a.eq(b);
491 }
492 template <typename T>
493 inline bool
494 isApproxEqual(const Vec2<T>& a, const Vec2<T>& b, const Vec2<T>& eps)
495 {
496  return isApproxEqual(a.x(), b.x(), eps.x()) &&
497  isApproxEqual(a.y(), b.y(), eps.y());
498 }
499 
500 template<typename T>
501 inline bool
502 isFinite(const Vec2<T>& v)
503 {
504  return isFinite(v[0]) && isFinite(v[1]);
505 }
506 
508 template<typename T>
509 inline bool
510 isZero(const Vec2<T>& v)
511 {
512  return isZero(v[0]) && isZero(v[1]);
513 }
514 
515 template<typename T>
516 inline Vec2<T>
517 Abs(const Vec2<T>& v)
518 {
519  return Vec2<T>(Abs(v[0]), Abs(v[1]));
520 }
521 
524 template <typename T>
525 inline void orthonormalize(Vec2<T> &v1, Vec2<T> &v2)
526 {
527  // If the input vectors are v0, v1, and v2, then the Gram-Schmidt
528  // orthonormalization produces vectors u0, u1, and u2 as follows,
529  //
530  // u0 = v0/|v0|
531  // u1 = (v1-(u0*v1)u0)/|v1-(u0*v1)u0|
532  //
533  // where |A| indicates length of vector A and A*B indicates dot
534  // product of vectors A and B.
535 
536  // compute u0
537  v1.normalize();
538 
539  // compute u1
540  T d0 = v1.dot(v2);
541  v2 -= v1*d0;
542  v2.normalize();
543 }
544 
545 
550 
552 template <typename T>
553 inline Vec2<T> minComponent(const Vec2<T> &v1, const Vec2<T> &v2)
554 {
555  return Vec2<T>(
556  std::min(v1.x(), v2.x()),
557  std::min(v1.y(), v2.y()));
558 }
559 
561 template <typename T>
562 inline Vec2<T> maxComponent(const Vec2<T> &v1, const Vec2<T> &v2)
563 {
564  return Vec2<T>(
565  std::max(v1.x(), v2.x()),
566  std::max(v1.y(), v2.y()));
567 }
568 
571 template <typename T>
572 inline Vec2<T> Exp(Vec2<T> v) { return v.exp(); }
573 
576 template <typename T>
577 inline Vec2<T> Log(Vec2<T> v) { return v.log(); }
578 
583 
584 } // namespace math
585 } // namespace OPENVDB_VERSION_NAME
586 } // namespace openvdb
587 
588 #endif // OPENVDB_MATH_VEC2_HAS_BEEN_INCLUDED
589 
590 // Copyright (c) 2012-2017 DreamWorks Animation LLC
591 // All rights reserved. This software is distributed under the
592 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
const Vec2< T > & operator-=(S scalar)
Returns v, where for .
Definition: Vec2.h:332
const Vec2< T > & div(T0 scalar, const Vec2< T1 > &v)
Definition: Vec2.h:190
const Vec2< T > & operator=(const Vec2< Source > &v)
Assignment operator.
Definition: Vec2.h:128
Vec2< double > Vec2d
Definition: Vec2.h:582
T * asPointer()
Definition: Vec2.h:108
bool isApproxEqual(const Vec2< T > &a, const Vec2< T > &b, const Vec2< T > &eps)
Definition: Vec2.h:494
static Vec2< T > zero()
Predefined constants, e.g. Vec2f v = Vec2f::xNegAxis();.
Definition: Vec2.h:388
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
Vec2(const Tuple< 2, Source > &t)
Conversion constructor.
Definition: Vec2.h:78
Vec2(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: Vec2.h:88
T mm[SIZE]
Definition: Tuple.h:164
T angle(const Vec2< T > &v1, const Vec2< T > &v2)
Definition: Vec2.h:480
const Vec2< T > & operator+=(const Vec2< S > &v1)
Returns v0, where for .
Definition: Vec2.h:323
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:101
Vec2< T > projection(const Vec2< T > &onto, T eps=1.0e-8) const
Definition: Vec2.h:365
bool eq(const Vec2< 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: Vec2.h:147
Vec2< T > Exp(Vec2< T > v)
Return a vector with the exponent applied to each of the components of the input vector.
Definition: Vec2.h:572
Vec2< float > Vec2s
Definition: Vec2.h:581
Vec2< typename promote< T0, T1 >::type > operator*(const Vec2< T0 > &v0, const Vec2< T1 > &v1)
Returns V, where for .
Definition: Vec2.h:411
const Vec2< T > & exp()
Definition: Vec2.h:213
Vec2< T > getArbPerpendicular() const
Definition: Vec2.h:376
bool operator==(const Vec2< T > &v) const
Equality operator, does exact floating point comparisons.
Definition: Vec2.h:138
T & x()
Reference to the component, e.g. v.x() = 4.5f;.
Definition: Vec2.h:95
T & operator()(int i)
Alternative indexed reference to the elements.
Definition: Vec2.h:103
static Vec2< T > ones()
Definition: Vec2.h:389
T sum() const
Return the sum of all the vector components.
Definition: Vec2.h:230
Vec2(T val)
Construct a vector all of whose components have the given value.
Definition: Vec2.h:59
const Vec2< T > & init(T x=0, T y=0)
Definition: Vec2.h:113
bool isFinite(const Vec2< T > &v)
Definition: Vec2.h:502
MatType unit(const MatType &mat, typename MatType::value_type eps=1.0e-8)
Return a copy of the given matrix with its upper 3x3 rows normalized.
Definition: Mat.h:643
static unsigned numColumns()
Definition: Vec2.h:350
void orthonormalize(Vec2< T > &v1, Vec2< T > &v2)
Definition: Vec2.h:525
const Vec2< T > & operator+=(S scalar)
Returns v, where for .
Definition: Vec2.h:314
T x() const
Get the component, e.g. float f = v.y();.
Definition: Vec2.h:99
Vec2< T > unit(T eps=0) const
return normalized this, throws if null vector
Definition: Vec2.h:253
T length() const
Length of the vector.
Definition: Vec2.h:202
Definition: Tuple.h:53
Vec2< T > maxComponent(const Vec2< T > &v1, const Vec2< T > &v2)
Return component-wise maximum of the two vectors.
Definition: Vec2.h:562
Vec2()
Trivial constructor, the vector is NOT initialized.
Definition: Vec2.h:56
Vec2< typename promote< S, T >::type > operator-(const Vec2< T > &v, S scalar)
Returns V, where for .
Definition: Vec2.h:470
const boost::disable_if_c< VecTraits< T >::IsVec, T >::type & min(const T &a, const T &b)
Definition: Composite.h:128
#define OPENVDB_VERSION_NAME
Definition: version.h:43
T dot(const Vec2< T > &v) const
Dot product.
Definition: Vec2.h:199
Dummy class for tag dispatch of conversion constructors.
Definition: Tuple.h:47
const Vec2< T > & setZero()
Set "this" vector to zero.
Definition: Vec2.h:120
static unsigned numElements()
Definition: Vec2.h:351
Vec2(T x, T y)
Constructor with two arguments, e.g. Vec2f v(1,2,3);.
Definition: Vec2.h:62
bool operator!=(const Vec2< T > &v) const
Inequality operator, does exact floating point comparisons.
Definition: Vec2.h:144
Vec2< T > unit(T eps, T &len) const
return normalized this and length, throws if null vector
Definition: Vec2.h:260
bool isInfinite() const
True if an Inf is present in vector.
Definition: Vec2.h:382
bool isNan() const
True if a Nan is present in vector.
Definition: Vec2.h:379
T value_type
Definition: Vec2.h:52
Definition: Exceptions.h:39
T component(const Vec2< T > &onto, T eps=1.0e-8) const
Definition: Vec2.h:355
Definition: Vec2.h:49
T lengthSqr() const
Definition: Vec2.h:209
const boost::disable_if_c< VecTraits< T >::IsVec, T >::type & max(const T &a, const T &b)
Definition: Composite.h:132
const Vec2< T > & operator/=(S scalar)
Returns v, where for .
Definition: Vec2.h:296
const Vec2< T > & log()
Definition: Vec2.h:222
T product() const
Return the product of all the vector components.
Definition: Vec2.h:236
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
const Vec2< T > & sub(const Vec2< T0 > &v1, const Vec2< T1 > &v2)
Definition: Vec2.h:170
Definition: Exceptions.h:82
T ValueType
Definition: Vec2.h:53
Vec2< T > operator-() const
Negation operator, for e.g. v1 = -v2;.
Definition: Vec2.h:154
bool isExactlyEqual(const T0 &a, const T1 &b)
Return true if a is exactly equal to b.
Definition: Math.h:407
const Vec2< T > & operator*=(const Vec2< S > &v1)
Returns v0, where for .
Definition: Vec2.h:287
bool isZero(const Vec2< T > &v)
Return true if all components are exactly equal to zero.
Definition: Vec2.h:510
const Vec2< T > & scale(T0 scalar, const Vec2< T1 > &v)
Definition: Vec2.h:181
T y() const
Definition: Vec2.h:100
Vec2< typename promote< S, T >::type > operator+(const Vec2< T > &v, S scalar)
Returns V, where for .
Definition: Vec2.h:452
Vec2< T > Abs(const Vec2< T > &v)
Definition: Vec2.h:517
const Vec2< T > & operator*=(S scalar)
Returns v, where for .
Definition: Vec2.h:278
const Vec2< T > & operator-=(const Vec2< S > &v1)
Returns v0, where for .
Definition: Vec2.h:341
Vec2< int32_t > Vec2i
Definition: Vec2.h:579
Vec2< T > unitSafe() const
return normalized this, or (1, 0) if this is null vector
Definition: Vec2.h:270
Vec2< typename promote< T0, T1 >::type > operator/(const Vec2< T0 > &v0, const Vec2< T1 > &v1)
Returns V, where for .
Definition: Vec2.h:435
const T * asPointer() const
Definition: Vec2.h:109
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71
Vec2< uint32_t > Vec2ui
Definition: Vec2.h:580
const Vec2< T > & operator/=(const Vec2< S > &v1)
Returns v0, where for .
Definition: Vec2.h:305
Vec2< T > minComponent(const Vec2< T > &v1, const Vec2< T > &v2)
Return component-wise minimum of the two vectors.
Definition: Vec2.h:553
T operator()(int i) const
Alternative indexed constant reference to the elements,.
Definition: Vec2.h:106
T & y()
Definition: Vec2.h:96
Definition: Vec2.h:46
bool normalize(T eps=1.0e-8)
this = normalized this
Definition: Vec2.h:242
bool isFinite() const
True if all no Nan or Inf values present.
Definition: Vec2.h:385
static unsigned numRows()
Definition: Vec2.h:349
Vec2(Source *a)
Constructor with array argument, e.g. float a[2]; Vec2f v(a);.
Definition: Vec2.h:70
const Vec2< T > & add(const Vec2< T0 > &v1, const Vec2< T1 > &v2)
Definition: Vec2.h:159