~ubuntu-branches/ubuntu/vivid/openwalnut/vivid

« back to all changes in this revision

Viewing changes to .pc/boost153/src/modules/applyMask/WMApplyMask.cpp

  • Committer: Package Import Robot
  • Author(s): Sebastian Eichelbaum
  • Date: 2014-03-19 17:46:12 UTC
  • mfrom: (3.1.2 sid)
  • Revision ID: package-import@ubuntu.com-20140319174612-e4mgtr1avbq3f7ph
Tags: 1.4.0~rc1+hg3a3147463ee2-1
* Major functionality and stability improvements.
* Several bug fixes
* Changed ttf-liberation dependency to fonts-liberation (Closes: #722405)
* OpenWalnut now works properly with OpenSceneGraph 3.2 (Closes: #718381)
* See http://www.openwalnut.org/versions/2

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 <stdint.h>
26
 
 
27
 
#include <iostream>
28
 
#include <fstream>
29
 
#include <string>
30
 
#include <vector>
31
 
#include <utility>
32
 
 
33
 
#include <cmath>
34
 
 
35
 
#include "core/common/WProgress.h"
36
 
#include "core/common/WAssert.h"
37
 
#include "core/kernel/WKernel.h"
38
 
#include "WMApplyMask.h"
39
 
#include "WMApplyMask.xpm"
40
 
 
41
 
// This line is needed by the module loader to actually find your module.
42
 
W_LOADABLE_MODULE( WMApplyMask )
43
 
 
44
 
WMApplyMask::WMApplyMask() :
45
 
    WModule()
46
 
{
47
 
    // WARNING: initializing connectors inside the constructor will lead to an exception.
48
 
    // Implement WModule::initializeConnectors instead.
49
 
}
50
 
 
51
 
WMApplyMask::~WMApplyMask()
52
 
{
53
 
    // cleanup
54
 
    removeConnectors();
55
 
}
56
 
 
57
 
boost::shared_ptr< WModule > WMApplyMask::factory() const
58
 
{
59
 
    return boost::shared_ptr< WModule >( new WMApplyMask() );
60
 
}
61
 
 
62
 
const char** WMApplyMask::getXPMIcon() const
63
 
{
64
 
    return apply_mask_xpm;
65
 
}
66
 
 
67
 
const std::string WMApplyMask::getName() const
68
 
{
69
 
    return "Apply Mask";
70
 
}
71
 
 
72
 
const std::string WMApplyMask::getDescription() const
73
 
{
74
 
    return "Applies a mask to a data set, i.e. sets all voxels to zero which are zero in the mask.";
75
 
}
76
 
 
77
 
void WMApplyMask::moduleMain()
78
 
{
79
 
    // use the m_input "data changed" flag
80
 
    m_moduleState.setResetable( true, true );
81
 
    m_moduleState.add( m_dataInput->getDataChangedCondition() );
82
 
    m_moduleState.add( m_maskInput->getDataChangedCondition() );
83
 
 
84
 
    // signal ready state
85
 
    ready();
86
 
 
87
 
    // loop until the module container requests the module to quit
88
 
    while( !m_shutdownFlag() )
89
 
    {
90
 
        // acquire data from the input connector
91
 
        m_dataSet = m_dataInput->getData();
92
 
        m_mask = m_maskInput->getData();
93
 
        if( !m_dataSet || !m_mask )
94
 
        {
95
 
            // ok, the output has not yet sent data
96
 
            // NOTE: see comment at the end of this while loop for m_moduleState
97
 
            debugLog() << "Waiting for data ...";
98
 
            m_moduleState.wait();
99
 
            continue;
100
 
        }
101
 
        dataType type = m_dataSet->getValueSet()->getDataType();
102
 
        switch( type )
103
 
        {
104
 
            case W_DT_UNSIGNED_CHAR:
105
 
            {
106
 
                boost::shared_ptr< WValueSet< unsigned char > > vals;
107
 
                vals = boost::shared_dynamic_cast< WValueSet< unsigned char > >( ( *m_dataSet ).getValueSet() );
108
 
                WAssert( vals, "Data type and data type indicator must fit." );
109
 
                applyMask( vals, type );
110
 
                break;
111
 
            }
112
 
            case W_DT_INT16:
113
 
            {
114
 
                boost::shared_ptr< WValueSet< int16_t > > vals;
115
 
                vals = boost::shared_dynamic_cast< WValueSet< int16_t > >( ( *m_dataSet ).getValueSet() );
116
 
                WAssert( vals, "Data type and data type indicator must fit." );
117
 
                applyMask( vals, type );
118
 
                break;
119
 
            }
120
 
            case W_DT_SIGNED_INT:
121
 
            {
122
 
                boost::shared_ptr< WValueSet< int32_t > > vals;
123
 
                vals = boost::shared_dynamic_cast< WValueSet< int32_t > >( ( *m_dataSet ).getValueSet() );
124
 
                WAssert( vals, "Data type and data type indicator must fit." );
125
 
                applyMask( vals, type );
126
 
                break;
127
 
            }
128
 
            case W_DT_FLOAT:
129
 
            {
130
 
                boost::shared_ptr< WValueSet< float > > vals;
131
 
                vals = boost::shared_dynamic_cast< WValueSet< float > >( ( *m_dataSet ).getValueSet() );
132
 
                WAssert( vals, "Data type and data type indicator must fit." );
133
 
                applyMask( vals, type );
134
 
                break;
135
 
            }
136
 
            case W_DT_DOUBLE:
137
 
            {
138
 
                boost::shared_ptr< WValueSet< double > > vals;
139
 
                vals = boost::shared_dynamic_cast< WValueSet< double > >( ( *m_dataSet ).getValueSet() );
140
 
                WAssert( vals, "Data type and data type indicator must fit." );
141
 
                applyMask( vals, type );
142
 
                break;
143
 
            }
144
 
            default:
145
 
                throw WException( std::string( "Data type of value set not supported by this module." ) );
146
 
        }
147
 
 
148
 
        // this waits for m_moduleState to fire. By default, this is only the m_shutdownFlag condition.
149
 
        // NOTE: you can add your own conditions to m_moduleState using m_moduleState.add( ... )
150
 
        m_moduleState.wait();
151
 
    }
152
 
}
153
 
 
154
 
void WMApplyMask::connectors()
155
 
{
156
 
    // initialize connectors
157
 
    m_dataInput = boost::shared_ptr< WModuleInputData< WDataSetScalar > >( new WModuleInputData< WDataSetScalar >(
158
 
                    shared_from_this(), "dataSet", "The dataset to apply the mask to." ) );
159
 
 
160
 
    // add it to the list of connectors. Please note, that a connector NOT added via addConnector will not work as expected.
161
 
    addConnector( m_dataInput );
162
 
 
163
 
    // initialize connectors
164
 
    m_maskInput = boost::shared_ptr< WModuleInputData< WDataSetScalar > >(
165
 
            new WModuleInputData< WDataSetScalar >( shared_from_this(), "mask",
166
 
                    "The mask applied to the data." ) );
167
 
 
168
 
    // add it to the list of connectors. Please note, that a connector NOT added via addConnector will not work as expected.
169
 
    addConnector( m_maskInput );
170
 
 
171
 
    // initialize connectors
172
 
    m_output = boost::shared_ptr< WModuleOutputData< WDataSetScalar > >(
173
 
            new WModuleOutputData< WDataSetScalar >( shared_from_this(), "out",
174
 
                    "The filtered data set." ) );
175
 
 
176
 
    // add it to the list of connectors. Please note, that a connector NOT added via addConnector will not work as expected.
177
 
    addConnector( m_output );
178
 
 
179
 
    // call WModules initialization
180
 
    WModule::connectors();
181
 
}
182
 
 
183
 
void WMApplyMask::properties()
184
 
{
185
 
    WModule::properties();
186
 
}
187
 
 
188
 
template< typename T > void WMApplyMask::applyMask( boost::shared_ptr< WValueSet< T > > valSet, dataType type )
189
 
{
190
 
    boost::shared_ptr< WValueSetBase > maskBase = m_mask->getValueSet();
191
 
    boost::shared_ptr< WValueSet< float > > mask = boost::shared_dynamic_cast< WValueSet< float > >( maskBase );
192
 
 
193
 
    if( !mask )
194
 
    {
195
 
        throw WException( std::string( "Mask is not of type float." ) );
196
 
    }
197
 
 
198
 
    boost::shared_ptr< WProgress > progress = boost::shared_ptr< WProgress >( new WProgress( "Apply Mask", valSet->size() ) );
199
 
    m_progress->addSubProgress( progress );
200
 
 
201
 
    boost::shared_ptr< std::vector< T > > newVals = boost::shared_ptr< std::vector< T > >( new std::vector< T >( valSet->size() ) );
202
 
    for( size_t i = 0; i < valSet->size(); ++i )
203
 
    {
204
 
        ++*progress;
205
 
        if( mask->getScalar( i ) == 0 )
206
 
        {
207
 
            ( *newVals )[i] = 0;
208
 
        }
209
 
        else
210
 
        {
211
 
            ( *newVals )[i] = valSet->getScalar( i );
212
 
        }
213
 
    }
214
 
    progress->finish();
215
 
 
216
 
    boost::shared_ptr< WValueSet< T > > valueSet;
217
 
    valueSet = boost::shared_ptr< WValueSet< T > >( new WValueSet< T >( 0, 1, newVals, type ) );
218
 
 
219
 
    m_dataSetOut = boost::shared_ptr< WDataSetScalar >( new WDataSetScalar( valueSet, m_dataSet->getGrid() ) );
220
 
    m_output->updateData( m_dataSetOut );
221
 
}