Vec4#

#include <Imath/ImathVec.h>

The Vec4 class template represents a 4D vector, with predefined typedefs for vectors of type short, int, int64_t, float, and double.

Note that the integer specializations of Vec4 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, V.w. 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
vec4_example ()
{
    Imath::V4f a (1.0f, 2.0f, 3.0f, 4.0f);
    Imath::V4f b; // b is uninitialized

    b.x = a[0];
    b.y = a[1];
    b.z = a[2];
    b.w = a[3];

    assert (a == b);

    assert (a.length () == sqrt (a ^ a));

    a.normalize ();
    assert (Imath::equalWithAbsError (a.length (), 1.0f, 1e-6f));
}
typedef Vec4<short> Imath::V4s#

Vec4 of short.

typedef Vec4<int> Imath::V4i#

Vec4 of integer.

typedef Vec4<int64_t> Imath::V4i64#

Vec4 of int64_t.

typedef Vec4<float> Imath::V4f#

Vec4 of float.

typedef Vec4<double> Imath::V4d#

Vec4 of double.

template<class T>
class Vec4#

4-element vector

Direct access to elements

T x#
T y#
T z#
T w#

Constructors and Assignment

inline Vec4() noexcept#

Uninitialized by default.

inline explicit constexpr Vec4(T a) noexcept#

Initialize to a scalar (a,a,a,a)

inline constexpr Vec4(T a, T b, T c, T d) noexcept#

Initialize to given elements (a,b,c,d)

inline constexpr Vec4(const Vec4 &v) noexcept#

Copy constructor.

template<class S>
inline constexpr Vec4(const Vec4<S> &v) noexcept#

Construct from Vec4 of another base type.

template<class S>
inline explicit constexpr Vec4(const Vec3<S> &v) noexcept#

Vec3 to Vec4 conversion, sets w to 1.

inline constexpr const Vec4 &operator=(const Vec4 &v) noexcept#

Assignment.

~Vec4() noexcept = default#

Destructor.

Compatibility with Sb

template<class S>
inline void setValue(S a, S b, S c, S d) noexcept#

Set the value.

template<class S>
inline void setValue(const Vec4<S> &v) noexcept#

Set the value.

template<class S>
inline void getValue(S &a, S &b, S &c, S &d) const noexcept#

Return the value in a and b

template<class S>
inline void getValue(Vec4<S> &v) const noexcept#

Return the value in v

inline T *getValue() noexcept#

Return a raw pointer to the array of values.

inline const T *getValue() const noexcept#

Return a raw pointer to the array of values.

Arithmetic and Comparison

template<class S>
inline constexpr bool operator==(const Vec4<S> &v) const noexcept#

Equality.

template<class S>
inline constexpr bool operator!=(const Vec4<S> &v) const noexcept#

Inequality.

inline constexpr bool equalWithAbsError(const Vec4<T> &v, T e) const noexcept#

Compare two vectors and test if they are “approximately equal”:

Returns:

True if the components of this and v are the same with an absolute error of no more than e, i.e., for all i:

abs (this[i] - v[i]) <= e

inline constexpr bool equalWithRelError(const Vec4<T> &v, T e) const noexcept#

Compare two vectors and test if they are “approximately equal”:

Returns:

True if the components of this and v are 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 constexpr T dot(const Vec4 &v) const noexcept#

Dot product.

inline constexpr T operator^(const Vec4 &v) const noexcept#

Dot product.

inline constexpr const Vec4 &operator+=(const Vec4 &v) noexcept#

Component-wise addition.

inline constexpr Vec4 operator+(const Vec4 &v) const noexcept#

Component-wise addition.

inline constexpr const Vec4 &operator-=(const Vec4 &v) noexcept#

Component-wise subtraction.

inline constexpr Vec4 operator-(const Vec4 &v) const noexcept#

Component-wise subtraction.

inline constexpr Vec4 operator-() const noexcept#

Component-wise multiplication by -1.

inline constexpr const Vec4 &negate() noexcept#

Component-wise multiplication by -1.

inline constexpr const Vec4 &operator*=(const Vec4 &v) noexcept#

Component-wise multiplication.

inline constexpr const Vec4 &operator*=(T a) noexcept#

Component-wise multiplication.

inline constexpr Vec4 operator*(const Vec4 &v) const noexcept#

Component-wise multiplication.

inline constexpr Vec4 operator*(T a) const noexcept#

Component-wise multiplication.

inline constexpr const Vec4 &operator/=(const Vec4 &v) noexcept#

Component-wise division.

inline constexpr const Vec4 &operator/=(T a) noexcept#

Component-wise division.

inline constexpr Vec4 operator/(const Vec4 &v) const noexcept#

Component-wise division.

inline constexpr Vec4 operator/(T a) const noexcept#

Component-wise division.

Query and Manipulation

template<typename S = T, IMATH_NAMESPACE::enable_if_t<(is_float_like<S>::value), int> = 0>
inline T length() const noexcept#

Return the Euclidean norm.

inline constexpr T length2() const noexcept#

Return the square of the Euclidean norm, i.e.

the dot product with itself.

template<typename S = T, IMATH_NAMESPACE::enable_if_t<(is_float_like<S>::value), int> = 0>
const Vec4 &normalize() noexcept#

Normalize in place. If length()==0, return a null vector.

template<typename S = T, IMATH_NAMESPACE::enable_if_t<(is_float_like<S>::value), int> = 0>
const Vec4 &normalizeExc()#

Normalize in place. If length()==0, throw an exception.

template<typename S = T, IMATH_NAMESPACE::enable_if_t<(is_float_like<S>::value), int> = 0>
const Vec4 &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.

template<typename S = T, IMATH_NAMESPACE::enable_if_t<(is_float_like<S>::value), int> = 0>
inline Vec4<T> normalized() const noexcept#

Return a normalized vector. Does not modify *this.

template<typename S = T, IMATH_NAMESPACE::enable_if_t<(is_float_like<S>::value), int> = 0>
inline Vec4<T> normalizedExc() const#

Return a normalized vector.

Does not modify *this. Throw an exception if length()==0.

template<typename S = T, IMATH_NAMESPACE::enable_if_t<(is_float_like<S>::value), int> = 0>
inline Vec4<T> normalizedNonNull() const noexcept#

Return a normalized vector.

Does not modify *this, and does not check 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 T baseTypeLowest() noexcept#

Largest possible negative value.

static inline constexpr T baseTypeMax() noexcept#

Largest possible positive value.

static inline constexpr T baseTypeSmallest() noexcept#

Smallest possible positive value.

static inline constexpr T baseTypeEpsilon() noexcept#

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 to T as V::BaseType

Public Functions

inline constexpr T &operator[](int i) noexcept#

Element access by index.

NB: This method of access uses 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, z, w when possible should be preferred.

inline constexpr const T &operator[](int i) const noexcept#

Element access by index.

NB: This method of access uses 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, z, w when possible should be preferred.

template<typename S, IMATH_NAMESPACE::enable_if_t<(is_float_like<S>::value), int>>
inline const Vec4<T> &normalize() noexcept#
template<typename S, IMATH_NAMESPACE::enable_if_t<(is_float_like<S>::value), int>>
inline const Vec4<T> &normalizeExc()#
template<typename S, IMATH_NAMESPACE::enable_if_t<(is_float_like<S>::value), int>>
inline const Vec4<T> &normalizeNonNull() noexcept#

Public Static Functions

static inline constexpr unsigned int dimensions() noexcept#

Return the number of dimensions, i.e. 4.

template<class T>
std::ostream &Imath::operator<<(std::ostream &s, const Vec4<T> &v)#

Stream output, as “(x y z w)”.