swizzle

Macro swizzle 

Source
macro_rules! swizzle {
    ($mat: expr, $( $i:tt),+) => { ... };
}
Expand description

Rearrange the rows of a matrix by the given row identifiers

§Arguments

  • mat: the matrix to manipulate

  • i...: variadic comma-seperated list of row selectors. The row selectors are each one of:

    • const expression representing the row index of the input to copy. eg: 0 or 2
    • a letter representing the same. you can use r,g,b,a or x,y,z,w to represent index 0 through 3, or u,v to represent indices 0 through 1.
    • an expression in curly braces, representing a value to be copied to an entire row of the result, eg: {1.0} or {5}

    note that the number of selectors doesnt need to match the height of the input!

§Examples

let myvec = Vector::vec([0, 1, 2, 3]);

// each element can be selected with:
// 0: r, x, u, or 0
// 1: g, y, v, or 1
// 2: b, z, or 2
// 3: a, w, or 3
// or a result row can be filled by a new value

assert_eq!(swizzle!(myvec, a, z, v, 0, {7}), Vector::vec([3, 2, 1, 0, 7]));

More often you wont mix and match selector “flavors”. This example unpacks a DXT5nm color into the red and green channels, with blue filled with 0.

let myvec = Vector::vec([0, 120, 0, 255]);
assert_eq!(swizzle!(myvec, a, g, {0}), Vector::vec([255, 120, 0]));