31 #ifndef OPENVDB_MATH_VEC2_HAS_BEEN_INCLUDED 32 #define OPENVDB_MATH_VEC2_HAS_BEEN_INCLUDED 39 #include <type_traits> 47 template<
typename T>
class Mat2;
60 explicit Vec2(T val) { this->mm[0] = this->mm[1] = val; }
70 template <
typename Source>
78 template<
typename Source>
81 this->mm[0] = static_cast<T>(t[0]);
82 this->mm[1] = static_cast<T>(t[1]);
88 template<
typename Other>
92 this->mm[0] = this->mm[1] = static_cast<T>(val);
96 T&
x() {
return this->mm[0];}
97 T&
y() {
return this->mm[1];}
100 T
x()
const {
return this->mm[0];}
101 T
y()
const {
return this->mm[1];}
116 this->mm[0] = x; this->mm[1] = y;
123 this->mm[0] = 0; this->mm[1] = 0;
128 template<
typename Source>
148 bool eq(
const Vec2<T> &v, T eps = static_cast<T>(1.0e-7))
const 159 template <
typename T0,
typename T1>
162 this->mm[0] = v1[0] + v2[0];
163 this->mm[1] = v1[1] + v2[1];
170 template <
typename T0,
typename T1>
173 this->mm[0] = v1[0] - v2[0];
174 this->mm[1] = v1[1] - v2[1];
181 template <
typename T0,
typename T1>
184 this->mm[0] = scalar * v[0];
185 this->mm[1] = scalar * v[1];
190 template <
typename T0,
typename T1>
193 this->mm[0] = v[0] / scalar;
194 this->mm[1] = v[1] / scalar;
200 T
dot(
const Vec2<T> &v)
const {
return this->mm[0]*v[0] + this->mm[1]*v[1]; }
205 return static_cast<T>(sqrt(
double(this->mm[0]*this->mm[0] + this->mm[1]*this->mm[1])));
210 T
lengthSqr()
const {
return (this->mm[0]*this->mm[0] + this->mm[1]*this->mm[1]); }
216 this->mm[0] = std::exp(this->mm[0]);
217 this->mm[1] = std::exp(this->mm[1]);
225 this->mm[0] = std::log(this->mm[0]);
226 this->mm[1] = std::log(this->mm[1]);
233 return this->mm[0] + this->mm[1];
239 return this->mm[0] * this->mm[1];
274 return l2 ? *
this/static_cast<T>(sqrt(l2)) :
Vec2<T>(1,0);
278 template <
typename S>
281 this->mm[0] *= scalar;
282 this->mm[1] *= scalar;
287 template <
typename S>
290 this->mm[0] *= v1[0];
291 this->mm[1] *= v1[1];
296 template <
typename S>
299 this->mm[0] /= scalar;
300 this->mm[1] /= scalar;
305 template <
typename S>
308 this->mm[0] /= v1[0];
309 this->mm[1] /= v1[1];
314 template <
typename S>
317 this->mm[0] += scalar;
318 this->mm[1] += scalar;
323 template <
typename S>
326 this->mm[0] += v1[0];
327 this->mm[1] += v1[1];
332 template <
typename S>
335 this->mm[0] -= scalar;
336 this->mm[1] -= scalar;
341 template <
typename S>
344 this->mm[0] -= v1[0];
345 this->mm[1] -= v1[1];
361 return dot(onto)*(T(1)/l);
371 return onto*(dot(onto)*(T(1)/l));
386 template <
typename S,
typename T>
393 template <
typename S,
typename T>
402 template <
typename T0,
typename T1>
410 template <
typename S,
typename T>
417 template <
typename S,
typename T>
426 template <
typename T0,
typename T1>
434 template <
typename T0,
typename T1>
443 template <
typename S,
typename T>
452 template <
typename T0,
typename T1>
461 template <
typename S,
typename T>
471 template <
typename T>
478 template <
typename T>
484 template <
typename T>
501 template <
typename T>
529 template <
typename T>
538 template <
typename T>
548 template <
typename T>
553 template <
typename T>
565 #endif // OPENVDB_MATH_VEC2_HAS_BEEN_INCLUDED void orthonormalize(Vec2< T > &v1, Vec2< T > &v2)
Definition: Vec2.h:502
T * asPointer()
Definition: Vec2.h:109
T dot(const Vec2< T > &v) const
Dot product.
Definition: Vec2.h:200
const Vec2< T > & init(T x=0, T y=0)
Definition: Vec2.h:114
Vec2< typename promote< S, T >::type > operator+(const Vec2< T > &v, S scalar)
Add scalar to each element of the given vector and return the result.
Definition: Vec2.h:444
Vec2< T > getArbPerpendicular() const
Definition: Vec2.h:377
const Vec2< T > & scale(T0 scalar, const Vec2< T1 > &v)
Definition: Vec2.h:182
Vec2(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: Vec2.h:89
bool isApproxEqual(const Vec2< T > &a, const Vec2< T > &b, const Vec2< T > &eps)
Definition: Vec2.h:486
Vec2< typename promote< S, T >::type > operator-(const Vec2< T > &v, S scalar)
Subtract scalar from each element of the given vector and return the result.
Definition: Vec2.h:462
static Vec2< T > zero()
Predefined constants, e.g. Vec2f v = Vec2f::xNegAxis();.
Definition: Vec2.h:380
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
static Vec2< T > ones()
Definition: Vec2.h:381
T lengthSqr() const
Definition: Vec2.h:210
static unsigned numColumns()
Definition: Vec2.h:351
#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
const Vec2< T > & log()
Definition: Vec2.h:223
bool eq(const Vec2< 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: Vec2.h:148
Vec2< T > minComponent(const Vec2< T > &v1, const Vec2< T > &v2)
Return component-wise minimum of the two vectors.
Definition: Vec2.h:530
const Vec2< T > & operator/=(const Vec2< S > &v1)
Divide each element of this vector by the corresponding element of the given vector.
Definition: Vec2.h:306
Dummy class for tag dispatch of conversion constructors.
Definition: Tuple.h:49
T mm[SIZE]
Definition: Tuple.h:197
static unsigned numElements()
Definition: Vec2.h:352
T length() const
Length of the vector.
Definition: Vec2.h:203
Vec2< T > projection(const Vec2< T > &onto, T eps=static_cast< T >(1.0e-8)) const
Definition: Vec2.h:366
T ValueType
Definition: Vec2.h:54
Vec2(T x, T y)
Constructor with two arguments, e.g. Vec2f v(1,2,3);.
Definition: Vec2.h:63
Vec2< T > maxComponent(const Vec2< T > &v1, const Vec2< T > &v2)
Return component-wise maximum of the two vectors.
Definition: Vec2.h:539
bool operator!=(const Vec2< T > &v) const
Inequality operator, does exact floating point comparisons.
Definition: Vec2.h:145
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:136
static unsigned numRows()
Definition: Vec2.h:350
const Vec2< T > & operator-=(S scalar)
Subtract scalar from each element of this vector.
Definition: Vec2.h:333
const Vec2< T > & operator+=(const Vec2< S > &v1)
Add each element of the given vector to the corresponding element of this vector.
Definition: Vec2.h:324
Definition: Exceptions.h:40
T operator()(int i) const
Alternative indexed constant reference to the elements,.
Definition: Vec2.h:107
Vec2< typename promote< T0, T1 >::type > operator *(const Vec2< T0 > &v0, const Vec2< T1 > &v1)
Multiply corresponding elements of v0 and v1 and return the result.
Definition: Vec2.h:403
const Vec2< T > & setZero()
Set "this" vector to zero.
Definition: Vec2.h:121
Vec2< T > unitSafe() const
return normalized this, or (1, 0) if this is null vector
Definition: Vec2.h:271
const Vec2< T > & div(T0 scalar, const Vec2< T1 > &v)
Definition: Vec2.h:191
T component(const Vec2< T > &onto, T eps=static_cast< T >(1.0e-8)) const
Definition: Vec2.h:356
const Vec2< T > & operator=(const Vec2< Source > &v)
Assignment operator.
Definition: Vec2.h:129
Vec2< T > Abs(const Vec2< T > &v)
Definition: Vec2.h:494
const Vec2< T > & operator-=(const Vec2< S > &v1)
Subtract each element of the given vector from the corresponding element of this vector.
Definition: Vec2.h:342
T & operator()(int i)
Alternative indexed reference to the elements.
Definition: Vec2.h:104
Vec2(Source *a)
Constructor with array argument, e.g. float a[2]; Vec2f v(a);.
Definition: Vec2.h:71
Vec2< T > operator-() const
Negation operator, for e.g. v1 = -v2;.
Definition: Vec2.h:155
T & x()
Reference to the component, e.g. v.x() = 4.5f;.
Definition: Vec2.h:96
T x() const
Get the component, e.g. float f = v.y();.
Definition: Vec2.h:100
T product() const
Return the product of all the vector components.
Definition: Vec2.h:237
Vec2< typename promote< T0, T1 >::type > operator/(const Vec2< T0 > &v0, const Vec2< T1 > &v1)
Divide corresponding elements of v0 and v1 and return the result.
Definition: Vec2.h:427
T angle(const Vec2< T > &v1, const Vec2< T > &v2)
Definition: Vec2.h:472
Vec2()
Trivial constructor, the vector is NOT initialized.
Definition: Vec2.h:57
Vec2(const Tuple< 2, Source > &t)
Conversion constructor.
Definition: Vec2.h:79
Vec2< T > Exp(Vec2< T > v)
Return a vector with the exponent applied to each of the components of the input vector.
Definition: Vec2.h:549
Vec2< T > Log(Vec2< T > v)
Return a vector with log applied to each of the components of the input vector.
Definition: Vec2.h:554
const Vec2< T > & add(const Vec2< T0 > &v1, const Vec2< T1 > &v2)
Definition: Vec2.h:160
const Vec2< T > & operator+=(S scalar)
Add scalar to each element of this vector.
Definition: Vec2.h:315
bool operator==(const Vec2< T > &v) const
Equality operator, does exact floating point comparisons.
Definition: Vec2.h:139
const Vec2< T > & operator/=(S scalar)
Divide each element of this vector by scalar.
Definition: Vec2.h:297
Vec2(T val)
Construct a vector all of whose components have the given value.
Definition: Vec2.h:60
T sum() const
Return the sum of all the vector components.
Definition: Vec2.h:231
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:188
T & y()
Definition: Vec2.h:97
T value_type
Definition: Vec2.h:53
const Vec2< T > & exp()
Definition: Vec2.h:214
bool normalize(T eps=static_cast< T >(1.0e-8))
this = normalized this
Definition: Vec2.h:243
const T * asPointer() const
Definition: Vec2.h:110
T y() const
Definition: Vec2.h:101
Vec2< T > unit(T eps=0) const
return normalized this, throws if null vector
Definition: Vec2.h:254
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
Definition: Exceptions.h:83
Vec2< T > unit(T eps, T &len) const
return normalized this and length, throws if null vector
Definition: Vec2.h:261
const Vec2< T > & sub(const Vec2< T0 > &v1, const Vec2< T1 > &v2)
Definition: Vec2.h:171