Boost LA

Swizzling

Boost LA provides arbitrary swizzling support for vector types, exposing vector views of different dimentions, and/or views with reordered elements. The example below rotates v around the X axis, and stores the resulting vector back in v but with the X and Y elements swapped:

float3 v = {0,0,7};
v|YXZ = rotx_matrix<3>(3.14159f) * v;

A special case of swizzling provides next-dimension-view to a vector object, adding either 0 or 1 as its last component. Assuming float3 is a 3D vector type, and float4 is a 4D vector type, the following statements are valid:

float3 v = {0,0,7};
float4 point = v|XYZ1; //{0,0,7,1}
float4 vector = v|XYZ0; //{0,0,7,0}

It is also valid for swizzling to address vector elements more than once:

float3 v = {0,0,7};
float4 v1 = v|ZZZZ; //{7,7,7,7}

More generic swizzling is supported by the sw function template. It provides overloads that can be instantiated either by a list of vector element indexes or by a list of vector element names, for example:

float3 v1, v2;
v1 = v2|sw<1,0,2>; //swaps elements with index 0 and 1
v1 = v2|sw<_y_,_x_,_z_>; //same as v2|YXZ;

Tutorial: Vector and Matrix Types | C Arrays | Views | Swizzling | Interoperability | Back to Boost LA