WMScalarSegmentation.cpp 5.27 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 <string>
#include <vector>

#include "../../kernel/WKernel.h"

#include "../../common/WColor.h"
#include "../../common/WPropertyHelper.h"

33
#include "WMScalarSegmentation.xpm"
34 35
#include "WMScalarSegmentation.h"

36 37 38
// This line is needed by the module loader to actually find your module.
W_LOADABLE_MODULE( WMScalarSegmentation )

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
WMScalarSegmentation::WMScalarSegmentation():
    WModule()
{
    m_algoIndex = 0;
}

WMScalarSegmentation::~WMScalarSegmentation()
{
}

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

const char** WMScalarSegmentation::getXPMIcon() const
{
    return scalarSegmentation_xpm;
}

const std::string WMScalarSegmentation::getName() const
{
Alexander Wiebel's avatar
Alexander Wiebel committed
61
    return "Scalar Segmentation";
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
}

const std::string WMScalarSegmentation::getDescription() const
{
    return "This module segments scalar datasets by threshold.";
}

void WMScalarSegmentation::connectors()
{
    m_input = boost::shared_ptr< WModuleInputData < WDataSetScalar  > >(
        new WModuleInputData< WDataSetScalar >( shared_from_this(), "inputSet", "The dataset to segment." )
        );

    addConnector( m_input );

    m_output = boost::shared_ptr< WModuleOutputData < WDataSetScalar  > >(
        new WModuleOutputData< WDataSetScalar >( shared_from_this(), "outputSet", "The calculated dataset." )
        );

    addConnector( m_output );

    WModule::connectors();
}

void WMScalarSegmentation::properties()
{
    m_propCondition = boost::shared_ptr< WCondition >( new WCondition() );

    m_algos = boost::shared_ptr< WItemSelection >( new WItemSelection );
    m_algos->addItem( "Simple Threshold", "" );
92
    m_algos->addItem( "Watershed", "" );
93 94 95

    m_algoType = m_properties->addProperty( "Seg Algo", "Choose a segmentation method.", m_algos->getSelectorFirst(), m_propCondition );

96 97
    m_threshold = m_properties->addProperty( "Threshold", "Threshold value for segmentation.", 10.0, m_propCondition );
    m_threshold->setMax( 100.0 );
98
    m_threshold->setMin( 0.0 );
99 100

    WModule::properties();
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
}

void WMScalarSegmentation::moduleMain()
{
    debugLog() << "Entering moduleMain()";

    m_moduleState.setResetable( true, true );
    m_moduleState.add( m_input->getDataChangedCondition() );
    m_moduleState.add( m_propCondition );

    ready();

    while ( !m_shutdownFlag() )
    {
        m_moduleState.wait();

        if ( m_shutdownFlag() )
        {
            break;
        }

        boost::shared_ptr< WDataSetScalar > newDataSet = m_input->getData();
        bool dataChanged = ( m_dataSet != newDataSet );
        bool dataValid   = ( newDataSet );

126
        if( dataChanged && dataValid )
127 128
        {
            m_dataSet = newDataSet;
129 130 131 132 133
            WAssert( m_dataSet, "" );
            WAssert( m_dataSet->getValueSet(), "" );
            WAssert( m_dataSet->getGrid(), "" );
            WAssert( m_dataSet->getValueSet()->dimension() == 1, "" );
            WAssert( m_dataSet->getValueSet()->order() == 0, "" );
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
        }

        //bool propChanged = false;
        //if( !m_algoType->changed() )
        //{
            //WPVGroup::PropertyConstIterator it;
            //for( it = m_propGroups[ m_algoIndex ]->begin(); it != m_propGroups[ m_algoIndex ]->end(); ++it )
            //{
                //propChanged = propChanged || it->changed();
            //}
        //}
        //else
        //{
            //WItemSelector w = m_algoType.get( true );
            //m_algoIndex = w.getItemIndexOfSelected( 0 );
            //propChanged = true;
        //}

        bool propChanged = m_threshold->changed();
153
        if( m_dataSet && ( dataChanged || propChanged ) )
154 155
        {
            // redo calculation
156
            doSegmentation();
157 158 159 160 161 162 163 164 165 166
            m_output->updateData( m_result );
        }
    }
}

void WMScalarSegmentation::activate()
{
    WModule::activate();
}

167
void WMScalarSegmentation::doSegmentation()
168
{
169
    boost::shared_ptr< WValueSetBase > vs;
170 171 172
    switch( m_algoIndex )
    {
    case 0:
173 174
        vs = m_dataSet->getValueSet()->applyFunction( SimpleSegmentation( m_threshold->get( true ) ) );
        break;
175
    default:
176
        errorLog() << "Invalid algorithm selected." << std::endl;
177
    }
178
    m_result = boost::shared_ptr< WDataSetScalar >( new WDataSetScalar( vs, m_dataSet->getGrid() ) );
179
}