Get Involved

Float4

A vector with four floating-point components x, y, z and w.

Header File

#include <ply-math/Vector.h>

Also included from <ply-math/Base.h>.

Data Members

 float x [code]
 float y [code]
 float z [code]
 float w [code]

w is the fourth component. It follows z sequentially in memory.

Constructors

  Float4::Float4() [code]

Constructs an uninitialized Float4.

  Float4::Float4(float t) [code]

Constructs a Float4 with all components set to t.

Float4 v = {1};
StdOut::text() << v;  // "{1, 1, 1, 1}"
  Float4::Float4(float x, float y, float z, float w) [code]

Constructs a Float4 from the given components.

Float4 v = {1, 0, 0, 0};
  Float4::Float4(const Float3& v, float w) [code]

Constructs a Float4 from a Float3 and a fourth component.

Float3 a = {1, 2, 3};
StdOut::text() << Float4{a, 0};  // "{1, 2, 3, 0}"
  Float4::Float4(const Float2& v, float z, float w) [code]

Constructs a Float4 from a Float2 and two additional components.

Float2 a = {1, 2};
StdOut::text() << Float4{a, 0, 0};  // "{1, 2, 0, 0}"

Assignment Operator

void Float4::operator=(const Float4& arg) & [code]

Copy assignment. Declared with an lvalue ref-qualifier so that it's an error to assign to an rvalue.

a.normalized() = b;  // error

Arithmetic Operators

Float4 operator-(const Float4& a) [code]

Unary negation.

Float4 operator+(const Float4& a, const Float4& b) [code]
Float4 operator-(const Float4& a, const Float4& b) [code]
Float4 operator*(const Float4& a, const Float4& b) [code]
Float4 operator/(const Float4& a, const Float4& b) [code]

Returns a vector whose components are the result of applying the given operation to the corresponding components of a and b. Each component is acted on independently.

StdOut::text() << Float4{2, 3, 2, 0} * Float4{4, 1, 2, 5};  // "{8, 3, 4, 0}"

If you specify a scalar value in place of a Float4, it will be promoted to a Float4 by replicating the value to each component.

StdOut::text() << Float4{2, 3, 2, 0} * 2;  // "{4, 6, 4, 0}"
StdOut::text() << 8 / Float4{2, 4, 1, 8};  // "{4, 2, 8, 1}"
void operator+=(Float4& a, const Float4& b) [code]
void operator-=(Float4& a, const Float4& b) [code]
void operator*=(Float4& a, const Float4& b) [code]
void operator/=(Float4& a, const Float4& b) [code]

In-place versions of the above operators.

Float4 v = {2, 3, 2, 0};
v *= {4, 1, 2, 5};
StdOut::text() << v;  // "{8, 3, 4, 0}"

Comparison Functions

bool operator==(const Float4& a, const Float4& b) [code]
bool operator!=(const Float4& a, const Float4& b) [code]

Returns true if the vectors are equal (or not equal) using floating-point comparison. In particular, Float4{0.f} == Float4{-0.f} is true.

 bool isNear(const Float4& a, const Float4& b, float epsilon) [code]

Returns true if a is approximately equal to b. The tolerance is given by epsilon.

Float4 v = {0.9999f, 0.0001f, 1.9999f, 3.0001f};
StdOut::text() << isNear(v, Float4{1, 0, 2, 3}, 1e-3f);  // "true"
Bool4 operator<(const Float4& a, const Float4& b) [code]
Bool4 operator<=(const Float4& a, const Float4& b) [code]
Bool4 operator>(const Float4& a, const Float4& b) [code]
Bool4 operator>=(const Float4& a, const Float4& b) [code]

These functions compare each component individually. The result of each comparison is returned in a Bool4. Call all() to check if the result was true for all components, or call any() to check if the result was true for any component.

StdOut::text() << all(Float4{1, 2, 3, 4} > Float4{0, 1, 2, 3});  // "true"

These functions are useful for testing whether a point is inside a box. See the implementation of Box<>::contains for an example.

Geometric Functions

 float dot(const Float4& a, const Float4& b) [code]

Returns the dot product of two vectors.

StdOut::text() << dot(Float4{2, 3, 1, 3}, Float4{4, 5, 1, 0});  // "24"

Length Functions

 float Float4::length2() const [code]

Returns the square of the length of the vector.

 float Float4::length() const [code]

Returns the length of the vector. Equivalent to sqrtf(this->length2()).

 bool Float4::isUnit(float thresh = 0.001f) const [code]

Returns true if the squared length of the vector is sufficiently close to 1.0. The threshold is given by thresh.

 Float4 Float4::normalized() const [code]

Returns a unit-length vector having the same direction as this. No safety check is performed.

 Float4 Float4::safeNormalized(const Float4& fallback = {1, 0, 0, 0}, float epsilon = 1e-20f) const [code]

Returns a unit-length vector having the same direction as this with safety checks.

Conversion Functions

 const Float2& Float4::asFloat2() const [code]

Returns a const reference to the first two components as a Float2 using type punning. This should only be used as a temporary expression.

Float4 v = {4, 5, 6, 7};
StdOut::text() << v.asFloat2();  // "{4, 5}"
 const Float3& Float4::asFloat3() const [code]

Returns a const reference to the first three components as a Float3 using type punning. This should only be used as a temporary expression.

Float4 v = {4, 5, 6, 7};
StdOut::text() << v.asFloat3();  // "{4, 5, 6}"
 const Quaternion& Float4::asQuaternion() const [code]

Casts to Quaternion using type punning. This should only be used as a temporary expression.

 template <typename OtherVec4>
OtherVec4 Float4::to() const [code]

Converts to another 4D vector type such as IntVec4 or Int4<s16>.

Float4 a = {4, 5, 6, 7};
IntVec4 b = a.to<IntVec4>();

Color Functions

 float& Float4::r() [code]
 float Float4::r() const [code]
 float& Float4::g() [code]
 float Float4::g() const [code]
 float& Float4::b() [code]
 float Float4::b() const [code]
 float& Float4::a() [code]
 float Float4::a() const [code]

Convenience functions for interpreting the vector as a color. The r(), g(), b() and a() functions are aliases for the x, y, z and w components respectively.

Float4 c = {1.0f, 0.8f, 0.7f, 0.5f};
StdOut::text().format("{}, {}, {}, {}", c.r(), c.g(), c.b(), c.a());
// prints "1.0, 0.8, 0.7, 0.5"

Swizzle Functions

 Float2 Float4::swizzle(u32 i0, u32 i1) const [code]
 Float3 Float4::swizzle(u32 i0, u32 i1, u32 i2) const [code]
 Float4 Float4::swizzle(u32 i0, u32 i1, u32 i2, u32 i3) const [code]

Returns a new vector whose components are taken from the given indices. x, y, z and w are at indices 0, 1, 2 and 3 respectively. Similar to GLSL swizzling except that the components are specified by numeric index, and you can't use it to modify the original vector; only to read from it.

Float4 v = {4, 5, 6, 0};
StdOut::text() << v.swizzle(1, 0);        // "{5, 4}"
StdOut::text() << v.swizzle(2, 3, 2, 1);  // "{6, 0, 6, 5}"

These functions work correctly in the current version of all major compilers even though they use type punning, which is undefined behavior in standard C++.

Componentwise Functions

 Float4 clamp(const Float4& v, const Float4& mins, const Float4& maxs) [code]

Returns a copy of v with each component constrained to lie within the range determined by the corresponding components of mins and maxs.

Float4 v = {3, 1.5f, 0, 0.5f};
StdOut::text() << clamp(v, Float4{0, 1, 2, 3}, Float4{1, 2, 3, 4});  // "{1, 1.5, 2, 3}"
StdOut::text() << clamp(v, 0, 1);                                    // "{1, 1, 0, 0.5f}"
 Float4 abs(const Float4& a) [code]

Returns a vector with each component set to the absolute value of the corresponding component of a.

StdOut::text() << abs(Float4{-2, 3, 0, -1});  // "{2, 3, 0, 1}"
 Float4 pow(const Float4& a, const Float4& b) [code]

Returns a vector with each component set to the corresponding component of a raised to the power of the corresponding component of b.

StdOut::text() << pow(Float4{1, 2, 2, 3}, Float4{2, 3, 1, 2});  // "{1, 8, 2, 9}"
StdOut::text() << pow(Float4{1, 2, 3, -2}, 2);                  // "{1, 4, 9, 4}"
 Float4 min(const Float4& a, const Float4& b) [code]

Returns a vector with each component set to minimum of the corresponding components of a and b.

StdOut::text() << min(Float4{0, 1, 0, 1}, Float4{1, 0, 1, 0});  // "{0, 0, 0, 0}"
 Float4 max(const Float4& a, const Float4& b) [code]

Returns a vector with each component set to maximum of the corresponding components of a and b.

StdOut::text() << max(Float4{0, 1, 0, 1}, Float4{1, 0, 1, 0});  // "{1, 1, 1, 1}"

Rounding Functions

 Float4 roundUp(const Float4& vec, float spacing = 1) [code]
 Float4 roundDown(const Float4& vec, float spacing = 1) [code]
 Float4 roundNearest(const Float4& vec, float spacing = 1) [code]

Returns a vector with each component set to the rounded result of the corresponding component of vec. The optional spacing argument can be used to round to arbitrary spacings. Most precise when spacing is a power of 2.

StdOut::text() << roundUp(Float4{-0.3f, 1.4f, 0.8f, -1.2f});  // "{0, 2, 1, -1}"
StdOut::text() << roundDown(Float4{1.8f}, 0.5f);              // "{1.5, 1.5, 1.5, 1.5}"
 bool isRounded(const Float4& vec, float spacing = 1) [code]

Returns true if every component of vec is already rounded. The optional spacing argument can be used to round to arbitrary spacings. Most precise when spacing is a power of 2.

StdOut::text() << isRounded(Float4{1.5f, 0.5f, 0, 2}, 0.5f);  // true