OpenVDB: openvdb/math/Vec3.h Source File

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