Commit 6eaf5efd authored by Mathias Goldau's avatar Mathias Goldau
Browse files

[ADD #157] Fiber Clustering may now load precomputated distance matrix from...

[ADD #157] Fiber Clustering may now load precomputated distance matrix from fixed hard coded path => really experimental!!! Do not use this yet!!!
parent 28d95bf3
//---------------------------------------------------------------------------
//
// 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 <stdexcept>
#include <string>
#include "WDHNoSuchFile.h"
WDHNoSuchFile::WDHNoSuchFile( const std::string &fname )
: WDHException( "file: '" + fname + "' not found" )
{
}
WDHNoSuchFile::~WDHNoSuchFile() throw()
{
// clean up
}
//---------------------------------------------------------------------------
//
// 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 WDHNOSUCHFILE_H
#define WDHNOSUCHFILE_H
#include <stdexcept>
#include <string>
#include "WDHException.h"
/**
* File not found exception.
*/
class WDHNoSuchFile : public WDHException
{
public:
/**
* Default constructor.
* \param msg the exception message.
*/
explicit WDHNoSuchFile( const std::string &fname = "unknown file not found" );
/**
* Destructor
*/
virtual ~WDHNoSuchFile() throw();
protected:
private:
};
#endif // WDHNOSUCHFILE_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 <string>
#include "WReader.h"
#include "WIOTools.hpp"
#include "../exceptions/WDHNoSuchFile.h"
WReader::WReader( std::string fname )
{
setFileName( fname ); // not in constructor list since fileExcsits check
}
void WReader::setFileName( std::string fname ) throw( WDHNoSuchFile )
{
m_fname = fname;
if( !wiotools::fileExists( m_fname ) )
{
throw WDHNoSuchFile( m_fname );
}
}
//---------------------------------------------------------------------------
//
// 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 WREADER_H
#define WREADER_H
#include <string>
#include "../exceptions/WDHNoSuchFile.h"
/**
* Read some data from a given file. This base class is just for file
* management (file exists, etc.) Subclasses may use those mechanisms and
* specify their file format which is not the purpose of this base class.
*/
class WReader
{
public:
/**
* Creates a read object for file reading.
*
* \param fname path to file which should be loaded
*/
explicit WReader( std::string fname );
/**
* Reset the file name and checks if it exists.
*
* \throws WDHNoSuchFile
*/
void setFileName( std::string fname ) throw( WDHNoSuchFile );
protected:
std::string m_fname; //!< Absolute path of the file to read from
private:
};
#endif // WREADER_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 <fstream>
#include <stdexcept>
#include <string>
#include <vector>
#include <boost/lexical_cast.hpp>
#include <boost/shared_ptr.hpp>
#include "WIOTools.hpp"
#include "WReader.h"
#include "WReaderLookUpTableVTK.h"
#include "../exceptions/WDHException.h"
#include "../exceptions/WDHIOFailure.h"
#include "../../common/WStringUtils.hpp"
WReaderLookUpTableVTK::WReaderLookUpTableVTK( std::string fname )
: WReader( fname )
{
}
void WReaderLookUpTableVTK::readTable( boost::shared_ptr< std::vector< double > > table ) const
{
assert( table->size() == 0 && "error, since the vector will be filled IN HERE" );
// code mainly taken from WLoaderFibers.cpp, and adjusted since I don't
// know how to code this DRY. Any suggestions?
std::ifstream ifs;
ifs.open( m_fname.c_str(), std::ifstream::in | std::ifstream::binary );
assert( ifs && !ifs.bad() );
std::vector< std::string > header;
std::string line;
try
{
for( int i = 0; i < 4; ++i ) // strip first four lines
{
std::getline( ifs, line );
if( !ifs.good() )
{
throw WDHException( "Unexpected end of file: " + m_fname );
}
header.push_back( line );
}
}
catch( const std::ios_base::failure &e )
{
throw WDHIOFailure( "Reading first 4 lines of '" + m_fname + "': " + e.what() );
}
assert( header[0] == "# vtk DataFile Version 3.0" );
assert( header[1] == "DXtLookUpTable from OpenWalnut" );
assert( header[2] == "BINARY" );
assert( header[3] == "FIELD DXtLookUpTable 1" );
try
{
std::getline( ifs, line ); // something like this: "DISTANCES 15879430 1 float" expected
}
catch( const std::ios_base::failure &e )
{
throw WDHIOFailure( "Error reading DISTANCES field '" + m_fname + "': " + e.what() );
}
namespace su = string_utils;
size_t numDistances = 0;
std::vector< std::string > tokens = su::tokenize( line );
if( tokens.size() != 4 || su::toLower( tokens.at( 3 ) ) != "float" )
{
throw WDHException( "Invalid DISTANCES declaration: " + line );
}
try
{
numDistances = boost::lexical_cast< size_t >( tokens.at( 1 ) );
}
catch( const boost::bad_lexical_cast &e )
{
throw WDHException( "Invalid number of distances: " + tokens.at( 1 ) );
}
float *data = new float[ numDistances ];
try
{
ifs.read( reinterpret_cast< char* >( data ), sizeof( float ) * numDistances );
}
catch( const std::ios_base::failure &e )
{
throw WDHIOFailure( "Error reading distances in VTK DISTANCES field '" + m_fname + "': " + e.what() );
}
// all 4 bytes of each float are in wrong order we need to reorder them
wiotools::switchByteOrderOfArray( data, numDistances );
for( size_t i = 0; i < numDistances; ++i )
{
table->push_back( static_cast< double >( data[ i ] ) );
}
delete[] data;
}
//---------------------------------------------------------------------------
//
// 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 WREADERLOOKUPTABLEVTK_H
#define WREADERLOOKUPTABLEVTK_H
#include <string>
#include <vector>
#include <boost/shared_ptr.hpp>
#include "WReader.h"
/**
* Can read a look up table from a file in VTK format.
*/
class WReaderLookUpTableVTK : public WReader
{
public:
/**
* Creates a reader object for look up tables. On parameter documention
* take a look into the WReader base class.
*/
explicit WReaderLookUpTableVTK( std::string fname );
/**
* Perform reading from the file.
*/
void readTable( boost::shared_ptr< std::vector< double > > table ) const;
protected:
private:
};
#endif // WREADERLOOKUPTABLEVTK_H
......@@ -28,11 +28,9 @@
#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 );
* Write some data to the given file. This base class is just for file
* management (file exists, etc.) Subclasses may use those mechanisms and
* specify their file format which is not the purpose of this base class.
*/
class WWriter
{
......@@ -47,7 +45,8 @@ public:
WWriter( std::string fname, bool overwrite = false );
/**
* Reset the destination (file) where the writing should take place.
* Reset file name and checks if the file already exists in case of
* non overwriting is specified.
*/
void setFileName( std::string fname );
......
......@@ -38,7 +38,7 @@ WWriterLookUpTableVTK::WWriterLookUpTableVTK( std::string fname, bool overwrite
{
}
void WWriterLookUpTableVTK::writeTable( const std::vector< double > &table ) const
void WWriterLookUpTableVTK::writeTable( const std::vector< double > &table, size_t dim ) const
{
using std::fstream;
fstream out( m_fname.c_str(), fstream::out | fstream::in | fstream::trunc );
......@@ -51,14 +51,15 @@ void WWriterLookUpTableVTK::writeTable( const std::vector< double > &table ) con
out << "BINARY" << std::endl;
out << "FIELD DXtLookUpTable 1" << std::endl;
unsigned int numDistances = table.size();
unsigned int numDistances = table.size() + 1;
out << "DISTANCES " << numDistances << " 1 float" << std::endl;
float *data = new float[numDistances];
for( size_t i = 0; i < numDistances; ++i )
for( size_t i = 0; i < table.size() ; ++i )
{
data[i] = static_cast< float >( table[i] );
}
data[ numDistances - 1 ] = static_cast< float >( dim );
wiotools::switchByteOrderOfArray< float >( data, numDistances );
out.write( reinterpret_cast< char* >( data ), sizeof( float ) * numDistances );
......
......@@ -50,7 +50,7 @@ public:
* \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;
void writeTable( const std::vector< double > &table, size_t dim ) const;
protected:
private:
......
......@@ -58,3 +58,8 @@ const std::vector< double >& WDXtLookUpTable::getData() const
{
return _data;
}
void WDXtLookUpTable::setData( const std::vector< double > &data )
{
_data = std::vector< double >( data ); // copy content
}
......@@ -67,6 +67,11 @@ public:
*/
const std::vector< double >& getData() const;
/**
* Resets the internal data (e.g. loaded from file).
*/
void setData( const std::vector< double > &data );
private:
/**
* Internal data structure to store the elements. The order is line major
......
......@@ -40,9 +40,11 @@
#include "../../common/WStatusReport.h"
#include "../../common/WStringUtils.hpp"
#include "../../dataHandler/WDataHandler.h"
#include "../../dataHandler/WSubject.h"
#include "../../dataHandler/WDataSetFibers.h"
#include "../../dataHandler/WSubject.h"
#include "../../dataHandler/io/WIOTools.hpp"
#include "../../dataHandler/io/WWriterLookUpTableVTK.h"
#include "../../dataHandler/io/WReaderLookUpTableVTK.h"
#include "../../kernel/WKernel.h"
#include "../../utils/WColorUtils.h"
......@@ -99,6 +101,33 @@ void WMFiberClustering::moduleMain()
void WMFiberClustering::checkDLtLookUpTable()
{
// TODO(math): replace this hard coded path when properties are available
if( wiotools::fileExists( "/tmp/pansen.dist" ) )
{
try
{
// TODO(math): replace this hard coded path when properties are available
WReaderLookUpTableVTK r( "/tmp/pansen.dist" );
using boost::shared_ptr;
using std::vector;
shared_ptr< vector< double > > data = shared_ptr< vector < double > >( new vector< double >() );
r.readTable( data );
m_dLtTable.reset( new WDXtLookUpTable( static_cast< size_t >( data->back() ) ) );
m_lastFibsSize = static_cast< size_t >( data->back() );
// remove the dimension from data array since it's not representing any distance
data->pop_back();
m_dLtTable->setData( *data );
// check if elements match number of fibers and reset m_lastFibsSize
m_dLtTableExists = true;
}
catch( WDHException e )
{
std::cout << e.what() << std::endl;
}
}
if( m_dLtTableExists )
{
if( m_fibs->size() != m_lastFibsSize )
......@@ -197,7 +226,7 @@ void WMFiberClustering::cluster()
m_lastFibsSize = m_fibs->size();
WWriterLookUpTableVTK w( "/tmp/pansen.dist", true );
w.writeTable( m_dLtTable->getData() );
w.writeTable( m_dLtTable->getData(), m_lastFibsSize );
}
osg::ref_ptr< osg::Geode > WMFiberClustering::genFiberGeode( const WFiberCluster &cluster, const WColor color ) const
......
......@@ -224,7 +224,7 @@ class TestCode(HeaderCode):
class ExceptionHeader(HeaderCode):
def __init__(self, name, structors=None):
HeaderCode.__init__(self, name + "Exception",
HeaderCode.__init__(self, name,
structors=structors,
subclass="public std::logic_error",
includes=["<stdexcept>", "<string>\n"]
......@@ -237,7 +237,7 @@ class ExceptionHeader(HeaderCode):
class ExceptionImplementation(ImplementationCode):
def __init__(self, name, structors=None):
ImplementationCode.__init__(self, name + "Exception",
ImplementationCode.__init__(self, name,
structors=structors,
includes=["<stdexcept>", "<string>\n"]
)
......@@ -249,7 +249,7 @@ class ExceptionImplementation(ImplementationCode):
class ExceptionTest(TestCode):
def __init__(self, name):
TestCode.__init__(self, name + "Exception")
TestCode.__init__(self, name)
def writeToFile( fname, payload ):
......
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