WMBoundingBox.cpp 4.86 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
#include "../../common/WAssert.h"
34 35
#include "../../common/WStringUtils.h"
#include "../../dataHandler/WGridRegular3D.h"
36
#include "../../graphicsEngine/WGEGeodeUtils.h"
37
#include "../../kernel/WKernel.h"
38 39
#include "../../common/math/WPosition.h"
#include "../../common/math/WVector3D.h"
40 41
#include "../data/WMData.h"
#include "WMBoundingBox.h"
42
#include "boundingbox.xpm"
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61

WMBoundingBox::WMBoundingBox():
    WModule()
{
    // 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() );
}

62 63 64 65 66
const char** WMBoundingBox::getXPMIcon() const
{
    return boundingbox_xpm;
}

67 68 69 70 71 72 73 74 75 76 77 78 79
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
80
    m_moduleState.setResetable( true, true );
81 82 83 84 85 86 87 88 89 90
    m_moduleState.add( m_input->getDataChangedCondition() );

    // signal ready state
    ready();

    // loop until the module container requests the module to quit
    while ( !m_shutdownFlag() )
    {
        // acquire data from the input connector
        m_dataSet = m_input->getData();
91
        if ( !m_dataSet.get() )
92
        {
93
            // OK, the output has not yet sent data
94 95 96 97 98 99 100 101
            // NOTE: see comment at the end of this while loop for m_moduleState
            debugLog() << "Waiting for data ...";
            m_moduleState.wait();
            continue;
        }

        boost::shared_ptr< WGridRegular3D > grid = boost::shared_dynamic_cast< WGridRegular3D >( m_dataSet->getGrid() );

102
        WAssert( grid, "Seems that grid is of wrong type." );
103 104 105 106

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

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

109 110 111 112 113 114 115 116
        m_bBoxNode->insert( wge::generateBoundingBoxGeode( bb.first, bb.second, WColor( 0.3, 0.3, 0.3, 1 ) ) );

        WGraphicsEngine::getGraphicsEngine()->getScene()->insert( m_bBoxNode );

        // 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();
    }
117 118

    WGraphicsEngine::getGraphicsEngine()->getScene()->remove( m_bBoxNode );
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
}

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()
{
//    ( m_properties->addInt( "Thickness", 1 ) )->connect( boost::bind( &WMBoundingBox::slotPropertyChanged, this, _1 ) );
140

141 142
    // m_active gets initialized in WModule and is available for all modules. Overwrite activate() to have a special callback for m_active
    // changes.
143
}
144 145 146

void WMBoundingBox::activate()
{
147
    if ( m_bBoxNode )
148
    {
149 150 151 152 153 154 155 156
        if ( m_active->get() )
        {
            m_bBoxNode->setNodeMask( 0xFFFFFFFF );
        }
        else
        {
            m_bBoxNode->setNodeMask( 0x0 );
        }
157
    }
158
    WModule::activate();
159 160
}