OpenWalnut
1.4.0
|
00001 //--------------------------------------------------------------------------- 00002 // 00003 // Project: OpenWalnut ( http://www.openwalnut.org ) 00004 // 00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS 00006 // For more information see http://www.openwalnut.org/copying 00007 // 00008 // This file is part of OpenWalnut. 00009 // 00010 // OpenWalnut is free software: you can redistribute it and/or modify 00011 // it under the terms of the GNU Lesser General Public License as published by 00012 // the Free Software Foundation, either version 3 of the License, or 00013 // (at your option) any later version. 00014 // 00015 // OpenWalnut is distributed in the hope that it will be useful, 00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00018 // GNU Lesser General Public License for more details. 00019 // 00020 // You should have received a copy of the GNU Lesser General Public License 00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>. 00022 // 00023 //--------------------------------------------------------------------------- 00024 00025 #ifdef OW_USE_OSSIM 00026 00027 #include <ossim/matrix/newmat.h> 00028 #include <ossim/matrix/newmatap.h> 00029 00030 #include <boost/shared_ptr.hpp> 00031 00032 #include "WOSSIMHelper.h" 00033 00034 boost::shared_ptr< NEWMAT::Matrix > WOSSIMHelper::OWMatrixToOSSIMMatrix( const WMatrix<double> &input ) 00035 { 00036 boost::shared_ptr< NEWMAT::Matrix > result( new NEWMAT::Matrix( input.getNbRows(), input.getNbCols() ) ); 00037 for( size_t row = 0; row < input.getNbRows(); row++ ) 00038 { 00039 for( size_t col = 0; col < input.getNbCols(); col++ ) 00040 { 00041 ( *result )( static_cast<int>( row+1 ), static_cast<int>( col+1 ) ) = input( row, col ); 00042 } 00043 } 00044 return result; 00045 } 00046 00047 WMatrix<double> WOSSIMHelper::OSSIMMatrixToOWMatrix( const NEWMAT::Matrix& input ) 00048 { 00049 WMatrix<double> result( static_cast<size_t>( input.Nrows() ), static_cast<size_t>( input.Ncols() ) ); 00050 00051 for( size_t row = 0; row < result.getNbRows(); row++ ) 00052 { 00053 for( size_t col = 0; col < result.getNbCols(); col++ ) 00054 { 00055 result( row, col ) = input( static_cast<int>( row+1 ), static_cast<int>( col+1 ) ); 00056 } 00057 } 00058 return result; 00059 } 00060 00061 WMatrix<double> WOSSIMHelper::OSSIMDiagonalMatrixToOWMatrix( const NEWMAT::DiagonalMatrix& input ) 00062 { 00063 WMatrix<double> result( static_cast<size_t>( input.Nrows() ), static_cast<size_t>( input.Ncols() ) ); 00064 00065 for( size_t i = 0; i < result.getNbRows(); i++ ) 00066 { 00067 result( i, i ) = input( static_cast<int>( i+1 ) ); 00068 } 00069 return result; 00070 } 00071 00072 WValue<double> WOSSIMHelper::OSSIMDiagonalMatrixToOWVector( const NEWMAT::DiagonalMatrix& input ) 00073 { 00074 WValue<double> result( static_cast<size_t>( input.Nrows() ) ); 00075 00076 for( size_t i = 0; i < result.size(); i++ ) 00077 { 00078 result[ i ] = input( static_cast<int>( i+1 ) ); 00079 } 00080 return result; 00081 } 00082 00083 // gsl_vector* OSSIMHelper::OWVectorToOSSIMVector( const WValue<double> &input ) 00084 // { 00085 // } 00086 // 00087 // WValue<double> OSSIMHelper::OSSIMVectorToOWVector( const gsl_vector* input ) 00088 // { 00089 // } 00090 00091 00092 void WOSSIMHelper::computeSVD( const WMatrix< double >& A, 00093 WMatrix< double >& U, 00094 WMatrix< double >& V, 00095 WValue< double >& S ) 00096 { 00097 // create matrices in OSSIM format 00098 boost::shared_ptr< NEWMAT::Matrix > AA( OWMatrixToOSSIMMatrix( A ) ); 00099 NEWMAT::DiagonalMatrix SS( static_cast<int>( S.size() ) ); 00100 NEWMAT::Matrix UU( static_cast<int>( U.getNbRows() ), static_cast<int>( U.getNbCols() ) ); 00101 NEWMAT::Matrix VV( static_cast<int>( V.getNbRows() ), static_cast<int>( V.getNbCols() ) ); 00102 // do SVD 00103 NEWMAT::SVD( *AA, SS, UU, VV ); 00104 // convert Matrices to OW format 00105 S = OSSIMDiagonalMatrixToOWVector( SS ); 00106 U = OSSIMMatrixToOWMatrix( UU ); 00107 V = OSSIMMatrixToOWMatrix( VV ); 00108 } 00109 00110 // WMatrix<double> WOSSIMHelper::pseudoInverse( const WMatrix<double>& input ) 00111 // { 00112 // // calc pseudo inverse 00113 // WMatrix< double > U( input.getNbRows(), input.getNbCols() ); 00114 // WMatrix< double > V( input.getNbCols(), input.getNbCols() ); 00115 // WValue< double > Svec( input.getNbCols() ); 00116 // WOSSIMHelper::computeSVD( input, U, V, Svec ); 00117 // 00118 // // create diagonal matrix S 00119 // WMatrix< double > S( input.getNbCols(), input.getNbCols() ); 00120 // 00121 // for( size_t i = 0; i < Svec.size() && i < S.getNbRows() && i < S.getNbCols(); i++ ) 00122 // S( i, i ) = ( Svec[ i ] == 0.0 ) ? 0.0 : 1.0 / Svec[ i ]; 00123 // 00124 // return WMatrix< double >( V*S*U.transposed() ); 00125 // } 00126 #endif