Commit f3e19236 authored by Alexander Wiebel's avatar Alexander Wiebel
Browse files

[ADD] new module to convert a datasetsingle with any data type in its

valueset into a datsetsingle with an valueset of floats.
parent 42a65e42
......@@ -46,6 +46,7 @@
#include "../modules/navSlices/WMNavSlices.h"
#include "../modules/voxelizer/WMVoxelizer.h"
#include "../modules/writeNIfTI/WMWriteNIfTI.h"
#include "../modules/dataTypeConversion/WMDataTypeConversion.h"
#include "WModuleFactory.h"
#include "exceptions/WPrototypeNotUnique.h"
#include "exceptions/WPrototypeUnknown.h"
......@@ -89,6 +90,7 @@ void WModuleFactory::load()
m_prototypes.insert( boost::shared_ptr< WModule >( new WMVoxelizer() ) );
m_prototypes.insert( boost::shared_ptr< WModule >( new WMDirectVolumeRendering() ) );
m_prototypes.insert( boost::shared_ptr< WModule >( new WMWriteNIfTI() ) );
m_prototypes.insert( boost::shared_ptr< WModule >( new WMDataTypeConversion() ) );
m_prototypes.insert( boost::shared_ptr< WModule >( new WMConnectomeView() ) );
lock.unlock();
......
//---------------------------------------------------------------------------
//
// 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 <stdint.h>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cmath>
#include "../../common/WStringUtils.h"
#include "../../common/WProgress.h"
#include "../../dataHandler/WGridRegular3D.h"
#include "../../kernel/WKernel.h"
#include "../../math/WPosition.h"
#include "../../math/WVector3D.h"
#include "../data/WMData.h"
#include "WMDataTypeConversion.h"
#include "datatypeconversion.xpm"
WMDataTypeConversion::WMDataTypeConversion() :
WModule()
{
// WARNING: initializing connectors inside the constructor will lead to an exception.
// Implement WModule::initializeConnectors instead.
}
WMDataTypeConversion::~WMDataTypeConversion()
{
// cleanup
removeConnectors();
}
boost::shared_ptr<WModule> WMDataTypeConversion::factory() const
{
return boost::shared_ptr<WModule>( new WMDataTypeConversion() );
}
const char** WMDataTypeConversion::getXPMIcon() const
{
return datatypeconversion_xpm;
}
const std::string WMDataTypeConversion::getName() const
{
return "Data Type Conversion";
}
const std::string WMDataTypeConversion::getDescription() const
{
return "This module is intended for development use only. Modules for general use should not depend on it.<br><br> "
"Allows to change the data type of the provided WDataSetSingle to another type. "
"E.g., double to float. At the moment only conversion to float is supported.";
}
void WMDataTypeConversion::moduleMain()
{
// use the m_input "data changed" flag
m_moduleState.add( m_input->getDataChangedCondition() );
// signal ready state
ready();
// loop until the module container requests the module to quit
while( !m_shutdownFlag() )
{
// acquire data from the input connector
m_dataSet = m_input->getData();
if( !m_dataSet )
{
// ok, the output has not yet sent data
// NOTE: see comment at the end of this while loop for m_moduleState
debugLog() << "Waiting for data ...";
m_moduleState.wait();
continue;
}
assert( m_dataSet );
switch( (*m_dataSet).getValueSet()->getDataType() )
{
case W_DT_UNSIGNED_CHAR:
{
boost::shared_ptr<WValueSet<unsigned char> > vals;
vals = boost::shared_dynamic_cast<WValueSet<unsigned char> >( ( *m_dataSet ).getValueSet() );
assert( vals );
convertDataSet( vals );
break;
}
case W_DT_INT16:
{
boost::shared_ptr<WValueSet<int16_t> > vals;
vals = boost::shared_dynamic_cast<WValueSet<int16_t> >( ( *m_dataSet ).getValueSet() );
assert( vals );
convertDataSet( vals );
}
case W_DT_SIGNED_INT:
{
boost::shared_ptr<WValueSet<int32_t> > vals;
vals = boost::shared_dynamic_cast<WValueSet<int32_t> >( ( *m_dataSet ).getValueSet() );
assert( vals );
convertDataSet( vals );
break;
}
case W_DT_FLOAT:
{
boost::shared_ptr< WValueSet< float > > vals;
vals = boost::shared_dynamic_cast< WValueSet< float > >( ( *m_dataSet ).getValueSet() );
assert( vals );
convertDataSet( vals );
break;
}
case W_DT_DOUBLE:
{
boost::shared_ptr< WValueSet< double > > vals;
vals = boost::shared_dynamic_cast< WValueSet< double > >( ( *m_dataSet ).getValueSet() );
assert( vals );
convertDataSet( vals );
break;
}
default:
assert( false && "Unknow data type in Data Type Conversion module" );
}
// this waits for m_moduleState to fire. By default, this is only the m_shutdownFlag condition.
// NOTE: you can add your own conditions to m_moduleState using m_moduleState.add( ... )
m_moduleState.wait();
}
}
void WMDataTypeConversion::connectors()
{
// initialize connectors
m_input = boost::shared_ptr<WModuleInputData<WDataSetSingle> >(
new WModuleInputData<WDataSetSingle> ( shared_from_this(), "in",
"The dataset to filter" ) );
// add it to the list of connectors. Please note, that a connector NOT added via addConnector will not work as expected.
addConnector( m_input );
// initialize connectors
m_output = boost::shared_ptr<WModuleOutputData<WDataSetSingle> >(
new WModuleOutputData<WDataSetSingle> ( shared_from_this(), "out",
"The filtered data set." ) );
// add it to the list of connectors. Please note, that a connector NOT added via addConnector will not work as expected.
addConnector( m_output );
// call WModules initialization
WModule::connectors();
}
void WMDataTypeConversion::properties()
{
}
template< typename T > void WMDataTypeConversion::convertDataSet( boost::shared_ptr< WValueSet< T > > vals )
{
std::vector< float > newVals( vals->size() );
for( size_t i = 0; i < newVals.size(); ++i )
{
newVals[i] = static_cast< float >( vals->getScalar( i ) );
}
boost::shared_ptr< WValueSet< float > > valueSet;
valueSet = boost::shared_ptr< WValueSet< float > >( new WValueSet< float > ( 0, 1, newVals, W_DT_FLOAT ) );
m_dataSet = boost::shared_ptr<WDataSetSingle>( new WDataSetSingle( valueSet, m_dataSet->getGrid() ) );
m_output->updateData( m_dataSet );
}
//---------------------------------------------------------------------------
//
// 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 WMDATATYPECONVERSION_H
#define WMDATATYPECONVERSION_H
#include <map>
#include <string>
#include <vector>
#include <osg/Node>
#include <osg/Geode>
#include <osg/Uniform>
#include "../../kernel/WModule.h"
#include "../../kernel/WModuleInputData.h"
#include "../../math/WVector3D.h"
/**
* Provides a new field with the approximately the same data, but with another data type. E.g., it
* can convert double into float.
* \ingroup modules
*/
class WMDataTypeConversion : public WModule
{
public:
/**
* Standard constructor.
*/
WMDataTypeConversion();
/**
* Destructor.
*/
~WMDataTypeConversion();
/**
* Gives back the name of this module.
* \return the module's name.
*/
virtual const std::string getName() const;
/**
* Gives back a description of this module.
* \return description of module.
*/
virtual const std::string getDescription() const;
/**
* Due to the prototype design pattern used to build modules, this method returns a new instance of this method. NOTE: it
* should never be initialized or modified in some other way. A simple new instance is required.
*
* \return the prototype used to create every module in OpenWalnut.
*/
virtual boost::shared_ptr< WModule > factory() const;
/**
* Get the icon for this module in XPM format.
*/
virtual const char** getXPMIcon() const;
protected:
/**
* Entry point after loading the module. Runs in separate thread.
*/
virtual void moduleMain();
/**
* Initialize the connectors this module is using.
*/
virtual void connectors();
/**
* Initialize the properties for this module.
*/
virtual void properties();
private:
boost::shared_ptr< WModuleInputData< WDataSetSingle > > m_input; //!< Input connector required by this module.
boost::shared_ptr< WModuleOutputData< WDataSetSingle > > m_output; //!< The only output of this filter module.
boost::shared_ptr< WDataSetSingle > m_dataSet; //!< Pointer providing access to the treated data set in the whole module.
/**
* The heart of this module. it does the conversion of the values.
* \param vals valueset of data set.
*/
template< typename T > void convertDataSet( boost::shared_ptr< WValueSet< T > > vals );
};
#endif // WMDATATYPECONVERSION_H
/* XPM */
static const char * datatypeconversion_xpm[] = {
"32 32 2 1",
" c None",
". c #000000",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" .......... ",
" .......... ",
" . .. . ",
" ... .. ... ",
" ..... .. ..... ",
" .... .. .... ",
"... .. ... ",
" .... .. .... ",
" ..... .. ..... ",
" ... .. ... ",
" . .. . ",
" .. ",
" .. ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" "};
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