SpatialAlgebra.h

Go to the documentation of this file.
00001 #ifndef SimTK_SIMMATRIX_SPATIAL_ALGEBRA_H_
00002 #define SimTK_SIMMATRIX_SPATIAL_ALGEBRA_H_
00003 
00004 /* -------------------------------------------------------------------------- *
00005  *                      SimTK Core: SimTK Simmatrix(tm)                       *
00006  * -------------------------------------------------------------------------- *
00007  * This is part of the SimTK Core biosimulation toolkit originating from      *
00008  * Simbios, the NIH National Center for Physics-Based Simulation of           *
00009  * Biological Structures at Stanford, funded under the NIH Roadmap for        *
00010  * Medical Research, grant U54 GM072970. See https://simtk.org.               *
00011  *                                                                            *
00012  * Portions copyright (c) 2005-7 Stanford University and the Authors.         *
00013  * Authors: Michael Sherman                                                   *
00014  * Contributors:                                                              *
00015  *                                                                            *
00016  * Permission is hereby granted, free of charge, to any person obtaining a    *
00017  * copy of this software and associated documentation files (the "Software"), *
00018  * to deal in the Software without restriction, including without limitation  *
00019  * the rights to use, copy, modify, merge, publish, distribute, sublicense,   *
00020  * and/or sell copies of the Software, and to permit persons to whom the      *
00021  * Software is furnished to do so, subject to the following conditions:       *
00022  *                                                                            *
00023  * The above copyright notice and this permission notice shall be included in *
00024  * all copies or substantial portions of the Software.                        *
00025  *                                                                            *
00026  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
00027  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   *
00028  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    *
00029  * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,    *
00030  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR      *
00031  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE  *
00032  * USE OR OTHER DEALINGS IN THE SOFTWARE.                                     *
00033  * -------------------------------------------------------------------------- */
00034 
00041 #include "SimTKcommon/SmallMatrix.h"
00042 
00043 #include <iostream>
00044 
00045 namespace SimTK {
00046 
00047 // Spatial vectors are used for (orientation,translation) quantities.
00048 // These include
00049 //      spatial velocity     = (angularVelocity,linearVelocity)
00050 //      spatial acceleration = (angularAcceleration,linearAcceleration)
00051 //      generalized forces   = (torque,force)
00052 // Spatial configuration has to be handled differently though since
00053 // orientation is not a vector quantity. (We use "Transform" for this concept
00054 // which includes an orientation matrix and a translation vector.)
00055 typedef Vec<2,   Vec3>  SpatialVec;
00056 typedef Row<2,   Row3>  SpatialRow;
00057 typedef Mat<2,2, Mat33> SpatialMat;
00058 
00059 // support for efficient matrix multiplication involving the special phi
00060 // matrix
00061 
00062 using namespace SimTK;
00063 
00064 class PhiMatrixTranspose;
00065 
00066 
00067 inline static Vec3 
00068 unitVec(const Vec3& v) { 
00069     Real tmpNorm = v.norm();
00070     if (tmpNorm < 1e-15) tmpNorm = 1e-15;
00071 
00072     Vec3 ret = v/tmpNorm;
00073 
00074     return ret;
00075 }
00076 
00077 class PhiMatrix {
00078 public:
00079     typedef PhiMatrixTranspose TransposeType;
00080 
00081     PhiMatrix() { setToNaN(); }
00082     PhiMatrix(const Vec3& l) : l_(l) {}
00083 
00084     void setToZero() { l_ = 0.; }
00085     void setToNaN()  { l_.setToNaN(); }
00086 
00087     const Vec3& l() const { return l_; }
00088 private:
00089     Vec3 l_;
00090 };
00091 
00092 class PhiMatrixTranspose {
00093 public:
00094   PhiMatrixTranspose(const PhiMatrix& phi) : phi(phi) {}
00095   const Vec3& l() const {return phi.l();}
00096 private:
00097   const PhiMatrix& phi;
00098 };
00099 
00100 inline PhiMatrixTranspose
00101 transpose(const PhiMatrix& phi)
00102 {
00103     PhiMatrixTranspose ret(phi);
00104     return ret;
00105 }
00106 
00107 inline PhiMatrixTranspose
00108 operator~(const PhiMatrix& phi) {return transpose(phi);}
00109 
00110 inline SpatialVec
00111 operator*(const PhiMatrix&  phi,
00112           const SpatialVec& v)
00113 {
00114     return SpatialVec(v[0] + phi.l() % v[1],
00115                       v[1]);
00116 }
00117 
00118 inline SpatialMat
00119 operator*(const PhiMatrix&  phi,
00120           const SpatialMat& m)
00121 {
00122     const Mat33 x = crossMat(phi.l());
00123     return SpatialMat( m(0,0) + x*m(1,0), m(0,1) + x*m(1,1),
00124                            m(1,0)       ,     m(1,1));
00125 }
00126 
00127 inline SpatialVec
00128 operator*(const PhiMatrixTranspose& phiT,
00129           const SpatialVec&         v)
00130 {
00131     return SpatialVec(v[0],
00132                       v[1] + v[0] % phiT.l());
00133 }
00134 
00135 
00136 inline SpatialMat
00137 operator*(const SpatialMat::THerm&  m,
00138           const PhiMatrixTranspose& phiT)
00139 {
00140     const Mat33 x = crossMat(phiT.l());
00141     return SpatialMat( m(0,0) - m(0,1) * x, m(0,1),
00142                        m(1,0) - m(1,1) * x, m(1,1) );
00143 }
00144 
00145 inline SpatialMat
00146 operator*(const SpatialMat&         m,
00147           const PhiMatrixTranspose& phiT)
00148 {
00149     const Mat33 x = crossMat(phiT.l());
00150     return SpatialMat( m(0,0) - m(0,1) * x, m(0,1),
00151                        m(1,0) - m(1,1) * x, m(1,1) );
00152 }
00153 
00154 inline bool
00155 operator==(const PhiMatrix& p1, const PhiMatrix& p2)
00156 {
00157     return p1.l() == p2.l();
00158 }
00159 
00160 inline bool
00161 operator==(const PhiMatrixTranspose& p1, const PhiMatrixTranspose& p2)
00162 {
00163     return p1.l() == p2.l();
00164 }
00165 } // namespace SimTK
00166 
00167 #endif // SimTK_SIMMATRIX_SPATIAL_ALGEBRA_H_

Generated on Thu Feb 28 01:34:32 2008 for SimTKcommon by  doxygen 1.4.7