:last-update-label!: :icons: font :prewrap!: :source-highlighter: coderay :stylesheet: zajo.css = QVM Generic {CPP} library for working with Quaternions Vectors and Matrices :toclevels: 3 :toc: left :sectnumlevels: 2 :keywords: c++, boost, matrix, vector, quaternion [abstract] == Abstract QVM is a generic library for working with Quaternions, Vectors and Matrices of static size. Features: ==== * Emphasis on 2, 3 and 4-dimensional operations needed in graphics, video games and simulation applications. * Free function templates operate on any compatible user-defined quaternion, vector or matrix type. * Quaternion, vector and matrix types from different libraries or subsystems can be safely mixed in the same expression. * Type-safe mapping between compatible lvalue types with no temporary objects; e.g. transpose remaps the elements, rather than transforming the matrix. [.text-right] https://github.com/boostorg/qvm[GitHub] | <> | <> | <> ==== [[tutorial]] == Tutorial === Quaternions, Vectors, Matrices Out of the box QVM defines generic yet simple <>, <> and <> types. For example, the following snippet creates a quaternion object that rotates around the X axis: [source,c++] ---- quat rx = rotx_quat(3.14159f); ---- Similarly, a matrix that translates by a given vector can be created as follows: [source,c++] ---- vec v = {0,0,7}; mat tr = translation_mat(v); ---- The usual quaternion, vector and matrix operations work on these QVM types, however the operations are decoupled from any specific type: they work on any suitable type that has been registered by specializing the <>, <> and <> templates. For example, a user-defined 3D vector type `float3` can be introduced to QVM as follows: [source,c++] ---- struct float3 { float a[3]; }; namespace boost { namespace qvm { template <> struct vec_traits { static int const dim=3; typedef float scalar_type; template static inline scalar_type & write_element( float3 & v ) { return v.a[I]; } template static inline scalar_type read_element( float3 const & v ) { return v.a[I]; } static inline scalar_type & write_element_idx( int i, float3 & v ) { return v.a[i]; } //optional static inline scalar_type read_element_idx( int i, float3 const & v ) { return v.a[i]; } //optional }; } } ---- Equivalently, using the <> template the above can be shortened to: [source,c++] ---- namespace boost { namespace qvm { template <> struct vec_traits: vec_traits_defaults { template static inline scalar_type & write_element( float3 & v ) { return v.a[I]; } static inline scalar_type & write_element_idx( int i, float3 & v ) { return v.a[i]; } //optional }; } } ---- After a similar specialization of the <> template for a user-defined 3x3 matrix type `float33`, the full range of vector and matrix operations defined by QVM headers becomes available automatically: [source,c++] ---- float3 v; X(v) = 0; Y(v) = 0; Z(v) = 7; float vmag = mag(v); float33 m = rotx_mat<3>(3.14159f); float3 vrot = m * v; ---- User-defined quaternion types are similarly introduced to QVM by specializing the <> template. ''' === C Arrays In <>, <> and <> QVM defines appropriate <>, <> and <> specializations that allow QVM functions to operate directly on plain old C arrays: [source,c++] ---- float v[3] = {0,0,7}; float3 vrot = rotx_mat<3>(3.14159f) * v; ---- Naturally, operator overloads cannot kick in if all elements of an expression are of built-in types. The following is still illegal: [source,c++] ---- float v[3] = {0,0,7}; v *= 42; ---- The <> and <> function templates can be used to work around this issue: [source,c++] ---- float v[3] = {0,0,7}; vref(v) *= 42; ---- ''' [[view_proxy]] === View proxies QVM defines various function templates that provide static mapping between (possibly user-defined) quaternion, vector and matrix types. The example below multiplies column 1 (QVM indexes are always zero-based) of the matrix `m` by a scalar: [source,c++] ---- void multiply_column1( float33 & m, float scalar ) { col<1>(m) *= scalar; } ---- The expression <(m)`>> is an lvalue of an unspecified 3D vector type that refers to column 1 of `m`. Note however that this does not create any temporary objects; instead `operator*=` above works directly with a reference to `m`. Here is another example, multiplying a transposed view of a matrix by a vector of some user-defined type `float3`: [source,c++] ---- float3 v = {0,0,7}; float3 vrot = transposed(rotx_mat<3>(3.14159f)) * v; ---- In general, the various view proxy functions return references of unspecified, non-copyable types that refer to the original object. They can be assigned from or converted to any compatible vector or matrix type. ''' === Swizzling QVM allows accessing vector elements by swizzling, exposing vector views of different dimensions, 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: [source,c++] ---- float3 v = {0,0,7}; YXZ(v) = rotx_mat<3>(3.14159f) * v; ---- A special case of swizzling provides next-dimension-view of 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: [source,c++] ---- float3 v = {0,0,7}; float4 point = XYZ1(v); //{0,0,7,1} float4 vector = XYZ0(v); //{0,0,7,0} ---- It is also valid for swizzling to address vector elements more than once: [source,c++] ---- float3 v = {0,0,7}; float4 v1 = ZZZZ(v); //{7,7,7,7} ---- QVM defines all permutations of `X`, `Y`, `Z`, `W` for 1D, 2D, 3D and 4D swizzling, plus each dimension defines variants with 0 or 1 used at any position (if 0 or 1 appear at the first position, the swizzling function name begins with underscore, e.g. `_1XY`). The swizzling syntax can also be used to bind scalars as vectors. For example: [source,c++] ---- float3 v = _00X(42.0f); //{0,0,42} ---- ''' [[enable_if]] === SFINAE/enable_if SFINAE stands for Substitution Failure Is Not An Error. This refers to a situation in {CPP} where an invalid substitution of template parameters (including when those parameters are deduced implicitly as a result of an unqualified call) is not in itself an error. In absence of concepts support, SFINAE can be used to disable function template overloads that would otherwise present a signature that is too generic. More formally, this is supported by the Boost `enable_if` library. For example, QVM defines `operator*` overload which works with any user-defined matrix and vector types. The naive approach would be to declare this overload as follows: [source,c++] ---- template Vector operator*( Matrix const & m, Vector const & v ); ---- Even if the function definition might contain code that would compile only for `Matrix` and `Vector` types, because the function declaration itself is valid, it will participate in overload rezolutions when multiplying objects of any two types whatsoever. This typically renders overload resolutions ambiguous and the compiler (correctly) issues an error. Using `enable_if`, QVM declares such overloads in a way that preserves their generic signature but only participate in overload resolutions if the passed parameters make sense depending on the semantics of the operation being defined: [source,c++] ---- template typename enable_if_c< is_mat::value && is_vec::value && mat_traits::cols==vec_traits::dim, //Condition B>::type //Return type operator*( A const & a, B const & b ); ---- For brevity, function declarations throughout this documentation specify the condition which controls whether they are enabled or not without specifying exactly what `enable_if` construct is used to achieve this effect. ''' === Interoperability An important design goal of QVM is that it works seamlessly with 3rd-party quaternion, vector and matrix types and libraries. Even when such libraries overload the same {CPP} operators as QVM, it is safe to bring the entire `boost::qvm` namespace in scope by specifying: [source,c++] ---- using namespace boost::qvm; ---- The above using directive does not introduce ambiguities with function and operator overloads defined by a 3rd-party library because: - Most `boost::qvm` function overloads and all operator overloads use SFINAE/`enable_if`, which makes them "disappear" unless an expression uses types that have the appropriate QVM-specific type traits defined; - Whenever such overloads are compatible with a given expression, their signature is extremely generic, which means that any other (user-defined) compatible overload will be a better match in any overload resolution. NOTE: Bringing the entire boost::qvm namespace in scope may introduce ambiguities when accessing types (as opposed to functions) defined by 3rd-party libraries. In that case, you can safely bring namespace `boost::qvm::sfinae` in scope instead, which contains only function and operator overloads that use SFINAE/`enable_if`. ==== Specifying return types for binary operations Bringing the `boost::qvm` namespace in scope lets you mix vector and matrix types that come from different APIs into a common, type-safe framework. In this case however, it should be considered what types should be returned by binary operations that return an object by value. For example, if you multiply a 3x3 matrix `m1` of type `user_matrix1` by a 3x3 matrix `m2` of type `user_matrix2`, what type should that operation return? The answer is that by default, QVM returns some kind of compatible matrix type, so it is always safe to write: [source,c++] ---- auto & m = m1 * m2; ---- However, the type deduced by default converts implicitly to any compatible matrix type, so the following is also valid, at the cost of a temporary: [source,c++] ---- user_matrix1 m = m1 * m2; ---- While the temporary object can be optimized away by many compilers, it can be avoided altogether by specializing the <> template. For example, to specify that multiplying a `user_matrix1` by a `user_matrix2` should always produce a `user_matrix1` object, you could write: [source,c++] ---- namespace boost { namespace qvm { template <> struct deduce_mat2 { typedef user_matrix1 type; }; template <> struct deduce_mat2 { typedef user_matrix1 type; }; } } ---- [WARNING] ==== Be mindful of potential ODR violation when using <>, <> and <> in independent libraries. For example, this could happen if `lib1` defines `deduce_vec2::type` as `lib1::vec` and in the same program `lib2` defines `deduce_vec2::type` as `lib2::vec`. It is best to keep such specializations out of `lib1` and `lib2`. Of course, it is always safe for `lib1` and `lib2` to use <> to convert between the `lib1::vec` and `lib2::vec` types as needed. ==== ==== Specifying return types for unary operations Perhaps surprisingly, unary operations that return an object by value have a similar, though simpler issue. That's because the argument they're called with may not be copyable, as in: [source,c++] ---- float m[3][3]; auto & inv = inverse(m); ---- Above, the object returned by <> and captured by `inv` can not be of type `float[3][3]`, because that type isn't copyable. By default, QVM "just works", returning an object of suitable matrix type that is copyable. This deduction process can be controlled, by specializing the <> template. ==== Converting between different quaternion, vector and matrix types Any time you need to create a matrix of a particular {CPP} type from any other compatible matrix type, you can use the <> function: [source,c++] ---- user_matrix2 m=convert_to(m1 * m2); ---- [[reference]] == Reference === Header Files QVM is split into multiple headers to allow different compilation units to `#include` only the components they need. Each function in this document specifies the exact header that must be `#included` in order to use it. The tables below list commonly used components and the headers they're found in. Header names containing a number define functions that only work with objects of that dimension; e.g. `vec_operations2.hpp` contains only functions for working with 2D vectors. The header `boost/qvm/all.hpp` is provided for convenience. It includes all other QVM headers. .Quaternion header files [cols="1,2l"] |==== | Quaternion traits |#include #include #include | Quaternion element access |#include | Quaternion operations |#include | <> class template |#include |==== .Vector header files [cols="1,2l"] |==== | Vector traits |#include #include #include | Vector element access |#include | Vector <> |#include #include #include #include | Vector operations | #include #include #include #include | Quaternion-vector operations | #include | Vector-matrix operations | #include | Vector-matrix <> | #include | <> class template | #include |==== .Matrix header files [cols="1,2l"] |==== | Matrix traits |#include #include #include | Matrix element access |#include | Matrix operations |#include #include #include #include | Matrix-matrix <> | #include | Matrix-vector <> | #include | <> class template | #include |==== [[type_traits]] === Type Traits System QVM is designed to work with user-defined quaternion, vector and matrix types, as well as user-defined scalar types. This section formally defines the way such types can be integrated. ''' [[scalar_requirements]] ==== Scalar Requirements A valid scalar type `S` must have accessible destructor, default constructor, copy constructor and assignment operator, and must support the following operations: [source,c++] ---- S operator*( S, S ); S operator/( S, S ); S operator+( S, S ); S operator-( S, S ); S & operator*=( S &, S ); S & operator/=( S &, S ); S & operator+=( S &, S ); S & operator-=( S &, S ); bool operator==( S, S ); bool operator!=( S, S ); ---- In addition, the expression `S(0)` should construct a scalar of value zero, and `S(1)` should construct a scalar of value one, or else the <> template must be specialized appropriately. ''' [[is_scalar]] ==== `is_scalar` .#include [source,c++] ---- namespace boost { namespace qvm { template struct is_scalar { static bool const value=false; }; template <> struct is_scalar { static bool const value=true; }; template <> struct is_scalar { static bool const value=true; }; template <> struct is_scalar { static bool const value=true; }; template <> struct is_scalar { static bool const value=true; }; template <> struct is_scalar { static bool const value=true; }; template <> struct is_scalar { static bool const value=true; }; template <> struct is_scalar { static bool const value=true; }; template <> struct is_scalar { static bool const value=true; }; template <> struct is_scalar { static bool const value=true; }; template <> struct is_scalar { static bool const value=true; }; template <> struct is_scalar { static bool const value=true; }; template <> struct is_scalar { static bool const value=true; }; } } ---- This template defines a compile-time boolean constant value which can be used to determine whether a type `T` is a valid scalar type. It must be specialized together with the <> template in order to introduce a user-defined scalar type to QVM. Such types must satisfy the <>. ''' [[scalar_traits]] ==== `scalar_traits` .#include [source,c++] ---- namespace boost { namespace qvm { template struct scalar_traits { BOOST_QVM_INLINE_CRITICAL static Scalar value( int v ) { return Scalar(v); } }; } } ---- This template may be specialized for user-defined scalar types to define the appropriate conversion from `int`; this is primarily used whenever QVM needs to deduce a zero or one value. ''' [[deduce_scalar]] ==== `deduce_scalar` .#include [source,c++] ---- namespace boost { namespace qvm { template struct deduce_scalar { typedef typename impl::type type; }; } } ---- Requirements: :: `A` and `B` satisfy the <>. Returns: :: If `A` and `B` are the same type, `impl::type` returns that type. Otherwise, `impl::type` is well defined for the following types only: `signed`/`unsigned char`, `signed`/`unsigned short`, `signed`/`unsigned int`, `signed`/`unsigned long`, `float` and `double`. The deduction logic is as follows: - if either of `A` and `B` is `double`, the result is `double`; - else, if one of `A` or `B` is an integer type and the other is `float`, the result is `float`; - else, if one of `A` or `B` is a signed integer and the other type is unsigned integer, the signed type is changed to unsigned, and then the lesser of the two integers is promoted to the other. NOTE: This template is used by generic binary operations that return a scalar, to deduce the return type based on the (possibly different) scalars of their arguments. ''' [[scalar]] ==== `scalar` .#include [source,c++] ---- namespace boost { namespace qvm { template struct scalar { typedef /*exact definition unspecified*/ type; }; } } ---- The expression <::scalar_type`>> evaluates to the scalar type of the quaternion type `T` (if <::value`>> is `true`). The expression <::scalar_type`>> evaluates to the scalar type of the vector type `T` (if <::value`>> is `true`). The expression <::scalar_type`>> evaluates to the scalar type of the matrix type `T` (if <::value`>> is `true`). The expression `scalar::type` is similar, except that it automatically detects whether `T` is a vector or a matrix or a quaternion type. ''' [[is_quat]] ==== `is_quat` .#include [source,c++] ---- namespace boost { namespace qvm { template struct is_quat { static bool const value = false; }; } } ---- This type template defines a compile-time boolean constant value which can be used to determine whether a type `T` is a quaternion type. For quaternion types, the <> template can be used to access their elements generically, or to obtain their `scalar type`. ''' [[quat_traits]] ==== `quat_traits` .#include [source,c++] ---- namespace boost { namespace qvm { template struct quat_traits { /*main template members unspecified*/ }; /* User-defined (possibly partial) specializations: template <> struct quat_traits { typedef <> scalar_type; template static inline scalar_type read_element( Quaternion const & q ); template static inline scalar_type & write_element( Quaternion & q ); }; */ } } ---- The `quat_traits` template must be specialized for (user-defined) quaternion types in order to enable quaternion operations defined in QVM headers for objects of those types. NOTE: QVM quaternion operations do not require that quaternion types are copyable. The main `quat_traits` template members are not specified. Valid specializations are required to define the following members: - `scalar_type`: the expression `quat_traits::scalar_type` must be a value type which satisfies the <>. In addition, valid specializations of the `quat_traits` template must define at least one of the following access functions as static members, where `q` is an object of type `Quaternion`, and `I` is compile-time integer constant: - `read_element`: the expression `quat_traits::read_element(q)` returns either a copy of or a `const` reference to the `I`-th element of `q`. - `write_element`: the expression `quat_traits::write_element(q)` returns mutable reference to the `I`-th element of `q`. NOTE: For the quaternion `a + bi + cj + dk`, the elements are assumed to be in the following order: `a`, `b`, `c`, `d`; that is, `I`=`0`/`1`/`2`/`3` would access `a`/`b`/`c`/`d`. It is illegal to call any of the above functions unless `is_quat::value` is true. Even then, quaternion types are allowed to define only a subset of the access functions. Below is an example of a user-defined quaternion type, and its corresponding specialization of the quat_traits template: [source,c++] ---- #include struct fquat { float a[4]; }; namespace boost { namespace qvm { template <> struct quat_traits { typedef float scalar_type; template static inline scalar_type & write_element( fquat & q ) { return q.a[I]; } template static inline scalar_type read_element( fquat const & q ) { return q.a[I]; } }; } } ---- Equivalently, using the <> template the above can be shortened to: [source,c++] ---- namespace boost { namespace qvm { template <> struct quat_traits: quat_traits_defaults { template static inline scalar_type & write_element( fquat & q ) { return q.a[I]; } }; } } ---- ''' [[quat_traits_defaults]] ==== `quat_traits_defaults` .#include [source,c++] ---- namespace boost { namespace qvm { template struct quat_traits_defaults { typedef QuatType quat_type; typedef ScalarType scalar_type; template static BOOST_QVM_INLINE_CRITICAL scalar_type read_element( quat_type const & x ) { return quat_traits::template write_element(const_cast(x)); } }; } } ---- The `quat_traits_defaults` template is designed to be used as a public base for user-defined specializations of the <> template, to easily define the required members. If it is used, the only member that must be defined by the user in a `quat_traits` specialization is `write_element`; the `quat_traits_defaults` base will define `read_element`, as well as `scalar_type` automatically. ''' [[deduce_quat]] ==== `deduce_quat` .#include [source,c++] ---- namespace boost { namespace qvm { template struct deduce_quat { typedef Q type; }; } } ---- Requirements: :: - `<>::value` is `true`; - `<>::type>::value` must be `true`; - `deduce_quat::type` must be copyable. This template is used by QVM whenever it needs to deduce a copyable quaternion type from a single user-supplied function parameter of quaternion type. Note that `Q` itself may be non-copyable. The main template definition returns `Q`, which means that it is suitable only for copyable quaternion types. QVM also defines (partial) specializations for the non-copyable quaternion types it produces. Users can define other (partial) specializations for their own types. A typical use of the `deduce_quat` template is for specifying the preferred quaternion type to be returned by the generic function template overloads in QVM depending on the type of their arguments. ''' [[deduce_quat2]] ==== `deduce_quat2` .#include [source,c++] ---- namespace boost { namespace qvm { template struct deduce_quat2 { typedef /*unspecified*/ type; }; } } ---- Requirements: :: - Both `<>::type` and `scalar::type` are well defined; - `<>::value` || `is_quat::value` is `true`; - `is_quat::type>::value` must be `true`; - `deduce_quat2::type` must be copyable. This template is used by QVM whenever it needs to deduce a quaternion type from the types of two user-supplied function parameters. The returned type must have accessible copy constructor (the `A` and `B` types themselves could be non-copyable, and either one of them may not be a quaternion type.) The main template definition returns an unspecified quaternion type with <> obtained by `<>::type`, except if `A` and `B` are the same quaternion type `Q`, in which case `Q` is returned, which is only suitable for copyable types. QVM also defines (partial) specializations for the non-copyable quaternion types it produces. Users can define other (partial) specializations for their own types. A typical use of the `deduce_quat2` template is for specifying the preferred quaternion type to be returned by the generic function template overloads in QVM depending on the type of their arguments. ''' [[is_vec]] ==== `is_vec` .#include [source,c++] ---- namespace boost { namespace qvm { template struct is_vec { static bool const value = false; }; } } ---- This type template defines a compile-time boolean constant value which can be used to determine whether a type `T` is a vector type. For vector types, the <> template can be used to access their elements generically, or to obtain their dimension and `scalar type`. ''' [[vec_traits]] ==== `vec_traits` .#include [source,c++] ---- namespace boost { namespace qvm { template struct vec_traits { /*main template members unspecified*/ }; /* User-defined (possibly partial) specializations: template <> struct vec_traits { static int const dim = <>; typedef <> scalar_type; template static inline scalar_type read_element( Vector const & v ); template static inline scalar_type & write_element( Vector & v ); static inline scalar_type read_element_idx( int i, Vector const & v ); static inline scalar_type & write_element_idx( int i, Vector & v ); }; */ } } ---- The `vec_traits` template must be specialized for (user-defined) vector types in order to enable vector and matrix operations defined in QVM headers for objects of those types. NOTE: QVM vector operations do not require that vector types are copyable. The main `vec_traits` template members are not specified. Valid specializations are required to define the following members: - `dim`: the expression `vec_traits::dim` must evaluate to a compile-time integer constant greater than 0 that specifies the vector size. - `scalar_type`: the expression `vec_traits::scalar_type` must be a value type which satisfies the <>. In addition, valid specializations of the `vec_traits` template may define the following access functions as static members, where `v` is an object of type `Vector`, `I` is a compile-time integer constant, and `i` is a variable of type `int`: - `read_element`: the expression `vec_traits::read_element(v)` returns either a copy of or a const reference to the `I`-th element of `v`. - `write_element`: the expression `vec_traits::write_element(v)` returns mutable reference to the `I`-th element of `v`. - `read_element_idx`: the expression `vec_traits::read_element_idx(i,v)` returns either a copy of or a `const` reference to the `i`-th element of `v`. - `write_element_idx`: the expression `vec_traits::write_element_idx(i,v)` returns mutable reference to the `i`-th element of `v`. It is illegal to call any of the above functions unless `is_vec::value` is true. Even then, vector types are allowed to define only a subset of the access functions. The general requirements are: - At least one of `read_element` or `write_element` must be defined; - If `read_element_idx` is defined, `read_element` must also be defined; - If `write_element_idx` is defined, `write_element` must also be defined. Below is an example of a user-defined 3D vector type, and its corresponding specialization of the `vec_traits` template: [source,c++] ---- #include struct float3 { float a[3]; }; namespace boost { namespace qvm { template <> struct vec_traits { static int const dim=3; typedef float scalar_type; template static inline scalar_type & write_element( float3 & v ) { return v.a[I]; } template static inline scalar_type read_element( float3 const & v ) { return v.a[I]; } static inline scalar_type & write_element_idx( int i, float3 & v ) { return v.a[i]; } //optional static inline scalar_type read_element_idx( int i, float3 const & v ) { return v.a[i]; } //optional }; } } ---- Equivalently, using the <> template the above can be shortened to: [source,c++] ---- namespace boost { namespace qvm { template <> struct vec_traits: vec_traits_defaults { template static inline scalar_type & write_element( float3 & v ) { return v.a[I]; } static inline scalar_type & write_element_idx( int i, float3 & v ) { return v.a[i]; } //optional }; } } ---- ''' [[vec_traits_defaults]] ==== `vec_traits_defaults` .#include [source,c++] ---- namespace boost { namespace qvm { template struct vec_traits_defaults { typedef VecType vec_type; typedef ScalarType scalar_type; static int const dim=Dim; template static BOOST_QVM_INLINE_CRITICAL scalar_type write_element( vec_type const & x ) { return vec_traits::template write_element(const_cast(x)); } static BOOST_QVM_INLINE_CRITICAL scalar_type read_element_idx( int i, vec_type const & x ) { return vec_traits::write_element_idx(i,const_cast(x)); } protected: static BOOST_QVM_INLINE_TRIVIAL scalar_type & write_element_idx( int i, vec_type & m ) { /* unspecified */ } }; } } ---- The `vec_traits_defaults` template is designed to be used as a public base for user-defined specializations of the <> template, to easily define the required members. If it is used, the only member that must be defined by the user in a `vec_traits` specialization is `write_element`; the `vec_traits_defaults` base will define `read_element`, as well as `scalar_type` and `dim` automatically. Optionally, the user may also define `write_element_idx`, in which case the `vec_traits_defaults` base will provide a suitable `read_element_idx` definition automatically. If not, `vec_traits_defaults` defines a protected implementation of `write_element_idx` which may be made publicly available by the deriving `vec_traits` specialization in case the vector type for which it is being specialized can not be indexed efficiently. This `write_element_idx` function is less efficient (using meta-programming), implemented in terms of the required user-defined `write_element`. ''' [[deduce_vec]] ==== `deduce_vec` .#include [source,c++] ---- namespace boost { namespace qvm { template ::dim> struct deduce_vec { typedef /*unspecified*/ type; }; } } ---- Requirements: :: - `<>::value` is `true`; - `is_vec::type>::value` must be `true`; - `deduce_vec::type` must be copyable; - `vec_traits::type>::dim==Dim`. This template is used by QVM whenever it needs to deduce a copyable vector type of certain dimension from a single user-supplied function parameter of vector type. The returned type must have accessible copy constructor. Note that `V` may be non-copyable. The main template definition returns an unspecified copyable vector type of size `Dim`, except if `<>::dim==Dim`, in which case it returns `V`, which is suitable only if `V` is a copyable type. QVM also defines (partial) specializations for the non-copyable vector types it produces. Users can define other (partial) specializations for their own types. A typical use of the `deduce_vec` template is for specifying the preferred vector type to be returned by the generic function template overloads in QVM depending on the type of their arguments. ''' [[deduce_vec2]] ==== `deduce_vec2` .#include [source,c++] ---- namespace boost { namespace qvm { template struct deduce_vec2 { typedef /*unspecified*/ type; }; } } ---- Requirements: :: - Both `<>::type` and `scalar::type` are well defined; - `<>::value || is_vec::value` is `true`; - `is_vec::type>::value` must be `true`; - `deduce_vec2::type` must be copyable; - `vec_traits::type>::dim==Dim`. This template is used by QVM whenever it needs to deduce a vector type of certain dimension from the types of two user-supplied function parameters. The returned type must have accessible copy constructor (the `A` and `B` types themselves could be non-copyable, and either one of them may not be a vector type.) The main template definition returns an unspecified vector type of the requested dimension with <> obtained by `<>::type`, except if `A` and `B` are the same vector type `V` of dimension `Dim`, in which case `V` is returned, which is only suitable for copyable types. QVM also defines (partial) specializations for the non-copyable vector types it produces. Users can define other (partial) specializations for their own types. A typical use of the `deduce_vec2` template is for specifying the preferred vector type to be returned by the generic function template overloads in QVM depending on the type of their arguments. ''' [[is_mat]] ==== `is_mat` .#include [source,c++] ---- namespace boost { namespace qvm { template struct is_mat { static bool const value = false; }; } } ---- This type template defines a compile-time boolean constant value which can be used to determine whether a type `T` is a matrix type. For matrix types, the <> template can be used to access their elements generically, or to obtain their dimensions and scalar type. ''' [[mat_traits]] ==== `mat_traits` .#include [source,c++] ---- namespace boost { namespace qvm { template struct mat_traits { /*main template members unspecified*/ }; /* User-defined (possibly partial) specializations: template <> struct mat_traits { static int const rows = <>; static int const cols = <>; typedef <> scalar_type; template static inline scalar_type read_element( Matrix const & m ); template static inline scalar_type & write_element( Matrix & m ); static inline scalar_typeread_element_idx( int r, int c, Matrix const & m ); static inline scalar_type & write_element_idx( int r, int c, Matrix & m ); }; */ } } ---- The `mat_traits` template must be specialized for (user-defined) matrix types in order to enable vector and matrix operations defined in QVM headers for objects of those types. NOTE: The matrix operations defined by QVM do not require matrix types to be copyable. The main `mat_traits` template members are not specified. Valid specializations are required to define the following members: - `rows`: the expression `mat_traits::rows` must evaluate to a compile-time integer constant greater than 0 that specifies the number of rows in a matrix. - `cols` must evaluate to a compile-time integer constant greater than 0 that specifies the number of columns in a matrix. - `scalar_type`: the expression `mat_traits::scalar_type` must be a value type which satisfies the scalar requirements. In addition, valid specializations of the `mat_traits` template may define the following access functions as static members, where `m` is an object of type `Matrix`, `R` and `C` are compile-time integer constants, and `r` and `c` are variables of type `int`: - `read_element`: the expression `mat_traits::read_element(m)` returns either a copy of or a const reference to the element at row `R` and column `C` of `m`. - `write_element`: the expression `mat_traits::write_element(m)` returns mutable reference to the element at row `R` and column `C` of `m`. - `read_element_idx`: the expression `mat_traits::read_element_idx(r,c,m)` returns either a copy of or a const reference to the element at row `r` and column `c` of `m`. - `write_element_idx`: the expression `mat_traits::write_element_idx(r,c,m)` returns mutable reference to the element at row `r` and column `c` of `m`. It is illegal to call any of the above functions unless `is_mat::value` is true. Even then, matrix types are allowed to define only a subset of the access functions. The general requirements are: - At least one of `read_element` or `write_element` must be defined; - If `read_element_idx` is defined, `read_element` must also be defined; - If `write_element_idx` is defined, `write_element` must also be defined. Below is an example of a user-defined 3x3 matrix type, and its corresponding specialization of the `mat_traits` template: [source,c++] ---- #include struct float33 { float a[3][3]; }; namespace boost { namespace qvm { template <> struct mat_traits { static int const rows=3; static int const cols=3; typedef float scalar_type; template static inline scalar_type & write_element( float33 & m ) { return m.a[R][C]; } template static inline scalar_type read_element( float33 const & m ) { return m.a[R][C]; } static inline scalar_type & write_element_idx( int r, int c, float33 & m ) { return m.a[r][c]; } static inline scalar_type read_element_idx( int r, int c, float33 const & m ) { return m.a[r][c]; } }; } } ---- Equivalently, we could use the < struct mat_traits: mat_traits_defaults { template static inline scalar_type & write_element( float33 & m ) { return m.a[R][C]; } static inline scalar_type & write_element_idx( int r, int c, float33 & m ) { return m.a[r][c]; } }; } } ---- ''' [[mat_traits_defaults]] ==== `mat_traits_defaults` .#include [source,c++] ---- namespace boost { namespace qvm { template struct mat_traits_defaults { typedef MatType mat_type; typedef ScalarType scalar_type; static int const rows=Rows; static int const cols=Cols; template static BOOST_QVM_INLINE_CRITICAL scalar_type write_element( mat_type const & x ) { return mat_traits::template write_element(const_cast(x)); } static BOOST_QVM_INLINE_CRITICAL scalar_type read_element_idx( int r, int c, mat_type const & x ) { return mat_traits::write_element_idx(r,c,const_cast(x)); } protected: static BOOST_QVM_INLINE_TRIVIAL scalar_type & write_element_idx( int r, int c, mat_type & m ) { /* unspecified */ } }; } } ---- The `mat_traits_defaults` template is designed to be used as a public base for user-defined specializations of the <> template, to easily define the required members. If it is used, the only member that must be defined by the user in a `mat_traits` specialization is `write_element`; the `mat_traits_defaults` base will define `read_element`, as well as `scalar_type`, `rows` and `cols` automatically. Optionally, the user may also define `write_element_idx`, in which case the `mat_traits_defaults` base will provide a suitable `read_element_idx` definition automatically. Otherwise, `mat_traits_defaults` defines a protected implementation of `write_element_idx` which may be made publicly available by the deriving `mat_traits` specialization in case the matrix type for which it is being specialized can not be indexed efficiently. This `write_element_idx` function is less efficient (using meta-programming), implemented in terms of the required user-defined `write_element`. ''' [[deduce_mat]] ==== `deduce_mat` .#include [source,c++] ---- namespace boost { namespace qvm { template < class M, int Rows=mat_traits::rows, int Cols=mat_traits::cols> struct deduce_mat { typedef /*unspecified*/ type; }; } } ---- Requirements: :: - `<>::value` is `true`; - `is_mat::type>::value` must be `true`; - `deduce_mat::type` must be copyable; - `<>::type>::rows==Rows`; - `mat_traits::type>::cols==Cols`. This template is used by QVM whenever it needs to deduce a copyable matrix type of certain dimensions from a single user-supplied function parameter of matrix type. The returned type must have accessible copy constructor. Note that M itself may be non-copyable. The main template definition returns an unspecified copyable matrix type of size `Rows` x `Cols`, except if `<>::rows==Rows && mat_traits::cols==Cols`, in which case it returns `M`, which is suitable only if `M` is a copyable type. QVM also defines (partial) specializations for the non-copyable matrix types it produces. Users can define other (partial) specializations for their own types. A typical use of the deduce_mat template is for specifying the preferred matrix type to be returned by the generic function template overloads in QVM depending on the type of their arguments. ''' [[deduce_mat2]] ==== `deduce_mat2` .#include [source,c++] ---- namespace boost { namespace qvm { template struct deduce_mat2 { typedef /*unspecified*/ type; }; } } ---- Requirements: :: - Both `<>::type` and `scalar::type` are well defined; - `<>::value || is_mat::value` is `true`; - `is_mat::type>::value` must be `true`; - `deduce_mat2::type` must be copyable; - `<>::type>::rows==Rows`; - `mat_traits::type>::cols==Cols`. This template is used by QVM whenever it needs to deduce a matrix type of certain dimensions from the types of two user-supplied function parameters. The returned type must have accessible copy constructor (the `A` and `B` types themselves could be non-copyable, and either one of them may be a non-matrix type.) The main template definition returns an unspecified matrix type of the requested dimensions with <> obtained by `<>::type`, except if `A` and `B` are the same matrix type `M` of dimensions `Rows` x `Cols`, in which case `M` is returned, which is only suitable for copyable types. QVM also defines (partial) specializations for the non-copyable matrix types it produces. Users can define other (partial) specializations for their own types. A typical use of the `deduce_mat2` template is for specifying the preferred matrix type to be returned by the generic function template overloads in QVM depending on the type of their arguments. ''' === Built-in Quaternion, Vector and Matrix Types QVM defines several class templates (together with appropriate specializations of <>, <> and <> templates) which can be used as generic quaternion, vector and matrix types. Using these types directly wouldn't be typical though, the main design goal of QVM is to allow users to plug in their own quaternion, vector and matrix types. [[quat]] ==== `quat` .#include [source,c++] ---- namespace boost { namespace qvm { template struct quat { T a[4]; template operator R() const { R r; assign(r,*this); return r; } }; template struct quat_traits; template struct quat_traits< quat > { typedef T scalar_type; template static scalar_type read_element( quat const & x ) { return x.a[I]; } template static scalar_type & write_element( quat & x ) { return x.a[I]; } }; } } ---- This is a simple quaternion type. It converts to any other quaternion type. The partial specialization of the <> template makes the `quat` template compatible with the generic operations defined by QVM. ''' [[vec]] ==== `vec` .#include [source,c++] ---- namespace boost { namespace qvm { template struct vec { T a[Dim]; template operator R() const { R r; assign(r,*this); return r; } }; template struct vec_traits; template struct vec_traits< vec > { typedef T scalar_type; static int const dim=Dim; template static scalar_type read_element( vec const & x ) { return x.a[I]; } template static scalar_type & write_element( vec & x ) { return x.a[I]; } static scalar_type read_element_idx( int i, vec const & x ) { return x.a[i]; } static scalar_type & write_element_idx( int i, vec & x ) { return x.a[i]; } }; } } ---- This is a simple vector type. It converts to any other vector type of compatible size. The partial specialization of the <> template makes the `vec` template compatible with the generic operations defined by QVM. ''' [[mat]] ==== `mat` .#include [source,c++] ---- namespace boost { namespace qvm { template struct mat { T a[Rows][Cols]; template operator R() const { R r; assign(r,*this); return r; } }; template struct mat_traits; template struct mat_traits< mat > { typedef T scalar_type; static int const rows=Rows; static int const cols=Cols; template static scalar_type read_element( mat const & x ) { return x.a[Row][Col]; } template static scalar_type & write_element( mat & x ) { return x.a[Row][Col]; } static scalar_type read_element_idx( int row, int col, mat const & x ) { return x.a[row][col]; } static scalar_type & write_element_idx( int row, int col, mat & x ) { return x.a[row][col]; } }; } } ---- This is a simple matrix type. It converts to any other matrix type of compatible size. The partial specialization of the <> template makes the `mat` template compatible with the generic operations defined by QVM. ''' === Element Access [[quat_access]] ==== Quaternions .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: // is_quat::value template -unspecified-return-type- S( Q & q ); template -unspecified-return-type- V( Q & q ); template -unspecified-return-type- X( Q & q ); template -unspecified-return-type- Y( Q & q ); template -unspecified-return-type- Z( Q & q ); } } ---- An expression of the form `S(q)` can be used to access the scalar component of the quaternion `q`. For example, [source,c++] ---- S(q) *= 42; ---- multiplies the scalar component of `q` by the scalar 42. An expression of the form `V(q)` can be used to access the vector component of the quaternion `q`. For example, [source,c++] ---- V(q) *= 42 ---- multiplies the vector component of `q` by the scalar 42. The `X`, `Y` and `Z` elements of the vector component can also be accessed directly using `X(q)`, `Y(q)` and `Z(q)`. TIP: The return types are lvalues. [[vec_access]] ==== Vectors .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: // is_vec::value template -unspecified-return-type- A( V & v ); template -unspecified-return-type- A0( V & v ); template -unspecified-return-type- A1( V & v ); ... template -unspecified-return-type- A9( V & v ); template -unspecified-return-type- X( V & v ); template -unspecified-return-type- Y( V & v ); template -unspecified-return-type- Z( V & v ); template -unspecified-return-type- W( V & v ); } } ---- An expression of the form of `A(v)` can be used to access the `I`-th element a vector object `v`. For example, the expression: [source,c++] ---- A<1>(v) *= 42; ---- can be used to multiply the element at index 1 (indexing in QVM is always zero-based) of a vector `v` by 42. For convenience, there are also non-template overloads for `I` from 0 to 9; an alternative way to write the above expression is: [source,c++] ---- A1(v) *= 42; ---- `X`, `Y`, `Z` and `W` act the same as `A0`/`A1`/`A2`/`A3`; yet another alternative way to write the above expression is: [source,c++] ---- Y(v) *= 42; ---- TIP: The return types are lvalues. [[swizzling]] ==== Vector Element Swizzling .#include [source,c++] ---- namespace boost { namespace qvm { //*** Accessing vector elements by swizzling *** //2D view proxies, only enabled if: // is_vec::value template -unspecified-2D-vector-type- XX( V & v ); template -unspecified-2D-vector-type- XY( V & v ); template -unspecified-2D-vector-type- XZ( V & v ); template -unspecified-2D-vector-type- XW( V & v ); template -unspecified-2D-vector-type- X0( V & v ); template -unspecified-2D-vector-type- X1( V & v ); template -unspecified-2D-vector-type- YX( V & v ); template -unspecified-2D-vector-type- YY( V & v ); template -unspecified-2D-vector-type- YZ( V & v ); template -unspecified-2D-vector-type- YW( V & v ); template -unspecified-2D-vector-type- Y0( V & v ); template -unspecified-2D-vector-type- Y1( V & v ); template -unspecified-2D-vector-type- ZX( V & v ); template -unspecified-2D-vector-type- ZY( V & v ); template -unspecified-2D-vector-type- ZZ( V & v ); template -unspecified-2D-vector-type- ZW( V & v ); template -unspecified-2D-vector-type- Z0( V & v ); template -unspecified-2D-vector-type- Z1( V & v ); template -unspecified-2D-vector-type- WX( V & v ); template -unspecified-2D-vector-type- WY( V & v ); template -unspecified-2D-vector-type- WZ( V & v ); template -unspecified-2D-vector-type- WW( V & v ); template -unspecified-2D-vector-type- W0( V & v ); template -unspecified-2D-vector-type- W1( V & v ); ... //2D view proxies, only enabled if: // is_scalar::value template -unspecified-2D-vector-type- X0( S & s ); template -unspecified-2D-vector-type- X1( S & s ); template -unspecified-2D-vector-type- XX( S & s ); ... -unspecified-2D-vector-type- _00(); -unspecified-2D-vector-type- _01(); -unspecified-2D-vector-type- _10(); -unspecified-2D-vector-type- _11(); //3D view proxies, only enabled if: // is_vec::value template -unspecified-3D-vector-type- XXX( V & v ); ... template -unspecified-3D-vector-type- XXW( V & v ); template -unspecified-3D-vector-type- XX0( V & v ); template -unspecified-3D-vector-type- XX1( V & v ); template -unspecified-3D-vector-type- XYX( V & v ); ... template -unspecified-3D-vector-type- XY1( V & v ); ... template -unspecified-3D-vector-type- WW1( V & v ); ... //3D view proxies, only enabled if: // is_scalar::value template -unspecified-3D-vector-type- X00( S & s ); template -unspecified-3D-vector-type- X01( S & s ); ... template -unspecified-3D-vector-type- XXX( S & s ); template -unspecified-3D-vector-type- XX0( S & s ); ... -unspecified-3D-vector-type- _000(); -unspecified-3D-vector-type- _001(); -unspecified-3D-vector-type- _010(); ... -unspecified-3D-vector-type- _111(); //4D view proxies, only enabled if: // is_vec::value template -unspecified-4D-vector-type- XXXX( V & v ); ... template -unspecified-4D-vector-type- XXXW( V & v ); template -unspecified-4D-vector-type- XXX0( V & v ); template -unspecified-4D-vector-type- XXX1( V & v ); template -unspecified-4D-vector-type- XXYX( V & v ); ... template -unspecified-4D-vector-type- XXY1( V & v ); ... template -unspecified-4D-vector-type- WWW1( V & v ); ... //4D view proxies, only enabled if: // is_scalar::value template -unspecified-4D-vector-type- X000( S & s ); template -unspecified-4D-vector-type- X001( S & s ); ... template -unspecified-4D-vector-type- XXXX( S & s ); template -unspecified-4D-vector-type- XX00( S & s ); ... -unspecified-4D-vector-type- _0000(); -unspecified-4D-vector-type- _0001(); -unspecified-4D-vector-type- _0010(); ... -unspecified-4D-vector-type- _1111(); } } ---- Swizzling allows zero-overhead direct access to a (possibly rearranged) subset of the elements of 2D, 3D and 4D vectors. For example, if `v` is a 4D vector, the expression `YX(v) is a 2D view proxy whose `X` element refers to the `Y` element of `v`, and whose `Y` element refers to the `X` element of `v`. Like other view proxies `YX` is an lvalue, that is, if `v2` is a 2D vector, one could write: [source,c++] ---- YX(v) = v2; ---- The above will leave the `Z` and `W` elements of `v` unchanged but assign the `Y` element of `v2` to the `X` element of `v` and the `X` element of `v2` to the `Y` element of `v`. All permutations of `X`, `Y`, `Z`, `W`, `0`, `1` for 2D, 3D and 4D swizzling are available (if the first character of the swizzle identifier is `0` or `1`, it is preceded by a `_`, for example `_11XY`). It is valid to use the same vector element more than once: the expression `ZZZ(v)` is a 3D vector whose `X`, `Y` and `Z` elements all refer to the `Z` element of `v`. Finally, scalars can be "swizzled" to access them as vectors: the expression `_0X01(42.0f)` is a 4D vector with `X`=0, `Y`=42.0, `Z`=0, `W`=1. [[mat_access]] ==== Matrices .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: // is_quat::value template -unspecified-return-type- A( M & m ); template -unspecified-return-type- A00( M & m ); template -unspecified-return-type- A01( M & m ); ... template -unspecified-return-type- A09( M & m ); template -unspecified-return-type- A10( M & m ); ... template -unspecified-return-type- A99( M & m ); } } ---- An expression of the form `A(m)` can be used to access the element at row `R` and column `C` of a matrix object `m`. For example, the expression: [source,c++] ---- A<4,2>(m) *= 42; ---- can be used to multiply the element at row 4 and column 2 of a matrix `m` by 42. For convenience, there are also non-template overloads for `R` from `0` to `9` and `C` from `0` to `9`; an alternative way to write the above expression is: [source,c++] ---- A42(m) *= 42; ---- TIP: The return types are lvalues. ''' === Quaternion Operations [[quat_assign]] ==== `assign` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: // is_quat::value && is_quat::value template A & assign( A & a, B const & b ); } } ---- Effects: :: Copies all elements of the quaternion `b` to the quaternion `a`. Returns: :: `a`. ''' [[quat_convert_to]] ==== `convert_to` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: // is_quat::value && is_quat::value template R convert_to( A const & a ); //Only enabled if: // is_quat::value && is_mat::value && // mat_traits::rows==3 && mat_traits::cols==3 template R convert_to( A const & m ); } } ---- Requirements: :: `R` must be copyable. Effects: :: - The first overload is equivalent to: `R r; assign(r,a); return r;` - The second overload assumes that `m` is an orthonormal rotation matrix and converts it to a quaternion that performs the same rotation. ''' [[quat_minus_eq]] ==== `operator-=` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: // is_quat::value && is_quat::value template A & operator-=( A & a, B const & b ); } } ---- Effects: :: Subtracts the elements of `b` from the corresponding elements of `a`. Returns: :: `a`. ''' [[quat_minus_unary]] ==== `operator-` (unary) .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: is_quat::value template typename deduce_quat::type operator-( A const & a ); } } ---- Returns: :: A quaternion of the negated elements of `a`. NOTE: The <> template can be specialized to deduce the desired return type from the type `A`. ''' [[quat_minus]] ==== `operator-` (binary) .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: // is_quat::value && is_quat::value template typename deduce_quat2::type operator-( A const & a, B const & b ); } } ---- Returns: :: A quaternion with elements equal to the elements of `b` subtracted from the corresponding elements of `a`. NOTE: The <> template can be specialized to deduce the desired return type, given the types `A` and `B`. ''' [[quat_plus_eq]] ==== `operator+=` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: // is_quat::value && is_quat::value template A & operator+=( A & a, B const & b ); } } ---- Effects: :: Adds the elements of `b` to the corresponding elements of `a`. Returns: :: `a`. ''' [[quat_plus]] ==== `operator+` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: // is_quat::value && is_quat::value && template typename deduce_quat2::type operator+( A const & a, B const & b ); } } ---- Returns: :: A quaternion with elements equal to the elements of `a` added to the corresponding elements of `b`. NOTE: The <> template can be specialized to deduce the desired return type, given the types `A` and `B`. ''' [[quat_div_eq_scalar]] ==== `operator/=` (scalar) .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: is_quat::value && is_scalar::value template A & operator/=( A & a, B b ); } } ---- Effects: :: This operation divides a quaternion by a scalar. Returns: :: `a`. ''' [[quat_div_scalar]] ==== `operator/` (scalar) .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: is_quat::value && is_scalar::value template typename deduce_quat::type operator/( A const & a, B b ); } } ---- Returns: :: A quaternion that is the result of dividing the quaternion `a` by the scalar `b`. NOTE: The <> template can be specialized to deduce the desired return type from the type `A`. ''' [[quat_mul_eq_scalar]] ==== `operator*=` (scalar) .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: is_quat::value && is_scalar::value template A & operator*=( A & a, B b ); } } ---- Effects: :: This operation multiplies the quaternion `a` by the scalar `b`. Returns: :: `a`. ''' [[quat_mul_eq]] ==== `operator*=` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: // is_quat::value && is_quat::value template A & operator*=( A & a, B const & b ); } } ---- Effects: :: As if: + [source,c++] ---- A tmp(a); a = tmp * b; return a; ---- ''' [[quat_mul_scalar]] ==== `operator*` (scalar) .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: is_quat::value && is_scalar::value template typename deduce_quat::type operator*( A const & a, B b ); } } ---- Returns: :: A quaternion that is the result of multiplying the quaternion `a` by the scalar `b`. NOTE: The <> template can be specialized to deduce the desired return type from the type `A`. ''' [[quat_mul]] ==== `operator*` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: // is_quat::value && is_quat::value template typename deduce_quat2::type operator*( A const & a, B const & b ); } } ---- Returns: :: The result of multiplying the quaternions `a` and `b`. NOTE: The <> template can be specialized to deduce the desired return type, given the types `A` and `B`. ''' [[quat_eq]] ==== `operator==` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: // is_quat::value && is_quat::value template bool operator==( A const & a, B const & b ); } } ---- Returns: :: `true` if each element of `a` compares equal to its corresponding element of `b`, `false` otherwise. ''' [[quat_neq]] ==== `operator!=` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: // is_quat::value && is_quat::value template bool operator!=( A const & a, B const & b ); } } ---- Returns: :: `!(a == b)`. ''' [[quat_cmp]] ==== `cmp` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: // is_quat::value && is_quat::value template bool cmp( A const & a, B const & b, Cmp pred ); } } ---- Returns: :: Similar to <>, except that it uses the binary predicate `pred` to compare the individual quaternion elements. ''' [[quat_mag_sqr]] ==== `mag_sqr` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: is_quat::value template typename quat_traits::scalar_type mag_sqr( A const & a ); } } ---- Returns: :: The squared magnitude of the quaternion `a`. ''' [[quat_mag]] ==== `mag` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: is_quat::value template typename quat_traits::scalar_type mag( A const & a ); } } ---- Returns: :: The magnitude of the quaternion `a`. ''' [[quat_normalized]] ==== `normalized` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: is_quat::value template typename deduce_quat::type normalized( A const & a ); } } ---- Effects: :: As if: + [source,c++] ---- typename deduce_quat::type tmp; assign(tmp,a); normalize(tmp); return tmp; ---- NOTE: The <> template can be specialized to deduce the desired return type from the type `A`. ''' [[quat_normalize]] ==== `normalize` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: is_quat::value template void normalize( A & a ); } } ---- Effects: :: Normalizes `a`. Postcondition: :: `mag(a)==scalar_traits::scalar_type>::value(1).` Throws: :: If the magnitude of `a` is zero, throws <>. ''' [[quat_dot]] ==== `dot` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: // is_quat::value && is_quat::value template typename deduce_scalar::type dot( A const & a, B const & b ); } } ---- Returns: :: The dot product of the quaternions `a` and `b`. NOTE: The <> template can be specialized to deduce the desired return type, given the types `A` and `B`. ''' [[conjugate]] ==== `conjugate` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: is_quat::value template typename deduce_quat::type conjugate( A const & a ); } } ---- Returns: :: Computes the conjugate of `a`. NOTE: The <> template can be specialized to deduce the desired return type from the type `A`. ''' [[quat_inverse]] ==== `inverse` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: is_quat::value template typename deduce_quat::type inverse( A const & a ); } } ---- Returns: :: Computes the multiplicative inverse of `a`, or the conjugate-to-norm ratio. Throws: :: If the magnitude of `a` is zero, throws <>. TIP: If `a` is known to be unit length, `conjugate` is equivalent to <>, yet it is faster to compute. NOTE: The <> template can be specialized to deduce the desired return type from the type `A`. ''' [[slerp]] ==== `slerp` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: // is_quat::value && is_quat::value && is_scalar template typename deduce_quat2 >::type slerp( A const & a, B const & b, C c ); } } ---- Preconditions: :: `t>=0 && t\<=1`. Returns: :: A quaternion that is the result of Spherical Linear Interpolation of the quaternions `a` and `b` and the interpolation parameter `c`. When `slerp` is applied to unit quaternions, the quaternion path maps to a path through 3D rotations in a standard way. The effect is a rotation with uniform angular velocity around a fixed rotation axis. NOTE: The <> template can be specialized to deduce the desired return type, given the types `A` and `B`. ''' [[zero_quat]] ==== `zero_quat` .#include [source,c++] ---- namespace boost { namespace qvm { template -unspecified-return-type- zero_quat(); } } ---- Returns: :: A read-only quaternion of unspecified type with <> `T`, with all elements equal to <::value(0)`>>. ''' [[quat_set_zero]] ==== `set_zero` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: is_quat::value template void set_zero( A & a ); } } ---- Effects: :: As if: + [source,c++] ---- assign(a, zero_quat::scalar_type>()); ---- ''' [[identity_quat]] ==== `identity_quat` .#include [source,c++] ---- namespace boost { namespace qvm { template -unspecified-return-type- identity_quat(); } } ---- Returns: :: An identity quaternion with scalar type `S`. ''' [[quat_set_identity]] ==== `set_identity` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: is_quat::value template void set_identity( A & a ); } } ---- Effects: :: As if: + [source,c++] ---- assign( a, identity_quat::scalar_type>()); ---- ''' [[rot_quat]] ==== `rot_quat` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: // is_vec::value && vec_traits::dim==3 template -unspecified-return-type- rot_quat( A const & axis, typename vec_traits::scalar_type angle ); } } ---- Returns: :: A quaternion of unspecified type which performs a rotation around the `axis` at `angle` radians. Throws: :: In case the axis vector has zero magnitude, throws <>. NOTE: The `rot_quat` function is not a <>; it returns a temp object. ''' [[quat_set_rot]] ==== `set_rot` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: // is_quat::value && // is_vec::value && vec_traits::dim==3 template void set_rot( A & a, B const & axis, typename vec_traits::scalar_type angle ); } } ---- Effects: :: As if: + [source,c++] ---- assign( a, rot_quat(axis,angle)); ---- ''' [[quat_rotate]] ==== `rotate` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: // is_quat::value && // is_vec::value && vec_traits::dim==3 template void rotate( A & a, B const & axis, typename quat_traits::scalar_type angle ); } } ---- Effects: :: As if: `a *= <>(axis,angle)`. ''' [[rotx_quat]] ==== `rotx_quat` .#include [source,c++] ---- namespace boost { namespace qvm { template -unspecified-return-type- rotx_quat( Angle const & angle ); } } ---- Returns: :: A <> quaternion of unspecified type and scalar type `Angle`, which performs a rotation around the X axis at `angle` radians. ''' [[quat_set_rotx]] ==== `set_rotx` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: is_quat::value template void set_rotx( A & a, typename quat_traits::scalar_type angle ); } } ---- Effects: :: As if: + [source,c++] ---- assign( a, rotx_quat(angle)); ---- ''' [[quat_rotate_x]] ==== `rotate_x` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: is_quat::value template void rotate_x( A & a, typename quat_traits::scalar_type angle ); } } ---- Effects: :: As if: `a *= <>(angle)`. ''' [[roty_quat]] ==== `roty_quat` .#include [source,c++] ---- namespace boost { namespace qvm { template -unspecified-return-type- roty_quat( Angle const & angle ); } } ---- Returns: :: A <> quaternion of unspecified type and scalar type `Angle`, which performs a rotation around the Y axis at `angle` radians. ''' [[quat_set_roty]] ==== `set_roty` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: is_quat::value template void set_rotz( A & a, typename quat_traits::scalar_type angle ); } } ---- Effects: :: As if: + [source,c++] ---- assign( a, roty_quat(angle)); ---- ''' [[quat_rotate_y]] ==== `rotate_y` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: is_quat::value template void rotate_y( A & a, typename quat_traits::scalar_type angle ); } } ---- Effects: :: As if: `a *= <>(angle)`. ''' [[rotz_quat]] ==== `rotz_quat` .#include [source,c++] ---- namespace boost { namespace qvm { template -unspecified-return-type- rotz_quat( Angle const & angle ); } } ---- Returns: :: A <> quaternion of unspecified type and scalar type `Angle`, which performs a rotation around the Z axis at `angle` radians. ''' [[quat_set_rotz]] ==== `set_rotz` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: is_quat::value template void set_rotz( A & a, typename quat_traits::scalar_type angle ); } } ---- Effects: :: As if: + [source,c++] ---- assign( a, rotz_quat(angle)); ---- ''' [[quat_rotate_z]] ==== `rotate_z` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: is_quat::value template void rotate_z( A & a, typename quat_traits::scalar_type angle ); } } ---- Effects: :: As if: `a *= <>(angle)`. ''' [[quat_scalar_cast]] ==== `scalar_cast` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: is_quat::value template -unspecified-return_type- scalar_cast( A const & a ); } } ---- Returns: :: A read-only <> of `a` that looks like a quaternion of the same dimensions as `a`, but with <> `Scalar` and elements constructed from the corresponding elements of `a`. ''' [[qref]] ==== `qref` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: is_quat::value template -unspecified-return-type- qref( A & a ); } } ---- Returns: :: An identity view proxy of `a`; that is, it simply accesses the elements of `a`. TIP: `qref` allows calling QVM operations when `a` is of built-in type, for example a plain old C array. ''' === Vector Operations [[vec_assign]] ==== `assign` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: // is_vec::value && is_vec::value && // vec_traits::dim==vec_traits::dim template A & assign( A & a, B const & b ); } } ---- Effects: :: Copies all elements of the vector `b` to the vector `a`. Returns: :: `a`. ''' [[vec_convert_to]] ==== `convert_to` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: // is_vec::value && is_vec::value && // vec_traits::dim==vec_traits::dim template R convert_to( A const & a ); } } ---- Requirements: :: `R` must be copyable. Effects: :: As if: `R r; assign(r,a); return r;` ''' [[vec_minus_eq]] ==== `operator-=` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: // is_vec::value && is_vec::value && // vec_traits::dim==vec_traits::dim template A & operator-=( A & a, B const & b ); } } ---- Effects: :: Subtracts the elements of `b` from the corresponding elements of `a`. Returns: :: `a`. ''' [[vec_minus_unary]] ==== `operator-` (unary) operator-(vec) .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: is_vec::value template typename deduce_vec::type operator-( A const & a ); } } ---- Returns: :: A vector of the negated elements of `a`. NOTE: The <> template can be specialized to deduce the desired return type from the type `A`. ''' [[vec_minus]] ==== `operator-` (binary) .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: // is_vec::value && is_vec::value && // vec_traits::dim==vec_traits::dim template typename deduce_vec2::dim>::type operator-( A const & a, B const & b ); } } ---- Returns: :: A vector of the same size as `a` and `b`, with elements the elements of `b` subtracted from the corresponding elements of `a`. NOTE: The <> template can be specialized to deduce the desired return type, given the types `A` and `B`. ''' [[vec_plus_eq]] ==== `operator+=` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: // is_vec::value && is_vec::value && // vec_traits::dim==vec_traits::dim template A & operator+=( A & a, B const & b ); } } ---- Effects: :: Adds the elements of `b` to the corresponding elements of `a`. Returns: :: `a`. ''' [[vec_plus]] ==== `operator+` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: // is_vec::value && is_vec::value && // vec_traits::dim==vec_traits::dim template typename deduce_vec2::dim>::type operator+( A const & a, B const & b ); } } ---- Returns: :: A vector of the same size as `a` and `b`, with elements the elements of `b` added to the corresponding elements of `a`. NOTE: The <> template can be specialized to deduce the desired return type, given the types `A` and `B`. ''' [[vec_div_eq_scalar]] ==== `operator/=` (scalar) .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: is_vec::value && is_scalar::value template A & operator/=( A & a, B b ); } } ---- Effects: :: This operation divides a vector by a scalar. Returns: :: `a`. ''' [[vec_div_scalar]] ==== `operator/` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: is_vec::value && is_scalar::value template typename deduce_vec::type operator/( A const & a, B b ); } } ---- Returns: :: A vector that is the result of dividing the vector `a` by the scalar `b`. NOTE: The <> template can be specialized to deduce the desired return type from the type `A`. ''' [[vec_mul_eq_scalar]] ==== `operator*=` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: is_vec::value && is_scalar::value template A & operator*=( A & a, B b ); } } ---- Effects: :: This operation multiplies the vector `a` by the scalar `b`. Returns: :: `a`. ''' [[vec_mul_scalar]] ==== `operator*` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: is_vec::value && is_scalar::value template typename deduce_vec::type operator*( A const & a, B b ); } } ---- Returns: :: A vector that is the result of multiplying the vector `a` by the scalar `b`. NOTE: The <> template can be specialized to deduce the desired return type from the type `A`. ''' [[vec_eq]] ==== `operator==` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: // is_vec::value && is_vec::value && // vec_traits::dim==vec_traits::dim template bool operator==( A const & a, B const & b ); } } ---- Returns: :: `true` if each element of `a` compares equal to its corresponding element of `b`, `false` otherwise. ''' [[vec_neq]] ==== `operator!=` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: // is_vec::value && is_vec::value && // vec_traits::dim==vec_traits::dim template bool operator!=( A const & a, B const & b ); } } ---- Returns: :: `!(a == b)`. ''' [[vec_cmp]] ==== `cmp` ---- .#include namespace boost { namespace qvm { //Only enabled if: // is_mat::value && is_mat::value && // mat_traits::rows==mat_traits::rows && // mat_traits::cols==mat_traits::cols template bool cmp( A const & a, B const & b, Cmp pred ); } } ---- Returns: :: Similar to <>, except that the individual elements of `a` and `b` are passed to the binary predicate `pred` for comparison. ''' [[vec_mag_sqr]] ==== `mag_sqr` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: // is_vec::value template typename vec_traits::scalar_type mag_sqr( A const & a ); } } ---- Returns: :: The squared magnitude of the vector `a`. ''' [[vec_mag]] ==== `mag` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: // is_vec::value template typename vec_traits::scalar_type mag( A const & a ); } } ---- Returns: :: The magnitude of the vector `a`. ''' [[vec_normalized]] ==== `normalized` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: // is_vec::value template typename deduce_vec::type normalized( A const & a ); } } ---- Effects: :: As if: + [source,c++] ---- typename deduce_vec::type tmp; assign(tmp,a); normalize(tmp); return tmp; ---- NOTE: The <> template can be specialized to deduce the desired return type from the type `A`. ''' [[vec_normalize]] ==== `normalize` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: // is_vec::value template void normalize( A & a ); } } ---- Effects: :: Normalizes `a`. Postcondition: `mag(a)==<>::scalar_type>>>::value(1)`. Throws: :: If the magnitude of `a` is zero, throws <>. ''' [[vec_dot]] ==== `dot` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: // is_vec::value && is_vec::value && // vec_traits::dim==vec_traits::dim template typename deduce_scalar::type dot( A const & a, B const & b ); } } ---- Returns: :: The dot product of the vectors `a` and `b`. NOTE: The <> template can be specialized to deduce the desired return type, given the types `A` and `B`. ''' [[vec_cross]] ==== `cross` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: // is_vec::value && is_vec::value && // vec_traits::dim==3 && vec_traits::dim==3 template typename deduce_vec2::type cross( A const & a, B const & b ); } } ---- Returns: :: The cross product of the vectors `a` and `b`. NOTE: The <> template can be specialized to deduce the desired return type, given the types `A` and `B`. ''' [[zero_vec]] ==== `zero_vec` .#include [source,c++] ---- namespace boost { namespace qvm { template -unspecified-return-type- zero_vec(); } } ---- Returns: :: A read-only vector of unspecified type with <> `T` and size `S`, with all elements equal to <::value(0)`>>. ''' [[vec_set_zero]] ==== `set_zero` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: // is_vec::value template void set_zero( A & a ); } } ---- Effects: :: As if: + [source,c++] ---- assign(a, zero_vec< typename vec_traits::scalar_type, vec_traits::dim>()); ---- ''' [[vec_scalar_cast]] ==== `scalar_cast` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: is_vec::value template -unspecified-return_type- scalar_cast( A const & a ); } } ---- Returns: :: A read-only <> of `a` that looks like a vector of the same dimensions as `a`, but with <> `Scalar` and elements constructed from the corresponding elements of `a`. ''' [[vref]] ==== `vref` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: is_vec::value template -unspecified-return-type- vref( A & a ); } } ---- Returns: :: An identity <> of `a`; that is, it simply accesses the elements of `a`. TIP: `vref` allows calling QVM operations when `a` is of built-in type, for example a plain old C array. ''' === Matrix Operations [[mat_assign]] ==== `assign` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: // is_mat::value && is_mat::value && // mat_traits::rows==mat_traits::rows && // mat_traits::cols==mat_traits::cols template A & assign( A & a, B const & b ); } } ---- Effects: :: Copies all elements of the matrix `b` to the matrix `a`. Returns: :: `a`. ''' [[mat_convert_to]] ==== `convert_to` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: // is_mat::value && is_mat::value && // mat_traits::rows==mat_traits::rows && // mat_traits::cols==mat_traits::cols template R convert_to( A const & a ); } } ---- Requirements: :: `R` must be copyable. Effects: As if: `R r; <>(r,a); return r;` ''' [[mat_minus_eq_scalar]] ==== `operator-=` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: // is_mat::value && is_mat::value && // mat_traits::rows==mat_traits::rows && // mat_traits::cols==mat_traits::cols template A & operator-=( A & a, B const & b ); } } ---- Effects: :: Subtracts the elements of `b` from the corresponding elements of `a`. Returns: :: `a`. ''' [[mat_minus_unary]] ==== `operator-` (unary) .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: is_mat::value template typename deduce_mat::type operator-( A const & a ); } } ---- Returns: :: A matrix of the negated elements of `a`. NOTE: The <> template can be specialized to deduce the desired return type from the type `A`. ''' [[mat_minus]] ==== `operator-` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: // is_mat::value && is_mat::value && // mat_traits::rows==mat_traits::rows && // mat_traits::cols==mat_traits::cols template typename deduce_mat2::rows,mat_traits::cols>::type operator-( A const & a, B const & b ); } } ---- Returns: :: A matrix of the same size as `a` and `b`, with elements the elements of `b` subtracted from the corresponding elements of `a`. NOTE: The <> template can be specialized to deduce the desired return type, given the types `A` and `B`. ''' [[mat_plus_eq_scalar]] ==== `operator+=` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: // is_mat::value && is_mat::value && // mat_traits::rows==mat_traits::rows && // mat_traits::cols==mat_traits::cols template A & operator+=( A & a, B const & b ); } } ---- Effects: :: Adds the elements of `b` to the corresponding elements of `a`. Returns: :: `a`. ''' [[mat_plus]] ==== `operator+` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: // is_mat::value && is_mat::value && // mat_traits::rows==mat_traits::rows && // mat_traits::cols==mat_traits::cols template typename deduce_mat2::rows,mat_traits::cols>::type operator+( A const & a, B const & b ); } } ---- Returns: :: A matrix of the same size as `a` and `b`, with elements the elements of `b` added to the corresponding elements of `a`. NOTE: The <> template can be specialized to deduce the desired return type, given the types `A` and `B`. ''' [[mat_div_eq_scalar]] ==== `operator/=` (scalar) .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: is_mat::value && is_scalar::value template A & operator/=( A & a, B b ); } } ---- Effects: :: This operation divides a matrix by a scalar. Returns: :: `a`. ''' [[mat_div_scalar]] ==== `operator/` (scalar) .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: is_mat::value && is_scalar::value template typename deduce_mat::type operator/( A const & a, B b ); } } ---- Returns: :: A matrix that is the result of dividing the matrix `a` by the scalar `b`. NOTE: The <> template can be specialized to deduce the desired return type from the type `A`. ''' [[mat_mul_eq]] ==== `operator*=` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: // is_mat::value && is_mat::value && // mat_traits::rows==mat_traits::cols && // mat_traits::rows==mat_traits::rows && // mat_traits::cols==mat_traits::cols template A & operator*=( A & a, B const & b ); } } ---- Effects: :: As if: + [source,c++] ---- A tmp(a); a = tmp * b; return a; ---- ''' [[mat_mul_eq_scalar]] ==== `operator*=` (scalar) .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: is_mat::value && is_scalar::value template A & operator*=( A & a, B b ); } } ---- Effects: :: This operation multiplies the matrix `a` matrix by the scalar `b`. Returns: :: `a`. ''' [[mat_mul]] ==== `operator*` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: // is_mat::value && is_mat::value && // mat_traits::cols==mat_traits::rows template typename deduce_mat2::rows,mat_traits::cols>::type operator*( A const & a, B const & b ); } } ---- Returns: :: The result of https://en.wikipedia.org/wiki/Matrix_multiplication[multiplying] the matrices `a` and `b`. NOTE: The <> template can be specialized to deduce the desired return type, given the types `A` and `B`. ''' [[mat_mul_scalar]] ==== `operator*` (scalar) .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: is_mat::value && is_scalar::value template typename deduce_mat::type operator*( A const & a, B b ); //Only enabled if: is_scalar::value && is_mat::value template typename deduce_mat::type operator*( B b, A const & a ); } } ---- Returns: :: A matrix that is the result of multiplying the matrix `a` by the scalar `b`. NOTE: The <> template can be specialized to deduce the desired return type from the type `A`. ''' [[mat_eq]] ==== `operator==` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: // is_mat::value && is_mat::value && // mat_traits::rows==mat_traits::rows && // mat_traits::cols==mat_traits::cols template bool operator==( A const & a, B const & b ); } } ---- Returns: :: `true` if each element of `a` compares equal to its corresponding element of `b`, `false` otherwise. ''' [[mat_neq]] ==== `operator!=` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: // is_mat::value && is_mat::value && // mat_traits::rows==mat_traits::rows && // mat_traits::cols==mat_traits::cols template bool operator!=( A const & a, B const & b ); } } ---- Returns: :: `!( a <> b )`. ''' [[mat_cmp]] ==== `cmp` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: // is_mat::value && is_mat::value && // mat_traits::rows==mat_traits::rows && // mat_traits::cols==mat_traits::cols template bool cmp( A const & a, B const & b, Cmp pred ); } } ---- Returns: :: Similar to <>, except that the individual elements of `a` and `b` are passed to the binary predicate `pred` for comparison. ''' [[mat_inverse]] ==== `inverse` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: // is_mat::value && is_scalar::value // mat_traits::rows==mat_traits::cols template typename deduce_mat::type inverse( A const & a, B det ); template typename deduce_mat::type inverse( A const & a ); } } ---- Preconditions: :: `det!=<>::scalar_type>>>::value(0)` Returns: :: Both overloads compute the inverse of `a`. The first overload takes the pre-computed determinant of `a`. Throws: :: The second overload computes the determinant automatically and throws <> if the computed determinant is zero. NOTE: The <> template can be specialized to deduce the desired return type from the type `A`. ''' [[zero_mat]] ==== `zero_mat` .#include [source,c++] ---- namespace boost { namespace qvm { template -unspecified-return-type- zero_mat(); template -unspecified-return-type- zero_mat(); } } ---- Returns: :: A read-only matrix of unspecified type with <> `T`, `R` rows and `C` columns (or `D` rows and `D` columns), with all elements equal to <::value(0)`>>. ''' [[mat_set_zero]] ==== `set_zero` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: // is_mat::value template void set_zero( A & a ); } } ---- Effects: :: As if: + [source,c++] ---- assign(a, zero_mat< typename mat_traits::scalar_type, mat_traits::rows, mat_traits::cols>()); ---- ''' [[identity_mat]] ==== `identity_mat` .#include ---- namespace boost { namespace qvm { template -unspecified-return-type- identity_mat(); } } ---- Returns: :: An identity matrix of size `D` x `D` and scalar type `S`. ''' [[mat_set_identity]] ==== `set_identity` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: // is_mat::value && // mat_traits::cols==mat_traits::rows template void set_identity( A & a ); } } ---- Effects: :: As if: + [source,c++] ---- assign( a, identity_mat< typename mat_traits::scalar_type, mat_traits::rows, mat_traits::cols>()); ---- ''' [[rot_mat]] ==== `rot_mat` / Euler angles .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: // is_vec::value && vec_traits::dim==3 template -unspecified-return-type- rot_mat( A const & axis, Angle angle ); template -unspecified-return-type- rot_mat_xzy( Angle x1, Angle z2, Angle y3 ); template -unspecified-return-type- rot_mat_xyz( Angle x1, Angle y2, Angle z3 ); template -unspecified-return-type- rot_mat_yxz( Angle y1, Angle x2, Angle z3 ); template -unspecified-return-type- rot_mat_yzx( Angle y1, Angle z2, Angle x3 ); template -unspecified-return-type- rot_mat_zyx( Angle z1, Angle y2, Angle x3 ); template -unspecified-return-type- rot_mat_zxy( Angle z1, Angle x2, Angle y3 ); template -unspecified-return-type- rot_mat_xzx( Angle x1, Angle z2, Angle x3 ); template -unspecified-return-type- rot_mat_xyx( Angle x1, Angle y2, Angle x3 ); template -unspecified-return-type- rot_mat_yxy( Angle y1, Angle x2, Angle y3 ); template -unspecified-return-type- rot_mat_yzy( Angle y1, Angle z2, Angle y3 ); template -unspecified-return-type- rot_mat_zyz( Angle z1, Angle y2, Angle z3 ); template -unspecified-return-type- rot_mat_zxz( Angle z1, Angle y2, Angle z3 ); } } ---- Returns: :: A matrix of unspecified type, of `Dim` rows and `Dim` columns parameter, which performs a rotation around the `axis` at `angle` radians, or Tait–Bryan angles (x-y-z, y-z-x, z-x-y, x-z-y, z-y-x, y-x-z), or proper Euler angles (z-x-z, x-y-x, y-z-y, z-y-z, x-z-x, y-x-y). See https://en.wikipedia.org/wiki/Euler_angles[Euler angles]. Throws: :: In case the axis vector has zero magnitude, throws <>. NOTE: These functions are not view proxies; they return a temp object. ''' [[mat_set_rot]] ==== `set_rot` / Euler angles .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: // is_mat::value && mat_traits::rows>=3 && // mat_traits::rows==mat_traits::cols && // is_vec::value && vec_traits::dim==3 template void set_rot( A & a, B const & axis, typename vec_traits::scalar_type angle ); //Only enabled if: // is_mat::value && mat_traits::rows>=3 && // mat_traits::rows==mat_traits::cols template void set_rot_xzy( A & a, Angle x1, Angle z2, Angle y3 ); //Only enabled if: // is_mat::value && mat_traits::rows>=3 && // mat_traits::rows==mat_traits::cols template void set_rot_xyz( A & a, Angle x1, Angle y2, Angle z3 ); //Only enabled if: // is_mat::value && mat_traits::rows>=3 && // mat_traits::rows==mat_traits::cols template void set_rot_yxz( A & a, Angle y1, Angle x2, Angle z3 ); //Only enabled if: // is_mat::value && mat_traits::rows>=3 && // mat_traits::rows==mat_traits::cols template void set_rot_yzx( A & a, Angle y1, Angle z2, Angle x3 ); //Only enabled if: // is_mat::value && mat_traits::rows>=3 && // mat_traits::rows==mat_traits::cols template void set_rot_zyx( A & a, Angle z1, Angle y2, Angle x3 ); //Only enabled if: // is_mat::value && mat_traits::rows>=3 && // mat_traits::rows==mat_traits::cols template void set_rot_zxy( A & a, Angle z1, Angle x2, Angle y3 ); //Only enabled if: // is_mat::value && mat_traits::rows>=3 && // mat_traits::rows==mat_traits::cols template void set_rot_xzx( A & a, Angle x1, Angle z2, Angle x3 ); //Only enabled if: // is_mat::value && mat_traits::rows>=3 && // mat_traits::rows==mat_traits::cols template void set_rot_xyx( A & a, Angle x1, Angle y2, Angle x3 ); //Only enabled if: // is_mat::value && mat_traits::rows>=3 && // mat_traits::rows==mat_traits::cols template void set_rot_yxy( A & a, Angle y1, Angle x2, Angle y3 ); //Only enabled if: // is_mat::value && mat_traits::rows>=3 && // mat_traits::rows==mat_traits::cols template void set_rot_yzy( A & a, Angle y1, Angle z2, Angle y3 ); //Only enabled if: // is_mat::value && mat_traits::rows>=3 && // mat_traits::rows==mat_traits::cols template void set_rot_zyz( A & a, Angle z1, Angle y2, Angle z3 ); //Only enabled if: // is_mat::value && mat_traits::rows>=3 && // mat_traits::rows==mat_traits::cols template void set_rot_zxz( A & a, Angle z1, Angle x2, Angle z3 ); //Only enabled if: // is_mat::value && mat_traits::rows>=3 && // mat_traits::rows==mat_traits::cols template void set_rot_xzy( A & a, Angle x1, Angle z2, Angle y3 ); } } ---- Effects: :: Assigns the return value of the corresponding <> function to `a`. ''' [[mat_rotate]] ==== `rotate` / Euler angles .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: // is_mat::value && mat_traits::rows>=3 && // mat_traits::rows==mat_traits::cols && // is_vec::value && vec_traits::dim==3 template void rotate( A & a, B const & axis, typename mat_traits::scalar_type angle ); //Only enabled if: // is_mat::value && mat_traits::rows>=3 && // mat_traits::rows==mat_traits::cols template void rotate_xzy( A & a, Angle x1, Angle z2, Angle y3 ); //Only enabled if: // is_mat::value && mat_traits::rows>=3 && // mat_traits::rows==mat_traits::cols template void rotate_xyz( A & a, Angle x1, Angle y2, Angle z3 ); //Only enabled if: // is_mat::value && mat_traits::rows>=3 && // mat_traits::rows==mat_traits::cols template void rotate_yxz( A & a, Angle y1, Angle x2, Angle z3 ); //Only enabled if: // is_mat::value && mat_traits::rows>=3 && // mat_traits::rows==mat_traits::cols template void rotate_yzx( A & a, Angle y1, Angle z2, Angle x3 ); //Only enabled if: // is_mat::value && mat_traits::rows>=3 && // mat_traits::rows==mat_traits::cols template void rotate_zyx( A & a, Angle z1, Angle y2, Angle x3 ); //Only enabled if: // is_mat::value && mat_traits::rows>=3 && // mat_traits::rows==mat_traits::cols template void rotate_zxy( A & a, Angle z1, Angle x2, Angle y3 ); //Only enabled if: // is_mat::value && mat_traits::rows>=3 && // mat_traits::rows==mat_traits::cols template void rotate_xzx( A & a, Angle x1, Angle z2, Angle x3 ); //Only enabled if: // is_mat::value && mat_traits::rows>=3 && // mat_traits::rows==mat_traits::cols template void rotate_xyx( A & a, Angle x1, Angle y2, Angle x3 ); //Only enabled if: // is_mat::value && mat_traits::rows>=3 && // mat_traits::rows==mat_traits::cols template void rotate_yxy( A & a, Angle y1, Angle x2, Angle y3 ); //Only enabled if: // is_mat::value && mat_traits::rows>=3 && // mat_traits::rows==mat_traits::cols template void rotate_yzy( A & a, Angle y1, Angle z2, Angle y3 ); //Only enabled if: // is_mat::value && mat_traits::rows>=3 && // mat_traits::rows==mat_traits::cols template void rotate_zyz( A & a, Angle z1, Angle y2, Angle z3 ); //Only enabled if: // is_mat::value && mat_traits::rows>=3 && // mat_traits::rows==mat_traits::cols template void rotate_zxz( A & a, Angle z1, Angle x2, Angle z3 ); } } ---- Effects: :: Multiplies the matrix `a` in-place by the return value of the corresponding <> function. ''' [[rotx_mat]] ==== `rotx_mat` .#include [source,c++] ---- namespace boost { namespace qvm { template -unspecified-return-type- rotx_mat( Angle const & angle ); } } ---- Returns: :: A <> matrix of unspecified type, of `Dim` rows and `Dim` columns and scalar type `Angle`, which performs a rotation around the `X` axis at `angle` radians. ''' [[mat_set_rotx]] ==== `set_rotx` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: // is_mat::value && mat_traits::rows>=3 && // mat_traits::rows==mat_traits::cols template void set_rotx( A & a, typename mat_traits::scalar_type angle ); } } ---- Effects: :: As if: + [source,c++] ---- assign( a, rotx_mat::rows>(angle)); ---- ''' [[mat_rotate_x]] ==== `rotate_x` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: // is_mat::value && mat_traits::rows>=3 && // mat_traits::rows==mat_traits::cols template void rotate_x( A & a, typename mat_traits::scalar_type angle ); } } ---- Effects: :: As if: `a <> <><<::rows>>>(angle)`. ''' [[roty_mat]] ==== `roty_mat` .#include [source,c++] ---- namespace boost { namespace qvm { template -unspecified-return-type- roty_mat( Angle const & angle ); } } ---- Returns: :: A <> matrix of unspecified type, of `Dim` rows and `Dim` columns and scalar type `Angle`, which performs a rotation around the `Y` axis at `angle` radians. ''' [[mat_set_roty]] ==== `set_roty` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: // is_mat::value && mat_traits::rows>=3 && // mat_traits::rows==mat_traits::cols template void set_roty( A & a, typename mat_traits::scalar_type angle ); } } ---- Effects: :: As if: + [source,c++] ---- assign( a, roty_mat::rows>(angle)); ---- ''' [[mat_rotate_y]] ==== `rotate_y` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: // is_mat::value && mat_traits::rows>=3 && // mat_traits::rows==mat_traits::cols template void rotate_y( A & a, typename mat_traits::scalar_type angle ); } } ---- Effects: :: As if: `a <> <><<::rows>>>(angle)`. ''' [[rotz_mat]] ==== `rotz_mat` .#include [source,c++] ---- namespace boost { namespace qvm { template -unspecified-return-type- rotz_mat( Angle const & angle ); } } ---- Returns: :: A <> matrix of unspecified type, of `Dim` rows and `Dim` columns and scalar type `Angle`, which performs a rotation around the `Z` axis at `angle` radians. ''' [[mat_set_rotz]] ==== `set_rotz` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: // is_mat::value && mat_traits::rows>=3 && // mat_traits::rows==mat_traits::cols template void set_rotz( A & a, typename mat_traits::scalar_type angle ); } } ---- Effects: :: As if: + [source,c++] ---- assign( a, rotz_mat::rows>(angle)); ---- ''' [[mat_rotate_z]] ==== `rotate_z` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: // is_mat::value && mat_traits::rows>=3 && // mat_traits::rows==mat_traits::cols template void rotate_z( A & a, typename mat_traits::scalar_type angle ); } } ---- Effects: :: As if: `a <> <><<::rows>>>(angle)`. ''' [[determinant]] ==== `determinant` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: // is_mat::value && mat_traits::rows==mat_traits::cols template mat_traits::scalar_type determinant( A const & a ); } } ---- This function computes the https://en.wikipedia.org/wiki/Determinant[determinant] of the square matrix `a`. ''' [[perspective_lh]] ==== `perspective_lh` .#include [source,c++] ---- namespace boost { namespace qvm { template -unspecified-return-type- perspective_lh( T fov_y, T aspect, T zn, T zf ); } } ---- Returns: :: A 4x4 projection matrix of unspecified type of the following form: + [cols="^v,^v,^v,^v",width="50%"] |==== | `xs` | 0 | 0 | 0 | 0 | `ys` | 0 | 0 | 0 | 0 | `zf`/(`zf`-`zn`) | -`zn`*`zf`/(`zf`-`zn`) | 0 | 0 | 1 | 0 |==== + where `ys` = cot(`fov_y`/2) and `xs` = `ys`/`aspect`. ''' [[perspective_rh]] ==== `perspective_rh` .#include [source,c++] ---- namespace boost { namespace qvm { template -unspecified-return-type- perspective_rh( T fov_y, T aspect, T zn, T zf ); } } ---- Returns: :: A 4x4 projection matrix of unspecified type of the following form: + [cols="^v,^v,^v,^v",width="50%"] |==== | `xs` | 0 | 0 | 0 | 0 | `ys` | 0 | 0 | 0 | 0 | `zf`/(`zn`-`zf`) | `zn`*`zf`/(`zn`-`zf`) | 0 | 0 | -1 | 0 |==== + where `ys` = cot(`fov_y`/2), and `xs` = `ys`/`aspect`. ''' [[mat_scalar_cast]] ==== `scalar_cast` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: is_mat::value template -unspecified-return_type- scalar_cast( A const & a ); } } ---- Returns: :: A read-only <> of `a` that looks like a matrix of the same dimensions as `a`, but with <> `Scalar` and elements constructed from the corresponding elements of `a`. ''' [[mref]] ==== `mref` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: is_mat::value template -unspecified-return-type- mref( A & a ); } } ---- Returns: :: An identity view proxy of `a`; that is, it simply accesses the elements of `a`. TIP: `mref` allows calling QVM operations when `a` is of built-in type, for example a plain old C array. ''' === Quaternion-Vector Operations [[quat_vec_mul]] ==== `operator*` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: // is_mat::value && is_vec::value && // mat_traits::cols==vec_traits::dim template typename deduce_vec2::rows>::type operator*( A const & a, B const & b ); } } ---- Returns: :: The result of transforming the vector `b` by the quaternion `a`. NOTE: The <> template can be specialized to deduce the desired return type, given the types `A` and `B`. ''' === Matrix-Vector Operations [[mat_vec_mul]] ==== `operator*` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: // is_mat::value && is_vec::value && // mat_traits::cols==vec_traits::dim template typename deduce_vec2::rows>::type operator*( A const & a, B const & b ); } } ---- Returns: :: The result of multiplying the matrix `a` and the vector `b`, where `b` is interpreted as a matrix-column. The resulting matrix-row is returned as a vector type. NOTE: The <> template can be specialized to deduce the desired return type, given the types `A` and `B`. ''' [[transform_vector]] ==== `transform_vector` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: // is_mat::value && is_vec::value && // mat_traits::rows==4 && mat_traits::cols==4 && // vec_traits::dim==3 template deduce_vec2 >::type transform_vector( A const & a, B const & b ); } } ---- Effects: :: As if: `return a <> <>(b)`. ''' [[transform_point]] ==== `transform_point` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: // is_mat::value && is_vec::value && // mat_traits::rows==4 && mat_traits::cols==4 && // vec_traits::dim==3 template deduce_vec2 >::type transform_point( A const & a, B const & b ); } } ---- Effects: :: As if: `return a <> <>(b)`. ''' === Matrix-to-Matrix View Proxies [[del_row]] ==== `del_row` .#include [source,c++] ---- namespace boost { namespace qvm { template -unspecified-return-type- del_row(); } } ---- The expression `del_row(m)` returns an lvalue <> that looks like the matrix `m` with row `R` deleted. ''' [[del_col]] ==== `del_col` .#include [source,c++] ---- namespace boost { namespace qvm { template -unspecified-return-type- del_col(); } } ---- The expression `del_col(m)` returns an lvalue <> that looks like the matrix `m` with column `C` deleted. ''' [[del_row_col]] ==== `del_row_col` .#include [source,c++] ---- namespace boost { namespace qvm { template -unspecified-return-type- del_row_col(); } } ---- The expression `del_row_col(m)` returns an lvalue <> that looks like the matrix `m` with row `R` and column `C` deleted. ''' [[neg_row]] ==== `neg_row` .#include [source,c++] ---- namespace boost { namespace qvm { template -unspecified-return-type- neg_row(); } } ---- The expression `neg_row(m)` returns a read-only <> that looks like the matrix `m` with row `R` negated. ''' [[neg_col]] ==== `neg_col` .#include [source,c++] ---- namespace boost { namespace qvm { template -unspecified-return-type- neg_col(); } } ---- The expression `neg_col(m)` returns a read-only <> that looks like the matrix `m` with column `C` negated. ''' [[swap_rows]] ==== `swap_rows` .#include [source,c++] ---- namespace boost { namespace qvm { template -unspecified-return-type- swap_rows(); } } ---- The expression `swap_rows(m)` returns an lvalue <> that looks like the matrix `m` with rows `R1` and `R2` swapped. ''' [[swap_cols]] ==== `swap_cols` .#include [source,c++] ---- namespace boost { namespace qvm { template -unspecified-return-type- swap_cols(); } } ---- The expression `swap_cols(m)` returns an lvalue <> that looks like the matrix `m` with columns `C1` and `C2` swapped. ''' [[transposed]] ==== `transposed` .#include [source,c++] ---- namespace boost { namespace qvm { -unspecified-return-type- transposed(); } } ---- The expression `transposed(m)` returns an lvalue <> that transposes the matrix `m`. ''' === Vector-to-Matrix View Proxies [[col_mat]] ==== `col_mat` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: is_vec::value template -unspecified-return-type- col_mat( A & a ); } } ---- The expression `col_mat(v)` returns an lvalue <> that accesses the vector `v` as a matrix-column. ''' [[row_mat]] ==== `row_mat` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: is_vec::value template -unspecified-return-type- row_mat( A & a ); } } ---- The expression `row_mat(v)` returns an lvalue <> that accesses the vector `v` as a matrix-row. ''' [[translation_mat]] ==== `translation_mat` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: is_vec::value template -unspecified-return-type- translation_mat( A & a ); } } ---- The expression `translation_mat(v)` returns an lvalue <> that accesses the vector `v` as translation matrix of size 1 + <::dim`>>. ''' [[diag_mat]] ==== `diag_mat` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: is_vec::value template -unspecified-return-type- diag_mat( A & a ); } } ---- The expression `diag_mat(v)` returns an lvalue <> that accesses the vector `v` as a square matrix of the same dimensions in which the elements of `v` appear as the main diagonal and all other elements are zero. TIP: If `v` is a 3D vector, the expression `diag_mat(XYZ1(v))` can be used as a scaling 4D matrix. ''' === Matrix-to-Vector View Proxies [[col]] ==== `col` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: is_mat::value template -unspecified-return-type- col( A & a ); } } ---- The expression `col(m)` returns an lvalue <> that accesses column `C` of the matrix `m` as a vector. ''' [[row]] ==== `row` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: is_mat::value template -unspecified-return-type- row( A & a ); } } ---- The expression `row(m)` returns an lvalue <> that accesses row `R` of the matrix `m` as a vector. ''' [[diag]] ==== `diag` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: is_mat::value template -unspecified-return-type- diag( A & a ); } } ---- The expression `diag(m)` returns an lvalue <> that accesses the main diagonal of the matrix `m` as a vector. ''' [[translation]] ==== `translation` .#include [source,c++] ---- namespace boost { namespace qvm { //Only enabled if: // is_mat::value && // mat_traits::rows==mat_traits::cols && mat_traits::rows>=3 template -unspecified-return-type- translation( A & a ); } } ---- The expression `translation(m)` returns an lvalue <> that accesses the translation component of the square matrix `m`, which is a vector of size `D`-1, where `D` is the size of `m`. ''' === Exceptions [[error]] ==== `error` .#include [source,c++] ---- namespace boost { namespace qvm { struct error: virtual boost::exception, virtual std::exception { }; } } ---- This is the base for all exceptions thorwn by QVM. ''' [[zero_magnitude_error]] ==== `zero_magnitude_error` .#include [source,c++] ---- namespace boost { namespace qvm { struct zero_magnitude_error: virtual error { }; } } ---- This exception indicates that an operation requires a vector or a quaternion with non-zero magnitude, but the computed magnitude is zero. ''' [[zero_determinant_error]] ==== `zero_determinant_error` .#include [source,c++] ---- namespace boost { namespace qvm { struct zero_determinant_error: virtual error { }; } } ---- This exception indicates that an operation requires a matrix with non-zero determinant, but the computed determinant is zero. ''' === Macros and Configuration: BOOST_QVM_ [[BOOST_QVM_INLINE]] ==== `INLINE` ===== `BOOST_QVM_INLINE` .#include [source,c++] ---- namespace boost { namespace qvm { #ifndef BOOST_QVM_INLINE #define BOOST_QVM_INLINE inline #endif } } ---- This macro is not used directly by QVM, except as the default value of other macros from ``. A user-defined `BOOST_QVM_INLINE` should expand to a value that is valid substitution of the `inline` keyword in function definitions. ''' [[BOOST_QVM_FORCE_INLINE]] ==== `FORCE_INLINE` ===== `BOOST_QVM_FORCE_INLINE` .#include [source,c++] ---- namespace boost { namespace qvm { #ifndef BOOST_QVM_FORCE_INLINE #define BOOST_QVM_FORCE_INLINE /*platform-specific*/ #endif } } ---- This macro is not used directly by QVM, except as the default value of other macros from ``. A user-defined `BOOST_QVM_FORCE_INLINE` should expand to a value that is valid substitution of the `inline` keyword in function definitions, to indicate that the compiler must inline the function. Of course, actual inlining may or may not occur. ''' [[BOOST_QVM_INLINE_TRIVIAL]] ==== `INLINE_TRIVIAL` ===== `BOOST_QVM_INLINE_TRIVIAL` .#include [source,c++] ---- namespace boost { namespace qvm { #ifndef BOOST_QVM_INLINE_TRIVIAL #define BOOST_QVM_INLINE_TRIVIAL BOOST_QVM_FORCE_INLINE #endif } } ---- QVM uses `BOOST_QVM_INLINE_TRIVIAL` in definitions of functions that are not critical for the overall performance of the library but are extremely simple (such as one-liners) and therefore should always be inlined. ''' [[BOOST_QVM_INLINE_CRITICAL]] ==== `INLINE_CRITICAL` ===== `BOOST_QVM_INLINE_CRITICAL` .#include [source,c++] ---- namespace boost { namespace qvm { #ifndef BOOST_QVM_INLINE_CRITICAL #define BOOST_QVM_INLINE_CRITICAL BOOST_QVM_FORCE_INLINE #endif } } ---- QVM uses `BOOST_QVM_INLINE_CRITICAL` in definitions of functions that are critical for the overall performance of the library, such as functions that access individual vector and matrix elements. ''' [[BOOST_QVM_INLINE_OPERATIONS]] ==== `INLINE_OPERATIONS` ===== `BOOST_QVM_INLINE_OPERATIONS` .#include [source,c++] ---- namespace boost { namespace qvm { #ifndef BOOST_QVM_INLINE_OPERATIONS #define BOOST_QVM_INLINE_OPERATIONS BOOST_QVM_INLINE #endif } } ---- QVM uses `BOOST_QVM_INLINE_OPERATIONS` in definitions of functions that implement various high-level operations, such as matrix multiplication, computing the magnitude of a vector, etc. ''' [[BOOST_QVM_INLINE_RECURSION]] ==== `INLINE_RECURSION` ===== `BOOST_QVM_INLINE_RECURSION` .#include [source,c++] ---- namespace boost { namespace qvm { #ifndef BOOST_QVM_INLINE_RECURSION #define BOOST_QVM_INLINE_RECURSION BOOST_QVM_INLINE_OPERATIONS #endif } } ---- QVM uses `BOOST_QVM_INLINE_RECURSION` in definitions of recursive functions that are not critical for the overall performance of the library (definitions of all critical functions, including critical recursive functions, use <>). ''' [[BOOST_QVM_ASSERT]] ==== `ASSERT` ===== `BOOST_QVM_ASSERT` .#include [source,c++] ---- namespace boost { namespace qvm { #ifndef BOOST_QVM_ASSERT #include #define BOOST_QVM_ASSERT BOOST_ASSERT #endif } } ---- This is the macro QVM uses to assert on precondition violations and logic errors. A user-defined `BOOST_QVM_ASSERT` should have the semantics of the standard `assert`. ''' [[BOOST_QVM_STATIC_ASSERT]] ==== `STATIC_ASSERT` ===== `BOOST_QVM_STATIC_ASSERT` .#include [source,c++] ---- namespace boost { namespace qvm { #ifndef BOOST_QVM_STATIC_ASSERT #include #define BOOST_QVM_STATIC_ASSERT BOOST_STATIC_ASSERT #endif } } ---- All static assertions in QVM use the `BOOST_QVM_STATIC_ASSERT` macro. ''' [[BOOST_QVM_THROW_EXCEPTION]] ==== `THROW_EXCEPTION` ===== `BOOST_QVM_THROW_EXCEPTION` .#include [source,c++] ---- namespace boost { namespace qvm { #ifndef BOOST_QVM_THROW_EXCEPTION #include #define BOOST_QVM_THROW_EXCEPTION BOOST_THROW_EXCEPTION #endif } } ---- This macro is used whenever QVM throws an exception. Users who override the standard `BOOST_QVM_THROW_EXCEPTION` behavior must ensure that when invoked, the substituted implementation does not return control to the caller. Below is a list of all QVM functions that invoke `BOOST_QVM_THROW_EXCEPTION`: * Quaternion operations: ** <> ** <> ** <> ** <> * Vector operations: ** <> ** <> * Matrix operations: ** <> ** <> [[rationale]] == Design Rationale {CPP} is ideal for 3D graphics and other domains that require 3D transformations: define vector and matrix types and then overload the appropriate operators to implement the standard algebraic operations. Because this is relatively straight-forward, there are many libraries that do this, each providing custom vector and matrix types, and then defining the same operations (e.g. matrix multiply) for these types. Often these libraries are part of a higher level system. For example, video game programmers typically use one set of vector/matrix types with the rendering engine, and another with the physics simulation engine. QVM proides interoperability between all these different types and APIs by decoupling the standard algebraic functions from the types they operate on -- without compromising type safety. The operations work on any type for which proper traits have been specialized. Using QVM, there is no need to translate between the different quaternion, vector or matrix types; they can be mixed in the same expression safely and efficiently. This design enables QVM to generate types and adaptors at compile time, compatible with any other QVM or user-defined type. For example, transposing a matrix needs not store the result: rather than modifying its argument or returning a new object, it simply binds the original matrix object through a generated type which remaps element access on the fly. In addition, QVM can be helpful in selectively optimizing individual types or operations for maximum performance where that matters. For example, users can overload a specific operation for specific types, or define highly optimized, possibly platform-specific or for some reason cumbersome to use types, then mix and match them with more user-friendly types in parts of the program where performance isn't critical. == Code Generator While QVM defines generic functions that operate on matrix and vector types of arbitrary static dimensions, it also provides a code generator that can be used to create compatible header files that define much simpler specializations of these functions for specific dimensions. This is useful during debugging since the generated code is much easier to read than the template metaprogramming-heavy generic implementations. It is also potentially friendlier to the optimizer. The code generator is a command-line utility program. Its source code can be found in the `boost/libs/qvm/gen` directory. It was used to generate the following headers that ship with QVM: * 2D, 3D and 4D matrix operations: ** `boost/qvm/gen/mat_operations2.hpp` (matrices of size 2x2, 2x1 and 1x2, included by `boost/qvm/mat_operations2.hpp`) ** `boost/qvm/gen/mat_operations3.hpp` (matrices of size 3x3, 3x1 and 1x3, included by `boost/qvm/mat_operations3.hpp`) ** `boost/qvm/gen/mat_operations4.hpp` (matrices of size 4x4, 4x1 and 1x4, included by `boost/qvm/mat_operations4.hpp`) * 2D, 3D and 4D vector operations: ** `boost/qvm/gen/v2.hpp` (included by `boost/qvm/vec_operations2.hpp`) ** `boost/qvm/gen/v3.hpp` (included by `boost/qvm/vec_operations3.hpp`) ** `boost/qvm/gen/v4.hpp` (included by `boost/qvm/vec_operations4.hpp`) * 2D, 3D and 4D vector-matrix operations: ** `boost/qvm/gen/vm2.hpp` (included by `boost/qvm/vec_mat_operations2.hpp`) ** `boost/qvm/gen/vm3.hpp` (included by `boost/qvm/vec_mat_operations3.hpp`) ** `boost/qvm/gen/vm4.hpp` (included by `boost/qvm/vec_mat_operations4.hpp`) * 2D, 3D and 4D vector swizzling operations: ** `boost/qvm/gen/sw2.hpp` (included by `boost/qvm/swizzle2.hpp`) ** `boost/qvm/gen/sw3.hpp` (included by `boost/qvm/swizzle3.hpp`) ** `boost/qvm/gen/sw4.hpp` (included by `boost/qvm/swizzle4.hpp`) Any such generated headers must be included before the corresponding generic header file is included. For example, if one creates a header `boost/qvm/gen/m5.hpp`, it must be included before `boost/qvm/mat_operations.hpp` in included. However, the generic headers (`boost/qvm/mat_operations.hpp`, `boost/qvm/vec_operations.hpp`, `boost/qvm/vec_mat_operations.hpp` and `boost/qvm/swizzle.hpp`) already include the generated headers from the list above, so the generated headers don't need to be included manually. NOTE: headers under `boost/qvm/gen` are not part of the public interface of QVM. For example, `boost/qvm/gen/mat_operations2.hpp` should not be included directly; `#include ` instead. == Known Quirks and Issues === Capturing View Proxies with `auto` By design, <> must not return temporary objects. They return reference to an argument they take by (`const`) reference, cast to reference of unspecified type that is not copyable. Because of this, the return value of a view proxy can not be captured by value with `auto`: [source,c++] ---- auto tr = transposed(m); //Error: the return type of transposed can not be copied. ---- The correct use of auto with view proxies is: [source,c++] ---- auto & tr = transposed(m); ---- NOTE: Many view proxies are not read-only, that is, they're lvalues; changes made on the view proxy operate on the original object. This is another reason why they can not be captured by value with `auto`. ''' === Binding QVM Overloads From an Unrelated Namespace The operator overloads in namespace `boost::qvm` are designed to work with user-defined types. Typically it is sufficient to make these operators available in the namespace where the operator is used, by `using namespace boost::qvm`. A problem arises if the scope that uses the operator is not controlled by the user. For example: [source,c++] ---- namespace ns1 { struct float2 { float x, y; }; } namespace ns2 { using namespace boost::qvm; void f() { ns1::float2 a, b; a==b; //OK ns1::float2 arr1[2], arr2[2]; std::equal(arr1,arr1+2,arr2); //Error: operator== is inaccessible from namespace std } } ---- In the `std::equal` expression above, even though `boost::qvm::operator==` is made visible in namespace `ns2` by `using namespace boost::qvm`, the call originates from namespace `std`. In this case the compiler can't bind `boost::qvm::operator==` because only namespace `ns1` is visible through ADL, and it does not contain a suitable declaration. The solution is to declare `operator==` in namespace ns1, which can be done like this: [source,c++] ---- namespace ns1 { using boost::qvm::operator==; } ---- ''' === Link Errors When Calling Math Functions with `int` Arguments QVM does not call standard math functions (e.g. sin, cos, etc.) directly. Instead, it calls function templates declared in `boost/qvm/math.hpp` in namespace `boost::qvm`. This allows the user to specialize these templates for user-defined scalar types. QVM itself defines specializations of the math function templates only for `float` and `double`, but it does not provide generic definitions. This is done to protect the user from unintentionally writing code that binds standard math functions that take `double` when passing arguments of lesser types, which would be suboptimal. Because of this, a call to e.g. `<>(axis,1)` will compile successfully but fail to link, since it calls e.g. `boost::qvm::sin`, which is undefined. Because rotations by integer number of radians are rarely needed, in QVM there is no protection against such errors. In such cases the solution is to use `rot_mat(axis,1.0f)` instead. == Distribution QVM is part of https://www.boost.org/[Boost] and is distributed under the http://www.boost.org/LICENSE_1_0.txt[Boost Software License, Version 1.0]. The source code is available in https://github.com/boostorg/qvm[QVM GitHub repository]. (C) 2008-2018 Emil Dotchevski and Reverge Studios, Inc. == Portability See the link:https://travis-ci.org/boostorg/qvm[QVM Travis CI Builds]. == Feedback / Support Please use the link:https://lists.boost.org/mailman/listinfo.cgi/boost[Boost Developers mailing list]. == Q&A [qanda] What is the motivation behind QVM? Why not just use uBLAS/Eigen/CML/GLM/etc?:: The primary domain of QVM is realtime graphics and simulation applications, so it is not a complete linear algebra library. While (naturally) there is some overlap with such libraries, QVM puts the emphasis on 2, 3 and 4 dimensional zero-overhead operations (hence domain-specific features like Swizzling). How does the `qvm::<>` (or `qvm::<>`, or `qvm::<>`) template compare to vector types from other libraries?:: The `qvm::vec` template is not in any way central to the vector operations defined by QVM. The operations are designed to work with any user-defined vector type or with 3rd-party vector types (e.g. `D3DVECTOR`), while the `qvm::vec` template is simply a default return type for expressions that use arguments of different types that would be incompatible outside of QVM. For example, if the <> hasn't been specialized, calling <> with a user-defined type `vec3` and a user-defined type `float3` returns a `qvm::vec`. Why doesn't QVM use [] or () to access vector and matrix elements?:: Because it's designed to work with user-defined types, and the {CPP} standard requires these operators to be members. Of course if a user-defined type defines `operator[]` or `operator()` they are available for use with other QVM functions, but QVM defines its own mechanisms for <>, <> (as well as <>), and <>. ''' [.text-right] (C) 2008-2018 Emil Dotchevski and Reverge Studios, Inc.