Boost LA

Vector and Matrix Types

Boost LA does define generic yet simple vector and matrix types, however it has been specifically designed to work with user-defined vectors and matrices.

A user-defined 3D vector type float3 can be introduced to Boost LA as follows:

#include <boost/la/vector_traits.hpp>

struct float3 { float a[3]; };

namespace
boost
    {
    namespace
    la
        {
        template <>
        struct
        vector_traits<float3>
            {
            static int const dim=3;
            typedef float scalar_type;

            template <int I> static inline scalar_type & w( float3 & v ) { return v.a[I]; }
            template <int I> static inline scalar_type r( float3 const & v ) { return v.a[I]; }

            static inline scalar_type & iw( int i, float3 & v ) { return v.a[i]; }
            static inline scalar_type ir( int i, float3 const & v ) { return v.a[i]; }
            };
        }
    }

After a similar specialization of the matrix_traits template for a user-defined 3x3 matrix type float33, a full range of vector and matrix operations defined in Boost LA headers become available automatically:

float3 v;
v|X = 0;
v|Y = 0;
v|Z = 7;
float vmag = magnitude(v);
float33 m = rotx_matrix<3>(3.14159f);
float3 vrot = m * v;

Note:

The use of operator| to access vector elements is perhaps surprising, however it is a logical part of a more general system of view proxies in Boost LA. See operator| rationale.


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