### Matrix-Vector Multiplication with Transposed View Source: https://www.boost.org/doc/libs/latest/libs/qvm/doc/html/index Shows an example of multiplying a transposed matrix view by a user-defined vector type. View proxy functions return references to the original object, allowing for efficient operations. ```cpp float3 v = {0,0,7}; float3 vrot = transposed(rotx_mat<3>(3.14159f)) * v; ``` -------------------------------- ### Create Quaternion Rotation with QVM Source: https://www.boost.org/doc/libs/latest/libs/qvm/index Demonstrates creating a quaternion object using QVM's built-in quat type that represents a rotation around the X axis. This is a simple example of the out-of-box types provided by QVM for basic quaternion operations. ```cpp quat rx = rotx_quat(3.14159f); ``` -------------------------------- ### Vector Swizzling for Element Reordering and Views Source: https://www.boost.org/doc/libs/latest/libs/qvm/doc/html/index Demonstrates vector swizzling in Boost.QVM, allowing access to vector elements with reordered or repeated components, and creating views of different dimensions. It includes examples of swapping elements and creating 4D vectors from 3D vectors. ```cpp float3 v = {0,0,7}; YXZ(v) = rotx_mat<3>(3.14159f) * v; float3 v = {0,0,7}; float4 point = XYZ1(v); // {0,0,7,1} float4 vector = XYZ0(v); // {0,0,7,0} float3 v = {0,0,7}; float4 v1 = ZZZZ(v); // {7,7,7,7} // Binding scalars as vectors float3 v = _00X(42.0f); // {0,0,42} ``` -------------------------------- ### Vector Swizzling for Element Access and Reordering Source: https://www.boost.org/doc/libs/latest/libs/qvm/index Explains QVM's swizzling feature for accessing vector elements, exposing views of different dimensions, and reordering elements. Examples show modifying vector elements and creating new vectors with specific component arrangements, including adding 0 or 1. ```cpp float3 v = {0,0,7}; YXZ(v) = rotx_mat<3>(3.14159f) * v; ``` ```cpp float3 v = {0,0,7}; float4 point = XYZ1(v); // {0,0,7,1} float4 vector = XYZ0(v); // {0,0,7,0} ``` ```cpp float3 v = {0,0,7}; float4 v1 = ZZZZ(v); // {7,7,7,7} ``` ```cpp float3 v = _00X(42.0f); // {0,0,42} ``` -------------------------------- ### Vector Swizzling Assignment Example in Boost QVM Source: https://www.boost.org/doc/libs/latest/libs/qvm/doc/html/index Demonstrates swizzling as an lvalue operation for element rearrangement and assignment. Shows how YX(v) = v2 assigns v2 elements to rearranged positions in v while preserving unchanged elements (Z and W). Illustrates zero-overhead proxy semantics for efficient vector manipulation. ```cpp // Example: YX swizzling assignment // If v is a 4D vector and v2 is a 2D vector YX(v) = v2; // This assigns: // v2.Y -> v.X // v2.X -> v.Y // v.Z and v.W remain unchanged // YX(v) is a 2D view proxy where: // - X element refers to Y element of v // - Y element refers to X element of v ``` -------------------------------- ### Get Vector Reference Proxy - Boost QVM Source: https://www.boost.org/doc/libs/latest/libs/qvm/index Returns an identity view proxy for a vector, allowing direct access to its elements. This is particularly useful when `a` is a built-in type like a C-style array, enabling QVM operations on such types. ```cpp #include namespace boost { namespace qvm { //Only enabled if: is_vec::value template -unspecified-return-type- vref( A & a ); } } ``` -------------------------------- ### Assigning to a Swizzled Vector View Proxy (C++) Source: https://www.boost.org/doc/libs/latest/libs/qvm/index This C++ example demonstrates how to assign a vector to a swizzled vector view proxy. The assignment modifies specific elements of the original vector without affecting others. This showcases the lvalue nature of view proxies and their utility in selective element manipulation. ```cpp // Assume v is a 4D vector and v2 is a 2D vector. // The expression YX(v) creates a 2D view proxy for v. // Assigning v2 to YX(v) modifies the X and Y elements of v // based on the values in v2, leaving Z and W unchanged. // YX(v) = v2; ``` -------------------------------- ### Implement quat_traits Specialization for User-Defined Quaternion Source: https://www.boost.org/doc/libs/latest/libs/qvm/index Complete example implementing quat_traits specialization for a custom fquat quaternion type with float scalar elements stored in an array. Provides both read and write access to quaternion components via template member functions. ```cpp #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]; } }; } } ``` -------------------------------- ### Converting Between Matrix Types Source: https://www.boost.org/doc/libs/latest/libs/qvm/doc/html/index This code illustrates the use of the `convert_to` function in Boost QVM for creating a matrix of a specific C++ type from any other compatible matrix type. This is useful when interoperating with different libraries and ensuring type consistency, as shown in the example converting the result of a matrix multiplication to `user_matrix2`. ```cpp user_matrix2 m=convert_to(m1 * m2); ``` -------------------------------- ### Capturing Unary Operation Results with `auto` Source: https://www.boost.org/doc/libs/latest/libs/qvm/index This example demonstrates using `auto` to capture the result of a unary operation, such as `inverse`, applied to a potentially non-copyable type like a C-style array. QVM handles this by returning a suitable, copyable matrix type, ensuring the operation "just works". ```cpp float m[3][3]; auto & inv = inverse(m); ``` -------------------------------- ### Example vec_traits Specialization for a 3D Float Vector (C++) Source: https://www.boost.org/doc/libs/latest/libs/qvm/index This example demonstrates a full specialization of `vec_traits` for a custom `float3` struct. It defines the dimension as 3, the scalar type as `float`, and provides implementations for `read_element` and `write_element` to access the underlying array members. ```cpp #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 }; } } ``` -------------------------------- ### Include All QVM Headers at Once Source: https://www.boost.org/doc/libs/latest/libs/qvm/doc/html/index Convenience header that includes all QVM headers in a single include statement. Useful for rapid prototyping or when working with all QVM components, though selective inclusion is recommended for compilation efficiency. ```cpp #include ``` -------------------------------- ### Register Custom 3D Vector Type with QVM Source: https://www.boost.org/doc/libs/latest/libs/qvm/doc/html/index This example demonstrates how to introduce a user-defined 3D vector type 'float3' to QVM by specializing the 'vec_traits' template. This allows QVM's generic functions to operate on the custom type. ```cpp 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 }; } } ``` -------------------------------- ### Create Translation Matrix with QVM Source: https://www.boost.org/doc/libs/latest/libs/qvm/index Shows how to create a translation matrix from a vector using QVM's built-in vec and mat types. Demonstrates vector initialization and matrix creation for 3D translation operations commonly used in graphics applications. ```cpp vec v = {0,0,7}; mat tr = translation_mat(v); ``` -------------------------------- ### Implement quat_traits for Custom Quaternion Type C++ Source: https://www.boost.org/doc/libs/latest/libs/qvm/doc/html/index Complete example showing specialization of quat_traits for a user-defined quaternion type fquat with float array storage. Implements both write_element and read_element access functions to enable QVM quaternion operations on the custom type. ```cpp #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]; } }; } } ``` -------------------------------- ### Bringing Boost QVM Namespace In Scope Source: https://www.boost.org/doc/libs/latest/libs/qvm/index This code snippet shows how to safely bring the entire `boost::qvm` namespace into scope. It leverages SFINAE and `enable_if` to avoid ambiguities with 3rd-party libraries, ensuring that QVM's function and operator overloads only become visible when QVM-specific type traits are met. This is the recommended approach for seamless integration. ```cpp using namespace boost::qvm; ``` -------------------------------- ### Define write_element for non-reference-accessible types Source: https://www.boost.org/doc/libs/latest/libs/qvm/doc/html/index This example illustrates an alternative way to define 'write_element' for user-defined vector types where elements cannot be accessed by reference. This version takes a scalar value to write into the element. Boost QVM automatically detects which 'write_element' alternative is provided. ```cpp template static inline void write_element( float3 & v, scalar_type s ) { v.a[I] = s; } ``` -------------------------------- ### Include Matrix Headers in C++ Source: https://www.boost.org/doc/libs/latest/libs/qvm/index Lists the C++ header files required for matrix operations in Boost.QVM, including traits, element access, various dimensional operations, and view proxies for matrix-matrix and matrix-vector interactions. The 'mat' class template is also available. ```cpp #include #include #include #include #include #include #include #include #include #include #include ``` -------------------------------- ### View Proxies for Matrix and Vector Operations Source: https://www.boost.org/doc/libs/latest/libs/qvm/index Illustrates how QVM's view proxy function templates enable static mapping for operations on matrix columns and transposed matrices, including interactions with user-defined vector types. These proxies work directly on references, avoiding temporary objects. ```cpp void multiply_column1( float33 & m, float scalar ) { col<1>(m) *= scalar; } ``` ```cpp float3 v = {0,0,7}; float3 vrot = transposed(rotx_mat<3>(3.14159f)) * v; ``` -------------------------------- ### Include Quaternion Headers in C++ Source: https://www.boost.org/doc/libs/latest/libs/qvm/index Includes necessary headers for quaternion traits, element access, operations, and the 'quat' class template. These headers define the core functionality for working with quaternions in Boost.QVM. ```cpp #include #include #include #include #include #include ``` -------------------------------- ### Create Matrix Reference Proxy - C++ Source: https://www.boost.org/doc/libs/latest/libs/qvm/index Creates an identity view proxy for a matrix that allows calling QVM operations on built-in types such as plain C arrays. Returns an unspecified proxy type that simply accesses the original matrix elements. ```cpp namespace boost { namespace qvm { //Only enabled if: is_mat::value template -unspecified-return-type- mref( A & a ); } } ``` -------------------------------- ### Swap Columns View Proxy (Boost QVM) Source: https://www.boost.org/doc/libs/latest/libs/qvm/index Returns an lvalue view proxy for a matrix `m` with two specified columns, `C1` and `C2`, swapped. This facilitates rearranging columns within the matrix representation. The template parameters `C1` and `C2` specify the zero-based indices of the columns to be interchanged. ```cpp namespace boost { namespace qvm { template -unspecified-return-type- swap_cols(); } } ``` -------------------------------- ### Correct View Proxy Capture with Reference in C++ Source: https://www.boost.org/doc/libs/latest/libs/qvm/doc/html/index Shows the correct approach to capture view proxy return values using a reference with auto&. This allows proper binding to the non-copyable reference returned by view proxy functions. ```cpp auto & tr = transposed(m); ``` -------------------------------- ### Swap Rows View Proxy (Boost QVM) Source: https://www.boost.org/doc/libs/latest/libs/qvm/index Creates an lvalue view proxy for a matrix `m` where two specified rows, `R1` and `R2`, are swapped. This enables in-place modification of the row order. The template parameters `R1` and `R2` are the zero-based indices of the rows to be exchanged. ```cpp namespace boost { namespace qvm { template -unspecified-return-type- swap_rows(); } } ``` -------------------------------- ### Include Vector Headers in C++ Source: https://www.boost.org/doc/libs/latest/libs/qvm/index Provides the C++ include directives for various vector functionalities in Boost.QVM, covering traits, element access, swizzling, operations (2D, 3D, 4D), and interactions with quaternions and matrices. The 'vec' class template is also included. ```cpp #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include ``` -------------------------------- ### qref for Quaternions Source: https://www.boost.org/doc/libs/latest/libs/qvm/index Creates an identity view proxy for a quaternion 'a', allowing QVM operations on built-in types like C arrays. ```cpp #include namespace boost { namespace qvm { //Only enabled if: is_quat::value template -unspecified-return-type- qref( A & a ); } } ``` -------------------------------- ### Get scalar type of quaternion, vector, or matrix (C++) Source: https://www.boost.org/doc/libs/latest/libs/qvm/index The `scalar` template provides a `type` alias that resolves to the scalar type of a given type `T`. It automatically detects if `T` is a quaternion, vector, or matrix type and returns its associated scalar type. This is a generalized version of accessing scalar types via `quat_traits`, `vec_traits`, and `mat_traits`. ```cpp #include namespace boost { namespace qvm { template struct scalar { typedef /*exact definition unspecified*/ type; }; } } ``` -------------------------------- ### Get scalar type of quaternion, vector, or matrix (C++) Source: https://www.boost.org/doc/libs/latest/libs/qvm/doc/html/index The `scalar` template provides a unified way to access the scalar type associated with quaternion, vector, or matrix types in QVM. It leverages `quat_traits`, `vec_traits`, and `mat_traits` to determine the underlying scalar type, returning `type` as an alias for it. This is useful for generic programming where the specific container type (quat, vec, mat) might not be known beforehand. ```cpp #include namespace boost { namespace qvm { template struct scalar { typedef /*exact definition unspecified*/ type; }; } } ``` -------------------------------- ### C/C++ Array Operations with Boost QVM Source: https://www.boost.org/doc/libs/latest/libs/qvm/index Demonstrates how Boost QVM's traits specializations allow direct operation on C-style arrays and std::array. It highlights the use of `vref` and `mref` to overcome limitations with built-in types. ```cpp float v[3] = {0,0,7}; float3 vrot = rotx_mat<3>(3.14159f) * v; ``` ```cpp float v[3] = {0,0,7}; vref(v) *= 42; ``` -------------------------------- ### mat_traits Template Base Structure with Member Requirements Source: https://www.boost.org/doc/libs/latest/libs/qvm/index Shows the base mat_traits template structure and documents required and optional specialization members including rows, cols, scalar_type, and access functions for compile-time and runtime element access. ```cpp 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 ); static inline scalar_type read_element_idx( int r, int c, Matrix const & m ); template static inline scalar_type & write_element( Matrix & m ); static inline scalar_type & write_element_idx( int r, int c, Matrix & m ); // Alternative signature for write_element and write_element_idx: template static inline void write_element( Matrix & m, scalar_type s ); static inline void write_element_idx( int r, int c, Matrix & m, scalar_type s ); }; */ } } ``` -------------------------------- ### Matrix View Proxies (Boost.QVM) Source: https://www.boost.org/doc/libs/latest/libs/qvm/doc/html/index Provides functions to create view proxies for matrices, allowing modification or inspection of sub-parts without copying. These include deleting or swapping rows/columns, negating rows/columns, and transposing. ```cpp namespace boost { namespace qvm { template -unspecified-return-type- del_row(); template -unspecified-return-type- del_col(); template -unspecified-return-type- del_row_col(); template -unspecified-return-type- neg_row(); template -unspecified-return-type- neg_col(); template -unspecified-return-type- swap_rows(); template -unspecified-return-type- swap_cols(); -unspecified-return-type- transposed(); } } ``` -------------------------------- ### Include Vector Traits and Operations Headers Source: https://www.boost.org/doc/libs/latest/libs/qvm/doc/html/index C++ headers for vector traits, element access, swizzling, operations, and the vec class template. Includes dimension-specific headers (2D, 3D, 4D) for optimized vector operations and supports vector-matrix integration through view proxies. ```cpp #include #include #include ``` ```cpp #include ``` ```cpp #include #include #include #include ``` ```cpp #include #include #include #include ``` ```cpp #include ``` ```cpp #include ``` ```cpp #include ``` ```cpp #include ``` -------------------------------- ### Create Left-Hand Perspective Projection Matrix - C++ Source: https://www.boost.org/doc/libs/latest/libs/qvm/index Generates a 4x4 perspective projection matrix using left-hand coordinate system conventions. Takes field of view angle, aspect ratio, and near/far clipping plane distances as parameters. Returns a view proxy matrix of unspecified type. ```cpp namespace boost { namespace qvm { template -unspecified-return-type- perspective_lh( T fov_y, T aspect, T zn, T zf ); } } ``` -------------------------------- ### Create Right-Hand Perspective Projection Matrix - C++ Source: https://www.boost.org/doc/libs/latest/libs/qvm/index Generates a 4x4 perspective projection matrix using right-hand coordinate system conventions. Takes field of view angle, aspect ratio, and near/far clipping plane distances as parameters. Returns a view proxy matrix of unspecified type. ```cpp namespace boost { namespace qvm { template -unspecified-return-type- perspective_rh( T fov_y, T aspect, T zn, T zf ); } } ``` -------------------------------- ### Include Matrix Traits and Operations Headers Source: https://www.boost.org/doc/libs/latest/libs/qvm/doc/html/index C++ headers for matrix traits, element access, operations, and the mat class template. Provides dimension-specific headers (2D, 3D, 4D) for matrix operations and supports matrix-matrix and matrix-vector view proxies for efficient data access. ```cpp #include #include #include ``` ```cpp #include ``` ```cpp #include #include #include #include ``` ```cpp #include ``` ```cpp #include ``` ```cpp #include ``` -------------------------------- ### Using `boost::qvm::sfinae` Namespace Source: https://www.boost.org/doc/libs/latest/libs/qvm/index This code snippet shows how to selectively bring only the SFINAE-enabled function and operator overloads from the `boost::qvm` namespace into scope. This is a safer alternative to `using namespace boost::qvm;` when direct type access from third-party libraries might cause ambiguities. ```cpp using namespace boost::qvm::sfinae; ``` -------------------------------- ### Transpose Matrix View Proxy (Boost QVM) Source: https://www.boost.org/doc/libs/latest/libs/qvm/index Provides an lvalue view proxy that represents the transpose of a given matrix `m`. This allows accessing the transposed matrix without explicit data copying. The function takes the matrix `m` as an argument. ```cpp namespace boost { namespace qvm { -unspecified-return-type- transposed(); } } ``` -------------------------------- ### Accessing Vector Elements by Swizzling (C++) Source: https://www.boost.org/doc/libs/latest/libs/qvm/index This C++ code demonstrates accessing vector elements by swizzling, enabling the creation of view proxies for 2D, 3D, and 4D vectors. These proxies allow for direct, zero-overhead access to rearranged subsets of vector elements. The functionality is enabled based on whether the template argument is a vector type or a scalar. ```cpp #include 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(); } } ``` -------------------------------- ### Vector-to-Matrix View Proxies (Boost.QVM) Source: https://www.boost.org/doc/libs/latest/libs/qvm/doc/html/index Enables treating a vector as a matrix view. `col_mat` accesses a vector as a column matrix, `row_mat` as a row matrix, and `translation_mat` as a translation matrix. ```cpp namespace boost { namespace qvm { template -unspecified-return-type- col_mat( A & a ); template -unspecified-return-type- row_mat( A & a ); template -unspecified-return-type- translation_mat( A & a ); } } ``` -------------------------------- ### Converting Between Matrix Types with `convert_to` Source: https://www.boost.org/doc/libs/latest/libs/qvm/index Demonstrates the use of the `convert_to` function to explicitly create a matrix of a specific C++ type from another compatible matrix type. This is useful for ensuring type consistency when integrating with different libraries or APIs. ```cpp user_matrix2 m=convert_to(m1 * m2); ``` -------------------------------- ### Row Matrix View Proxy (Boost QVM) Source: https://www.boost.org/doc/libs/latest/libs/qvm/index Provides an lvalue view proxy enabling a vector `a` to be accessed as a matrix-row. This is beneficial for operations requiring vectors to be treated as single-row matrices. The function requires `a` to be of a vector type. ```cpp namespace boost { namespace qvm { //Only enabled if: is_vec::value template -unspecified-return-type- row_mat( A & a ); } } ``` -------------------------------- ### Simplify mat_traits Specialization Using mat_traits_defaults Source: https://www.boost.org/doc/libs/latest/libs/qvm/index Demonstrates using the mat_traits_defaults template to reduce boilerplate code in mat_traits specializations. This approach inherits default implementations and only requires overriding the element access functions. ```cpp namespace boost { namespace qvm { template <> 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]; } }; } } ``` -------------------------------- ### Capturing View Proxies with auto in C++ Source: https://www.boost.org/doc/libs/latest/libs/qvm/index Demonstrates the correct and incorrect ways to capture view proxies returned by QVM functions using `auto`. View proxies cannot be captured by value due to their design; they must be captured by reference. This is because view proxies return references to their arguments and are not copyable. ```C++ auto tr = transposed(m); //Error: the return type of transposed can not be copied. auto & tr = transposed(m); // Correct usage ``` -------------------------------- ### Create an Identity Quaternion Source: https://www.boost.org/doc/libs/latest/libs/qvm/doc/html/index Returns an identity quaternion. The scalar type `S` specifies the element type, and the return type is unspecified. ```cpp namespace boost { namespace qvm { template -unspecified-return-type- identity_quat(); } } ``` -------------------------------- ### Negate Row View Proxy (Boost QVM) Source: https://www.boost.org/doc/libs/latest/libs/qvm/index Provides a read-only view proxy for a matrix `m` where a specific row `R` is negated. This allows viewing the matrix with a row's elements multiplied by -1 without altering the original matrix. The template parameter `R` specifies the zero-based index of the row to negate. ```cpp namespace boost { namespace qvm { template -unspecified-return-type- neg_row(); } } ``` -------------------------------- ### Use Registered Custom Vector and Matrix Types with QVM Operations Source: https://www.boost.org/doc/libs/latest/libs/qvm/index Demonstrates using QVM operations on registered custom types (float3 vector and float33 matrix). Shows element access via swizzling (X, Y, Z), magnitude calculation, matrix creation, and matrix-vector multiplication operations available after trait specialization. ```cpp 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; ``` -------------------------------- ### Boost.QVM Quaternion Conversion (C++) Source: https://www.boost.org/doc/libs/latest/libs/qvm/index Converts between quaternion types or from a 3x3 rotation matrix to a quaternion. The first overload performs a direct assignment, while the second assumes an orthonormal rotation matrix and converts it. Requires the return type `R` to be copyable. ```cpp #include 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 ); } } // Requires: `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. ``` -------------------------------- ### Column Matrix View Proxy (Boost QVM) Source: https://www.boost.org/doc/libs/latest/libs/qvm/index Returns an lvalue view proxy that allows a vector `a` to be accessed as a matrix-column. This is useful for operations that treat vectors as single-column matrices. The function is enabled if `a` is a vector type. ```cpp namespace boost { namespace qvm { //Only enabled if: is_vec::value template -unspecified-return-type- col_mat( A & a ); } } ``` -------------------------------- ### Specifying Return Types for Binary Operations with `auto` Source: https://www.boost.org/doc/libs/latest/libs/qvm/index Demonstrates how to use `auto` with C++11 and later to capture the result of a binary operation between matrices from different APIs. QVM defaults to returning a compatible matrix type, which can be implicitly converted. This approach is safe and often optimized by compilers. ```cpp auto & m = m1 * m2; // auto requires C++11 ``` -------------------------------- ### Matrix Comparison with Predicate (Boost QVM) Source: https://www.boost.org/doc/libs/latest/libs/qvm/index Compares two matrices using a provided binary predicate for element comparison. Matrices must have the same dimensions. This allows for custom comparison logic beyond simple equality. ```cpp 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 ); } } ``` -------------------------------- ### Define Throw Exception Macro - C++ Source: https://www.boost.org/doc/libs/latest/libs/qvm/index Defines BOOST_QVM_THROW_EXCEPTION for exception handling in QVM. It defaults to BOOST_THROW_EXCEPTION, requiring substituted implementations to not return control to the caller. ```cpp #include namespace boost { namespace qvm { #ifndef BOOST_QVM_THROW_EXCEPTION #include #define BOOST_QVM_THROW_EXCEPTION BOOST_THROW_EXCEPTION #endif } } ``` -------------------------------- ### Using Boost QVM Namespace Source: https://www.boost.org/doc/libs/latest/libs/qvm/doc/html/index This code snippet demonstrates how to bring the entire `boost::qvm` namespace into scope. This is generally safe due to SFINAE/`enable_if` usage in QVM's function and operator overloads, which only enable them when QVM-specific type traits are present. This avoids ambiguities with third-party libraries unless accessing types, not functions. ```cpp using namespace boost::qvm; ``` -------------------------------- ### SFINAE and enable_if for Constrained Template Overloads Source: https://www.boost.org/doc/libs/latest/libs/qvm/index Details how SFINAE (Substitution Failure Is Not An Error) and Boost's `enable_if` are used in QVM to selectively enable template function overloads. This prevents participation in overload resolution for inappropriate types, ensuring correct compilation and avoiding ambiguity. ```cpp template Vector operator*( Matrix const & m, Vector const & v ); ``` ```cpp 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 ); ``` -------------------------------- ### Create Matrix for Translation Source: https://www.boost.org/doc/libs/latest/libs/qvm/doc/html/index This code snippet shows how to create a 4x4 matrix that translates by a given 3D vector using the translation_mat function. It defines a 3D vector and then uses it to create the translation matrix. ```cpp vec v = {0,0,7}; mat tr = translation_mat(v); ``` -------------------------------- ### Use Custom Vector and Matrix Types with QVM Operations Source: https://www.boost.org/doc/libs/latest/libs/qvm/doc/html/index This code snippet demonstrates using custom vector and matrix types ('float3' and 'float33') after they have been registered with QVM. It shows setting vector elements, calculating magnitude, creating a rotation matrix, and performing matrix-vector multiplication. ```cpp 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; ``` -------------------------------- ### Base Exception Class for QVM Errors (C++) Source: https://www.boost.org/doc/libs/latest/libs/qvm/index The error struct is the base class for all exceptions thrown by the QVM library. It inherits from virtual boost::exception and virtual std::exception, providing a common root for QVM-specific error types. ```cpp #include namespace boost { namespace qvm { struct error: virtual boost::exception, virtual std::exception { }; } } ``` -------------------------------- ### Define quat_traits_defaults Template Implementation Source: https://www.boost.org/doc/libs/latest/libs/qvm/index Base template providing default implementations of scalar_type and read_element for quaternion trait specializations. Designed to reduce boilerplate by automatically defining read_element from the user-provided write_element function. ```cpp 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)); } }; } } ``` -------------------------------- ### Translation Matrix View Proxy (Boost QVM) Source: https://www.boost.org/doc/libs/latest/libs/qvm/index Returns an lvalue view proxy that represents a vector `a` as a translation matrix. The size of the resulting matrix is 1 + the dimension of the vector `a`. This is enabled when `a` is a vector type. ```cpp namespace boost { namespace qvm { //Only enabled if: is_vec::value template -unspecified-return-type- translation_mat( A & a ); } } ``` -------------------------------- ### qref: Create Identity View Proxy for Quaternions (C++) Source: https://www.boost.org/doc/libs/latest/libs/qvm/doc/html/index The qref function creates an identity view proxy for a quaternion `a`. This is useful for calling QVM operations when `a` is of a built-in type, such as a plain old C array. ```cpp #include namespace boost { namespace qvm { //Only enabled if: is_quat::value template -unspecified-return-type- qref( A & a ); } } ``` -------------------------------- ### Create zero matrix (C++) Source: https://www.boost.org/doc/libs/latest/libs/qvm/index Creates a read-only zero matrix with specified scalar type and dimensions. Two template overloads allow specification of either a single dimension D (for square matrices) or separate row and column counts R and C. All elements are initialized to zero. ```cpp namespace boost { namespace qvm { template -unspecified-return-type- zero_mat(); template -unspecified-return-type- zero_mat(); } } ``` -------------------------------- ### Include Quaternion Traits and Operations Headers Source: https://www.boost.org/doc/libs/latest/libs/qvm/doc/html/index C++ headers for accessing quaternion traits, element access, operations, and the quat class template. These headers enable integration of quaternion types with the QVM library and provide trait definitions for quaternion type customization. ```cpp #include #include #include ``` ```cpp #include ``` ```cpp #include ``` ```cpp #include ``` -------------------------------- ### C/C++ Array Operations with Boost.QVM Source: https://www.boost.org/doc/libs/latest/libs/qvm/doc/html/index Demonstrates how Boost.QVM can operate directly on plain C arrays and std::array objects for vector and matrix operations. It also shows how to use `vref` to enable operations on C arrays that would otherwise be illegal. ```cpp float v[3] = {0,0,7}; float3 vrot = rotx_mat<3>(3.14159f) * v; // Illegal operation without vref // float v[3] = {0,0,7}; // v *= 42; // Using vref to enable operations on C arrays float v[3] = {0,0,7}; vref(v) *= 42; ``` -------------------------------- ### Configure Trivial Inline Macro for QVM Source: https://www.boost.org/doc/libs/latest/libs/qvm/doc/html/index Defines BOOST_QVM_INLINE_TRIVIAL macro used for simple, non-critical functions like one-liners that should always be inlined. Defaults to BOOST_QVM_FORCE_INLINE for optimal performance. ```C++ #include #ifndef BOOST_QVM_INLINE_TRIVIAL #define BOOST_QVM_INLINE_TRIVIAL BOOST_QVM_FORCE_INLINE #endif ``` -------------------------------- ### Shortened vec_traits Specialization using vec_traits_defaults (C++) Source: https://www.boost.org/doc/libs/latest/libs/qvm/index This snippet shows how to simplify `vec_traits` specialization by inheriting from `vec_traits_defaults`. This base template handles common definitions, allowing the user to only override specific access functions like `write_element` and `write_element_idx`. ```cpp 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 }; } } ``` -------------------------------- ### Convert Quaternion to String (to_string) Source: https://www.boost.org/doc/libs/latest/libs/qvm/index Converts a quaternion to its string representation. The format of the string is unspecified. This function requires the input `A` to be a quaternion. ```cpp #include namespace boost { namespace qvm { //Only enabled if: is_quat::value template std::string to_string( A const & a ); } } ``` -------------------------------- ### Scalar Type Requirements in C++ Source: https://www.boost.org/doc/libs/latest/libs/qvm/index Defines the essential operations and constructors required for a user-defined scalar type 'S' to be compatible with Boost.QVM. This includes arithmetic operators, assignment operators, and comparisons, along with specific zero and one value constructions. ```cpp 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 ); // Additionally, S(0) and S(1) should construct zero and one values, // or scalar_traits must be specialized. ```