Commit 291db3f0 authored by Mathias Goldau's avatar Mathias Goldau
Browse files

[CHANGE] Now the fiber resampling is adopted to the strategy pattern, as I...

[CHANGE] Now the fiber resampling is adopted to the strategy pattern, as I need other resampling methods. With this it is possible to move the resampling functionality out of core (it is currently in WLine) into this module. Also this module makes now the resampling by segment length accessible, which was not made previously due to unkown reason.
parent 858dc3bb
......@@ -26,20 +26,24 @@
#include <string>
#include <vector>
#include "core/common/math/WMath.h"
#include "core/common/WPropertyHelper.h"
#include "core/dataHandler/WDataHandler.h"
#include "core/dataHandler/WDataSetFiberVector.h"
#include "core/kernel/WKernel.h"
#include <core/common/WStrategyHelper.h>
#include <core/common/math/WMath.h>
#include <core/common/WPropertyHelper.h>
#include <core/dataHandler/WDataHandler.h>
#include <core/kernel/WKernel.h>
#include "WMFiberResampling.h"
#include "WMFiberResampling.xpm"
#include "WSimpleResampler.h"
#include "WResampleByNumPoints.h"
#include "WResampleBySegLength.h"
W_LOADABLE_MODULE( WMFiberResampling )
WMFiberResampling::WMFiberResampling():
WModule()
WModule(),
m_strategy( "Fiber Resampling Algorithms", "Select one algorithm to resample fibers", NULL,
"FiberResampling", "A list of all known resampling strategies" )
{
m_strategy.addStrategy( WResampleByNumPoints::SPtr( new WResampleByNumPoints() ) );
m_strategy.addStrategy( WResampleBySegLength::SPtr( new WResampleBySegLength() ) );
}
WMFiberResampling::~WMFiberResampling()
......@@ -51,11 +55,6 @@ boost::shared_ptr< WModule > WMFiberResampling::factory() const
return boost::shared_ptr< WModule >( new WMFiberResampling() );
}
const char** WMFiberResampling::getXPMIcon() const
{
return WMFiberResampling_xpm;
}
const std::string WMFiberResampling::getName() const
{
return "Fiber Re-Sampling";
......@@ -68,29 +67,16 @@ const std::string WMFiberResampling::getDescription() const
void WMFiberResampling::connectors()
{
// The input fiber dataset
m_fiberInput = boost::shared_ptr< WModuleInputData < WDataSetFibers > >(
new WModuleInputData< WDataSetFibers >( shared_from_this(), "fibers", "The fiber dataset to re-sample" )
);
// As properties, every connector needs to be added to the list of connectors.
addConnector( m_fiberInput );
// the selected fibers go to this output
m_fiberOutput = boost::shared_ptr< WModuleOutputData < WDataSetFibers > >(
new WModuleOutputData< WDataSetFibers >( shared_from_this(),
"out", "The fiber dataset re-sampled." )
);
addConnector( m_fiberOutput );
m_fiberIC = WModuleInputData< WDataSetFibers >::createAndAdd( shared_from_this(), "fibers", "The line data to resample." );
m_fiberOC = WModuleOutputData< WDataSetFibers >::createAndAdd( shared_from_this(), "resampledFibers", "The resampled fiber dataset." );
WModule::connectors();
}
void WMFiberResampling::properties()
{
m_newSamples = m_properties->addProperty( "#Sample Points", "The is the new number of which the tracts are resampled to", 20 );
m_newSamples->setMin( 2 );
m_properties->addProperty( m_strategy.getProperties() );
WModule::properties();
}
......@@ -98,8 +84,7 @@ void WMFiberResampling::moduleMain()
{
// get notified about data changes
m_moduleState.setResetable( true, true );
m_moduleState.add( m_fiberInput->getDataChangedCondition() );
m_moduleState.add( m_newSamples->getCondition() );
m_moduleState.add( m_fiberIC->getDataChangedCondition() );
ready();
......@@ -113,47 +98,19 @@ void WMFiberResampling::moduleMain()
break;
}
// To query whether an input was updated, simply ask the input:
bool dataUpdated = m_fiberInput->handledUpdate();
boost::shared_ptr< WDataSetFibers > dataSet = m_fiberInput->getData();
bool dataValid = ( dataSet );
if( m_newSamples->get() <= 1 )
{
warnLog() << "Resampling lines to have only 1 or less points is not supported, skipping this action";
}
if( !dataValid || ( m_newSamples->get() <= 1 ) ) // resampling with just one vertex will lead to points not to lines
WDataSetFibers::SPtr fibers = m_fiberIC->getData();
if( !fibers )
{
continue;
}
WAssert( m_newSamples->changed() || ( dataValid && dataUpdated ), "Bug: Think! I though this could never happen!" );
m_fiberOutput->updateData( resample( dataSet ) );
debugLog() << "Done";
WProgress::SPtr progress( new WProgress( "Resampling Fibers", fibers->size() ) );
m_progress->addSubProgress( progress );
debugLog() << "Start resampling";
m_fiberOC->updateData( m_strategy()->operator()( progress, m_shutdownFlag, fibers ) );
progress->finish();
m_progress->removeSubProgress( progress );
debugLog() << "Finished resampling";
}
}
boost::shared_ptr< WDataSetFibers > WMFiberResampling::resample( boost::shared_ptr< const WDataSetFibers > dataSet ) const
{
debugLog() << "Start resampling: " << dataSet->getLineStartIndexes()->size() << " tracts";
boost::shared_ptr< WProgress > progress1( new WProgress( "Resampling tracts.", dataSet->getLineStartIndexes()->size() ) );
m_progress->addSubProgress( progress1 );
boost::shared_ptr< WDataSetFiberVector > newDS( new WDataSetFiberVector() );
for( size_t fidx = 0; fidx < dataSet->getLineStartIndexes()->size() ; ++fidx )
{
WFiber tract( ( *dataSet )[ fidx ] );
tract.resampleByNumberOfPoints( m_newSamples->get( true ) );
newDS->push_back( tract );
++*progress1;
}
progress1->finish();
m_progress->removeSubProgress( progress1 );
return newDS->toWDataSetFibers();
}
......@@ -27,10 +27,12 @@
#include <string>
#include "core/dataHandler/WDataSetScalar.h"
#include "core/kernel/WModule.h"
#include "core/kernel/WModuleInputData.h"
#include "core/kernel/WModuleOutputData.h"
#include <core/dataHandler/WDataSetFibers.h>
#include <core/kernel/WModule.h>
#include <core/kernel/WModuleInputData.h>
#include <core/kernel/WModuleOutputData.h>
#include "WResampling_I.h"
/**
* This modules takes a fiber dataset and samples it in regular steps. This is useful when importing fiber datasets from other tools sampled at
......@@ -71,12 +73,6 @@ public:
*/
virtual boost::shared_ptr< WModule > factory() const;
/**
* Get the icon for this module in XPM format.
* \return the icon.
*/
virtual const char** getXPMIcon() const;
protected:
/**
* Entry point after loading the module. Runs in separate thread.
......@@ -93,30 +89,21 @@ protected:
*/
virtual void properties();
/**
* Resamples all tracts with a resampler instance for example the WSimpleResampler.
*
* \param dataSet Which dataset should be resampled
*
* \return The resampled dataset.
*/
boost::shared_ptr< WDataSetFibers > resample( boost::shared_ptr< const WDataSetFibers > dataSet ) const;
private:
/**
* The fiber dataset which is going to be filtered.
*/
boost::shared_ptr< WModuleInputData< WDataSetFibers > > m_fiberInput;
boost::shared_ptr< WModuleInputData< WDataSetFibers > > m_fiberIC;
/**
* The output connector used to provide the calculated data to other modules.
*/
boost::shared_ptr< WModuleOutputData< WDataSetFibers > > m_fiberOutput;
boost::shared_ptr< WModuleOutputData< WDataSetFibers > > m_fiberOC;
/**
* Number of new sample points all tracts are resampled to.
* Strategies for coloring fibers.
*/
WPropInt m_newSamples;
WStrategyHelper< WObjectNDIP< WResampling_I > > m_strategy;
};
#endif // WMFIBERRESAMPLING_H
......
......@@ -22,17 +22,24 @@
//
//---------------------------------------------------------------------------
#include <iostream>
#include <vector>
#include <cmath>
#include "core/common/WAssert.h"
#include "core/common/datastructures/WFiber.h"
#include <osg/Vec3>
#include "WSimpleResampler.h"
#include <core/common/math/WMath.h>
WFiber WSimpleResampler::resample( const WFiber& tract, size_t pts ) const
#include "WResampleByNumPoints.h"
WResampleByNumPoints::WResampleByNumPoints()
: WObjectNDIP< WResampling_I >( "#Points Resampling", "Each fiber has a an ensured number of points after resampling" )
{
m_numPoints = m_properties->addProperty( "#Points", "Number of points each fiber should have afterwards", 20 );
m_numPoints->setMin( 2 );
}
WFiber WResampleByNumPoints::resample( WFiber fib ) const
{
WFiber result( tract );
result.resampleByNumberOfPoints( pts );
return result;
fib.resampleByNumberOfPoints( m_numPoints->get( true ) );
return fib;
}
//---------------------------------------------------------------------------
//
// 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 WRESAMPLEBYNUMPOINTS_H
#define WRESAMPLEBYNUMPOINTS_H
#include <core/common/datastructures/WFiber.h>
#include <core/common/WObjectNDIP.h>
#include "WResampling_I.h"
/**
* Resamples fibers to have a given number of points afterwards.
*/
class WResampleByNumPoints : public WObjectNDIP< WResampling_I >
{
public:
/**
* Constructor.
*/
WResampleByNumPoints();
/**
* The given fiber is resampled so it contains the number of points given by m_numPoints and a copy is returned.
*
* \param fib Fiber to resample.
*
* \return Copy of the resampled fiber.
*/
virtual WFiber resample( WFiber fib ) const;
protected:
/**
* Number of new sample points all tracts are resampled to.
*/
WPropInt m_numPoints;
private:
};
#endif // WRESAMPLEBYNUMPOINTS_H
......@@ -22,34 +22,24 @@
//
//---------------------------------------------------------------------------
#ifndef WSIMPLERESAMPLER_H
#define WSIMPLERESAMPLER_H
#include <vector>
#include <cmath>
#include <boost/shared_ptr.hpp>
#include <osg/Vec3>
#include "core/common/datastructures/WFiber.h"
#include <core/common/math/WMath.h>
/**
* Algorithm to resample a single tract into almost equidistant segments.
*/
class WSimpleResampler
{
friend class WSimpleResamplerTest;
public:
/**
* Resamples a tract.
*
* \param tract The tract to resample
* \param pts The number of points the new tract should have
*
* \return The resampled tract
*/
WFiber resample( const WFiber& tract, size_t pts ) const;
#include "WResampleBySegLength.h"
protected:
private:
};
WResampleBySegLength::WResampleBySegLength()
: WObjectNDIP< WResampling_I >( "#Points Resampling", "Each fiber has a an ensured number of points after resampling" )
{
m_segLength = m_properties->addProperty( "#Points", "Number of points each fiber should have afterwards", 1.0 );
m_segLength->setMin( 0.0 );
}
#endif // WSIMPLERESAMPLER_H
WFiber WResampleBySegLength::resample( WFiber fib ) const
{
fib.resampleBySegmentLength( m_segLength->get( true ) );
return fib;
}
//---------------------------------------------------------------------------
//
// 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 WRESAMPLEBYSEGLENGTH_H
#define WRESAMPLEBYSEGLENGTH_H
#include <core/common/datastructures/WFiber.h>
#include <core/common/WObjectNDIP.h>
#include "WResampling_I.h"
/**
* Resamples fibers by segment length.
*/
class WResampleBySegLength : public WObjectNDIP< WResampling_I >
{
public:
/**
* Constructor.
*/
WResampleBySegLength();
/**
* The given fiber is resampled by segment length and a copy is returned.
*
* \param fib Fiber to resample.
*
* \return Copy of the resampled fiber.
*/
virtual WFiber resample( WFiber fib ) const;
protected:
/**
* Number of new sample points all tracts are resampled to.
*/
WPropDouble m_segLength;
private:
};
#endif // WRESAMPLEBYSEGLENGTH_H
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2014 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 <core/dataHandler/WDataSetFiberVector.h>
#include "WResampling_I.h"
WResampling_I::~WResampling_I()
{
}
WDataSetFibers::SPtr WResampling_I::operator()( WProgress::SPtr progress, WBoolFlag const &shutdown, WDataSetFibers::SPtr fibers )
{
wlog::debug( "WResampling_I" ) << "Start resampling: " << fibers->getLineStartIndexes()->size() << " fibers";
WDataSetFiberVector::SPtr newDS( new WDataSetFiberVector() );
for( size_t fidx = 0; fidx < fibers->getLineStartIndexes()->size() && !shutdown; ++fidx )
{
newDS->push_back( resample( (*fibers)[ fidx ] ) );
++*progress;
}
return newDS->toWDataSetFibers();
}
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2014 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 WRESAMPLING_I_H
#define WRESAMPLING_I_H
#include <core/common/WProgress.h>
#include <core/common/WFlag.h>
#include <core/dataHandler/WDataSetFibers.h>
/**
* Interface for Resampling fibers.
*/
class WResampling_I
{
public:
/**
* Resample each fiber within the given fiber dataset according to the given implementation of virtual resample().
*
* \param progress This will indicate progress.
* \param shutdown Possibility to abort in case of shutdown.
* \param fibers The fibers which should be resampled.
*
* \return Colored fibers
*/
virtual WDataSetFibers::SPtr operator()( WProgress::SPtr progress, WBoolFlag const &shutdown, WDataSetFibers::SPtr fibers );
/**
* All overrided methods should resample the fiber in their specific way.
*
* \param fib The fiber to resample.
*
* \return Copy of the resampled fiber.
*/
virtual WFiber resample( WFiber fib ) const = 0;
/**
* Destructor.
*/
virtual ~WResampling_I();
protected:
private:
};
#endif // WRESAMPLING_I_H
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