Commit 8dab11df authored by schurade's avatar schurade

(no commit message)

parent bea466ce
......@@ -49,6 +49,7 @@ WDataSetFibers::WDataSetFibers( boost::shared_ptr< std::vector< float > >vertice
m_lineLengths( lineLengths ),
m_verticesReverse( verticesReverse )
{
// TODO(schurade): replace this with a permanent solution
for ( size_t i = 0; i < m_vertices->size(); ++i )
{
m_vertices->at( i ) = 160 - m_vertices->at( i );
......@@ -57,6 +58,74 @@ WDataSetFibers::WDataSetFibers( boost::shared_ptr< std::vector< float > >vertice
++i;
//m_pointArray[i] = m_dh->frames - m_pointArray[i];
}
m_tangents = boost::shared_ptr< std::vector< float > >( new std::vector<float>() );
m_tangents->resize( m_vertices->size() );
m_colors = boost::shared_ptr< std::vector< float > >( new std::vector<float>() );
m_colors->resize( m_vertices->size() );
int pc = 0;
float r, g, b, rr, gg, bb;
float x1, x2, y1, y2, z1, z2;
float lastx, lasty, lastz;
for ( size_t i = 0; i < m_lineLengths->size(); ++i )
{
x1 = m_vertices->at( pc );
y1 = m_vertices->at( pc + 1 );
z1 = m_vertices->at( pc + 2 );
x2 = m_vertices->at( pc + m_lineLengths->at( i ) * 3 - 3 );
y2 = m_vertices->at( pc + m_lineLengths->at( i ) * 3 - 2 );
z2 = m_vertices->at( pc + m_lineLengths->at( i ) * 3 - 1 );
r = ( x1 ) - ( x2 );
g = ( y1 ) - ( y2 );
b = ( z1 ) - ( z2 );
if ( r < 0.0 )
r *= -1.0;
if ( g < 0.0 )
g *= -1.0;
if ( b < 0.0 )
b *= -1.0;
float norm = sqrt( r * r + g * g + b * b );
r *= 1.0 / norm;
g *= 1.0 / norm;
b *= 1.0 / norm;
lastx = m_vertices->at( pc ) + ( m_vertices->at( pc ) - m_vertices->at( pc + 3 ) );
lasty = m_vertices->at( pc + 1 ) + ( m_vertices->at( pc + 1 ) - m_vertices->at( pc + 4 ) );
lastz = m_vertices->at( pc + 2 ) + ( m_vertices->at( pc + 2 ) - m_vertices->at( pc + 5 ) );
for ( size_t j = 0; j < m_lineLengths->at( i ); ++j )
{
rr = lastx - m_vertices->at( pc );
gg = lasty - m_vertices->at( pc + 1 );
bb = lastz - m_vertices->at( pc + 2 );
lastx = m_vertices->at( pc );
lasty = m_vertices->at( pc + 1 );
lastz = m_vertices->at( pc + 2 );
// if ( rr < 0.0 )
// rr *= -1.0;
// if ( gg < 0.0 )
// gg *= -1.0;
// if ( bb < 0.0 )
// bb *= -1.0;
float norm = sqrt( rr * rr + gg * gg + bb * bb );
rr *= 1.0 / norm;
gg *= 1.0 / norm;
bb *= 1.0 / norm;
m_tangents->at( pc ) = rr;
m_tangents->at( pc + 1 ) = gg;
m_tangents->at( pc + 2 ) = bb;
m_colors->at( pc ) = r;
m_colors->at( pc + 1 ) = g;
m_colors->at( pc + 2 ) = b;
pc += 3;
}
}
}
void WDataSetFibers::sortDescLength()
......@@ -114,9 +183,46 @@ boost::shared_ptr< std::vector< size_t > > WDataSetFibers::getVerticesReverse()
return m_verticesReverse;
}
boost::shared_ptr< std::vector< float > > WDataSetFibers::getTangents() const
{
return m_tangents;
}
boost::shared_ptr< std::vector< float > > WDataSetFibers::getColors() const
{
return m_colors;
}
wmath::WPosition WDataSetFibers::getPosition( size_t fiber, size_t vertex ) const
{
size_t index = m_lineStartIndexes->at( fiber ) * 3;
index += vertex * 3;
return wmath::WPosition( m_vertices->at( index ), m_vertices->at( index + 1 ), m_vertices->at( index + 2 ) );
}
wmath::WPosition WDataSetFibers::getTangent( size_t fiber, size_t vertex ) const
{
wmath::WPosition point = getPosition( fiber, vertex );
wmath::WPosition tangent;
if ( vertex == 0 ) // first point
{
wmath::WPosition pointNext = getPosition( fiber, vertex + 1 );
tangent = point - pointNext;
}
else if ( vertex == m_lineLengths->at( fiber ) - 1 ) // last point
{
wmath::WPosition pointBefore = getPosition( fiber, vertex - 1 );
tangent = pointBefore - point;
}
else // somewhere in between
{
wmath::WPosition pointBefore = getPosition( fiber, vertex - 1 );
wmath::WPosition pointNext = getPosition( fiber, vertex + 1 );
tangent = pointBefore - pointNext;
}
tangent.normalize();
return tangent;
}
......@@ -117,6 +117,18 @@ public:
*/
boost::shared_ptr< std::vector< size_t > > getVerticesReverse() const;
/**
* Getter
*/
boost::shared_ptr< std::vector< float > > getTangents() const;
/**
* Getter
*/
boost::shared_ptr< std::vector< float > > getColors() const;
/**
* returns the position in space for a vertex of a given fiber
*
......@@ -125,6 +137,14 @@ public:
*/
wmath::WPosition getPosition( size_t fiber, size_t vertex ) const;
/**
* calculates the tangent for a point on the fiber
*
* \param fiber
* \param vertex
*/
wmath::WPosition getTangent( size_t fiber, size_t vertex ) const;
protected:
/**
......@@ -134,10 +154,20 @@ protected:
private:
/**
* Point vector for all fibers that is actually usable for what we want to do
* Point vector for all fibers
*/
boost::shared_ptr< std::vector< float > > m_vertices;
/**
* Point vector for tangents at each vertex, used for fake tubes
*/
boost::shared_ptr< std::vector< float > > m_tangents;
/**
* color vector for tangents at each vertex, used for fake tubes
*/
boost::shared_ptr< std::vector< float > > m_colors;
/**
* Line vector that contains the start index of its first point for each line.
* \warning The index returned cannot be used in the vertices array until
......
......@@ -91,7 +91,7 @@ boost::shared_ptr< WDataSet > WLoaderNIfTI::load()
unsigned int countVoxels = columns * rows * frames;
#ifdef DEBUG
nifti_image_infodump( header );
//nifti_image_infodump( header );
#endif
switch( header->datatype )
......
......@@ -42,6 +42,7 @@ WMFiberDisplay::WMFiberDisplay()
: WModule(),
m_osgNode( osg::ref_ptr< osg::Group >() )
{
m_shader = osg::ref_ptr< WShader > ( new WShader( "fake-tubes" ) );
}
WMFiberDisplay::~WMFiberDisplay()
......@@ -58,96 +59,8 @@ const char** WMFiberDisplay::getXPMIcon() const
return fiberdisplay_xpm;
}
osg::ref_ptr< osg::Geode > WMFiberDisplay::genFiberGeode()
{
using osg::ref_ptr;
ref_ptr< osg::Vec3Array > vertices = ref_ptr< osg::Vec3Array >( new osg::Vec3Array );
m_globalColors = ref_ptr< osg::Vec4Array >( new osg::Vec4Array );
m_localColors = ref_ptr< osg::Vec4Array >( new osg::Vec4Array );
m_geometry = ref_ptr< osg::Geometry >( new osg::Geometry );
vertices->reserve( m_dataset->getVertices()->size() );
m_globalColors->reserve( m_dataset->getVertices()->size() );
m_localColors->reserve( m_dataset->getVertices()->size() );
boost::shared_ptr< std::vector< size_t > > startIndexes = m_dataset->getLineStartIndexes();
boost::shared_ptr< std::vector< size_t > > pointsPerLine = m_dataset->getLineLengths();
boost::shared_ptr< std::vector< float > > verts = m_dataset->getVertices();
size_t vertexNum = 0;
WColor gc;
WColor lc;
boost::shared_ptr< WProgress > progress = boost::shared_ptr< WProgress >( new WProgress( "Fiber Display", m_dataset->size() ) );
m_progress->addSubProgress( progress );
for( size_t j = 0; j < m_dataset->size(); ++j )
{
vertices->push_back( osg::Vec3( verts->at( 3 * vertexNum ), verts->at( 3 * vertexNum + 1 ), verts->at( 3 * vertexNum + 2 ) ) );
++vertexNum;
gc = getRGBAColorFromDirection( m_dataset->getPosition( j, 0 ), m_dataset->getPosition( j, pointsPerLine->at( j ) - 1 ) );
for( size_t i = 1; i < pointsPerLine->at( j ); ++i )
{
vertices->push_back( osg::Vec3( verts->at( 3 * vertexNum ), verts->at( 3 * vertexNum + 1 ), verts->at( 3 * vertexNum + 2 ) ) );
++vertexNum;
lc = getRGBAColorFromDirection( m_dataset->getPosition( j, i ), m_dataset->getPosition( j, i - 1 ) );
m_localColors->push_back( wge::osgColor( lc ) );
m_globalColors->push_back( wge::osgColor( gc ) );
}
m_localColors->push_back( wge::osgColor( lc ) );
m_globalColors->push_back( wge::osgColor( gc ) );
m_geometry->addPrimitiveSet( new osg::DrawArrays( osg::PrimitiveSet::LINE_STRIP, startIndexes->at( j ), pointsPerLine->at( j ) ) );
++*progress;
}
progress->finish();
m_geometry->setVertexArray( vertices );
m_geometry->setColorArray( m_globalColors );
m_geometry->setColorBinding( osg::Geometry::BIND_PER_VERTEX );
m_geometry->setUseDisplayList( false );
m_geometry->setUseVertexBufferObjects( true );
osg::ref_ptr< osg::Geode > geode = osg::ref_ptr< osg::Geode >( new osg::Geode );
geode->addDrawable( m_geometry.get() );
return geode;
}
void WMFiberDisplay::updateColoring()
{
if ( m_properties->getValue< bool >( "Local Color" ) )
{
m_geometry->setColorArray( m_localColors );
}
else
{
m_geometry->setColorArray( m_globalColors );
}
}
void WMFiberDisplay::updateLinesShown()
{
boost::shared_ptr< std::vector< size_t > > startIndexes = m_dataset->getLineStartIndexes();
boost::shared_ptr< std::vector< size_t > > pointsPerLine = m_dataset->getLineLengths();
boost::shared_ptr< std::vector< bool > >active = WKernel::getRunningKernel()->getRoiManager()->getBitField( m_dataset );
m_geometry->removePrimitiveSet( 0, m_geometry->getNumPrimitiveSets() );
for( size_t i = 0; i < active->size(); ++i )
{
if ( active->at( i ) )
{
m_geometry->addPrimitiveSet( new osg::DrawArrays( osg::PrimitiveSet::LINE_STRIP, startIndexes->at( i ), pointsPerLine->at( i ) ) );
}
}
}
void WMFiberDisplay::moduleMain()
......@@ -198,9 +111,9 @@ void WMFiberDisplay::update()
if ( WKernel::getRunningKernel()->getRoiManager()->isDirty() )
{
//std::cout << "fd update" << std::endl;
updateLinesShown();
m_tubeDrawable->dirtyDisplayList();
}
slock.unlock();
}
......@@ -211,7 +124,15 @@ void WMFiberDisplay::create()
// create new node
m_osgNode = osg::ref_ptr< osg::Group >( new osg::Group );
m_osgNode->addChild( genFiberGeode().get() );
m_tubeDrawable = osg::ref_ptr< WTubeDrawable >( new WTubeDrawable );
m_tubeDrawable->setDataset( m_dataset );
osg::ref_ptr< osg::Geode > geode = osg::ref_ptr< osg::Geode >( new osg::Geode );
geode->addDrawable( m_tubeDrawable );
m_osgNode->addChild( geode );
m_osgNode->getOrCreateStateSet()->setMode( GL_LIGHTING, osg::StateAttribute::OFF );
WKernel::getRunningKernel()->getGraphicsEngine()->getScene()->addChild( m_osgNode.get() );
......@@ -236,6 +157,26 @@ void WMFiberDisplay::properties()
// this bool is hidden
m_properties->addBool( "active", true, true )->connect( boost::bind( &WMFiberDisplay::slotPropertyChanged, this, _1 ) );
m_properties->addBool( "Local Color", false )->connect( boost::bind( &WMFiberDisplay::slotPropertyChanged, this, _1 ) );
m_properties->addBool( "Use Tubes", false )->connect( boost::bind( &WMFiberDisplay::slotPropertyChanged, this, _1 ) );
}
void WMFiberDisplay::toggleTubes()
{
if ( m_properties->getValue< bool >( "Use Tubes" ) )
{
m_tubeDrawable->setUseTubes( true );
m_tubeDrawable->dirtyDisplayList();
m_shader->apply( m_osgNode );
osg::ref_ptr<osg::Uniform>( new osg::Uniform( "globalColor", 1 ) );
osg::StateSet* rootState = m_osgNode->getOrCreateStateSet();
rootState->addUniform( osg::ref_ptr<osg::Uniform>( new osg::Uniform( "globalColor", 1 ) ) );
}
else
{
m_tubeDrawable->setUseTubes( false );
m_tubeDrawable->dirtyDisplayList();
}
}
void WMFiberDisplay::slotPropertyChanged( std::string propertyName )
......@@ -255,6 +196,11 @@ void WMFiberDisplay::slotPropertyChanged( std::string propertyName )
{
updateColoring();
}
else if ( propertyName == "Use Tubes" )
{
toggleTubes();
}
else
{
// instead of WLogger we must use std::cerr since WLogger needs to much time!
......
......@@ -33,8 +33,11 @@
#include "../../kernel/WModule.h"
#include "../../kernel/WModuleInputData.h"
#include "../../graphicsEngine/WShader.h"
#include "../../math/WPosition.h"
#include "WTubeDrawable.h"
/**
* Test module for drawing fibers
*/
......@@ -90,13 +93,6 @@ protected:
*/
virtual void moduleMain();
/**
* Generates an OSG geometry for the given fiber dataset.
*
* \return OSG geometry representing the fiber.
*/
osg::ref_ptr< osg::Geode > genFiberGeode();
/**
* Initialize the connectors this module is using.
*/
......@@ -118,11 +114,6 @@ protected:
*/
void create();
/**
* Deletes the primitive list and adds only the avtivated
*/
void updateLinesShown();
/**
* Switches the osg to use the appropriate color array
*/
......@@ -146,25 +137,20 @@ private:
osg::ref_ptr< osg::Group > m_osgNode;
/**
* OSG geometry
* this pointer is stored for reuse when updating fibers to display
* stores pointer to the fiber drawer
*/
osg::ref_ptr< osg::Geometry > m_geometry;
osg::ref_ptr< WTubeDrawable > m_tubeDrawable;
/**
* Array that stores the global (direction) color for each fiber
* lock to prevent concurrent threads trying to update the osg node
*/
osg::ref_ptr< osg::Vec4Array > m_globalColors;
boost::shared_mutex m_updateLock;
/**
* Array that stores the local (direction) color for each fiber
* the shader object for this module
*/
osg::ref_ptr< osg::Vec4Array > m_localColors;
osg::ref_ptr< WShader >m_shader;
/**
* lock to prevent concurrent threads trying to update the osg node
*/
boost::shared_mutex m_updateLock;
/**
* calculates a color from the vector between two points in space
......@@ -174,6 +160,10 @@ private:
*/
WColor getRGBAColorFromDirection( const wmath::WPosition &pos1, const wmath::WPosition &pos2 );
/**
* switches between fiber display and tube representation
*/
void toggleTubes();
/**
* Node callback to handle updates properly
......
......@@ -45,28 +45,19 @@ void WRMBranch::addRoi( boost::shared_ptr< WRMROIRepresentation > roi )
m_rois.push_back( roi );
}
boost::shared_ptr< std::vector< bool > > WRMBranch::getBitField( unsigned int index )
boost::shared_ptr< std::vector< bool > > WRMBranch::getBitField()
{
if ( m_dirty )
{
recalculate();
}
unsigned int c = 0;
for( std::list< boost::shared_ptr< std::vector<bool> > >::iterator iter = m_bitFields.begin(); iter != m_bitFields.end(); ++iter )
{
if ( c == index)
{
return *iter;
}
++c;
}
return boost::shared_ptr< std::vector< bool > >();
return m_bitField;
}
void WRMBranch::addBitField( size_t size )
{
boost::shared_ptr< std::vector< bool > >bf = boost::shared_ptr< std::vector< bool > >( new std::vector< bool >( size, false ) );
m_bitFields.push_back( bf );
m_bitField = bf;
for( std::list< boost::shared_ptr< WRMROIRepresentation> >::iterator iter = m_rois.begin(); iter != m_rois.end(); ++iter )
{
( *iter )->addBitField( size );
......@@ -76,15 +67,16 @@ void WRMBranch::addBitField( size_t size )
void WRMBranch::recalculate()
{
// std::cout << "branch recalc" << std::endl;
boost::shared_ptr< std::vector<bool> > mbf = m_bitFields.front();
// boost::shared_lock<boost::shared_mutex> slock;
// slock = boost::shared_lock<boost::shared_mutex>( m_updateLock );
boost::shared_ptr< std::vector<bool> > mbf = m_bitField;
int size = mbf->size();
mbf->clear();
mbf->resize( size, true );
for( std::list< boost::shared_ptr< WRMROIRepresentation > >::iterator iter = m_rois.begin(); iter != m_rois.end(); ++iter )
{
boost::shared_ptr< std::vector<bool> > bf = ( *iter )->getBitField( 0 );
boost::shared_ptr< std::vector<bool> > bf = ( *iter )->getBitField();
bool isnot = ( *iter )->getROI()->isNot();
if ( !isnot )
{
......@@ -111,6 +103,8 @@ void WRMBranch::recalculate()
// std::cout << "active fibers in branch :" << counter << std::endl;
m_dirty = false;
// slock.unlock();
}
bool WRMBranch::isDirty()
......
......@@ -62,10 +62,9 @@ public:
/**
* getter for the bitfield
*
* \param index of the associated fiber dataset
* \return the bitfield
*/
boost::shared_ptr< std::vector< bool > > getBitField( unsigned int index );
boost::shared_ptr< std::vector< bool > > getBitField();
/**
* creates and adds a bitfield to the list
......@@ -111,10 +110,14 @@ private:
boost::shared_ptr< WROIManagerFibers > m_roiManager; //!< stores a pointer to the roi manager
std::list< boost::shared_ptr< std::vector<bool> > >m_bitFields; //!< list of bit fields for each fiber dataset
boost::shared_ptr< std::vector<bool> >m_bitField; //!< list of bit fields for each fiber dataset
std::list< boost::shared_ptr< WRMROIRepresentation> > m_rois; //!< list of rois in this this branch,
// first in the list is the master roi
/**
* lock to prevent concurrent threads trying to update the branch
*/
boost::shared_mutex m_updateLock;
};
#endif // WRMBRANCH_H
......@@ -54,45 +54,36 @@ boost::shared_ptr< WROI > WRMROIRepresentation::getROI()
return m_roi;
}
boost::shared_ptr< std::vector< bool > > WRMROIRepresentation::getBitField( unsigned int index )
boost::shared_ptr< std::vector< bool > > WRMROIRepresentation::getBitField()
{
if ( m_dirty )
{
recalculate();
}
unsigned int c = 0;
for( std::list< boost::shared_ptr< std::vector<bool> > >::iterator iter = m_bitFields.begin(); iter != m_bitFields.end(); ++iter )
{
if ( c == index)
{
return *iter;
}
++c;
}
return boost::shared_ptr< std::vector< bool > >();
return m_bitField;
}
void WRMROIRepresentation::addBitField( size_t size )
{
boost::shared_ptr< std::vector< bool > >bf = boost::shared_ptr< std::vector< bool > >( new std::vector< bool >( size, false ) );
m_bitFields.push_back( bf );
m_bitField = bf;
setDirty();
}
void WRMROIRepresentation::recalculate()
{
boost::shared_lock<boost::shared_mutex> slock;
slock = boost::shared_lock<boost::shared_mutex>( m_updateLock );
// boost::shared_lock<boost::shared_mutex> slock;
// slock = boost::shared_lock<boost::shared_mutex>( m_updateLock );
//std::cout << "roi recalc" << std::endl;
m_currentBitfield = m_bitFields.front();
m_currentBitfield = m_bitField;
size_t size = m_currentBitfield->size();
m_currentBitfield->clear();
m_currentBitfield->resize( size, false );
m_currentArray = m_branch->getRoiManager()->getDataSet( 0 )->getVertices();
m_currentReverse = m_branch->getRoiManager()->getDataSet( 0 )->getVerticesReverse();
m_kdTree = m_branch->getRoiManager()->getKdTree( 0 );
m_currentArray = m_branch->getRoiManager()->getDataSet()->getVertices();
m_currentReverse = m_branch->getRoiManager()->getDataSet()->getVerticesReverse();
m_kdTree = m_branch->getRoiManager()->getKdTree();
m_boxMin.resize( 3 );
m_boxMax.resize( 3 );
......@@ -117,7 +108,7 @@ void WRMROIRepresentation::recalculate()
// std::cout << "active fibers:" << counter << std::endl;
m_dirty = false;
slock.unlock();
//slock.unlock();
}
void WRMROIRepresentation::boxTest( int left, int right, int axis )
......
......@@ -66,10 +66,9 @@ public:
/**
* getter for bit field for a selected fiber dataset
*
*\param index
* \return the bit field
*/
boost::shared_ptr< std::vector< bool > > getBitField( unsigned int index );
boost::shared_ptr< std::vector< bool > > getBitField();
/**
* adds a bit field of a given size to the list of bit fields
......@@ -131,7 +130,7 @@ private:
boost::shared_ptr< WRMBranch > m_branch; //!< stores a pointer to the branch this roi belongs to
std::list< boost::shared_ptr< std::vector<bool> > >m_bitFields; //!< list of bit fields, one for each loaded
boost::shared_ptr< std::vector<bool> >m_bitField; //!< list of bit fields, one for each loaded
// fiber dataset
/**
......@@ -158,7 +157,7 @@ private:
std::vector<float> m_boxMax; //!< upper boundary of the box, used for boxtest
/**