Vec4

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

Vec4()

Uninitialized by default.

constexpr Vec4(T a)

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

constexpr Vec4(T a, T b, T c, T d)

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

constexpr Vec4(const Vec4 &v)

Copy constructor.

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

Construct from Vec4 of another base type.

template<class S>
constexpr Vec4(const Vec3<S> &v)

Vec3 to Vec4 conversion, sets w to 1.

constexpr const Vec4<T> &operator=(const Vec4 &v)

Assignment.

~Vec4()

Destructor.

Arithmetic and Comparison

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

Equality.

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

Inequality.

constexpr bool equalWithAbsError(const Vec4<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 Vec4<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 Vec4 &v) const

Dot product.

constexpr T operator^(const Vec4 &v) const

Dot product.

constexpr const Vec4<T> &operator+=(const Vec4 &v)

Component-wise addition.

constexpr Vec4<T> operator+(const Vec4 &v) const

Component-wise addition.

constexpr const Vec4<T> &operator-=(const Vec4 &v)

Component-wise subtraction.

constexpr Vec4<T> operator-(const Vec4 &v) const

Component-wise subtraction.

constexpr Vec4<T> operator-() const

Component-wise multiplication by -1.

constexpr const Vec4<T> &negate()

Component-wise multiplication by -1.

constexpr const Vec4<T> &operator*=(const Vec4 &v)

Component-wise multiplication.

constexpr const Vec4<T> &operator*=(T a)

Component-wise multiplication.

constexpr Vec4<T> operator*(const Vec4 &v) const

Component-wise multiplication.

constexpr Vec4<T> operator*(T a) const

Component-wise multiplication.

constexpr const Vec4<T> &operator/=(const Vec4 &v)

Component-wise division.

constexpr const Vec4<T> &operator/=(T a)

Component-wise division.

constexpr Vec4<T> operator/(const Vec4 &v) const

Component-wise division.

constexpr Vec4<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 Vec4<T> &normalize()

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

const Vec4<T> &normalizeExc()

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

const Vec4<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.

Vec4<T> normalized() const

Return a normalized vector. Does not modify *this.

Vec4<T> normalizedExc() const

Return a normalized vector.

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

Vec4<T> normalizedNonNull() const

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

Public Functions

constexpr T &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. 4.

Warning

doxygenfunction: Unable to resolve multiple matches for function “operator<<” with arguments (std::ostream& s, const Vec4<T>& v) in doxygen xml output for project “Imath” from directory: /build/ilmbase-5Yemou/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>&)