Skip to content

Vectors

Romain Milbert edited this page Jan 17, 2023 · 4 revisions

RaZ's vectors have a templated type & a templated fixed size. There is currently no constraint on either the type or the size, so their behavior may depend on what you do with them (technically, only arithmetic types (integers & floating-point) are expected).

Raz::Vector<float, 2> vec2f;

// Predefined aliases are available for types uint8_t, int, uint32_t, float & double,
//  and sizes 2, 3 & 4

Raz::Vector2<uint8_t> vec2b;
Raz::Vec2b vec2b;

Raz::Vector3<float> vec3f;
Raz::Vec3f vec3f;

Vector/vector equality comparison

Checking equality for two vectors yields a different behavior depending on the vector's type: if floating-point type (float, double, long double), this performs a near-equality check. This means that not-exactly-equal vectors may be deemed equal to each other.

Raz::Vec3f vec(1.f, 2.f, 3.f);
Raz::Vec3f vecEpsilon = vec + std::numeric_limits<float>::epsilon();
bool areEqual = (vec == vecEpsilon); // true!

// However, a function is available to check for strict equality
bool areStrictlyEqual = vec.strictlyEquals(vecEpsilon); // false!

Keep in mind that given this behavior, every algorithm calling Vector::operator==() may have unwanted behavior. For this reason, an std::equal_to specialization has been made for Vector.

Raz::Vec3f vec(1.f);
Raz::Vec3f vecEpsilon = vec + std::numeric_limits<float>::epsilon();

std::array<Raz::Vec3f, 2> vectors = { vec, vecEpsilon };
std::array<Raz::Vec3f, 2> swappedVectors = { vecEpsilon, vec };

bool areEqual = std::equal(vectors.cbegin(),
                           vectors.cend(),
                           swappedVectors.cbegin()); // true!

// std::equal_to's specialization must be used here
bool areStrictlyEqual = std::equal(vectors.cbegin(),
                                   vectors.cend(),
                                   swappedVectors.cbegin(),
                                   std::equal_to<Raz::Vec3f>()); // false!
Clone this wiki locally