WMNavSlices.cpp 40.1 KB
Newer Older
1 2
//---------------------------------------------------------------------------
//
3
// Project: OpenWalnut ( http://www.openwalnut.org )
4
//
5 6
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
//
// 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 <iostream>
26
#include <list>
27
#include <string>
28
#include <vector>
29
#include <utility>
30

31
#include <boost/shared_ptr.hpp>
32
#include <boost/signals2/signal.hpp>
33

34 35
#include <osg/Geode>
#include <osg/Geometry>
36
#include <osg/Group>
37
#include <osg/LineWidth>
38

39
#include "../../common/WAssert.h"
40
#include "../../common/math/WVector3D.h"
41
#include "../../dataHandler/WDataSet.h"
42
#include "../../dataHandler/WDataHandler.h"
43
#include "../../dataHandler/WDataSetSingle.h"
44 45
#include "../../dataHandler/WDataTexture3D.h"
#include "../../dataHandler/WGridRegular3D.h"
46
#include "../../dataHandler/WSubject.h"
47
#include "../../dataHandler/WValueSet.h"
48
#include "../../graphicsEngine/WShader.h"
49
#include "../../graphicsEngine/WGraphicsEngine.h"
Mathias Goldau's avatar
[MERGE]  
Mathias Goldau committed
50
#include "../../graphicsEngine/WGEUtils.h"
51 52 53 54 55
#include "../../kernel/WKernel.h"
#include "../../kernel/WModule.h"
#include "../../kernel/WModuleConnector.h"
#include "../../kernel/WModuleInputData.h"
#include "WMNavSlices.h"
56
#include "navslices.xpm"
57

58 59
bool WMNavSlices::m_navsliceRunning = false;

60
WMNavSlices::WMNavSlices():
61
    WModule(),
62
    m_textureChanged( true ),
63 64 65 66
    m_isPicked( false ),
    m_isPickedSagittal( false ),
    m_isPickedCoronal( false ),
    m_isPickedAxial( false )
67
{
68 69
    // WARNING: initializing connectors inside the constructor will lead to an exception.
    // Implement WModule::initializeConnectors instead.
70

71
    // initialize members
72
    std::string shaderPath = WKernel::getRunningKernel()->getGraphicsEngine()->getShaderPath();
73
    m_shader = osg::ref_ptr< WShader > ( new WShader( "slice" ) );
74 75
}

76
WMNavSlices::~WMNavSlices()
77 78
{
    // cleanup
79
    removeConnectors();
80 81
}

82 83
boost::shared_ptr< WModule > WMNavSlices::factory() const
{
84
    m_navsliceRunning = true;
85 86 87
    return boost::shared_ptr< WModule >( new WMNavSlices() );
}

88 89 90 91 92
bool WMNavSlices::isRunning()
{
    return m_navsliceRunning;
}

93 94 95 96 97
const char** WMNavSlices::getXPMIcon() const
{
    return navslices_xpm;
}

98
const std::string WMNavSlices::getName() const
99
{
100
    return "Navigation Slices";
101 102
}

103
const std::string WMNavSlices::getDescription() const
104
{
105
    return "This module shows 3 orthogonal navigation slices.";
106 107
}

108
void WMNavSlices::connectors()
109 110 111 112
{
    // initialize connectors

    // call WModules initialization
113
    WModule::connectors();
114 115
}

116
void WMNavSlices::properties()
117
{
118 119
    // NOTE: the appropriate type of addProperty is chosen by the type of the specified initial value.
    // So if you specify a bool as initial value, addProperty will create a WPropBool.
120 121 122
    m_showAxial      = m_properties->addProperty( "showAxial",      "Determines whether the axial slice should be visible.", true, true );
    m_showCoronal    = m_properties->addProperty( "showCoronal",    "Determines whether the coronal slice should be visible.", true, true );
    m_showSagittal   = m_properties->addProperty( "showSagittal",   "Determines whether the sagittal slice should be visible.", true, true );
123

124
    m_axialPos       = m_properties->addProperty( "Axial Slice",       "Position of axial slice.",    80 );
125 126
    m_axialPos->setMin( 0 );
    m_axialPos->setMax( 160 );
127
    m_coronalPos     = m_properties->addProperty( "Coronal Slice",     "Position of coronal slice.", 100 );
128
    m_coronalPos->setMin( 0 );
129
    m_coronalPos->setMax( 200 );
130
    m_sagittalPos    = m_properties->addProperty( "Sagittal Slice",    "Position of sagittal slice.", 80 );
131 132
    m_sagittalPos->setMin( 0 );
    m_sagittalPos->setMax( 160 );
133

134 135 136
    m_axialPos->setHidden();
    m_coronalPos->setHidden();
    m_sagittalPos->setHidden();
137 138
}

139
void WMNavSlices::notifyDataChange( boost::shared_ptr<WModuleConnector> input,
140
                                               boost::shared_ptr<WModuleConnector> output )
141
{
142 143
    WModule::notifyDataChange( input, output );

Alexander Wiebel's avatar
Alexander Wiebel committed
144
    // in this case input==m_input
145 146
}

147 148 149 150 151
void WMNavSlices::notifyTextureChange()
{
    m_textureChanged = true;
}

152
void WMNavSlices::moduleMain()
153
{
Mathias Goldau's avatar
[MERGE]  
Mathias Goldau committed
154
    boost::shared_ptr< WGraphicsEngine > ge = WGraphicsEngine::getGraphicsEngine();
155
    WAssert( ge, "No graphics engine present." );
Mathias Goldau's avatar
[MERGE]  
Mathias Goldau committed
156 157

    m_viewer = ge->getViewerByName( "main" );
158
    WAssert( m_viewer, "Requested viewer not found." );
Mathias Goldau's avatar
[MERGE]  
Mathias Goldau committed
159
    m_viewer->getPickHandler()->getPickSignal()->connect( boost::bind( &WMNavSlices::setSlicePosFromPick, this, _1 ) );
160

161
    m_viewer = ge->getViewerByName( "axial" );
162 163 164 165
    if( m_viewer )
    {
        m_viewer->getPickHandler()->getPickSignal()->connect( boost::bind( &WMNavSlices::setSlicePosFromPick, this, _1 ) );
    }
166 167

    m_viewer = ge->getViewerByName( "sagittal" );
168 169 170 171
    if( m_viewer )
    {
        m_viewer->getPickHandler()->getPickSignal()->connect( boost::bind( &WMNavSlices::setSlicePosFromPick, this, _1 ) );
    }
172 173

    m_viewer = ge->getViewerByName( "coronal" );
174 175 176 177
    if( m_viewer )
    {
        m_viewer->getPickHandler()->getPickSignal()->connect( boost::bind( &WMNavSlices::setSlicePosFromPick, this, _1 ) );
    }
178 179 180

    m_viewer = ge->getViewerByName( "main" );

181 182 183
    // signal ready state
    ready();

184 185 186 187 188
    // now, to watch changing/new textures use WSubject's change condition
    boost::signals2::connection con = WDataHandler::getDefaultSubject()->getChangeCondition()->subscribeSignal(
            boost::bind( &WMNavSlices::notifyTextureChange, this )
    );

189 190 191 192 193
    setMaxMinFromBoundingBox();
    m_sagittalPos->set( 0.5 * ( m_bb.first[0] + m_bb.second[0] ) );
    m_coronalPos->set( 0.5 * (  m_bb.first[1] + m_bb.second[1] ) );
    m_axialPos->set( 0.5 * (  m_bb.first[2] + m_bb.second[2] ) );

194
    create();
195

196 197
    // Since the modules run in a separate thread: wait
    waitForStop();
198 199

    // clean up stuff
200 201
    // NOTE: ALAWAYS remove your osg nodes!
    // Please, please always check for NULL
Mathias Goldau's avatar
[MERGE]  
Mathias Goldau committed
202 203 204
    boost::shared_ptr< WGEViewer > viewer;
    viewer = WKernel::getRunningKernel()->getGraphicsEngine()->getViewerByName( "axial" );
    if( viewer )
205
    {
206
        viewer->getScene()->remove( m_zSliceNode );
207
        viewer->getScene()->remove( m_zCrossNode );
208 209 210
    }

    viewer = WKernel::getRunningKernel()->getGraphicsEngine()->getViewerByName( "sagittal" );
Mathias Goldau's avatar
[MERGE]  
Mathias Goldau committed
211
    if( viewer )
212
    {
213 214
        viewer->getScene()->remove( m_xSliceNode );
        viewer->getScene()->remove( m_xCrossNode );
215 216 217
    }

    viewer = WKernel::getRunningKernel()->getGraphicsEngine()->getViewerByName( "coronal" );
Mathias Goldau's avatar
[MERGE]  
Mathias Goldau committed
218
    if( viewer )
219
    {
220 221
        viewer->getScene()->remove( m_ySliceNode );
        viewer->getScene()->remove( m_yCrossNode );
222 223
    }

224
    WKernel::getRunningKernel()->getGraphicsEngine()->getScene()->remove( m_slicesNode );
225 226 227

    // deregister from WSubject's change condition
    con.disconnect();
228 229
}

230 231
void WMNavSlices::create()
{
232
    m_rootNode = osg::ref_ptr< WGEGroupNode >( new WGEGroupNode() );
233
    m_slicesNode = osg::ref_ptr< WGEGroupNode >( new WGEGroupNode() );
234 235

    m_xSliceNode = osg::ref_ptr<osg::Geode>( new osg::Geode() );
236
    m_xSliceNode->setName( "Sagittal Slice" );
237
    m_ySliceNode = osg::ref_ptr<osg::Geode>( new osg::Geode() );
238
    m_ySliceNode->setName( "Coronal Slice" );
239
    m_zSliceNode = osg::ref_ptr<osg::Geode>( new osg::Geode() );
240
    m_zSliceNode->setName( "Axial Slice" );
241 242 243 244 245

    m_xSliceNode->addDrawable( createGeometry( 0 ) );
    m_ySliceNode->addDrawable( createGeometry( 1 ) );
    m_zSliceNode->addDrawable( createGeometry( 2 ) );

246 247 248 249 250 251 252 253 254 255 256 257
    m_xCrossNode = osg::ref_ptr<osg::Geode>( new osg::Geode() );
    m_yCrossNode = osg::ref_ptr<osg::Geode>( new osg::Geode() );
    m_zCrossNode = osg::ref_ptr<osg::Geode>( new osg::Geode() );
    m_xCrossNode->addDrawable( createCrossGeometry( 0 ) );
    m_yCrossNode->addDrawable( createCrossGeometry( 1 ) );
    m_zCrossNode->addDrawable( createCrossGeometry( 2 ) );

    m_rootNode->insert( m_xCrossNode );
    m_rootNode->insert( m_yCrossNode );
    m_rootNode->insert( m_zCrossNode );

    m_rootNode->insert( m_slicesNode );
258

259 260 261 262 263
    m_slicesNode->insert( m_xSliceNode );
    m_slicesNode->insert( m_ySliceNode );
    m_slicesNode->insert( m_zSliceNode );

    m_shader->apply( m_slicesNode );
264 265 266
    m_shader->apply( m_xSliceNode );
    m_shader->apply( m_ySliceNode );
    m_shader->apply( m_zSliceNode );
267

268
    osg::StateSet* rootState = m_slicesNode->getOrCreateStateSet();
269
    initUniforms( rootState );
270 271 272

    rootState->setMode( GL_BLEND, osg::StateAttribute::ON );

273
    m_rootNode->setUserData( this );
274
    m_slicesNode->setUserData( this );
275 276 277
    m_xSliceNode->setUserData( this );
    m_ySliceNode->setUserData( this );
    m_zSliceNode->setUserData( this );
278 279 280 281
    m_xCrossNode->setUserData( this );
    m_yCrossNode->setUserData( this );
    m_zCrossNode->setUserData( this );
    m_slicesNode->addUpdateCallback( new sliceNodeCallback );
282

283
    WKernel::getRunningKernel()->getGraphicsEngine()->getScene()->insert( m_slicesNode );
284

285 286
    // Please, please always check for NULL
    boost::shared_ptr< WGEViewer > viewer = WKernel::getRunningKernel()->getGraphicsEngine()->getViewerByName( "axial" );
Mathias Goldau's avatar
[MERGE]  
Mathias Goldau committed
287
    if( viewer )
288
    {
289
        viewer->getScene()->insert( m_zSliceNode );
290 291
        viewer->getScene()->insert( m_zCrossNode );
        viewer->setCameraManipulator( 0 );
292 293 294
    }

    viewer = WKernel::getRunningKernel()->getGraphicsEngine()->getViewerByName( "sagittal" );
Mathias Goldau's avatar
[MERGE]  
Mathias Goldau committed
295
    if( viewer )
296
    {
297
        viewer->getScene()->insert( m_xSliceNode );
298 299
        viewer->getScene()->insert( m_xCrossNode );
        viewer->setCameraManipulator( 0 );
300 301 302
    }

    viewer = WKernel::getRunningKernel()->getGraphicsEngine()->getViewerByName( "coronal" );
Mathias Goldau's avatar
[MERGE]  
Mathias Goldau committed
303
    if( viewer )
304
    {
305
        viewer->getScene()->insert( m_ySliceNode );
306 307
        viewer->getScene()->insert( m_yCrossNode );
        viewer->setCameraManipulator( 0 );
308 309 310 311 312
    }
}

void WMNavSlices::setSlicePosFromPick( WPickInfo pickInfo )
{
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
    // handle the pick information on the slice views
    if ( pickInfo.getViewerName() != "main" && pickInfo.getViewerName() != "" )
    {
        // this uses fixed windows size of 150x150 pixel
        boost::unique_lock< boost::shared_mutex > lock;
        lock = boost::unique_lock< boost::shared_mutex >( m_updateLock );

        float x = pickInfo.getPickPixelPosition().first;
        float y = pickInfo.getPickPixelPosition().second;
        float xPos;
        float yPos;
        float width;
        float height;
        // z slice
        if ( pickInfo.getViewerName() == "axial" )
        {
            width = m_bb.second[0] - m_bb.first[0];
            height = m_bb.second[1] - m_bb.first[1];

            if ( width > height )
            {
                xPos = ( x / 150 ) * width;
                yPos = ( y / 150 ) * width - ( width - height ) / 2;
            }
            else
            {
                xPos = ( x / 150 ) * height - ( height - width ) / 2;
                yPos = ( y / 150 ) * height;
            }
            xPos = xPos < m_bb.first[0] ? m_bb.first[0] : xPos;
            xPos = xPos > m_bb.second[0] ? m_bb.second[0] : xPos;
            yPos = yPos < m_bb.first[1] ? m_bb.first[1] : yPos;
            yPos = yPos > m_bb.second[1] ? m_bb.second[1] : yPos;

            m_sagittalPos->set( xPos );
            m_coronalPos->set( yPos );
        }

        // x slice
        if ( pickInfo.getViewerName() == "sagittal" )
        {
            width = m_bb.second[1] - m_bb.first[1];
            height = m_bb.second[2] - m_bb.first[2];

            if ( width > height )
            {
                xPos = ( x / 150 ) * width;
                yPos = ( y / 150 ) * width - ( width - height ) / 2;
            }
            else
            {
                xPos = ( x / 150 ) * height - ( height - width ) / 2;
                yPos = ( y / 150 ) * height;
            }
            xPos = m_bb.second[1] - xPos;
            xPos = xPos < m_bb.first[1] ? m_bb.first[1] : xPos;
            xPos = xPos > m_bb.second[1] ? m_bb.second[1] : xPos;
            yPos = yPos < m_bb.first[2] ? m_bb.first[2] : yPos;
            yPos = yPos > m_bb.second[2] ? m_bb.second[2] : yPos;

            m_coronalPos->set( xPos );
            m_axialPos->set( yPos );
        }

        // y slice
        if ( pickInfo.getViewerName() == "coronal" )
        {
            width = m_bb.second[0] - m_bb.first[0];
            height = m_bb.second[2] - m_bb.first[2];

            if ( width > height )
            {
                xPos = ( x / 150 ) * width;
                yPos = ( y / 150 ) * width - ( width - height ) / 2;
            }
            else
            {
                xPos = ( x / 150 ) * height - ( height - width ) / 2;
                yPos = ( y / 150 ) * height;
            }
            xPos = xPos < m_bb.first[0] ? m_bb.first[0] : xPos;
            xPos = xPos > m_bb.second[0] ? m_bb.second[0] : xPos;
            yPos = yPos < m_bb.first[2] ? m_bb.first[2] : yPos;
            yPos = yPos > m_bb.second[2] ? m_bb.second[2] : yPos;

            m_sagittalPos->set( xPos );
            m_axialPos->set( yPos );
        }

        return;
    }

405 406 407 408 409 410 411
    if ( pickInfo.getName() == "Axial Slice"
         ||  pickInfo.getName() == "Coronal Slice"
         ||  pickInfo.getName() == "Sagittal Slice" )
    {
        boost::unique_lock< boost::shared_mutex > lock;
        lock = boost::unique_lock< boost::shared_mutex >( m_updateLock );

Mathias Goldau's avatar
[MERGE]  
Mathias Goldau committed
412 413
        wmath::WVector3D normal = pickInfo.getPickNormal();

414 415 416
        std::pair< float, float > newPixelPos( pickInfo.getPickPixelPosition() );
        if ( m_isPicked )
        {
417 418
            osg::Vec3 startPosScreen( m_oldPixelPosition.first, m_oldPixelPosition.second, 0.0 );
            osg::Vec3 endPosScreen( newPixelPos.first, newPixelPos.second, 0.0 );
Mathias Goldau's avatar
[MERGE]  
Mathias Goldau committed
419 420 421 422 423

            osg::Vec3 startPosWorld = wge::unprojectFromScreen( startPosScreen, m_viewer->getCamera() );
            osg::Vec3 endPosWorld = wge::unprojectFromScreen( endPosScreen, m_viewer->getCamera() );

            osg::Vec3 moveDirWorld = endPosWorld - startPosWorld;
424
            float diff = wge::wv3D2ov3( normal ) * moveDirWorld;
Mathias Goldau's avatar
[MERGE]  
Mathias Goldau committed
425

426 427 428 429 430 431
            // recognize also small values.
            if( diff < 0 && diff > -1 )
            {
                diff = -1;
            }
            if( diff > 0 && diff < 1 )
Mathias Goldau's avatar
[MERGE]  
Mathias Goldau committed
432
            {
433
                diff = 1;
Mathias Goldau's avatar
[MERGE]  
Mathias Goldau committed
434 435
            }

436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
            if ( pickInfo.getName() == "Axial Slice" )
            {
                m_axialPos->set( m_axialPos->get() + diff );
            }
            if ( pickInfo.getName() == "Coronal Slice" )
            {
                m_coronalPos->set( m_coronalPos->get() + diff );
            }
            if ( pickInfo.getName() == "Sagittal Slice" )
            {
                m_sagittalPos->set( m_sagittalPos->get() + diff );
            }

            lock.unlock();
        }
        m_oldPixelPosition = newPixelPos;
        m_isPicked |= true;
453 454 455 456 457 458 459 460 461 462 463 464 465

        if ( pickInfo.getName() == "Axial Slice" )
        {
            m_isPickedAxial |= true;
        }
        if ( pickInfo.getName() == "Coronal Slice" )
        {
            m_isPickedCoronal |= true;
        }
        if ( pickInfo.getName() == "Sagittal Slice" )
        {
            m_isPickedSagittal |= true;
        }
466 467 468 469
    }
    else
    {
        m_isPicked &= false;
470 471 472
        m_isPickedCoronal &= false;
        m_isPickedAxial &= false;
        m_isPickedSagittal &= false;
473
    }
474 475
}

476 477 478 479 480 481 482 483
void WMNavSlices::setMaxMinFromBoundingBox()
{
    // grab a list of data textures
    std::vector< boost::shared_ptr< WDataTexture3D > > tex = WDataHandler::getDefaultSubject()->getDataTextures( true );

    if ( tex.size() > 0 )
    {
        std::pair< wmath::WPosition, wmath::WPosition > bb = tex[0]->getGrid()->getBoundingBox();
484

485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
        for( size_t i = 1; i < tex.size(); ++i )
        {
            std::pair< wmath::WPosition, wmath::WPosition > bbTmp = tex[i]->getGrid()->getBoundingBox();
            bb.first[0] = bb.first[0] < bbTmp.first[0] ? bb.first[0] : bbTmp.first[0];
            bb.first[1] = bb.first[1] < bbTmp.first[1] ? bb.first[1] : bbTmp.first[1];
            bb.first[2] = bb.first[2] < bbTmp.first[2] ? bb.first[2] : bbTmp.first[2];
            bb.second[0] = bb.second[0] > bbTmp.second[0] ? bb.second[0] : bbTmp.second[0];
            bb.second[1] = bb.second[1] > bbTmp.second[1] ? bb.second[1] : bbTmp.second[1];
            bb.second[2] = bb.second[2] > bbTmp.second[2] ? bb.second[2] : bbTmp.second[2];
        }

        m_bb = bb;

        m_sagittalPos->setMin( bb.first[0] );
        m_sagittalPos->setMax( bb.second[0] );
        m_coronalPos->setMin( bb.first[1] );
        m_coronalPos->setMax( bb.second[1] );
        m_axialPos->setMin( bb.first[2] );
        m_axialPos->setMax( bb.second[2] );
    }
}

507
osg::ref_ptr<osg::Geometry> WMNavSlices::createGeometry( int slice )
508
{
509
    const size_t nbVerts = 4;
schurade's avatar
schurade committed
510

511 512 513
    float xSlice = static_cast< float >( m_sagittalPos->get( true ) );
    float ySlice = static_cast< float >( m_coronalPos->get( true ) );
    float zSlice = static_cast< float >( m_axialPos->get( true ) );
schurade's avatar
schurade committed
514

schurade's avatar
schurade committed
515 516 517
    float xPos = xSlice + 0.5f;
    float yPos = ySlice + 0.5f;
    float zPos = zSlice + 0.5f;
518

519
    osg::ref_ptr<osg::Geometry> sliceGeometry = osg::ref_ptr<osg::Geometry>( new osg::Geometry() );
520 521

    osg::Vec3Array* sliceVertices = new osg::Vec3Array;
522

523 524
    // grab a list of data textures
    std::vector< boost::shared_ptr< WDataTexture3D > > tex = WDataHandler::getDefaultSubject()->getDataTextures( true );
schurade's avatar
schurade committed
525

526

527
    if ( tex.size() > 0 )
528
    {
529 530
        setMaxMinFromBoundingBox();

531 532 533 534
        switch ( slice )
        {
            case 0:
            {
535
                std::vector< wmath::WPosition > vertices;
536 537 538 539
                vertices.push_back( wmath::WPosition( xPos, m_bb.first[1],  m_bb.first[2]   ) );
                vertices.push_back( wmath::WPosition( xPos, m_bb.first[1],  m_bb.second[2]  ) );
                vertices.push_back( wmath::WPosition( xPos, m_bb.second[1], m_bb.second[2]  ) );
                vertices.push_back( wmath::WPosition( xPos, m_bb.second[1], m_bb.first[2]   ) );
540 541
                for( size_t i = 0; i < nbVerts; ++i )
                {
542
                    sliceVertices->push_back( wge::wv3D2ov3( vertices[i] ) );
543 544
                }

545 546 547
                sliceGeometry->setVertexArray( sliceVertices );

                int c = 0;
548
                for ( std::vector< boost::shared_ptr< WDataTexture3D > >::const_iterator iter = tex.begin(); iter != tex.end(); ++iter )
549
                {
550
                    boost::shared_ptr< WGridRegular3D > grid = ( *iter )->getGrid();
551

schurade's avatar
schurade committed
552
                    osg::Vec3Array* texCoords = new osg::Vec3Array;
553 554
                    for( size_t i = 0; i < nbVerts; ++i )
                    {
555
                        texCoords->push_back( wge::wv3D2ov3( grid->worldCoordToTexCoord( vertices[i] ) ) );
556
                    }
schurade's avatar
schurade committed
557 558 559
                    sliceGeometry->setTexCoordArray( c, texCoords );
                    ++c;
                }
560

561 562 563 564
                break;
            }
            case 1:
            {
565
                std::vector< wmath::WPosition > vertices;
566 567 568 569
                vertices.push_back( wmath::WPosition( m_bb.first[0],  yPos, m_bb.first[2]  ) );
                vertices.push_back( wmath::WPosition( m_bb.second[0], yPos, m_bb.first[2]  ) );
                vertices.push_back( wmath::WPosition( m_bb.second[0], yPos, m_bb.second[2] ) );
                vertices.push_back( wmath::WPosition( m_bb.first[0],  yPos, m_bb.second[2] ) );
570 571
                for( size_t i = 0; i < nbVerts; ++i )
                {
572
                    sliceVertices->push_back( wge::wv3D2ov3( vertices[i] ) );
573
                }
574 575 576
                sliceGeometry->setVertexArray( sliceVertices );

                int c = 0;
577
                for ( std::vector< boost::shared_ptr< WDataTexture3D > >::const_iterator iter = tex.begin(); iter != tex.end(); ++iter )
578
                {
579
                    boost::shared_ptr< WGridRegular3D > grid = ( *iter )->getGrid();
schurade's avatar
schurade committed
580

581
                    osg::Vec3Array* texCoords = new osg::Vec3Array;
582 583
                    for( size_t i = 0; i < nbVerts; ++i )
                    {
584
                        texCoords->push_back( wge::wv3D2ov3( grid->worldCoordToTexCoord( vertices[i] ) ) );
585
                    }
586 587
                    sliceGeometry->setTexCoordArray( c, texCoords );
                    ++c;
588 589 590 591 592
                }
                break;
            }
            case 2:
            {
593
                std::vector< wmath::WPosition > vertices;
594 595 596 597
                vertices.push_back( wmath::WPosition( m_bb.first[0],  m_bb.first[1],  zPos ) );
                vertices.push_back( wmath::WPosition( m_bb.first[0],  m_bb.second[1], zPos ) );
                vertices.push_back( wmath::WPosition( m_bb.second[0], m_bb.second[1], zPos ) );
                vertices.push_back( wmath::WPosition( m_bb.second[0], m_bb.first[1],  zPos ) );
598 599
                for( size_t i = 0; i < nbVerts; ++i )
                {
600
                    sliceVertices->push_back( wge::wv3D2ov3( vertices[i] ) );
601 602
                }

603
                sliceGeometry->setVertexArray( sliceVertices );
604

605
                int c = 0;
606
                for ( std::vector< boost::shared_ptr< WDataTexture3D > >::const_iterator iter = tex.begin(); iter != tex.end(); ++iter )
607
                {
608 609
                    boost::shared_ptr< WGridRegular3D > grid = ( *iter )->getGrid();

610 611
                    osg::Vec3Array* texCoords = new osg::Vec3Array;

612 613
                    for( size_t i = 0; i < nbVerts; ++i )
                    {
614
                        texCoords->push_back( wge::wv3D2ov3( grid->worldCoordToTexCoord( vertices[i] ) ) );
615
                    }
616 617
                    sliceGeometry->setTexCoordArray( c, texCoords );
                    ++c;
618 619 620 621
                }
                break;
            }
        }
schurade's avatar
schurade committed
622

623 624 625 626 627 628
        osg::DrawElementsUInt* quad = new osg::DrawElementsUInt( osg::PrimitiveSet::QUADS, 0 );
        quad->push_back( 3 );
        quad->push_back( 2 );
        quad->push_back( 1 );
        quad->push_back( 0 );
        sliceGeometry->addPrimitiveSet( quad );
629
    }
schurade's avatar
schurade committed
630 631
    WKernel::getRunningKernel()->getSelectionManager()->getCrosshair()->setPosition(
            wmath::WPosition( m_sagittalPos->get(), m_coronalPos->get(), m_axialPos->get() ) );
632

633
    return sliceGeometry;
634
}
635

636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
osg::ref_ptr<osg::Geometry> WMNavSlices::createCrossGeometry( int slice )
{
    float xSlice = static_cast< float >( m_sagittalPos->get( true ) );
    float ySlice = static_cast< float >( m_coronalPos->get( true ) );
    float zSlice = static_cast< float >( m_axialPos->get( true ) );

    float xPos = xSlice + 0.5f;
    float yPos = ySlice + 0.5f;
    float zPos = zSlice + 0.5f;

    osg::ref_ptr<osg::Geometry> geometry = osg::ref_ptr<osg::Geometry>( new osg::Geometry() );

    osg::Vec3Array* crossVertices = new osg::Vec3Array;
    osg::Vec3Array* colorArray = new osg::Vec3Array;

    float minx = m_bb.first[0];
    float miny = m_bb.first[1];
    float minz = m_bb.first[2];
    float maxx = m_bb.second[0];
    float maxy = m_bb.second[1];
    float maxz = m_bb.second[2];

    std::vector< wmath::WPosition > vertices;

    switch ( slice )
    {
    case 0:
        {
            vertices.push_back( wmath::WPosition( xPos - 0.5f, miny, zPos ) );
            vertices.push_back( wmath::WPosition( xPos - 0.5f, maxy, zPos ) );
            vertices.push_back( wmath::WPosition( xPos - 0.5f, yPos, minz ) );
            vertices.push_back( wmath::WPosition( xPos - 0.5f, yPos, maxz ) );
            break;
        }
    case 1:
        {
            vertices.push_back( wmath::WPosition( minx, yPos - 0.5f, zPos ) );
            vertices.push_back( wmath::WPosition( maxx, yPos - 0.5f, zPos ) );
            vertices.push_back( wmath::WPosition( xPos, yPos - 0.5f, minz ) );
            vertices.push_back( wmath::WPosition( xPos, yPos - 0.5f, maxz ) );
            break;
        }
    case 2:
        {
            vertices.push_back( wmath::WPosition( minx, yPos, zPos + 0.5f ) );
            vertices.push_back( wmath::WPosition( maxx, yPos, zPos + 0.5f ) );
            vertices.push_back( wmath::WPosition( xPos, miny, zPos + 0.5f ) );
            vertices.push_back( wmath::WPosition( xPos, maxy, zPos + 0.5f ) );
            break;
        }
    }

    for( size_t i = 0; i < 4; ++i )
    {
        crossVertices->push_back( wge::wv3D2ov3( vertices[i] ) );
    }

    colorArray->push_back( osg::Vec3( 1.0f, 0.0f, 0.0f ) );

    geometry->setColorArray( colorArray );
    geometry->setColorBinding( osg::Geometry::BIND_OVERALL );

    geometry->setVertexArray( crossVertices );

    unsigned int rawIndexData[] = { 0, 1, 2, 3 }; // NOLINT
    std::vector< unsigned int > indexData( rawIndexData, rawIndexData + sizeof( rawIndexData ) / sizeof( unsigned int ) );
    osg::DrawElementsUInt* lines = new osg::DrawElementsUInt( osg::PrimitiveSet::LINES, indexData.begin(), indexData.end() );

    geometry->addPrimitiveSet( lines );

    // disable light for this geode as lines can't be lit properly
    osg::StateSet* state = geometry->getOrCreateStateSet();
    state->setMode( GL_LIGHTING, osg::StateAttribute::OFF | osg::StateAttribute::PROTECTED );
    state->setMode( GL_BLEND, osg::StateAttribute::OFF | osg::StateAttribute::PROTECTED );

    osg::LineWidth* linewidth = new osg::LineWidth();
    linewidth->setWidth( 1.1f );
    state->setAttributeAndModes( linewidth, osg::StateAttribute::ON );

    return geometry;
}

718
void WMNavSlices::updateGeometry()
719
{
720 721
    boost::shared_lock<boost::shared_mutex> slock;
    slock = boost::shared_lock<boost::shared_mutex>( m_updateLock );
schurade's avatar
schurade committed
722

723
    if ( m_textureChanged // Depends on call order of update routines in callback.
724 725 726 727 728
         || m_sagittalPos->changed()
         || m_coronalPos->changed()
         || m_axialPos->changed()
        )
    {
729 730 731
        // if the texture got changed we want to rearange the scene Matrix for the SliceViewports to be centered
        updateViewportMatrix();

732 733 734 735 736 737 738 739 740 741
        osg::ref_ptr<osg::Geometry> xSliceGeometry = createGeometry( 0 );
        osg::ref_ptr<osg::Geometry> ySliceGeometry = createGeometry( 1 );
        osg::ref_ptr<osg::Geometry> zSliceGeometry = createGeometry( 2 );

        osg::ref_ptr<osg::Drawable> oldx = osg::ref_ptr<osg::Drawable>( m_xSliceNode->getDrawable( 0 ) );
        m_xSliceNode->replaceDrawable( oldx, xSliceGeometry );
        osg::ref_ptr<osg::Drawable> oldy = osg::ref_ptr<osg::Drawable>( m_ySliceNode->getDrawable( 0 ) );
        m_ySliceNode->replaceDrawable( oldy, ySliceGeometry );
        osg::ref_ptr<osg::Drawable> oldz = osg::ref_ptr<osg::Drawable>( m_zSliceNode->getDrawable( 0 ) );
        m_zSliceNode->replaceDrawable( oldz, zSliceGeometry );
742 743 744 745 746 747 748 749 750 751 752

        osg::ref_ptr<osg::Geometry> xCrossGeometry = createCrossGeometry( 0 );
        osg::ref_ptr<osg::Geometry> yCrossGeometry = createCrossGeometry( 1 );
        osg::ref_ptr<osg::Geometry> zCrossGeometry = createCrossGeometry( 2 );

        osg::ref_ptr<osg::Drawable> oldcx = osg::ref_ptr<osg::Drawable>( m_xCrossNode->getDrawable( 0 ) );
        m_xCrossNode->replaceDrawable( oldcx, xCrossGeometry );
        osg::ref_ptr<osg::Drawable> oldcy = osg::ref_ptr<osg::Drawable>( m_yCrossNode->getDrawable( 0 ) );
        m_yCrossNode->replaceDrawable( oldcy, yCrossGeometry );
        osg::ref_ptr<osg::Drawable> oldcz = osg::ref_ptr<osg::Drawable>( m_zCrossNode->getDrawable( 0 ) );
        m_zCrossNode->replaceDrawable( oldcz, zCrossGeometry );
753
    }
754

755
    std::vector< boost::shared_ptr< WDataTexture3D > > tex = WDataHandler::getDefaultSubject()->getDataTextures( true );
Sebastian Eichelbaum's avatar
[STYLE]  
Sebastian Eichelbaum committed
756
    bool noSlices = ( tex.size() == 0 ) || !m_active->get();
757

758
    if ( m_showAxial->get() && !noSlices )
759
    {
760 761 762 763 764
        m_zSliceNode->setNodeMask( 0xFFFFFFFF );
    }
    else
    {
        m_zSliceNode->setNodeMask( 0x0 );
765 766
    }

767
    if ( m_showCoronal->get() && !noSlices )
768
    {
769
        m_ySliceNode->setNodeMask( 0xFFFFFFFF );
770 771 772
    }
    else
    {
773
        m_ySliceNode->setNodeMask( 0x0 );
774 775
    }

776
    if ( m_showSagittal->get() && !noSlices )
777
    {
778
        m_xSliceNode->setNodeMask( 0xFFFFFFFF );
779 780 781
    }
    else
    {
782
        m_xSliceNode->setNodeMask( 0x0 );
783
    }
784 785

    slock.unlock();
786 787 788
}


789
void WMNavSlices::updateTextures()
790
{
791
    osg::StateSet* rootState = m_slicesNode->getOrCreateStateSet();
792
    if ( m_textureChanged )
793
    {
794
        m_textureChanged = false;
795

796
        // grab a list of data textures
797
        std::vector< boost::shared_ptr< WDataTexture3D > > tex = WDataHandler::getDefaultSubject()->getDataTextures( true );
798 799

        if ( tex.size() > 0 )
800
        {
801
            // reset all uniforms
802
            for ( int i = 0; i < m_maxNumberOfTextures; ++i )
803 804 805
            {
                m_typeUniforms[i]->set( 0 );
            }
806

807
            // for each texture -> apply
808
            int c = 0;
809
            for ( std::vector< boost::shared_ptr< WDataTexture3D > >::const_iterator iter = tex.begin(); iter != tex.end(); ++iter )
810
            {
811
                osg::ref_ptr<osg::Texture3D> texture3D = ( *iter )->getTexture();
schurade's avatar
schurade committed
812 813 814 815 816 817 818 819 820 821 822

                if ( ( *iter )->isInterpolated() )
                {
                    texture3D->setFilter( osg::Texture::MIN_FILTER, osg::Texture::LINEAR );
                    texture3D->setFilter( osg::Texture::MAG_FILTER, osg::Texture::LINEAR );
                }
                else
                {
                    texture3D->setFilter( osg::Texture::MIN_FILTER, osg::Texture::NEAREST );
                    texture3D->setFilter( osg::Texture::MAG_FILTER, osg::Texture::NEAREST );
                }
823
                rootState->setTextureAttributeAndModes( c, texture3D, osg::StateAttribute::ON );
824

825 826 827
                // set threshold/opacity as uniforms
                float t = ( *iter )->getThreshold() / 255.0;
                float a = ( *iter )->getAlpha();
828
                int cmap = ( *iter )->getSelectedColormap();
829

830 831 832
                m_typeUniforms[c]->set( ( *iter )->getDataType() );
                m_thresholdUniforms[c]->set( t );
                m_alphaUniforms[c]->set( a );
833
                m_cmapUniforms[c]->set( cmap );
834

835
                ++c;
836 837 838 839
                if( c == m_maxNumberOfTextures )
                {
                    break;
                }
840 841
            }
        }
842
    }
843 844 845 846 847 848 849 850

    m_highlightUniformSagittal->set( m_isPickedSagittal );
    m_highlightUniformCoronal->set( m_isPickedCoronal );
    m_highlightUniformAxial->set( m_isPickedAxial );

    m_xSliceNode->getOrCreateStateSet()->merge( *rootState );
    m_ySliceNode->getOrCreateStateSet()->merge( *rootState );
    m_zSliceNode->getOrCreateStateSet()->merge( *rootState );
851 852
}

853
void WMNavSlices::initUniforms( osg::StateSet* rootState )
854
{
855 856 857
    boost::shared_lock<boost::shared_mutex> slock;
    slock = boost::shared_lock<boost::shared_mutex>( m_updateLock );

858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900
    m_typeUniforms.push_back( osg::ref_ptr<osg::Uniform>( new osg::Uniform( "type0", 0 ) ) );
    m_typeUniforms.push_back( osg::ref_ptr<osg::Uniform>( new osg::Uniform( "type1", 0 ) ) );
    m_typeUniforms.push_back( osg::ref_ptr<osg::Uniform>( new osg::Uniform( "type2", 0 ) ) );
    m_typeUniforms.push_back( osg::ref_ptr<osg::Uniform>( new osg::Uniform( "type3", 0 ) ) );
    m_typeUniforms.push_back( osg::ref_ptr<osg::Uniform>( new osg::Uniform( "type4", 0 ) ) );
    m_typeUniforms.push_back( osg::ref_ptr<osg::Uniform>( new osg::Uniform( "type5", 0 ) ) );
    m_typeUniforms.push_back( osg::ref_ptr<osg::Uniform>( new osg::Uniform( "type6", 0 ) ) );
    m_typeUniforms.push_back( osg::ref_ptr<osg::Uniform>( new osg::Uniform( "type7",