~noskcaj/ubuntu/saucy/openwalnut/liberation

« back to all changes in this revision

Viewing changes to .pc/boost153/src/modules/data/WMData.cpp

  • Committer: Package Import Robot
  • Author(s): Dmitrijs Ledkovs
  • Date: 2013-05-24 03:12:03 UTC
  • Revision ID: package-import@ubuntu.com-20130524031203-l5g1lzm1vd83fupi
Tags: 1.3.1+hg5849-1ubuntu1
Cherrypick boost1.53 pointer cast fixes.

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/common/WAssert.h"
 
29
#include "core/common/WIOTools.h"
 
30
#include "core/common/WPropertyHelper.h"
 
31
#include "core/dataHandler/WDataSet.h"
 
32
#include "core/dataHandler/WDataSetSingle.h"
 
33
#include "core/dataHandler/WDataSetScalar.h"
 
34
#include "core/dataHandler/WDataSetTimeSeries.h"
 
35
#include "core/dataHandler/WDataSetVector.h"
 
36
#include "core/dataHandler/WSubject.h"
 
37
#include "core/dataHandler/WDataHandler.h"
 
38
#include "core/dataHandler/WDataTexture3D.h"
 
39
#include "core/dataHandler/WEEG2.h"
 
40
#include "core/dataHandler/exceptions/WDHException.h"
 
41
#include "core/graphicsEngine/WGEColormapping.h"
 
42
#include "core/kernel/WModuleOutputData.h"
 
43
 
 
44
#include "io/WReaderBiosig.h"
 
45
#include "io/WReaderEEGASCII.h"
 
46
#include "io/WReaderNIfTI.h"
 
47
#include "io/WReaderELC.h"
 
48
#include "io/WReaderFiberVTK.h"
 
49
#include "io/WReaderLibeep.h"
 
50
#include "io/WPagerEEGLibeep.h"
 
51
 
 
52
#include "WMData.h"
 
53
#include "WMData.xpm"
 
54
 
 
55
WMData::WMData():
 
56
    WDataModule(),
 
57
    m_fileNameSet( false ),
 
58
    m_isTexture(),
 
59
    m_transformNoMatrix( 4, 4 ),
 
60
    m_transformSForm( 4, 4 ),
 
61
    m_transformQForm( 4, 4 )
 
62
{
 
63
    // initialize members
 
64
}
 
65
 
 
66
WMData::~WMData()
 
67
{
 
68
    // cleanup
 
69
}
 
70
 
 
71
boost::shared_ptr< WModule > WMData::factory() const
 
72
{
 
73
    return boost::shared_ptr< WModule >( new WMData() );
 
74
}
 
75
 
 
76
const char** WMData::getXPMIcon() const
 
77
{
 
78
    return WMData_xpm;
 
79
}
 
80
 
 
81
const std::string WMData::getName() const
 
82
{
 
83
    return "Data Module";
 
84
}
 
85
 
 
86
const std::string WMData::getDescription() const
 
87
{
 
88
    return "This module encapsulates data.";
 
89
}
 
90
 
 
91
boost::shared_ptr< WDataSet > WMData::getDataSet()
 
92
{
 
93
    return m_dataSet;
 
94
}
 
95
 
 
96
void WMData::setFilename( boost::filesystem::path fname )
 
97
{
 
98
    if( !m_fileNameSet )
 
99
    {
 
100
        m_fileNameSet = true;
 
101
        m_fileName = fname;
 
102
    }
 
103
}
 
104
 
 
105
boost::filesystem::path WMData::getFilename() const
 
106
{
 
107
    return m_fileName;
 
108
}
 
109
 
 
110
void WMData::connectors()
 
111
{
 
112
    // initialize connectors
 
113
    m_output= boost::shared_ptr< WModuleOutputData< WDataSet > >( new WModuleOutputData< WDataSet >(
 
114
                shared_from_this(), "out", "A loaded dataset." )
 
115
            );
 
116
 
 
117
    // add it to the list of connectors. Please note, that a connector NOT added via addConnector will not work as expected.
 
118
    addConnector( m_output );
 
119
 
 
120
    // call WModules initialization
 
121
    WDataModule::connectors();
 
122
}
 
123
 
 
124
void WMData::properties()
 
125
{
 
126
    m_propCondition = boost::shared_ptr< WCondition >( new WCondition() );
 
127
 
 
128
    // properties
 
129
    m_dataName = m_infoProperties->addProperty( "Filename", "The filename of the dataset.", std::string( "" ) );
 
130
    m_dataType = m_infoProperties->addProperty( "Data type", "The type of the the single data values.", std::string( "" ) );
 
131
 
 
132
    m_matrixSelectionsList = boost::shared_ptr< WItemSelection >( new WItemSelection() );
 
133
    m_matrixSelectionsList->addItem( "No matrix", "" );
 
134
    m_matrixSelectionsList->addItem( "sform", "" );
 
135
    m_matrixSelectionsList->addItem( "qform", "" );
 
136
 
 
137
    m_matrixSelection = m_properties->addProperty( "Transformation matrix",  "matrix",
 
138
            m_matrixSelectionsList->getSelectorFirst(), m_propCondition );
 
139
    WPropertyHelper::PC_SELECTONLYONE::addTo( m_matrixSelection );
 
140
 
 
141
    // use this callback for the other properties
 
142
    WPropertyBase::PropertyChangeNotifierType propertyCallback = boost::bind( &WMData::propertyChanged, this, _1 );
 
143
}
 
144
 
 
145
void WMData::propertyChanged( boost::shared_ptr< WPropertyBase > property )
 
146
{
 
147
    if( m_isTexture )
 
148
    {
 
149
        if( property == m_active )
 
150
        {
 
151
            // forward to texture
 
152
            m_dataSet->getTexture()->active()->set( m_active->get( true ) );
 
153
        }
 
154
    }
 
155
    else
 
156
    {
 
157
        if( property == m_active )
 
158
        {
 
159
            if( m_active->get() )
 
160
            {
 
161
                m_output->updateData( m_dataSet );
 
162
            }
 
163
            else
 
164
            {
 
165
                m_output->updateData( boost::shared_ptr< WDataSet >() );
 
166
            }
 
167
        }
 
168
    }
 
169
}
 
170
 
 
171
void WMData::moduleMain()
 
172
{
 
173
    WAssert( m_fileNameSet, "No filename specified." );
 
174
 
 
175
    m_transformNoMatrix.makeIdentity();
 
176
    m_transformSForm.makeIdentity();
 
177
    m_transformQForm.makeIdentity();
 
178
 
 
179
    m_moduleState.setResetable( true, true );
 
180
    m_moduleState.add( m_propCondition );
 
181
 
 
182
    std::string fileName = m_fileName.string();
 
183
 
 
184
    debugLog() << "Loading data from \"" << fileName << "\".";
 
185
    m_dataName->set( fileName );
 
186
 
 
187
    // remove the path up to the file name and set it as a convenient name for this module instance
 
188
    if( fileName != "" )
 
189
    {
 
190
        m_runtimeName->set( string_utils::tokenize( fileName, "/" ).back() );
 
191
    }
 
192
 
 
193
    // load it now
 
194
    std::string suffix = getSuffix( fileName );
 
195
 
 
196
    if( suffix == ".nii"
 
197
        || ( suffix == ".gz" && ::nifti_compiled_with_zlib() ) )
 
198
    {
 
199
        if( suffix == ".gz" )  // it may be a NIfTI file too
 
200
        {
 
201
            boost::filesystem::path p( fileName );
 
202
            p.replace_extension( "" );
 
203
            suffix = getSuffix( p.string() );
 
204
            WAssert( suffix == ".nii", "Currently only nii files may be gzipped." );
 
205
        }
 
206
 
 
207
        WReaderNIfTI niiLoader( fileName );
 
208
        m_dataSet = niiLoader.load();
 
209
        m_transformNoMatrix = niiLoader.getStandardTransform();
 
210
        m_transformSForm = niiLoader.getSFormTransform();
 
211
        m_transformQForm = niiLoader.getQFormTransform();
 
212
 
 
213
        m_isTexture = m_dataSet->isTexture();
 
214
 
 
215
        boost::shared_ptr< WDataSetSingle > dss = boost::shared_dynamic_cast< WDataSetSingle >( m_dataSet );
 
216
        if( dss )
 
217
        {
 
218
            m_dataType->set( getDataTypeString( dss ) );
 
219
            if( dss->isTexture() )
 
220
            {
 
221
                switch( (*dss).getValueSet()->getDataType() )
 
222
                {
 
223
                    case W_DT_UNSIGNED_CHAR:
 
224
                    case W_DT_INT16:
 
225
                    case W_DT_UINT16:
 
226
                    case W_DT_UINT32:
 
227
                    case W_DT_SIGNED_INT:
 
228
                        m_dataSet->getTexture()->colormap()->set(
 
229
                            m_dataSet->getTexture()->colormap()->get().newSelector( WItemSelector::IndexList( 1, 0 ) )
 
230
                        );
 
231
                        break;
 
232
                    case W_DT_FLOAT:
 
233
                    case W_DT_DOUBLE:
 
234
                        if( boost::shared_dynamic_cast< WDataSetVector >( m_dataSet ) )
 
235
                        {
 
236
                            m_dataSet->getTexture()->colormap()->set(
 
237
                                m_dataSet->getTexture()->colormap()->get().newSelector( WItemSelector::IndexList( 1, 6 ) )
 
238
                            );
 
239
                            m_dataSet->getTexture()->interpolation()->set( false );
 
240
                        }
 
241
                        else
 
242
                        {
 
243
                            m_dataSet->getTexture()->colormap()->set(
 
244
                                m_dataSet->getTexture()->colormap()->get().newSelector( WItemSelector::IndexList( 1, 5 ) )
 
245
                            );
 
246
                        }
 
247
                        break;
 
248
                    default:
 
249
                        WAssert( false, "Could not load \"" + fileName + "\". Reason: unknown data type in Data module" );
 
250
                }
 
251
            }
 
252
        }
 
253
    }
 
254
    else if( suffix == ".edf" )
 
255
    {
 
256
        WReaderBiosig biosigLoader( fileName );
 
257
        m_dataSet = biosigLoader.load();
 
258
    }
 
259
    else if( suffix == ".asc" )
 
260
    {
 
261
        WReaderEEGASCII eegAsciiLoader( fileName );
 
262
        m_dataSet = eegAsciiLoader.load();
 
263
    }
 
264
    else if( suffix == ".cnt" )
 
265
    {
 
266
        boost::shared_ptr< WPagerEEG > pager( new WPagerEEGLibeep( fileName ) );
 
267
 
 
268
        std::string elcFileName = fileName;
 
269
        elcFileName.resize( elcFileName.size() - 3 ); // drop suffix
 
270
        elcFileName += "elc"; // add new suffix
 
271
        WReaderELC elcReader( elcFileName );
 
272
        boost::shared_ptr< WEEGPositionsLibrary > eegPositionsLibrary = elcReader.read();
 
273
 
 
274
        m_dataSet = boost::shared_ptr< WEEG2 >( new WEEG2( pager, eegPositionsLibrary ) );
 
275
    }
 
276
    else if( suffix == ".fib" )
 
277
    {
 
278
        WReaderFiberVTK fibReader( fileName );
 
279
        m_dataSet = fibReader.read();
 
280
    }
 
281
    else
 
282
    {
 
283
        throw WDHException( std::string( "Could not load \"" + fileName + "\". Reason: unknown file type: \"" + suffix + "\"" ) );
 
284
    }
 
285
 
 
286
    debugLog() << "Loading data done.";
 
287
 
 
288
    // register the dataset properties
 
289
    m_properties->addProperty( m_dataSet->getProperties() );
 
290
    m_infoProperties->addProperty( m_dataSet->getInformationProperties() );
 
291
 
 
292
    // I am interested in the active property ( manually subscribe signal )
 
293
    m_active->getCondition()->subscribeSignal( boost::bind( &WMData::propertyChanged, this, m_active ) );
 
294
 
 
295
    // textures also provide properties
 
296
    if( m_dataSet->isTexture() )
 
297
    {
 
298
        if( !getSuppressColormaps() )
 
299
        {
 
300
            WGEColormapping::registerTexture( m_dataSet->getTexture(), m_runtimeName->get() );
 
301
        }
 
302
        m_properties->addProperty( m_dataSet->getTexture()->getProperties() );
 
303
        m_infoProperties->addProperty( m_dataSet->getTexture()->getInformationProperties() );
 
304
    }
 
305
 
 
306
    // notify
 
307
    m_output->updateData( m_dataSet );
 
308
    ready();
 
309
 
 
310
    WDataSetSingle::SPtr dataSetAsSingle = boost::shared_dynamic_cast< WDataSetSingle >( m_dataSet );
 
311
 
 
312
    while( !m_shutdownFlag() )
 
313
    {
 
314
        m_moduleState.wait();
 
315
        if( m_shutdownFlag() )
 
316
        {
 
317
            break;
 
318
        }
 
319
 
 
320
        // change transform matrix (only if we have a dataset single which contains the grid)
 
321
        if( m_matrixSelection->changed() && dataSetAsSingle )
 
322
        {
 
323
            if( m_dataSet && m_isTexture )
 
324
            {
 
325
                // remove dataset from datahandler
 
326
                if( m_dataSet->isTexture() )
 
327
                {
 
328
                    m_properties->removeProperty( m_dataSet->getTexture()->getProperties() );
 
329
                    m_infoProperties->removeProperty( m_dataSet->getTexture()->getInformationProperties() );
 
330
                    WGEColormapping::deregisterTexture( m_dataSet->getTexture() );
 
331
                }
 
332
            }
 
333
 
 
334
            // a new grid
 
335
            boost::shared_ptr< WGrid > newGrid;
 
336
            boost::shared_ptr< WGridRegular3D > oldGrid = boost::shared_dynamic_cast< WGridRegular3D >( dataSetAsSingle->getGrid() );
 
337
 
 
338
            switch( m_matrixSelection->get( true ).getItemIndexOfSelected( 0 ) )
 
339
            {
 
340
            case 0:
 
341
                newGrid = boost::shared_ptr< WGrid >( new WGridRegular3D( oldGrid->getNbCoordsX(), oldGrid->getNbCoordsY(), oldGrid->getNbCoordsZ(),
 
342
                                                                          WGridTransformOrtho( m_transformNoMatrix ) ) );
 
343
                break;
 
344
            case 1:
 
345
                newGrid = boost::shared_ptr< WGrid >( new WGridRegular3D( oldGrid->getNbCoordsX(), oldGrid->getNbCoordsY(), oldGrid->getNbCoordsZ(),
 
346
                                                                          WGridTransformOrtho( m_transformSForm ) ) );
 
347
                break;
 
348
            case 2:
 
349
                newGrid = boost::shared_ptr< WGrid >( new WGridRegular3D( oldGrid->getNbCoordsX(), oldGrid->getNbCoordsY(), oldGrid->getNbCoordsZ(),
 
350
                                                                          WGridTransformOrtho( m_transformQForm ) ) );
 
351
                break;
 
352
            }
 
353
 
 
354
            m_dataSet = dataSetAsSingle->clone( newGrid );
 
355
 
 
356
            // the clone() may have returned a zero-pointer, only update if it hasn't
 
357
            // this may happen if the clone() operation has not been implemented in the derived dataset class
 
358
            if( m_dataSet )
 
359
            {
 
360
                m_output->updateData( m_dataSet );
 
361
 
 
362
                if( m_isTexture )
 
363
                {
 
364
                    if( !getSuppressColormaps() )
 
365
                    {
 
366
                        WGEColormapping::registerTexture( m_dataSet->getTexture(), m_runtimeName->get() );
 
367
                    }
 
368
                    m_properties->addProperty( m_dataSet->getTexture()->getProperties() );
 
369
                    m_infoProperties->addProperty( m_dataSet->getTexture()->getInformationProperties() );
 
370
                }
 
371
            }
 
372
        }
 
373
    }
 
374
 
 
375
    // remove dataset from datahandler
 
376
    if( m_dataSet->isTexture() )
 
377
    {
 
378
        m_properties->removeProperty( m_dataSet->getTexture()->getProperties() );
 
379
        m_infoProperties->removeProperty( m_dataSet->getTexture()->getInformationProperties() );
 
380
        WGEColormapping::deregisterTexture( m_dataSet->getTexture() );
 
381
    }
 
382
}
 
383
 
 
384
// TODO(wiebel): move this to some central place.
 
385
std::string WMData::getDataTypeString( boost::shared_ptr< WDataSetSingle > dss )
 
386
{
 
387
    std::string result;
 
388
    switch( (*dss).getValueSet()->getDataType() )
 
389
    {
 
390
        case W_DT_NONE:
 
391
            result = "none";
 
392
            break;
 
393
        case W_DT_BINARY:
 
394
            result = "binary (1 bit)";
 
395
            break;
 
396
        case W_DT_UNSIGNED_CHAR:
 
397
            result = "unsigned char (8 bits)";
 
398
            break;
 
399
        case W_DT_SIGNED_SHORT:
 
400
            result = "signed short (16 bits)";
 
401
            break;
 
402
        case W_DT_SIGNED_INT:
 
403
            result = "signed int (32 bits)";
 
404
            break;
 
405
        case W_DT_FLOAT:
 
406
            result = "float (32 bits)";
 
407
            break;
 
408
        case W_DT_COMPLEX:
 
409
            result = "complex";
 
410
            break;
 
411
        case W_DT_DOUBLE:
 
412
            result = "double (64 bits)";
 
413
            break;
 
414
        case W_DT_RGB:
 
415
            result = "RGB triple (24 bits)";
 
416
            break;
 
417
        case W_DT_ALL:
 
418
            result = "ALL (not very useful)";
 
419
            break;
 
420
        case W_DT_INT8:
 
421
            result = "signed char (8 bits)";
 
422
            break;
 
423
        case W_DT_UINT16:
 
424
            result = "unsigned short (16 bits)";
 
425
            break;
 
426
        case W_DT_UINT32 :
 
427
            result = "unsigned int (32 bits)";
 
428
            break;
 
429
        case W_DT_INT64:
 
430
            result = "int64";
 
431
            break;
 
432
        case W_DT_UINT64:
 
433
            result = "unsigned long long (64 bits)";
 
434
            break;
 
435
        case W_DT_FLOAT128:
 
436
            result = "float (128 bits)";
 
437
            break;
 
438
        case W_DT_COMPLEX128:
 
439
            result = "double pair (128 bits)";
 
440
            break;
 
441
        case W_DT_COMPLEX256:
 
442
            result = " long double pair (256 bits)";
 
443
            break;
 
444
        case W_DT_RGBA32:
 
445
            result = "4 byte RGBA (32 bits)";
 
446
            break;
 
447
        default:
 
448
            WAssert( false, "Unknow data type in getDataTypeString" );
 
449
    }
 
450
    return result;
 
451
}
 
452