31 #ifndef OPENVDB_MATH_VEC3_HAS_BEEN_INCLUDED
32 #define OPENVDB_MATH_VEC3_HAS_BEEN_INCLUDED
35 #include <openvdb/Exceptions.h>
44 template<
typename T>
class Mat3;
47 class Vec3:
public Tuple<3, T>
57 explicit Vec3(T val) { this->mm[0] = this->mm[1] = this->mm[2] = val; }
68 template <
typename Source>
78 template<
typename Source>
81 this->mm[0] =
static_cast<T
>(v[0]);
82 this->mm[1] =
static_cast<T
>(v[1]);
83 this->mm[2] =
static_cast<T
>(v[2]);
88 template<
typename Other>
91 this->mm[0] =
static_cast<T
>(v[0]);
92 this->mm[1] =
static_cast<T
>(v[1]);
93 this->mm[2] =
static_cast<T
>(v[2]);
97 T&
x() {
return this->mm[0]; }
98 T&
y() {
return this->mm[1]; }
99 T&
z() {
return this->mm[2]; }
102 T
x()
const {
return this->mm[0]; }
103 T
y()
const {
return this->mm[1]; }
104 T
z()
const {
return this->mm[2]; }
119 this->mm[0] = x; this->mm[1] = y; this->mm[2] = z;
127 this->mm[0] = 0; this->mm[1] = 0; this->mm[2] = 0;
133 template<
typename Source>
145 bool eq(
const Vec3<T> &v, T eps = static_cast<T>(1.0e-7))
const
158 template <
typename T0,
typename T1>
161 this->mm[0] = v1[0] + v2[0];
162 this->mm[1] = v1[1] + v2[1];
163 this->mm[2] = v1[2] + v2[2];
170 template <
typename T0,
typename T1>
173 this->mm[0] = v1[0] - v2[0];
174 this->mm[1] = v1[1] - v2[1];
175 this->mm[2] = v1[2] - v2[2];
182 template <
typename T0,
typename T1>
185 this->mm[0] = scale * v[0];
186 this->mm[1] = scale * v[1];
187 this->mm[2] = scale * v[2];
192 template <
typename T0,
typename T1>
195 this->mm[0] = v[0] /
scale;
196 this->mm[1] = v[1] /
scale;
197 this->mm[2] = v[2] /
scale;
206 this->mm[0]*v.mm[0] +
207 this->mm[1]*v.mm[1] +
214 return static_cast<T
>(sqrt(
double(
215 this->mm[0]*this->mm[0] +
216 this->mm[1]*this->mm[1] +
217 this->mm[2]*this->mm[2])));
226 this->mm[0]*this->mm[0] +
227 this->mm[1]*this->mm[1] +
228 this->mm[2]*this->mm[2];
234 return Vec3<T>(this->mm[1]*v.mm[2] - this->mm[2]*v.mm[1],
235 this->mm[2]*v.mm[0] - this->mm[0]*v.mm[2],
236 this->mm[0]*v.mm[1] - this->mm[1]*v.mm[0]);
245 this->mm[0] = v1.mm[1]*v2.mm[2] - v1.mm[2]*v2.mm[1];
246 this->mm[1] = v1.mm[2]*v2.mm[0] - v1.mm[0]*v2.mm[2];
247 this->mm[2] = v1.mm[0]*v2.mm[1] - v1.mm[1]*v2.mm[0];
252 template <
typename S>
255 this->mm[0] =
static_cast<T
>(this->mm[0] * scalar);
256 this->mm[1] =
static_cast<T
>(this->mm[1] * scalar);
257 this->mm[2] =
static_cast<T
>(this->mm[2] * scalar);
262 template <
typename S>
265 this->mm[0] *= v1[0];
266 this->mm[1] *= v1[1];
267 this->mm[2] *= v1[2];
272 template <
typename S>
275 this->mm[0] /= scalar;
276 this->mm[1] /= scalar;
277 this->mm[2] /= scalar;
282 template <
typename S>
285 this->mm[0] /= v1[0];
286 this->mm[1] /= v1[1];
287 this->mm[2] /= v1[2];
292 template <
typename S>
295 this->mm[0] =
static_cast<T
>(this->mm[0] + scalar);
296 this->mm[1] =
static_cast<T
>(this->mm[1] + scalar);
297 this->mm[2] =
static_cast<T
>(this->mm[2] + scalar);
302 template <
typename S>
305 this->mm[0] += v1[0];
306 this->mm[1] += v1[1];
307 this->mm[2] += v1[2];
312 template <
typename S>
315 this->mm[0] -= scalar;
316 this->mm[1] -= scalar;
317 this->mm[2] -= scalar;
322 template <
typename S>
325 this->mm[0] -= v1[0];
326 this->mm[1] -= v1[1];
327 this->mm[2] -= v1[2];
335 this->mm[0] = std::exp(this->mm[0]);
336 this->mm[1] = std::exp(this->mm[1]);
337 this->mm[2] = std::exp(this->mm[2]);
344 return this->mm[0] + this->mm[1] + this->mm[2];
388 return dot(onto)*(T(1)/l);
395 T l = onto.lengthSqr();
398 return onto*(dot(onto)*(T(1)/l));
409 if ( fabs(this->mm[0]) >= fabs(this->mm[1]) ) {
411 l = this->mm[0]*this->mm[0] + this->mm[2]*this->mm[2];
412 l =
static_cast<T
>(T(1)/sqrt(
double(l)));
413 u.mm[0] = -this->mm[2]*l;
415 u.mm[2] = +this->mm[0]*l;
418 l = this->mm[1]*this->mm[1] + this->mm[2]*this->mm[2];
419 l =
static_cast<T
>(T(1)/sqrt(
double(l)));
421 u.mm[1] = +this->mm[2]*l;
422 u.mm[2] = -this->mm[1]*l;
429 bool isNan()
const {
return isnan(this->mm[0]) || isnan(this->mm[1]) || isnan(this->mm[2]); }
434 return isinf(this->mm[0]) || isinf(this->mm[1]) || isinf(this->mm[2]);
440 return finite(this->mm[0]) && finite(this->mm[1]) && finite(this->mm[2]);
449 template <
typename T0,
typename T1>
457 template <
typename T0,
typename T1>
461 template <
typename S,
typename T>
465 template <
typename S,
typename T>
474 template <
typename T0,
typename T1>
483 template <
typename S,
typename T>
490 template <
typename S,
typename T>
499 template <
typename T0,
typename T1>
507 template <
typename T0,
typename T1>
516 template <
typename S,
typename T>
525 template <
typename T0,
typename T1>
534 template <
typename S,
typename T>
544 template <
typename T>
548 return static_cast<T
>(atan2(c.length(), v1.dot(v2)));
551 template <
typename T>
557 template <
typename T>
568 template <
typename T>
602 template <
typename T>
612 template <
typename T>
623 template <
typename T>
635 #endif // OPENVDB_MATH_VEC3_HAS_BEEN_INCLUDED
T x() const
Get the component, e.g. float f = v.y();.
Definition: Vec3.h:102
const Vec3< T > & add(const Vec3< T0 > &v1, const Vec3< T1 > &v2)
Definition: Vec3.h:159
Vec3(const Vec3< Other > &v)
Construct a Vec3 from another Vec3 with a possibly different value type.
Definition: Vec3.h:89
Vec3(Source *a)
Constructor with array argument, e.g. double a[3]; Vec3d v(a);.
Definition: Vec3.h:69
Vec3< T > unit(T eps, T &len) const
return normalized this and length, throws if null vector
Definition: Vec3.h:367
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
T length() const
Length of the vector.
Definition: Vec3.h:212
T & z()
Definition: Vec3.h:99
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:97
static unsigned numElements()
Definition: Vec3.h:379
Vec3< typename promote< S, T >::type > operator-(const Vec3< T > &v, S scalar)
Returns V, where for .
Definition: Vec3.h:535
static unsigned numColumns()
Definition: Vec3.h:378
const Vec3< T > & operator+=(const Vec3< S > &v1)
Returns v0, where for .
Definition: Vec3.h:303
T value_type
Definition: Vec3.h:50
bool isNan() const
True if a Nan is present in vector.
Definition: Vec3.h:429
T y() const
Definition: Vec3.h:103
bool operator!=(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Inequality operator, does exact floating point comparisons.
Definition: Vec3.h:458
Vec3< int32_t > Vec3i
Definition: Vec3.h:626
static unsigned numRows()
Definition: Vec3.h:377
const Vec3< T > & operator/=(S scalar)
Returns v, where for .
Definition: Vec3.h:273
const Vec3< T > & sub(const Vec3< T0 > &v1, const Vec3< T1 > &v2)
Definition: Vec3.h:171
T ValueType
Definition: Vec3.h:51
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:145
T * asPointer()
Definition: Vec3.h:106
Vec3< T > unit(T eps=0) const
return normalized this, throws if null vector
Definition: Vec3.h:360
T angle(const Vec3< T > &v1, const Vec3< T > &v2)
Definition: Vec3.h:545
#define OPENVDB_VERSION_NAME
Definition: version.h:43
Vec3< T > getArbPerpendicular() const
Definition: Vec3.h:404
void orthonormalize(Vec3< T > &v1, Vec3< T > &v2, Vec3< T > &v3)
Definition: Vec3.h:569
const T * asPointer() const
Definition: Vec3.h:107
T sum() const
Return the sum of all the vector components.
Definition: Vec3.h:342
const Vec3< T > & div(T0 scale, const Vec3< T1 > &v)
Definition: Vec3.h:193
bool isRelOrApproxEqual(const Type &a, const Type &b, const Type &absTol, const Type &relTol)
Definition: Math.h:401
bool isInfinite() const
True if an Inf is present in vector.
Definition: Vec3.h:432
Vec3()
Trivial constructor, the vector is NOT initialized.
Definition: Vec3.h:54
Vec3(const Tuple< 3, Source > &v)
Construct a Vec3 from a 3-Tuple with a possibly different value type.
Definition: Vec3.h:79
const Vec3< T > & operator*=(const Vec3< S > &v1)
Returns v0, where for .
Definition: Vec3.h:263
T dot(const Vec3< T > &v) const
Dot product.
Definition: Vec3.h:203
bool normalize(T eps=T(1.0e-7))
this = normalized this
Definition: Vec3.h:348
T operator()(int i) const
Alternative indexed constant reference to the elements,.
Definition: Vec3.h:113
Definition: Exceptions.h:39
static Vec3< T > zero()
Predefined constants, e.g. Vec3d v = Vec3d::xNegAxis();.
Definition: Vec3.h:444
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:241
MatType unit(const MatType &mat, typename MatType::value_type eps=1.0e-8)
Return a copy of the given matrix with its upper 3x3 rows normalized.
Definition: Mat.h:627
OPENVDB_API Hermite min(const Hermite &, const Hermite &)
min and max operations done directly on the compressed data.
Vec3< T > projection(const Vec3< T > &onto, T eps=static_cast< T >(1.0e-7)) const
Definition: Vec3.h:393
T & x()
Reference to the component, e.g. v.x() = 4.5f;.
Definition: Vec3.h:97
Vec3< double > Vec3d
Definition: Vec3.h:629
T & operator()(int i)
Alternative indexed reference to the elements.
Definition: Vec3.h:110
T & y()
Definition: Vec3.h:98
const Vec3< T > & init(T x=0, T y=0, T z=0)
Definition: Vec3.h:117
Definition: Exceptions.h:78
const Vec3< T > & setZero()
Set "this" vector to zero.
Definition: Vec3.h:125
T component(const Vec3< T > &onto, T eps=static_cast< T >(1.0e-7)) const
Definition: Vec3.h:383
bool operator==(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Equality operator, does exact floating point comparisons.
Definition: Vec3.h:450
bool isFinite() const
True if all no Nan or Inf values present.
Definition: Vec3.h:438
Vec3< T > Exp(Vec3< T > v)
Return a vector with the exponent applied to each of the components of the input vector.
Definition: Vec3.h:624
MatType scale(const Vec3< typename MatType::value_type > &s)
Return a matrix that scales by s.
Definition: Mat.h:594
const Vec3< T > & operator=(const Vec3< Source > &v)
Assignment operator.
Definition: Vec3.h:134
Vec3< uint32_t > Vec3ui
Definition: Vec3.h:627
const Vec3< T > & exp()
Definition: Vec3.h:333
Vec3< T > minComponent(const Vec3< T > &v1, const Vec3< T > &v2)
Return component-wise minimum of the two vectors.
Definition: Vec3.h:603
const Vec3< T > & operator-=(const Vec3< S > &v1)
Returns v0, where for .
Definition: Vec3.h:323
OPENVDB_API Hermite max(const Hermite &, const Hermite &)
min and max operations done directly on the compressed data.
bool isExactlyEqual(const T0 &a, const T1 &b)
Return true if a is exactly equal to b.
Definition: Math.h:391
Vec3< typename promote< S, T >::type > operator+(const Vec3< T > &v, S scalar)
Returns V, where for .
Definition: Vec3.h:517
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71
Vec3< T > maxComponent(const Vec3< T > &v1, const Vec3< T > &v2)
Return component-wise maximum of the two vectors.
Definition: Vec3.h:613
Vec3(T val)
Constructor with one argument, e.g. Vec3f v(0);.
Definition: Vec3.h:57
Vec3< float > Vec3s
Definition: Vec3.h:628
const Vec3< T > & operator+=(S scalar)
Returns v, where for .
Definition: Vec3.h:293
const Vec3< T > & scale(T0 scale, const Vec3< T1 > &v)
Definition: Vec3.h:183
Vec3(T x, T y, T z)
Constructor with three arguments, e.g. Vec3d v(1,2,3);.
Definition: Vec3.h:60
const Vec3< T > & operator/=(const Vec3< S > &v1)
Returns v0, where for .
Definition: Vec3.h:283
const Vec3< T > & operator*=(S scalar)
Returns v, where for .
Definition: Vec3.h:253
bool isApproxEqual(const Vec3< T > &a, const Vec3< T > &b, const Vec3< T > &eps)
Definition: Vec3.h:559
T lengthSqr() const
Definition: Vec3.h:223
const Vec3< T > & operator-=(S scalar)
Returns v, where for .
Definition: Vec3.h:313
Vec3< typename promote< T0, T1 >::type > operator*(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Returns V, where for .
Definition: Vec3.h:475
Vec3< T > operator-() const
Negation operator, for e.g. v1 = -v2;.
Definition: Vec3.h:154
Vec3< typename promote< T0, T1 >::type > operator/(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Returns V, where for .
Definition: Vec3.h:500
Vec3< T > cross(const Vec3< T > &v) const
Return the cross product of "this" vector and v;.
Definition: Vec3.h:232
T z() const
Definition: Vec3.h:104