OpenWalnut 1.2.5
WWriterFiberVTK.cpp
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 // Use filesystem version 2 for compatibility with newer boost versions.
00029 #ifndef BOOST_FILESYSTEM_VERSION
00030     #define BOOST_FILESYSTEM_VERSION 2
00031 #endif
00032 #include <boost/filesystem.hpp>
00033 #include <boost/shared_ptr.hpp>
00034 
00035 #include "../../common/WAssert.h"
00036 #include "../../common/WIOTools.h"
00037 #include "../WDataSetFiberVector.h"
00038 #include "../exceptions/WDHIOFailure.h"
00039 #include "WWriterFiberVTK.h"
00040 
00041 WWriterFiberVTK::WWriterFiberVTK( const boost::filesystem::path& path, bool overwrite )
00042     : WWriter( path.file_string(), overwrite )
00043 {
00044 }
00045 void WWriterFiberVTK::writeFibs( boost::shared_ptr< const WDataSetFibers > fiberDS ) const
00046 {
00047     writeFibs( boost::shared_ptr< WDataSetFiberVector >( new WDataSetFiberVector( fiberDS ) ) );
00048 }
00049 
00050 void WWriterFiberVTK::writeFibs( boost::shared_ptr< const WDataSetFiberVector > fiberDS ) const
00051 {
00052     using std::fstream;
00053     fstream out( m_fname.c_str(), fstream::out | fstream::in | fstream::trunc );
00054     if( !out || out.bad() )
00055     {
00056         throw WDHIOFailure( std::string( "Invalid file, or permission: " + m_fname ) );
00057     }
00058     // We use '\n' as line delimiter so also files written under windows (having '\r\n' as delimtier) may be read anywhere
00059     char lineDelimiter = '\n';
00060 
00061     out << "# vtk DataFile Version 3.0" << lineDelimiter;
00062     out << "Fibers from OpenWalnut" << lineDelimiter;
00063     out << "BINARY" << lineDelimiter;
00064     out << "DATASET POLYDATA" << lineDelimiter;
00065     unsigned int numPoints = 0;
00066     unsigned int numLines = fiberDS->size();
00067     for( size_t i = 0; i < fiberDS->size(); ++i )
00068     {
00069         numPoints += (*fiberDS)[i].size();
00070     }
00071     out << "POINTS " << numPoints << " float" << lineDelimiter;
00072     unsigned int *rawLineData = new unsigned int[numPoints + numLines];
00073     float *rawPointData = new float[numPoints * 3];
00074 
00075     unsigned int pntPosOffset = 0;
00076     unsigned int lnsPosOffset = 0;
00077     for( size_t i = 0; i < fiberDS->size(); ++i )
00078     {
00079         const WFiber &fib = (*fiberDS)[i];
00080         rawLineData[lnsPosOffset++] = static_cast< unsigned int >( fib.size() );
00081         for( size_t j = 0; j < fib.size(); ++j )
00082         {
00083             const WPosition &point = fib[j];
00084             WAssert( pntPosOffset % 3 == 0, "(pOff % 3) was not equal to 0" );
00085             WAssert( pntPosOffset / 3 < numPoints, "pntPosOffset is to large." );
00086             rawLineData[lnsPosOffset++] = static_cast< unsigned int >( pntPosOffset / 3 );
00087             rawPointData[pntPosOffset++] = static_cast< float >( point[0] );
00088             rawPointData[pntPosOffset++] = static_cast< float >( point[1] );
00089             rawPointData[pntPosOffset++] = static_cast< float >( point[2] );
00090             WAssert( pntPosOffset < ( ( numPoints * 3 ) + 1 ), "pOff < #pts" );
00091         }
00092     }
00093     switchByteOrderOfArray< float >( rawPointData, numPoints * 3 );
00094     switchByteOrderOfArray< unsigned int >( rawLineData, numLines + numPoints );
00095     out.write( reinterpret_cast< char* >( rawPointData ), sizeof( float ) * numPoints * 3 );
00096     out << lineDelimiter;
00097     out << "LINES " << numLines << " " << numPoints + numLines << lineDelimiter;
00098     out.write( reinterpret_cast< char* >( rawLineData ), sizeof( unsigned int ) * ( numPoints + numLines ) );
00099     out << lineDelimiter;
00100     out.close();
00101 }
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends