Main Page   Class Hierarchy   Compound List   File List   Compound Members  

simMatrix.cpp

00001 //////////////////////////////////////////////////////////////////////
00002 // Copyright (c) 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/simulator/simMatrix.h>
00020 
00021 ////////////////////////////////////////////////////////////////////////////////
00022 // LOCAL DECLARATIONS
00023 
00024 namespace freecloth {
00025 
00026 ////////////////////////////////////////////////////////////////////////////////
00027 // CLASS SimMatrix
00028 
00029 //------------------------------------------------------------------------------
00030 
00031 SimMatrix& SimMatrix::operator+=( const SimMatrix& rhs )
00032 {
00033     DGFX_ASSERT( nbRows() == rhs.nbRows() );
00034     DGFX_ASSERT( nbColumns() == rhs.nbColumns() );
00035     RowIterator r1;
00036     RowConstIterator r2;
00037     for( r1 = beginRow(), r2 = rhs.beginRow(); r1 != endRow(); ++r1, ++r2 ) {
00038         ColumnIterator c1 = r1->beginColumn();
00039         ColumnConstIterator c2;
00040         for( c2 = r2->beginColumn(); c2 != r2->endColumn(); ++c2 ) {
00041             while ( c1 != r1->endColumn() && c1->_column < c2->_column ) {
00042                 ++c1;
00043             }
00044             if ( c1 == r1->endColumn() ) {
00045                 r1->_columns.push_back( *c2 );
00046                 continue;
00047             } else if ( c2->_column < c1->_column ) {
00048                 r1->_columns.insert( c1, *c2 );
00049                 continue;
00050             }
00051             DGFX_ASSERT( c1->_column == c2->_column );
00052             c1->_data += c2->_data;
00053         }
00054     }
00055     return *this;
00056 }
00057 
00058 //------------------------------------------------------------------------------
00059 
00060 void SimMatrix::multiply(
00061     SimVector& destV,
00062     const SimMatrix& srcM,
00063     const SimVector& srcV
00064 ) {
00065     DGFX_ASSERT( srcM.nbColumns() == srcV.size() );
00066     if ( destV.size() != srcM.nbRows() ) {
00067         destV = SimVector::zero( srcM.nbRows() );
00068     }
00069     else {
00070         destV.clear();
00071     }
00072     for ( RowConstIterator rit = srcM.beginRow(); rit != srcM.endRow(); ++rit ){
00073         UInt32 row = rit - srcM.beginRow();
00074         GeVector& v = destV[ row ];
00075         ColumnConstIterator c;
00076         for ( c = rit->beginColumn(); c != rit->endColumn(); ++c ) {
00077             v += c->_data * srcV[ c->_column ];
00078         }
00079     }
00080 }
00081 
00082 
00083 ////////////////////////////////////////////////////////////////////////////////
00084 // GLOBAL FUNCTIONS
00085 //
00086 
00087 //------------------------------------------------------------------------------
00088 
00089 std::ostream& operator<<( std::ostream& out, const SimMatrix& m )
00090 {
00091     out << "M(" << std::endl;
00092     for ( UInt32 r = 0; r < m.nbRows(); ++r ) {
00093         out << "  ";
00094         for ( UInt32 c = 0; c < m.nbColumns(); ++c ) {
00095             out << m[ r ][ c ] << " ";
00096         }
00097         out << std::endl;
00098     }
00099     out << ")";
00100     return out;
00101 }
00102 
00103 }

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