Main Page   Class Hierarchy   Compound List   File List   Compound Members  

geMatrix3.cpp

00001 //////////////////////////////////////////////////////////////////////
00002 // Copyright (c) 2002-2003 David Pritchard <drpritch@alumni.uwaterloo.ca>
00003 // 
00004 // This program is free software; you can redistribute it and/or
00005 // modify it under the terms of the GNU Lesser General Public License
00006 // as published by the Free Software Foundation; either
00007 // version 2 of the License, or (at your option) any later
00008 // version.
00009 // 
00010 // This program is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU Lesser General Public License for more details.
00014 // 
00015 // You should have received a copy of the GNU Lesser General Public License
00016 // along with this program; if not, write to the Free Software
00017 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018 
00019 #include <freecloth/geom/geMatrix3.h>
00020 
00021 ////////////////////////////////////////////////////////////////////////////////
00022 // LOCAL DECLARATIONS
00023 
00024 FREECLOTH_NAMESPACE_START
00025 
00026 ////////////////////////////////////////////////////////////////////////////////
00027 // CLASS GeMatrix3
00028 
00029 const GeMatrix3 GeMatrix3::IDENTITY(1,0,0, 0,1,0, 0,0,1);
00030 const GeMatrix3 GeMatrix3::ZERO(0,0,0, 0,0,0, 0,0,0);
00031 
00032 
00033 //------------------------------------------------------------------------------
00034 
00035 GeMatrix3 GeMatrix3::colMajor( const Float data[ 9 ] )
00036 {
00037     GeMatrix3 result;
00038     ::memcpy( result._data, data, sizeof( result._data ) );
00039     return result;
00040 }
00041 
00042 //------------------------------------------------------------------------------
00043 
00044 GeMatrix3 GeMatrix3::rowMajor( const Float data[ 9 ] )
00045 {
00046     GeMatrix3 result;
00047     for( UInt32 r = 0; r < 3; ++r ) {
00048         for( UInt32 c = 0; c < 3; ++c ) {
00049             result._data[ c*3 + r ] = data[ r*3 + c ];
00050         }
00051     }
00052     return result;
00053 }
00054 
00055 //------------------------------------------------------------------------------
00056 
00057 GeMatrix3 GeMatrix3::rotation( const GeVector& axis, Float theta )
00058 {
00059     const Float s = BaMath::sin( theta );
00060     const Float c = BaMath::cos( theta );
00061     const Float t = 1-c;
00062     const Float x = axis._x, y = axis._y, z = axis._z;
00063     return GeMatrix3(
00064         t*x*x + c,   t*x*y - s*z, t*x*z + s*y,
00065         t*x*y + s*z, t*y*y + c,   t*y*z - s*x,
00066         t*x*z - s*y, t*y*z + s*x, t*z*z + c
00067     );
00068 }
00069 
00070 //------------------------------------------------------------------------------
00071 
00072 GeMatrix3 GeMatrix3::operator*( const GeMatrix3& rhs ) const
00073 {
00074     GeMatrix3 result;
00075     for ( UInt32 r = 0; r < 3; ++r ) {
00076         for ( UInt32 c = 0; c < 3; ++c ) {
00077             Float val = 0;
00078             for ( UInt32 i = 0; i < 3; ++i ) {
00079                 val += operator()( r, i ) * rhs( i, c );
00080             }
00081             result( r, c ) = val;
00082         }
00083     }
00084     return result;
00085 }
00086 
00087 //------------------------------------------------------------------------------
00088 
00089 GeMatrix3 GeMatrix3::getInverse() const
00090 {
00091     GeMatrix3 result(
00092         operator()(1,1) * operator()(2,2) - operator()(1,2) * operator()(2,1),
00093         operator()(0,2) * operator()(2,1) - operator()(0,1) * operator()(2,2),
00094         operator()(0,1) * operator()(1,2) - operator()(0,2) * operator()(1,1),
00095 
00096         operator()(1,2) * operator()(2,0) - operator()(1,0) * operator()(2,2),
00097         operator()(0,0) * operator()(2,2) - operator()(0,2) * operator()(2,0),
00098         operator()(0,2) * operator()(1,0) - operator()(0,0) * operator()(1,2),
00099 
00100         operator()(1,0) * operator()(2,1) - operator()(1,1) * operator()(2,0),
00101         operator()(0,1) * operator()(2,0) - operator()(0,0) * operator()(2,1),
00102         operator()(0,0) * operator()(1,1) - operator()(0,1) * operator()(1,0)
00103     );
00104 
00105     Float det =
00106         operator()(0,0) * result(0,0) +
00107         operator()(0,1) * result(1,0) +
00108         operator()(0,2) * result(2,0);
00109 
00110     DGFX_ASSERT( ! BaMath::isEqual( BaMath::abs( det ), 0 ) );
00111 
00112     Float invDet = 1.0f / det;
00113     for( UInt32 i = 0; i < 9; ++i ) {
00114         result._data[ i ] *= invDet;
00115     }
00116 
00117     return result;
00118 }
00119 
00120 
00121 ////////////////////////////////////////////////////////////////////////////////
00122 // GLOBAL FUNCTIONS
00123 //
00124 
00125 //------------------------------------------------------------------------------
00126 
00127 std::ostream& operator<<( std::ostream& out, const GeMatrix3& m )
00128 {
00129     out << "M3(" << std::endl;
00130     out << "  " << m(0,0) << " " << m(0,1) << " " << m(0,2) << std::endl;
00131     out << "  " << m(1,0) << " " << m(1,1) << " " << m(1,2) << std::endl;
00132     out << "  " << m(2,0) << " " << m(2,1) << " " << m(2,2) << std::endl;
00133     out << ")";
00134     return out;
00135 }
00136 
00137 FREECLOTH_NAMESPACE_END

Generated on Fri May 2 13:04:24 2003 for Freecloth by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002