WMBoundingBox.cpp 6.13 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 32
//---------------------------------------------------------------------------
//
// 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 <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <utility>

#include <cmath>

33 34 35 36
#include <osg/MatrixTransform>
#include <osg/PositionAttitudeTransform>
#include <osgText/Text>

37
#include "../../common/WAssert.h"
38 39
#include "../../common/WStringUtils.h"
#include "../../dataHandler/WGridRegular3D.h"
40
#include "../../graphicsEngine/WGEGeodeUtils.h"
41
#include "../../kernel/WKernel.h"
42 43
#include "../../common/math/WPosition.h"
#include "../../common/math/WVector3D.h"
44
#include "../../kernel/modules/data/WMData.h"
45
#include "WMBoundingBox.h"
46
#include "WMBoundingBox.xpm"
47

48 49 50
// This line is needed by the module loader to actually find your module.
W_LOADABLE_MODULE( WMBoundingBox )

51
WMBoundingBox::WMBoundingBox():
52 53
    WModule(),
    m_recompute( boost::shared_ptr< WCondition >( new WCondition() ) )
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
{
    // WARNING: initializing connectors inside the constructor will lead to an exception.
    // Implement WModule::initializeConnectors instead.
}

WMBoundingBox::~WMBoundingBox()
{
    // cleanup
    removeConnectors();
}

boost::shared_ptr< WModule > WMBoundingBox::factory() const
{
    return boost::shared_ptr< WModule >( new WMBoundingBox() );
}

70 71 72 73 74
const char** WMBoundingBox::getXPMIcon() const
{
    return boundingbox_xpm;
}

75 76 77 78 79 80 81 82 83 84 85 86 87
const std::string WMBoundingBox::getName() const
{
    return "Bounding Box";
}

const std::string WMBoundingBox::getDescription() const
{
    return "Shows the bounding box of a data set.";
}

void WMBoundingBox::moduleMain()
{
    // use the m_input "data changed" flag
88
    m_moduleState.setResetable( true, true );
89
    m_moduleState.add( m_input->getDataChangedCondition() );
90
    m_moduleState.add( m_recompute );
91 92 93 94 95

    // signal ready state
    ready();

    // loop until the module container requests the module to quit
Alexander Wiebel's avatar
[STYLE]  
Alexander Wiebel committed
96
    while( !m_shutdownFlag() )
97
    {
98
        boost::shared_ptr< WDataSetSingle > dataSet = m_input->getData();
99 100
        bool dataValid = ( dataSet );

Alexander Wiebel's avatar
[STYLE]  
Alexander Wiebel committed
101
        if( !dataValid )
102
        {
103
            // OK, the output has not yet sent data
104 105 106 107 108 109
            // NOTE: see comment at the end of this while loop for m_moduleState
            debugLog() << "Waiting for data ...";
            m_moduleState.wait();
            continue;
        }

110
        createGFX();
111 112 113 114 115 116 117 118

        // this waits for m_moduleState to fire. By default, this is only the m_shutdownFlag condition.
        // NOTE: you can add your own conditions to m_moduleState using m_moduleState.add( ... )
        m_moduleState.wait();
    }

    WGraphicsEngine::getGraphicsEngine()->getScene()->remove( m_bBoxNode );
}
119

120 121 122
void WMBoundingBox::createGFX()
{
    boost::shared_ptr< WDataSetSingle > dataSet = m_input->getData();
123

124
    boost::shared_ptr< WGridRegular3D > grid = boost::shared_dynamic_cast< WGridRegular3D >( dataSet->getGrid() );
125

126
    WAssert( grid, "Seems that grid is of wrong type." );
127

128
    WGraphicsEngine::getGraphicsEngine()->getScene()->remove( m_bBoxNode );
129

130
    std::pair< wmath::WPosition, wmath::WPosition > bb = grid->getBoundingBox();
131

132 133
    m_bBoxNode = osg::ref_ptr< WGEGroupNode >( new WGEGroupNode );
    m_bBoxNode->setNodeMask( m_active->get() ? 0xFFFFFFFF : 0x0 );
134

135
    m_bBoxNode->insert( wge::generateBoundingBoxGeode( bb.first, bb.second, WColor( 0.3, 0.3, 0.3, 1 ) ) );
136

137 138 139 140 141 142 143 144 145 146 147 148 149
    if( m_showCornerCoordinates->get( true ) )
    {
        wmath::WVector3D pos1 = bb.first;
        wmath::WVector3D pos2 = bb.second;
        m_bBoxNode->addChild( wge::vector2label( osg::Vec3( pos1[0], pos1[1], pos1[2] ) ) );
        m_bBoxNode->addChild( wge::vector2label( osg::Vec3( pos2[0], pos2[1], pos2[2] ) ) );
        m_bBoxNode->addChild( wge::vector2label( osg::Vec3( pos2[0], pos2[1], pos1[2] ) ) );
        m_bBoxNode->addChild( wge::vector2label( osg::Vec3( pos1[0], pos2[1], pos1[2] ) ) );
        m_bBoxNode->addChild( wge::vector2label( osg::Vec3( pos2[0], pos1[1], pos1[2] ) ) );
        m_bBoxNode->addChild( wge::vector2label( osg::Vec3( pos1[0], pos2[1], pos2[2] ) ) );
        m_bBoxNode->addChild( wge::vector2label( osg::Vec3( pos2[0], pos1[1], pos2[2] ) ) );
        m_bBoxNode->addChild( wge::vector2label( osg::Vec3( pos1[0], pos1[1], pos2[2] ) ) );
    }
150

151

152
    WGraphicsEngine::getGraphicsEngine()->getScene()->insert( m_bBoxNode );
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
}

void WMBoundingBox::connectors()
{
    // initialize connectors
    m_input = boost::shared_ptr< WModuleInputData < WDataSetSingle  > >(
        new WModuleInputData< WDataSetSingle >( shared_from_this(),
                                                               "in", "The dataset to filter" )
        );

    // add it to the list of connectors. Please note, that a connector NOT added via addConnector will not work as expected.
    addConnector( m_input );


    // call WModules initialization
    WModule::connectors();
}

void WMBoundingBox::properties()
{
173
    m_showCornerCoordinates = m_properties->addProperty( "Show coordinates", "Show coordinates at the corners of the box.", true, m_recompute );
174
    WModule::properties();
175
}
176 177 178

void WMBoundingBox::activate()
{
Alexander Wiebel's avatar
[STYLE]  
Alexander Wiebel committed
179
    if( m_bBoxNode )
180
    {
Alexander Wiebel's avatar
[STYLE]  
Alexander Wiebel committed
181
        if( m_active->get() )
182 183 184 185 186 187 188
        {
            m_bBoxNode->setNodeMask( 0xFFFFFFFF );
        }
        else
        {
            m_bBoxNode->setNodeMask( 0x0 );
        }
189
    }
190
    WModule::activate();
191
}