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];
368 return dot(onto)*(T(1)/l);
375 T l = onto.lengthSqr();
378 return onto*(dot(onto)*(T(1)/l));
389 if ( fabs(this->mm[0]) >= fabs(this->mm[1]) ) {
391 l = this->mm[0]*this->mm[0] + this->mm[2]*this->mm[2];
392 l =
static_cast<T
>(T(1)/sqrt(
double(l)));
393 u.mm[0] = -this->mm[2]*l;
395 u.mm[2] = +this->mm[0]*l;
398 l = this->mm[1]*this->mm[1] + this->mm[2]*this->mm[2];
399 l =
static_cast<T
>(T(1)/sqrt(
double(l)));
401 u.mm[1] = +this->mm[2]*l;
402 u.mm[2] = -this->mm[1]*l;
409 bool isNan()
const {
return isnan(this->mm[0]) || isnan(this->mm[1]) || isnan(this->mm[2]); }
414 return isinf(this->mm[0]) || isinf(this->mm[1]) || isinf(this->mm[2]);
420 return finite(this->mm[0]) && finite(this->mm[1]) && finite(this->mm[2]);
429 template <
typename T0,
typename T1>
437 template <
typename T0,
typename T1>
441 template <
typename S,
typename T>
445 template <
typename S,
typename T>
454 template <
typename T0,
typename T1>
463 template <
typename S,
typename T>
470 template <
typename S,
typename T>
479 template <
typename T0,
typename T1>
487 template <
typename T0,
typename T1>
496 template <
typename S,
typename T>
505 template <
typename T0,
typename T1>
514 template <
typename S,
typename T>
524 template <
typename T>
528 return static_cast<T
>(atan2(c.length(), v1.dot(v2)));
531 template <
typename T>
537 template <
typename T>
548 template <
typename T>
582 template <
typename T>
592 template <
typename T>
611 #endif // OPENVDB_MATH_VEC3_HAS_BEEN_INCLUDED
const Vec3< T > & add(const Vec3< T0 > &v1, const Vec3< T1 > &v2)
Definition: Vec3.h:155
OPENVDB_API Hermite max(const Hermite &, const Hermite &)
min and max operations done directly on the compressed data.
Vec3< T > unit(T eps, T &len) const
return normalized this and length, throws if null vector
Definition: Vec3.h:347
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
Vec3(const Vec3< Other > &v)
Definition: Vec3.h:86
static unsigned numColumns()
Definition: Vec3.h:358
T length() const
Length of the vector.
Definition: Vec3.h:208
T & z()
Definition: Vec3.h:96
static unsigned numElements()
Definition: Vec3.h:359
T y() const
Definition: Vec3.h:100
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:97
T component(const Vec3< T > &onto, T eps=1.0e-7) const
Definition: Vec3.h:363
Vec2< typename promote< S, T >::type > operator/(S scalar, const Vec2< T > &v)
Returns V, where for .
Definition: Vec2.h:370
T value_type
Definition: Vec3.h:50
Definition: Exceptions.h:78
const Vec3< T > & operator+=(const Vec3< S > &v1)
Returns v0, where for .
Definition: Vec3.h:299
bool isNan() const
True if a Nan is present in vector.
Definition: Vec3.h:409
T angle(const Vec2< T > &v1, const Vec2< T > &v2)
Definition: Vec2.h:431
const Vec3< T > & operator/=(S scalar)
Returns v, where for .
Definition: Vec3.h:269
Vec3< int32_t > Vec3i
Definition: Vec3.h:602
Vec3< float > Vec3s
Definition: Vec3.h:604
static unsigned numRows()
Definition: Vec3.h:357
const Vec3< T > & sub(const Vec3< T0 > &v1, const Vec3< T1 > &v2)
Definition: Vec3.h:167
T * asPointer()
Definition: Vec3.h:103
Vec2< T > minComponent(const Vec2< T > &v1, const Vec2< T > &v2)
Return component-wise minimum of the two vectors.
Definition: Vec2.h:482
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
bool operator==(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Equality operator, does exact floating point comparisons.
Definition: Vec3.h:430
const T * asPointer() const
Definition: Vec3.h:104
Vec3< T > projection(const Vec3< T > &onto, T eps=1.0e-7) const
Definition: Vec3.h:373
T ValueType
Definition: Vec3.h:51
#define OPENVDB_VERSION_NAME
Definition: version.h:45
Vec3< T > unit(T eps=0) const
return normalized this, throws if null vector
Definition: Vec3.h:340
void orthonormalize(Vec2< T > &v1, Vec2< T > &v2)
Definition: Vec2.h:454
MatType unit(const MatType &mat, typename MatType::value_type eps=1.0e-8)
Definition: Mat.h:629
bool isInfinite() const
True if an Inf is present in vector.
Definition: Vec3.h:412
Vec3< uint32_t > Vec3ui
Definition: Vec3.h:603
T operator()(int i) const
Alternative indexed constant reference to the elements,.
Definition: Vec3.h:110
Vec3(const Tuple< 3, Source > &v)
Conversion constructor.
Definition: Vec3.h:78
Vec3()
Trivial constructor, the vector is NOT initialized.
Definition: Vec3.h:54
Vec2< T > maxComponent(const Vec2< T > &v1, const Vec2< T > &v2)
Return component-wise maximum of the two vectors.
Definition: Vec2.h:491
T & x()
Reference to the component, e.g. v.x() = 4.5f;.
Definition: Vec3.h:94
Vec3< T > getArbPerpendicular() const
Definition: Vec3.h:384
static Vec3< T > zero()
Predefined constants, e.g. Vec3d v = Vec3d::xNegAxis();.
Definition: Vec3.h:424
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:382
const Vec3< T > & operator*=(const Vec3< S > &v1)
Returns v0, where for .
Definition: Vec3.h:259
OPENVDB_API Hermite min(const Hermite &, const Hermite &)
min and max operations done directly on the compressed data.
T dot(const Vec3< T > &v) const
Dot product.
Definition: Vec3.h:199
bool normalize(T eps=T(1.0e-7))
this = normalized this
Definition: Vec3.h:328
const Vec3< T > & div(T0 scale, const Vec3< T1 > &v)
Definition: Vec3.h:189
Vec3< double > Vec3d
Definition: Vec3.h:605
const Vec3< T > & setZero()
Set "this" vector to zero.
Definition: Vec3.h:122
bool isRelOrApproxEqual(const Type &a, const Type &b, const Type &absTol, const Type &relTol)
Definition: Math.h:363
const Vec3< T > & init(T x=0, T y=0, T z=0)
Definition: Vec3.h:114
T & y()
Definition: Vec3.h:95
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
bool isExactlyEqual(const T0 &a, const T1 &b)
Return true if a is exactly equal to b.
Definition: Math.h:353
T & operator()(int i)
Alternative indexed reference to the elements.
Definition: Vec3.h:107
MatType scale(const Vec3< typename MatType::value_type > &scaling)
Definition: Mat.h:595
bool operator!=(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Inequality operator, does exact floating point comparisons.
Definition: Vec3.h:438
bool isFinite() const
True if all no Nan or Inf values present.
Definition: Vec3.h:418
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:408
const Vec3< T > & operator+=(S scalar)
Returns v, where for .
Definition: Vec3.h:289
const Vec3< T > & operator/=(const Vec3< S > &v1)
Returns v0, where for .
Definition: Vec3.h:279
Vec3(T val)
Constructor with one argument, e.g. Vec3f v(0);.
Definition: Vec3.h:57
const Vec3< T > & operator=(const Vec3< Source > &v)
Assignment operator.
Definition: Vec3.h:130
const Vec3< T > & scale(T0 scale, const Vec3< T1 > &v)
Definition: Vec3.h:179
const Vec3< T > & operator-=(const Vec3< S > &v1)
Returns v0, where for .
Definition: Vec3.h:319
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:56
bool isApproxEqual(const Hermite &lhs, const Hermite &rhs)
Definition: Hermite.h:470
const Vec3< T > & operator-=(S scalar)
Returns v, where for .
Definition: Vec3.h:309
Vec3< T > operator-() const
Negation operator, for e.g. v1 = -v2;.
Definition: Vec3.h:150
Vec3(T x, T y, T z)
Constructor with three arguments, e.g. Vec3d v(1,2,3);.
Definition: Vec3.h:60
Mat3< typename promote< T0, T1 >::type > operator*(const Mat3< T0 > &m0, const Mat3< T1 > &m1)
Matrix multiplication.
Definition: Mat3.h:608
T x() const
Get the component, e.g. float f = v.y();.
Definition: Vec3.h:99
const Vec3< T > & operator*=(S scalar)
Returns v, where for .
Definition: Vec3.h:249
Vec3(Source *a)
Constructor with array argument, e.g. double a[3]; Vec3d v(a);.
Definition: Vec3.h:69
T lengthSqr() const
Definition: Vec3.h:219
Vec3< T > cross(const Vec3< T > &v) const
Return the cross product of "this" vector and v;.
Definition: Vec3.h:228
T z() const
Definition: Vec3.h:101