OpenVDB  4.0.2
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  static Vec2<T> zero() { return Vec2<T>(0, 0); }
380  static Vec2<T> ones() { return Vec2<T>(1, 1); }
381 };
382 
383 
385 template <typename S, typename T>
387 {
388  return v * scalar;
389 }
390 
392 template <typename S, typename T>
394 {
396  result *= scalar;
397  return result;
398 }
399 
401 template <typename T0, typename T1>
403 {
404  Vec2<typename promote<T0, T1>::type> result(v0[0] * v1[0], v0[1] * v1[1]);
405  return result;
406 }
407 
409 template <typename S, typename T>
411 {
412  return Vec2<typename promote<S, T>::type>(scalar/v[0], scalar/v[1]);
413 }
414 
416 template <typename S, typename T>
418 {
420  result /= scalar;
421  return result;
422 }
423 
425 template <typename T0, typename T1>
427 {
428  Vec2<typename promote<T0, T1>::type> result(v0[0] / v1[0], v0[1] / v1[1]);
429  return result;
430 }
431 
433 template <typename T0, typename T1>
435 {
437  result += v1;
438  return result;
439 }
440 
442 template <typename S, typename T>
444 {
446  result += scalar;
447  return result;
448 }
449 
451 template <typename T0, typename T1>
453 {
455  result -= v1;
456  return result;
457 }
458 
460 template <typename S, typename T>
462 {
464  result -= scalar;
465  return result;
466 }
467 
470 template <typename T>
471 inline T angle(const Vec2<T> &v1, const Vec2<T> &v2)
472 {
473  T c = v1.dot(v2);
474  return acos(c);
475 }
476 
477 template <typename T>
478 inline bool
479 isApproxEqual(const Vec2<T>& a, const Vec2<T>& b)
480 {
481  return a.eq(b);
482 }
483 template <typename T>
484 inline bool
485 isApproxEqual(const Vec2<T>& a, const Vec2<T>& b, const Vec2<T>& eps)
486 {
487  return isApproxEqual(a.x(), b.x(), eps.x()) &&
488  isApproxEqual(a.y(), b.y(), eps.y());
489 }
490 
491 template<typename T>
492 inline Vec2<T>
493 Abs(const Vec2<T>& v)
494 {
495  return Vec2<T>(Abs(v[0]), Abs(v[1]));
496 }
497 
500 template <typename T>
501 inline void orthonormalize(Vec2<T> &v1, Vec2<T> &v2)
502 {
503  // If the input vectors are v0, v1, and v2, then the Gram-Schmidt
504  // orthonormalization produces vectors u0, u1, and u2 as follows,
505  //
506  // u0 = v0/|v0|
507  // u1 = (v1-(u0*v1)u0)/|v1-(u0*v1)u0|
508  //
509  // where |A| indicates length of vector A and A*B indicates dot
510  // product of vectors A and B.
511 
512  // compute u0
513  v1.normalize();
514 
515  // compute u1
516  T d0 = v1.dot(v2);
517  v2 -= v1*d0;
518  v2.normalize();
519 }
520 
521 
526 
528 template <typename T>
529 inline Vec2<T> minComponent(const Vec2<T> &v1, const Vec2<T> &v2)
530 {
531  return Vec2<T>(
532  std::min(v1.x(), v2.x()),
533  std::min(v1.y(), v2.y()));
534 }
535 
537 template <typename T>
538 inline Vec2<T> maxComponent(const Vec2<T> &v1, const Vec2<T> &v2)
539 {
540  return Vec2<T>(
541  std::max(v1.x(), v2.x()),
542  std::max(v1.y(), v2.y()));
543 }
544 
547 template <typename T>
548 inline Vec2<T> Exp(Vec2<T> v) { return v.exp(); }
549 
552 template <typename T>
553 inline Vec2<T> Log(Vec2<T> v) { return v.log(); }
554 
559 
560 } // namespace math
561 } // namespace OPENVDB_VERSION_NAME
562 } // namespace openvdb
563 
564 #endif // OPENVDB_MATH_VEC2_HAS_BEEN_INCLUDED
565 
566 // Copyright (c) 2012-2017 DreamWorks Animation LLC
567 // All rights reserved. This software is distributed under the
568 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
Vec2< uint32_t > Vec2ui
Definition: Vec2.h:556
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
Dummy class for tag dispatch of conversion constructors.
Definition: Tuple.h:48
const Vec2< T > & operator=(const Vec2< Source > &v)
Assignment operator.
Definition: Vec2.h:128
T * asPointer()
Definition: Vec2.h:108
static Vec2< T > zero()
Predefined constants, e.g. Vec2f v = Vec2f::xNegAxis();.
Definition: Vec2.h:379
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
T angle(const Vec2< T > &v1, const Vec2< T > &v2)
Definition: Vec2.h:471
void orthonormalize(Vec2< T > &v1, Vec2< T > &v2)
Definition: Vec2.h:501
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
Vec2< typename promote< S, T >::type > operator-(const Vec2< T > &v, S scalar)
Returns V, where for .
Definition: Vec2.h:461
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< typename promote< T0, T1 >::type > operator*(const Vec2< T0 > &v0, const Vec2< T1 > &v1)
Returns V, where for .
Definition: Vec2.h:402
bool isExactlyEqual(const T0 &a, const T1 &b)
Return true if a is exactly equal to b.
Definition: Math.h:391
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
tbb::atomic< Index32 > i
Definition: LeafBuffer.h:71
Definition: Exceptions.h:82
const Vec2< T > & exp()
Definition: Vec2.h:213
T mm[SIZE]
Definition: Tuple.h:196
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
static Vec2< T > ones()
Definition: Vec2.h:380
T & operator()(int i)
Alternative indexed reference to the elements.
Definition: Vec2.h:103
Vec2(T val)
Construct a vector all of whose components have the given value.
Definition: Vec2.h:59
T sum() const
Return the sum of all the vector components.
Definition: Vec2.h:230
const Vec2< T > & init(T x=0, T y=0)
Definition: Vec2.h:113
Vec2< T > Log(Vec2< T > v)
Return a vector with log applied to each of the components of the input vector.
Definition: Vec2.h:553
static unsigned numColumns()
Definition: Vec2.h:350
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
const Vec2< T > & operator+=(S scalar)
Returns v, where for .
Definition: Vec2.h:314
T length() const
Length of the vector.
Definition: Vec2.h:202
const std::enable_if<!VecTraits< T >::IsVec, T >::type & max(const T &a, const T &b)
Definition: Composite.h:133
Vec2< float > Vec2s
Definition: Vec2.h:557
const std::enable_if<!VecTraits< T >::IsVec, T >::type & min(const T &a, const T &b)
Definition: Composite.h:129
Vec2< T > minComponent(const Vec2< T > &v1, const Vec2< T > &v2)
Return component-wise minimum of the two vectors.
Definition: Vec2.h:529
Vec2()
Trivial constructor, the vector is NOT initialized.
Definition: Vec2.h:56
bool isApproxEqual(const Vec2< T > &a, const Vec2< T > &b, const Vec2< T > &eps)
Definition: Vec2.h:485
#define OPENVDB_VERSION_NAME
Definition: version.h:43
T dot(const Vec2< T > &v) const
Dot product.
Definition: Vec2.h:199
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
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
T lengthSqr() const
Definition: Vec2.h:209
const Vec2< T > & operator/=(S scalar)
Returns v, where for .
Definition: Vec2.h:296
const Vec2< T > & log()
Definition: Vec2.h:222
Vec2< typename promote< S, T >::type > operator+(const Vec2< T > &v, S scalar)
Returns V, where for .
Definition: Vec2.h:443
T product() const
Return the product of all the vector components.
Definition: Vec2.h:236
const Vec2< T > & sub(const Vec2< T0 > &v1, const Vec2< T1 > &v2)
Definition: Vec2.h:170
Vec2< typename promote< T0, T1 >::type > operator/(const Vec2< T0 > &v0, const Vec2< T1 > &v1)
Returns V, where for .
Definition: Vec2.h:426
Vec2< double > Vec2d
Definition: Vec2.h:558
Vec2< T > maxComponent(const Vec2< T > &v1, const Vec2< T > &v2)
Return component-wise maximum of the two vectors.
Definition: Vec2.h:538
T ValueType
Definition: Vec2.h:53
Vec2< T > operator-() const
Negation operator, for e.g. v1 = -v2;.
Definition: Vec2.h:154
Definition: Vec2.h:46
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:548
const Vec2< T > & operator*=(const Vec2< S > &v1)
Returns v0, where for .
Definition: Vec2.h:287
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:676
const Vec2< T > & scale(T0 scalar, const Vec2< T1 > &v)
Definition: Vec2.h:181
T y() const
Definition: Vec2.h:100
Vec2< T > Abs(const Vec2< T > &v)
Definition: Vec2.h:493
Definition: Vec2.h:49
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< T > unitSafe() const
return normalized this, or (1, 0) if this is null vector
Definition: Vec2.h:270
const T * asPointer() const
Definition: Vec2.h:109
Definition: Tuple.h:54
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71
const Vec2< T > & operator/=(const Vec2< S > &v1)
Returns v0, where for .
Definition: Vec2.h:305
T operator()(int i) const
Alternative indexed constant reference to the elements,.
Definition: Vec2.h:106
T & y()
Definition: Vec2.h:96
Vec2< int32_t > Vec2i
Definition: Vec2.h:555
bool normalize(T eps=1.0e-8)
this = normalized this
Definition: Vec2.h:242
static unsigned numRows()
Definition: Vec2.h:349
const Vec2< T > & add(const Vec2< T0 > &v1, const Vec2< T1 > &v2)
Definition: Vec2.h:159
Vec2(Source *a)
Constructor with array argument, e.g. float a[2]; Vec2f v(a);.
Definition: Vec2.h:70