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>
77 template<
typename Source>
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]);
85 template<
typename Other>
88 this->mm[0] =
static_cast<T
>(v[0]);
89 this->mm[1] =
static_cast<T
>(v[1]);
90 this->mm[2] =
static_cast<T
>(v[2]);
94 T&
x() {
return this->mm[0]; }
95 T&
y() {
return this->mm[1]; }
96 T&
z() {
return this->mm[2]; }
99 T
x()
const {
return this->mm[0]; }
100 T
y()
const {
return this->mm[1]; }
101 T
z()
const {
return this->mm[2]; }
116 this->mm[0] = x; this->mm[1] = y; this->mm[2] = z;
124 this->mm[0] = 0; this->mm[1] = 0; this->mm[2] = 0;
129 template<
typename Source>
141 bool eq(
const Vec3<T> &v, T eps = static_cast<T>(1.0e-7))
const
154 template <
typename T0,
typename T1>
157 this->mm[0] = v1[0] + v2[0];
158 this->mm[1] = v1[1] + v2[1];
159 this->mm[2] = v1[2] + v2[2];
166 template <
typename T0,
typename T1>
169 this->mm[0] = v1[0] - v2[0];
170 this->mm[1] = v1[1] - v2[1];
171 this->mm[2] = v1[2] - v2[2];
178 template <
typename T0,
typename T1>
181 this->mm[0] = scale * v[0];
182 this->mm[1] = scale * v[1];
183 this->mm[2] = scale * v[2];
188 template <
typename T0,
typename T1>
191 this->mm[0] = v[0] /
scale;
192 this->mm[1] = v[1] /
scale;
193 this->mm[2] = v[2] /
scale;
202 this->mm[0]*v.mm[0] +
203 this->mm[1]*v.mm[1] +
210 return static_cast<T
>(sqrt(
double(
211 this->mm[0]*this->mm[0] +
212 this->mm[1]*this->mm[1] +
213 this->mm[2]*this->mm[2])));
222 this->mm[0]*this->mm[0] +
223 this->mm[1]*this->mm[1] +
224 this->mm[2]*this->mm[2];
230 return Vec3<T>(this->mm[1]*v.mm[2] - this->mm[2]*v.mm[1],
231 this->mm[2]*v.mm[0] - this->mm[0]*v.mm[2],
232 this->mm[0]*v.mm[1] - this->mm[1]*v.mm[0]);
241 this->mm[0] = v1.mm[1]*v2.mm[2] - v1.mm[2]*v2.mm[1];
242 this->mm[1] = v1.mm[2]*v2.mm[0] - v1.mm[0]*v2.mm[2];
243 this->mm[2] = v1.mm[0]*v2.mm[1] - v1.mm[1]*v2.mm[0];
248 template <
typename S>
251 this->mm[0] *= scalar;
252 this->mm[1] *= scalar;
253 this->mm[2] *= scalar;
258 template <
typename S>
261 this->mm[0] *= v1[0];
262 this->mm[1] *= v1[1];
263 this->mm[2] *= v1[2];
268 template <
typename S>
271 this->mm[0] /= scalar;
272 this->mm[1] /= scalar;
273 this->mm[2] /= scalar;
278 template <
typename S>
281 this->mm[0] /= v1[0];
282 this->mm[1] /= v1[1];
283 this->mm[2] /= v1[2];
288 template <
typename S>
291 this->mm[0] += scalar;
292 this->mm[1] += scalar;
293 this->mm[2] += scalar;
298 template <
typename S>
301 this->mm[0] += v1[0];
302 this->mm[1] += v1[1];
303 this->mm[2] += v1[2];
308 template <
typename S>
311 this->mm[0] -= scalar;
312 this->mm[1] -= scalar;
313 this->mm[2] -= scalar;
318 template <
typename S>
321 this->mm[0] -= v1[0];
322 this->mm[1] -= v1[1];
323 this->mm[2] -= v1[2];
331 this->mm[0] = std::exp(this->mm[0]);
332 this->mm[1] = std::exp(this->mm[1]);
333 this->mm[2] = std::exp(this->mm[2]);
340 return this->mm[0] + this->mm[1] + this->mm[2];
384 return dot(onto)*(T(1)/l);
391 T l = onto.lengthSqr();
394 return onto*(dot(onto)*(T(1)/l));
405 if ( fabs(this->mm[0]) >= fabs(this->mm[1]) ) {
407 l = this->mm[0]*this->mm[0] + this->mm[2]*this->mm[2];
408 l =
static_cast<T
>(T(1)/sqrt(
double(l)));
409 u.mm[0] = -this->mm[2]*l;
411 u.mm[2] = +this->mm[0]*l;
414 l = this->mm[1]*this->mm[1] + this->mm[2]*this->mm[2];
415 l =
static_cast<T
>(T(1)/sqrt(
double(l)));
417 u.mm[1] = +this->mm[2]*l;
418 u.mm[2] = -this->mm[1]*l;
425 bool isNan()
const {
return isnan(this->mm[0]) || isnan(this->mm[1]) || isnan(this->mm[2]); }
430 return isinf(this->mm[0]) || isinf(this->mm[1]) || isinf(this->mm[2]);
436 return finite(this->mm[0]) && finite(this->mm[1]) && finite(this->mm[2]);
445 template <
typename T0,
typename T1>
453 template <
typename T0,
typename T1>
457 template <
typename S,
typename T>
461 template <
typename S,
typename T>
470 template <
typename T0,
typename T1>
479 template <
typename S,
typename T>
486 template <
typename S,
typename T>
495 template <
typename T0,
typename T1>
503 template <
typename T0,
typename T1>
512 template <
typename S,
typename T>
521 template <
typename T0,
typename T1>
530 template <
typename S,
typename T>
540 template <
typename T>
544 return static_cast<T
>(atan2(c.length(), v1.dot(v2)));
547 template <
typename T>
553 template <
typename T>
564 template <
typename T>
598 template <
typename T>
608 template <
typename T>
619 template <
typename T>
631 #endif // OPENVDB_MATH_VEC3_HAS_BEEN_INCLUDED
OPENVDB_API Hermite min(const Hermite &, const Hermite &)
min and max operations done directly on the compressed data.
const Vec3< T > & div(T0 scale, const Vec3< T1 > &v)
Definition: Vec3.h:189
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:620
const Vec3< T > & scale(T0 scale, const Vec3< T1 > &v)
Definition: Vec3.h:179
const Vec3< T > & add(const Vec3< T0 > &v1, const Vec3< T1 > &v2)
Definition: Vec3.h:155
Vec3(const Tuple< 3, Source > &v)
Conversion constructor.
Definition: Vec3.h:78
Vec3< typename promote< S, T >::type > operator+(const Vec3< T > &v, S scalar)
Returns V, where for .
Definition: Vec3.h:513
Vec3(Source *a)
Constructor with array argument, e.g. double a[3]; Vec3d v(a);.
Definition: Vec3.h:69
Vec3< T > projection(const Vec3< T > &onto, T eps=1.0e-7) const
Definition: Vec3.h:389
static unsigned numColumns()
Definition: Vec3.h:374
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 > & init(T x=0, T y=0, T z=0)
Definition: Vec3.h:114
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
T operator()(int i) const
Alternative indexed constant reference to the elements,.
Definition: Vec3.h:110
T & z()
Definition: Vec3.h:96
static unsigned numElements()
Definition: Vec3.h:375
Definition: Exceptions.h:78
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:237
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:97
bool normalize(T eps=T(1.0e-7))
this = normalized this
Definition: Vec3.h:344
const Vec3< T > & operator+=(S scalar)
Returns v, where for .
Definition: Vec3.h:289
bool isInfinite() const
True if an Inf is present in vector.
Definition: Vec3.h:428
bool operator==(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Equality operator, does exact floating point comparisons.
Definition: Vec3.h:446
T sum() const
Return the sum of all the vector components.
Definition: Vec3.h:338
const Vec3< T > & operator=(const Vec3< Source > &v)
Assignment operator.
Definition: Vec3.h:130
T length() const
Length of the vector.
Definition: Vec3.h:208
T lengthSqr() const
Definition: Vec3.h:219
bool isRelOrApproxEqual(const Type &a, const Type &b, const Type &absTol, const Type &relTol)
Definition: Math.h:361
const Vec3< T > & exp()
Definition: Vec3.h:329
T * asPointer()
Definition: Vec3.h:103
static Vec3< T > zero()
Predefined constants, e.g. Vec3d v = Vec3d::xNegAxis();.
Definition: Vec3.h:440
T component(const Vec3< T > &onto, T eps=1.0e-7) const
Definition: Vec3.h:379
Vec3< T > maxComponent(const Vec3< T > &v1, const Vec3< T > &v2)
Return component-wise maximum of the two vectors.
Definition: Vec3.h:609
const Vec3< T > & operator*=(S scalar)
Returns v, where for .
Definition: Vec3.h:249
T & y()
Definition: Vec3.h:95
Vec3< uint32_t > Vec3ui
Definition: Vec3.h:623
bool isApproxEqual(const Vec3< T > &a, const Vec3< T > &b, const Vec3< T > &eps)
Definition: Vec3.h:555
const T * asPointer() const
Definition: Vec3.h:104
Vec3< T > cross(const Vec3< T > &v) const
Return the cross product of "this" vector and v;.
Definition: Vec3.h:228
#define OPENVDB_VERSION_NAME
Definition: version.h:45
bool isNan() const
True if a Nan is present in vector.
Definition: Vec3.h:425
T dot(const Vec3< T > &v) const
Dot product.
Definition: Vec3.h:199
const Vec3< T > & operator-=(S scalar)
Returns v, where for .
Definition: Vec3.h:309
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:351
T x() const
Get the component, e.g. float f = v.y();.
Definition: Vec3.h:99
T y() const
Definition: Vec3.h:100
Vec3< int32_t > Vec3i
Definition: Vec3.h:622
static unsigned numRows()
Definition: Vec3.h:373
T & operator()(int i)
Alternative indexed reference to the elements.
Definition: Vec3.h:107
T value_type
Definition: Vec3.h:50
MatType scale(const Vec3< typename MatType::value_type > &s)
Return a matrix that scales by s.
Definition: Mat.h:593
Vec3< typename promote< T0, T1 >::type > operator/(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Returns V, where for .
Definition: Vec3.h:496
Vec3< T > unit(T eps, T &len) const
return normalized this and length, throws if null vector
Definition: Vec3.h:363
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:141
Vec3< double > Vec3d
Definition: Vec3.h:625
const Vec3< T > & operator*=(const Vec3< S > &v1)
Returns v0, where for .
Definition: Vec3.h:259
Vec3< float > Vec3s
Definition: Vec3.h:624
const Vec3< T > & setZero()
Set "this" vector to zero.
Definition: Vec3.h:122
Vec3< typename promote< T0, T1 >::type > operator*(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Returns V, where for .
Definition: Vec3.h:471
Vec3< T > minComponent(const Vec3< T > &v1, const Vec3< T > &v2)
Return component-wise minimum of the two vectors.
Definition: Vec3.h:599
Vec3< T > unit(T eps=0) const
return normalized this, throws if null vector
Definition: Vec3.h:356
const Vec3< T > & operator+=(const Vec3< S > &v1)
Returns v0, where for .
Definition: Vec3.h:299
void orthonormalize(Vec3< T > &v1, Vec3< T > &v2, Vec3< T > &v3)
Definition: Vec3.h:565
Vec3< T > getArbPerpendicular() const
Definition: Vec3.h:400
bool operator!=(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Inequality operator, does exact floating point comparisons.
Definition: Vec3.h:454
const Vec3< T > & sub(const Vec3< T0 > &v1, const Vec3< T1 > &v2)
Definition: Vec3.h:167
const Vec3< T > & operator/=(S scalar)
Returns v, where for .
Definition: Vec3.h:269
Vec3(T val)
Constructor with one argument, e.g. Vec3f v(0);.
Definition: Vec3.h:57
T angle(const Vec3< T > &v1, const Vec3< T > &v2)
Definition: Vec3.h:541
T ValueType
Definition: Vec3.h:51
Vec3< typename promote< S, T >::type > operator-(const Vec3< T > &v, S scalar)
Returns V, where for .
Definition: Vec3.h:531
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:626
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:67
const Vec3< T > & operator/=(const Vec3< S > &v1)
Returns v0, where for .
Definition: Vec3.h:279
Vec3< T > operator-() const
Negation operator, for e.g. v1 = -v2;.
Definition: Vec3.h:150
bool isFinite() const
True if all no Nan or Inf values present.
Definition: Vec3.h:434
T & x()
Reference to the component, e.g. v.x() = 4.5f;.
Definition: Vec3.h:94
T z() const
Definition: Vec3.h:101
const Vec3< T > & operator-=(const Vec3< S > &v1)
Returns v0, where for .
Definition: Vec3.h:319
Vec3(const Vec3< Other > &v)
Definition: Vec3.h:86
Vec3()
Trivial constructor, the vector is NOT initialized.
Definition: Vec3.h:54