WMDistanceMapIsosurface.cpp 6.96 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 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
//---------------------------------------------------------------------------
//
// 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 <stdint.h>
#include <string>
#include <algorithm>
#include <vector>

#include "WMDistanceMap.h"
#include "WMDistanceMapIsosurface.h"

#include "../../kernel/WKernel.h"
#include "../../kernel/WModuleFactory.h"
#include "../../dataHandler/WSubject.h"
#include "../../dataHandler/WGridRegular3D.h"

WMDistanceMapIsosurface::WMDistanceMapIsosurface():
    WModuleContainer( "Distance Map Isosurface",
                      "This description has to be improved when the module is completed."
                      " By now lets say the following: Computes a smoothed version of the dataset"
                      " and a distance map on it. Finally it renders this distance map using MarchinCubes" )
{
    // WARNING: initializing connectors inside the constructor will lead to an exception.
    // NOTE: Do not use the module factory inside this constructor. This will cause a dead lock as the module factory is locked
    // during construction of this instance and can then not be used to create another instance (Isosurface in this case). If you
    // want to initialize some modules using the module factory BEFORE the moduleMain() call, overwrite WModule::initialize().
}

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

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

void WMDistanceMapIsosurface::moduleMain()
{
    //////////////////////////////////////////////////////////////////////////////////
    // Marching Cubes
    //////////////////////////////////////////////////////////////////////////////////

    // create an instance using the prototypes
    m_marchingCubesModule = WModuleFactory::getModuleFactory()->create(  WModuleFactory::getModuleFactory()->getPrototypeByName( "Isosurface" ) );

    // add the marching cubes to the container
    add( m_marchingCubesModule );

    // now wait for it to be ready
    m_marchingCubesModule->isReady().wait();
75
    m_marchingCubesModule->getProperties()->findProp( "Iso Value" )->setValue< float >( 0.5 );
76 77
    m_properties->addProperty( m_marchingCubesModule->getProperties()->findProp( "active" ) );
    m_properties->addProperty( m_marchingCubesModule->getProperties()->findProp( "Iso Value" ) );
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

    //////////////////////////////////////////////////////////////////////////////////
    // Distance Map
    //////////////////////////////////////////////////////////////////////////////////

    // create a new instance of WMDistanceMap
    // NOTE: as the distance map is a "local" module, we can't use the module factory (as WMDistanceMap is not a prototype there).
    // The initialization of the module has to be done by the ModuleFactory to ensure proper initialization.
    m_distanceMapModule = boost::shared_ptr< WModule >( new WMDistanceMap() );
    WModuleFactory::initializeModule( m_distanceMapModule );

    // add it to the container
    add( m_distanceMapModule );

    // wait until it is ready
    m_distanceMapModule->isReady().wait();

    //////////////////////////////////////////////////////////////////////////////////
    // Hard wire both modules
    //////////////////////////////////////////////////////////////////////////////////

    // NOTE: you can use the WModuleContainer::applyModule functions here, which, in this case, is possible, since the connectors
    // can be connected unambiguously (one to one connection). But to show how hard wiring works, we do it manually here.
    m_marchingCubesModule->getInputConnector( "in" )->connect( m_distanceMapModule->getOutputConnector( "out" ) );
    // this is the same as doing it the other way around.
    // m_distanceMapModule->getOutputConnector( "out" )->connect( m_marchingCubesModule->getInputConnector( "in" ) );
    // simple, isn't it? ;-)

    //////////////////////////////////////////////////////////////////////////////////
    // Setup forwarding of this modules connectors with the contained ones
    //////////////////////////////////////////////////////////////////////////////////

    // connect the distance map output to the container output to ensure other modules can use the distance map if they want to
111
    m_output->forward( m_distanceMapModule->getOutputConnector( "out" ) );
112
    // we want the container input connector "in" to be connected to the input of WMDistanceMap
113
    m_input->forward( m_distanceMapModule->getInputConnector( "in" ) );
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133

    //////////////////////////////////////////////////////////////////////////////////
    // Done!
    //////////////////////////////////////////////////////////////////////////////////

    // signal ready state
    ready();

    // wait for stop request
    waitForStop();

    // stop container and the contained modules.
    stop();
}

void WMDistanceMapIsosurface::connectors()
{
    // initialize connectors

    // this is the scalar field input
134 135
    m_input = boost::shared_ptr< WModuleInputForwardData< WDataSetSingle > >(
        new WModuleInputForwardData< WDataSetSingle >( shared_from_this(),
136 137 138 139 140 141 142
                                                               "in", "Dataset to compute distance map for." )
        );

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

    // this output is used to provide the distance map to other modules.
143 144
    m_output = boost::shared_ptr< WModuleOutputForwardData< WDataSetSingle > >(
        new WModuleOutputForwardData< WDataSetSingle >( shared_from_this(),
145 146 147 148 149 150 151 152 153 154 155 156
                                                               "out", "Distance map for the input data set." )
        );

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

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

void WMDistanceMapIsosurface::properties()
{
157 158
}

159
void WMDistanceMapIsosurface::slotPropertyChanged( std::string /*propertyName*/ )
160
{
161 162
}