Commit 85f2cc5a authored by Mathias Goldau's avatar Mathias Goldau
Browse files

[ADD #157] Saving of DLtLookUpTable now possible

parent b7e0ef8b
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#include <string>
#include "WWriter.h"
#include "WIOTools.hpp"
#include "../exceptions/WDHIOFailure.h"
WWriter::WWriter( std::string fname, bool overwrite )
: m_overwrite( overwrite )
{
setFileName( fname ); // not in constructor list since fileExcsits check
}
void WWriter::setFileName( std::string fname )
{
m_fname = fname;
if( !m_overwrite && wiotools::fileExists( m_fname ) )
{
throw WDHIOFailure( "File '" + m_fname + "' already exists, skip writing" );
}
}
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#ifndef WWRITER_H
#define WWRITER_H
#include <string>
/**
* Writes some data to a file. This class is only for convenience access to
* rudimentary file management things such as: file exists, if the given
* file should be overwritten, etc. Subclasses may use those mechanisms in
* their own public memberfunctions where the signature is different every
* time. e.g. writeFibs( WDataSetFibers ds );
*/
class WWriter
{
public:
/**
* Creates a writer object for writing something to a file. Use the flags
* to specify the behaviour.
*
* \param fname path to the target file where stuff will be written to
* \param overwrite If true existing files will be overwritten
*/
WWriter( std::string fname, bool overwrite = false );
/**
* Reset the destination (file) where the writing should take place.
*/
void setFileName( std::string fname );
protected:
std::string m_fname; //!< Absolute path of the file to write to
bool m_overwrite; //!< flag indicating if the file may be overwritten (true) or not (false)
private:
};
#endif // WWRITER_H
......@@ -28,27 +28,17 @@
#include <boost/shared_ptr.hpp>
#include "WWriterVTK.h"
#include "WWriterFiberVTK.h"
#include "WIOTools.hpp"
#include "../WDataSetFibers.h"
#include "../exceptions/WDHIOFailure.h"
WWriterVTK::WWriterVTK( std::string fname, bool overwrite )
: m_overwrite( overwrite )
WWriterFiberVTK::WWriterFiberVTK( std::string fname, bool overwrite )
: WWriter( fname, overwrite )
{
setFileName( fname );
}
void WWriterVTK::setFileName( std::string fname )
{
m_fname = fname;
if( !m_overwrite && wiotools::fileExists( m_fname ) )
{
throw WDHIOFailure( "File '" + m_fname + "' already exists, skip writing" );
}
}
void WWriterVTK::writeFibs( boost::shared_ptr< const WDataSetFibers > fiberDS ) const
void WWriterFiberVTK::writeFibs( boost::shared_ptr< const WDataSetFibers > fiberDS ) const
{
using std::fstream;
fstream out( m_fname.c_str(), fstream::out | fstream::in | fstream::trunc );
......@@ -56,7 +46,7 @@ void WWriterVTK::writeFibs( boost::shared_ptr< const WDataSetFibers > fiberDS )
{
throw WDHIOFailure( "Invalid file, or permission: " + m_fname );
}
out << "# vtk DataFile Version 3.0" << std::endl;
out << "# VTK DataFile Version 3.0" << std::endl;
out << "Fibers from OpenWalnut" << std::endl;
out << "BINARY" << std::endl;
out << "DATASET POLYDATA" << std::endl;
......
......@@ -22,32 +22,27 @@
//
//---------------------------------------------------------------------------
#ifndef WWRITERVTK_H
#define WWRITERVTK_H
#ifndef WWRITERFIBERVTK_H
#define WWRITERFIBERVTK_H
#include <string>
#include <boost/shared_ptr.hpp>
#include "../WDataSetFibers.h"
#include "WWriter.h"
/**
* Writes a VTK file.
* Writes a FiberVTK file.
*/
class WWriterVTK
class WWriterFiberVTK : public WWriter
{
public:
/**
* Creates a writer object for VTK file writing.
*
* \param fname absolute file name
* Creates a writer object for FiberVTK file writing. On parameter documentation
* take a look into the WWriter class.
*/
explicit WWriterVTK( std::string fname, bool overwrite = false );
/**
* Reset the destination (file) where the writing should take place.
*/
void setFileName( std::string fname );
WWriterFiberVTK( std::string fname, bool overwrite = false );
/**
* Writes a WDataSetFibers down to the previousely given file
......@@ -55,10 +50,7 @@ public:
void writeFibs( boost::shared_ptr< const WDataSetFibers > fiberDS ) const;
protected:
private:
std::string m_fname; //!< Absolute path of the file to write to
bool m_overwrite; //!< flag indicating if the file may be overwritten (true) or not (false)
};
#endif // WWRITERVTK_H
#endif // WWRITERFIBERVTK_H
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#include <cassert>
#include <fstream>
#include <string>
#include <vector>
#include <boost/shared_ptr.hpp>
#include "WWriterLookUpTableVTK.h"
#include "WIOTools.hpp"
#include "../exceptions/WDHIOFailure.h"
WWriterLookUpTableVTK::WWriterLookUpTableVTK( std::string fname, bool overwrite )
: WWriter( fname, overwrite )
{
}
void WWriterLookUpTableVTK::writeTable( const std::vector< double > &table ) const
{
using std::fstream;
fstream out( m_fname.c_str(), fstream::out | fstream::in | fstream::trunc );
if( !out || out.bad() )
{
throw WDHIOFailure( "Invalid file, or permission: " + m_fname );
}
out << "# vtk DataFile Version 3.0" << std::endl;
out << "DXtLookUpTable from OpenWalnut" << std::endl;
out << "BINARY" << std::endl;
out << "FIELD DXtLookUpTable 1" << std::endl;
unsigned int numDistances = table.size();
out << "DISTANCES " << numDistances << " 1 float" << std::endl;
float *data = new float[numDistances];
for( size_t i = 0; i < numDistances; ++i )
{
data[i] = static_cast< float >( table[i] );
}
wiotools::switchByteOrderOfArray< float >( data, numDistances );
out.write( reinterpret_cast< char* >( data ), sizeof( float ) * numDistances );
out << std::endl;
out.close();
}
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#ifndef WWRITERLOOKUPTABLEVTK_H
#define WWRITERLOOKUPTABLEVTK_H
#include <string>
#include <vector>
#include <boost/shared_ptr.hpp>
#include "WWriter.h"
/**
* Can write a look up table to a file in VTK format.
*/
class WWriterLookUpTableVTK : public WWriter
{
public:
/**
* Creates a writer object for FiberVTK file writing. On parameter documentation
* take a look into the WWriter class.
*/
WWriterLookUpTableVTK( std::string fname, bool overwrite = false );
/**
* Actually perform writing to file.
*
* \param table The data in that table will be saved
* \param dim the dimensionality of the table
*/
void writeTable( const std::vector< double > &table ) const;
protected:
private:
};
#endif // WWRITERLOOKUPTABLEVTK_H
......@@ -24,6 +24,7 @@
#include <cassert>
#include <algorithm>
#include <vector>
#include "WDXtLookUpTable.h"
......@@ -42,3 +43,18 @@ double& WDXtLookUpTable::operator()( size_t i, size_t j )
}
return _data.at( i * _dim + j - ( i + 1 ) * ( i + 2 ) / 2 );
}
size_t WDXtLookUpTable::size() const
{
return _data.size();
}
size_t WDXtLookUpTable::dim() const
{
return _dim;
}
const std::vector< double >& WDXtLookUpTable::getData() const
{
return _data;
}
......@@ -52,9 +52,25 @@ public:
*/
double& operator()( size_t i, size_t j );
/**
* \return the number of distances inside this table.
*/
size_t size() const;
/**
* \return the dimensionality of this table
*/
size_t dim() const;
/**
* \return readonly access to the internal data
*/
const std::vector< double >& getData() const;
private:
/**
* Internal data structure to store the elements.
* Internal data structure to store the elements. The order is line major
* meaning first element (0,0)...(0,n-1),(1,0)...(1, n-1)...
*/
std::vector< double > _data;
......
......@@ -42,6 +42,7 @@
#include "../../dataHandler/WDataHandler.h"
#include "../../dataHandler/WSubject.h"
#include "../../dataHandler/WDataSetFibers.h"
#include "../../dataHandler/io/WWriterLookUpTableVTK.h"
#include "../../kernel/WKernel.h"
#include "../../utils/WColorUtils.h"
......@@ -195,6 +196,8 @@ void WMFiberClustering::cluster()
std::cout << "Using " << m_clusters.size() << " clusters.";
m_lastFibsSize = m_fibs->size();
WWriterLookUpTableVTK w( "/tmp/pansen.dist", true );
w.writeTable( m_dLtTable->getData() );
}
osg::ref_ptr< osg::Geode > WMFiberClustering::genFiberGeode( const WFiberCluster &cluster, const WColor color ) const
......
......@@ -39,7 +39,7 @@
#include "../../dataHandler/WDataHandler.h"
#include "../../dataHandler/WSubject.h"
#include "../../dataHandler/WDataSetFibers.h"
#include "../../dataHandler/io/WWriterVTK.h"
#include "../../dataHandler/io/WWriterFiberVTK.h"
#include "../../kernel/WKernel.h"
#include "../../utils/WColorUtils.h"
......@@ -143,10 +143,8 @@ void WMFiberCulling::cullOutFibers( boost::shared_ptr< WDataSetFibers > fibers )
std::cout << "Erasing done." << std::endl;
std::cout << "Culled out " << numFibers - fibers->size() << " fibers" << std::endl;
std::cout << "There are " << fibers->size() << " fibers left." << std::endl;
if( true )
{
WWriterVTK w( "/tmp/pansen", true );
w.writeFibs( fibers );
// std::cout << "Saved fibers left from culling to " << << " done." << std::endl;
}
// TODO(math): make saving parameter dependent, and apply the desired path for saving
WWriterFiberVTK w( "/tmp/pansen.fib", true );
w.writeFibs( fibers );
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment