~noskcaj/ubuntu/saucy/openwalnut/liberation

« back to all changes in this revision

Viewing changes to .pc/boost153/src/modules/paintTexture/WMPaintTexture.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-Leipzig and CNCF-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 <sstream>
 
27
#include <vector>
 
28
 
 
29
#include "core/common/WPropertyHelper.h"
 
30
#include "core/graphicsEngine/WGEColormapping.h"
 
31
#include "core/graphicsEngine/WGEUtils.h"
 
32
#include "core/graphicsEngine/WROIArbitrary.h"
 
33
#include "core/kernel/WKernel.h"
 
34
#include "core/kernel/WROIManager.h"
 
35
#include "core/kernel/WSelectionManager.h"
 
36
#include "WMPaintTexture.h"
 
37
#include "WMPaintTexture.xpm"
 
38
 
 
39
// This line is needed by the module loader to actually find your module. Do not remove. Do NOT add a ";" here.
 
40
W_LOADABLE_MODULE( WMPaintTexture )
 
41
 
 
42
WMPaintTexture::WMPaintTexture():
 
43
    WModule()
 
44
{
 
45
}
 
46
 
 
47
WMPaintTexture::~WMPaintTexture()
 
48
{
 
49
    // Cleanup!
 
50
}
 
51
 
 
52
boost::shared_ptr< WModule > WMPaintTexture::factory() const
 
53
{
 
54
    return boost::shared_ptr< WModule >( new WMPaintTexture() );
 
55
}
 
56
 
 
57
const char** WMPaintTexture::getXPMIcon() const
 
58
{
 
59
    return paintTexture_xpm; // Please put a real icon here.
 
60
}
 
61
const std::string WMPaintTexture::getName() const
 
62
{
 
63
    return "Paint Texture";
 
64
}
 
65
 
 
66
const std::string WMPaintTexture::getDescription() const
 
67
{
 
68
    return "This module allows painting areas in a 3D texture";
 
69
}
 
70
 
 
71
void WMPaintTexture::connectors()
 
72
{
 
73
    // the input dataset is just used as source for resolurtion and transformation matrix
 
74
    m_input = boost::shared_ptr< WModuleInputData < WDataSetSingle  > >(
 
75
        new WModuleInputData< WDataSetSingle >( shared_from_this(), "in", "The input dataset." ) );
 
76
    addConnector( m_input );
 
77
 
 
78
    m_output = boost::shared_ptr< WModuleOutputData < WDataSetScalar  > >(
 
79
        new WModuleOutputData< WDataSetScalar >( shared_from_this(), "out", "The extracted image." ) );
 
80
    addConnector( m_output );
 
81
 
 
82
    WModule::connectors();
 
83
}
 
84
 
 
85
void WMPaintTexture::properties()
 
86
{
 
87
    m_propCondition = boost::shared_ptr< WCondition >( new WCondition() );
 
88
 
 
89
    m_painting = m_properties->addProperty( "Paint", "If active, left click in the scene with pressed ctrl key"
 
90
                                                      " will paint something.", false, m_propCondition );
 
91
 
 
92
    m_pencilSelectionsList = boost::shared_ptr< WItemSelection >( new WItemSelection() );
 
93
    m_pencilSelectionsList->addItem( "1x1", "" );
 
94
    m_pencilSelectionsList->addItem( "3x3", "" );
 
95
    m_pencilSelectionsList->addItem( "5x5", "" );
 
96
    m_pencilSelectionsList->addItem( "3x1 (only works on orthogonal slices)", "" );
 
97
    m_pencilSelectionsList->addItem( "5x1 (only works on orthogonal slices)", "" );
 
98
    m_pencilSelection = m_properties->addProperty( "Pencil",  "Pencil type.", m_pencilSelectionsList->getSelectorFirst() );
 
99
    WPropertyHelper::PC_SELECTONLYONE::addTo( m_pencilSelection );
 
100
 
 
101
    m_paintIndex = m_properties->addProperty( "Paint index", "Index we paint into the output texture", 1 );
 
102
    m_paintIndex->setMin( 0 );
 
103
    m_paintIndex->setMax( 255 );
 
104
 
 
105
    m_colorMapSelectionsList = boost::shared_ptr< WItemSelection >( new WItemSelection() );
 
106
    m_colorMapSelectionsList->addItem( "Grayscale", "" );
 
107
    m_colorMapSelectionsList->addItem( "Rainbow", "" );
 
108
    m_colorMapSelectionsList->addItem( "Hot iron", "" );
 
109
    m_colorMapSelectionsList->addItem( "Red-Yellow", "" );
 
110
    m_colorMapSelectionsList->addItem( "Atlas", "" );
 
111
    m_colorMapSelectionsList->addItem( "Blue-Green-Purple", "" );
 
112
 
 
113
    m_colorMapSelection = m_properties->addProperty( "Colormap",  "Colormap type.", m_colorMapSelectionsList->getSelector( 4 ), m_propCondition );
 
114
    WPropertyHelper::PC_SELECTONLYONE::addTo( m_colorMapSelection );
 
115
 
 
116
    m_queueAdded = m_properties->addProperty( "Something paint", "", false, m_propCondition );
 
117
    m_queueAdded->setHidden();
 
118
 
 
119
    m_buttonCopyFromInput = m_properties->addProperty( "Copy from input", "Copies data from the input dataset into the paint texture",
 
120
                WPVBaseTypes::PV_TRIGGER_READY, m_propCondition  );
 
121
 
 
122
    m_buttonUpdateOutput = m_properties->addProperty( "Update output", "Updates the output connector",
 
123
            WPVBaseTypes::PV_TRIGGER_READY, m_propCondition  );
 
124
    m_buttonCreateRoi = m_properties->addProperty( "Create ROI", "Create a ROI from the currently selected paint value",
 
125
                WPVBaseTypes::PV_TRIGGER_READY, m_propCondition  );
 
126
 
 
127
    WModule::properties();
 
128
}
 
129
 
 
130
void WMPaintTexture::moduleMain()
 
131
{
 
132
    boost::signals2::connection con = WKernel::getRunningKernel()->getGraphicsEngine()->getViewer()->getPickHandler()->getPickSignal()->
 
133
        connect( boost::bind( &WMPaintTexture::queuePaint, this, _1 ) );
 
134
 
 
135
    m_moduleState.setResetable( true, true );
 
136
    m_moduleState.add( m_input->getDataChangedCondition() );
 
137
    m_moduleState.add( m_propCondition );
 
138
    m_moduleState.add( m_active->getUpdateCondition() );
 
139
 
 
140
    ready();
 
141
 
 
142
    while( !m_shutdownFlag() )
 
143
    {
 
144
        m_moduleState.wait();
 
145
 
 
146
        if( m_shutdownFlag() )
 
147
        {
 
148
            break;
 
149
        }
 
150
 
 
151
        boost::shared_ptr< WDataSetSingle > newDataSet = m_input->getData();
 
152
        bool dataChanged = ( m_dataSet != newDataSet );
 
153
        bool dataValid   = ( newDataSet );
 
154
 
 
155
        if( dataValid )
 
156
        {
 
157
            if( dataChanged )
 
158
            {
 
159
                m_dataSet = newDataSet;
 
160
                WAssert( m_dataSet, "" );
 
161
                WAssert( m_dataSet->getValueSet(), "" );
 
162
 
 
163
                createTexture();
 
164
 
 
165
                updateOutDataset();
 
166
            }
 
167
 
 
168
            if( m_painting->changed() )
 
169
            {
 
170
                if( m_painting->get( true ) )
 
171
                {
 
172
                    WKernel::getRunningKernel()->getSelectionManager()->setPaintMode( PAINTMODE_PAINT );
 
173
                }
 
174
                else
 
175
                {
 
176
                    WKernel::getRunningKernel()->getSelectionManager()->setPaintMode( PAINTMODE_NONE );
 
177
                }
 
178
            }
 
179
 
 
180
            if( m_active->changed() )
 
181
            {
 
182
                if( m_active->get( true ) )
 
183
                {
 
184
                    if( m_painting->get( true ) )
 
185
                    {
 
186
                        WKernel::getRunningKernel()->getSelectionManager()->setPaintMode( PAINTMODE_PAINT );
 
187
                    }
 
188
                    if( m_texture )
 
189
                    {
 
190
                        m_texture->active()->set( true );
 
191
                    }
 
192
                }
 
193
                else
 
194
                {
 
195
                    m_texture->active()->set( false );
 
196
                    WKernel::getRunningKernel()->getSelectionManager()->setPaintMode( PAINTMODE_NONE );
 
197
                }
 
198
            }
 
199
 
 
200
            if( m_buttonCopyFromInput->get( true ) == WPVBaseTypes::PV_TRIGGER_TRIGGERED )
 
201
            {
 
202
                copyFromInput();
 
203
                m_buttonCopyFromInput->set( WPVBaseTypes::PV_TRIGGER_READY, false );
 
204
            }
 
205
 
 
206
            if( m_buttonCreateRoi->get( true ) == WPVBaseTypes::PV_TRIGGER_TRIGGERED )
 
207
            {
 
208
                m_painting->set( false );
 
209
                createROI();
 
210
                m_buttonCreateRoi->set( WPVBaseTypes::PV_TRIGGER_READY, false );
 
211
            }
 
212
        }
 
213
        else // case !dataValid
 
214
        {
 
215
            if( m_outData )
 
216
            m_outData = boost::shared_ptr< WDataSetScalar >();
 
217
            m_output->updateData( m_outData );
 
218
        }
 
219
        if( m_queueAdded->changed() && m_queueAdded->get( true ) )
 
220
        {
 
221
            doPaint();
 
222
        }
 
223
 
 
224
        if( m_buttonUpdateOutput->get( true ) == WPVBaseTypes::PV_TRIGGER_TRIGGERED )
 
225
        {
 
226
            updateOutDataset();
 
227
            m_buttonUpdateOutput->set( WPVBaseTypes::PV_TRIGGER_READY, false );
 
228
        }
 
229
    }
 
230
 
 
231
    debugLog() << "Shutting down...";
 
232
 
 
233
    WGEColormapping::deregisterTexture( m_texture );
 
234
    WKernel::getRunningKernel()->getSelectionManager()->setPaintMode( PAINTMODE_NONE );
 
235
 
 
236
    con.disconnect();
 
237
 
 
238
    debugLog() << "Finished! Good Bye!";
 
239
}
 
240
 
 
241
void WMPaintTexture::activate()
 
242
{
 
243
    WModule::activate();
 
244
}
 
245
 
 
246
void WMPaintTexture::doPaint()
 
247
{
 
248
    unsigned char* data = m_texture->getImage()->data();
 
249
 
 
250
    while( !m_paintQueue.empty() )
 
251
    {
 
252
        WPickInfo pickInfo = m_paintQueue.front();
 
253
        WPosition paintPosition = pickInfo.getPickPosition();
 
254
        m_paintQueue.pop();
 
255
 
 
256
        int voxelNum = m_grid->getVoxelNum( paintPosition );
 
257
        if( voxelNum != -1 )
 
258
        {
 
259
            switch( m_pencilSelection->get( true ).getItemIndexOfSelected( 0 ) )
 
260
            {
 
261
                case 0:
 
262
                    data[ voxelNum ] = m_paintIndex->get();
 
263
                    break;
 
264
                case 1:
 
265
                {
 
266
                    data[ voxelNum ] = m_paintIndex->get();
 
267
                    std::vector< size_t > ids = m_grid->getNeighbours27( voxelNum );
 
268
                    for( size_t i = 0; i < ids.size(); ++i )
 
269
                    {
 
270
                        data[ ids[i] ] = m_paintIndex->get();
 
271
                    }
 
272
                    break;
 
273
                }
 
274
                case 2:
 
275
                {
 
276
                    data[ voxelNum ] = m_paintIndex->get();
 
277
                    std::vector< size_t > ids = m_grid->getNeighbours27( voxelNum );
 
278
                    for( size_t i = 0; i < ids.size(); ++i )
 
279
                    {
 
280
                        std::vector< size_t > allIds = m_grid->getNeighbours27( ids[i] );
 
281
                        for( size_t k = 0; k < allIds.size(); ++k )
 
282
                        {
 
283
                            data[ allIds[k] ] = m_paintIndex->get();
 
284
                        }
 
285
                    }
 
286
                    break;
 
287
                }
 
288
                case 3:
 
289
                {
 
290
                    if( pickInfo.getName() == "Axial Slice" )
 
291
                    {
 
292
                        data[ voxelNum ] = m_paintIndex->get();
 
293
                        std::vector< size_t > ids = m_grid->getNeighbours9XY( voxelNum );
 
294
                        for( size_t i = 0; i < ids.size(); ++i )
 
295
                        {
 
296
                            data[ ids[i] ] = m_paintIndex->get();
 
297
                        }
 
298
                    }
 
299
                    if( pickInfo.getName() == "Coronal Slice" )
 
300
                    {
 
301
                        data[ voxelNum ] = m_paintIndex->get();
 
302
                        std::vector< size_t > ids = m_grid->getNeighbours9XZ( voxelNum );
 
303
                        for( size_t i = 0; i < ids.size(); ++i )
 
304
                        {
 
305
                            data[ ids[i] ] = m_paintIndex->get();
 
306
                        }
 
307
                    }
 
308
                    if( pickInfo.getName() == "Sagittal Slice" )
 
309
                    {
 
310
                        data[ voxelNum ] = m_paintIndex->get();
 
311
                        std::vector< size_t > ids = m_grid->getNeighbours9YZ( voxelNum );
 
312
                        for( size_t i = 0; i < ids.size(); ++i )
 
313
                        {
 
314
                            data[ ids[i] ] = m_paintIndex->get();
 
315
                        }
 
316
                    }
 
317
                    break;
 
318
                }
 
319
                case 4:
 
320
                {
 
321
                    if( pickInfo.getName() == "Axial Slice" )
 
322
                    {
 
323
                        data[ voxelNum ] = m_paintIndex->get();
 
324
                        std::vector< size_t > ids = m_grid->getNeighbours9XY( voxelNum );
 
325
                        for( size_t i = 0; i < ids.size(); ++i )
 
326
                        {
 
327
                            std::vector< size_t > allIds = m_grid->getNeighbours9XY( ids[i] );
 
328
                            for( size_t k = 0; k < allIds.size(); ++k )
 
329
                            {
 
330
                                data[ allIds[k] ] = m_paintIndex->get();
 
331
                            }
 
332
                        }
 
333
                    }
 
334
                    if( pickInfo.getName() == "Coronal Slice" )
 
335
                    {
 
336
                        data[ voxelNum ] = m_paintIndex->get();
 
337
                        std::vector< size_t > ids = m_grid->getNeighbours9XZ( voxelNum );
 
338
                        for( size_t i = 0; i < ids.size(); ++i )
 
339
                        {
 
340
                            std::vector< size_t > allIds = m_grid->getNeighbours9XZ( ids[i] );
 
341
                            for( size_t k = 0; k < allIds.size(); ++k )
 
342
                            {
 
343
                                data[ allIds[k] ] = m_paintIndex->get();
 
344
                            }
 
345
                        }
 
346
                    }
 
347
                    if( pickInfo.getName() == "Sagittal Slice" )
 
348
                    {
 
349
                        data[ voxelNum ] = m_paintIndex->get();
 
350
                        std::vector< size_t > ids = m_grid->getNeighbours9YZ( voxelNum );
 
351
                        for( size_t i = 0; i < ids.size(); ++i )
 
352
                        {
 
353
                            std::vector< size_t > allIds = m_grid->getNeighbours9YZ( ids[i] );
 
354
                            for( size_t k = 0; k < allIds.size(); ++k )
 
355
                            {
 
356
                                data[ allIds[k] ] = m_paintIndex->get();
 
357
                            }
 
358
                        }
 
359
                    }
 
360
                    break;
 
361
                }
 
362
 
 
363
                default:
 
364
                    break;
 
365
            }
 
366
        }
 
367
    }
 
368
    m_queueAdded->set( false );
 
369
 
 
370
    m_texture->dirtyTextureObject();
 
371
 
 
372
    updateOutDataset();
 
373
}
 
374
 
 
375
void WMPaintTexture::queuePaint( WPickInfo pickInfo )
 
376
{
 
377
    if( pickInfo.getModifierKey() == WPickInfo::SHIFT )
 
378
    {
 
379
        setColorFromPick( pickInfo );
 
380
        return;
 
381
    }
 
382
 
 
383
    if( !m_painting->get() || ( pickInfo.getMouseButton() != WPickInfo::MOUSE_LEFT ) || ( pickInfo.getName() == "unpick" ) )
 
384
    {
 
385
        return;
 
386
    }
 
387
 
 
388
    m_paintQueue.push( pickInfo );
 
389
    m_queueAdded->set( true );
 
390
}
 
391
 
 
392
void WMPaintTexture::setColorFromPick( WPickInfo pickInfo )
 
393
{
 
394
    WPosition paintPosition = pickInfo.getPickPosition();
 
395
    int voxelNum = m_grid->getVoxelNum( paintPosition );
 
396
 
 
397
    if( voxelNum != -1 )
 
398
    {
 
399
        unsigned char* data = m_texture->getImage()->data();
 
400
        m_paintIndex->set( data[ voxelNum ] );
 
401
    }
 
402
}
 
403
 
 
404
void WMPaintTexture::createTexture()
 
405
{
 
406
    m_grid = boost::shared_dynamic_cast< WGridRegular3D >( m_dataSet->getGrid() );
 
407
 
 
408
    osg::ref_ptr< osg::Image > ima = new osg::Image;
 
409
    ima->allocateImage( m_grid->getNbCoordsX(), m_grid->getNbCoordsY(), m_grid->getNbCoordsZ(), GL_LUMINANCE, GL_UNSIGNED_BYTE );
 
410
 
 
411
    unsigned char* data = ima->data();
 
412
 
 
413
    for( unsigned int i = 0; i < m_grid->size(); ++i )
 
414
    {
 
415
        data[i] = 0.0;
 
416
    }
 
417
 
 
418
    m_texture = osg::ref_ptr< WGETexture3D >( new WGETexture3D( ima ) );
 
419
    m_texture->setFilterMinMag( osg::Texture3D::LINEAR );
 
420
    m_texture->setWrapSTR( osg::Texture::CLAMP_TO_BORDER );
 
421
    m_texture->colormap()->set( m_texture->colormap()->get().newSelector( WItemSelector::IndexList( 1, 4 ) ) );
 
422
    m_properties->addProperty( m_texture->alpha() );
 
423
 
 
424
    WGEColormapping::registerTexture( m_texture, "Painted Texture" );
 
425
}
 
426
 
 
427
void WMPaintTexture::updateOutDataset()
 
428
{
 
429
    WAssert( m_dataSet, "" );
 
430
    WAssert( m_dataSet->getValueSet(), "" );
 
431
    WAssert( m_dataSet->getGrid(), "" );
 
432
 
 
433
    unsigned char* data = m_texture->getImage()->data();
 
434
    boost::shared_ptr< std::vector< unsigned char > > values =
 
435
        boost::shared_ptr< std::vector< unsigned char > >( new std::vector< unsigned char >( m_grid->size(), 0.0 ) );
 
436
 
 
437
    for( unsigned int i = 0; i < m_grid->size(); ++i )
 
438
    {
 
439
        ( *values )[i] = data[i];
 
440
    }
 
441
 
 
442
    boost::shared_ptr< WValueSet< unsigned char > > vs =
 
443
        boost::shared_ptr< WValueSet< unsigned char > >( new WValueSet< unsigned char >( 0, 1, values, W_DT_UINT8 ) );
 
444
 
 
445
    m_outData = boost::shared_ptr< WDataSetScalar >( new WDataSetScalar( vs, m_grid ) );
 
446
    m_outData->getTexture()->interpolation()->set( false );
 
447
    m_output->updateData( m_outData );
 
448
}
 
449
 
 
450
void WMPaintTexture::copyFromInput()
 
451
{
 
452
    m_grid = boost::shared_dynamic_cast< WGridRegular3D >( m_dataSet->getGrid() );
 
453
 
 
454
    unsigned char* data = m_texture->getImage()->data();
 
455
 
 
456
    boost::shared_ptr< WValueSet< unsigned char > > vals;
 
457
    vals =  boost::shared_dynamic_cast< WValueSet< unsigned char > >( m_dataSet->getValueSet() );
 
458
 
 
459
    for( unsigned int i = 0; i < m_grid->size(); ++i )
 
460
    {
 
461
        data[i] = vals->getScalar( i );
 
462
    }
 
463
    m_texture->dirtyTextureObject();
 
464
}
 
465
 
 
466
void WMPaintTexture::createROI()
 
467
{
 
468
    bool valid = false;
 
469
    std::vector<float>roiVals( m_grid->size(), 0 );
 
470
    unsigned char index = m_paintIndex->get();
 
471
 
 
472
    unsigned char* data = m_texture->getImage()->data();
 
473
 
 
474
    for( size_t i = 0; i < m_grid->size(); ++i )
 
475
    {
 
476
        if( data[i] == index )
 
477
        {
 
478
            roiVals[i] = 1.0;
 
479
            valid = true;
 
480
        }
 
481
    }
 
482
 
 
483
    if( valid )
 
484
    {
 
485
        osg::ref_ptr< WROI > newRoi = osg::ref_ptr< WROI >(
 
486
                new WROIArbitrary( m_grid->getNbCoordsX(), m_grid->getNbCoordsY(), m_grid->getNbCoordsZ(),
 
487
                                   m_grid->getTransformationMatrix(), roiVals, 1.0, wge::createColorFromIndex( index ) ) );
 
488
 
 
489
        if( WKernel::getRunningKernel()->getRoiManager()->getSelectedRoi() == NULL )
 
490
        {
 
491
            WLogger::getLogger()->addLogMessage( " new roi without parent ", "WMPaintTexture", LL_DEBUG );
 
492
            WKernel::getRunningKernel()->getRoiManager()->addRoi( newRoi );
 
493
        }
 
494
        else
 
495
        {
 
496
            WLogger::getLogger()->addLogMessage( " new roi with parent ", "WMPaintTexture", LL_DEBUG );
 
497
            WKernel::getRunningKernel()->getRoiManager()->addRoi( newRoi, WKernel::getRunningKernel()->getRoiManager()->getSelectedRoi() );
 
498
        }
 
499
    }
 
500
}