Commit 7679c99c authored by Sebastian Eichelbaum's avatar Sebastian Eichelbaum
Browse files

[ADD #194] TF exporter module. Exports transfer functions in simple txt format

parent a299326f
......@@ -124,6 +124,21 @@ namespace string_utils
return ss.str();
}
/**
* Convert a given value to a string. The input value must provide a operator<< or be a standard scalar type.
*
* \param value the value to cast to string
*
* \return the string.
*/
inline std::string toString( const unsigned char& value ) // NOLINT: stylechecker complains about non const ref!?
{
std::stringstream ss;
// NOTE: unsigned chars are interpreted as ASCII chars. We want it to be used as number.
ss << static_cast< int >( value );
return ss.str();
}
/**
* Convert a given value to a string. The input value must provide a operator<< or be a standard scalar type. This method additionally allows
* setting width and precision flags of the used std::stringstream.
......
......@@ -53,6 +53,16 @@ public:
*/
typedef boost::shared_ptr< WModuleInputData< T > > PtrType;
/**
* Pointer to this. For convenience.
*/
typedef boost::shared_ptr< WModuleInputData< T > > SPtr;
/**
* Pointer to this. For convenience.
*/
typedef boost::shared_ptr< const WModuleInputData< T > > ConstSPtr;
/**
* Reference to this type.
*/
......
......@@ -52,6 +52,16 @@ public:
*/
typedef boost::shared_ptr< WModuleOutputData< T > > PtrType;
/**
* Pointer to this. For convenience.
*/
typedef boost::shared_ptr< WModuleOutputData< T > > SPtr;
/**
* Pointer to this. For convenience.
*/
typedef boost::shared_ptr< const WModuleOutputData< T > > ConstSPtr;
/**
* Reference to this type.
*/
......
......@@ -6,3 +6,4 @@ ADD_MODULE( writeDendrogram )
ADD_MODULE( writeGeometry )
ADD_MODULE( writeMesh )
ADD_MODULE( writeTracts )
ADD_MODULE( writeTransferFunction )
......@@ -105,12 +105,12 @@ private:
* An input connector used to get datasets from other modules. The connection management between co
nnectors must not be handled by the module.
*/
boost::shared_ptr< WModuleInputData< WDataSetSingle > > m_input;
WModuleInputData< WDataSetSingle >::SPtr m_input;
/**
* The output connector used to provide the calculated data to other modules.
*/
boost::shared_ptr< WModuleOutputData< WDataSetSingle > > m_output;
WModuleOutputData< WDataSetSingle >::SPtr m_output;
/**
* A condition used to notify about changes in several properties.
......
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV-Leipzig and CNCF-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/>.
//
//---------------------------------------------------------------------------
// C headers
// ...
// C++ headers
#include <fstream>
#include <string>
// External lib headers
#include <boost/shared_ptr.hpp>
// OW core headers
#include <core/common/WAssert.h>
#include <core/common/WPropertyHelper.h>
#include <core/common/WPathHelper.h>
#include <core/common/WStringUtils.h>
// own, local headers
#include "WMWriteTransferFunction.h"
W_LOADABLE_MODULE( WMWriteTransferFunction )
WMWriteTransferFunction::WMWriteTransferFunction():
WModule()
{
}
WMWriteTransferFunction::~WMWriteTransferFunction()
{
// cleanup
}
boost::shared_ptr< WModule > WMWriteTransferFunction::factory() const
{
// See "src/modules/template/" for an extensively documented example.
return boost::shared_ptr< WModule >( new WMWriteTransferFunction() );
}
const std::string WMWriteTransferFunction::getName() const
{
return "Write Transfer Function";
}
const std::string WMWriteTransferFunction::getDescription() const
{
// Specify your module description here. Be detailed. This text is read by the user.
return "Allows to export transfer functions in several formats.";
}
void WMWriteTransferFunction::connectors()
{
// an output connector for the transfer function created
m_input = WModuleInputData < WDataSetSingle >::createAndAdd( shared_from_this(), "transferFunction1D", "The transfer function" );
// call WModule's initialization
WModule::connectors();
}
void WMWriteTransferFunction::properties()
{
m_propCondition = boost::shared_ptr< WCondition >( new WCondition() );
m_savePath = m_properties->addProperty( "Save Location", "Set the path to the file.", WPathHelper::getAppPath() / "tf.txt" );
m_saveTrigger = m_properties->addProperty( "Save", "Save to file.", WPVBaseTypes::PV_TRIGGER_READY, m_propCondition );
// Call parent method
WModule::properties();
}
void WMWriteTransferFunction::moduleMain()
{
m_moduleState.setResetable( true, true );
m_moduleState.add( m_propCondition );
m_moduleState.add( m_input->getDataChangedCondition() );
ready();
// lets go
while( !m_shutdownFlag() )
{
m_saveTrigger->set( WPVBaseTypes::PV_TRIGGER_READY );
m_moduleState.wait();
if( m_shutdownFlag() )
{
break;
}
if( m_saveTrigger->get( true ) == WPVBaseTypes::PV_TRIGGER_TRIGGERED )
{
WDataSetSingle::SPtr d = m_input->getData();
// valid data?
if( !d )
{
warnLog() << "No data available to save.";
continue;
}
// valid path?
boost::filesystem::path p = m_savePath->get( true );
debugLog() << "Save TF to \"" + p.string() + "\".";
std::ofstream f;
f.open( p.string().c_str() );
if( !f.good() )
{
errorLog() << "Failed to open file. Abort.";
continue;
}
// get value set and write it
boost::shared_ptr< WValueSetBase > vsb = d->getValueSet();
if( ( vsb->order() != 1 ) || ( vsb->dimension() != 4 ) || ( vsb->getDataType() != W_DT_UNSIGNED_CHAR ) )
{
errorLog() << "This is not a proper TF. Abort.";
continue;
}
// we ensured that the valuesetbase is this valueset:
boost::shared_ptr< WValueSet< unsigned char > > vs = boost::shared_dynamic_cast< WValueSet< unsigned char > >( vsb );
f << "Exported using OpenWalnut. http://www.openwalnut.org" << std::endl;
f << "# TF export format:" << std::endl <<
"# Comments begin with #" << std::endl <<
"# 1st line: width height" << std::endl <<
"# Then, RGBA quadruples; line-wise; values space separated; in x direction first." << std::endl;
// later, we might support 2d TFs. Now, write 1 as second dimension
f << string_utils::toString( vs->size() ) << " " << "1" << std::endl;
// go through each RGBA vector
for( size_t i = 0; i < vs->size(); ++i )
{
f << string_utils::toString( vs->getScalar( i * 4 + 0 ) ) << " "
<< string_utils::toString( vs->getScalar( i * 4 + 1 ) ) << " "
<< string_utils::toString( vs->getScalar( i * 4 + 2 ) ) << " "
<< string_utils::toString( vs->getScalar( i * 4 + 3 ) ) << std::endl;
}
// done.
f.close();
// done
m_saveTrigger->set( WPVBaseTypes::PV_TRIGGER_READY );
}
}
}
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV-Leipzig and CNCF-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 WMMYNEWMODULE_H
#define WMMYNEWMODULE_H
#include <string>
#include <core/kernel/WModule.h>
#include <core/kernel/WModuleOutputData.h>
#include <core/kernel/WModuleInputData.h>
#include <core/common/WProperties.h>
/**
* Module to export transferfunctions
*
* \ingroup modules
*/
class WMWriteTransferFunction: public WModule
{
public:
/**
* Constructor.
*/
WMWriteTransferFunction();
/**
* Destructor
*/
virtual ~WMWriteTransferFunction();
/**
* 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 to 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;
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:
/**
* Condition used throughout the module to notify the thread if some changes happened (like properties have changed and similar).
*/
boost::shared_ptr< WCondition > m_propCondition;
/**
* The connector used to get the TF.
*/
WModuleInputData< WDataSetSingle >::SPtr m_input;
/**
* Where to save the file to
*/
WPropFilename m_savePath;
/**
* DO the save operation
*/
WPropTrigger m_saveTrigger;
};
#endif // WMMYNEWMODULE_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/>.
//
//---------------------------------------------------------------------------
// Provide additional material and descriptions for your module. You can
// provide this for multiple modules if you like.
// NOTE: everything but the module name is optional, even if this makes no
// sense at all.
// This defines some properties of the module "Template". This must
// match the name specified in WMTemplate::getName().
"Write Transfer Function"
{
// Provide an icon. If the icon exists, it overrides the one provided by your
// getIcon method. This path is relative to your module's resource directory.
icon="WMTransferFunction.png";
// Where to find the module?
website = "http://www.openwalnut.org";
// Provide a description, If you do so, this description overrides the one
// provided by your getDescription method.
// HINT: multi-line strings are not supported. Please provide long texts in
// one line.
description = "This module allows you to write transfer functions to disk.";
// Help file. This should be an HTML or TXT document. Only one document allowed.
// Provide a list of authors. These authors can have further information associated with them.
author = "OpenWalnut Project";
// Provide author information. Especially a contact address is very handy.
// This associates some URL and Mail contact info to "OpenWalnut Project".
"OpenWalnut Project"
{
url="http://www.openwalnut.org";
email="contact@openwalnut.org";
what="Design, Development and Bug fixing";
};
// Provide some tags to have modules nicely grouped and ordered.
// NOTE: tags are handled case insesitive.
// IMPORTANT: the order of appearance will be used by OW to classify your tags
tag = "DVR";
};
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