Commit 402d4cdb authored by Sebastian Eichelbaum's avatar Sebastian Eichelbaum
Browse files

[CHANGE] moved fiber creator code to dataCreator module

parent 96afa79c
//---------------------------------------------------------------------------
//
// 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 <algorithm>
#include <core/common/math/WMath.h>
#include "WDataCreatorFiberSpiral.h"
WDataCreatorFiberSpiral::WDataCreatorFiberSpiral():
WObjectNDIP< WDataSetFibersCreatorInterface >( "Spiral", "Create a spiral of fibers." )
{
// initialize members
m_numRotations = m_properties->addProperty( "Num Rotations", "The number of rotations.", 25 );
m_numRotations->setMin( 1 );
m_numRotations->setMax( 1000 );
m_tubeRadius = m_properties->addProperty( "Tube Radius", "The radius of the tube spiralling up.", 1.0 );
m_tubeRadius->setMin( 0.01 );
m_tubeRadius->setMax( 5.0 );
}
WDataCreatorFiberSpiral::~WDataCreatorFiberSpiral()
{
// cleanup
}
void WDataCreatorFiberSpiral::operator()( WProgress::SPtr progress,
const WColor& color,
size_t numFibers,
size_t numVertsPerFiber,
const WPosition& origin,
const WPosition& size,
WDataSetFibers::VertexArray vertices,
WDataSetFibers::IndexArray fibIdx,
WDataSetFibers::LengthArray lengths,
WDataSetFibers::IndexArray fibIdxVertexMap,
WDataSetFibers::ColorArray colors )
{
std::srand( time( 0 ) );
WPosition originOffset = origin / 2.0;
double spiralRadius = std::min( size.x(), size.y() );
double tubeRadius = m_tubeRadius->get();
double height = size.z();
double numRotations = m_numRotations->get();
WColor fibColor = color;
// create each
for( size_t fidx = 0; fidx < numFibers; ++fidx )
{
size_t vertOffset = fidx * numVertsPerFiber;
fibIdx->push_back( vertOffset );
lengths->push_back( numVertsPerFiber );
double a1 = static_cast< double >( std::rand() % 255 ) / 255.0;
double a2 = static_cast< double >( std::rand() % 255 ) / 255.0;
double seedX = cos( 2.0 * piDouble * a1 ) * tubeRadius;
double seedY = sin( 2.0 * piDouble * a2 ) * tubeRadius;
double seedZ = sqrt( tubeRadius - ( seedX * seedX ) - ( seedY * seedY ) );
WPosition seed( seedX, seedY, seedZ );
// create the vertices
for( size_t vidx = 0; vidx < numVertsPerFiber; ++vidx )
{
double v = static_cast< double >( vidx ) / static_cast< double >( numVertsPerFiber - 1 );
double degree = v * 2.0 * piDouble * numRotations;
double X = seed.x() + cos( degree ) * v * spiralRadius;
double Y = seed.y() + sin( degree ) * v * spiralRadius;
double Z = seed.z() + ( v * height );
vertices->push_back( originOffset.x() + X );
vertices->push_back( originOffset.y() + Y );
vertices->push_back( originOffset.z() + Z );
colors->push_back( fibColor.x() );
colors->push_back( fibColor.y() );
colors->push_back( fibColor.z() );
fibIdxVertexMap->push_back( fidx );
}
++( *progress );
}
}
//---------------------------------------------------------------------------
//
// 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 WDATACREATORFIBERSPIRAL_H
#define WDATACREATORFIBERSPIRAL_H
#include <boost/shared_ptr.hpp>
#include "core/common/WObjectNDIP.h"
#include "WDataSetFibersCreatorInterface.h"
/**
* Create a fiber spiral
*/
class WDataCreatorFiberSpiral: public WObjectNDIP< WDataSetFibersCreatorInterface >
{
public:
/**
* Abbreviate shared_ptr
*/
typedef boost::shared_ptr< WDataCreatorFiberSpiral > SPtr;
/**
* Abbreviate const shared_ptr
*/
typedef boost::shared_ptr< const WDataCreatorFiberSpiral > ConstSPtr;
/**
* Default constructor.
*/
WDataCreatorFiberSpiral();
/**
* Destructor.
*/
virtual ~WDataCreatorFiberSpiral();
/**
* Create the dataset. This needs to be implemented by all the creators you write.
*
* \param progress progress indicator
* \param color color of all fibers
* \param numFibers number of fibers
* \param numVertsPerFiber number of vertices per fiber
* \param origin origin of the bbox
* \param size size of the bounding box
* \param vertices the vertex array. Fill this.
* \param fibIdx the fiber index array. Fill this.
* \param lengths the lengths array. Fill this.
* \param fibIdxVertexMap inverse map. Fill this.
* \param colors the color array. Fill this.
*
* \return fiber data
*/
virtual void operator()( WProgress::SPtr progress,
const WColor& color,
size_t numFibers,
size_t numVertsPerFiber,
const WPosition& origin,
const WPosition& size,
WDataSetFibers::VertexArray vertices,
WDataSetFibers::IndexArray fibIdx,
WDataSetFibers::LengthArray lengths,
WDataSetFibers::IndexArray fibIdxVertexMap,
WDataSetFibers::ColorArray colors );
protected:
private:
/**
* Number of rotations to do.
*/
WPropInt m_numRotations;
/**
* The radius of a tube (consisting of multiple fibers
*/
WPropDouble m_tubeRadius;
};
#endif // WDATACREATORFIBERSPIRAL_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 "WDataSetFibersCreatorInterface.h"
WDataSetFibersCreatorInterface::~WDataSetFibersCreatorInterface()
{
// cleanup
}
//---------------------------------------------------------------------------
//
// 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 WDATASETFIBERSCREATORINTERFACE_H
#define WDATASETFIBERSCREATORINTERFACE_H
#include <core/common/WProgress.h>
#include <core/common/math/linearAlgebra/WMatrixFixed.h>
#include <core/dataHandler/WDataSetFibers.h>
/**
* Define the interface which is injected into an \ref WObjectNDIP. Remember that \ref WObjectNDIP is a template class deriving from its
* template type. This way we can inject methods into the base class. It avoids derivation from \ref WObjectNDIP.
*
* This class is especially useful for all dataset types that are WDataSetFibers types.
*/
class WDataSetFibersCreatorInterface
{
public:
/**
* Create the dataset. This needs to be implemented by all the creators you write.
*
* \param progress progress indicator
* \param color color of all fibers
* \param numFibers number of fibers
* \param numVertsPerFiber number of vertices per fiber
* \param origin origin of the bbox
* \param size size of the bounding box
* \param vertices the vertex array. Fill this.
* \param fibIdx the fiber index array. Fill this.
* \param lengths the lengths array. Fill this.
* \param fibIdxVertexMap inverse map. Fill this.
* \param colors the color array. Fill this.
*
* \return fiber data
*/
virtual void operator()( WProgress::SPtr progress,
const WColor& color,
size_t numFibers,
size_t numVertsPerFiber,
const WPosition& origin,
const WPosition& size,
WDataSetFibers::VertexArray vertices,
WDataSetFibers::IndexArray fibIdx,
WDataSetFibers::LengthArray lengths,
WDataSetFibers::IndexArray fibIdxVertexMap,
WDataSetFibers::ColorArray colors ) = 0;
/**
* Destructor
*/
virtual ~WDataSetFibersCreatorInterface();
};
#endif // WDATASETFIBERSCREATORINTERFACE_H
......@@ -28,6 +28,7 @@
#include "WMDataCreatorScalar.h"
#include "WMDataCreatorVector.h"
#include "WMDataCreatorFibers.h"
#include "WMDataCreator.h"
......@@ -35,5 +36,6 @@ extern "C" void WLoadModule( WModuleList& m ) // NOLINT
{
m.push_back( boost::shared_ptr< WModule >( new WMDataCreatorScalar ) );
m.push_back( boost::shared_ptr< WModule >( new WMDataCreatorVector ) );
m.push_back( boost::shared_ptr< WModule >( new WMDataCreatorFibers ) );
}
......@@ -22,66 +22,74 @@
//
//---------------------------------------------------------------------------
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <core/kernel/WKernel.h>
#include <core/common/WPropertyHelper.h>
#include <core/common/math/WMath.h>
#include <core/dataHandler/WDataHandler.h>
#include "core/common/WAssert.h"
#include "core/common/WProgress.h"
#include "core/common/WStrategyHelper.h"
#include "core/dataHandler/WDataSetFibers.h"
#include "core/kernel/WKernel.h"
#include "WMFiberCreator.h"
#include "WMFiberCreator.xpm"
#include "WDataCreatorFiberSpiral.h"
// This line is needed by the module loader to actually find your module. You need to add this to your module too. Do NOT add a ";" here.
W_LOADABLE_MODULE( WMFiberCreator )
#include "WMDataCreatorFibers.xpm"
#include "WMDataCreatorFibers.h"
WMFiberCreator::WMFiberCreator():
WModule()
WMDataCreatorFibers::WMDataCreatorFibers():
WModule(),
m_strategy( "Dataset Creators", "Select one of the dataset creators and configure it to your needs.", NULL,
"Creator", "A list of all known creators." )
{
// add some strategies here
m_strategy.addStrategy( WDataCreatorFiberSpiral::SPtr( new WDataCreatorFiberSpiral() ) );
}
WMFiberCreator::~WMFiberCreator()
WMDataCreatorFibers::~WMDataCreatorFibers()
{
// Cleanup!
// cleanup
removeConnectors();
}
boost::shared_ptr< WModule > WMFiberCreator::factory() const
boost::shared_ptr< WModule > WMDataCreatorFibers::factory() const
{
return boost::shared_ptr< WModule >( new WMFiberCreator() );
return boost::shared_ptr< WModule >( new WMDataCreatorFibers() );
}
const char** WMFiberCreator::getXPMIcon() const
const char** WMDataCreatorFibers::getXPMIcon() const
{
return WMFiberCreator_xpm;
return datacreator_xpm;
}
const std::string WMFiberCreator::getName() const
const std::string WMDataCreatorFibers::getName() const
{
return "Fiber Creator";
return "Fiber Data Creator";
}
const std::string WMFiberCreator::getDescription() const
const std::string WMDataCreatorFibers::getDescription() const
{
return "This module can create fiber datasets using certain schemes. This can be useful to test your algorithms against special artificial "
"data that shows certain strengths or weaknesses of your module.";
return "Allows the user to create fiber data sets providing a bunch of data creation schemes.";
}
void WMFiberCreator::connectors()
void WMDataCreatorFibers::connectors()
{
// the selected fibers go to this output
m_fiberOutput = WModuleOutputData< WDataSetFibers >::createAndAdd( shared_from_this(), "out", "The created fiber data." );
// initialize connectors
m_output = WModuleOutputData< WDataSetFibers >::createAndAdd( shared_from_this(), "out", "The data that has been created" );
// call WModule's initialization
WModule::connectors();
}
void WMFiberCreator::properties()
void WMDataCreatorFibers::properties()
{
m_propCondition = boost::shared_ptr< WCondition >( new WCondition() );
// grid transform information
m_origin = m_properties->addProperty( "Origin", "Coordinate of the origin (voxel 0,0,0).", WPosition( 0.0, 0.0, 0.0 ), m_propCondition );
m_size = m_properties->addProperty( "Size", "The size of the dataset along the X,Y, and Z axis in the OpenWalnut coordinate system.",
WPosition( 128.0, 128.0, 128.0 ), m_propCondition );
// how much fibs and verts?
m_numFibers = m_properties->addProperty( "Num Fibers", "The number of fibers to create.", 500, m_propCondition );
m_numFibers->setMin( 1 );
m_numFibers->setMax( 10000 );
......@@ -91,144 +99,37 @@ void WMFiberCreator::properties()
m_fibColor = m_properties->addProperty( "Color", "Color for the fibers.", defaultColor::WHITE, m_propCondition );
// now, setup the strategy helper.
m_properties->addProperty( m_strategy.getProperties() );
// call WModule's initialization
WModule::properties();
}
void WMFiberCreator::spiral( size_t numFibers, size_t numVertsPerFiber,
WDataSetFibers::VertexArray vertices,
WDataSetFibers::IndexArray fibIdx,
WDataSetFibers::LengthArray lengths,
WDataSetFibers::IndexArray fibIdxVertexMap,
WDataSetFibers::ColorArray colors
)
{
WPosition m_centerPoint = WPosition( 0.0, 0.0, 0.0 );
double m_spiralRadius = 10.0;
double m_tubeRadius = 1.0;
double m_height = 25.0;
double m_rotations = 10.0;
WColor fibColor = m_fibColor->get();
// create each
for( size_t fidx = 0; fidx < numFibers; ++fidx )
{
size_t vertOffset = fidx * numVertsPerFiber;
fibIdx->push_back( vertOffset );
lengths->push_back( numVertsPerFiber );
double a1 = static_cast< double >( std::rand() % 255 ) / 255.0;
double a2 = static_cast< double >( std::rand() % 255 ) / 255.0;
double seedX = cos( 2.0 * piDouble * a1 ) * m_tubeRadius;
double seedY = sin( 2.0 * piDouble * a2 ) * m_tubeRadius;
double seedZ = sqrt( m_tubeRadius - ( seedX * seedX ) - ( seedY * seedY ) );
WPosition seed = m_centerPoint + WPosition( seedX, seedY, seedZ );
// create the vertices
for( size_t vidx = 0; vidx < numVertsPerFiber; ++vidx )
{
double v = static_cast< double >( vidx ) / static_cast< double >( numVertsPerFiber - 1 );
double degree = v * 2.0 * piDouble * m_rotations;
double X = seed.x() + cos( degree ) * v * m_spiralRadius;
double Y = seed.y() + sin( degree ) * v * m_spiralRadius;
double Z = seed.z() + ( v * m_height );
vertices->push_back( X );
vertices->push_back( Y );
vertices->push_back( Z );
colors->push_back( fibColor.x() );
colors->push_back( fibColor.y() );
colors->push_back( fibColor.z() );
fibIdxVertexMap->push_back( fidx );
}
}
}
void WMFiberCreator::crossing( size_t numFibers, size_t numVertsPerFiber,
WDataSetFibers::VertexArray vertices,
WDataSetFibers::IndexArray fibIdx,
WDataSetFibers::LengthArray lengths,
WDataSetFibers::IndexArray fibIdxVertexMap,
WDataSetFibers::ColorArray colors
)
void WMDataCreatorFibers::moduleMain()
{
WPosition starts[2];
WPosition ends[2];
starts[0] = WPosition( -10.0, 0.0, -10.0 );
starts[1] = WPosition( -10.0, 5.0, 10.0 );
ends[0] = WPosition( 10.0, 0.0, 10.0 );
ends[1] = WPosition( 10.0, 5.0, -10.0 );
WColor fibColor = m_fibColor->get();
// create each
unsigned char pidx = 0;
for( size_t fidx = 0; fidx < numFibers; ++fidx )
{
size_t vertOffset = fidx * numVertsPerFiber;
fibIdx->push_back( vertOffset );
lengths->push_back( numVertsPerFiber );
// current bundle
pidx = ( fidx >= ( numFibers / 2 ) );
// offset from start for this fib
WPosition offset( 0.0, 0.0, static_cast< double >( fidx ) / static_cast< double >( numFibers - 1 ) );
// create the vertices
for( size_t vidx = 0; vidx < numVertsPerFiber; ++vidx )
{
double v = static_cast< double >( vidx ) / static_cast< double >( numVertsPerFiber - 1 );
WPosition vert = v * ( offset + starts[pidx] ) + ( 1.0 - v ) * ( offset + ends[pidx] );
vertices->push_back( vert.x() );
vertices->push_back( vert.y() );
vertices->push_back( vert.z() );
colors->push_back( fibColor.x() );
colors->push_back( fibColor.y() );
colors->push_back( fibColor.z() );
fibIdxVertexMap->push_back( fidx );
}
}
}
void WMFiberCreator::moduleMain()
{
// get notified about data changes
// let the main loop awake if the data changes or the properties changed.
m_moduleState.setResetable( true, true );
// Remember the condition provided to some properties in properties()? The condition can now be used with this condition set.
m_moduleState.add( m_propCondition );
// we need to wake up if some strategy prop changed
m_moduleState.add( m_strategy.getProperties()->getUpdateCondition() );
// signal ready state
ready();
// main loop
while ( !m_shutdownFlag() )
// loop until the module container requests the module to quit
while( !m_shutdownFlag() )
{
// woke up since the module is requested to finish?
if ( m_shutdownFlag() )
{
break;
}
debugLog() << "Creating dataset";
// To query whether an input was updated, simply ask the input:
bool propUpdated = true;
if ( !propUpdated )
{
continue;
}
WProgress::SPtr progress( new WProgress( "Creating Dataset", m_numFibers->get() ) );
m_progress->addSubProgress( progress );
// for each fiber:
// build structures for keeping the data
size_t numFibers = m_numFibers->get();
size_t numVertsPerFiber = m_numVertsPerFiber->get();
size_t numVerts = numVertsPerFiber * numFibers;
debugLog() << "Creating " << numFibers << " fibers.";
boost::shared_ptr< WProgress > progress1 = boost::shared_ptr< WProgress >( new WProgress( "Creating fibers.",
numFibers ) );
// Create needed arrays:
WDataSetFibers::VertexArray vertices = WDataSetFibers::VertexArray( new WDataSetFibers::VertexArray::element_type() );
WDataSetFibers::IndexArray fibIdx = WDataSetFibers::IndexArray( new WDataSetFibers::IndexArray::element_type() );
WDataSetFibers::LengthArray lengths = WDataSetFibers::LengthArray( new WDataSetFibers::LengthArray::element_type() );
......@@ -240,19 +141,28 @@ void WMFiberCreator::moduleMain()
fibIdxVertexMap->reserve( numVerts );
colors->reserve( numVerts * 3 );
// the bounding box. We calc it during creation to save some time later during WDataSetFibers creation.