OpenVDB  0.104.0
Vec4.h
Go to the documentation of this file.
1 
2 //
3 // Copyright (c) 2012 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 
240  bool normalize(T eps=1.0e-8)
241  {
242  T d = length();
243  if (isApproxEqual(d, T(0), eps)) {
244  return false;
245  }
246  *this *= (T(1) / d);
247  return true;
248  }
249 
251  Vec4<T> unit(T eps=0) const
252  {
253  T d;
254  return unit(eps, d);
255  }
256 
258  Vec4<T> unit(T eps, T& len) const
259  {
260  len = length();
261  if (isApproxEqual(len, T(0), eps)) {
262  throw ArithmeticError("Normalizing null 4-vector");
263  }
264  return *this / len;
265  }
266 
268  template <typename S>
269  const Vec4<T> &operator*=(S scalar)
270  {
271  this->mm[0] *= scalar;
272  this->mm[1] *= scalar;
273  this->mm[2] *= scalar;
274  this->mm[3] *= scalar;
275  return *this;
276  }
277 
279  template <typename S>
280  const Vec4<T> &operator*=(const Vec4<S> &v1)
281  {
282  this->mm[0] *= v1[0];
283  this->mm[1] *= v1[1];
284  this->mm[2] *= v1[2];
285  this->mm[3] *= v1[3];
286 
287  return *this;
288  }
289 
291  template <typename S>
292  const Vec4<T> &operator/=(S scalar)
293  {
294  this->mm[0] /= scalar;
295  this->mm[1] /= scalar;
296  this->mm[2] /= scalar;
297  this->mm[3] /= scalar;
298  return *this;
299  }
300 
302  template <typename S>
303  const Vec4<T> &operator/=(const Vec4<S> &v1)
304  {
305  this->mm[0] /= v1[0];
306  this->mm[1] /= v1[1];
307  this->mm[2] /= v1[2];
308  this->mm[3] /= v1[3];
309  return *this;
310  }
311 
313  template <typename S>
314  const Vec4<T> &operator+=(S scalar)
315  {
316  this->mm[0] += scalar;
317  this->mm[1] += scalar;
318  this->mm[2] += scalar;
319  this->mm[3] += scalar;
320  return *this;
321  }
322 
324  template <typename S>
325  const Vec4<T> &operator+=(const Vec4<S> &v1)
326  {
327  this->mm[0] += v1[0];
328  this->mm[1] += v1[1];
329  this->mm[2] += v1[2];
330  this->mm[3] += v1[3];
331  return *this;
332  }
333 
335  template <typename S>
336  const Vec4<T> &operator-=(S scalar)
337  {
338  this->mm[0] -= scalar;
339  this->mm[1] -= scalar;
340  this->mm[2] -= scalar;
341  this->mm[3] -= scalar;
342  return *this;
343  }
344 
346  template <typename S>
347  const Vec4<T> &operator-=(const Vec4<S> &v1)
348  {
349  this->mm[0] -= v1[0];
350  this->mm[1] -= v1[1];
351  this->mm[2] -= v1[2];
352  this->mm[3] -= v1[3];
353  return *this;
354  }
355 
356  // Number of cols, rows, elements
357  static unsigned numRows() { return 1; }
358  static unsigned numColumns() { return 4; }
359  static unsigned numElements() { return 4; }
360 
362  bool isNan() const
363  {
364  return isnan(this->mm[0]) || isnan(this->mm[1])
365  || isnan(this->mm[2]) || isnan(this->mm[3]);
366  }
367 
369  bool isInfinite() const
370  {
371  return isinf(this->mm[0]) || isinf(this->mm[1])
372  || isinf(this->mm[2]) || isinf(this->mm[3]);
373  }
374 
376  bool isFinite() const
377  {
378  return finite(this->mm[0]) && finite(this->mm[1])
379  && finite(this->mm[2]) && finite(this->mm[3]);
380  }
381 
383  static Vec4<T> zero() { return Vec4<T>(0, 0, 0, 0); }
384  static Vec4<T> origin() { return Vec4<T>(0, 0, 0, 1); }
385 };
386 
388 template <typename T0, typename T1>
389 inline bool operator==(const Vec4<T0> &v0, const Vec4<T1> &v1)
390 {
391  return
392  isExactlyEqual(v0[0], v1[0]) &&
393  isExactlyEqual(v0[1], v1[1]) &&
394  isExactlyEqual(v0[2], v1[2]) &&
395  isExactlyEqual(v0[3], v1[3]);
396 }
397 
399 template <typename T0, typename T1>
400 inline bool operator!=(const Vec4<T0> &v0, const Vec4<T1> &v1) { return !(v0==v1); }
401 
403 template <typename S, typename T>
405 { return v*scalar; }
406 
408 template <typename S, typename T>
410 {
412  result *= scalar;
413  return result;
414 }
415 
417 template <typename T0, typename T1>
419  const Vec4<T1> &v1)
420 {
421  Vec4<typename promote<T0, T1>::type> result(v0[0]*v1[0],
422  v0[1]*v1[1],
423  v0[2]*v1[2],
424  v0[3]*v1[3]);
425  return result;
426 }
427 
429 template <typename S, typename T>
431 {
432  return Vec4<typename promote<S, T>::type>(scalar/v[0],
433  scalar/v[1],
434  scalar/v[2],
435  scalar/v[3]);
436 }
437 
439 template <typename S, typename T>
441 {
443  result /= scalar;
444  return result;
445 }
446 
448 template <typename T0, typename T1>
450  const Vec4<T1> &v1)
451 {
453  result(v0[0]/v1[0], v0[1]/v1[1], v0[2]/v1[2], v0[3]/v1[3]);
454  return result;
455 }
456 
458 template <typename T0, typename T1>
460 {
462  result += v1;
463  return result;
464 }
465 
467 template <typename S, typename T>
469 {
471  result += scalar;
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 
497 
499 template <typename T>
500 inline Vec4<T> minComponent(const Vec4<T> &v1, const Vec4<T> &v2)
501 {
502  return Vec4<T>(
503  std::min(v1.x(), v2.x()),
504  std::min(v1.y(), v2.y()),
505  std::min(v1.z(), v2.z()),
506  std::min(v1.w(), v2.w()));
507 }
508 
510 template <typename T>
511 inline Vec4<T> maxComponent(const Vec4<T> &v1, const Vec4<T> &v2)
512 {
513  return Vec4<T>(
514  std::max(v1.x(), v2.x()),
515  std::max(v1.y(), v2.y()),
516  std::max(v1.z(), v2.z()),
517  std::max(v1.w(), v2.w()));
518 }
519 
520 
521 typedef Vec4<int> Vec4i;
525 
526 #if DWREAL_IS_DOUBLE == 1
527 typedef Vec4d Vec4f;
528 #else
529 typedef Vec4s Vec4f;
530 #endif // DWREAL_IS_DOUBLE
531 
532 } // namespace math
533 } // namespace OPENVDB_VERSION_NAME
534 } // namespace openvdb
535 
536 #endif // OPENVDB_MATH_VEC4_HAS_BEEN_INCLUDED
537 
538 // Copyright (c) 2012 DreamWorks Animation LLC
539 // All rights reserved. This software is distributed under the
540 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )