31 #ifndef OPENVDB_MATH_VEC3_HAS_BEEN_INCLUDED 32 #define OPENVDB_MATH_VEC3_HAS_BEEN_INCLUDED 39 #include <type_traits> 47 template<
typename T>
class Mat3;
50 class Vec3:
public Tuple<3, T>
60 explicit Vec3(T val) { this->mm[0] = this->mm[1] = this->mm[2] = val; }
71 template <
typename Source>
81 template<
typename Source>
84 this->mm[0] = static_cast<T>(v[0]);
85 this->mm[1] = static_cast<T>(v[1]);
86 this->mm[2] = static_cast<T>(v[2]);
92 template<
typename Other>
96 this->mm[0] = this->mm[1] = this->mm[2] = static_cast<T>(val);
101 template<
typename Other>
104 this->mm[0] = static_cast<T>(v[0]);
105 this->mm[1] = static_cast<T>(v[1]);
106 this->mm[2] = static_cast<T>(v[2]);
110 T&
x() {
return this->mm[0]; }
111 T&
y() {
return this->mm[1]; }
112 T&
z() {
return this->mm[2]; }
115 T
x()
const {
return this->mm[0]; }
116 T
y()
const {
return this->mm[1]; }
117 T
z()
const {
return this->mm[2]; }
132 this->mm[0] = x; this->mm[1] = y; this->mm[2] = z;
140 this->mm[0] = 0; this->mm[1] = 0; this->mm[2] = 0;
146 template<
typename Source>
158 bool eq(
const Vec3<T> &v, T eps = static_cast<T>(1.0e-7))
const 171 template <
typename T0,
typename T1>
174 this->mm[0] = v1[0] + v2[0];
175 this->mm[1] = v1[1] + v2[1];
176 this->mm[2] = v1[2] + v2[2];
183 template <
typename T0,
typename T1>
186 this->mm[0] = v1[0] - v2[0];
187 this->mm[1] = v1[1] - v2[1];
188 this->mm[2] = v1[2] - v2[2];
195 template <
typename T0,
typename T1>
198 this->mm[0] =
scale * v[0];
199 this->mm[1] =
scale * v[1];
200 this->mm[2] =
scale * v[2];
205 template <
typename T0,
typename T1>
208 this->mm[0] = v[0] /
scale;
209 this->mm[1] = v[1] /
scale;
210 this->mm[2] = v[2] /
scale;
219 this->mm[0]*v.mm[0] +
220 this->mm[1]*v.mm[1] +
227 return static_cast<T>(sqrt(
double(
228 this->mm[0]*this->mm[0] +
229 this->mm[1]*this->mm[1] +
230 this->mm[2]*this->mm[2])));
239 this->mm[0]*this->mm[0] +
240 this->mm[1]*this->mm[1] +
241 this->mm[2]*this->mm[2];
247 return Vec3<T>(this->mm[1]*v.mm[2] - this->mm[2]*v.mm[1],
248 this->mm[2]*v.mm[0] - this->mm[0]*v.mm[2],
249 this->mm[0]*v.mm[1] - this->mm[1]*v.mm[0]);
258 this->mm[0] = v1.mm[1]*v2.mm[2] - v1.mm[2]*v2.mm[1];
259 this->mm[1] = v1.mm[2]*v2.mm[0] - v1.mm[0]*v2.mm[2];
260 this->mm[2] = v1.mm[0]*v2.mm[1] - v1.mm[1]*v2.mm[0];
265 template <
typename S>
268 this->mm[0] = static_cast<T>(this->mm[0] * scalar);
269 this->mm[1] = static_cast<T>(this->mm[1] * scalar);
270 this->mm[2] = static_cast<T>(this->mm[2] * scalar);
275 template <
typename S>
278 this->mm[0] *= v1[0];
279 this->mm[1] *= v1[1];
280 this->mm[2] *= v1[2];
285 template <
typename S>
288 this->mm[0] /= scalar;
289 this->mm[1] /= scalar;
290 this->mm[2] /= scalar;
295 template <
typename S>
298 this->mm[0] /= v1[0];
299 this->mm[1] /= v1[1];
300 this->mm[2] /= v1[2];
305 template <
typename S>
308 this->mm[0] = static_cast<T>(this->mm[0] + scalar);
309 this->mm[1] = static_cast<T>(this->mm[1] + scalar);
310 this->mm[2] = static_cast<T>(this->mm[2] + scalar);
315 template <
typename S>
318 this->mm[0] += v1[0];
319 this->mm[1] += v1[1];
320 this->mm[2] += v1[2];
325 template <
typename S>
328 this->mm[0] -= scalar;
329 this->mm[1] -= scalar;
330 this->mm[2] -= scalar;
335 template <
typename S>
338 this->mm[0] -= v1[0];
339 this->mm[1] -= v1[1];
340 this->mm[2] -= v1[2];
348 this->mm[0] = std::exp(this->mm[0]);
349 this->mm[1] = std::exp(this->mm[1]);
350 this->mm[2] = std::exp(this->mm[2]);
358 this->mm[0] = std::log(this->mm[0]);
359 this->mm[1] = std::log(this->mm[1]);
360 this->mm[2] = std::log(this->mm[2]);
367 return this->mm[0] + this->mm[1] + this->mm[2];
373 return this->mm[0] * this->mm[1] * this->mm[2];
409 return l2 ? *
this / static_cast<T>(sqrt(l2)) :
Vec3<T>(1, 0 ,0);
424 return dot(onto)*(T(1)/l);
431 T l = onto.lengthSqr();
434 return onto*(dot(onto)*(T(1)/l));
445 if ( fabs(this->mm[0]) >= fabs(this->mm[1]) ) {
447 l = this->mm[0]*this->mm[0] + this->mm[2]*this->mm[2];
448 l = static_cast<T>(T(1)/sqrt(
double(l)));
449 u.mm[0] = -this->mm[2]*l;
451 u.mm[2] = +this->mm[0]*l;
454 l = this->mm[1]*this->mm[1] + this->mm[2]*this->mm[2];
455 l = static_cast<T>(T(1)/sqrt(
double(l)));
457 u.mm[1] = +this->mm[2]*l;
458 u.mm[2] = -this->mm[1]*l;
468 if( r.mm[0] > r.mm[1] ) std::swap(r.mm[0], r.mm[1]);
469 if( r.mm[1] > r.mm[2] ) std::swap(r.mm[1], r.mm[2]);
470 if( r.mm[0] > r.mm[1] ) std::swap(r.mm[0], r.mm[1]);
477 return Vec3<T>(this->mm[2], this->mm[1], this->mm[0]);
487 template <
typename T0,
typename T1>
495 template <
typename T0,
typename T1>
499 template <
typename S,
typename T>
503 template <
typename S,
typename T>
512 template <
typename T0,
typename T1>
521 template <
typename S,
typename T>
528 template <
typename S,
typename T>
537 template <
typename T0,
typename T1>
545 template <
typename T0,
typename T1>
554 template <
typename S,
typename T>
563 template <
typename T0,
typename T1>
572 template <
typename S,
typename T>
582 template <
typename T>
586 return static_cast<T>(atan2(c.length(), v1.dot(v2)));
589 template <
typename T>
595 template <
typename T>
613 template <
typename T>
647 template <
typename T>
657 template <
typename T>
668 template <
typename T>
673 template <
typename T>
685 #endif // OPENVDB_MATH_VEC3_HAS_BEEN_INCLUDED Vec3< T > Abs(const Vec3< T > &v)
Definition: Vec3.h:606
Real value_type
Definition: Vec3.h:53
T dot(const Vec3< T > &v) const
Dot product.
Definition: Vec3.h:216
bool normalize(T eps=T(1.0e-7))
this = normalized this
Definition: Vec3.h:377
static unsigned numElements()
Definition: Vec3.h:415
static unsigned numColumns()
Definition: Vec3.h:414
const Vec3< T > & scale(T0 scale, const Vec3< T1 > &v)
Definition: Vec3.h:196
bool isApproxEqual(const Vec3< T > &a, const Vec3< T > &b, const Vec3< T > &eps)
Definition: Vec3.h:597
Real ValueType
Definition: Vec3.h:54
T y() const
Definition: Vec3.h:116
const Vec3< T > & sub(const Vec3< T0 > &v1, const Vec3< T1 > &v2)
Definition: Vec3.h:184
Vec3< typename promote< S, T >::type > operator+(const Vec3< T > &v, S scalar)
Add scalar to each element of the given vector and return the result.
Definition: Vec3.h:555
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
T z() const
Definition: Vec3.h:117
Vec3(const Vec3< Other > &v)
Construct a Vec3 from another Vec3 with a possibly different value type.
Definition: Vec3.h:102
T sum() const
Return the sum of all the vector components.
Definition: Vec3.h:365
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:109
bool isExactlyEqual(const T0 &a, const T1 &b)
Return true if a is exactly equal to b.
Definition: Math.h:395
Vec3< T > sorted() const
Return a vector with the components of this in ascending order.
Definition: Vec3.h:465
Vec3(const Tuple< 3, Source > &v)
Construct a Vec3 from a 3-Tuple with a possibly different value type.
Definition: Vec3.h:82
Vec3< T > cross(const Vec3< T > &v) const
Return the cross product of "this" vector and v;.
Definition: Vec3.h:245
const Vec3< T > & operator=(const Vec3< Source > &v)
Assignment operator.
Definition: Vec3.h:147
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:254
Dummy class for tag dispatch of conversion constructors.
Definition: Tuple.h:49
bool isRelOrApproxEqual(const Type &a, const Type &b, const Type &absTol, const Type &relTol)
Definition: Math.h:405
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:93
Vec3(T x, T y, T z)
Constructor with three arguments, e.g. Vec3d v(1,2,3);.
Definition: Vec3.h:63
const Vec3< T > & div(T0 scale, const Vec3< T1 > &v)
Definition: Vec3.h:206
Vec3< T > unit(T eps, T &len) const
return normalized this and length, throws if null vector
Definition: Vec3.h:396
T * asPointer()
Definition: Vec3.h:119
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:336
const Vec3< T > & operator/=(S scalar)
Divide each element of this vector by scalar.
Definition: Vec3.h:286
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:158
T operator()(int i) const
Alternative indexed constant reference to the elements,.
Definition: Vec3.h:126
Vec3< T > getArbPerpendicular() const
Definition: Vec3.h:440
bool operator==(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Equality operator, does exact floating point comparisons.
Definition: Vec3.h:488
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:136
T lengthSqr() const
Definition: Vec3.h:236
const Vec3< T > & setZero()
Set "this" vector to zero.
Definition: Vec3.h:138
Vec3< typename promote< T0, T1 >::type > operator/(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Divide corresponding elements of v0 and v1 and return the result.
Definition: Vec3.h:538
Vec3< typename promote< T0, T1 >::type > operator *(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Multiply corresponding elements of v0 and v1 and return the result.
Definition: Vec3.h:513
T & z()
Definition: Vec3.h:112
T & y()
Definition: Vec3.h:111
Definition: Exceptions.h:40
const Vec3< T > & operator-=(S scalar)
Subtract scalar from each element of this vector.
Definition: Vec3.h:326
MatType scale(const Vec3< typename MatType::value_type > &s)
Return a matrix that scales by s.
Definition: Mat.h:647
static Vec3< T > ones()
Definition: Vec3.h:482
Vec3< T > projection(const Vec3< T > &onto, T eps=static_cast< T >(1.0e-7)) const
Definition: Vec3.h:429
T length() const
Length of the vector.
Definition: Vec3.h:225
Vec3(T val)
Construct a vector all of whose components have the given value.
Definition: Vec3.h:60
Vec3< T > reversed() const
Return the vector (z, y, x)
Definition: Vec3.h:475
Vec3< T > minComponent(const Vec3< T > &v1, const Vec3< T > &v2)
Return component-wise minimum of the two vectors.
Definition: Vec3.h:648
Vec3(Source *a)
Constructor with array argument, e.g. double a[3]; Vec3d v(a);.
Definition: Vec3.h:72
const Vec3< T > & init(T x=0, T y=0, T z=0)
Definition: Vec3.h:130
const Vec3< T > & log()
Definition: Vec3.h:356
const T * asPointer() const
Definition: Vec3.h:120
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:316
Vec3< T > unitSafe() const
return normalized this, or (1, 0, 0) if this is null vector
Definition: Vec3.h:406
T angle(const Vec3< T > &v1, const Vec3< T > &v2)
Definition: Vec3.h:583
static unsigned numRows()
Definition: Vec3.h:413
Vec3< typename promote< S, T >::type > operator-(const Vec3< T > &v, S scalar)
Subtract scalar from each element of the given vector and return the result.
Definition: Vec3.h:573
Vec3< T > unit(T eps=0) const
return normalized this, throws if null vector
Definition: Vec3.h:389
void orthonormalize(Vec3< T > &v1, Vec3< T > &v2, Vec3< T > &v3)
Definition: Vec3.h:614
T x() const
Get the component, e.g. float f = v.y();.
Definition: Vec3.h:115
T product() const
Return the product of all the vector components.
Definition: Vec3.h:371
const Vec3< T > & operator+=(S scalar)
Add scalar to each element of this vector.
Definition: Vec3.h:306
Vec3()
Trivial constructor, the vector is NOT initialized.
Definition: Vec3.h:57
Vec3< T > maxComponent(const Vec3< T > &v1, const Vec3< T > &v2)
Return component-wise maximum of the two vectors.
Definition: Vec3.h:658
const Vec3< T > & exp()
Definition: Vec3.h:346
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:188
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:669
Vec3< T > Log(Vec3< T > v)
Return a vector with log applied to each of the components of the input vector.
Definition: Vec3.h:674
const Vec3< T > & add(const Vec3< T0 > &v1, const Vec3< T1 > &v2)
Definition: Vec3.h:172
Vec3< T > operator-() const
Negation operator, for e.g. v1 = -v2;.
Definition: Vec3.h:167
T & operator()(int i)
Alternative indexed reference to the elements.
Definition: Vec3.h:123
MatType unit(const MatType &mat, typename MatType::value_type eps=1.0e-8)
Return a copy of the given matrix with its upper 3×3 rows normalized.
Definition: Mat.h:680
T component(const Vec3< T > &onto, T eps=static_cast< T >(1.0e-7)) const
Definition: Vec3.h:419
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:296
bool operator!=(const Vec3< T0 > &v0, const Vec3< T1 > &v1)
Inequality operator, does exact floating point comparisons.
Definition: Vec3.h:496
static Vec3< T > zero()
Predefined constants, e.g. Vec3d v = Vec3d::xNegAxis();.
Definition: Vec3.h:481
Definition: Exceptions.h:83
T & x()
Reference to the component, e.g. v.x() = 4.5f;.
Definition: Vec3.h:110