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::operator*( const GeMatrix3& rhs ) const
00058 {
00059     GeMatrix3 result;
00060     for ( UInt32 r = 0; r < 3; ++r ) {
00061         for ( UInt32 c = 0; c < 3; ++c ) {
00062             Float val = 0;
00063             for ( UInt32 i = 0; i < 3; ++i ) {
00064                 val += operator()( r, i ) * rhs( i, c );
00065             }
00066             result( r, c ) = val;
00067         }
00068     }
00069     return result;
00070 }
00071 
00072 //------------------------------------------------------------------------------
00073 
00074 GeMatrix3 GeMatrix3::getInverse() const
00075 {
00076     GeMatrix3 result(
00077         operator()(1,1) * operator()(2,2) - operator()(1,2) * operator()(2,1),
00078         operator()(0,2) * operator()(2,1) - operator()(0,1) * operator()(2,2),
00079         operator()(0,1) * operator()(1,2) - operator()(0,2) * operator()(1,1),
00080 
00081         operator()(1,2) * operator()(2,0) - operator()(1,0) * operator()(2,2),
00082         operator()(0,0) * operator()(2,2) - operator()(0,2) * operator()(2,0),
00083         operator()(0,2) * operator()(1,0) - operator()(0,0) * operator()(1,2),
00084 
00085         operator()(1,0) * operator()(2,1) - operator()(1,1) * operator()(2,0),
00086         operator()(0,1) * operator()(2,0) - operator()(0,0) * operator()(2,1),
00087         operator()(0,0) * operator()(1,1) - operator()(0,1) * operator()(1,0)
00088     );
00089 
00090     Float det =
00091         operator()(0,0) * result(0,0) +
00092         operator()(0,1) * result(1,0) +
00093         operator()(0,2) * result(2,0);
00094 
00095     DGFX_ASSERT( ! BaMath::isEqual( BaMath::abs( det ), 0 ) );
00096 
00097     Float invDet = 1.0f / det;
00098     for( UInt32 i = 0; i < 9; ++i ) {
00099         result._data[ i ] *= invDet;
00100     }
00101 
00102     return result;
00103 }
00104 
00105 
00106 ////////////////////////////////////////////////////////////////////////////////
00107 // GLOBAL FUNCTIONS
00108 //
00109 
00110 //------------------------------------------------------------------------------
00111 
00112 std::ostream& operator<<( std::ostream& out, const GeMatrix3& m )
00113 {
00114     out << "M3(" << std::endl;
00115     out << "  " << m(0,0) << " " << m(0,1) << " " << m(0,2) << std::endl;
00116     out << "  " << m(1,0) << " " << m(1,1) << " " << m(1,2) << std::endl;
00117     out << "  " << m(2,0) << " " << m(2,1) << " " << m(2,2) << std::endl;
00118     out << ")";
00119     return out;
00120 }
00121 
00122 FREECLOTH_NAMESPACE_END

Generated on Wed Apr 23 15:58:51 2003 for Freecloth by doxygen1.3-rc3