Commit 9936f96a authored by Mathias Goldau's avatar Mathias Goldau

[ADD #157] Now saving of culled fibers is possible.

parent ccd0ca9a
......@@ -3,9 +3,9 @@ ADD_SUBDIRECTORY( exceptions )
FILE( GLOB DATAHANDLER_SRC "*.[c,h]pp" )
FILE( GLOB DATAHANDLER_EXCEPTIONS_SRC "exceptions/*.[c,h]pp" )
FILE( GLOB DATAHANDLER_LOADERS_SRC "io/WLoader*.[c,h]pp" )
FILE( GLOB DATAHANDLER_IO_SRC "io/*.[c,h]pp" ) # former WLoader
ADD_LIBRARY( dataHandler SHARED ${DATAHANDLER_SRC} ${DATAHANDLER_EXCEPTIONS_SRC} ${DATAHANDLER_LOADERS_SRC} )
ADD_LIBRARY( dataHandler SHARED ${DATAHANDLER_SRC} ${DATAHANDLER_EXCEPTIONS_SRC} ${DATAHANDLER_IO_SRC} )
TARGET_LINK_LIBRARIES( dataHandler common math ${Boost_LIBRARIES} niftiio biosig)
# Unit tests
......
......@@ -24,18 +24,16 @@
#include <string>
#include <boost/filesystem.hpp>
#include "WLoader.h"
#include "exceptions/WDHIOFailure.h"
#include "io/WIOTools.hpp"
WLoader::WLoader( std::string fileName, boost::shared_ptr< WDataHandler > dataHandler ) throw( WDHIOFailure )
: m_fileName( fileName ),
m_dataHandler( dataHandler )
{
boost::filesystem::path p( m_fileName );
if( !boost::filesystem::exists( p ) )
if( !wiotools::fileExists( m_fileName ) )
{
throw WDHIOFailure( "file '" + m_fileName + "' doesn't exists." );
}
......
ADD_SUBDIRECTORY( nifti )
ADD_SUBDIRECTORY( biosig )
FILE( GLOB DATAHANDLER_LOADERS_SRC "WLoader*.[c,h]pp" )
FILE( GLOB DATAHANDLER_IO_SRC "*.[c,h]pp" )
# Unit tests
IF( CXXTEST_FOUND )
CXXTEST_ADD_TESTS_FROM_LIST( "${DATAHANDLER_LOADERS_SRC}" "dataHandler;niftiio;biosig" )
CXXTEST_ADD_TESTS_FROM_LIST( "${DATAHANDLER_IO_SRC}" "dataHandler;niftiio;biosig" )
ENDIF( CXXTEST_FOUND )
......@@ -90,8 +90,15 @@ namespace wiotools
*/
inline std::string getSuffix( std::string name )
{
boost::filesystem::path p( name );
return p.extension();
return boost::filesystem::path( name ).extension();
}
/**
* Checks if a given path already exists or not
*/
inline bool fileExists( std::string path )
{
return boost::filesystem::exists( boost::filesystem::path( path ) );
}
} // end of namespace
#endif // WIOTOOLS_HPP
//---------------------------------------------------------------------------
//
// 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 <boost/shared_ptr.hpp>
#include "WWriterVTK.h"
#include "WIOTools.hpp"
#include "../WDataSetFibers.h"
#include "../exceptions/WDHIOFailure.h"
WWriterVTK::WWriterVTK( std::string fname, bool overwrite )
: m_overwrite( 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
{
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 << "Fibers from OpenWalnut" << std::endl;
out << "BINARY" << std::endl;
out << "DATASET POLYDATA" << std::endl;
unsigned int numPoints = 0;
unsigned int numLines = fiberDS->size();
for( size_t i = 0; i < fiberDS->size(); ++i )
{
numPoints += (*fiberDS)[i].size();
}
out << "POINTS " << numPoints << " float" << std::endl;
unsigned int *rawLineData = new unsigned int[numPoints + numLines];
float *rawPointData = new float[numPoints * 3];
unsigned int pntPosOffset = 0;
unsigned int lnsPosOffset = 0;
for( size_t i = 0; i < fiberDS->size(); ++i )
{
const wmath::WFiber &fib = (*fiberDS)[i];
rawLineData[lnsPosOffset++] = static_cast< unsigned int >( fib.size() );
for( size_t j = 0; j < fib.size(); ++j )
{
const wmath::WPosition &point = fib[j];
assert( pntPosOffset % 3 == 0 && "(pOff % 3) was not equal to 0" );
assert( pntPosOffset / 3 < numPoints );
rawLineData[lnsPosOffset++] = static_cast< unsigned int >( pntPosOffset / 3 );
rawPointData[pntPosOffset++] = static_cast< float >( point[0] );
rawPointData[pntPosOffset++] = static_cast< float >( point[1] );
rawPointData[pntPosOffset++] = static_cast< float >( point[2] );
assert( pntPosOffset < ( ( numPoints * 3 ) + 1 ) && "pOff < #pts" );
}
}
wiotools::switchByteOrderOfArray< float >( rawPointData, numPoints * 3 );
wiotools::switchByteOrderOfArray< unsigned int >( rawLineData, numLines + numPoints );
out.write( reinterpret_cast< char* >( rawPointData ), sizeof( float ) * numPoints * 3 );
out << std::endl;
out << "LINES " << numLines << " " << numPoints + numLines << std::endl;
out.write( reinterpret_cast< char* >( rawLineData ), sizeof( unsigned int ) * ( numPoints + numLines ) );
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 WWRITERVTK_H
#define WWRITERVTK_H
#include <string>
#include <boost/shared_ptr.hpp>
#include "../WDataSetFibers.h"
/**
* Writes a VTK file.
*/
class WWriterVTK
{
public:
/**
* Creates a writer object for VTK file writing.
*
* \param fname absolute file name
*/
explicit WWriterVTK( std::string fname, bool overwrite = false );
/**
* Reset the destination (file) where the writing should take place.
*/
void setFileName( std::string fname );
/**
* Writes a WDataSetFibers down to the previousely given file
*/
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
......@@ -132,8 +132,8 @@ void WKernel::threadMain()
m_graphicsEngine->run();
// default modules
m_moduleContainer->add( m_moduleFactory->create( m_moduleFactory->getPrototypeByName( "Navigation Slice Module" ) ) , true );
m_moduleContainer->add( m_moduleFactory->create( m_moduleFactory->getPrototypeByName( "Coordinate System Module" ) ) , true );
// m_moduleContainer->add( m_moduleFactory->create( m_moduleFactory->getPrototypeByName( "Navigation Slice Module" ) ) , true );
// m_moduleContainer->add( m_moduleFactory->create( m_moduleFactory->getPrototypeByName( "Coordinate System Module" ) ) , true );
// actually there is nothing more to do here
waitForStop();
......@@ -223,11 +223,27 @@ const WBoolFlag& WKernel::isFinishRequested() const
void WKernel::doLoadDataSets( std::vector< std::string > fileNames )
{
// add a new data module for each file to load
for ( std::vector< std::string >::iterator iter = fileNames.begin(); iter != fileNames.end(); ++iter )
using boost::shared_ptr;
for( std::vector< std::string >::iterator iter = fileNames.begin(); iter != fileNames.end(); ++iter )
{
boost::shared_ptr< WModule > mod = m_moduleFactory->create( m_moduleFactory->getPrototypeByName( "Data Module" ) );
shared_ptr< WModule > mod = m_moduleFactory->create( m_moduleFactory->getPrototypeByName( "Data Module" ) );
mod->getProperties()->setValue( "filename" , ( *iter ) );
m_moduleContainer->add( mod );
//// fast hack to create always a fiber display module along a data module containing fibers
//shared_ptr< WMData > m1 = boost::shared_dynamic_cast< WMData >( mod );
//assert( m1 );
//sleep( 10 );
//assert( m1->getDataSet() );
//if( wiotools::getSuffix( m1->getDataSet()->getFileName() ) == ".fib" )
//{
// shared_ptr< WModule > m2 = m_moduleFactory->create( m_moduleFactory->getPrototypeByName( "Fiber Display Module" ) );
// assert( m1->getOutputConnectors().size() == 1 );
// assert( m2->getInputConnectors().size() == 1 );
//}
boost::shared_ptr< WModule > mod2 = m_moduleFactory->create( m_moduleFactory->getPrototypeByName( "Fiber Display Module" ) );
m_moduleContainer->add( mod2 );
}
}
......
......@@ -39,6 +39,7 @@
#include "../../dataHandler/WDataHandler.h"
#include "../../dataHandler/WSubject.h"
#include "../../dataHandler/WDataSetFibers.h"
#include "../../dataHandler/io/WWriterVTK.h"
#include "../../kernel/WKernel.h"
#include "../../utils/WColorUtils.h"
......@@ -79,17 +80,6 @@ void WMFiberCulling::moduleMain()
boost::shared_ptr< WDataSetFibers > fiberDS;
assert( fiberDS = boost::shared_dynamic_cast< WDataSetFibers >( dataHandler->getSubject( 0 )->getDataSet( 0 ) ) );
// osg::ref_ptr< osg::Group > group = osg::ref_ptr< osg::Group >( new osg::Group );
//
// for( size_t i = 0; i < fibers.size(); ++i )
// {
// group->addChild( genFiberGeode( fibers[i] ).get() );
// }
// group->getOrCreateStateSet()->setMode( GL_LIGHTING, osg::StateAttribute::OFF );
//
// WKernel::getRunningKernel()->getGraphicsEngine()->getScene()->addChild( group.get() );
//
cullOutFibers( fiberDS );
std::cout << "done." << std::endl;
......@@ -148,14 +138,15 @@ void WMFiberCulling::cullOutFibers( boost::shared_ptr< WDataSetFibers > fibers )
std::cout << ( ++st ).progress() << " " << st.stringBar() << std::flush;
}
std::cout << std::endl;
// if( m_saveCulledCurves ) {
// saveFib( m_savePath, fibers, unusedFibers );
// std::cout << "Saving culled fibers to " << m_savePath << " done." << std::endl;
// }
// create new DataSet where unused fibers are not contained
fibers->erase( unusedFibers );
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;
}
}
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