OpenWalnut  1.5.0dev
WWriterMatrixSymVTK.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 <string>
28 #include <vector>
29 
30 
31 #include "../../common/WIOTools.h"
32 #include "../exceptions/WDHIOFailure.h"
33 #include "WWriterMatrixSymVTK.h"
34 
35 WWriterMatrixSymVTK::WWriterMatrixSymVTK( std::string fname, bool overwrite )
36  : WWriter( fname, overwrite )
37 {
38 }
39 
40 void WWriterMatrixSymVTK::writeTable( const std::vector< double > &table, size_t dim ) const
41 {
42  using std::fstream;
43  fstream out( m_fname.c_str(), fstream::out | fstream::in | fstream::trunc );
44  if( !out || out.bad() )
45  {
46  throw WDHIOFailure( std::string( "Invalid file, or permission: " + m_fname ) );
47  }
48  out << "# vtk DataFile Version 3.0" << std::endl;
49  out << "WMatrixSym from OpenWalnut" << std::endl;
50  out << "BINARY" << std::endl;
51 
52  out << "FIELD WMatrixSym 1" << std::endl;
53  unsigned int numDistances = table.size() + 1;
54  out << "ELEMENTS " << numDistances << " 1 float" << std::endl;
55  float *data = new float[numDistances];
56 
57  for( size_t i = 0; i < table.size() ; ++i )
58  {
59  data[i] = static_cast< float >( table[i] );
60  }
61  data[ numDistances - 1 ] = static_cast< float >( dim );
62 
63  switchByteOrderOfArray< float >( data, numDistances );
64  out.write( reinterpret_cast< char* >( data ), sizeof( float ) * numDistances );
65  out << std::endl;
66  out.close();
67 }
Use this for IO error handling.
Definition: WDHIOFailure.h:38
WWriterMatrixSymVTK(std::string fname, bool overwrite=false)
Creates a writer object for FiberVTK file writing.
void writeTable(const std::vector< double > &table, size_t dim) const
Actually perform writing to file.
Write some data to the given file.
Definition: WWriter.h:38
std::string m_fname
Absolute path of the file to write to.
Definition: WWriter.h:67