31 #ifndef OPENVDB_MATH_VEC3_HAS_BEEN_INCLUDED 32 #define OPENVDB_MATH_VEC3_HAS_BEEN_INCLUDED 38 #include <type_traits> 46 template<
typename T>
class Mat3;
49 class Vec3:
public Tuple<3, T>
59 explicit Vec3(T val) { this->mm[0] = this->mm[1] = this->mm[2] = val; }
70 template <
typename Source>
80 template<
typename Source>
83 this->mm[0] =
static_cast<T
>(v[0]);
84 this->mm[1] =
static_cast<T
>(v[1]);
85 this->mm[2] =
static_cast<T
>(v[2]);
91 template<
typename Other>
95 this->mm[0] = this->mm[1] = this->mm[2] =
static_cast<T
>(val);
100 template<
typename Other>
103 this->mm[0] =
static_cast<T
>(v[0]);
104 this->mm[1] =
static_cast<T
>(v[1]);
105 this->mm[2] =
static_cast<T
>(v[2]);
109 T&
x() {
return this->mm[0]; }
110 T&
y() {
return this->mm[1]; }
111 T&
z() {
return this->mm[2]; }
114 T
x()
const {
return this->mm[0]; }
115 T
y()
const {
return this->mm[1]; }
116 T
z()
const {
return this->mm[2]; }
131 this->mm[0] = x; this->mm[1] = y; this->mm[2] = z;
139 this->mm[0] = 0; this->mm[1] = 0; this->mm[2] = 0;
145 template<
typename Source>
157 bool eq(
const Vec3<T> &v, T eps = static_cast<T>(1.0e-7))
const 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] = v1[0] - v2[0];
186 this->mm[1] = v1[1] - v2[1];
187 this->mm[2] = v1[2] - v2[2];
194 template <
typename T0,
typename T1>
197 this->mm[0] =
scale * v[0];
198 this->mm[1] =
scale * v[1];
199 this->mm[2] =
scale * v[2];
204 template <
typename T0,
typename T1>
207 this->mm[0] = v[0] /
scale;
208 this->mm[1] = v[1] /
scale;
209 this->mm[2] = v[2] /
scale;
218 this->mm[0]*v.mm[0] +
219 this->mm[1]*v.mm[1] +
226 return static_cast<T
>(sqrt(
double(
227 this->mm[0]*this->mm[0] +
228 this->mm[1]*this->mm[1] +
229 this->mm[2]*this->mm[2])));
238 this->mm[0]*this->mm[0] +
239 this->mm[1]*this->mm[1] +
240 this->mm[2]*this->mm[2];
246 return Vec3<T>(this->mm[1]*v.mm[2] - this->mm[2]*v.mm[1],
247 this->mm[2]*v.mm[0] - this->mm[0]*v.mm[2],
248 this->mm[0]*v.mm[1] - this->mm[1]*v.mm[0]);
257 this->mm[0] = v1.mm[1]*v2.mm[2] - v1.mm[2]*v2.mm[1];
258 this->mm[1] = v1.mm[2]*v2.mm[0] - v1.mm[0]*v2.mm[2];
259 this->mm[2] = v1.mm[0]*v2.mm[1] - v1.mm[1]*v2.mm[0];
264 template <
typename S>
267 this->mm[0] =
static_cast<T
>(this->mm[0] * scalar);
268 this->mm[1] =
static_cast<T
>(this->mm[1] * scalar);
269 this->mm[2] =
static_cast<T
>(this->mm[2] * scalar);
274 template <
typename S>
277 this->mm[0] *= v1[0];
278 this->mm[1] *= v1[1];
279 this->mm[2] *= v1[2];
284 template <
typename S>
287 this->mm[0] /= scalar;
288 this->mm[1] /= scalar;
289 this->mm[2] /= scalar;
294 template <
typename S>
297 this->mm[0] /= v1[0];
298 this->mm[1] /= v1[1];
299 this->mm[2] /= v1[2];
304 template <
typename S>
307 this->mm[0] =
static_cast<T
>(this->mm[0] + scalar);
308 this->mm[1] =
static_cast<T
>(this->mm[1] + scalar);
309 this->mm[2] =
static_cast<T
>(this->mm[2] + scalar);
314 template <
typename S>
317 this->mm[0] += v1[0];
318 this->mm[1] += v1[1];
319 this->mm[2] += v1[2];
324 template <
typename S>
327 this->mm[0] -= scalar;
328 this->mm[1] -= scalar;
329 this->mm[2] -= scalar;
334 template <
typename S>
337 this->mm[0] -= v1[0];
338 this->mm[1] -= v1[1];
339 this->mm[2] -= v1[2];
347 this->mm[0] = std::exp(this->mm[0]);
348 this->mm[1] = std::exp(this->mm[1]);
349 this->mm[2] = std::exp(this->mm[2]);
357 this->mm[0] = std::log(this->mm[0]);
358 this->mm[1] = std::log(this->mm[1]);
359 this->mm[2] = std::log(this->mm[2]);
366 return this->mm[0] + this->mm[1] + this->mm[2];
372 return this->mm[0] * this->mm[1] * this->mm[2];
408 return l2 ? *
this /
static_cast<T
>(sqrt(l2)) :
Vec3<T>(1, 0 ,0);
423 return dot(onto)*(T(1)/l);
430 T l = onto.lengthSqr();
433 return onto*(dot(onto)*(T(1)/l));
444 if ( fabs(this->mm[0]) >= fabs(this->mm[1]) ) {
446 l = this->mm[0]*this->mm[0] + this->mm[2]*this->mm[2];
447 l =
static_cast<T
>(T(1)/sqrt(
double(l)));
448 u.mm[0] = -this->mm[2]*l;
450 u.mm[2] = +this->mm[0]*l;
453 l = this->mm[1]*this->mm[1] + this->mm[2]*this->mm[2];
454 l =
static_cast<T
>(T(1)/sqrt(
double(l)));
456 u.mm[1] = +this->mm[2]*l;
457 u.mm[2] = -this->mm[1]*l;
467 if( r.mm[0] > r.mm[1] ) std::swap(r.mm[0], r.mm[1]);
468 if( r.mm[1] > r.mm[2] ) std::swap(r.mm[1], r.mm[2]);
469 if( r.mm[0] > r.mm[1] ) std::swap(r.mm[0], r.mm[1]);
476 return Vec3<T>(this->mm[2], this->mm[1], this->mm[0]);
486 template <
typename T0,
typename T1>
494 template <
typename T0,
typename T1>
498 template <
typename S,
typename T>
502 template <
typename S,
typename T>
511 template <
typename T0,
typename T1>
520 template <
typename S,
typename T>
527 template <
typename S,
typename T>
536 template <
typename T0,
typename T1>
544 template <
typename T0,
typename T1>
553 template <
typename S,
typename T>
562 template <
typename T0,
typename T1>
571 template <
typename S,
typename T>
581 template <
typename T>
585 return static_cast<T
>(atan2(c.length(), v1.dot(v2)));
588 template <
typename T>
594 template <
typename T>
612 template <
typename T>
646 template <
typename T>
656 template <
typename T>
667 template <
typename T>
672 template <
typename T>
684 #endif // OPENVDB_MATH_VEC3_HAS_BEEN_INCLUDED Vec3< typename promote< S, T >::type > operator-(const Vec3< T > &v, S scalar)
Returns V, where for .
Definition: Vec3.h:572
const Vec3< T > & add(const Vec3< T0 > &v1, const Vec3< T1 > &v2)
Definition: Vec3.h:171
T angle(const Vec3< T > &v1, const Vec3< T > &v2)
Definition: Vec3.h:582
Vec3(Source *a)
Constructor with array argument, e.g. double a[3]; Vec3d v(a);.
Definition: Vec3.h:71
Vec3(const Vec3< Other > &v)
Construct a Vec3 from another Vec3 with a possibly different value type.
Definition: Vec3.h:101
Dummy class for tag dispatch of conversion constructors.
Definition: Tuple.h:48
const Vec3< T > & log()
Definition: Vec3.h:355
bool operator!=(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Inequality operator, does exact floating point comparisons.
Definition: Vec3.h:495
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
T dot(const Vec3< T > &v) const
Dot product.
Definition: Vec3.h:215
Vec3< typename promote< T0, T1 >::type > operator*(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Returns V, where for .
Definition: Vec3.h:512
T & z()
Definition: Vec3.h:111
static unsigned numElements()
Definition: Vec3.h:414
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:101
bool isExactlyEqual(const T0 &a, const T1 &b)
Return true if a is exactly equal to b.
Definition: Math.h:391
Vec3< T > projection(const Vec3< T > &onto, T eps=static_cast< T >(1.0e-7)) const
Definition: Vec3.h:428
static unsigned numColumns()
Definition: Vec3.h:413
tbb::atomic< Index32 > i
Definition: LeafBuffer.h:71
Definition: Exceptions.h:82
const Vec3< T > & operator+=(const Vec3< S > &v1)
Returns v0, where for .
Definition: Vec3.h:315
bool isRelOrApproxEqual(const Type &a, const Type &b, const Type &absTol, const Type &relTol)
Definition: Math.h:401
T value_type
Definition: Vec3.h:52
void orthonormalize(Vec3< T > &v1, Vec3< T > &v2, Vec3< T > &v3)
Definition: Vec3.h:613
Vec3< T > unit(T eps=0) const
return normalized this, throws if null vector
Definition: Vec3.h:388
static Vec3< T > ones()
Definition: Vec3.h:481
Vec3< int32_t > Vec3i
Definition: Vec3.h:675
static unsigned numRows()
Definition: Vec3.h:412
Vec3< float > Vec3s
Definition: Vec3.h:677
const Vec3< T > & operator/=(S scalar)
Returns v, where for .
Definition: Vec3.h:285
MatType scale(const Vec3< typename MatType::value_type > &s)
Return a matrix that scales by s.
Definition: Mat.h:643
const Vec3< T > & sub(const Vec3< T0 > &v1, const Vec3< T1 > &v2)
Definition: Vec3.h:183
T ValueType
Definition: Vec3.h:53
bool isApproxEqual(const Vec3< T > &a, const Vec3< T > &b, const Vec3< T > &eps)
Definition: Vec3.h:596
T * asPointer()
Definition: Vec3.h:118
#define OPENVDB_VERSION_NAME
Definition: version.h:43
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:668
Vec3< T > reversed() const
Return the vector (z, y, x)
Definition: Vec3.h:474
Vec3< uint32_t > Vec3ui
Definition: Vec3.h:676
T x() const
Get the component, e.g. float f = v.y();.
Definition: Vec3.h:114
const Vec3< T > & div(T0 scale, const Vec3< T1 > &v)
Definition: Vec3.h:205
Vec3(const Tuple< 3, Source > &v)
Construct a Vec3 from a 3-Tuple with a possibly different value type.
Definition: Vec3.h:81
Vec3()
Trivial constructor, the vector is NOT initialized.
Definition: Vec3.h:56
const Vec3< T > & operator*=(const Vec3< S > &v1)
Returns v0, where for .
Definition: Vec3.h:275
Vec3< T > Log(Vec3< T > v)
Return a vector with log applied to each of the components of the input vector.
Definition: Vec3.h:673
bool normalize(T eps=T(1.0e-7))
this = normalized this
Definition: Vec3.h:376
Definition: Exceptions.h:39
static Vec3< T > zero()
Predefined constants, e.g. Vec3d v = Vec3d::xNegAxis();.
Definition: Vec3.h:480
bool operator==(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Equality operator, does exact floating point comparisons.
Definition: Vec3.h:487
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:253
Vec3< T > cross(const Vec3< T > &v) const
Return the cross product of "this" vector and v;.
Definition: Vec3.h:244
T component(const Vec3< T > &onto, T eps=static_cast< T >(1.0e-7)) const
Definition: Vec3.h:418
T & x()
Reference to the component, e.g. v.x() = 4.5f;.
Definition: Vec3.h:109
Vec3< double > Vec3d
Definition: Vec3.h:678
T & operator()(int i)
Alternative indexed reference to the elements.
Definition: Vec3.h:122
T & y()
Definition: Vec3.h:110
T sum() const
Return the sum of all the vector components.
Definition: Vec3.h:364
Vec3< typename promote< T0, T1 >::type > operator/(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Returns V, where for .
Definition: Vec3.h:537
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:157
const Vec3< T > & init(T x=0, T y=0, T z=0)
Definition: Vec3.h:129
Vec3< T > getArbPerpendicular() const
Definition: Vec3.h:439
T length() const
Length of the vector.
Definition: Vec3.h:224
const Vec3< T > & setZero()
Set "this" vector to zero.
Definition: Vec3.h:137
Vec3< T > sorted() const
Return a vector with the components of this in ascending order.
Definition: Vec3.h:464
T z() const
Definition: Vec3.h:116
const T * asPointer() const
Definition: Vec3.h:119
Vec3< T > operator-() const
Negation operator, for e.g. v1 = -v2;.
Definition: Vec3.h:166
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:676
T product() const
Return the product of all the vector components.
Definition: Vec3.h:370
T operator()(int i) const
Alternative indexed constant reference to the elements,.
Definition: Vec3.h:125
const Vec3< T > & operator=(const Vec3< Source > &v)
Assignment operator.
Definition: Vec3.h:146
const Vec3< T > & exp()
Definition: Vec3.h:345
const Vec3< T > & operator-=(const Vec3< S > &v1)
Returns v0, where for .
Definition: Vec3.h:335
Vec3< T > Abs(const Vec3< T > &v)
Definition: Vec3.h:605
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:71
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:92
Vec3(T val)
Construct a vector all of whose components have the given value.
Definition: Vec3.h:59
const Vec3< T > & operator+=(S scalar)
Returns v, where for .
Definition: Vec3.h:305
Vec3< T > minComponent(const Vec3< T > &v1, const Vec3< T > &v2)
Return component-wise minimum of the two vectors.
Definition: Vec3.h:647
const Vec3< T > & scale(T0 scale, const Vec3< T1 > &v)
Definition: Vec3.h:195
T y() const
Definition: Vec3.h:115
Vec3(T x, T y, T z)
Constructor with three arguments, e.g. Vec3d v(1,2,3);.
Definition: Vec3.h:62
T lengthSqr() const
Definition: Vec3.h:235
const Vec3< T > & operator/=(const Vec3< S > &v1)
Returns v0, where for .
Definition: Vec3.h:295
const Vec3< T > & operator*=(S scalar)
Returns v, where for .
Definition: Vec3.h:265
Vec3< T > unitSafe() const
return normalized this, or (1, 0, 0) if this is null vector
Definition: Vec3.h:405
const Vec3< T > & operator-=(S scalar)
Returns v, where for .
Definition: Vec3.h:325
Vec3< T > maxComponent(const Vec3< T > &v1, const Vec3< T > &v2)
Return component-wise maximum of the two vectors.
Definition: Vec3.h:657
Vec3< typename promote< S, T >::type > operator+(const Vec3< T > &v, S scalar)
Returns V, where for .
Definition: Vec3.h:554
Vec3< T > unit(T eps, T &len) const
return normalized this and length, throws if null vector
Definition: Vec3.h:395