Vec2¶
The Vec2
class template represents a 2D vector, with predefined
typedefs for vectors of type short
, int
, int64_t
, float
, and
double
.
Note that the integer specializations of Vec2
lack the
length()
and normalize()
methods that are present in the
float
and double
versions, because the results don’t fit into
integer quantities.
There are also various utility functions that operate on vectors
defined in ImathVecAlgo.h
and described in Vector Functions.
Individual components of a vector V
may be referenced as either V[i]
or V.x
, V.y
. Obviously, the []
notation is more
suited to looping over components, or in cases where a variable determines
which coordinate is needed. However, when the coordinate is known, it can be
more efficient to directly address the components, such as V.y
rather than
V[1]
. While both appear to do the same thing (and indeed do generate the
same machine operations for ordinary scalar code), when used inside loops that
you hope to parallelize (either through compiler auto-vectorization or
explicit hints such as #pragma omp simd
), the function call and
pointer casting of operator[]
can confuse the compiler just enough to
prevent vectorization of the loop.
Example:
#include <Imath/ImathVec.h>
#include <cassert>
void
vec2_example()
{
Imath::V2f a (1.0f, 2.0f);
Imath::V2f b; // b is uninitialized
b.x = a[0];
b.y = a[1];
assert (a == b);
assert (a.length() == sqrt (a ^ a));
a.normalize();
assert (Imath::equalWithAbsError (a.length(), 1.0f, 1e-6f));
}
-
template<class
T
>
classVec2
¶ 2-element vector
Constructors and Assignment
-
Vec2
()¶ Uninitialized by default.
-
constexpr
Vec2
(T a)¶ Initialize to a scalar
(a,a)
-
constexpr
Vec2
(T a, T b)¶ Initialize to given elements
(a,b)
-
constexpr const Vec2<T> &
operator=
(const Vec2 &v)¶ Assignment.
-
~Vec2
()¶ Destructor.
Compatibility with Sb
-
T *
getValue
()¶ Return a raw pointer to the array of values.
-
const T *
getValue
() const¶ Return a raw pointer to the array of values.
Arithmetic and Comparison
-
constexpr bool
equalWithAbsError
(const Vec2<T> &v, T e) const¶ Compare two matrices and test if they are “approximately equal”:
- Return
- True if the coefficients of this and
m
are the same with an absolute error of no more than e, i.e., for all i, j:abs (this[i][j] - m[i][j]) <= e
-
constexpr bool
equalWithRelError
(const Vec2<T> &v, T e) const¶ Compare two matrices and test if they are “approximately equal”:
- Return
- True if the coefficients of this and m are the same with a relative error of no more than e, i.e., for all i, j:
abs (this[i] - v[i][j]) <= e * abs (this[i][j])
-
constexpr T
dot
(const Vec2 &v) const¶ Dot product.
-
constexpr T
operator^
(const Vec2 &v) const¶ Dot product.
-
constexpr T
cross
(const Vec2 &v) const¶ Right-handed cross product, i.e.
z component of Vec3 (this->x, this->y, 0) % Vec3 (v.x, v.y, 0)
-
constexpr T
operator%
(const Vec2 &v) const¶ Right-handed cross product, i.e.
z component of Vec3 (this->x, this->y, 0) % Vec3 (v.x, v.y, 0)
-
constexpr const Vec2<T> &
operator+=
(const Vec2 &v)¶ Component-wise addition.
-
constexpr Vec2<T>
operator+
(const Vec2 &v) const¶ Component-wise addition.
-
constexpr const Vec2<T> &
operator-=
(const Vec2 &v)¶ Component-wise subtraction.
-
constexpr Vec2<T>
operator-
(const Vec2 &v) const¶ Component-wise subtraction.
-
constexpr Vec2<T>
operator-
() const¶ Component-wise multiplication by -1.
-
constexpr const Vec2<T> &
negate
()¶ Component-wise multiplication by -1.
-
constexpr const Vec2<T> &
operator*=
(const Vec2 &v)¶ Component-wise multiplication.
-
constexpr const Vec2<T> &
operator*=
(T a)¶ Component-wise multiplication.
-
constexpr Vec2<T>
operator*
(const Vec2 &v) const¶ Component-wise multiplication.
-
constexpr Vec2<T>
operator*
(T a) const¶ Component-wise multiplication.
-
constexpr const Vec2<T> &
operator/=
(const Vec2 &v)¶ Component-wise division.
-
constexpr const Vec2<T> &
operator/=
(T a)¶ Component-wise division.
-
constexpr Vec2<T>
operator/
(const Vec2 &v) const¶ Component-wise division.
-
constexpr Vec2<T>
operator/
(T a) const¶ Component-wise division.
Query and Manipulation
-
T
length
() const¶ Return the Euclidean norm.
-
constexpr T
length2
() const¶ Return the square of the Euclidean norm, i.e.
the dot product with itself.
-
const Vec2<T> &
normalizeNonNull
()¶ Normalize without any checks for length()==0.
Slightly faster than the other normalization routines, but if v.length() is 0.0, the result is undefined.
-
Vec2<T>
normalized
() const¶ Return a normalized vector. Does not modify *this.
Numeric Limits
-
static constexpr T
baseTypeLowest
()¶ Largest possible negative value.
-
static constexpr T
baseTypeMax
()¶ Largest possible positive value.
-
static constexpr T
baseTypeSmallest
()¶ Smallest possible positive value.
-
static constexpr T
baseTypeEpsilon
()¶ Smallest possible e for which 1+e != 1.
Public Types
-
typedef T
BaseType
¶ The base type: In templates that accept a parameter
V
, you can refer toT
asV::BaseType
Public Functions
-
Doxygen_Suppress constexpr T & Imath::Vec2::operator[](int i)
Element access by index.
-
constexpr const T &
operator[]
(int i) const¶ Element access by index.
Public Static Functions
-
static constexpr unsigned int
dimensions
()¶ Return the number of dimensions, i.e. 2.
-
Warning
doxygenfunction: Unable to resolve multiple matches for function “operator<<” with arguments (std::ostream& s, const Vec2<T>& v) in doxygen xml output for project “Imath” from directory: /build/ilmbase-pisqTC/ilmbase-3.1.11/obj-x86_64-linux-gnu/website/doxygen/xml. Potential matches:
- std::ostream &operator<<(std::ostream&, Imath::half)
- template<class T>
std::ostream &Imath::operator<<(std::ostream&, const Color4<T>&)
- template<class T>
std::ostream &Imath::operator<<(std::ostream&, const Euler<T>&)
- template<class T>
std::ostream &Imath::operator<<(std::ostream&, const Interval<T>&)
- template<class T>
std::ostream &Imath::operator<<(std::ostream&, const Line3<T>&)
- template<class T>
std::ostream &Imath::operator<<(std::ostream&, const Matrix22<T>&)
- template<class T>
std::ostream &Imath::operator<<(std::ostream&, const Matrix33<T>&)
- template<class T>
std::ostream &Imath::operator<<(std::ostream&, const Matrix44<T>&)
- template<class T>
std::ostream &Imath::operator<<(std::ostream&, const Plane3<T>&)
- template<class T>
std::ostream &Imath::operator<<(std::ostream&, const Quat<T>&)
- template<class T>
std::ostream &Imath::operator<<(std::ostream&, const Shear6<T>&)
- template<class T>
std::ostream &Imath::operator<<(std::ostream&, const Vec2<T>&)
- template<class T>
std::ostream &Imath::operator<<(std::ostream&, const Vec3<T>&)
- template<class T>
std::ostream &Imath::operator<<(std::ostream&, const Vec4<T>&)