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