OpenVDB  3.1.0
Vec4.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_VEC4_HAS_BEEN_INCLUDED
32 #define OPENVDB_MATH_VEC4_HAS_BEEN_INCLUDED
33 
34 #include <cmath>
35 #include <openvdb/Exceptions.h>
36 #include "Math.h"
37 #include "Tuple.h"
38 #include "Vec3.h"
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 Vec4: public Tuple<4, T>
50 {
51 public:
52  typedef T value_type;
53  typedef T ValueType;
54 
56  Vec4() {}
57 
59  explicit Vec4(T val) { this->mm[0] = this->mm[1] = this->mm[2] = this->mm[3] = val; }
60 
62  Vec4(T x, T y, T z, T w)
63  {
64  this->mm[0] = x;
65  this->mm[1] = y;
66  this->mm[2] = z;
67  this->mm[3] = w;
68  }
69 
71  template <typename Source>
72  Vec4(Source *a)
73  {
74  this->mm[0] = a[0];
75  this->mm[1] = a[1];
76  this->mm[2] = a[2];
77  this->mm[3] = a[3];
78  }
79 
81  template<typename Source>
82  explicit Vec4(const Tuple<4, Source> &v)
83  {
84  this->mm[0] = static_cast<T>(v[0]);
85  this->mm[1] = static_cast<T>(v[1]);
86  this->mm[2] = static_cast<T>(v[2]);
87  this->mm[3] = static_cast<T>(v[3]);
88  }
89 
91  T& x() { return this->mm[0]; }
92  T& y() { return this->mm[1]; }
93  T& z() { return this->mm[2]; }
94  T& w() { return this->mm[3]; }
95 
97  T x() const { return this->mm[0]; }
98  T y() const { return this->mm[1]; }
99  T z() const { return this->mm[2]; }
100  T w() const { return this->mm[3]; }
101 
102  T* asPointer() { return this->mm; }
103  const T* asPointer() const { return this->mm; }
104 
106  T& operator()(int i) { return this->mm[i]; }
107 
109  T operator()(int i) const { return this->mm[i]; }
110 
112  Vec3<T> getVec3() const { return Vec3<T>(this->mm[0], this->mm[1], this->mm[2]); }
113 
116  const Vec4<T>& init(T x=0, T y=0, T z=0, T w=0)
117  {
118  this->mm[0] = x; this->mm[1] = y; this->mm[2] = z; this->mm[3] = w;
119  return *this;
120  }
121 
123  const Vec4<T>& setZero()
124  {
125  this->mm[0] = 0; this->mm[1] = 0; this->mm[2] = 0; this->mm[3] = 0;
126  return *this;
127  }
128 
130  template<typename Source>
131  const Vec4<T>& operator=(const Vec4<Source> &v)
132  {
133  // note: don't static_cast because that suppresses warnings
134  this->mm[0] = v[0];
135  this->mm[1] = v[1];
136  this->mm[2] = v[2];
137  this->mm[3] = v[3];
138 
139  return *this;
140  }
141 
144  bool eq(const Vec4<T> &v, T eps=1.0e-8) const
145  {
146  return isApproxEqual(this->mm[0], v.mm[0], eps) &&
147  isApproxEqual(this->mm[1], v.mm[1], eps) &&
148  isApproxEqual(this->mm[2], v.mm[2], eps) &&
149  isApproxEqual(this->mm[3], v.mm[3], eps);
150  }
151 
154  {
155  return Vec4<T>(
156  -this->mm[0],
157  -this->mm[1],
158  -this->mm[2],
159  -this->mm[3]);
160  }
161 
164  template <typename T0, typename T1>
165  const Vec4<T>& add(const Vec4<T0> &v1, const Vec4<T1> &v2)
166  {
167  this->mm[0] = v1[0] + v2[0];
168  this->mm[1] = v1[1] + v2[1];
169  this->mm[2] = v1[2] + v2[2];
170  this->mm[3] = v1[3] + v2[3];
171 
172  return *this;
173  }
174 
175 
178  template <typename T0, typename T1>
179  const Vec4<T>& sub(const Vec4<T0> &v1, const Vec4<T1> &v2)
180  {
181  this->mm[0] = v1[0] - v2[0];
182  this->mm[1] = v1[1] - v2[1];
183  this->mm[2] = v1[2] - v2[2];
184  this->mm[3] = v1[3] - v2[3];
185 
186  return *this;
187  }
188 
191  template <typename T0, typename T1>
192  const Vec4<T>& scale(T0 scale, const Vec4<T1> &v)
193  {
194  this->mm[0] = scale * v[0];
195  this->mm[1] = scale * v[1];
196  this->mm[2] = scale * v[2];
197  this->mm[3] = scale * v[3];
198 
199  return *this;
200  }
201 
202  template <typename T0, typename T1>
203  const Vec4<T> &div(T0 scalar, const Vec4<T1> &v)
204  {
205  this->mm[0] = v[0] / scalar;
206  this->mm[1] = v[1] / scalar;
207  this->mm[2] = v[2] / scalar;
208  this->mm[3] = v[3] / scalar;
209 
210  return *this;
211  }
212 
214  T dot(const Vec4<T> &v) const
215  {
216  return (this->mm[0]*v.mm[0] + this->mm[1]*v.mm[1]
217  + this->mm[2]*v.mm[2] + this->mm[3]*v.mm[3]);
218  }
219 
221  T length() const
222  {
223  return sqrt(
224  this->mm[0]*this->mm[0] +
225  this->mm[1]*this->mm[1] +
226  this->mm[2]*this->mm[2] +
227  this->mm[3]*this->mm[3]);
228  }
229 
230 
233  T lengthSqr() const
234  {
235  return (this->mm[0]*this->mm[0] + this->mm[1]*this->mm[1]
236  + this->mm[2]*this->mm[2] + this->mm[3]*this->mm[3]);
237  }
238 
241  inline const Vec4<T>& exp()
242  {
243  this->mm[0] = std::exp(this->mm[0]);
244  this->mm[1] = std::exp(this->mm[1]);
245  this->mm[2] = std::exp(this->mm[2]);
246  this->mm[3] = std::exp(this->mm[3]);
247  return *this;
248  }
249 
251  inline T sum() const
252  {
253  return this->mm[0] + this->mm[1] + this->mm[2] + this->mm[3];
254  }
255 
256 
258  bool normalize(T eps=1.0e-8)
259  {
260  T d = length();
261  if (isApproxEqual(d, T(0), eps)) {
262  return false;
263  }
264  *this *= (T(1) / d);
265  return true;
266  }
267 
269  Vec4<T> unit(T eps=0) const
270  {
271  T d;
272  return unit(eps, d);
273  }
274 
276  Vec4<T> unit(T eps, T& len) const
277  {
278  len = length();
279  if (isApproxEqual(len, T(0), eps)) {
280  throw ArithmeticError("Normalizing null 4-vector");
281  }
282  return *this / len;
283  }
284 
286  template <typename S>
287  const Vec4<T> &operator*=(S scalar)
288  {
289  this->mm[0] *= scalar;
290  this->mm[1] *= scalar;
291  this->mm[2] *= scalar;
292  this->mm[3] *= scalar;
293  return *this;
294  }
295 
297  template <typename S>
298  const Vec4<T> &operator*=(const Vec4<S> &v1)
299  {
300  this->mm[0] *= v1[0];
301  this->mm[1] *= v1[1];
302  this->mm[2] *= v1[2];
303  this->mm[3] *= v1[3];
304 
305  return *this;
306  }
307 
309  template <typename S>
310  const Vec4<T> &operator/=(S scalar)
311  {
312  this->mm[0] /= scalar;
313  this->mm[1] /= scalar;
314  this->mm[2] /= scalar;
315  this->mm[3] /= scalar;
316  return *this;
317  }
318 
320  template <typename S>
321  const Vec4<T> &operator/=(const Vec4<S> &v1)
322  {
323  this->mm[0] /= v1[0];
324  this->mm[1] /= v1[1];
325  this->mm[2] /= v1[2];
326  this->mm[3] /= v1[3];
327  return *this;
328  }
329 
331  template <typename S>
332  const Vec4<T> &operator+=(S scalar)
333  {
334  this->mm[0] += scalar;
335  this->mm[1] += scalar;
336  this->mm[2] += scalar;
337  this->mm[3] += scalar;
338  return *this;
339  }
340 
342  template <typename S>
343  const Vec4<T> &operator+=(const Vec4<S> &v1)
344  {
345  this->mm[0] += v1[0];
346  this->mm[1] += v1[1];
347  this->mm[2] += v1[2];
348  this->mm[3] += v1[3];
349  return *this;
350  }
351 
353  template <typename S>
354  const Vec4<T> &operator-=(S scalar)
355  {
356  this->mm[0] -= scalar;
357  this->mm[1] -= scalar;
358  this->mm[2] -= scalar;
359  this->mm[3] -= scalar;
360  return *this;
361  }
362 
364  template <typename S>
365  const Vec4<T> &operator-=(const Vec4<S> &v1)
366  {
367  this->mm[0] -= v1[0];
368  this->mm[1] -= v1[1];
369  this->mm[2] -= v1[2];
370  this->mm[3] -= v1[3];
371  return *this;
372  }
373 
374  // Number of cols, rows, elements
375  static unsigned numRows() { return 1; }
376  static unsigned numColumns() { return 4; }
377  static unsigned numElements() { return 4; }
378 
380  bool isNan() const
381  {
382  return isnan(this->mm[0]) || isnan(this->mm[1])
383  || isnan(this->mm[2]) || isnan(this->mm[3]);
384  }
385 
387  bool isInfinite() const
388  {
389  return isinf(this->mm[0]) || isinf(this->mm[1])
390  || isinf(this->mm[2]) || isinf(this->mm[3]);
391  }
392 
394  bool isFinite() const
395  {
396  return finite(this->mm[0]) && finite(this->mm[1])
397  && finite(this->mm[2]) && finite(this->mm[3]);
398  }
399 
401  static Vec4<T> zero() { return Vec4<T>(0, 0, 0, 0); }
402  static Vec4<T> origin() { return Vec4<T>(0, 0, 0, 1); }
403 };
404 
406 template <typename T0, typename T1>
407 inline bool operator==(const Vec4<T0> &v0, const Vec4<T1> &v1)
408 {
409  return
410  isExactlyEqual(v0[0], v1[0]) &&
411  isExactlyEqual(v0[1], v1[1]) &&
412  isExactlyEqual(v0[2], v1[2]) &&
413  isExactlyEqual(v0[3], v1[3]);
414 }
415 
417 template <typename T0, typename T1>
418 inline bool operator!=(const Vec4<T0> &v0, const Vec4<T1> &v1) { return !(v0==v1); }
419 
421 template <typename S, typename T>
423 { return v*scalar; }
424 
426 template <typename S, typename T>
428 {
430  result *= scalar;
431  return result;
432 }
433 
435 template <typename T0, typename T1>
437  const Vec4<T1> &v1)
438 {
439  Vec4<typename promote<T0, T1>::type> result(v0[0]*v1[0],
440  v0[1]*v1[1],
441  v0[2]*v1[2],
442  v0[3]*v1[3]);
443  return result;
444 }
445 
447 template <typename S, typename T>
449 {
450  return Vec4<typename promote<S, T>::type>(scalar/v[0],
451  scalar/v[1],
452  scalar/v[2],
453  scalar/v[3]);
454 }
455 
457 template <typename S, typename T>
459 {
461  result /= scalar;
462  return result;
463 }
464 
466 template <typename T0, typename T1>
468  const Vec4<T1> &v1)
469 {
471  result(v0[0]/v1[0], v0[1]/v1[1], v0[2]/v1[2], v0[3]/v1[3]);
472  return result;
473 }
474 
476 template <typename T0, typename T1>
478 {
480  result += v1;
481  return result;
482 }
483 
485 template <typename S, typename T>
487 {
489  result += scalar;
490  return result;
491 }
492 
494 template <typename T0, typename T1>
496 {
498  result -= v1;
499  return result;
500 }
501 
503 template <typename S, typename T>
505 {
507  result -= scalar;
508  return result;
509 }
510 
511 template <typename T>
512 inline bool
513 isApproxEqual(const Vec4<T>& a, const Vec4<T>& b)
514 {
515  return a.eq(b);
516 }
517 template <typename T>
518 inline bool
519 isApproxEqual(const Vec4<T>& a, const Vec4<T>& b, const Vec4<T>& eps)
520 {
521  return isApproxEqual(a[0], b[0], eps[0]) &&
522  isApproxEqual(a[1], b[1], eps[1]) &&
523  isApproxEqual(a[2], b[2], eps[2]) &&
524  isApproxEqual(a[3], b[3], eps[3]);
525 }
526 
527 template<typename T>
528 inline bool
529 isFinite(const Vec4<T>& v)
530 {
531  return isFinite(v[0]) && isFinite(v[1]) && isFinite(v[2]) && isFinite(v[3]);
532 }
533 
534 template<typename T>
535 inline Vec4<T>
536 Abs(const Vec4<T>& v)
537 {
538  return Vec4<T>(Abs(v[0]), Abs(v[1]), Abs(v[2]), Abs(v[3]));
539 }
540 
545 
547 template <typename T>
548 inline Vec4<T> minComponent(const Vec4<T> &v1, const Vec4<T> &v2)
549 {
550  return Vec4<T>(
551  std::min(v1.x(), v2.x()),
552  std::min(v1.y(), v2.y()),
553  std::min(v1.z(), v2.z()),
554  std::min(v1.w(), v2.w()));
555 }
556 
558 template <typename T>
559 inline Vec4<T> maxComponent(const Vec4<T> &v1, const Vec4<T> &v2)
560 {
561  return Vec4<T>(
562  std::max(v1.x(), v2.x()),
563  std::max(v1.y(), v2.y()),
564  std::max(v1.z(), v2.z()),
565  std::max(v1.w(), v2.w()));
566 }
567 
570 template <typename T>
571 inline Vec4<T> Exp(Vec4<T> v) { return v.exp(); }
572 
577 
578 } // namespace math
579 } // namespace OPENVDB_VERSION_NAME
580 } // namespace openvdb
581 
582 #endif // OPENVDB_MATH_VEC4_HAS_BEEN_INCLUDED
583 
584 // Copyright (c) 2012-2015 DreamWorks Animation LLC
585 // All rights reserved. This software is distributed under the
586 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
Vec4(T val)
Constructor with one argument, e.g. Vec4f v(0);.
Definition: Vec4.h:59
Vec4< T > maxComponent(const Vec4< T > &v1, const Vec4< T > &v2)
Return component-wise maximum of the two vectors.
Definition: Vec4.h:559
T length() const
Length of the vector.
Definition: Vec4.h:221
const T * asPointer() const
Definition: Vec4.h:103
const Vec4< T > & init(T x=0, T y=0, T z=0, T w=0)
Definition: Vec4.h:116
Vec4(Source *a)
Constructor with array argument, e.g. float a[4]; Vec4f v(a);.
Definition: Vec4.h:72
T * asPointer()
Definition: Vec4.h:102
Vec4< typename promote< S, T >::type > operator-(const Vec4< T > &v, S scalar)
Returns V, where for .
Definition: Vec4.h:504
const Vec4< T > & operator*=(S scalar)
Returns v, where for .
Definition: Vec4.h:287
Vec4()
Trivial constructor, the vector is NOT initialized.
Definition: Vec4.h:56
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
bool eq(const Vec4< T > &v, T eps=1.0e-8) const
Definition: Vec4.h:144
const Vec4< T > & sub(const Vec4< T0 > &v1, const Vec4< T1 > &v2)
Definition: Vec4.h:179
bool isExactlyEqual(const T0 &a, const T1 &b)
Return true if a is exactly equal to b.
Definition: Math.h:407
Vec4< double > Vec4d
Definition: Vec4.h:576
bool isNan() const
True if a Nan is present in vector.
Definition: Vec4.h:380
Vec3< T > getVec3() const
Returns a Vec3 with the first three elements of the Vec4.
Definition: Vec4.h:112
T & x()
Reference to the component, e.g. v.x() = 4.5f;.
Definition: Vec4.h:91
static Vec4< T > zero()
Predefined constants, e.g. Vec4f v = Vec4f::xNegAxis();.
Definition: Vec4.h:401
T y() const
Definition: Vec4.h:98
Vec4< int32_t > Vec4i
Definition: Vec4.h:573
const Vec4< T > & operator+=(S scalar)
Returns v, where for .
Definition: Vec4.h:332
T z() const
Definition: Vec4.h:99
Vec4< typename promote< T0, T1 >::type > operator/(const Vec4< T0 > &v0, const Vec4< T1 > &v1)
Returns V, where for .
Definition: Vec4.h:467
bool operator==(const Vec4< T0 > &v0, const Vec4< T1 > &v1)
Equality operator, does exact floating point comparisons.
Definition: Vec4.h:407
const Vec4< T > & operator*=(const Vec4< S > &v1)
Returns v0, where for .
Definition: Vec4.h:298
Definition: Mat4.h:51
T operator()(int i) const
Alternative indexed constant reference to the elements,.
Definition: Vec4.h:109
const boost::disable_if_c< VecTraits< T >::IsVec, T >::type & min(const T &a, const T &b)
Definition: Composite.h:105
const Vec4< T > & exp()
Definition: Vec4.h:241
static Vec4< T > origin()
Definition: Vec4.h:402
Vec4< typename promote< T0, T1 >::type > operator*(const Vec4< T0 > &v0, const Vec4< T1 > &v1)
Returns V, where for .
Definition: Vec4.h:436
bool operator!=(const Vec4< T0 > &v0, const Vec4< T1 > &v1)
Inequality operator, does exact floating point comparisons.
Definition: Vec4.h:418
T & operator()(int i)
Alternative indexed reference to the elements.
Definition: Vec4.h:106
Vec4< T > Abs(const Vec4< T > &v)
Definition: Vec4.h:536
Definition: Tuple.h:50
bool isFinite(const Vec4< T > &v)
Definition: Vec4.h:529
#define OPENVDB_VERSION_NAME
Definition: version.h:43
T value_type
Definition: Vec4.h:52
static unsigned numColumns()
Definition: Vec4.h:376
const Vec4< T > & operator-=(S scalar)
Returns v, where for .
Definition: Vec4.h:354
const Vec4< T > & operator=(const Vec4< Source > &v)
Assignment operator.
Definition: Vec4.h:131
Vec4< uint32_t > Vec4ui
Definition: Vec4.h:574
Definition: Mat.h:146
Vec4(T x, T y, T z, T w)
Constructor with four arguments, e.g. Vec4f v(1,2,3,4);.
Definition: Vec4.h:62
const Vec4< T > & operator/=(S scalar)
Returns v, where for .
Definition: Vec4.h:310
const Vec4< T > & operator/=(const Vec4< S > &v1)
Returns v0, where for .
Definition: Vec4.h:321
Vec4< float > Vec4s
Definition: Vec4.h:575
Definition: Exceptions.h:39
T w() const
Definition: Vec4.h:100
const Vec4< T > & setZero()
Set "this" vector to zero.
Definition: Vec4.h:123
const Vec4< T > & operator-=(const Vec4< S > &v1)
Returns v0, where for .
Definition: Vec4.h:365
Vec4< typename promote< S, T >::type > operator+(const Vec4< T > &v, S scalar)
Returns V, where for .
Definition: Vec4.h:486
T & w()
Definition: Vec4.h:94
Vec4< T > unit(T eps, T &len) const
return normalized this and length, throws if null vector
Definition: Vec4.h:276
const Vec4< T > & scale(T0 scale, const Vec4< T1 > &v)
Definition: Vec4.h:192
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
T & y()
Definition: Vec4.h:92
Vec4< T > unit(T eps=0) const
return normalized this, throws if null vector
Definition: Vec4.h:269
Vec4(const Tuple< 4, Source > &v)
Conversion constructor.
Definition: Vec4.h:82
T x() const
Get the component, e.g. float f = v.y();.
Definition: Vec4.h:97
Vec4< T > minComponent(const Vec4< T > &v1, const Vec4< T > &v2)
Return component-wise minimum of the two vectors.
Definition: Vec4.h:548
const Vec4< T > & div(T0 scalar, const Vec4< T1 > &v)
Definition: Vec4.h:203
T sum() const
Return the sum of all the vector components.
Definition: Vec4.h:251
Vec4< T > Exp(Vec4< T > v)
Return a vector with the exponent applied to each of the components of the input vector.
Definition: Vec4.h:571
bool normalize(T eps=1.0e-8)
this = normalized this
Definition: Vec4.h:258
bool isInfinite() const
True if an Inf is present in vector.
Definition: Vec4.h:387
const Vec4< T > & add(const Vec4< T0 > &v1, const Vec4< T1 > &v2)
Definition: Vec4.h:165
const boost::disable_if_c< VecTraits< T >::IsVec, T >::type & max(const T &a, const T &b)
Definition: Composite.h:109
T ValueType
Definition: Vec4.h:53
bool isFinite() const
True if all no Nan or Inf values present.
Definition: Vec4.h:394
Definition: Exceptions.h:78
Vec4< T > operator-() const
Negation operator, for e.g. v1 = -v2;.
Definition: Vec4.h:153
T lengthSqr() const
Definition: Vec4.h:233
T dot(const Vec4< T > &v) const
Dot product.
Definition: Vec4.h:214
T & z()
Definition: Vec4.h:93
const Vec4< T > & operator+=(const Vec4< S > &v1)
Returns v0, where for .
Definition: Vec4.h:343
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71
MatType scale(const Vec3< typename MatType::value_type > &s)
Return a matrix that scales by s.
Definition: Mat.h:594
static unsigned numRows()
Definition: Vec4.h:375
bool isApproxEqual(const Vec4< T > &a, const Vec4< T > &b, const Vec4< T > &eps)
Definition: Vec4.h:519
static unsigned numElements()
Definition: Vec4.h:377