Vec3¶
#include <Imath/ImathVec.h>
The Vec3
class template represents a 3D vector, with predefined
typedefs for vectors of type short
, int
, int64_t
,
float
, and double
.
Note that the integer specializations of Vec3
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
, V.z
. 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>
void
vec3_example()
{
Imath::V3f a (1.0f, 2.0f, 3.0f);
Imath::V3f b; // b is uninitialized
b.x = a[0];
b.y = a[1];
b.z = a[2];
assert (a == b);
assert (a.length() == sqrt (a ^ a));
a.normalize();
assert (Imath::equalWithAbsError (a.length(), 1.0f, 1e-6f));
}
-
template<class T>
class Imath::Vec3¶ 3-element vector
Subclassed by Imath::Color3< T >, Imath::Euler< T >
Constructors and Assignment
-
inline Vec3() noexcept¶
Uninitialized by default.
-
template<class S>
inline constexpr Vec3(const Vec3<S> &v) noexcept¶ Construct from Vec3 of another base type.
-
template<class S>
inline explicit constexpr Vec3(const Vec4<S> &v) noexcept¶ Vec4 to Vec3 conversion: divide x, y and z by w, even if w is 0.
The result depends on how the environment handles floating-point exceptions.
-
template<class S>
inline explicit constexpr Vec3(const Vec4<S> &v, InfException)¶ Vec4 to Vec3 conversion: divide x, y and z by w.
Throws an exception if w is zero or if division by w would overflow.
-
~Vec3() noexcept = default¶
Destructor.
Compatibility with Sb
Arithmetic and Comparison
-
inline constexpr bool equalWithAbsError(const Vec3<T> &v, T e) const noexcept¶
Compare two matrices and test if they are “approximately equal”:
- Returns
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
-
inline constexpr bool equalWithRelError(const Vec3<T> &v, T e) const noexcept¶
Compare two matrices and test if they are “approximately equal”:
- Returns
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])
Query and Manipulation
-
inline constexpr T length2() const noexcept¶
Return the square of the Euclidean norm, i.e.
the dot product with itself.
-
inline const Vec3 &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
- static inline constexpr static T baseTypeLowest () noexcept
Largest possible negative value.
- static inline constexpr static T baseTypeMax () noexcept
Largest possible positive value.
- static inline constexpr static T baseTypeSmallest () noexcept
Smallest possible positive value.
- static inline constexpr static T baseTypeEpsilon () noexcept
Smallest possible e for which 1+e != 1.
Public Types
Public Functions
Public Static Functions
- static inline constexpr static unsigned int dimensions () noexcept
Return the number of dimensions, i.e. 3.
-
inline Vec3() noexcept¶