Containers

CPVector

A fixed-size arithmetic vector with component-wise operations and numeric vector aliases.

C++23 mc/CPVector.h
#include <mc/CPVector.h>

Exceptions escaping container operations are translated to CError. Direct iterator operations, element references, and calls through .std() follow the underlying type’s contracts.

An omitted initializer lane is zero-initialized. Arithmetic applies per lane; size() is a static member.

Jump to a declaration · 90

CPVectorAlign_

template<size_t S, int N> struct CPVectorAlign_
template<> struct CPVectorAlign_<1, 2>
template<> struct CPVectorAlign_<1, 3>
template<> struct CPVectorAlign_<1, 4>
template<> struct CPVectorAlign_<2, 2>
template<> struct CPVectorAlign_<2, 3>
template<> struct CPVectorAlign_<2, 4>
template<> struct CPVectorAlign_<4, 2>
template<> struct CPVectorAlign_<4, 3>
template<> struct CPVectorAlign_<4, 4>
template<> struct CPVectorAlign_<8, 2>
template<> struct CPVectorAlign_<8, 3>
template<> struct CPVectorAlign_<8, 4>

Types, constants & data

static constexpr size_t value = 16;
static constexpr size_t value = 2;
static constexpr size_t value = 4;
static constexpr size_t value = 8;
static constexpr size_t value = 32;

CPVector

template<CArithmetic T, int N> class CPVector

Methods

CPVector

CPVector();
CPVector(std::initializer_list<T> il);
template<class S> explicit CPVector(const CVector<S>& v);
CPVector(CBuffer& b);

The default constructor leaves arithmetic components uninitialized. An initializer list zero-initializes omitted components and rejects more than N values; conversion from CVector requires exactly N elements.

operator=

CPVector& operator=(std::initializer_list<T> il);
template<class S> CPVector& operator=(const CVector<S>& v);
CPVector& operator=(const CPVector& v);

Copies components from another vector or the supplied initializer list. A short initializer list leaves the remaining components unchanged; a CVector source must contain exactly N elements.

size

static constexpr int size();

operator[]

T& operator[](int i);
const T& operator[](int i) const;

Returns the element at a zero-based index. The index must be below size(); this is not a checked, recoverable out-of-range lookup.

data

const T* data() const;
T* data();

view

std::span<T, N> view() noexcept;
std::span<const T, N> view() const noexcept;

Returns a borrowed span over the current elements. Keep the container alive and do not use the view after an operation that invalidates its storage.

operator+=

CPVector& operator+=(const CPVector& v);
CPVector& operator+=(T x);

Adds corresponding components, or applies the scalar to every component. Updates this vector in place.

operator-=

CPVector& operator-=(const CPVector& v);
CPVector& operator-=(T x);

Subtracts corresponding components, or applies the scalar to every component. Updates this vector in place.

operator*=

CPVector& operator*=(const CPVector& v);
CPVector& operator*=(T x);

Multiplies corresponding components, or applies the scalar to every component. Updates this vector in place.

operator/=

CPVector& operator/=(const CPVector& v);
CPVector& operator/=(T x);

Divides corresponding components, or applies the scalar to every component. Supply valid nonzero divisors for integral components. Updates this vector in place.

operator/

CPVector operator/(const CPVector& v) const;
CPVector operator/(T x) const;
friend CPVector operator/(T x, const CPVector& v);

Divides corresponding components, or applies the scalar to every component. Supply valid nonzero divisors for integral components. Returns a new vector.

operator%=

CPVector& operator%=(const CPVector& v);
CPVector& operator%=(T x);

Takes the remainder of corresponding components, or applies the scalar to every component. Floating-point components use std::fmod. Supply valid nonzero divisors for integral components. Updates this vector in place.

operator%

CPVector operator%(const CPVector& v) const;
CPVector operator%(T x) const;
friend CPVector operator%(T x, const CPVector& v);

Takes the remainder of corresponding components, or applies the scalar to every component. Floating-point components use std::fmod. Supply valid nonzero divisors for integral components. Returns a new vector.

normalize

void normalize();

Divides every component by the Euclidean magnitude in place. Use a nonzero vector with a floating-point component type for a unit-vector result.

magnitude

T magnitude() const;

Returns the square root of the sum of squared components, computed in the component type. Integral component types can truncate the result.

store

void store(CBuffer& b) const;

Appends the container to a CBuffer; restore it with the buffer-taking constructor.

dump

__attribute__((used)) cstr dump() const;

Returns the stream-formatted representation as a cstr; this is display text rather than the binary storage format.

Free functions & types

Types, constants & data

using bool2 = CPVector<bool, 2>;
using bool3 = CPVector<bool, 3>;
using bool4 = CPVector<bool, 4>;
using bool8 = CPVector<bool, 8>;
using bool16 = CPVector<bool, 16>;
using uchar2 = CPVector<uint8_t, 2>;
using uchar3 = CPVector<uint8_t, 3>;
using uchar4 = CPVector<uint8_t, 4>;
using uchar8 = CPVector<uint8_t, 8>;
using uchar16 = CPVector<uint8_t, 16>;
using char2 = CPVector<int8_t, 2>;
using char3 = CPVector<int8_t, 3>;
using char4 = CPVector<int8_t, 4>;
using char8 = CPVector<int8_t, 8>;
using char16 = CPVector<int8_t, 16>;
using ushort2 = CPVector<uint16_t, 2>;
using ushort3 = CPVector<uint16_t, 3>;
using ushort4 = CPVector<uint16_t, 4>;
using ushort8 = CPVector<uint16_t, 8>;
using ushort16 = CPVector<uint16_t, 16>;
using short2 = CPVector<int16_t, 2>;
using short3 = CPVector<int16_t, 3>;
using short4 = CPVector<int16_t, 4>;
using short8 = CPVector<int16_t, 8>;
using short16 = CPVector<int16_t, 16>;
using uint2 = CPVector<uint32_t, 2>;
using uint3 = CPVector<uint32_t, 3>;
using uint4 = CPVector<uint32_t, 4>;
using uint8 = CPVector<uint32_t, 8>;
using uint16 = CPVector<uint32_t, 16>;
using int2 = CPVector<int32_t, 2>;
using int3 = CPVector<int32_t, 3>;
using int4 = CPVector<int32_t, 4>;
using int8 = CPVector<int32_t, 8>;
using int16 = CPVector<int32_t, 16>;
using ulong2 = CPVector<uint64_t, 2>;
using ulong3 = CPVector<uint64_t, 3>;
using ulong4 = CPVector<uint64_t, 4>;
using ulong8 = CPVector<uint64_t, 8>;
using ulong16 = CPVector<uint64_t, 16>;
using long2 = CPVector<int64_t, 2>;
using long3 = CPVector<int64_t, 3>;
using long4 = CPVector<int64_t, 4>;
using long8 = CPVector<int64_t, 8>;
using long16 = CPVector<int64_t, 16>;
using float2 = CPVector<float, 2>;
using float3 = CPVector<float, 3>;
using float4 = CPVector<float, 4>;
using float8 = CPVector<float, 8>;
using float9 = CPVector<float, 9>;
using float16 = CPVector<float, 16>;
using double2 = CPVector<double, 2>;
using double3 = CPVector<double, 3>;
using double4 = CPVector<double, 4>;
using double8 = CPVector<double, 8>;
using double9 = CPVector<double, 9>;
using double16 = CPVector<double, 16>;

Functions

cCross

template<class T, int N> CPVector<T, N> cCross(const CPVector<T, N>& u, const CPVector<T, N>& v);

Computes the three-dimensional cross product. Use a three-component vector; the implementation accesses and writes only components 0, 1, and 2.

cDot

template<class T, int N> T cDot(const CPVector<T, N>& u, const CPVector<T, N>& v);

Returns the sum of component-wise products. Both vectors have the same lane count and component type.

cDistance

template<class T, int N> T cDistance(const CPVector<T, N>& u, const CPVector<T, N>& v);

Returns the Euclidean distance between two vectors, using their component type for the calculation.

cQuatMul

template<CFloat T> CPVector<T, 4> cQuatMul(const CPVector<T, 4>& a, const CPVector<T, 4>& b);

Multiplies two quaternions stored as (x, y, z, w). Multiplication order matters when composing rotations.

cQuatFromAxisAngle

template<CFloat T> CPVector<T, 4> cQuatFromAxisAngle(T angle, T ax, T ay, T az);

Builds an (x, y, z, w) quaternion from an angle in radians and an axis. Supply a unit axis; this helper does not normalize it.

cQuatRotator

template<CFloat T> CPVector<T, 4> cQuatRotator(const CPVector<T, 3>& r);

Builds a quaternion from X, Y, and Z rotation angles in radians, composing qz * qy * qx.

cRotate

template<CFloat T> void cRotate(CPVector<T, 4>& q, const CPVector<T, 3>& r);

Updates quaternion q by pre-multiplying the rotation represented by the supplied X/Y/Z angles in radians.