Commit 4b7c3527 authored by schurade's avatar schurade
Browse files

[CHANGE] create color arrays for fiber dataset using several threads

parent 23d97431
//---------------------------------------------------------------------------
//
// 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 <vector>
#include "WCreateColorArraysThread.h"
WCreateColorArraysThread::WCreateColorArraysThread( int left, int right, boost::shared_ptr< std::vector< float > >vertices,
boost::shared_ptr< std::vector< size_t > > lineStartIndexes,
boost::shared_ptr< std::vector< size_t > > lineLengths,
boost::shared_ptr< std::vector< float > > globalColors,
boost::shared_ptr< std::vector< float > > localColors,
boost::shared_ptr< std::vector< float > > tangents ):
WThreadedRunner(),
m_myThreadFinished( false ),
m_left( left ),
m_right( right ),
m_vertices( vertices ),
m_tangents( tangents ),
m_globalColors( globalColors ),
m_localColors( localColors ),
m_lineStartIndexes( lineStartIndexes ),
m_lineLengths( lineLengths )
{
}
WCreateColorArraysThread::~WCreateColorArraysThread()
{
}
void WCreateColorArraysThread::threadMain()
{
int pc = 0;
for ( int i = 0; i < m_left; ++i )
{
pc += (*m_lineLengths)[i]*3;
}
float r, g, b, rr, gg, bb;
float x1, x2, y1, y2, z1, z2;
float lastx, lasty, lastz;
for ( size_t i = m_left; i < m_right; ++i )
{
x1 = (*m_vertices)[pc];
y1 = (*m_vertices)[pc + 1];
z1 = (*m_vertices)[pc + 2];
x2 = (*m_vertices)[pc + (*m_lineLengths)[i] * 3 - 3 ];
y2 = (*m_vertices)[pc + (*m_lineLengths)[i] * 3 - 2 ];
z2 = (*m_vertices)[pc + (*m_lineLengths)[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)[pc] + ( (*m_vertices)[pc] - (*m_vertices)[pc + 3] );
lasty = (*m_vertices)[pc+ 1] + ( (*m_vertices)[pc + 1] - (*m_vertices)[pc + 4] );
lastz = (*m_vertices)[pc + 2] + ( (*m_vertices)[pc + 2] - (*m_vertices)[pc + 5] );
for ( size_t j = 0; j < m_lineLengths->at( i ); ++j )
{
rr = lastx - (*m_vertices)[pc];
gg = lasty - (*m_vertices)[pc + 1];
bb = lastz - (*m_vertices)[pc + 2];
lastx = (*m_vertices)[pc];
lasty = (*m_vertices)[pc + 1];
lastz = (*m_vertices)[pc + 2];
float norm = sqrt( rr * rr + gg * gg + bb * bb );
rr *= 1.0 / norm;
gg *= 1.0 / norm;
bb *= 1.0 / norm;
(*m_tangents)[pc] = rr;
(*m_tangents)[pc+1] = gg;
(*m_tangents)[pc+2] = bb;
if ( rr < 0.0 )
rr *= -1.0;
if ( gg < 0.0 )
gg *= -1.0;
if ( bb < 0.0 )
bb *= -1.0;
(*m_localColors)[pc] = rr;
(*m_localColors)[pc+1] = gg;
(*m_localColors)[pc+2] = bb;
(*m_globalColors)[pc] = r;
(*m_globalColors)[pc+1] = g;
(*m_globalColors)[pc+2] = b;
pc += 3;
}
}
m_myThreadFinished = true;
}
bool WCreateColorArraysThread::isFinished()
{
return m_myThreadFinished;
}
//---------------------------------------------------------------------------
//
// 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 WCREATECOLORARRAYSTHREAD_H
#define WCREATECOLORARRAYSTHREAD_H
#include <vector>
#include "../common/WThreadedRunner.h"
/**
* implements a thread that updates the fiber selection bit field
*/
class WCreateColorArraysThread: public WThreadedRunner
{
public:
/**
* default constructor
*
* \param left
* \param right
* \param vertices
* \param lineStartIndexes
* \param lineLengths
* \param globalColors
* \param localColors
* \param tangents
*/
WCreateColorArraysThread( int left, int right, boost::shared_ptr< std::vector< float > >vertices,
boost::shared_ptr< std::vector< size_t > > lineStartIndexes,
boost::shared_ptr< std::vector< size_t > > lineLengths,
boost::shared_ptr< std::vector< float > > globalColors,
boost::shared_ptr< std::vector< float > > localColors,
boost::shared_ptr< std::vector< float > > tangents );
/**
* destructor
*/
virtual ~WCreateColorArraysThread();
/**
* entry for the run command
*/
virtual void threadMain();
/**
* Return the value of the finished flag.
*/
inline bool isFinished();
protected:
private:
bool m_myThreadFinished; //!< Has the thread finished?
int m_left; //!< left boundary
int m_right; //!< right boundary
/**
* 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;
/**
* Storing the global color value of the fibers for each point.
*/
boost::shared_ptr< std::vector< float > > m_globalColors;
/**
* Storing the local color value of the fibers for each point.
* \note it is mutable to allow getLocalColors creating it on demand.
*/
mutable boost::shared_ptr< std::vector< float > > m_localColors;
/**
* 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
* the number of components for each point is multiplied.
*/
boost::shared_ptr< std::vector< size_t > > m_lineStartIndexes;
/**
* Line vector that contains the number of vertices for each line
*/
boost::shared_ptr< std::vector< size_t > > m_lineLengths;
};
#endif // WCREATECOLORARRAYSTHREAD_H
......@@ -31,6 +31,7 @@
#include "../graphicsEngine/WGEUtils.h"
#include "WDataSet.h"
#include "WDataSetFibers.h"
#include "WCreateColorArraysThread.h"
// prototype instance as singleton
boost::shared_ptr< WPrototyped > WDataSetFibers::m_prototype = boost::shared_ptr< WPrototyped >();
......@@ -67,137 +68,32 @@ WDataSetFibers::WDataSetFibers( boost::shared_ptr< std::vector< float > >vertice
m_tangents->resize( m_vertices->size() );
m_globalColors = boost::shared_ptr< std::vector< float > >( new std::vector<float>() );
m_globalColors->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)[pc];
y1 = (*m_vertices)[pc + 1];
z1 = (*m_vertices)[pc + 2];
x2 = (*m_vertices)[pc + (*m_lineLengths)[i] * 3 - 3 ];
y2 = (*m_vertices)[pc + (*m_lineLengths)[i] * 3 - 2 ];
z2 = (*m_vertices)[pc + (*m_lineLengths)[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)[pc] + ( (*m_vertices)[pc] - (*m_vertices)[pc + 3] );
lasty = (*m_vertices)[pc+ 1] + ( (*m_vertices)[pc + 1] - (*m_vertices)[pc + 4] );
lastz = (*m_vertices)[pc + 2] + ( (*m_vertices)[pc + 2] - (*m_vertices)[pc + 5] );
for ( size_t j = 0; j < m_lineLengths->at( i ); ++j )
{
rr = lastx - (*m_vertices)[pc];
gg = lasty - (*m_vertices)[pc + 1];
bb = lastz - (*m_vertices)[pc + 2];
lastx = (*m_vertices)[pc];
lasty = (*m_vertices)[pc + 1];
lastz = (*m_vertices)[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_globalColors->at( pc ) = r;
m_globalColors->at( pc + 1 ) = g;
m_globalColors->at( pc + 2 ) = b;
pc += 3;
}
}
}
void WDataSetFibers::calculateLocalColors() const
{
wlog::debug( "WDataSetFibers" ) << "Calculating local colors.";
m_localColors = boost::shared_ptr< std::vector< float > >( new std::vector<float>() );
m_localColors->resize( m_vertices->size() );
int pc = 0;
float r, g, b;
float x1, x2, y1, y2, z1, z2;
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;
WCreateColorArraysThread* t1 = new WCreateColorArraysThread( 0, m_lineLengths->size()/4, m_vertices,
m_lineStartIndexes, m_lineLengths, m_globalColors, m_localColors, m_tangents );
WCreateColorArraysThread* t2 = new WCreateColorArraysThread( m_lineLengths->size()/4+1, m_lineLengths->size()/2, m_vertices,
m_lineStartIndexes, m_lineLengths, m_globalColors, m_localColors, m_tangents );
WCreateColorArraysThread* t3 = new WCreateColorArraysThread( m_lineLengths->size()/2+1, m_lineLengths->size()/4*3, m_vertices,
m_lineStartIndexes, m_lineLengths, m_globalColors, m_localColors, m_tangents );
WCreateColorArraysThread* t4 = new WCreateColorArraysThread( m_lineLengths->size()/4*3+1, m_lineLengths->size(), m_vertices,
m_lineStartIndexes, m_lineLengths, m_globalColors, m_localColors, m_tangents );
t1->run();
t2->run();
t3->run();
t4->run();
float norm = sqrt( r * r + g * g + b * b );
r *= 1.0 / norm;
g *= 1.0 / norm;
b *= 1.0 / norm;
t1->wait();
t2->wait();
t3->wait();
t4->wait();
for ( size_t j = 0; j < m_lineLengths->at( i ); ++j )
{
// local color fun:
WColor localColor;
wmath::WPosition currentPos( m_vertices->at( pc ), m_vertices->at( pc + 1 ), m_vertices->at( pc + 2 ) );
wmath::WPosition nextPos;
if( j < m_lineLengths->at( i ) - 1 )
{
nextPos[0] = m_vertices->at( pc + 3 );
nextPos[1] = m_vertices->at( pc + 4 );
nextPos[2] = m_vertices->at( pc + 5 );
localColor = wge::getRGBAColorFromDirection( currentPos, nextPos );
}
else if( m_lineLengths->size() > 1 ) // there was a color in this line before!
{
localColor = WColor( m_localColors->at( pc - 3 ),
m_localColors->at( pc - 2 ),
m_localColors->at( pc - 1 ) );
}
else // there was only one point
{
localColor = WColor( r, g, b ); // same as global color
}
m_localColors->at( pc ) = localColor.getRed();
m_localColors->at( pc + 1 ) = localColor.getGreen();
m_localColors->at( pc + 2 ) = localColor.getBlue();
pc += 3;
}
}
wlog::debug( "WDataSetFibers" ) << "Calculating local colors: done!";
delete t1;
delete t2;
delete t3;
delete t4;
}
bool WDataSetFibers::isTexture() const
......@@ -262,12 +158,6 @@ boost::shared_ptr< std::vector< float > > WDataSetFibers::getGlobalColors() cons
boost::shared_ptr< std::vector< float > > WDataSetFibers::getLocalColors() const
{
if ( !m_localColors )
{
// only calculate local colors if really needed
calculateLocalColors();
}
return m_localColors;
}
......
......@@ -154,12 +154,6 @@ protected:
*/
static boost::shared_ptr< WPrototyped > m_prototype;
/**
* Calculates local colors for this dataset.
* \note it is const as it gets used in getLocalColors for creating the local colors only on demand.
*/
virtual void calculateLocalColors() const;
private:
/**
* Point vector for all fibers
......
......@@ -109,19 +109,28 @@ void WTubeDrawable::drawFibers( osg::RenderInfo& renderInfo ) const //NOLINT
boost::shared_ptr< std::vector< float > > verts = m_dataset->getVertices();
boost::shared_ptr< std::vector< float > > colors = ( m_globalColoring ? m_dataset->getGlobalColors() : m_dataset->getLocalColors() );
boost::shared_ptr< std::vector< bool > > active = WKernel::getRunningKernel()->getRoiManager()->getBitField();
#if 0
glEnableClientState( GL_VERTEX_ARRAY );
glEnableClientState( GL_COLOR_ARRAY );
// glEnableClientState( GL_VERTEX_ARRAY );
// glEnableClientState( GL_COLOR_ARRAY );
//
// glVertexPointer( 3, GL_FLOAT, 0, &(*verts)[0] );
// glColorPointer( 3, GL_FLOAT, 0, &(*colors)[0] );
glVertexPointer( 3, GL_FLOAT, 0, &(*verts)[0] ); //NOLINT
glColorPointer( 3, GL_FLOAT, 0, &(*colors)[0] ); //NOLINT
for ( size_t i = 0; i < active->size(); ++i )
{
if ( (*active)[i] )
{
//glDrawArrays( GL_LINE_STRIP, (*startIndexes)[i] * 3, (*pointsPerLine)[i] );
glDrawArrays( GL_LINE_STRIP, (*startIndexes)[i] * 3, (*pointsPerLine)[i] );
}
}
glDisableClientState( GL_VERTEX_ARRAY );
glDisableClientState( GL_COLOR_ARRAY );
#else
for ( size_t i = 0; i < active->size(); ++i )
{
if ( (*active)[i] )
{
glBegin( GL_LINE_STRIP );
int idx = (*startIndexes)[i] * 3;
for ( size_t k = 0; k < pointsPerLine->at( i ); ++k )
......@@ -133,10 +142,7 @@ void WTubeDrawable::drawFibers( osg::RenderInfo& renderInfo ) const //NOLINT
glEnd();
}
}
//std::cout << "huhu4" << std::endl;
// glDisableClientState( GL_VERTEX_ARRAY );
// glDisableClientState( GL_COLOR_ARRAY );
#endif
}
void WTubeDrawable::drawTubes() const
......
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