~ubuntu-branches/ubuntu/precise/openwalnut/precise

« back to all changes in this revision

Viewing changes to src/modules/scalarSegmentation/WMScalarSegmentation.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Sebastian Eichelbaum
  • Date: 2011-06-21 10:26:54 UTC
  • Revision ID: james.westby@ubuntu.com-20110621102654-rq0zf436q949biih
Tags: upstream-1.2.5
ImportĀ upstreamĀ versionĀ 1.2.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//---------------------------------------------------------------------------
 
2
//
 
3
// Project: OpenWalnut ( http://www.openwalnut.org )
 
4
//
 
5
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
 
6
// For more information see http://www.openwalnut.org/copying
 
7
//
 
8
// This file is part of OpenWalnut.
 
9
//
 
10
// OpenWalnut is free software: you can redistribute it and/or modify
 
11
// it under the terms of the GNU Lesser General Public License as published by
 
12
// the Free Software Foundation, either version 3 of the License, or
 
13
// (at your option) any later version.
 
14
//
 
15
// OpenWalnut is distributed in the hope that it will be useful,
 
16
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
17
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
18
// GNU Lesser General Public License for more details.
 
19
//
 
20
// You should have received a copy of the GNU Lesser General Public License
 
21
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
 
22
//
 
23
//---------------------------------------------------------------------------
 
24
 
 
25
#include <string>
 
26
#include <vector>
 
27
 
 
28
#include "core/kernel/WKernel.h"
 
29
 
 
30
#include "core/common/WColor.h"
 
31
#include "core/common/WPropertyHelper.h"
 
32
 
 
33
#include "WMScalarSegmentation.xpm"
 
34
#include "WMScalarSegmentation.h"
 
35
 
 
36
// This line is needed by the module loader to actually find your module.
 
37
W_LOADABLE_MODULE( WMScalarSegmentation )
 
38
 
 
39
WMScalarSegmentation::WMScalarSegmentation():
 
40
    WModule()
 
41
{
 
42
    m_algoIndex = 0;
 
43
    m_algos.push_back( boost::shared_ptr< WSegmentationAlgo >( new WSegmentationAlgoThreshold() ) );
 
44
#ifdef OW_USE_ITK
 
45
    m_algos.push_back( boost::shared_ptr< WSegmentationAlgo >( new WSegmentationAlgoWatershed() ) );
 
46
    m_algos.push_back( boost::shared_ptr< WSegmentationAlgo >( new WSegmentationAlgoOtsu() ) );
 
47
    m_algos.push_back( boost::shared_ptr< WSegmentationAlgo >( new WSegmentationAlgoRegionGrowingConfidenceConnected() ) );
 
48
    m_algos.push_back( boost::shared_ptr< WSegmentationAlgo >( new WSegmentationAlgoLevelSetCanny() ) );
 
49
#endif  // OW_USE_ITK
 
50
}
 
51
 
 
52
WMScalarSegmentation::~WMScalarSegmentation()
 
53
{
 
54
    m_algos.clear();
 
55
}
 
56
 
 
57
boost::shared_ptr< WModule > WMScalarSegmentation::factory() const
 
58
{
 
59
    return boost::shared_ptr< WModule >( new WMScalarSegmentation() );
 
60
}
 
61
 
 
62
const char** WMScalarSegmentation::getXPMIcon() const
 
63
{
 
64
    return scalarSegmentation_xpm;
 
65
}
 
66
 
 
67
const std::string WMScalarSegmentation::getName() const
 
68
{
 
69
    return "Scalar Segmentation";
 
70
}
 
71
 
 
72
const std::string WMScalarSegmentation::getDescription() const
 
73
{
 
74
    return "This module segments scalar datasets.";
 
75
}
 
76
 
 
77
void WMScalarSegmentation::connectors()
 
78
{
 
79
    m_input = boost::shared_ptr< WModuleInputData < WDataSetScalar  > >(
 
80
        new WModuleInputData< WDataSetScalar >( shared_from_this(), "inputSet", "The dataset to segment." )
 
81
        );
 
82
 
 
83
    addConnector( m_input );
 
84
 
 
85
    m_output = boost::shared_ptr< WModuleOutputData < WDataSetScalar  > >(
 
86
        new WModuleOutputData< WDataSetScalar >( shared_from_this(), "outputSet", "The calculated dataset." )
 
87
        );
 
88
 
 
89
    addConnector( m_output );
 
90
 
 
91
    WModule::connectors();
 
92
}
 
93
 
 
94
void WMScalarSegmentation::properties()
 
95
{
 
96
    m_propCondition = boost::shared_ptr< WCondition >( new WCondition() );
 
97
 
 
98
    m_algoSelection = boost::shared_ptr< WItemSelection >( new WItemSelection );
 
99
    for( AlgoList::iterator it = m_algos.begin(); it != m_algos.end(); ++it )
 
100
    {
 
101
        m_algoSelection->addItem( ( *it )->getName(), ( *it )->getDescription() );
 
102
    }
 
103
    m_algoType = m_properties->addProperty( "Segmentation algorithm", "Choose a segmentation method.",
 
104
                                            m_algoSelection->getSelectorFirst(), m_propCondition );
 
105
 
 
106
    for( AlgoList::iterator it = m_algos.begin(); it != m_algos.end(); ++it )
 
107
    {
 
108
        ( *it )->initProperties( m_properties->addPropertyGroup( ( *it )->getName(), "The properties for this segmentation algorithm.", true ) );
 
109
    }
 
110
 
 
111
    m_algos.at( 0 )->hideProperties( false );
 
112
 
 
113
    WPropertyHelper::PC_SELECTONLYONE::addTo( m_algoType );
 
114
    WPropertyHelper::PC_NOTEMPTY::addTo( m_algoType );
 
115
 
 
116
    WModule::properties();
 
117
}
 
118
 
 
119
void WMScalarSegmentation::moduleMain()
 
120
{
 
121
    m_moduleState.setResetable( true, true );
 
122
    m_moduleState.add( m_input->getDataChangedCondition() );
 
123
    m_moduleState.add( m_propCondition );
 
124
    for( AlgoList::iterator it = m_algos.begin(); it != m_algos.end(); ++it )
 
125
    {
 
126
        m_moduleState.add( ( *it )->getCondition() );
 
127
    }
 
128
 
 
129
    ready();
 
130
 
 
131
    while( !m_shutdownFlag() )
 
132
    {
 
133
        m_moduleState.wait();
 
134
 
 
135
        if( m_shutdownFlag() )
 
136
        {
 
137
            break;
 
138
        }
 
139
 
 
140
        boost::shared_ptr< WDataSetScalar > newDataSet = m_input->getData();
 
141
        bool dataChanged = ( m_dataSet != newDataSet );
 
142
        bool dataValid   = ( newDataSet );
 
143
 
 
144
        if( dataChanged && dataValid )
 
145
        {
 
146
            m_dataSet = newDataSet;
 
147
            if( !m_dataSet->getValueSet() || !m_dataSet->getGrid()
 
148
                || m_dataSet->getValueSet()->dimension() != 1 || m_dataSet->getValueSet()->order() != 0 )
 
149
            {
 
150
                m_dataSet = boost::shared_ptr< WDataSetScalar >();
 
151
            }
 
152
        }
 
153
 
 
154
        bool algoChanged = m_algoType->changed();
 
155
 
 
156
        if( algoChanged )
 
157
        {
 
158
            WItemSelector w = m_algoType->get( true );
 
159
            m_algos.at( m_algoIndex )->hideProperties( true );
 
160
            m_algoIndex = w.getItemIndexOfSelected( 0 );
 
161
            m_algos.at( m_algoIndex )->hideProperties( false );
 
162
        }
 
163
 
 
164
        bool propChanged = m_algos.at( m_algoIndex )->propChanged();
 
165
        if( m_dataSet && ( dataChanged || propChanged || algoChanged ) )
 
166
        {
 
167
            // redo calculation
 
168
            doSegmentation();
 
169
            m_output->updateData( m_result );
 
170
        }
 
171
    }
 
172
 
 
173
    for( AlgoList::iterator it = m_algos.begin(); it != m_algos.end(); ++it )
 
174
    {
 
175
        m_moduleState.remove( ( *it )->getCondition() );
 
176
    }
 
177
}
 
178
 
 
179
void WMScalarSegmentation::activate()
 
180
{
 
181
    WModule::activate();
 
182
}
 
183
 
 
184
void WMScalarSegmentation::doSegmentation()
 
185
{
 
186
    debugLog() << "Starting segmentation.";
 
187
    m_result = m_algos.at( m_algoIndex )->segment( m_dataSet );
 
188
    debugLog() << "Segmentation finished.";
 
189
}