OpenWalnut  1.5.0dev
WOSSIMHelper.cpp
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifdef OW_USE_OSSIM
26 
27 #include <memory>
28 
29 #include <ossim/matrix/newmat.h> // NOLINT: brainlint thinks this is C System Header
30 #include <ossim/matrix/newmatap.h> // NOLINT: brainlint thinks this is C System Header
31 
32 #include "WOSSIMHelper.h"
33 
34 std::shared_ptr< NEWMAT::Matrix > WOSSIMHelper::OWMatrixToOSSIMMatrix( const WMatrix<double> &input )
35 {
36  std::shared_ptr< NEWMAT::Matrix > result( new NEWMAT::Matrix( input.getNbRows(), input.getNbCols() ) );
37  for( size_t row = 0; row < input.getNbRows(); row++ )
38  {
39  for( size_t col = 0; col < input.getNbCols(); col++ )
40  {
41  ( *result )( static_cast<int>( row+1 ), static_cast<int>( col+1 ) ) = input( row, col );
42  }
43  }
44  return result;
45 }
46 
47 WMatrix<double> WOSSIMHelper::OSSIMMatrixToOWMatrix( const NEWMAT::Matrix& input )
48 {
49  WMatrix<double> result( static_cast<size_t>( input.Nrows() ), static_cast<size_t>( input.Ncols() ) );
50 
51  for( size_t row = 0; row < result.getNbRows(); row++ )
52  {
53  for( size_t col = 0; col < result.getNbCols(); col++ )
54  {
55  result( row, col ) = input( static_cast<int>( row+1 ), static_cast<int>( col+1 ) );
56  }
57  }
58  return result;
59 }
60 
61 WMatrix<double> WOSSIMHelper::OSSIMDiagonalMatrixToOWMatrix( const NEWMAT::DiagonalMatrix& input )
62 {
63  WMatrix<double> result( static_cast<size_t>( input.Nrows() ), static_cast<size_t>( input.Ncols() ) );
64 
65  for( size_t i = 0; i < result.getNbRows(); i++ )
66  {
67  result( i, i ) = input( static_cast<int>( i+1 ) );
68  }
69  return result;
70 }
71 
72 WValue<double> WOSSIMHelper::OSSIMDiagonalMatrixToOWVector( const NEWMAT::DiagonalMatrix& input )
73 {
74  WValue<double> result( static_cast<size_t>( input.Nrows() ) );
75 
76  for( size_t i = 0; i < result.size(); i++ )
77  {
78  result[ i ] = input( static_cast<int>( i+1 ) );
79  }
80  return result;
81 }
82 
83 // gsl_vector* OSSIMHelper::OWVectorToOSSIMVector( const WValue<double> &input )
84 // {
85 // }
86 //
87 // WValue<double> OSSIMHelper::OSSIMVectorToOWVector( const gsl_vector* input )
88 // {
89 // }
90 
91 
92 void WOSSIMHelper::computeSVD( const WMatrix< double >& A,
95  WValue< double >& S )
96 {
97  // create matrices in OSSIM format
98  std::shared_ptr< NEWMAT::Matrix > AA( OWMatrixToOSSIMMatrix( A ) );
99  NEWMAT::DiagonalMatrix SS( static_cast<int>( S.size() ) );
100  NEWMAT::Matrix UU( static_cast<int>( U.getNbRows() ), static_cast<int>( U.getNbCols() ) );
101  NEWMAT::Matrix VV( static_cast<int>( V.getNbRows() ), static_cast<int>( V.getNbCols() ) );
102  // do SVD
103  NEWMAT::SVD( *AA, SS, UU, VV );
104  // convert Matrices to OW format
105  S = OSSIMDiagonalMatrixToOWVector( SS );
106  U = OSSIMMatrixToOWMatrix( UU );
107  V = OSSIMMatrixToOWMatrix( VV );
108 }
109 
110 // WMatrix<double> WOSSIMHelper::pseudoInverse( const WMatrix<double>& input )
111 // {
112 // // calc pseudo inverse
113 // WMatrix< double > U( input.getNbRows(), input.getNbCols() );
114 // WMatrix< double > V( input.getNbCols(), input.getNbCols() );
115 // WValue< double > Svec( input.getNbCols() );
116 // WOSSIMHelper::computeSVD( input, U, V, Svec );
117 //
118 // // create diagonal matrix S
119 // WMatrix< double > S( input.getNbCols(), input.getNbCols() );
120 //
121 // for( size_t i = 0; i < Svec.size() && i < S.getNbRows() && i < S.getNbCols(); i++ )
122 // S( i, i ) = ( Svec[ i ] == 0.0 ) ? 0.0 : 1.0 / Svec[ i ];
123 //
124 // return WMatrix< double >( V*S*U.transposed() );
125 // }
126 #endif
size_t getNbRows() const
Get number of rows.
Definition: WMatrix.h:375
size_t getNbCols() const
Get number of columns.
Definition: WMatrix.h:383
Base class for all higher level values like tensors, vectors, matrices and so on.
Definition: WValue.h:41
size_t size() const
Get number of components the value consists of.
Definition: WValue.h:116