WSpanSpace.h 14.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
//---------------------------------------------------------------------------
//
// 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 WSPANSPACE_H
#define WSPANSPACE_H

#include <vector>
#include <algorithm>
#include <utility>
#include <tr1/functional>
32
#include <sys/time.h>
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116

#include "../../common/WLogger.h"
#include "../../common/WProgressCombiner.h"

/**
 * Contains the minimum and maximum iso value
 */
template< typename T > struct MinMax
{
    /**
     * Initial min/max iso values
     *
     * \param min minimum isovalue
     * \param max maximum isovalue
     */
    MinMax( T min, T max ):
    m_max( max ), m_min( min )
    {
    }
    T m_max; //!< max iso value
    T m_min; //!< min iso value
};

/**
 * Contains the KdCell coordinates
 */
struct KdCellId
{
    /**
     * Initial KdCell coordinates
     *
     * \param x X coordinate
     * \param y Y coordinate
     * \param z Z coordinate
     */
    KdCellId( unsigned int x, unsigned int y, unsigned int z ):
    m_x( x ), m_y( y ), m_z( z )
    {
    }

    unsigned int m_x; //!< X coordinate
    unsigned int m_y; //!< Y coordinate
    unsigned int m_z; //!< Z coordinate
};

/**
 * Contains the min/max values and the Kd coordinates
 */
template< typename T > struct KdCells
{
    /**
     * Initial KdCell coordinates and min/max iso value
     *
     * \param minMax minimum and maximum isovalue
     * \param x X coordinate
     * \param y Y coordinate
     * \param z Z coordinate
     */
    KdCells( MinMax< T > minMax, unsigned int x, unsigned int y, unsigned int z ):
    m_minMax( minMax ), m_id( KdCellId( x, y, z ) )
    {
    }
    MinMax< T > m_minMax; //!< min/max iso value of KdCell
    KdCellId m_id; //!< KdCell coordinates
};

/**
 * This class saves the node of the KdTree nodes
 */
template< class T > class SSKdTreeNode
{
public:
    /**
     * Initial min/max iso values
     */
    SSKdTreeNode():
        m_location( minmax_t( T(), T() ), ( unsigned int ) 0, ( unsigned int ) 0, ( unsigned int ) 0 )
    {
    }

    typedef MinMax< T > minmax_t; //!< minmax type used for KdCells
    typedef KdCells< T > cells_t; //!< typ for KdCells
    typedef std::vector< cells_t > kdTree; //!< vector of KdCells

117 118
    SSKdTreeNode< T >* m_leftTree; //!< left tree of kd-tree median is NOT included
    SSKdTreeNode< T >* m_rightTree; //!< right tree of kd-tree median is included
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
    cells_t m_location; //!< location of KdTree Node contains cell cordinates and min/max iso values
};

/**
 * This class creates the KdTree
 */
template< class T > class WSpanSpaceKdTree
{
public:
    typedef MinMax< T > minmax_t; //!< minmax type used for KdCells
    typedef KdCells< T > cells_t; //!< the typ for KdCells
    typedef std::vector< cells_t > kdTree_t; //!< define vector of KdCells

    WSpanSpaceKdTree()
    {
    }

    /**
     * Returns a SpanSpace Kd-Tree node.
     *
     * \param points vector of MinMax points
     * \param depth Kd-Tree depth
     * \param first (this element is included)
     * \param last  (this element isn't included)
     *
     * \return return a SpanSpace Kd-Tree node.
     */
146
    SSKdTreeNode< T >* createTree(  kdTree_t& points, unsigned int depth, size_t first, size_t last );
147 148 149

private:
    int m_depth; //!< depth of KdTree
150

151 152 153 154 155 156
    /**
     * Sort method for the nth_element function
     *
     * \param i first cell which is compared to secound cell
     * \param j secound cell which is compared to the first cell
     *
157
     * \return for even numbers true if the first cell has a smaller minimum isovalue and
158 159 160 161 162
     *         for odd numbers true if the secound cell has a bigger maximum isovalue
     */
    bool sortValues( cells_t i, cells_t j );
};

163
template< class T > SSKdTreeNode< T >* WSpanSpaceKdTree< T >::createTree(
164 165 166 167
        kdTree_t& points, unsigned int depth, size_t first, size_t last )
{
    size_t size = last - first;
    size_t median = size / 2;
168
    SSKdTreeNode< T >* kdNode( new SSKdTreeNode< T >() );
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
    m_depth = depth;

    if( size <=  1 )
    {
        kdNode->m_location = points[ first + median ];
        return kdNode;
    }

    std::nth_element(  points.begin() + first, points.begin() + first + median, points.begin() + last,
                        std::tr1::bind( &WSpanSpaceKdTree<T>::sortValues,
                                        this,
                                        std::tr1::placeholders::_1, std::tr1::placeholders::_2 ) );

    kdNode->m_location = points[ first + median ];
    kdNode->m_leftTree = createTree( points, depth + 1, first, first + median );
    kdNode->m_rightTree = createTree( points, depth + 1, first + median, last );

    return kdNode;
}

template< class T > bool WSpanSpaceKdTree< T >::sortValues( cells_t i, cells_t j )
{
    /*
        ^ max = y = 0
        |
        |
        |
        |
        +-------> min = x = 1
    */
    if( m_depth % 2 == 1  ) // choos x or y axis
200
    {
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
        // for even numbers like (0, 2, 4)
        // the function returns true if the first cell has a smaller minimum isovalue
        return ( i.m_minMax.m_min < j.m_minMax.m_min );
    }
    else
    {
        // for odd numbers like (1, 3, 5)
        // the function returns true if the secound cell has a bigger maximum isovalue
        return ( i.m_minMax.m_max < j.m_minMax.m_max );
    }
}

/**
 * This class is the virtual base class for the spanSpace algorithm.
 */
class WSpanSpaceBase
{
public:
    typedef std::vector< KdCellId* > cellids_t; //!< vector of pointers on KdCells
220
    typedef boost::shared_ptr< WSpanSpaceBase > SPtr;
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
    virtual ~WSpanSpaceBase()
    {
    }

    /**
     * Virtual function to find cells with given isovalue
     *
     * \param iso the value which is searched for
     *
     * \return a pointer on the KdCell Vector
     */
    virtual boost::shared_ptr< cellids_t > findCells( double iso ) const = 0;
};

/**
 * This class does the calculation work for the spanSpace algorithm
 */
template< class T> class WSpanSpace
:public WSpanSpaceBase
{
public:
    typedef MinMax< T > minmax_t; //!< minmax type used for KdCells
    typedef KdCells< T > cells_t; //!< the typ for KdCells
    typedef std::vector< cells_t > kdCells_t; //!< vector of KdCells
245

246 247 248 249 250 251 252 253 254 255 256 257 258 259
    /**
     * Virtual function of SpanSpace algorithm
     *
     * \param nbCoordsX No. of coordinates in X direction
     * \param nbCoordsY No. of coordinates in Y direction
     * \param nbCoordsZ No. of coordinates in Z direction
     * \param vals the values at the vertices
     * \param mainProgress progress combiner used to report progress of spanSpace algorithm
     */
    WSpanSpace(  size_t nbCoordsX, size_t nbCoordsY, size_t nbCoordsZ,
                 const std::vector< T >* vals,  boost::shared_ptr< WProgressCombiner > mainProgress );
    virtual ~WSpanSpace()
    {
    }
260

261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
    /**
     * Find cells with given isovalue
     *
     * \param iso the value which is searched for
     *
     * \return a pointer on the KdCell Vector
     */
    boost::shared_ptr< cellids_t > findCells( double iso ) const;

    /**
     * Get the minimum and maximun isovalue of a special cell with given x,y,z coordinates
     *
     * \param coordX x coordinate of cell
     * \param coordY y coordinate of cell
     * \param coordZ z coordinate of cell
     *
     * \return minMax isovalue
     */
    minmax_t getCellMinMax( unsigned int coordX, unsigned int coordY, unsigned int coordZ );
280

281 282 283
protected:
private:

284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
    /**
     * Calculate the id of a cell
     *
     * \param x x coordinate of cell
     * \param y y coordinate of cell
     * \param z z coordinate of cell
     *
     * \return calculated cell id
     */
    T getValue( unsigned int x, unsigned int y, unsigned int z );

    size_t m_nCellsX; //!< X coordinate of KdCell
    size_t m_nCellsY; //!< Y coordinate of KdCell
    size_t m_nCellsZ; //!< Z coordinate of KdCell

    const std::vector< T >* m_vals; //!< vector of cellids
    kdCells_t m_spanSpace; //!< vector of KdCells
301
    SSKdTreeNode< T >* m_kdTreeNode; //!< pointer of KdTree Node
302 303 304 305 306 307 308 309

    /**
     * Search Cells with special isocalue
     * and write this cells in given cellVector
     *
     * \param isovalue search in KdTree for this isovalue
     * \param cellVector write in this vector the cells which this isovalue
     */
310
    void searchKdTree( double isovalue, cellids_t& cellVector ) const;
311 312 313 314 315 316 317 318

    /**
     * Search the minimun isovalues in KdTree
     *
     * \param root KdTreeNode in which is searched
     * \param isovalue which is search for
     * \param cellVector pointer of vector in which cells are written
     */
319
    void searchKdMinMax( SSKdTreeNode< T >* root, double isovalue, cellids_t& cellVector ) const;
320 321 322 323 324 325 326 327

    /**
     * Search the maximum values in KdTree
     *
     * \param root KdTreeNode in which is searched
     * \param isovalue which is search for
     * \param cellVector pointer of vector in which cells are written
     */
328
    void searchKdMaxMin( SSKdTreeNode< T >* root, double isovalue, cellids_t& cellVector ) const;
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
    cellids_t m_activeCells; //!< variable is used to get the number of active cells
};

template< class T > boost::shared_ptr< WSpanSpaceBase::cellids_t > WSpanSpace<T>::findCells( double iso ) const
{
        boost::shared_ptr< cellids_t > cellVector( new cellids_t );
        T isoValue = static_cast< T >( iso );
        // fill cellVec with content
        searchKdTree( isoValue, *cellVector );
        return cellVector;
}


template< class T > WSpanSpace<T>::WSpanSpace( size_t nbCoordsX, size_t nbCoordsY, size_t nbCoordsZ,
                                             const std::vector< T >*  vals,  boost::shared_ptr< WProgressCombiner > mainProgress )
{
345 346
    timeval timer1, timer2;

347 348 349 350 351 352 353 354 355 356 357 358
    m_nCellsX = nbCoordsX - 1;
    m_nCellsY = nbCoordsY - 1;
    m_nCellsZ = nbCoordsZ - 1;

    boost::shared_ptr< WProgress > progressSpan = boost::shared_ptr< WProgress >( new WProgress( "Span Space", m_nCellsZ ) );
    mainProgress->addSubProgress( progressSpan );

    m_vals = vals;

    m_spanSpace.reserve( m_nCellsX * m_nCellsY * m_nCellsZ );
    for( unsigned int z = 0; z < m_nCellsZ; z++ )
    {
359
        // ++*progressSpan;
360 361 362 363 364 365 366 367 368 369 370
        for( unsigned int y = 0; y < m_nCellsY; y++ )
        {
            for( unsigned int x = 0; x < m_nCellsX; x++ )
            {
                m_spanSpace.push_back( cells_t( getCellMinMax( x, y, z ), x, y, z ) );
            }
        }
    }

    progressSpan->finish();
    WSpanSpaceKdTree< T > kdTree;
371

372
    gettimeofday( &timer1, NULL );
373
    m_kdTreeNode = kdTree.createTree( m_spanSpace, 0, 0, m_spanSpace.size() );
374 375 376
    gettimeofday( &timer2, NULL );
    long millis = ( (timer2.tv_sec * 1000) + (timer2.tv_usec / 1000) ) - ( (timer1.tv_sec * 1000) + (timer1.tv_usec / 1000) );
    wlog::info( "build" ) << millis;
377
}
378

379
template< class T > void WSpanSpace<T>::searchKdTree( double isovalue,
380 381 382 383 384 385 386 387 388
                                                      cellids_t& cellVector ) const
{
    if( m_kdTreeNode )
    {
        searchKdMaxMin( m_kdTreeNode, isovalue, cellVector );
    }
    else
        cellVector.push_back( &m_kdTreeNode->m_location.m_id );
}
389
template< class T > void WSpanSpace<T>::searchKdMaxMin( SSKdTreeNode< T >* root, double isovalue,
390 391
                                         cellids_t& cellVector ) const
{
392
    if( root->m_location.m_minMax.m_max > isovalue )
393
    {
394
        if( root->m_location.m_minMax.m_min <= isovalue )
395 396
        {
            if( !root->m_rightTree && !root->m_leftTree )
397
            {
398
                cellVector.push_back( &root->m_location.m_id );
399
            }
400 401 402 403 404 405 406 407 408 409 410 411
        }

        if( root->m_leftTree )
        {
            searchKdMinMax( root->m_leftTree, isovalue, cellVector );
        }
    }
    if( root->m_rightTree )
    {
        searchKdMinMax( root->m_rightTree, isovalue, cellVector );
    }
}
412
template< class T > void WSpanSpace<T>::searchKdMinMax( SSKdTreeNode< T >* root, double isovalue,
413 414
                                         cellids_t& cellVector ) const
{
415
    if( root->m_location.m_minMax.m_min <= isovalue )
416
    {
417
        if( root->m_location.m_minMax.m_max > isovalue )
418 419
        {
            if( !root->m_rightTree && !root->m_leftTree )
420
            {
421
                cellVector.push_back( &root->m_location.m_id );
422
            }
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
        }
        if( root->m_rightTree )
        {
            searchKdMaxMin( root->m_rightTree, isovalue, cellVector );
        }
    }
    if( root->m_leftTree )
    {
        searchKdMaxMin( root->m_leftTree, isovalue, cellVector );
    }
}
template< class T > typename WSpanSpace< T >::minmax_t WSpanSpace< T >::getCellMinMax( unsigned int coordX, unsigned int coordY, unsigned int coordZ )
{
    T valMax, valMin;
    valMax = valMin = getValue( coordX, coordY, coordZ  );

    // get point neighbours
440 441 442 443 444 445 446 447

    // Not needed since we already initialize valMin and valMax using this voxel's value
    // valMin = std::min( valMin, getValue( coordX, coordY, coordZ ) );
    // valMax = std::max( valMax, getValue( coordX, coordY, coordZ ) );

    valMin = std::min( valMin, getValue( coordX + 1, coordY + 1, coordZ + 1 ) );
    valMax = std::max( valMax, getValue( coordX + 1, coordY + 1, coordZ + 1 ) );

448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
    valMin = std::min( valMin, getValue( coordX + 1, coordY, coordZ ) );
    valMax = std::max( valMax, getValue( coordX + 1, coordY, coordZ  ) );

    valMin = std::min( valMin, getValue( coordX + 1, coordY + 1, coordZ ) );
    valMax = std::max( valMax, getValue( coordX + 1, coordY + 1, coordZ ) );

    valMin = std::min( valMin, getValue( coordX + 1, coordY, coordZ + 1 ) );
    valMax = std::max( valMax, getValue( coordX + 1, coordY, coordZ + 1 ) );

    valMin = std::min( valMin, getValue( coordX, coordY + 1, coordZ ) );
    valMax = std::max( valMax, getValue( coordX, coordY + 1, coordZ ) );

    valMin = std::min( valMin, getValue( coordX, coordY + 1, coordZ + 1 ) );
    valMax = std::max( valMax, getValue( coordX, coordY + 1, coordZ + 1 ) );

    valMin = std::min( valMin, getValue( coordX, coordY, coordZ + 1 ) );
    valMax = std::max( valMax, getValue( coordX, coordY, coordZ + 1 ) );

    return minmax_t( valMin, valMax );
}

template< class T > T WSpanSpace< T >::getValue( unsigned int x, unsigned int y, unsigned int z )
{
    return ( *m_vals )[ z * ( m_nCellsX + 1 ) * ( m_nCellsY + 1 ) + y * ( m_nCellsX + 1 ) + x ];
}

#endif  // WSPANSPACE_H