OpenWalnut
1.4.0
|
00001 //--------------------------------------------------------------------------- 00002 // 00003 // Project: OpenWalnut ( http://www.openwalnut.org ) 00004 // 00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS 00006 // For more information see http://www.openwalnut.org/copying 00007 // 00008 // This file is part of OpenWalnut. 00009 // 00010 // OpenWalnut is free software: you can redistribute it and/or modify 00011 // it under the terms of the GNU Lesser General Public License as published by 00012 // the Free Software Foundation, either version 3 of the License, or 00013 // (at your option) any later version. 00014 // 00015 // OpenWalnut is distributed in the hope that it will be useful, 00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00018 // GNU Lesser General Public License for more details. 00019 // 00020 // You should have received a copy of the GNU Lesser General Public License 00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>. 00022 // 00023 //--------------------------------------------------------------------------- 00024 00025 #include <fstream> 00026 #include <string> 00027 00028 #include <boost/filesystem.hpp> 00029 #include <boost/shared_ptr.hpp> 00030 00031 #include "../../common/WAssert.h" 00032 #include "../../common/WIOTools.h" 00033 #include "../WDataSetFiberVector.h" 00034 #include "../exceptions/WDHIOFailure.h" 00035 #include "WWriterFiberVTK.h" 00036 00037 WWriterFiberVTK::WWriterFiberVTK( const boost::filesystem::path& path, bool overwrite ) 00038 : WWriter( path.string(), overwrite ) 00039 { 00040 } 00041 void WWriterFiberVTK::writeFibs( boost::shared_ptr< const WDataSetFibers > fiberDS ) const 00042 { 00043 writeFibs( boost::shared_ptr< WDataSetFiberVector >( new WDataSetFiberVector( fiberDS ) ) ); 00044 } 00045 00046 void WWriterFiberVTK::writeFibs( boost::shared_ptr< const WDataSetFiberVector > fiberDS ) const 00047 { 00048 using std::fstream; 00049 fstream out( m_fname.c_str(), fstream::out | fstream::in | fstream::trunc ); 00050 if( !out || out.bad() ) 00051 { 00052 throw WDHIOFailure( std::string( "Invalid file, or permission: " + m_fname ) ); 00053 } 00054 // We use '\n' as line delimiter so also files written under windows (having '\r\n' as delimtier) may be read anywhere 00055 char lineDelimiter = '\n'; 00056 00057 out << "# vtk DataFile Version 3.0" << lineDelimiter; 00058 out << "Fibers from OpenWalnut" << lineDelimiter; 00059 out << "BINARY" << lineDelimiter; 00060 out << "DATASET POLYDATA" << lineDelimiter; 00061 unsigned int numPoints = 0; 00062 unsigned int numLines = fiberDS->size(); 00063 for( size_t i = 0; i < fiberDS->size(); ++i ) 00064 { 00065 numPoints += (*fiberDS)[i].size(); 00066 } 00067 out << "POINTS " << numPoints << " float" << lineDelimiter; 00068 unsigned int *rawLineData = new unsigned int[numPoints + numLines]; 00069 float *rawPointData = new float[numPoints * 3]; 00070 00071 unsigned int pntPosOffset = 0; 00072 unsigned int lnsPosOffset = 0; 00073 for( size_t i = 0; i < fiberDS->size(); ++i ) 00074 { 00075 const WFiber &fib = (*fiberDS)[i]; 00076 rawLineData[lnsPosOffset++] = static_cast< unsigned int >( fib.size() ); 00077 for( size_t j = 0; j < fib.size(); ++j ) 00078 { 00079 const WPosition &point = fib[j]; 00080 WAssert( pntPosOffset % 3 == 0, "(pOff % 3) was not equal to 0" ); 00081 WAssert( pntPosOffset / 3 < numPoints, "pntPosOffset is to large." ); 00082 rawLineData[lnsPosOffset++] = static_cast< unsigned int >( pntPosOffset / 3 ); 00083 rawPointData[pntPosOffset++] = static_cast< float >( point[0] ); 00084 rawPointData[pntPosOffset++] = static_cast< float >( point[1] ); 00085 rawPointData[pntPosOffset++] = static_cast< float >( point[2] ); 00086 WAssert( pntPosOffset < ( ( numPoints * 3 ) + 1 ), "pOff < #pts" ); 00087 } 00088 } 00089 switchByteOrderOfArray< float >( rawPointData, numPoints * 3 ); 00090 switchByteOrderOfArray< unsigned int >( rawLineData, numLines + numPoints ); 00091 out.write( reinterpret_cast< char* >( rawPointData ), sizeof( float ) * numPoints * 3 ); 00092 out << lineDelimiter; 00093 out << "LINES " << numLines << " " << numPoints + numLines << lineDelimiter; 00094 out.write( reinterpret_cast< char* >( rawLineData ), sizeof( unsigned int ) * ( numPoints + numLines ) ); 00095 out << lineDelimiter; 00096 out.close(); 00097 }