OpenWalnut  1.4.0
WWriterMatrixSymVTK.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 #include <vector>
00028 
00029 #include <boost/shared_ptr.hpp>
00030 
00031 #include "../../common/WIOTools.h"
00032 #include "../exceptions/WDHIOFailure.h"
00033 #include "WWriterMatrixSymVTK.h"
00034 
00035 WWriterMatrixSymVTK::WWriterMatrixSymVTK( std::string fname, bool overwrite )
00036     : WWriter( fname, overwrite )
00037 {
00038 }
00039 
00040 void WWriterMatrixSymVTK::writeTable( const std::vector< double > &table, size_t dim ) const
00041 {
00042     using std::fstream;
00043     fstream out( m_fname.c_str(), fstream::out | fstream::in | fstream::trunc );
00044     if( !out || out.bad() )
00045     {
00046         throw WDHIOFailure( std::string( "Invalid file, or permission: " + m_fname ) );
00047     }
00048     out << "# vtk DataFile Version 3.0" << std::endl;
00049     out << "WMatrixSym from OpenWalnut" << std::endl;
00050     out << "BINARY" << std::endl;
00051 
00052     out << "FIELD WMatrixSym 1" << std::endl;
00053     unsigned int numDistances = table.size() + 1;
00054     out << "ELEMENTS " << numDistances << " 1 float" << std::endl;
00055     float *data = new float[numDistances];
00056 
00057     for( size_t i = 0; i < table.size() ; ++i )
00058     {
00059         data[i] = static_cast< float >( table[i] );
00060     }
00061     data[ numDistances - 1 ] = static_cast< float >( dim );
00062 
00063     switchByteOrderOfArray< float >( data, numDistances );
00064     out.write( reinterpret_cast< char* >( data ), sizeof( float ) * numDistances );
00065     out << std::endl;
00066     out.close();
00067 }