WVoxelOutliner.cpp 4.04 KB
Newer Older
1 2 3 4
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
5
// Copyright 2013-2014 Andreas Schwarzkopf, OpenWalnut Community
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
// 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 <iostream>

#include "WVoxelOutliner.h"

WVoxelOutliner::WVoxelOutliner()
{
31
    m_tree = new WOctree( 1.0 );
32 33 34 35
}

WVoxelOutliner::~WVoxelOutliner()
{
36
    delete m_tree;
37 38
}

39 40 41 42 43 44 45


void WVoxelOutliner::setVoxelWidth( double voxelWidth )
{
    delete m_tree;
    m_tree = new WOctree( voxelWidth / 2.0 );
}
46

47 48 49 50
void WVoxelOutliner::registerPoint( double x, double y, double z )
{
    m_tree->registerPoint( x, y, z );
}
51

52 53 54 55 56 57 58 59
WOctNode* WVoxelOutliner::getOctreeLeafNode( double x, double y, double z )
{
    return m_tree->getLeafNode( x, y, z );
}



boost::shared_ptr< WTriangleMesh > WVoxelOutliner::getOutline( bool highlightUsingColors )
60 61
{
    boost::shared_ptr< WTriangleMesh > tmpMesh( new WTriangleMesh( 0, 0 ) );
62
    drawNode( m_tree->getRootNode(), tmpMesh, m_tree, highlightUsingColors );
63 64 65
    return tmpMesh;
}

66 67
void WVoxelOutliner::drawNode( WOctNode* node, boost::shared_ptr< WTriangleMesh > outputMesh,
                               WOctree* octree, bool highlightUsingColors )
68 69 70
{
    if  ( node->getRadius() <= octree->getDetailLevel() )
    {
71
        osg::Vec4 color = osg::Vec4(
72 73 74 75 76 77 78 79 80 81 82 83 84
                WOctree::calcColor( node->getGroupNr(), 0 ),
                WOctree::calcColor( node->getGroupNr(), 1 ),
                WOctree::calcColor( node->getGroupNr(), 2 ), 1.0 );
        size_t index = outputMesh->vertSize();
        for( size_t vertex = 0; vertex <= 8; vertex++ )
        {
            double iX = vertex % 2;
            double iY = ( vertex / 2 ) % 2;
            double iZ = ( vertex / 4 ) % 2;
            double x = node->getCenter( 0 ) + node->getRadius() * ( iX * 2.0 - 1.0 );
            double y = node->getCenter( 1 ) + node->getRadius() * ( iY * 2.0 - 1.0 );
            double z = node->getCenter( 2 ) + node->getRadius() * ( iZ * 2.0 - 1.0 );
            outputMesh->addVertex( x, y, z );
85
            outputMesh->setVertexColor( index+vertex, highlightUsingColors ?color :osg::Vec4( 0.9, 0.9, 0.9, 1.0 ) );
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
        }
        // Z = 0
        outputMesh->addTriangle( index + 0, index + 2, index + 1 );
        outputMesh->addTriangle( index + 3, index + 1, index + 2 );
        // X = 0
        outputMesh->addTriangle( index + 0, index + 4, index + 2 );
        outputMesh->addTriangle( index + 4, index + 6, index + 2 );
        // Y = 0
        outputMesh->addTriangle( index + 0, index + 1, index + 4 );
        outputMesh->addTriangle( index + 1, index + 5, index + 4 );
        // Z = 1
        outputMesh->addTriangle( index + 4, index + 5, index + 6 );
        outputMesh->addTriangle( index + 5, index + 7, index + 6 );
        // X = 1
        outputMesh->addTriangle( index + 1, index + 3, index + 5 );
        outputMesh->addTriangle( index + 3, index + 7, index + 5 );
        // Y = 1
        outputMesh->addTriangle( index + 2, index + 6, index + 3 );
        outputMesh->addTriangle( index + 6, index + 7, index + 3 );
    }
    else
    {
        for  ( int child = 0; child < 8; child++ )
            if  ( node->getChild( child ) != 0 )
110
                drawNode( node->getChild( child ), outputMesh, octree, highlightUsingColors );
111 112
    }
}