OpenWalnut  1.2.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
WReaderMatrixSymVTK.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 #include <fstream>
26 #include <stdexcept>
27 #include <string>
28 #include <vector>
29 
30 #include <boost/lexical_cast.hpp>
31 #include <boost/shared_ptr.hpp>
32 
33 #include "../../common/WAssert.h"
34 #include "../../common/WIOTools.h"
35 #include "../../common/WLogger.h"
36 #include "../../common/WStringUtils.h"
37 #include "../exceptions/WDHException.h"
38 #include "../exceptions/WDHIOFailure.h"
39 #include "WReader.h"
40 #include "WReaderMatrixSymVTK.h"
41 
43  : WReader( fname )
44 {
45 }
46 
47 void WReaderMatrixSymVTK::readTable( boost::shared_ptr< std::vector< double > > table ) const
48 {
49  WAssert( table->size() == 0, "Non-zero size indicates an error, since the vector will be filled IN HERE." );
50 
51  // code mainly taken from WLoaderFibers.cpp, and adjusted since I don't
52  // know how to code this DRY. Any suggestions?
53  std::ifstream ifs;
54  ifs.open( m_fname.c_str(), std::ifstream::in | std::ifstream::binary );
55  WAssert( ifs && !ifs.bad(), "" );
56 
57  std::vector< std::string > header;
58  std::string line;
59  try
60  {
61  for( int i = 0; i < 4; ++i ) // strip first four lines
62  {
63  std::getline( ifs, line );
64  if( !ifs.good() )
65  {
66  throw WDHException( std::string( "Unexpected end of file: " + m_fname ) );
67  }
68  header.push_back( line );
69  }
70  }
71  catch( const std::ios_base::failure &e )
72  {
73  throw WDHIOFailure( std::string( "Reading first 4 lines of '" + m_fname + "': " + e.what() ) );
74  }
75  if( header[0] != "# vtk DataFile Version 3.0" )
76  {
77  wlog::warn( "WReaderMatrixSymVTK" ) << "Wrong version string in file header found, expected: "
78  "\"# vtk DataFile Version 3.0\" but got: " << header[0];
79  }
80  if( header[2] != "BINARY" )
81  {
82  wlog::error( "WReaderMatrixSymVTK" ) << "Wrong data format: BINARY expected but got: " << header[2];
83  throw WDHIOFailure( "Error reading file '" + m_fname + " invalid binary format." );
84  }
85  if( header[3] != "FIELD WMatrixSym 1" )
86  {
87  wlog::error( "WReaderMatrixSymVTK" ) << "Wrong field desc in file header found: " << header[3] << " but expected: \"FIELD WMatrixSym 1\"";
88  throw WDHIOFailure( "Error reading file '" + m_fname + " invalid VTK field name." );
89  }
90 
91  try
92  {
93  std::getline( ifs, line ); // something like this: "DISTANCES 15879430 1 float" expected
94  }
95  catch( const std::ios_base::failure &e )
96  {
97  throw WDHIOFailure( std::string( "Error reading ELEMENTS field '" + m_fname + "': " + e.what() ) );
98  }
99  namespace su = string_utils;
100  size_t numDistances = 0;
101  std::vector< std::string > tokens = su::tokenize( line );
102  if( tokens.size() != 4 || su::toLower( tokens.at( 3 ) ) != "float" )
103  {
104  throw WDHException( std::string( "Invalid ELEMENTS declaration: " + line ) );
105  }
106  try
107  {
108  numDistances = boost::lexical_cast< size_t >( tokens.at( 1 ) );
109  }
110  catch( const boost::bad_lexical_cast &e )
111  {
112  throw WDHException( std::string( "Invalid number of elements: " + tokens.at( 1 ) ) );
113  }
114 
115  float *data = new float[ numDistances ];
116  try
117  {
118  ifs.read( reinterpret_cast< char* >( data ), sizeof( float ) * numDistances );
119  }
120  catch( const std::ios_base::failure &e )
121  {
122  throw WDHIOFailure( std::string( "Error reading elements in VTK ELEMENTS field '" + m_fname + "': " + e.what() ) );
123  }
124 
125  // all 4 bytes of each float are in wrong order we need to reorder them
126  switchByteOrderOfArray( data, numDistances );
127 
128  for( size_t i = 0; i < numDistances; ++i )
129  {
130  table->push_back( static_cast< double >( data[ i ] ) );
131  }
132 
133  delete[] data;
134 }