OpenVDB 11.0.0

Loading...
Searching...
No Matches
Vec3.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_VEC3_HAS_BEEN_INCLUDED
5#define OPENVDB_MATH_VEC3_HAS_BEEN_INCLUDED
6
7#include <openvdb/Exceptions.h>
8#include "Math.h"
9#include "Tuple.h"
10#include <algorithm>
11#include <cmath>
12#include <type_traits>
13
14
15 namespace openvdb {
16 OPENVDB_USE_VERSION_NAMESPACE
17 namespace OPENVDB_VERSION_NAME {
18 namespace math {
19
20 template<typename T> class Mat3;
21
22 template<typename T>
23 class Vec3: public Tuple<3, T>
24{
25 public:
26 using value_type = T;
27 using ValueType = T;
28
29 /// Trivial constructor, the vector is NOT initialized
30 /// @note destructor, copy constructor, assignment operator and
31 /// move constructor are left to be defined by the compiler (default)
32 Vec3() = default;
33
34 /// @brief Construct a vector all of whose components have the given value.
35 explicit Vec3(T val) { this->mm[0] = this->mm[1] = this->mm[2] = val; }
36
37 /// Constructor with three arguments, e.g. Vec3d v(1,2,3);
38 Vec3(T x, T y, T z)
39 {
40 this->mm[0] = x;
41 this->mm[1] = y;
42 this->mm[2] = z;
43 }
44
45 /// Constructor with array argument, e.g. double a[3]; Vec3d v(a);
46 template <typename Source>
47 Vec3(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 }
53
54 /// @brief Construct a Vec3 from a 3-Tuple with a possibly different value type.
55 /// @details Type conversion warnings are suppressed.
56 template<typename Source>
57 explicit Vec3(const Tuple<3, 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 }
63
64 /// @brief Construct a vector all of whose components have the given value,
65 /// which may be of an arithmetic type different from this vector's value type.
66 /// @details Type conversion warnings are suppressed.
67 template<typename Other>
68 explicit Vec3(Other val,
69 typename std::enable_if<std::is_arithmetic<Other>::value, Conversion>::type = Conversion{})
70 {
71 this->mm[0] = this->mm[1] = this->mm[2] = static_cast<T>(val);
72 }
73
74 /// @brief Construct a Vec3 from another Vec3 with a possibly different value type.
75 /// @details Type conversion warnings are suppressed.
76 template<typename Other>
77 Vec3(const Vec3<Other>& v)
78 {
79 this->mm[0] = static_cast<T>(v[0]);
80 this->mm[1] = static_cast<T>(v[1]);
81 this->mm[2] = static_cast<T>(v[2]);
82 }
83
84 /// Reference to the component, e.g. v.x() = 4.5f;
85 T& x() { return this->mm[0]; }
86 T& y() { return this->mm[1]; }
87 T& z() { return this->mm[2]; }
88
89 /// Get the component, e.g. float f = v.y();
90 T x() const { return this->mm[0]; }
91 T y() const { return this->mm[1]; }
92 T z() const { return this->mm[2]; }
93
94 T* asPointer() { return this->mm; }
95 const T* asPointer() const { return this->mm; }
96
97 /// Alternative indexed reference to the elements
98 T& operator()(int i) { return this->mm[i]; }
99
100 /// Alternative indexed constant reference to the elements,
101 T operator()(int i) const { return this->mm[i]; }
102
103 /// "this" vector gets initialized to [x, y, z],
104 /// calling v.init(); has same effect as calling v = Vec3::zero();
105 const Vec3<T>& init(T x=0, T y=0, T z=0)
106 {
107 this->mm[0] = x; this->mm[1] = y; this->mm[2] = z;
108 return *this;
109 }
110
111
112 /// Set "this" vector to zero
113 const Vec3<T>& setZero()
114 {
115 this->mm[0] = 0; this->mm[1] = 0; this->mm[2] = 0;
116 return *this;
117 }
118
119 /// @brief Assignment operator
120 /// @details Type conversion warnings are not suppressed.
121 template<typename Source>
122 const Vec3<T>& operator=(const Vec3<Source> &v)
123 {
124 // note: don't static_cast because that suppresses warnings
125 this->mm[0] = v[0];
126 this->mm[1] = v[1];
127 this->mm[2] = v[2];
128
129 return *this;
130 }
131
132 /// Test if "this" vector is equivalent to vector v with tolerance of eps
133 bool eq(const Vec3<T> &v, T eps = static_cast<T>(1.0e-7)) const
134 {
135 return isRelOrApproxEqual(this->mm[0], v.mm[0], eps, eps) &&
136 isRelOrApproxEqual(this->mm[1], v.mm[1], eps, eps) &&
137 isRelOrApproxEqual(this->mm[2], v.mm[2], eps, eps);
138 }
139
140
141 /// Negation operator, for e.g. v1 = -v2;
142 Vec3<T> operator-() const { return Vec3<T>(-this->mm[0], -this->mm[1], -this->mm[2]); }
143
144 /// this = v1 + v2
145 /// "this", v1 and v2 need not be distinct objects, e.g. v.add(v1,v);
146 template <typename T0, typename T1>
147 const Vec3<T>& add(const Vec3<T0> &v1, const Vec3<T1> &v2)
148 {
149 this->mm[0] = v1[0] + v2[0];
150 this->mm[1] = v1[1] + v2[1];
151 this->mm[2] = v1[2] + v2[2];
152
153 return *this;
154 }
155
156 /// this = v1 - v2
157 /// "this", v1 and v2 need not be distinct objects, e.g. v.sub(v1,v);
158 template <typename T0, typename T1>
159 const Vec3<T>& sub(const Vec3<T0> &v1, const Vec3<T1> &v2)
160 {
161 this->mm[0] = v1[0] - v2[0];
162 this->mm[1] = v1[1] - v2[1];
163 this->mm[2] = v1[2] - v2[2];
164
165 return *this;
166 }
167
168 /// this = scalar*v, v need not be a distinct object from "this",
169 /// e.g. v.scale(1.5,v1);
170 template <typename T0, typename T1>
171 const Vec3<T>& scale(T0 scale, const Vec3<T1> &v)
172 {
173 this->mm[0] = scale * v[0];
174 this->mm[1] = scale * v[1];
175 this->mm[2] = scale * v[2];
176
177 return *this;
178 }
179
180 template <typename T0, typename T1>
181 const Vec3<T> &div(T0 scale, const Vec3<T1> &v)
182 {
183 this->mm[0] = v[0] / scale;
184 this->mm[1] = v[1] / scale;
185 this->mm[2] = v[2] / scale;
186
187 return *this;
188 }
189
190 /// Dot product
191 T dot(const Vec3<T> &v) const
192 {
193 return
194 this->mm[0]*v.mm[0] +
195 this->mm[1]*v.mm[1] +
196 this->mm[2]*v.mm[2];
197 }
198
199 /// Length of the vector
200 T length() const
201 {
202 return static_cast<T>(sqrt(double(
203 this->mm[0]*this->mm[0] +
204 this->mm[1]*this->mm[1] +
205 this->mm[2]*this->mm[2])));
206 }
207
208
209 /// Squared length of the vector, much faster than length() as it
210 /// does not involve square root
211 T lengthSqr() const
212 {
213 return
214 this->mm[0]*this->mm[0] +
215 this->mm[1]*this->mm[1] +
216 this->mm[2]*this->mm[2];
217 }
218
219 /// Return the cross product of "this" vector and v;
220 Vec3<T> cross(const Vec3<T> &v) const
221 {
222 return Vec3<T>(this->mm[1]*v.mm[2] - this->mm[2]*v.mm[1],
223 this->mm[2]*v.mm[0] - this->mm[0]*v.mm[2],
224 this->mm[0]*v.mm[1] - this->mm[1]*v.mm[0]);
225 }
226
227
228 /// this = v1 cross v2, v1 and v2 must be distinct objects than "this"
229 const Vec3<T>& cross(const Vec3<T> &v1, const Vec3<T> &v2)
230 {
231 // assert(this!=&v1);
232 // assert(this!=&v2);
233 this->mm[0] = v1.mm[1]*v2.mm[2] - v1.mm[2]*v2.mm[1];
234 this->mm[1] = v1.mm[2]*v2.mm[0] - v1.mm[0]*v2.mm[2];
235 this->mm[2] = v1.mm[0]*v2.mm[1] - v1.mm[1]*v2.mm[0];
236 return *this;
237 }
238
239 /// Multiply each element of this vector by @a scalar.
240 template <typename S>
241 const Vec3<T> &operator*=(S scalar)
242 {
243 OPENVDB_NO_TYPE_CONVERSION_WARNING_BEGIN
244 const auto value0 = this->mm[0] * scalar;
245 const auto value1 = this->mm[1] * scalar;
246 const auto value2 = this->mm[2] * scalar;
247 OPENVDB_NO_TYPE_CONVERSION_WARNING_END
248 this->mm[0] = static_cast<T>(value0);
249 this->mm[1] = static_cast<T>(value1);
250 this->mm[2] = static_cast<T>(value2);
251 return *this;
252 }
253
254 /// Multiply each element of this vector by the corresponding element of the given vector.
255 template <typename S>
256 const Vec3<T> &operator*=(const Vec3<S> &v1)
257 {
258 this->mm[0] *= v1[0];
259 this->mm[1] *= v1[1];
260 this->mm[2] *= v1[2];
261 return *this;
262 }
263
264 /// Divide each element of this vector by @a scalar.
265 template <typename S>
266 const Vec3<T> &operator/=(S scalar)
267 {
268 this->mm[0] /= scalar;
269 this->mm[1] /= scalar;
270 this->mm[2] /= scalar;
271 return *this;
272 }
273
274 /// Divide each element of this vector by the corresponding element of the given vector.
275 template <typename S>
276 const Vec3<T> &operator/=(const Vec3<S> &v1)
277 {
278 this->mm[0] /= v1[0];
279 this->mm[1] /= v1[1];
280 this->mm[2] /= v1[2];
281 return *this;
282 }
283
284 /// Add @a scalar to each element of this vector.
285 template <typename S>
286 const Vec3<T> &operator+=(S scalar)
287 {
288 OPENVDB_NO_TYPE_CONVERSION_WARNING_BEGIN
289 const auto value0 = this->mm[0] + scalar;
290 const auto value1 = this->mm[1] + scalar;
291 const auto value2 = this->mm[2] + scalar;
292 OPENVDB_NO_TYPE_CONVERSION_WARNING_END
293 this->mm[0] = static_cast<T>(value0);
294 this->mm[1] = static_cast<T>(value1);
295 this->mm[2] = static_cast<T>(value2);
296 return *this;
297 }
298
299 /// Add each element of the given vector to the corresponding element of this vector.
300 template <typename S>
301 const Vec3<T> &operator+=(const Vec3<S> &v1)
302 {
303 this->mm[0] += v1[0];
304 this->mm[1] += v1[1];
305 this->mm[2] += v1[2];
306 return *this;
307 }
308
309 /// Subtract @a scalar from each element of this vector.
310 template <typename S>
311 const Vec3<T> &operator-=(S scalar)
312 {
313 this->mm[0] -= scalar;
314 this->mm[1] -= scalar;
315 this->mm[2] -= scalar;
316 return *this;
317 }
318
319 /// Subtract each element of the given vector from the corresponding element of this vector.
320 template <typename S>
321 const Vec3<T> &operator-=(const Vec3<S> &v1)
322 {
323 this->mm[0] -= v1[0];
324 this->mm[1] -= v1[1];
325 this->mm[2] -= v1[2];
326 return *this;
327 }
328
329 /// Return a reference to itself after the exponent has been
330 /// applied to all the vector components.
331 inline const Vec3<T>& exp()
332 {
333 this->mm[0] = std::exp(this->mm[0]);
334 this->mm[1] = std::exp(this->mm[1]);
335 this->mm[2] = std::exp(this->mm[2]);
336 return *this;
337 }
338
339 /// Return a reference to itself after log has been
340 /// applied to all the vector components.
341 inline const Vec3<T>& log()
342 {
343 this->mm[0] = std::log(this->mm[0]);
344 this->mm[1] = std::log(this->mm[1]);
345 this->mm[2] = std::log(this->mm[2]);
346 return *this;
347 }
348
349 /// Return the sum of all the vector components.
350 inline T sum() const
351 {
352 return this->mm[0] + this->mm[1] + this->mm[2];
353 }
354
355 /// Return the product of all the vector components.
356 inline T product() const
357 {
358 return this->mm[0] * this->mm[1] * this->mm[2];
359 }
360
361 /// this = normalized this
362 bool normalize(T eps = T(1.0e-7))
363 {
364 T d = length();
365 if (isApproxEqual(d, T(0), eps)) {
366 return false;
367 }
368 *this *= (T(1) / d);
369 return true;
370 }
371
372
373 /// return normalized this, throws if null vector
374 Vec3<T> unit(T eps=0) const
375 {
376 T d;
377 return unit(eps, d);
378 }
379
380 /// return normalized this and length, throws if null vector
381 Vec3<T> unit(T eps, T& len) const
382 {
383 len = length();
384 if (isApproxEqual(len, T(0), eps)) {
385 OPENVDB_THROW(ArithmeticError, "Normalizing null 3-vector");
386 }
387 return *this / len;
388 }
389
390 /// return normalized this, or (1, 0, 0) if this is null vector
391 Vec3<T> unitSafe() const
392 {
393 T l2 = lengthSqr();
394 return l2 ? *this / static_cast<T>(sqrt(l2)) : Vec3<T>(1, 0 ,0);
395 }
396
397 // Number of cols, rows, elements
398 static unsigned numRows() { return 1; }
399 static unsigned numColumns() { return 3; }
400 static unsigned numElements() { return 3; }
401
402 /// Returns the scalar component of v in the direction of onto, onto need
403 /// not be unit. e.g double c = Vec3d::component(v1,v2);
404 T component(const Vec3<T> &onto, T eps = static_cast<T>(1.0e-7)) const
405 {
406 T l = onto.length();
407 if (isApproxEqual(l, T(0), eps)) return 0;
408
409 return dot(onto)*(T(1)/l);
410 }
411
412 /// Return the projection of v onto the vector, onto need not be unit
413 /// e.g. Vec3d a = vprojection(n);
414 Vec3<T> projection(const Vec3<T> &onto, T eps = static_cast<T>(1.0e-7)) const
415 {
416 T l = onto.lengthSqr();
417 if (isApproxEqual(l, T(0), eps)) return Vec3::zero();
418
419 return onto*(dot(onto)*(T(1)/l));
420 }
421
422 /// Return an arbitrary unit vector perpendicular to v
423 /// Vector this must be a unit vector
424 /// e.g. v = v.normalize(); Vec3d n = v.getArbPerpendicular();
425 Vec3<T> getArbPerpendicular() const
426 {
427 Vec3<T> u;
428 T l;
429
430 if ( fabs(this->mm[0]) >= fabs(this->mm[1]) ) {
431 // v.x or v.z is the largest magnitude component, swap them
432 l = this->mm[0]*this->mm[0] + this->mm[2]*this->mm[2];
433 l = static_cast<T>(T(1)/sqrt(double(l)));
434 u.mm[0] = -this->mm[2]*l;
435 u.mm[1] = T(0);
436 u.mm[2] = +this->mm[0]*l;
437 } else {
438 // W.y or W.z is the largest magnitude component, swap them
439 l = this->mm[1]*this->mm[1] + this->mm[2]*this->mm[2];
440 l = static_cast<T>(T(1)/sqrt(double(l)));
441 u.mm[0] = T(0);
442 u.mm[1] = +this->mm[2]*l;
443 u.mm[2] = -this->mm[1]*l;
444 }
445
446 return u;
447 }
448
449 /// Return a vector with the components of this in ascending order
450 Vec3<T> sorted() const
451 {
452 Vec3<T> r(*this);
453 if( r.mm[0] > r.mm[1] ) std::swap(r.mm[0], r.mm[1]);
454 if( r.mm[1] > r.mm[2] ) std::swap(r.mm[1], r.mm[2]);
455 if( r.mm[0] > r.mm[1] ) std::swap(r.mm[0], r.mm[1]);
456 return r;
457 }
458
459 /// Return the vector (z, y, x)
460 Vec3<T> reversed() const
461 {
462 return Vec3<T>(this->mm[2], this->mm[1], this->mm[0]);
463 }
464
465 /// Predefined constants, e.g. Vec3d v = Vec3d::xNegAxis();
466 static Vec3<T> zero() { return Vec3<T>(0, 0, 0); }
467 static Vec3<T> ones() { return Vec3<T>(1, 1, 1); }
468};
469
470
471/// Equality operator, does exact floating point comparisons
472 template <typename T0, typename T1>
473 inline bool operator==(const Vec3<T0> &v0, const Vec3<T1> &v1)
474{
475 return isExactlyEqual(v0[0], v1[0]) && isExactlyEqual(v0[1], v1[1])
476 && isExactlyEqual(v0[2], v1[2]);
477}
478
479/// Inequality operator, does exact floating point comparisons
480 template <typename T0, typename T1>
481 inline bool operator!=(const Vec3<T0> &v0, const Vec3<T1> &v1) { return !(v0==v1); }
482
483/// Multiply each element of the given vector by @a scalar and return the result.
484 template <typename S, typename T>
485 inline Vec3<typename promote<S, T>::type> operator*(S scalar, const Vec3<T> &v) { return v*scalar; }
486
487/// Multiply each element of the given vector by @a scalar and return the result.
488 template <typename S, typename T>
489 inline Vec3<typename promote<S, T>::type> operator*(const Vec3<T> &v, S scalar)
490{
491 Vec3<typename promote<S, T>::type> result(v);
492 result *= scalar;
493 return result;
494}
495
496/// Multiply corresponding elements of @a v0 and @a v1 and return the result.
497 template <typename T0, typename T1>
498 inline Vec3<typename promote<T0, T1>::type> operator*(const Vec3<T0> &v0, const Vec3<T1> &v1)
499{
500 Vec3<typename promote<T0, T1>::type> result(v0[0] * v1[0], v0[1] * v1[1], v0[2] * v1[2]);
501 return result;
502}
503
504
505/// Divide @a scalar by each element of the given vector and return the result.
506 template <typename S, typename T>
507 inline Vec3<typename promote<S, T>::type> operator/(S scalar, const Vec3<T> &v)
508{
509 return Vec3<typename promote<S, T>::type>(scalar/v[0], scalar/v[1], scalar/v[2]);
510}
511
512/// Divide each element of the given vector by @a scalar and return the result.
513 template <typename S, typename T>
514 inline Vec3<typename promote<S, T>::type> operator/(const Vec3<T> &v, S scalar)
515{
516 Vec3<typename promote<S, T>::type> result(v);
517 result /= scalar;
518 return result;
519}
520
521/// Divide corresponding elements of @a v0 and @a v1 and return the result.
522 template <typename T0, typename T1>
523 inline Vec3<typename promote<T0, T1>::type> operator/(const Vec3<T0> &v0, const Vec3<T1> &v1)
524{
525 Vec3<typename promote<T0, T1>::type> result(v0[0] / v1[0], v0[1] / v1[1], v0[2] / v1[2]);
526 return result;
527}
528
529/// Add corresponding elements of @a v0 and @a v1 and return the result.
530 template <typename T0, typename T1>
531 inline Vec3<typename promote<T0, T1>::type> operator+(const Vec3<T0> &v0, const Vec3<T1> &v1)
532{
533 Vec3<typename promote<T0, T1>::type> result(v0);
534 result += v1;
535 return result;
536}
537
538/// Add @a scalar to each element of the given vector and return the result.
539 template <typename S, typename T>
540 inline Vec3<typename promote<S, T>::type> operator+(const Vec3<T> &v, S scalar)
541{
542 Vec3<typename promote<S, T>::type> result(v);
543 result += scalar;
544 return result;
545}
546
547/// Subtract corresponding elements of @a v0 and @a v1 and return the result.
548 template <typename T0, typename T1>
549 inline Vec3<typename promote<T0, T1>::type> operator-(const Vec3<T0> &v0, const Vec3<T1> &v1)
550{
551 Vec3<typename promote<T0, T1>::type> result(v0);
552 result -= v1;
553 return result;
554}
555
556/// Subtract @a scalar from each element of the given vector and return the result.
557 template <typename S, typename T>
558 inline Vec3<typename promote<S, T>::type> operator-(const Vec3<T> &v, S scalar)
559{
560 Vec3<typename promote<S, T>::type> result(v);
561 result -= scalar;
562 return result;
563}
564
565/// Angle between two vectors, the result is between [0, pi],
566/// e.g. double a = Vec3d::angle(v1,v2);
567 template <typename T>
568 inline T angle(const Vec3<T> &v1, const Vec3<T> &v2)
569{
570 Vec3<T> c = v1.cross(v2);
571 return static_cast<T>(atan2(c.length(), v1.dot(v2)));
572}
573
574 template <typename T>
575 inline bool
576 isApproxEqual(const Vec3<T>& a, const Vec3<T>& b)
577{
578 return a.eq(b);
579}
580 template <typename T>
581 inline bool
582 isApproxEqual(const Vec3<T>& a, const Vec3<T>& b, const Vec3<T>& eps)
583{
584 return isApproxEqual(a.x(), b.x(), eps.x()) &&
585 isApproxEqual(a.y(), b.y(), eps.y()) &&
586 isApproxEqual(a.z(), b.z(), eps.z());
587}
588
589 template<typename T>
590 inline Vec3<T>
591 Abs(const Vec3<T>& v)
592{
593 return Vec3<T>(Abs(v[0]), Abs(v[1]), Abs(v[2]));
594}
595
596/// Orthonormalize vectors v1, v2 and v3 and store back the resulting
597/// basis e.g. Vec3d::orthonormalize(v1,v2,v3);
598 template <typename T>
599 inline void orthonormalize(Vec3<T> &v1, Vec3<T> &v2, Vec3<T> &v3)
600{
601 // If the input vectors are v0, v1, and v2, then the Gram-Schmidt
602 // orthonormalization produces vectors u0, u1, and u2 as follows,
603 //
604 // u0 = v0/|v0|
605 // u1 = (v1-(u0*v1)u0)/|v1-(u0*v1)u0|
606 // u2 = (v2-(u0*v2)u0-(u1*v2)u1)/|v2-(u0*v2)u0-(u1*v2)u1|
607 //
608 // where |A| indicates length of vector A and A*B indicates dot
609 // product of vectors A and B.
610
611 // compute u0
612 v1.normalize();
613
614 // compute u1
615 T d0 = v1.dot(v2);
616 v2 -= v1*d0;
617 v2.normalize();
618
619 // compute u2
620 T d1 = v2.dot(v3);
621 d0 = v1.dot(v3);
622 v3 -= v1*d0 + v2*d1;
623 v3.normalize();
624}
625
626/// @remark We are switching to a more explicit name because the semantics
627/// are different from std::min/max. In that case, the function returns a
628/// reference to one of the objects based on a comparator. Here, we must
629/// fabricate a new object which might not match either of the inputs.
630
631/// Return component-wise minimum of the two vectors.
632 template <typename T>
633 inline Vec3<T> minComponent(const Vec3<T> &v1, const Vec3<T> &v2)
634{
635 return Vec3<T>(
636 std::min(v1.x(), v2.x()),
637 std::min(v1.y(), v2.y()),
638 std::min(v1.z(), v2.z()));
639}
640
641/// Return component-wise maximum of the two vectors.
642 template <typename T>
643 inline Vec3<T> maxComponent(const Vec3<T> &v1, const Vec3<T> &v2)
644{
645 return Vec3<T>(
646 std::max(v1.x(), v2.x()),
647 std::max(v1.y(), v2.y()),
648 std::max(v1.z(), v2.z()));
649}
650
651/// @brief Return a vector with the exponent applied to each of
652/// the components of the input vector.
653 template <typename T>
654 inline Vec3<T> Exp(Vec3<T> v) { return v.exp(); }
655
656/// @brief Return a vector with log applied to each of
657/// the components of the input vector.
658 template <typename T>
659 inline Vec3<T> Log(Vec3<T> v) { return v.log(); }
660
661 using Vec3i = Vec3<int32_t>;
662 using Vec3ui = Vec3<uint32_t>;
663 using Vec3s = Vec3<float>;
664 using Vec3d = Vec3<double>;
665
666 OPENVDB_IS_POD(Vec3i)
667 OPENVDB_IS_POD(Vec3ui)
668 OPENVDB_IS_POD(Vec3s)
669 OPENVDB_IS_POD(Vec3d)
670
671} // namespace math
672} // namespace OPENVDB_VERSION_NAME
673} // namespace openvdb
674
675#endif // OPENVDB_MATH_VEC3_HAS_BEEN_INCLUDED
Exceptions.h
OPENVDB_THROW
#define OPENVDB_THROW(exception, message)
Definition Exceptions.h:74
Math.h
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
OPENVDB_IS_POD
#define OPENVDB_IS_POD(Type)
Definition Math.h:56
OPENVDB_NO_TYPE_CONVERSION_WARNING_BEGIN
#define OPENVDB_NO_TYPE_CONVERSION_WARNING_BEGIN
Bracket code with OPENVDB_NO_TYPE_CONVERSION_WARNING_BEGIN/_END, to inhibit warnings about type conve...
Definition Platform.h:227
OPENVDB_NO_TYPE_CONVERSION_WARNING_END
#define OPENVDB_NO_TYPE_CONVERSION_WARNING_END
Definition Platform.h:228
Tuple.h
openvdb::v11_0::ArithmeticError
Definition Exceptions.h:56
openvdb::v11_0::math::Tuple
Definition Tuple.h:30
openvdb::v11_0::math::Tuple::mm
T mm[SIZE]
Copies this tuple into an array of a compatible type.
Definition Tuple.h:164
openvdb::v11_0::math::Vec3
Definition Vec3.h:24
openvdb::v11_0::math::Vec3::sorted
Vec3< T > sorted() const
Return a vector with the components of this in ascending order.
Definition Vec3.h:450
openvdb::v11_0::math::Vec3::div
const Vec3< T > & div(T0 scale, const Vec3< T1 > &v)
Definition Vec3.h:181
openvdb::v11_0::math::Vec3::x
T & x()
Reference to the component, e.g. v.x() = 4.5f;.
Definition Vec3.h:85
openvdb::v11_0::math::Vec3::sub
const Vec3< T > & sub(const Vec3< T0 > &v1, const Vec3< T1 > &v2)
Definition Vec3.h:159
openvdb::v11_0::math::Vec3::setZero
const Vec3< T > & setZero()
Set "this" vector to zero.
Definition Vec3.h:113
openvdb::v11_0::math::Vec3::operator/=
const Vec3< T > & operator/=(S scalar)
Divide each element of this vector by scalar.
Definition Vec3.h:266
openvdb::v11_0::math::Vec3::reversed
Vec3< T > reversed() const
Return the vector (z, y, x)
Definition Vec3.h:460
openvdb::v11_0::math::Vec3::add
const Vec3< T > & add(const Vec3< T0 > &v1, const Vec3< T1 > &v2)
Definition Vec3.h:147
openvdb::v11_0::math::Vec3::length
T length() const
Length of the vector.
Definition Vec3.h:200
openvdb::v11_0::math::Vec3::Vec3
Vec3(Source *a)
Constructor with array argument, e.g. double a[3]; Vec3d v(a);.
Definition Vec3.h:47
openvdb::v11_0::math::Vec3::dot
T dot(const Vec3< T > &v) const
Dot product.
Definition Vec3.h:191
openvdb::v11_0::math::Vec3::operator*=
const Vec3< T > & operator*=(const Vec3< S > &v1)
Multiply each element of this vector by the corresponding element of the given vector.
Definition Vec3.h:256
openvdb::v11_0::math::Vec3::operator*=
const Vec3< T > & operator*=(S scalar)
Multiply each element of this vector by scalar.
Definition Vec3.h:241
openvdb::v11_0::math::Vec3::sum
T sum() const
Return the sum of all the vector components.
Definition Vec3.h:350
openvdb::v11_0::math::Vec3::numColumns
static unsigned numColumns()
Definition Vec3.h:399
openvdb::v11_0::math::Vec3::operator/=
const Vec3< T > & operator/=(const Vec3< S > &v1)
Divide each element of this vector by the corresponding element of the given vector.
Definition Vec3.h:276
openvdb::v11_0::math::Vec3::init
const Vec3< T > & init(T x=0, T y=0, T z=0)
Definition Vec3.h:105
openvdb::v11_0::math::Vec3::Vec3
Vec3(T x, T y, T z)
Constructor with three arguments, e.g. Vec3d v(1,2,3);.
Definition Vec3.h:38
openvdb::v11_0::math::Vec3::unit
Vec3< T > unit(T eps, T &len) const
return normalized this and length, throws if null vector
Definition Vec3.h:381
openvdb::v11_0::math::Vec3::asPointer
T * asPointer()
Definition Vec3.h:94
openvdb::v11_0::math::Vec3::eq
bool eq(const Vec3< 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 Vec3.h:133
openvdb::v11_0::math::Vec3::numRows
static unsigned numRows()
Definition Vec3.h:398
openvdb::v11_0::math::Vec3::getArbPerpendicular
Vec3< T > getArbPerpendicular() const
Definition Vec3.h:425
openvdb::v11_0::math::Vec3::log
const Vec3< T > & log()
Definition Vec3.h:341
openvdb::v11_0::math::Vec3::operator()
T & operator()(int i)
Alternative indexed reference to the elements.
Definition Vec3.h:98
openvdb::v11_0::math::Vec3::y
T & y()
Definition Vec3.h:86
openvdb::v11_0::math::Vec3::component
T component(const Vec3< T > &onto, T eps=static_cast< T >(1.0e-7)) const
Definition Vec3.h:404
openvdb::v11_0::math::Vec3::Vec3
Vec3(T val)
Construct a vector all of whose components have the given value.
Definition Vec3.h:35
openvdb::v11_0::math::Vec3::z
T & z()
Definition Vec3.h:87
openvdb::v11_0::math::Vec3::operator()
T operator()(int i) const
Alternative indexed constant reference to the elements,.
Definition Vec3.h:101
openvdb::v11_0::math::Vec3::product
T product() const
Return the product of all the vector components.
Definition Vec3.h:356
openvdb::v11_0::math::Vec3::cross
Vec3< T > cross(const Vec3< T > &v) const
Return the cross product of "this" vector and v;.
Definition Vec3.h:220
openvdb::v11_0::math::Vec3::asPointer
const T * asPointer() const
Definition Vec3.h:95
openvdb::v11_0::math::Vec3::lengthSqr
T lengthSqr() const
Definition Vec3.h:211
openvdb::v11_0::math::Vec3::Vec3
Vec3(const Vec3< Other > &v)
Construct a Vec3 from another Vec3 with a possibly different value type.
Definition Vec3.h:77
openvdb::v11_0::math::Vec3::zero
static Vec3< T > zero()
Predefined constants, e.g. Vec3d v = Vec3d::xNegAxis();.
Definition Vec3.h:466
openvdb::v11_0::math::Vec3::Vec3
Vec3()=default
openvdb::v11_0::math::Vec3::cross
const Vec3< T > & cross(const Vec3< T > &v1, const Vec3< T > &v2)
this = v1 cross v2, v1 and v2 must be distinct objects than "this"
Definition Vec3.h:229
openvdb::v11_0::math::Vec3::Vec3
Vec3(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 Vec3.h:68
openvdb::v11_0::math::Vec3::x
T x() const
Get the component, e.g. float f = v.y();.
Definition Vec3.h:90
openvdb::v11_0::math::Vec3::Vec3
Vec3(const Tuple< 3, Source > &v)
Construct a Vec3 from a 3-Tuple with a possibly different value type.
Definition Vec3.h:57
openvdb::v11_0::math::Vec3::operator+=
const Vec3< T > & operator+=(S scalar)
Add scalar to each element of this vector.
Definition Vec3.h:286
openvdb::v11_0::math::Vec3::operator=
const Vec3< T > & operator=(const Vec3< Source > &v)
Assignment operator.
Definition Vec3.h:122
openvdb::v11_0::math::Vec3::z
T z() const
Definition Vec3.h:92
openvdb::v11_0::math::Vec3::ones
static Vec3< T > ones()
Definition Vec3.h:467
openvdb::v11_0::math::Vec3::unitSafe
Vec3< T > unitSafe() const
return normalized this, or (1, 0, 0) if this is null vector
Definition Vec3.h:391
openvdb::v11_0::math::Vec3::y
T y() const
Definition Vec3.h:91
openvdb::v11_0::math::Vec3::operator-
Vec3< T > operator-() const
Negation operator, for e.g. v1 = -v2;.
Definition Vec3.h:142
openvdb::v11_0::math::Vec3::scale
const Vec3< T > & scale(T0 scale, const Vec3< T1 > &v)
Definition Vec3.h:171
openvdb::v11_0::math::Vec3::projection
Vec3< T > projection(const Vec3< T > &onto, T eps=static_cast< T >(1.0e-7)) const
Definition Vec3.h:414
openvdb::v11_0::math::Vec3::numElements
static unsigned numElements()
Definition Vec3.h:400
openvdb::v11_0::math::Vec3::unit
Vec3< T > unit(T eps=0) const
return normalized this, throws if null vector
Definition Vec3.h:374
openvdb::v11_0::math::Vec3::operator-=
const Vec3< T > & operator-=(S scalar)
Subtract scalar from each element of this vector.
Definition Vec3.h:311
openvdb::v11_0::math::Vec3::normalize
bool normalize(T eps=T(1.0e-7))
this = normalized this
Definition Vec3.h:362
openvdb::v11_0::math::Vec3::ValueType
T ValueType
Definition Vec3.h:27
openvdb::v11_0::math::Vec3::operator-=
const Vec3< T > & operator-=(const Vec3< S > &v1)
Subtract each element of the given vector from the corresponding element of this vector.
Definition Vec3.h:321
openvdb::v11_0::math::Vec3::exp
const Vec3< T > & exp()
Definition Vec3.h:331
openvdb::v11_0::math::Vec3::operator+=
const Vec3< T > & operator+=(const Vec3< S > &v1)
Add each element of the given vector to the corresponding element of this vector.
Definition Vec3.h:301
openvdb::v11_0::math::Vec3::value_type
T value_type
Definition Vec3.h:26
openvdb::v11_0::math::Log
Vec2< T > Log(Vec2< T > v)
Return a vector with log applied to each of the components of the input vector.
Definition Vec2.h:528
openvdb::v11_0::math::operator==
bool operator==(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Equality operator, does exact floating point comparisons.
Definition Vec3.h:473
openvdb::v11_0::math::orthonormalize
void orthonormalize(Vec2< T > &v1, Vec2< T > &v2)
Definition Vec2.h:476
openvdb::v11_0::math::operator*
Mat3< typename promote< T0, T1 >::type > operator*(const Mat3< T0 > &m0, const Mat3< T1 > &m1)
Multiply m0 by m1 and return the resulting matrix.
Definition Mat3.h:597
openvdb::v11_0::math::isApproxEqual
bool isApproxEqual(const Type &a, const Type &b, const Type &tolerance)
Return true if a is equal to b to within the given tolerance.
Definition Math.h:406
openvdb::v11_0::math::isRelOrApproxEqual
bool isRelOrApproxEqual(const Type &a, const Type &b, const Type &absTol, const Type &relTol)
Definition Math.h:453
openvdb::v11_0::math::operator-
Vec3< typename promote< T, Coord::ValueType >::type > operator-(const Vec3< T > &v0, const Coord &v1)
Allow a Coord to be subtracted from a Vec3.
Definition Coord.h:553
openvdb::v11_0::math::scale
MatType scale(const Vec3< typename MatType::value_type > &s)
Return a matrix that scales by s.
Definition Mat.h:615
openvdb::v11_0::math::operator+
Vec3< typename promote< T, typename Coord::ValueType >::type > operator+(const Vec3< T > &v0, const Coord &v1)
Allow a Coord to be added to or subtracted from a Vec3.
Definition Coord.h:527
openvdb::v11_0::math::Exp
Type Exp(const Type &x)
Return ex.
Definition Math.h:710
openvdb::v11_0::math::unit
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:648
openvdb::v11_0::math::Abs
Coord Abs(const Coord &xyz)
Definition Coord.h:517
openvdb::v11_0::math::angle
T angle(const Vec2< T > &v1, const Vec2< T > &v2)
Definition Vec2.h:446
openvdb::v11_0::math::operator/
Vec2< typename promote< S, T >::type > operator/(S scalar, const Vec2< T > &v)
Divide scalar by each element of the given vector and return the result.
Definition Vec2.h:385
openvdb::v11_0::math::isExactlyEqual
bool isExactlyEqual(const T0 &a, const T1 &b)
Return true if a is exactly equal to b.
Definition Math.h:443
openvdb::v11_0::math::minComponent
Vec2< T > minComponent(const Vec2< T > &v1, const Vec2< T > &v2)
Return component-wise minimum of the two vectors.
Definition Vec2.h:504
openvdb::v11_0::math::operator!=
bool operator!=(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Inequality operator, does exact floating point comparisons.
Definition Vec3.h:481
openvdb::v11_0::math::maxComponent
Vec2< T > maxComponent(const Vec2< T > &v1, const Vec2< T > &v2)
Return component-wise maximum of the two vectors.
Definition Vec2.h:513
openvdb
Definition Exceptions.h:13
openvdb::v11_0::math::Conversion
Dummy class for tag dispatch of conversion constructors.
Definition Tuple.h:23
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition version.h.in:121
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition version.h.in:212