OpenVDB  3.1.0
Vec2.h
Go to the documentation of this file.
1 //
3 // Copyright (c) 2012-2015 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 <cmath>
35 #include <openvdb/Exceptions.h>
36 #include "Math.h"
37 #include "Tuple.h"
38 
39 
40 namespace openvdb {
42 namespace OPENVDB_VERSION_NAME {
43 namespace math {
44 
45 template<typename T> class Mat2;
46 
47 template<typename T>
48 class Vec2: public Tuple<2, T>
49 {
50 public:
51  typedef T value_type;
52  typedef T ValueType;
53 
55  Vec2() {}
56 
58  explicit Vec2(T val) { this->mm[0] = this->mm[1] = val; }
59 
61  Vec2(T x, T y)
62  {
63  this->mm[0] = x;
64  this->mm[1] = y;
65  }
66 
68  template <typename Source>
69  Vec2(Source *a)
70  {
71  this->mm[0] = a[0];
72  this->mm[1] = a[1];
73  } // trivial
74 
76  template<typename Source>
77  explicit Vec2(const Tuple<2, Source> &t)
78  {
79  this->mm[0] = static_cast<T>(t[0]);
80  this->mm[1] = static_cast<T>(t[1]);
81  }
82 
84  T& x() {return this->mm[0];}
85  T& y() {return this->mm[1];}
86 
88  T x() const {return this->mm[0];}
89  T y() const {return this->mm[1];}
90 
92  T& operator()(int i) {return this->mm[i];}
93 
95  T operator()(int i) const {return this->mm[i];}
96 
97  T* asPointer() {return this->mm;}
98  const T* asPointer() const {return this->mm;}
99 
102  const Vec2<T>& init(T x=0, T y=0)
103  {
104  this->mm[0] = x; this->mm[1] = y;
105  return *this;
106  }
107 
109  const Vec2<T>& setZero()
110  {
111  this->mm[0] = 0; this->mm[1] = 0;
112  return *this;
113  }
114 
116  template<typename Source>
117  const Vec2<T>& operator=(const Vec2<Source> &v)
118  {
119  // note: don't static_cast because that suppresses warnings
120  this->mm[0] = v[0];
121  this->mm[1] = v[1];
122 
123  return *this;
124  }
125 
127  bool operator==(const Vec2<T> &v) const
128  {
129  return (isExactlyEqual(this->mm[0], v.mm[0]) && isExactlyEqual(this->mm[1], v.mm[1]));
130  }
131 
133  bool operator!=(const Vec2<T> &v) const { return !(*this==v); }
134 
136  bool eq(const Vec2<T> &v, T eps = static_cast<T>(1.0e-7)) const
137  {
138  return isApproxEqual(this->mm[0], v.mm[0], eps) &&
139  isApproxEqual(this->mm[1], v.mm[1], eps);
140  } // trivial
141 
143  Vec2<T> operator-() const {return Vec2<T>(-this->mm[0], -this->mm[1]);}
144 
147  template <typename T0, typename T1>
148  const Vec2<T>& add(const Vec2<T0> &v1, const Vec2<T1> &v2)
149  {
150  this->mm[0] = v1[0] + v2[0];
151  this->mm[1] = v1[1] + v2[1];
152 
153  return *this;
154  }
155 
158  template <typename T0, typename T1>
159  const Vec2<T>& sub(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>& scale(T0 scalar, const Vec2<T1> &v)
171  {
172  this->mm[0] = scalar * v[0];
173  this->mm[1] = scalar * v[1];
174 
175  return *this;
176  }
177 
178  template <typename T0, typename T1>
179  const Vec2<T> &div(T0 scalar, const Vec2<T1> &v)
180  {
181  this->mm[0] = v[0] / scalar;
182  this->mm[1] = v[1] / scalar;
183 
184  return *this;
185  }
186 
188  T dot(const Vec2<T> &v) const { return this->mm[0]*v[0] + this->mm[1]*v[1]; } // trivial
189 
191  T length() const
192  {
193  return static_cast<T>(sqrt(double(this->mm[0]*this->mm[0] + this->mm[1]*this->mm[1])));
194  }
195 
198  T lengthSqr() const { return (this->mm[0]*this->mm[0] + this->mm[1]*this->mm[1]); }
199 
202  inline const Vec2<T>& exp()
203  {
204  this->mm[0] = std::exp(this->mm[0]);
205  this->mm[1] = std::exp(this->mm[1]);
206  return *this;
207  }
208 
210  inline T sum() const
211  {
212  return this->mm[0] + this->mm[1];
213  }
214 
216  bool normalize(T eps=1.0e-8)
217  {
218  T d = length();
219  if (isApproxEqual(d, T(0), eps)) {
220  return false;
221  }
222  *this *= (T(1) / d);
223  return true;
224  }
225 
227  Vec2<T> unit(T eps=0) const
228  {
229  T d;
230  return unit(eps, d);
231  }
232 
234  Vec2<T> unit(T eps, T& len) const
235  {
236  len = length();
237  if (isApproxEqual(len, T(0), eps)) {
238  OPENVDB_THROW(ArithmeticError, "Normalizing null 2-vector");
239  }
240  return *this / len;
241  }
242 
244  template <typename S>
245  const Vec2<T> &operator*=(S scalar)
246  {
247  this->mm[0] *= scalar;
248  this->mm[1] *= scalar;
249  return *this;
250  }
251 
253  template <typename S>
254  const Vec2<T> &operator*=(const Vec2<S> &v1)
255  {
256  this->mm[0] *= v1[0];
257  this->mm[1] *= v1[1];
258  return *this;
259  }
260 
262  template <typename S>
263  const Vec2<T> &operator/=(S scalar)
264  {
265  this->mm[0] /= scalar;
266  this->mm[1] /= scalar;
267  return *this;
268  }
269 
271  template <typename S>
272  const Vec2<T> &operator/=(const Vec2<S> &v1)
273  {
274  this->mm[0] /= v1[0];
275  this->mm[1] /= v1[1];
276  return *this;
277  }
278 
280  template <typename S>
281  const Vec2<T> &operator+=(S scalar)
282  {
283  this->mm[0] += scalar;
284  this->mm[1] += scalar;
285  return *this;
286  }
287 
289  template <typename S>
290  const Vec2<T> &operator+=(const Vec2<S> &v1)
291  {
292  this->mm[0] += v1[0];
293  this->mm[1] += v1[1];
294  return *this;
295  }
296 
298  template <typename S>
299  const Vec2<T> &operator-=(S scalar)
300  {
301  this->mm[0] -= scalar;
302  this->mm[1] -= scalar;
303  return *this;
304  }
305 
307  template <typename S>
308  const Vec2<T> &operator-=(const Vec2<S> &v1)
309  {
310  this->mm[0] -= v1[0];
311  this->mm[1] -= v1[1];
312  return *this;
313  }
314 
315  // Number of cols, rows, elements
316  static unsigned numRows() { return 1; }
317  static unsigned numColumns() { return 2; }
318  static unsigned numElements() { return 2; }
319 
322  T component(const Vec2<T> &onto, T eps=1.0e-8) const
323  {
324  T l = onto.length();
325  if (isApproxEqual(l, T(0), eps)) return 0;
326 
327  return dot(onto)*(T(1)/l);
328  }
329 
332  Vec2<T> projection(const Vec2<T> &onto, T eps=1.0e-8) const
333  {
334  T l = onto.lengthSqr();
335  if (isApproxEqual(l, T(0), eps)) return Vec2::zero();
336 
337  return onto*(dot(onto)*(T(1)/l));
338  }
339 
343  Vec2<T> getArbPerpendicular() const { return Vec2<T>(-this->mm[1], this->mm[0]); }
344 
346  bool isNan() const { return isnan(this->mm[0]) || isnan(this->mm[1]); }
347 
349  bool isInfinite() const { return isinf(this->mm[0]) || isinf(this->mm[1]); }
350 
352  bool isFinite() const { return finite(this->mm[0]) && finite(this->mm[1]); }
353 
355  static Vec2<T> zero() { return Vec2<T>(0, 0); }
356 };
357 
358 
360 template <typename S, typename T>
362 {
363  return v * scalar;
364 }
365 
367 template <typename S, typename T>
369 {
371  result *= scalar;
372  return result;
373 }
374 
376 template <typename T0, typename T1>
378 {
379  Vec2<typename promote<T0, T1>::type> result(v0[0] * v1[0], v0[1] * v1[1]);
380  return result;
381 }
382 
384 template <typename S, typename T>
386 {
387  return Vec2<typename promote<S, T>::type>(scalar/v[0], scalar/v[1]);
388 }
389 
391 template <typename S, typename T>
393 {
395  result /= scalar;
396  return result;
397 }
398 
400 template <typename T0, typename T1>
402 {
403  Vec2<typename promote<T0, T1>::type> result(v0[0] / v1[0], v0[1] / v1[1]);
404  return result;
405 }
406 
408 template <typename T0, typename T1>
410 {
412  result += v1;
413  return result;
414 }
415 
417 template <typename S, typename T>
419 {
421  result += scalar;
422  return result;
423 }
424 
426 template <typename T0, typename T1>
428 {
430  result -= v1;
431  return result;
432 }
433 
435 template <typename S, typename T>
437 {
439  result -= scalar;
440  return result;
441 }
442 
445 template <typename T>
446 inline T angle(const Vec2<T> &v1, const Vec2<T> &v2)
447 {
448  T c = v1.dot(v2);
449  return acos(c);
450 }
451 
452 template <typename T>
453 inline bool
454 isApproxEqual(const Vec2<T>& a, const Vec2<T>& b)
455 {
456  return a.eq(b);
457 }
458 template <typename T>
459 inline bool
460 isApproxEqual(const Vec2<T>& a, const Vec2<T>& b, const Vec2<T>& eps)
461 {
462  return isApproxEqual(a.x(), b.x(), eps.x()) &&
463  isApproxEqual(a.y(), b.y(), eps.y());
464 }
465 
466 template<typename T>
467 inline bool
468 isFinite(const Vec2<T>& v)
469 {
470  return isFinite(v[0]) && isFinite(v[1]);
471 }
472 
473 template<typename T>
474 inline Vec2<T>
475 Abs(const Vec2<T>& v)
476 {
477  return Vec2<T>(Abs(v[0]), Abs(v[1]));
478 }
479 
482 template <typename T>
483 inline void orthonormalize(Vec2<T> &v1, Vec2<T> &v2)
484 {
485  // If the input vectors are v0, v1, and v2, then the Gram-Schmidt
486  // orthonormalization produces vectors u0, u1, and u2 as follows,
487  //
488  // u0 = v0/|v0|
489  // u1 = (v1-(u0*v1)u0)/|v1-(u0*v1)u0|
490  //
491  // where |A| indicates length of vector A and A*B indicates dot
492  // product of vectors A and B.
493 
494  // compute u0
495  v1.normalize();
496 
497  // compute u1
498  T d0 = v1.dot(v2);
499  v2 -= v1*d0;
500  v2.normalize();
501 }
502 
503 
508 
510 template <typename T>
511 inline Vec2<T> minComponent(const Vec2<T> &v1, const Vec2<T> &v2)
512 {
513  return Vec2<T>(
514  std::min(v1.x(), v2.x()),
515  std::min(v1.y(), v2.y()));
516 }
517 
519 template <typename T>
520 inline Vec2<T> maxComponent(const Vec2<T> &v1, const Vec2<T> &v2)
521 {
522  return Vec2<T>(
523  std::max(v1.x(), v2.x()),
524  std::max(v1.y(), v2.y()));
525 }
526 
529 template <typename T>
530 inline Vec2<T> Exp(Vec2<T> v) { return v.exp(); }
531 
536 
537 } // namespace math
538 } // namespace OPENVDB_VERSION_NAME
539 } // namespace openvdb
540 
541 #endif // OPENVDB_MATH_VEC2_HAS_BEEN_INCLUDED
542 
543 // Copyright (c) 2012-2015 DreamWorks Animation LLC
544 // All rights reserved. This software is distributed under the
545 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
Vec2< typename promote< S, T >::type > operator-(const Vec2< T > &v, S scalar)
Returns V, where for .
Definition: Vec2.h:436
Definition: Vec2.h:48
Vec2< T > unit(T eps=0) const
return normalized this, throws if null vector
Definition: Vec2.h:227
Vec2< typename promote< S, T >::type > operator+(const Vec2< T > &v, S scalar)
Returns V, where for .
Definition: Vec2.h:418
const Vec2< T > & operator/=(S scalar)
Returns v, where for .
Definition: Vec2.h:263
Vec2< typename promote< T0, T1 >::type > operator/(const Vec2< T0 > &v0, const Vec2< T1 > &v1)
Returns V, where for .
Definition: Vec2.h:401
const Vec2< T > & operator*=(const Vec2< S > &v1)
Returns v0, where for .
Definition: Vec2.h:254
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
const Vec2< T > & setZero()
Set "this" vector to zero.
Definition: Vec2.h:109
bool isFinite(const Vec2< T > &v)
Definition: Vec2.h:468
const Vec2< T > & operator*=(S scalar)
Returns v, where for .
Definition: Vec2.h:245
T x() const
Get the component, e.g. float f = v.y();.
Definition: Vec2.h:88
bool isExactlyEqual(const T0 &a, const T1 &b)
Return true if a is exactly equal to b.
Definition: Math.h:407
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:97
const Vec2< T > & sub(const Vec2< T0 > &v1, const Vec2< T1 > &v2)
Definition: Vec2.h:159
Vec2< T > maxComponent(const Vec2< T > &v1, const Vec2< T > &v2)
Return component-wise maximum of the two vectors.
Definition: Vec2.h:520
Vec2< uint32_t > Vec2ui
Definition: Vec2.h:533
Vec2< int32_t > Vec2i
Definition: Vec2.h:532
T y() const
Definition: Vec2.h:89
T angle(const Vec2< T > &v1, const Vec2< T > &v2)
Definition: Vec2.h:446
const Vec2< T > & scale(T0 scalar, const Vec2< T1 > &v)
Definition: Vec2.h:170
const boost::disable_if_c< VecTraits< T >::IsVec, T >::type & min(const T &a, const T &b)
Definition: Composite.h:105
T ValueType
Definition: Vec2.h:52
const Vec2< T > & operator/=(const Vec2< S > &v1)
Returns v0, where for .
Definition: Vec2.h:272
bool isFinite() const
True if all no Nan or Inf values present.
Definition: Vec2.h:352
static unsigned numRows()
Definition: Vec2.h:316
const Vec2< T > & operator-=(const Vec2< S > &v1)
Returns v0, where for .
Definition: Vec2.h:308
Vec2< T > minComponent(const Vec2< T > &v1, const Vec2< T > &v2)
Return component-wise minimum of the two vectors.
Definition: Vec2.h:511
Definition: Tuple.h:50
const Vec2< T > & operator=(const Vec2< Source > &v)
Assignment operator.
Definition: Vec2.h:117
bool operator==(const Vec2< T > &v) const
Equality operator, does exact floating point comparisons.
Definition: Vec2.h:127
#define OPENVDB_VERSION_NAME
Definition: version.h:43
static Vec2< T > zero()
Predefined constants, e.g. Vec2f v = Vec2f::xNegAxis();.
Definition: Vec2.h:355
Vec2< T > getArbPerpendicular() const
Definition: Vec2.h:343
Vec2< typename promote< T0, T1 >::type > operator*(const Vec2< T0 > &v0, const Vec2< T1 > &v1)
Returns V, where for .
Definition: Vec2.h:377
Vec2< T > Abs(const Vec2< T > &v)
Definition: Vec2.h:475
Definition: Vec2.h:45
T * asPointer()
Definition: Vec2.h:97
Vec2(Source *a)
Constructor with array argument, e.g. float a[2]; Vec2f v(a);.
Definition: Vec2.h:69
void orthonormalize(Vec2< T > &v1, Vec2< T > &v2)
Definition: Vec2.h:483
Definition: Exceptions.h:39
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:136
T mm[SIZE]
Definition: Tuple.h:148
T & y()
Definition: Vec2.h:85
bool normalize(T eps=1.0e-8)
this = normalized this
Definition: Vec2.h:216
T dot(const Vec2< T > &v) const
Dot product.
Definition: Vec2.h:188
const Vec2< T > & operator+=(const Vec2< S > &v1)
Returns v0, where for .
Definition: Vec2.h:290
Vec2< T > operator-() const
Negation operator, for e.g. v1 = -v2;.
Definition: Vec2.h:143
T & x()
Reference to the component, e.g. v.x() = 4.5f;.
Definition: Vec2.h:84
const Vec2< T > & add(const Vec2< T0 > &v1, const Vec2< T1 > &v2)
Definition: Vec2.h:148
const Vec2< T > & operator-=(S scalar)
Returns v, where for .
Definition: Vec2.h:299
Vec2(const Tuple< 2, Source > &t)
Conversion constructor.
Definition: Vec2.h:77
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:627
const Vec2< T > & init(T x=0, T y=0)
Definition: Vec2.h:102
const Vec2< T > & div(T0 scalar, const Vec2< T1 > &v)
Definition: Vec2.h:179
T component(const Vec2< T > &onto, T eps=1.0e-8) const
Definition: Vec2.h:322
const Vec2< T > & operator+=(S scalar)
Returns v, where for .
Definition: Vec2.h:281
bool isNan() const
True if a Nan is present in vector.
Definition: Vec2.h:346
T lengthSqr() const
Definition: Vec2.h:198
Vec2< T > unit(T eps, T &len) const
return normalized this and length, throws if null vector
Definition: Vec2.h:234
bool operator!=(const Vec2< T > &v) const
Inequality operator, does exact floating point comparisons.
Definition: Vec2.h:133
const boost::disable_if_c< VecTraits< T >::IsVec, T >::type & max(const T &a, const T &b)
Definition: Composite.h:109
Vec2(T val)
Constructor with one argument, e.g. Vec2f v(0);.
Definition: Vec2.h:58
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:530
T operator()(int i) const
Alternative indexed constant reference to the elements,.
Definition: Vec2.h:95
const Vec2< T > & exp()
Definition: Vec2.h:202
T sum() const
Return the sum of all the vector components.
Definition: Vec2.h:210
Definition: Exceptions.h:78
bool isInfinite() const
True if an Inf is present in vector.
Definition: Vec2.h:349
T & operator()(int i)
Alternative indexed reference to the elements.
Definition: Vec2.h:92
Vec2< float > Vec2s
Definition: Vec2.h:534
Vec2< T > projection(const Vec2< T > &onto, T eps=1.0e-8) const
Definition: Vec2.h:332
static unsigned numElements()
Definition: Vec2.h:318
Vec2(T x, T y)
Constructor with two arguments, e.g. Vec2f v(1,2,3);.
Definition: Vec2.h:61
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71
Vec2()
Trivial constructor, the vector is NOT initialized.
Definition: Vec2.h:55
bool isApproxEqual(const Vec2< T > &a, const Vec2< T > &b, const Vec2< T > &eps)
Definition: Vec2.h:460
T length() const
Length of the vector.
Definition: Vec2.h:191
const T * asPointer() const
Definition: Vec2.h:98
Vec2< double > Vec2d
Definition: Vec2.h:535
T value_type
Definition: Vec2.h:51
static unsigned numColumns()
Definition: Vec2.h:317