Commit 70f6478c by Sebastian Eichelbaum

[ADD] optimized code a bit and added some debug parts for time measurement

parent 81b17d7e
......@@ -191,7 +191,7 @@ private:
* \param z number of vertices in Z direction
* \param vals the values at the vertices
*/
template< typename T > void calculateMarchingCube( unsigned int x, unsigned int y, unsigned int z, const std::vector< T >* vals );
template< typename T > void calculateMarchingCube( const unsigned int& x, const unsigned int& y, const unsigned int& z, const std::vector< T >* vals );
unsigned int m_nCellsX; //!< No. of cells in x direction.
unsigned int m_nCellsY; //!< No. of cells in y direction.
......@@ -206,7 +206,7 @@ private:
};
template<typename T> void WMarchingCubesAlgorithm::calculateMarchingCube(
unsigned int x, unsigned int y, unsigned int z, const std::vector< T >* vals )
const unsigned int& x, const unsigned int& y, const unsigned int& z, const std::vector< T >* vals )
{
unsigned int nX = m_nCellsX + 1;
unsigned int nY = m_nCellsY + 1;
......@@ -337,6 +337,15 @@ template<typename T> void WMarchingCubesAlgorithm::calculateMarchingCube(
}
}
bool cellSort( WSpanSpaceBase::cellids_t::value_type i, WSpanSpaceBase::cellids_t::value_type j )
{
return ( ( i->m_z < j->m_z ) ) ||
( ( i->m_z == j->m_z ) && ( i->m_y < j->m_y) ) ||
( ( i->m_z == j->m_z ) && ( i->m_y == j->m_y ) && ( i->m_x < j->m_x ) );
}
template<typename T> boost::shared_ptr<WTriangleMesh> WMarchingCubesAlgorithm::generateSurface( size_t nbCoordsX, size_t nbCoordsY, size_t nbCoordsZ,
const WMatrix< double >& mat,
const std::vector< T >* vals,
......@@ -344,7 +353,7 @@ template<typename T> boost::shared_ptr<WTriangleMesh> WMarchingCubesAlgorithm::g
boost::shared_ptr< WSpanSpaceBase > spanSpace,
boost::shared_ptr< WProgressCombiner > mainProgress )
{
timeval timer1, timer2;
timeval timer1, timer2, timer3, timer4;
long millis;
double activeCells = 0, allCells;
......@@ -364,7 +373,7 @@ template<typename T> boost::shared_ptr<WTriangleMesh> WMarchingCubesAlgorithm::g
// unsigned int nX = m_nCellsX + 1;
// unsigned int nY = m_nCellsY + 1;
// unsigned int nPointsInSlice = nX * nY;
boost::shared_ptr< WProgress > progress;
progress = boost::shared_ptr< WProgress >( new WProgress( "Marching Cubes" ) );
mainProgress->addSubProgress( progress );
......@@ -387,23 +396,61 @@ template<typename T> boost::shared_ptr<WTriangleMesh> WMarchingCubesAlgorithm::g
millis = ( (timer2.tv_sec * 1000) + (timer2.tv_usec / 1000) ) - ( (timer1.tv_sec * 1000) + (timer1.tv_usec / 1000) );
wlog::info( "Time - MC" ) << millis;
// Cache unoptimale version!
// calculate normal marching cubes algorithm
gettimeofday( &timer1, NULL );
for( unsigned int x = 0; x < m_nCellsX; x++ )
{
++*progress;
for( unsigned int y = 0; y < m_nCellsY; y++ )
{
for( unsigned int z = 0; z < m_nCellsZ; z++ )
{
// calculate
calculateMarchingCube( x, y, z, vals );
}
}
}
gettimeofday( &timer2, NULL );
millis = ( (timer2.tv_sec * 1000) + (timer2.tv_usec / 1000) ) - ( (timer1.tv_sec * 1000) + (timer1.tv_usec / 1000) );
wlog::info( "Time - MC - Cacheunoptimaal" ) << millis;
// calculate marching cubes algorithm
gettimeofday( &timer1, NULL );
boost::shared_ptr< WSpanSpace< T > > sstyped = boost::dynamic_pointer_cast< WSpanSpace< T > >( spanSpace );
boost::shared_ptr< WSpanSpaceBase::cellids_t > cells = spanSpace->findCells( isoValue );
gettimeofday( &timer2, NULL );
millis = ( (timer2.tv_sec * 1000) + (timer2.tv_usec / 1000) ) - ( (timer1.tv_sec * 1000) + (timer1.tv_usec / 1000) );
wlog::info( "Time - SS-find" ) << millis;
std::sort( cells->begin(), cells->end(), cellSort );
gettimeofday( &timer3, NULL );
millis = ( (timer3.tv_sec * 1000) + (timer3.tv_usec / 1000) ) - ( (timer2.tv_sec * 1000) + (timer2.tv_usec / 1000) );
wlog::info( "Time - Sort" ) << millis;
for( typename WSpanSpaceBase::cellids_t::const_iterator i = cells->begin(); i < cells->end(); ++i )
{
calculateMarchingCube( ( *i )->m_x, ( *i )->m_y, ( *i )->m_z, vals );
//wlog::info( "Time - SS" ) << ( *i )->m_x ;
activeCells++;
}
gettimeofday( &timer2, NULL );
millis = ( (timer2.tv_sec * 1000) + (timer2.tv_usec / 1000) ) - ( (timer1.tv_sec * 1000) + (timer1.tv_usec / 1000) );
gettimeofday( &timer4, NULL );
millis = ( (timer4.tv_sec * 1000) + (timer4.tv_usec / 1000) ) - ( (timer3.tv_sec * 1000) + (timer3.tv_usec / 1000) );
wlog::info( "Time - SS" ) << millis;
gettimeofday( &timer4, NULL );
millis = ( (timer4.tv_sec * 1000) + (timer4.tv_usec / 1000) ) - ( (timer1.tv_sec * 1000) + (timer1.tv_usec / 1000) );
wlog::info( "Time - SS insgesamt" ) << millis;
// debug - show how many cells are active
allCells = m_nCellsZ * m_nCellsY * m_nCellsX;
wlog::info( "Cells" ) << allCells << " - " << activeCells << " -> " << (activeCells / allCells ) * 100;
unsigned int nextID = 0;
boost::shared_ptr< WTriangleMesh > triMesh( new WTriangleMesh( m_idToVertices.size(), m_trivecTriangles.size() ) );
......
......@@ -106,6 +106,8 @@ public:
* Initial min/max iso values
*/
SSKdTreeNode():
m_leftTree( NULL ),
m_rightTree( NULL ),
m_location( minmax_t( T(), T() ), ( unsigned int ) 0, ( unsigned int ) 0, ( unsigned int ) 0 )
{
}
......@@ -379,16 +381,21 @@ template< class T > WSpanSpace<T>::WSpanSpace( size_t nbCoordsX, size_t nbCoords
template< class T > void WSpanSpace<T>::searchKdTree( double isovalue,
cellids_t& cellVector ) const
{
cellVector.reserve( m_nCellsX * m_nCellsY * m_nCellsZ );
if( m_kdTreeNode )
{
searchKdMaxMin( m_kdTreeNode, isovalue, cellVector );
}
else
{
cellVector.push_back( &m_kdTreeNode->m_location.m_id );
}
}
template< class T > void WSpanSpace<T>::searchKdMaxMin( SSKdTreeNode< T >* root, double isovalue,
cellids_t& cellVector ) const
{
//wlog::info( "max min" ) << "1";
if( root->m_location.m_minMax.m_max > isovalue )
{
if( root->m_location.m_minMax.m_min <= isovalue )
......@@ -398,12 +405,13 @@ template< class T > void WSpanSpace<T>::searchKdMaxMin( SSKdTreeNode< T >* root,
cellVector.push_back( &root->m_location.m_id );
}
}
//wlog::info( "max min" ) << "left";
if( root->m_leftTree )
{
searchKdMinMax( root->m_leftTree, isovalue, cellVector );
}
}
//wlog::info( "max min" ) << "right";
if( root->m_rightTree )
{
searchKdMinMax( root->m_rightTree, isovalue, cellVector );
......@@ -412,6 +420,7 @@ template< class T > void WSpanSpace<T>::searchKdMaxMin( SSKdTreeNode< T >* root,
template< class T > void WSpanSpace<T>::searchKdMinMax( SSKdTreeNode< T >* root, double isovalue,
cellids_t& cellVector ) const
{
//wlog::info( "min max" ) << "1";
if( root->m_location.m_minMax.m_min <= isovalue )
{
if( root->m_location.m_minMax.m_max > isovalue )
......@@ -421,11 +430,13 @@ template< class T > void WSpanSpace<T>::searchKdMinMax( SSKdTreeNode< T >* root,
cellVector.push_back( &root->m_location.m_id );
}
}
//wlog::info( "min max" ) << "right";
if( root->m_rightTree )
{
searchKdMaxMin( root->m_rightTree, isovalue, cellVector );
}
}
//wlog::info( "min max" ) << "left";
if( root->m_leftTree )
{
searchKdMaxMin( root->m_leftTree, isovalue, cellVector );
......
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