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

28
#include <osg/LightModel>
29
#include <osgGA/StateSetManipulator>
30
#include <osgGA/TrackballManipulator>
31 32 33 34 35
#include <osgViewer/ViewerEventHandlers>
#include <osgWidget/Util> //NOLINT
#include <osgWidget/ViewerEventHandlers> //NOLINT
#include <osgWidget/WindowManager> //NOLINT

36 37
#include "core/kernel/WKernel.h"
#include "core/kernel/WSelectionManager.h"
38

39
#include "WMDatasetProfile.h"
40
#include "WMDatasetProfile.xpm" // Please put a real icon here.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

// This line is needed by the module loader to actually find your module. Do not remove. Do NOT add a ";" here.
W_LOADABLE_MODULE( WMDatasetProfile )

WMDatasetProfile::WMDatasetProfile():
    WModule(),
    m_dirty( true )
{
}

WMDatasetProfile::~WMDatasetProfile()
{
    // Cleanup!
}

boost::shared_ptr< WModule > WMDatasetProfile::factory() const
{
    // See "src/modules/template/" for an extensively documented example.
    return boost::shared_ptr< WModule >( new WMDatasetProfile() );
}

const char** WMDatasetProfile::getXPMIcon() const
{
64
    return WMDatasetProfile_xpm; // Please put a real icon here.
65 66 67
}
const std::string WMDatasetProfile::getName() const
{
68
    return "Dataset Profile";
69 70 71 72
}

const std::string WMDatasetProfile::getDescription() const
{
73
    return "Show values along a 3D profile through the dataset as a graph.";
74 75 76 77 78
}

void WMDatasetProfile::connectors()
{
    // the input dataset is just used as source for resolurtion and transformation matrix
79 80
    m_input = boost::shared_ptr< WModuleInputData < WDataSetScalar  > >(
        new WModuleInputData< WDataSetScalar >( shared_from_this(), "in", "The input dataset." ) );
81 82 83 84 85 86 87 88 89 90
    addConnector( m_input );

    WModule::connectors();
}

void WMDatasetProfile::properties()
{
    m_propCondition = boost::shared_ptr< WCondition >( new WCondition() );

    m_snapSelectionsList = boost::shared_ptr< WItemSelection >( new WItemSelection() );
91 92 93 94
    m_snapSelectionsList->addItem( "Free", "No snapping." );
    m_snapSelectionsList->addItem( "Axial slice", "Snap to axial slice." );
    m_snapSelectionsList->addItem( "Coronal slice", "Snap to coronal slice." );
    m_snapSelectionsList->addItem( "Sagittal slice", "Snap to sagittal slice." );
95

96
    m_snapSelection = m_properties->addProperty( "Snap to", "Snap the profile to one of the slices of the Navigation Slices Module.",
97 98 99
            m_snapSelectionsList->getSelectorFirst(), m_propCondition );
    WPropertyHelper::PC_SELECTONLYONE::addTo( m_snapSelection );

Alexander Wiebel's avatar
[STYLE]  
Alexander Wiebel committed
100 101 102 103 104 105 106 107 108 109 110 111
    m_propAddKnobTrigger = m_properties->addProperty( "Add knob",
                                                      "Add a segment and a corresponding manipulator knob to profile.",
                                                      WPVBaseTypes::PV_TRIGGER_READY,
                                                      m_propCondition );

    m_graphColor = m_properties->addProperty( "Graph color",
                                              "Color of the curve representing the data profile graph.",
                                              WColor( 0.2, 0.2, 0.2, 1.0 ) );

    m_propLength = m_properties->addProperty( "Length",
                                              "Length of profile segments. Needs to activated by Use Length property",
                                              60.0 );
112 113
    m_propLength->setMin( 1 );
    m_propLength->setMax( 500 );
114
    m_propUseLength = m_properties->addProperty( "Use length", "Apply the value of the Length property.", false );
115

Alexander Wiebel's avatar
[STYLE]  
Alexander Wiebel committed
116 117 118
    m_propInterpolate = m_properties->addProperty( "Interpolate",
                                                   "Use value of voxel closest to sample or interpolate between surrounding values.",
                                                   true );
119

120
    m_propNumSamples = m_properties->addProperty( "Number of sample points", "Number of samples along the overall profile.", 100 );
121 122 123
    m_propNumSamples->setMin( 1 );
    m_propNumSamples->setMax( 500 );

124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
    WModule::properties();
}

void WMDatasetProfile::requirements()
{
    // Put the code for your requirements here. See "src/modules/template/" for an extensively documented example.
}

void WMDatasetProfile::moduleMain()
{
    m_moduleState.setResetable( true, true );
    m_moduleState.add( m_input->getDataChangedCondition() );
    m_moduleState.add( m_propCondition );
    m_moduleState.add( m_active->getUpdateCondition() );

    init();

    ready();

    m_dirty = true;

    // wait for a dataset to be connected, most likely an anatomy dataset
146
    while( !m_shutdownFlag() )
147 148 149
    {
        m_moduleState.wait();

150
        if( m_shutdownFlag() )
151 152 153 154
        {
            break;
        }

155
        boost::shared_ptr< WDataSetScalar > newDataSet = m_input->getData();
156
        bool dataChanged = ( m_dataSet != newDataSet );
157
        bool dataValid = ( newDataSet != NULL );
158 159 160

        if( dataValid )
        {
161
            if( dataChanged )
162 163
            {
                m_dataSet = newDataSet;
164
                m_grid = boost::dynamic_pointer_cast< WGridRegular3D >( m_dataSet->getGrid() );
165 166 167
            }
        }

168
        if( m_snapSelection->changed( true ) )
169 170 171 172
        {
            m_dirty = true;
        }

173
        if( m_propAddKnobTrigger->changed( true ) )
174
        {
175
            WPosition center = WKernel::getRunningKernel()->getSelectionManager()->getCrosshair()->getPosition();
176 177 178 179 180 181
            addKnob( center );

            m_propAddKnobTrigger->set( WPVBaseTypes::PV_TRIGGER_READY, true );
        }


182
        if( m_propUseLength->changed() || m_propLength->changed() || m_propInterpolate->changed( true ) || m_propNumSamples->changed( true ) )
183 184 185 186 187
        {
            m_dirty = true;
        }
    }

188
    for( size_t i = 0; i < knobs.size(); ++i )
189 190 191 192 193 194 195 196 197 198
    {
        WGraphicsEngine::getGraphicsEngine()->getScene()->remove( &( *knobs[i] ) );
        knobs[i]->removeROIChangeNotifier( m_changeRoiSignal );
    }

    WKernel::getRunningKernel()->getGraphicsEngine()->getScene()->remove( m_rootNode );
}

void WMDatasetProfile::updateCallback()
{
199
    WPosition center = WKernel::getRunningKernel()->getSelectionManager()->getCrosshair()->getPosition();
200

201
    for( size_t i = 0; i < knobs.size(); ++i )
202 203 204 205 206
    {
        knobs[i]->setLockX( false );
        knobs[i]->setLockY( false );
        knobs[i]->setLockZ( false );
    }
207
    switch( m_snapSelection->get( true ).getItemIndexOfSelected( 0 ) )
208 209 210 211
    {
        case 0:
            break;
        case 1:
212
            for( size_t i = 0; i < knobs.size(); ++i )
213 214 215 216 217 218
            {
                knobs[i]->setZ( center[2] );
                knobs[i]->setLockZ( true );
            }
            break;
        case 2:
219
            for( size_t i = 0; i < knobs.size(); ++i )
220 221 222 223 224 225
            {
                knobs[i]->setY( center[1] );
                knobs[i]->setLockY( true );
            }
            break;
        case 3:
226
            for( size_t i = 0; i < knobs.size(); ++i )
227 228 229 230 231 232 233 234 235
            {
                knobs[i]->setX( center[0] );
                knobs[i]->setLockX( true );
            }
            break;
        default:
            break;
    }

236
        if( m_propUseLength->get( true ) )
237
        {
238
            for( size_t i = 0; i < knobs.size() - 1; ++i )
239
            {
240 241
                WPosition p1 = knobs[i]->getPosition();
                WPosition p2 = knobs[i+1]->getPosition();
242 243 244 245 246 247 248

                float l = sqrt( ( p1[0] - p2[0] ) * ( p1[0] - p2[0] ) +
                                ( p1[1] - p2[1] ) * ( p1[1] - p2[1] ) +
                                ( p1[2] - p2[2] ) * ( p1[2] - p2[2] ) );

                float mult = m_propLength->get( true ) / l;

249
                WPosition vec = p2 - p1;
250

251
                if( ( fabs( l - static_cast<float>( m_propLength->get( true ) ) ) ) > 0.001 )
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
                {
                    knobs[i+1]->setPosition( p1 + vec * mult );
                }
            }
        }

    m_dirty = true;
    update();
}

void WMDatasetProfile::setDirty()
{
    m_dirty = true;
}


void WMDatasetProfile::init()
{
    m_rootNode = new WGEManagedGroupNode( m_active );
    WKernel::getRunningKernel()->getGraphicsEngine()->getScene()->insert( m_rootNode );
    m_lineGeode = osg::ref_ptr< osg::Geode >( new osg::Geode() );
    m_lineGeode->setName( "_line" );
    m_rootNode->insert( m_lineGeode );

    m_changeRoiSignal
            = boost::shared_ptr< boost::function< void() > >( new boost::function< void() >( boost::bind( &WMDatasetProfile::setDirty, this ) ) );

279
    WPosition center = WKernel::getRunningKernel()->getSelectionManager()->getCrosshair()->getPosition();
280

281 282
    addKnob( WPosition( center[0] + 30, center[1], center[2] ) );
    addKnob( WPosition( center[0] - 30, center[1], center[2] ) );
283 284 285 286 287 288 289 290 291 292 293 294 295 296

    osg::ref_ptr<osgViewer::View> viewer = WKernel::getRunningKernel()->getGraphicsEngine()->getViewer()->getView();

    int height = viewer->getCamera()->getViewport()->height();
    int width = viewer->getCamera()->getViewport()->width();

    m_oldViewHeight = height;
    m_oldViewWidth = width;

    m_graphNode = osg::ref_ptr< WGEManagedGroupNode >( new WGEManagedGroupNode( m_active ) );
    m_rootNode->insert( m_graphNode );

    m_wm = new osgWidget::WindowManager( viewer, 0.0f, 0.0f, MASK_2D );

297
    m_camera = new WGECamera();
298 299 300 301 302 303
    m_camera->getOrCreateStateSet()->setMode( GL_LIGHTING, osg::StateAttribute::PROTECTED | osg::StateAttribute::OFF );

    m_camera->setProjectionMatrix( osg::Matrix::ortho2D( 0.0, width, 0.0f, height ) );
    m_camera->setReferenceFrame( osg::Transform::ABSOLUTE_RF );
    m_camera->setViewMatrix( osg::Matrix::identity() );
    m_camera->setClearMask( GL_DEPTH_BUFFER_BIT );
304
    m_camera->setRenderOrder( WGECamera::POST_RENDER );
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321

    m_graphNode->addChild( m_camera );
    m_camera->addChild( m_wm );

    viewer->addEventHandler( new osgWidget::MouseHandler( m_wm ) );
    viewer->addEventHandler( new osgWidget::KeyboardHandler( m_wm ) );
    viewer->addEventHandler( new osgWidget::ResizeHandler( m_wm, m_camera ) );
    viewer->addEventHandler( new osgWidget::CameraSwitchHandler( m_wm, m_camera ) );
    viewer->addEventHandler( new osgViewer::StatsHandler() );
    viewer->addEventHandler( new osgViewer::WindowSizeHandler() );
    viewer->addEventHandler( new osgGA::StateSetManipulator( viewer->getCamera()->getOrCreateStateSet() ) );

    m_wm->resizeAllWindows();

    m_rootNode->addUpdateCallback( new WGEFunctorCallback< osg::Node >( boost::bind( &WMDatasetProfile::updateCallback, this ) ) );
}

322
void WMDatasetProfile::addKnob( WPosition pos )
323 324 325 326 327 328
{
    osg::ref_ptr<WROISphere> s = osg::ref_ptr<WROISphere>( new WROISphere( pos, 2.5 ) );
    WGraphicsEngine::getGraphicsEngine()->getScene()->addChild( &( *s ) );
    knobs.push_back( s );
    s->addROIChangeNotifier( m_changeRoiSignal );

329
    for( size_t i = 0; i < knobs.size(); ++i )
330 331 332 333 334 335 336 337 338 339 340
    {
        knobs[i]->setColor( osg::Vec4( 0.0, 1.0, 1.0, 1.0 ) );
    }
    knobs[0]->setColor( osg::Vec4( 0.0, 1.0, 0.0, 1.0 ) );
    knobs[knobs.size()- 1]->setColor( osg::Vec4( 1.0, 0.0, 0.0, 1.0 ) );
}

void WMDatasetProfile::update()
{
    m_lineGeode->removeDrawables( 0, 1 );

341
    if( m_active->get() )
342 343 344
    {
        osg::ref_ptr< osg::Vec3Array > vertices = osg::ref_ptr< osg::Vec3Array >( new osg::Vec3Array );
        osg::ref_ptr< osg::Vec4Array > colors = osg::ref_ptr< osg::Vec4Array >( new osg::Vec4Array );
345
        osg::ref_ptr< osg::Geometry > geometry = osg::ref_ptr< osg::Geometry >( new osg::Geometry );
346

347
        for( size_t i = 0; i < knobs.size(); ++i )
348 349 350 351 352 353 354 355 356
        {
            vertices->push_back( osg::Vec3( knobs[i]->getPosition() ) );
        }

        geometry->addPrimitiveSet( new osg::DrawArrays( osg::PrimitiveSet::LINE_STRIP, 0, vertices->size() ) );
        geometry->setVertexArray( vertices );

        colors->push_back( m_graphColor->get() );
        geometry->setColorArray( colors );
357
        geometry->setColorBinding( osg::Geometry::BIND_OVERALL );
358 359 360 361 362 363 364 365 366 367 368 369

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

        m_lineGeode->addDrawable( geometry );

        osg::ref_ptr<osgViewer::View> viewer = WKernel::getRunningKernel()->getGraphicsEngine()->getViewer()->getView();

        int height = viewer->getCamera()->getViewport()->height();
        int width = viewer->getCamera()->getViewport()->width();

370
        if( ( height != m_oldViewHeight ) || width != m_oldViewWidth || m_active->changed( true ) )
371 372 373 374 375 376 377 378 379 380 381 382 383
        {
            m_oldViewHeight = height;
            m_oldViewWidth = width;

            m_camera->removeChild( m_graphGridGeode );
            m_graphGridGeode = createGraphGridGeode();
            m_camera->addChild( m_graphGridGeode );
        }

        m_camera->removeChild( m_graphGeode );
        m_graphGeode = createGraphGeode();
        m_camera->addChild( m_graphGeode );

384
        for( size_t i = 0; i < knobs.size(); ++i )
385 386 387 388 389 390 391 392
        {
            knobs[i]->unhide();
        }

        m_dirty = false;
    }
    else
    {
393
        for( size_t i = 0; i < knobs.size(); ++i )
394 395 396 397 398 399 400 401 402 403 404 405 406 407
        {
            knobs[i]->hide();
        }
        m_camera->removeChild( m_graphGridGeode );
        m_camera->removeChild( m_graphGeode );
    }
}

osg::ref_ptr< osg::Geode > WMDatasetProfile::createGraphGridGeode()
{
    osg::ref_ptr< osg::Geode > newGeode = osg::ref_ptr< osg::Geode >( new osg::Geode() );

    osg::ref_ptr< osg::Vec3Array > vertices = osg::ref_ptr< osg::Vec3Array >( new osg::Vec3Array );
    osg::ref_ptr< osg::Vec4Array > colors = osg::ref_ptr< osg::Vec4Array >( new osg::Vec4Array );
408
    osg::ref_ptr< osg::Geometry > geometry = osg::ref_ptr< osg::Geometry >( new osg::Geometry );
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444

    int step = ( m_oldViewWidth - 20 ) / 10;
    int i;
    for( i = 10; i < m_oldViewWidth; i += step )
    {
        vertices->push_back( osg::Vec3( i, m_oldViewHeight / 2, 0 ) );
        vertices->push_back( osg::Vec3( i, 10, 0 ) );
    }
    i -= step;
    int yStep = ( m_oldViewHeight / 2 - 10 ) / 4;

    vertices->push_back( osg::Vec3( 10, 10, 0 ) );
    vertices->push_back( osg::Vec3( i, 10, 0 ) );

    vertices->push_back( osg::Vec3( 10, yStep, 0 ) );
    vertices->push_back( osg::Vec3( i, yStep, 0 ) );

    vertices->push_back( osg::Vec3( 10, yStep * 2, 0 ) );
    vertices->push_back( osg::Vec3( i, yStep * 2, 0 ) );

    vertices->push_back( osg::Vec3( 10, yStep * 3, 0 ) );
    vertices->push_back( osg::Vec3( i, yStep * 3, 0 ) );

    vertices->push_back( osg::Vec3( 10, m_oldViewHeight / 2, 0 ) );
    vertices->push_back( osg::Vec3( i, m_oldViewHeight / 2, 0 ) );

    vertices->push_back( osg::Vec3( 10, 10, 0 ) );
    vertices->push_back( osg::Vec3( i, 10, 0 ) );


    geometry->addPrimitiveSet( new osg::DrawArrays( osg::PrimitiveSet::LINES, 0, vertices->size() ) );
    geometry->setVertexArray( vertices );

    WColor color( 0.7, 0.7, 0.7, 1.0 );
    colors->push_back( color );
    geometry->setColorArray( colors );
445
    geometry->setColorBinding( osg::Geometry::BIND_OVERALL );
446 447 448 449 450 451 452 453 454 455 456 457

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

    newGeode->addDrawable( geometry );

    return newGeode;
}

osg::ref_ptr< osg::Geode > WMDatasetProfile::createGraphGeode()
{
458 459 460 461 462
    if( !m_dataSet )
    {
        return osg::ref_ptr< osg::Geode >();
    }

463 464 465 466
    osg::ref_ptr< osg::Geode > newGeode = osg::ref_ptr< osg::Geode >( new osg::Geode() );

    osg::ref_ptr< osg::Vec3Array > vertices = osg::ref_ptr< osg::Vec3Array >( new osg::Vec3Array );
    osg::ref_ptr< osg::Vec4Array > colors = osg::ref_ptr< osg::Vec4Array >( new osg::Vec4Array );
467
    osg::ref_ptr< osg::Geometry > geometry = osg::ref_ptr< osg::Geometry >( new osg::Geometry );
468 469 470 471 472 473 474

    std::vector<float>knobPositions;

    float max = m_dataSet->getValueSet()->getMaximumValue();
    float value;

    float overallLength = 0;
475
    std::vector<float> segmentLengths;
476
    for( size_t k = 0; k < knobs.size() - 1 ; ++k )
477
    {
478
        WPosition p = ( knobs[k+1]->getPosition() - knobs[k]->getPosition() );
479 480
        segmentLengths.push_back( length( p ) );
        overallLength += length( p );
481 482 483 484
    }

    float x = 12;
    float y;
485
    for( size_t k = 0; k < knobs.size() - 1 ; ++k )
486 487
    {
        float l1 = segmentLengths[k] / overallLength;
488
        int steps = m_propNumSamples->get() * l1;
489 490

        knobPositions.push_back( x );
491
        WPosition p1 = ( knobs[k+1]->getPosition() - knobs[k]->getPosition() ) / static_cast<float>( steps );
492

493
        for( int i = 0; i < steps; ++i )
494
        {
495
            if( m_propInterpolate->get() )
496 497 498 499 500 501 502 503 504 505
            {
                bool success;
                value = m_dataSet->interpolate( knobs[k]->getPosition() + p1 * i, &success );
            }
            else
            {
                value = m_dataSet->getValueAt( m_grid->getVoxelNum( knobs[k]->getPosition() + p1 * i ) );
            }


506
            x = x + ( m_oldViewWidth /  m_propNumSamples->get() );
507 508 509 510 511 512
            y = 10 + ( value / max * ( m_oldViewHeight - 20 ) / 2 );
            vertices->push_back( osg::Vec3( x, y, 0 ) );
        }
    }
    knobPositions.push_back( x );

513
    for( size_t j = 0; j < knobPositions.size(); ++j )
514 515 516 517 518 519 520 521 522 523 524 525 526
    {
        vertices->push_back( osg::Vec3( knobPositions[j], m_oldViewHeight / 2, 0 ) );
        vertices->push_back( osg::Vec3( knobPositions[j], 10, 0 ) );
    }

    geometry->addPrimitiveSet( new osg::DrawArrays( osg::PrimitiveSet::LINE_STRIP, 0, vertices->size() - knobPositions.size() * 2 ) );
    geometry->setVertexArray( vertices );

    geometry->addPrimitiveSet( new osg::DrawArrays( osg::PrimitiveSet::LINES, vertices->size() - ( knobPositions.size() ) * 2,
            ( knobPositions.size() + 1 ) * 2 ) );

    colors->push_back( m_graphColor->get() );
    geometry->setColorArray( colors );
527
    geometry->setColorBinding( osg::Geometry::BIND_OVERALL );
528 529 530 531 532 533 534 535 536

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

    newGeode->addDrawable( geometry );

    return newGeode;
}