OpenVDB  7.0.0
Vec4.h
Go to the documentation of this file.
1 // Copyright Contributors to the OpenVDB Project
2 // SPDX-License-Identifier: MPL-2.0
3 
4 #ifndef OPENVDB_MATH_VEC4_HAS_BEEN_INCLUDED
5 #define OPENVDB_MATH_VEC4_HAS_BEEN_INCLUDED
6 
7 #include <openvdb/Exceptions.h>
8 #include "Math.h"
9 #include "Tuple.h"
10 #include "Vec3.h"
11 #include <algorithm>
12 #include <cmath>
13 #include <type_traits>
14 
15 
16 namespace openvdb {
18 namespace OPENVDB_VERSION_NAME {
19 namespace math {
20 
21 template<typename T> class Mat3;
22 
23 template<typename T>
24 class Vec4: public Tuple<4, T>
25 {
26 public:
27  using value_type = T;
28  using ValueType = T;
29 
31  Vec4() {}
32 
34  explicit Vec4(T val) { this->mm[0] = this->mm[1] = this->mm[2] = this->mm[3] = val; }
35 
37  Vec4(T x, T y, T z, T w)
38  {
39  this->mm[0] = x;
40  this->mm[1] = y;
41  this->mm[2] = z;
42  this->mm[3] = w;
43  }
44 
46  template <typename Source>
47  Vec4(Source *a)
48  {
49  this->mm[0] = static_cast<T>(a[0]);
50  this->mm[1] = static_cast<T>(a[1]);
51  this->mm[2] = static_cast<T>(a[2]);
52  this->mm[3] = static_cast<T>(a[3]);
53  }
54 
56  template<typename Source>
57  explicit Vec4(const Tuple<4, Source> &v)
58  {
59  this->mm[0] = static_cast<T>(v[0]);
60  this->mm[1] = static_cast<T>(v[1]);
61  this->mm[2] = static_cast<T>(v[2]);
62  this->mm[3] = static_cast<T>(v[3]);
63  }
64 
68  template<typename Other>
69  explicit Vec4(Other val,
70  typename std::enable_if<std::is_arithmetic<Other>::value, Conversion>::type = Conversion{})
71  {
72  this->mm[0] = this->mm[1] = this->mm[2] = this->mm[3] = static_cast<T>(val);
73  }
74 
76  T& x() { return this->mm[0]; }
77  T& y() { return this->mm[1]; }
78  T& z() { return this->mm[2]; }
79  T& w() { return this->mm[3]; }
80 
82  T x() const { return this->mm[0]; }
83  T y() const { return this->mm[1]; }
84  T z() const { return this->mm[2]; }
85  T w() const { return this->mm[3]; }
86 
87  T* asPointer() { return this->mm; }
88  const T* asPointer() const { return this->mm; }
89 
91  T& operator()(int i) { return this->mm[i]; }
92 
94  T operator()(int i) const { return this->mm[i]; }
95 
97  Vec3<T> getVec3() const { return Vec3<T>(this->mm[0], this->mm[1], this->mm[2]); }
98 
101  const Vec4<T>& init(T x=0, T y=0, T z=0, T w=0)
102  {
103  this->mm[0] = x; this->mm[1] = y; this->mm[2] = z; this->mm[3] = w;
104  return *this;
105  }
106 
108  const Vec4<T>& setZero()
109  {
110  this->mm[0] = 0; this->mm[1] = 0; this->mm[2] = 0; this->mm[3] = 0;
111  return *this;
112  }
113 
115  template<typename Source>
116  const Vec4<T>& operator=(const Vec4<Source> &v)
117  {
118  // note: don't static_cast because that suppresses warnings
119  this->mm[0] = v[0];
120  this->mm[1] = v[1];
121  this->mm[2] = v[2];
122  this->mm[3] = v[3];
123 
124  return *this;
125  }
126 
129  bool eq(const Vec4<T> &v, T eps = static_cast<T>(1.0e-8)) const
130  {
131  return isApproxEqual(this->mm[0], v.mm[0], eps) &&
132  isApproxEqual(this->mm[1], v.mm[1], eps) &&
133  isApproxEqual(this->mm[2], v.mm[2], eps) &&
134  isApproxEqual(this->mm[3], v.mm[3], eps);
135  }
136 
139  {
140  return Vec4<T>(
141  -this->mm[0],
142  -this->mm[1],
143  -this->mm[2],
144  -this->mm[3]);
145  }
146 
149  template <typename T0, typename T1>
150  const Vec4<T>& add(const Vec4<T0> &v1, const Vec4<T1> &v2)
151  {
152  this->mm[0] = v1[0] + v2[0];
153  this->mm[1] = v1[1] + v2[1];
154  this->mm[2] = v1[2] + v2[2];
155  this->mm[3] = v1[3] + v2[3];
156 
157  return *this;
158  }
159 
160 
163  template <typename T0, typename T1>
164  const Vec4<T>& sub(const Vec4<T0> &v1, const Vec4<T1> &v2)
165  {
166  this->mm[0] = v1[0] - v2[0];
167  this->mm[1] = v1[1] - v2[1];
168  this->mm[2] = v1[2] - v2[2];
169  this->mm[3] = v1[3] - v2[3];
170 
171  return *this;
172  }
173 
176  template <typename T0, typename T1>
177  const Vec4<T>& scale(T0 scale, const Vec4<T1> &v)
178  {
179  this->mm[0] = scale * v[0];
180  this->mm[1] = scale * v[1];
181  this->mm[2] = scale * v[2];
182  this->mm[3] = scale * v[3];
183 
184  return *this;
185  }
186 
187  template <typename T0, typename T1>
188  const Vec4<T> &div(T0 scalar, const Vec4<T1> &v)
189  {
190  this->mm[0] = v[0] / scalar;
191  this->mm[1] = v[1] / scalar;
192  this->mm[2] = v[2] / scalar;
193  this->mm[3] = v[3] / scalar;
194 
195  return *this;
196  }
197 
199  T dot(const Vec4<T> &v) const
200  {
201  return (this->mm[0]*v.mm[0] + this->mm[1]*v.mm[1]
202  + this->mm[2]*v.mm[2] + this->mm[3]*v.mm[3]);
203  }
204 
206  T length() const
207  {
208  return sqrt(
209  this->mm[0]*this->mm[0] +
210  this->mm[1]*this->mm[1] +
211  this->mm[2]*this->mm[2] +
212  this->mm[3]*this->mm[3]);
213  }
214 
215 
218  T lengthSqr() const
219  {
220  return (this->mm[0]*this->mm[0] + this->mm[1]*this->mm[1]
221  + this->mm[2]*this->mm[2] + this->mm[3]*this->mm[3]);
222  }
223 
226  inline const Vec4<T>& exp()
227  {
228  this->mm[0] = std::exp(this->mm[0]);
229  this->mm[1] = std::exp(this->mm[1]);
230  this->mm[2] = std::exp(this->mm[2]);
231  this->mm[3] = std::exp(this->mm[3]);
232  return *this;
233  }
234 
237  inline const Vec4<T>& log()
238  {
239  this->mm[0] = std::log(this->mm[0]);
240  this->mm[1] = std::log(this->mm[1]);
241  this->mm[2] = std::log(this->mm[2]);
242  this->mm[3] = std::log(this->mm[3]);
243  return *this;
244  }
245 
247  inline T sum() const
248  {
249  return this->mm[0] + this->mm[1] + this->mm[2] + this->mm[3];
250  }
251 
253  inline T product() const
254  {
255  return this->mm[0] * this->mm[1] * this->mm[2] * this->mm[3];
256  }
257 
259  bool normalize(T eps = static_cast<T>(1.0e-8))
260  {
261  T d = length();
262  if (isApproxEqual(d, T(0), eps)) {
263  return false;
264  }
265  *this *= (T(1) / d);
266  return true;
267  }
268 
270  Vec4<T> unit(T eps=0) const
271  {
272  T d;
273  return unit(eps, d);
274  }
275 
277  Vec4<T> unit(T eps, T& len) const
278  {
279  len = length();
280  if (isApproxEqual(len, T(0), eps)) {
281  throw ArithmeticError("Normalizing null 4-vector");
282  }
283  return *this / len;
284  }
285 
288  {
289  T l2 = lengthSqr();
290  return l2 ? *this / static_cast<T>(sqrt(l2)) : Vec4<T>(1, 0, 0, 0);
291  }
292 
294  template <typename S>
295  const Vec4<T> &operator*=(S scalar)
296  {
297  this->mm[0] *= scalar;
298  this->mm[1] *= scalar;
299  this->mm[2] *= scalar;
300  this->mm[3] *= scalar;
301  return *this;
302  }
303 
305  template <typename S>
306  const Vec4<T> &operator*=(const Vec4<S> &v1)
307  {
308  this->mm[0] *= v1[0];
309  this->mm[1] *= v1[1];
310  this->mm[2] *= v1[2];
311  this->mm[3] *= v1[3];
312 
313  return *this;
314  }
315 
317  template <typename S>
318  const Vec4<T> &operator/=(S scalar)
319  {
320  this->mm[0] /= scalar;
321  this->mm[1] /= scalar;
322  this->mm[2] /= scalar;
323  this->mm[3] /= scalar;
324  return *this;
325  }
326 
328  template <typename S>
329  const Vec4<T> &operator/=(const Vec4<S> &v1)
330  {
331  this->mm[0] /= v1[0];
332  this->mm[1] /= v1[1];
333  this->mm[2] /= v1[2];
334  this->mm[3] /= v1[3];
335  return *this;
336  }
337 
339  template <typename S>
340  const Vec4<T> &operator+=(S scalar)
341  {
342  this->mm[0] += scalar;
343  this->mm[1] += scalar;
344  this->mm[2] += scalar;
345  this->mm[3] += scalar;
346  return *this;
347  }
348 
350  template <typename S>
351  const Vec4<T> &operator+=(const Vec4<S> &v1)
352  {
353  this->mm[0] += v1[0];
354  this->mm[1] += v1[1];
355  this->mm[2] += v1[2];
356  this->mm[3] += v1[3];
357  return *this;
358  }
359 
361  template <typename S>
362  const Vec4<T> &operator-=(S scalar)
363  {
364  this->mm[0] -= scalar;
365  this->mm[1] -= scalar;
366  this->mm[2] -= scalar;
367  this->mm[3] -= scalar;
368  return *this;
369  }
370 
372  template <typename S>
373  const Vec4<T> &operator-=(const Vec4<S> &v1)
374  {
375  this->mm[0] -= v1[0];
376  this->mm[1] -= v1[1];
377  this->mm[2] -= v1[2];
378  this->mm[3] -= v1[3];
379  return *this;
380  }
381 
382  // Number of cols, rows, elements
383  static unsigned numRows() { return 1; }
384  static unsigned numColumns() { return 4; }
385  static unsigned numElements() { return 4; }
386 
388  static Vec4<T> zero() { return Vec4<T>(0, 0, 0, 0); }
389  static Vec4<T> origin() { return Vec4<T>(0, 0, 0, 1); }
390  static Vec4<T> ones() { return Vec4<T>(1, 1, 1, 1); }
391 };
392 
394 template <typename T0, typename T1>
395 inline bool operator==(const Vec4<T0> &v0, const Vec4<T1> &v1)
396 {
397  return
398  isExactlyEqual(v0[0], v1[0]) &&
399  isExactlyEqual(v0[1], v1[1]) &&
400  isExactlyEqual(v0[2], v1[2]) &&
401  isExactlyEqual(v0[3], v1[3]);
402 }
403 
405 template <typename T0, typename T1>
406 inline bool operator!=(const Vec4<T0> &v0, const Vec4<T1> &v1) { return !(v0==v1); }
407 
409 template <typename S, typename T>
411 { return v*scalar; }
412 
414 template <typename S, typename T>
416 {
418  result *= scalar;
419  return result;
420 }
421 
423 template <typename T0, typename T1>
425 {
426  Vec4<typename promote<T0, T1>::type> result(v0[0]*v1[0],
427  v0[1]*v1[1],
428  v0[2]*v1[2],
429  v0[3]*v1[3]);
430  return result;
431 }
432 
434 template <typename S, typename T>
436 {
437  return Vec4<typename promote<S, T>::type>(scalar/v[0],
438  scalar/v[1],
439  scalar/v[2],
440  scalar/v[3]);
441 }
442 
444 template <typename S, typename T>
446 {
448  result /= scalar;
449  return result;
450 }
451 
453 template <typename T0, typename T1>
455 {
457  result(v0[0]/v1[0], v0[1]/v1[1], v0[2]/v1[2], v0[3]/v1[3]);
458  return result;
459 }
460 
462 template <typename T0, typename T1>
464 {
466  result += v1;
467  return result;
468 }
469 
471 template <typename S, typename T>
473 {
475  result += scalar;
476  return result;
477 }
478 
480 template <typename T0, typename T1>
482 {
484  result -= v1;
485  return result;
486 }
487 
489 template <typename S, typename T>
491 {
493  result -= scalar;
494  return result;
495 }
496 
497 template <typename T>
498 inline bool
499 isApproxEqual(const Vec4<T>& a, const Vec4<T>& b)
500 {
501  return a.eq(b);
502 }
503 template <typename T>
504 inline bool
505 isApproxEqual(const Vec4<T>& a, const Vec4<T>& b, const Vec4<T>& eps)
506 {
507  return isApproxEqual(a[0], b[0], eps[0]) &&
508  isApproxEqual(a[1], b[1], eps[1]) &&
509  isApproxEqual(a[2], b[2], eps[2]) &&
510  isApproxEqual(a[3], b[3], eps[3]);
511 }
512 
513 template<typename T>
514 inline Vec4<T>
515 Abs(const Vec4<T>& v)
516 {
517  return Vec4<T>(Abs(v[0]), Abs(v[1]), Abs(v[2]), Abs(v[3]));
518 }
519 
524 
526 template <typename T>
527 inline Vec4<T> minComponent(const Vec4<T> &v1, const Vec4<T> &v2)
528 {
529  return Vec4<T>(
530  std::min(v1.x(), v2.x()),
531  std::min(v1.y(), v2.y()),
532  std::min(v1.z(), v2.z()),
533  std::min(v1.w(), v2.w()));
534 }
535 
537 template <typename T>
538 inline Vec4<T> maxComponent(const Vec4<T> &v1, const Vec4<T> &v2)
539 {
540  return Vec4<T>(
541  std::max(v1.x(), v2.x()),
542  std::max(v1.y(), v2.y()),
543  std::max(v1.z(), v2.z()),
544  std::max(v1.w(), v2.w()));
545 }
546 
549 template <typename T>
550 inline Vec4<T> Exp(Vec4<T> v) { return v.exp(); }
551 
554 template <typename T>
555 inline Vec4<T> Log(Vec4<T> v) { return v.log(); }
556 
561 
562 } // namespace math
563 } // namespace OPENVDB_VERSION_NAME
564 } // namespace openvdb
565 
566 #endif // OPENVDB_MATH_VEC4_HAS_BEEN_INCLUDED
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:550
Definition: Tuple.h:28
T lengthSqr() const
Definition: Vec4.h:218
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
const T * asPointer() const
Definition: Vec4.h:88
Vec4< typename promote< T0, T1 >::type > operator/(const Vec4< T0 > &v0, const Vec4< T1 > &v1)
Divide corresponding elements of v0 and v1 and return the result.
Definition: Vec4.h:454
const Vec4< T > & operator=(const Vec4< Source > &v)
Assignment operator.
Definition: Vec4.h:116
Vec4(Source *a)
Constructor with array argument, e.g. float a[4]; Vec4f v(a);.
Definition: Vec4.h:47
Vec4(T val)
Construct a vector all of whose components have the given value.
Definition: Vec4.h:34
T ValueType
Definition: Vec4.h:28
const Vec4< T > & exp()
Definition: Vec4.h:226
bool operator==(const Vec4< T0 > &v0, const Vec4< T1 > &v1)
Equality operator, does exact floating point comparisons.
Definition: Vec4.h:395
const Vec4< T > & operator-=(S scalar)
Subtract scalar from each element of this vector.
Definition: Vec4.h:362
T product() const
Return the product of all the vector components.
Definition: Vec4.h:253
static Vec4< T > origin()
Definition: Vec4.h:389
T operator()(int i) const
Alternative indexed constant reference to the elements,.
Definition: Vec4.h:94
const Vec4< T > & operator*=(S scalar)
Multiply each element of this vector by scalar.
Definition: Vec4.h:295
T & z()
Definition: Vec4.h:78
T sum() const
Return the sum of all the vector components.
Definition: Vec4.h:247
Vec4< T > unit(T eps, T &len) const
return normalized this and length, throws if null vector
Definition: Vec4.h:277
static Vec4< T > ones()
Definition: Vec4.h:390
const Vec4< T > & init(T x=0, T y=0, T z=0, T w=0)
Definition: Vec4.h:101
Vec3< T > getVec3() const
Returns a Vec3 with the first three elements of the Vec4.
Definition: Vec4.h:97
T w() const
Definition: Vec4.h:85
Vec4< typename promote< T0, T1 >::type > operator*(const Vec4< T0 > &v0, const Vec4< T1 > &v1)
Multiply corresponding elements of v0 and v1 and return the result.
Definition: Vec4.h:424
Vec4< T > Log(Vec4< T > v)
Return a vector with log applied to each of the components of the input vector.
Definition: Vec4.h:555
const Vec4< T > & scale(T0 scale, const Vec4< T1 > &v)
Definition: Vec4.h:177
const Vec4< T > & log()
Definition: Vec4.h:237
T & w()
Definition: Vec4.h:79
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:102
T dot(const Vec4< T > &v) const
Dot product.
Definition: Vec4.h:199
Dummy class for tag dispatch of conversion constructors.
Definition: Tuple.h:22
Vec4(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: Vec4.h:69
Vec4()
Trivial constructor, the vector is NOT initialized.
Definition: Vec4.h:31
Vec4(const Tuple< 4, Source > &v)
Conversion constructor.
Definition: Vec4.h:57
Vec4< typename promote< S, T >::type > operator-(const Vec4< T > &v, S scalar)
Subtract scalar from each element of the given vector and return the result.
Definition: Vec4.h:490
static Vec4< T > zero()
Predefined constants, e.g. Vec4f v = Vec4f::xNegAxis();.
Definition: Vec4.h:388
Definition: Mat4.h:24
Vec4< T > Abs(const Vec4< T > &v)
Definition: Vec4.h:515
Definition: Exceptions.h:13
Definition: Exceptions.h:56
const Vec4< T > & add(const Vec4< T0 > &v1, const Vec4< T1 > &v2)
Definition: Vec4.h:150
const Vec4< T > & sub(const Vec4< T0 > &v1, const Vec4< T1 > &v2)
Definition: Vec4.h:164
Vec4< T > unitSafe() const
return normalized this, or (1, 0, 0, 0) if this is null vector
Definition: Vec4.h:287
const Vec4< T > & operator*=(const Vec4< S > &v1)
Multiply each element of this vector by the corresponding element of the given vector.
Definition: Vec4.h:306
bool isApproxEqual(const Vec4< T > &a, const Vec4< T > &b, const Vec4< T > &eps)
Definition: Vec4.h:505
Vec4< typename promote< S, T >::type > operator+(const Vec4< T > &v, S scalar)
Add scalar to each element of the given vector and return the result.
Definition: Vec4.h:472
bool normalize(T eps=static_cast< T >(1.0e-8))
this = normalized this
Definition: Vec4.h:259
const Vec4< T > & div(T0 scalar, const Vec4< T1 > &v)
Definition: Vec4.h:188
const Vec4< T > & operator+=(S scalar)
Add scalar to each element of this vector.
Definition: Vec4.h:340
Vec4< T > maxComponent(const Vec4< T > &v1, const Vec4< T > &v2)
Return component-wise maximum of the two vectors.
Definition: Vec4.h:538
Vec4< T > unit(T eps=0) const
return normalized this, throws if null vector
Definition: Vec4.h:270
static unsigned numRows()
Definition: Vec4.h:383
const Vec4< T > & setZero()
Set "this" vector to zero.
Definition: Vec4.h:108
const Vec4< T > & operator-=(const Vec4< S > &v1)
Subtract each element of the given vector from the corresponding element of this vector.
Definition: Vec4.h:373
MatType unit(const MatType &mat, typename MatType::value_type eps=1.0e-8)
Return a copy of the given matrix with its upper 3×3 rows normalized.
Definition: Mat.h:653
T length() const
Length of the vector.
Definition: Vec4.h:206
static unsigned numElements()
Definition: Vec4.h:385
T & y()
Definition: Vec4.h:77
const Vec4< T > & operator/=(const Vec4< S > &v1)
Divide each element of this vector by the corresponding element of the given vector.
Definition: Vec4.h:329
T * asPointer()
Definition: Vec4.h:87
T z() const
Definition: Vec4.h:84
Vec4< T > operator-() const
Negation operator, for e.g. v1 = -v2;.
Definition: Vec4.h:138
Definition: Mat.h:170
const std::enable_if<!VecTraits< T >::IsVec, T >::type & min(const T &a, const T &b)
Definition: Composite.h:102
bool isExactlyEqual(const T0 &a, const T1 &b)
Return true if a is exactly equal to b.
Definition: Math.h:388
T & x()
Reference to the component, e.g. v.x() = 4.5f;.
Definition: Vec4.h:76
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:154
bool operator!=(const Vec4< T0 > &v0, const Vec4< T1 > &v1)
Inequality operator, does exact floating point comparisons.
Definition: Vec4.h:406
const Vec4< T > & operator+=(const Vec4< S > &v1)
Add each element of the given vector to the corresponding element of this vector. ...
Definition: Vec4.h:351
Vec4< T > minComponent(const Vec4< T > &v1, const Vec4< T > &v2)
Return component-wise minimum of the two vectors.
Definition: Vec4.h:527
const Vec4< T > & operator/=(S scalar)
Divide each element of this vector by scalar.
Definition: Vec4.h:318
T value_type
Definition: Vec4.h:27
T x() const
Get the component, e.g. float f = v.y();.
Definition: Vec4.h:82
Vec4(T x, T y, T z, T w)
Constructor with four arguments, e.g. Vec4f v(1,2,3,4);.
Definition: Vec4.h:37
MatType scale(const Vec3< typename MatType::value_type > &s)
Return a matrix that scales by s.
Definition: Mat.h:620
T y() const
Definition: Vec4.h:83
static unsigned numColumns()
Definition: Vec4.h:384
const std::enable_if<!VecTraits< T >::IsVec, T >::type & max(const T &a, const T &b)
Definition: Composite.h:106
bool eq(const Vec4< T > &v, T eps=static_cast< T >(1.0e-8)) const
Definition: Vec4.h:129
T & operator()(int i)
Alternative indexed reference to the elements.
Definition: Vec4.h:91