matrix_misc.h

Go to the documentation of this file.
00001 /* -*- C++ -*- ------------------------------------------------------------
00002  
00003 Copyright (c) 2007 Jesse Anders and Demian Nave http://cmldev.net/
00004 
00005 The Configurable Math Library (CML) is distributed under the terms of the
00006 Boost Software License, v1.0 (see cml/LICENSE for details).
00007 
00008  *-----------------------------------------------------------------------*/
00013 #ifndef matrix_misc_h
00014 #define matrix_misc_h
00015 
00016 #include <cml/mathlib/checking.h>
00017 
00018 /* Miscellaneous matrix functions. */
00019 
00020 namespace cml {
00021 
00023 template < typename E, class A, class B, class L > void
00024 identity_transform(matrix<E,A,B,L>& m)
00025 {
00026     typedef matrix<E,A,B,L> matrix_type;
00027     typedef typename matrix_type::value_type value_type;
00028 
00029     for (size_t i = 0; i < m.rows(); ++i) {
00030         for (size_t j = 0; j < m.cols(); ++j) {
00031             m(i,j) = value_type((i == j) ? 1 : 0);
00032         }
00033     }
00034 }
00035 
00037 template < class MatT > typename MatT::value_type
00038 trace(const MatT& m)
00039 {
00040     typedef typename MatT::value_type value_type;
00041     
00042     /* Checking */
00043     detail::CheckMatSquare(m);
00044 
00045     value_type t = value_type(0);
00046     for (size_t i = 0; i < m.rows(); ++i) {
00047         t += m(i,i);
00048     }
00049     return t;
00050 }
00051 
00053 template < class MatT > typename MatT::value_type
00054 trace_3x3(const MatT& m)
00055 {
00056     /* Checking */
00057     detail::CheckMatMin3x3(m);
00058     
00059     return m(0,0) + m(1,1) + m(2,2);
00060 }
00061 
00063 template < class MatT > typename MatT::value_type
00064 trace_2x2(const MatT& m)
00065 {
00066     /* Checking */
00067     detail::CheckMatMin2x2(m);
00068     
00069     return m(0,0) + m(1,1);
00070 }
00071 
00073 template < typename E, class A, class B, class L, class VecT > void
00074 matrix_skew_symmetric(matrix<E,A,B,L>& m, const VecT& v)
00075 {
00076     /* Checking */
00077     detail::CheckMatMin3x3(m);
00078     detail::CheckVec3(v);
00079 
00080     m.zero();
00081 
00082     m.set_basis_element(1,2, v[0]);
00083     m.set_basis_element(2,1,-v[0]);
00084     m.set_basis_element(2,0, v[1]);
00085     m.set_basis_element(0,2,-v[1]);
00086     m.set_basis_element(0,1, v[2]);
00087     m.set_basis_element(1,0,-v[2]);
00088 }
00089 
00091 template < typename E, class A, class B, class L > void
00092 matrix_skew_symmetric_2D(matrix<E,A,B,L>& m, E s)
00093 {
00094     /* Checking */
00095     detail::CheckMatMin2x2(m);
00096 
00097     m.zero();
00098 
00099     m.set_basis_element(0,1, s);
00100     m.set_basis_element(1,0,-s);
00101 }
00102 
00103 /* @todo: Clean this up, and implement SRT as well */
00104 
00106 template < typename E, class A, class B, class L > void
00107 matrix_invert_RT_only(matrix<E,A,B,L>& m)
00108 {
00109     typedef vector< E, fixed<3> > vector_type;
00110 
00111     vector_type x, y, z;
00112     matrix_get_basis_vectors(m,x,y,z);
00113     matrix_set_transposed_basis_vectors(m,x,y,z);
00114     
00115     vector_type p = matrix_get_translation(m);
00116     matrix_set_translation(m,-dot(p,x),-dot(p,y),-dot(p,z));
00117 }
00118 
00120 template < typename E, class A, class B, class L > void
00121 matrix_invert_RT_only_2D(matrix<E,A,B,L>& m)
00122 {
00123     typedef vector< E, fixed<2> > vector_type;
00124 
00125     vector_type x, y;
00126     matrix_get_basis_vectors_2D(m,x,y);
00127     matrix_set_transposed_basis_vectors_2D(m,x,y);
00128     
00129     vector_type p = matrix_get_translation_2D(m);
00130     matrix_set_translation_2D(m,-dot(p,x),-dot(p,y));
00131 }
00132 
00133 } // namespace cml
00134 
00135 #endif

Generated on Sat Jul 18 19:35:22 2009 for CML 1.0 by  doxygen 1.5.9