OpenWalnut  1.2.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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 <string>
27 
28 // Use filesystem version 2 for compatibility with newer boost versions.
29 #ifndef BOOST_FILESYSTEM_VERSION
30  #define BOOST_FILESYSTEM_VERSION 2
31 #endif
32 #include <boost/filesystem.hpp>
33 #include <boost/shared_ptr.hpp>
34 
35 #include "../../common/WAssert.h"
36 #include "../../common/WIOTools.h"
37 #include "../WDataSetFiberVector.h"
38 #include "../exceptions/WDHIOFailure.h"
39 #include "WWriterFiberVTK.h"
40 
41 WWriterFiberVTK::WWriterFiberVTK( const boost::filesystem::path& path, bool overwrite )
42  : WWriter( path.file_string(), overwrite )
43 {
44 }
45 void WWriterFiberVTK::writeFibs( boost::shared_ptr< const WDataSetFibers > fiberDS ) const
46 {
47  writeFibs( boost::shared_ptr< WDataSetFiberVector >( new WDataSetFiberVector( fiberDS ) ) );
48 }
49 
50 void WWriterFiberVTK::writeFibs( boost::shared_ptr< const WDataSetFiberVector > fiberDS ) const
51 {
52  using std::fstream;
53  fstream out( m_fname.c_str(), fstream::out | fstream::in | fstream::trunc );
54  if( !out || out.bad() )
55  {
56  throw WDHIOFailure( std::string( "Invalid file, or permission: " + m_fname ) );
57  }
58  // We use '\n' as line delimiter so also files written under windows (having '\r\n' as delimtier) may be read anywhere
59  char lineDelimiter = '\n';
60 
61  out << "# vtk DataFile Version 3.0" << lineDelimiter;
62  out << "Fibers from OpenWalnut" << lineDelimiter;
63  out << "BINARY" << lineDelimiter;
64  out << "DATASET POLYDATA" << lineDelimiter;
65  unsigned int numPoints = 0;
66  unsigned int numLines = fiberDS->size();
67  for( size_t i = 0; i < fiberDS->size(); ++i )
68  {
69  numPoints += (*fiberDS)[i].size();
70  }
71  out << "POINTS " << numPoints << " float" << lineDelimiter;
72  unsigned int *rawLineData = new unsigned int[numPoints + numLines];
73  float *rawPointData = new float[numPoints * 3];
74 
75  unsigned int pntPosOffset = 0;
76  unsigned int lnsPosOffset = 0;
77  for( size_t i = 0; i < fiberDS->size(); ++i )
78  {
79  const WFiber &fib = (*fiberDS)[i];
80  rawLineData[lnsPosOffset++] = static_cast< unsigned int >( fib.size() );
81  for( size_t j = 0; j < fib.size(); ++j )
82  {
83  const WPosition &point = fib[j];
84  WAssert( pntPosOffset % 3 == 0, "(pOff % 3) was not equal to 0" );
85  WAssert( pntPosOffset / 3 < numPoints, "pntPosOffset is to large." );
86  rawLineData[lnsPosOffset++] = static_cast< unsigned int >( pntPosOffset / 3 );
87  rawPointData[pntPosOffset++] = static_cast< float >( point[0] );
88  rawPointData[pntPosOffset++] = static_cast< float >( point[1] );
89  rawPointData[pntPosOffset++] = static_cast< float >( point[2] );
90  WAssert( pntPosOffset < ( ( numPoints * 3 ) + 1 ), "pOff < #pts" );
91  }
92  }
93  switchByteOrderOfArray< float >( rawPointData, numPoints * 3 );
94  switchByteOrderOfArray< unsigned int >( rawLineData, numLines + numPoints );
95  out.write( reinterpret_cast< char* >( rawPointData ), sizeof( float ) * numPoints * 3 );
96  out << lineDelimiter;
97  out << "LINES " << numLines << " " << numPoints + numLines << lineDelimiter;
98  out.write( reinterpret_cast< char* >( rawLineData ), sizeof( unsigned int ) * ( numPoints + numLines ) );
99  out << lineDelimiter;
100  out.close();
101 }