Vec2¶
#include <Imath/ImathVec.h>
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>
class Vec2¶ 2-element vector
Constructors and Assignment
-
inline Vec2() noexcept¶
Uninitialized by default.
-
template<class S>
inline constexpr Vec2(const Vec2<S> &v) noexcept¶ Construct from Vec2 of another base type.
-
~Vec2() noexcept = default¶
Destructor.
Compatibility with Sb
Arithmetic and Comparison
-
inline bool equalWithAbsError(const Vec2<T> &v, T e) const noexcept¶
Compare two vectors and test if they are “approximately equal”:
- Returns:
True if the components of this and
vare the same with an absolute error of no more than e, i.e., for all i:abs (this[i] - v[i]) <= e
-
inline bool equalWithRelError(const Vec2<T> &v, T e) const noexcept¶
Compare two vectors and test if they are “approximately equal”:
- Returns:
True if the components of this and
vare the same with a relative error of no more than e, i.e., for all i:abs (this[i] - v[i]) <= e * abs (this[i])
-
inline T cross(const Vec2 &v) const noexcept¶
Right-handed cross product, i.e.
z component of Vec3 (this->x, this->y, 0) % Vec3 (v.x, v.y, 0)
Query and Manipulation
-
inline T length2() const noexcept¶
Return the square of the Euclidean norm, i.e.
the dot product with itself.
-
inline const Vec2 &normalizeNonNull() noexcept¶
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.
Numeric Limits
Public Types
Public Functions
-
inline T &operator[](int i) noexcept¶
Element access by index.
NB: This method of access may use dynamic array accesses which can prevent compiler optimizations and force temporaries to be stored to the stack and other missed vectorization opportunities. Use of direct access to x, y when possible should be preferred.
-
inline const T &operator[](int i) const noexcept¶
Element access by index.
NB: This method of access may use dynamic array accesses which can prevent compiler optimizations and force temporaries to be stored to the stack and other missed vectorization opportunities. Use of direct access to x, y when possible should be preferred.
Public Static Functions
-
static inline unsigned int dimensions() noexcept¶
Return the number of dimensions, i.e. 2.
-
inline Vec2() noexcept¶