Manifold 3.0
Robust geometry
Loading...
Searching...
No Matches
Vec

linalg::vec<T,M> defines a fixed-length vector containing exactly M elements of type T. More...

Classes

struct  vec< T, 1 >
struct  vec< T, 2 >
struct  vec< T, 3 >
struct  vec< T, 4 >

Detailed Description

linalg::vec<T,M> defines a fixed-length vector containing exactly M elements of type T.

This data structure can be used to store a wide variety of types of data, including geometric vectors, points, homogeneous coordinates, plane equations, colors, texture coordinates, or any other situation where you need to manipulate a small sequence of numbers. As such, vec<T,M> is supported by a set of algebraic and component-wise functions, as well as a set of standard reductions.

vec<T,M>:

  • is DefaultConstructible:
    float3 v; // v contains 0,0,0
  • is constructible from M elements of type T:
    float3 v {1,2,3}; // v contains 1,2,3
  • is CopyConstructible and CopyAssignable:
    float3 v {1,2,3}; // v contains 1,2,3
    float3 u {v}; // u contains 1,2,3
    float3 w; // w contains 0,0,0
    w = u; // w contains 1,2,3
  • is EqualityComparable and LessThanComparable:
    if(v == y) cout << "v and u contain equal elements in the same positions" <<
    endl; if(v < u) cout << "v precedes u lexicographically" << endl;
  • is explicitly constructible from a single element of type T:
    float3 v = float3{4}; // v contains 4,4,4
  • is explicitly constructible from a vec<U,M> of some other type U:
    float3 v {1.1f,2.3f,3.5f}; // v contains 1.1,2.3,3.5
    int3 u = int3{v}; // u contains 1,2,3
  • has fields x,y,z,w:
    float y = point.y; // y contains second element of point
    pixel.w = 0.5; // fourth element of pixel set to 0.5
    float s = tc.x; // s contains first element of tc
  • supports indexing:
    float x = v[0]; // x contains first element of v
    v[2] = 5; // third element of v set to 5
  • supports unary operators +, -, ! and ~ in component-wise fashion:
    auto v = -float{2,3}; // v is float2{-2,-3}
  • supports binary operators +, -, *, /, %, |, &, ^, << and >> in component-wise fashion:
    auto v = float2{1,1} + float2{2,3}; // v is float2{3,4}
  • supports binary operators with a scalar on the left or the right:
    auto v = 2 * float3{1,2,3}; // v is float3{2,4,6}
    auto u = float3{1,2,3} + 1; // u is float3{2,3,4}
  • supports operators +=, -=, *=, /=, %=, |=, &=, ^=, <<= and >>= with vectors or scalars on the right:
    float2 v {1,2}; v *= 3; // v is float2{3,6}
  • supports operations on mixed element types:
    auto v = float3{1,2,3} + int3{4,5,6}; // v is float3{5,7,9}
  • supports range-based for:
    for(auto elem : float3{1,2,3}) cout << elem << ' '; // prints "1 2 3 "
  • has a flat memory layout:
    float3 v {1,2,3};
    float * p = v.data(); // &v[i] == p+i
    p[1] = 4; // v contains 1,4,3