OpenWalnut  1.5.0dev
WWriterFiberVTK.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 
29 #include <boost/filesystem.hpp>
30 
31 #include "../../common/WAssert.h"
32 #include "../../common/WIOTools.h"
33 #include "../WDataSetFiberVector.h"
34 #include "../exceptions/WDHIOFailure.h"
35 #include "WWriterFiberVTK.h"
36 
37 WWriterFiberVTK::WWriterFiberVTK( const boost::filesystem::path& path, bool overwrite )
38  : WWriter( path.string(), overwrite )
39 {
40 }
41 void WWriterFiberVTK::writeFibs( std::shared_ptr< const WDataSetFibers > fiberDS ) const
42 {
43  writeFibs( std::shared_ptr< WDataSetFiberVector >( new WDataSetFiberVector( fiberDS ) ) );
44 }
45 
46 void WWriterFiberVTK::writeFibs( std::shared_ptr< const WDataSetFiberVector > fiberDS ) const
47 {
48  using std::fstream;
49  fstream out( m_fname.c_str(), fstream::out | fstream::in | fstream::trunc );
50  if( !out || out.bad() )
51  {
52  throw WDHIOFailure( std::string( "Invalid file, or permission: " + m_fname ) );
53  }
54  // We use '\n' as line delimiter so also files written under windows (having '\r\n' as delimtier) may be read anywhere
55  char lineDelimiter = '\n';
56 
57  out << "# vtk DataFile Version 3.0" << lineDelimiter;
58  out << "Fibers from OpenWalnut" << lineDelimiter;
59  out << "BINARY" << lineDelimiter;
60  out << "DATASET POLYDATA" << lineDelimiter;
61  unsigned int numPoints = 0;
62  unsigned int numLines = fiberDS->size();
63  for( size_t i = 0; i < fiberDS->size(); ++i )
64  {
65  numPoints += (*fiberDS)[i].size();
66  }
67  out << "POINTS " << numPoints << " float" << lineDelimiter;
68  unsigned int *rawLineData = new unsigned int[numPoints + numLines];
69  float *rawPointData = new float[numPoints * 3];
70 
71  unsigned int pntPosOffset = 0;
72  unsigned int lnsPosOffset = 0;
73  for( size_t i = 0; i < fiberDS->size(); ++i )
74  {
75  const WFiber &fib = (*fiberDS)[i];
76  rawLineData[lnsPosOffset++] = static_cast< unsigned int >( fib.size() );
77  for( size_t j = 0; j < fib.size(); ++j )
78  {
79  const WPosition &point = fib[j];
80  WAssert( pntPosOffset % 3 == 0, "(pOff % 3) was not equal to 0" );
81  WAssert( pntPosOffset / 3 < numPoints, "pntPosOffset is to large." );
82  rawLineData[lnsPosOffset++] = static_cast< unsigned int >( pntPosOffset / 3 );
83  rawPointData[pntPosOffset++] = static_cast< float >( point[0] );
84  rawPointData[pntPosOffset++] = static_cast< float >( point[1] );
85  rawPointData[pntPosOffset++] = static_cast< float >( point[2] );
86  WAssert( pntPosOffset < ( ( numPoints * 3 ) + 1 ), "pOff < #pts" );
87  }
88  }
89  switchByteOrderOfArray< float >( rawPointData, numPoints * 3 );
90  switchByteOrderOfArray< unsigned int >( rawLineData, numLines + numPoints );
91  out.write( reinterpret_cast< char* >( rawPointData ), sizeof( float ) * numPoints * 3 );
92  out << lineDelimiter;
93  out << "LINES " << numLines << " " << numPoints + numLines << lineDelimiter;
94  out.write( reinterpret_cast< char* >( rawLineData ), sizeof( unsigned int ) * ( numPoints + numLines ) );
95  out << lineDelimiter;
96  out.close();
97 
98  // free memory
99  delete[] rawLineData;
100  delete[] rawPointData;
101 }
Use this for IO error handling.
Definition: WDHIOFailure.h:38
Represents a simple set of WFibers.
Represents a neural pathway.
Definition: WFiber.h:40
size_type size() const
Wrapper around std::vector member function.
Definition: WMixinVector.h:267
This only is a 3d double vector.
WWriterFiberVTK(const boost::filesystem::path &path, bool overwrite=false)
Creates a writer object for FiberVTK file writing.
void writeFibs(std::shared_ptr< const WDataSetFiberVector > fiberDS) const
Writes a WDataSetFiberVector down to the previousely given 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