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

#include "../../kernel/WKernel.h"

31
#include "../../common/WBoundingBox.h"
32
#include "../../dataHandler/WDataHandler.h"
33
#include "../../dataHandler/WDataTexture3D_2.h"
34
#include "../../dataHandler/WSubject.h"
Alexander Wiebel's avatar
Alexander Wiebel committed
35
#include "WMDatasetManipulator.xpm"
36 37 38 39 40 41 42

#include "WMDatasetManipulator.h"

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

WMDatasetManipulator::WMDatasetManipulator():
43 44 45
    WModule(),
    m_updated( false ),
    m_updateMutex()
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
{
}

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

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

const char** WMDatasetManipulator::getXPMIcon() const
{
Alexander Wiebel's avatar
Alexander Wiebel committed
62
    return WMDatasetManipulator_xpm; // Please put a real icon here.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
}
const std::string WMDatasetManipulator::getName() const
{
    // Specify your module name here. This name must be UNIQUE!
    return "DatasetManipulator";
}

const std::string WMDatasetManipulator::getDescription() const
{
    // Specify your module description here. Be detailed. This text is read by the user.
    // See "src/modules/template/" for an extensively documented example.
    return "This module allows manipulation of the dataset properties via manipulators in the 3D scene";
}

void WMDatasetManipulator::connectors()
{
    // the input dataset is just used as source for resolurtion and transformation matrix
    m_input = boost::shared_ptr< WModuleInputData < WDataSetSingle  > >(
        new WModuleInputData< WDataSetSingle >( shared_from_this(), "in", "The input dataset." ) );
    addConnector( m_input );

    m_output = boost::shared_ptr< WModuleOutputData < WDataSetSingle  > >(
        new WModuleOutputData< WDataSetSingle >( shared_from_this(), "out", "The modified dataset." ) );
    addConnector( m_output );

    WModule::connectors();
}

void WMDatasetManipulator::properties()
{
    // Initialize the properties
    m_propCondition = boost::shared_ptr< WCondition >( new WCondition() );
95
    m_transformChangedCondition = boost::shared_ptr< WCondition >( new WCondition() );
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140

    m_showManipulators = m_properties->addProperty( "Show manipulators", "Hide/Show manipulators.", true, m_propCondition );
    m_rotationMode = m_properties->addProperty( "Rotation mode", "Toggles rotation mode", false, m_propCondition );

    m_groupTexManip = m_properties->addPropertyGroup( "Texture Manipulation",  "Properties only related to the texture manipulation." );

    m_translationX = m_groupTexManip->addProperty( "X translation", "", 0, m_propCondition );
    m_translationX->setMax( 500 );
    m_translationX->setMin( -500 );
    m_translationY = m_groupTexManip->addProperty( "Y translation", "", 0, m_propCondition );
    m_translationY->setMax( 500 );
    m_translationY->setMin( -500 );
    m_translationZ = m_groupTexManip->addProperty( "Z translation", "", 0, m_propCondition );
    m_translationZ->setMax( 500 );
    m_translationZ->setMin( -500 );

    m_stretchX = m_groupTexManip->addProperty( "Voxel size X", "", 1.0, m_propCondition );
    m_stretchX->setMax( 10. );
    m_stretchX->setMin( -10. );
    m_stretchY = m_groupTexManip->addProperty( "Voxel size Y", "", 1.0, m_propCondition );
    m_stretchY->setMax( 10. );
    m_stretchY->setMin( -10. );
    m_stretchZ = m_groupTexManip->addProperty( "Voxel size Z", "", 1.0, m_propCondition );
    m_stretchZ->setMax( 10. );
    m_stretchZ->setMin( -10. );

    m_rotationX = m_groupTexManip->addProperty( "X rotation", "", 0, m_propCondition );
    m_rotationX->setMax( 180 );
    m_rotationX->setMin( -180 );
    m_rotationY = m_groupTexManip->addProperty( "Y rotation", "", 0, m_propCondition );
    m_rotationY->setMax( 180 );
    m_rotationY->setMin( -180 );
    m_rotationZ = m_groupTexManip->addProperty( "Z rotation", "", 0, m_propCondition );
    m_rotationZ->setMax( 180 );
    m_rotationZ->setMin( -180 );
    // TODO(schurade): activate this
    m_rotationX->setHidden( true );
    m_rotationY->setHidden( true );
    m_rotationZ->setHidden( true );

    WModule::properties();
}

void WMDatasetManipulator::init()
{
141 142
    m_grid = boost::shared_dynamic_cast< WGridRegular3D >( m_input->getData()->getGrid() );
    WAssert( m_grid, "The grid needs to be a regular 3D grid." );
143 144
    m_transform = boost::shared_ptr< WGridTransformOrtho >( new WGridTransformOrtho( m_grid->getTransform() ) );

145
    WBoundingBox bb = m_grid->getBoundingBox();
146

147
    wmath::WPosition center = bb.center();
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172

    m_knobCenter = boost::shared_ptr<WROISphere>( new WROISphere( center, 2.5 ) );
    m_knobx1 = boost::shared_ptr<WROISphere>( new WROISphere( center, 2.5 ) );
    m_knobx2 = boost::shared_ptr<WROISphere>( new WROISphere( center, 2.5 ) );
    m_knoby1 = boost::shared_ptr<WROISphere>( new WROISphere( center, 2.5 ) );
    m_knoby2 = boost::shared_ptr<WROISphere>( new WROISphere( center, 2.5 ) );
    m_knobz1 = boost::shared_ptr<WROISphere>( new WROISphere( center, 2.5 ) );
    m_knobz2 = boost::shared_ptr<WROISphere>( new WROISphere( center, 2.5 ) );

    m_knobRotCenter = boost::shared_ptr<WROISphere>( new WROISphere( center, 2.5 ) );
    m_knobRot = boost::shared_ptr<WROISphere>( new WROISphere( center, 2.5 ) );

    setManipulatorsFromBoundingBox();

    WGraphicsEngine::getGraphicsEngine()->getScene()->addChild( &( *m_knobCenter ) );
    WGraphicsEngine::getGraphicsEngine()->getScene()->addChild( &( *m_knobx1 ) );
    WGraphicsEngine::getGraphicsEngine()->getScene()->addChild( &( *m_knobx2 ) );
    WGraphicsEngine::getGraphicsEngine()->getScene()->addChild( &( *m_knoby1 ) );
    WGraphicsEngine::getGraphicsEngine()->getScene()->addChild( &( *m_knoby2 ) );
    WGraphicsEngine::getGraphicsEngine()->getScene()->addChild( &( *m_knobz1 ) );
    WGraphicsEngine::getGraphicsEngine()->getScene()->addChild( &( *m_knobz2 ) );

    WGraphicsEngine::getGraphicsEngine()->getScene()->addChild( &( *m_knobRotCenter ) );
    WGraphicsEngine::getGraphicsEngine()->getScene()->addChild( &( *m_knobRot ) );

173 174 175
    using boost::function;
    m_changeRoiSignal
        = boost::shared_ptr< function< void() > >( new function< void() >( boost::bind( &WMDatasetManipulator::manipulatorMoved, this ) ) );
176 177 178 179 180 181 182
    m_knobCenter->addROIChangeNotifier( m_changeRoiSignal );
    m_knobx1->addROIChangeNotifier( m_changeRoiSignal );
    m_knobx2->addROIChangeNotifier( m_changeRoiSignal );
    m_knoby1->addROIChangeNotifier( m_changeRoiSignal );
    m_knoby2->addROIChangeNotifier( m_changeRoiSignal );
    m_knobz1->addROIChangeNotifier( m_changeRoiSignal );
    m_knobz2->addROIChangeNotifier( m_changeRoiSignal );
183 184 185

    m_changeRotRoiSignal
        = boost::shared_ptr< function< void() > >( new function< void() >( boost::bind( &WMDatasetManipulator::manipulatorRotMoved, this ) ) );
186 187
    m_knobRotCenter->addROIChangeNotifier( m_changeRotRoiSignal );
    m_knobRot->addROIChangeNotifier( m_changeRotRoiSignal );
188 189 190 191 192 193

    setManipulatorMode();
}

void WMDatasetManipulator::setManipulatorsFromBoundingBox()
{
194 195
    WGridRegular3D g( m_grid->getNbCoordsX(), m_grid->getNbCoordsY(), m_grid->getNbCoordsZ(), *m_transform );
    WBoundingBox bb = g.getBoundingBox();
196 197 198 199 200 201 202 203 204

    m_posCenter = bb.center();

    m_posx1 = wmath::WPosition( bb.xMin(), m_posCenter[1], m_posCenter[2] );
    m_posx2 = wmath::WPosition( bb.xMax(), m_posCenter[1], m_posCenter[2] );
    m_posy1 = wmath::WPosition( m_posCenter[0], bb.yMin(), m_posCenter[2] );
    m_posy2 = wmath::WPosition( m_posCenter[0], bb.yMax(), m_posCenter[2] );
    m_posz1 = wmath::WPosition( m_posCenter[0], m_posCenter[1], bb.zMin() );
    m_posz2 = wmath::WPosition( m_posCenter[0], m_posCenter[1], bb.zMax() );
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 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 279 280 281 282 283 284 285 286 287 288 289 290 291 292

    m_knobCenter->setPosition( m_posCenter );
    m_knobx1->setPosition( m_posx1 );
    m_knobx2->setPosition( m_posx2 );
    m_knoby1->setPosition( m_posy1 );
    m_knoby2->setPosition( m_posy2 );
    m_knobz1->setPosition( m_posz1 );
    m_knobz2->setPosition( m_posz2 );

    m_posCenterOrig = m_posCenter;
    m_posx1Orig = m_posx1;
    m_posx2Orig = m_posx2;
    m_posy1Orig = m_posy1;
    m_posy2Orig = m_posy2;
    m_posz1Orig = m_posz1;
    m_posz2Orig = m_posz2;

    m_posRotCenter = m_posCenter;
    m_posRot = wmath::WPosition( ( m_posCenter.x() + ( fabs( m_posCenter.x() - m_posx1.x() ) / 2.0 ) ), m_posx1.y(), m_posx1.z() );
    m_posRotOrig = m_posRot;

    m_knobRotCenter->setPosition( m_posRotCenter );
    m_knobRot->setPosition( m_posRot );
}

void WMDatasetManipulator::setManipulatorMode()
{
    m_knobx1->setLockX( false );
    m_knobx1->setLockY( true );
    m_knobx1->setLockZ( true );
    m_knobx2->setLockX( false );
    m_knobx2->setLockY( true );
    m_knobx2->setLockZ( true );

    m_knoby1->setLockY( false );
    m_knoby1->setLockX( true );
    m_knoby1->setLockZ( true );
    m_knoby2->setLockY( false );
    m_knoby2->setLockX( true );
    m_knoby2->setLockZ( true );

    m_knobz1->setLockZ( false );
    m_knobz1->setLockY( true );
    m_knobz1->setLockX( true );
    m_knobz2->setLockZ( false );
    m_knobz2->setLockY( true );
    m_knobz2->setLockX( true );

    if ( m_rotationMode->get( true ) )
    {
        m_knobCenter->hide();
        m_knobx1->hide();
        m_knobx2->hide();
        m_knoby1->hide();
        m_knoby2->hide();
        m_knobz1->hide();
        m_knobz2->hide();

        m_knobRotCenter->unhide();
        m_knobRot->unhide();
    }
    else
    {
        m_knobCenter->unhide();
        m_knobx1->unhide();
        m_knobx2->unhide();
        m_knoby1->unhide();
        m_knoby2->unhide();
        m_knobz1->unhide();
        m_knobz2->unhide();

        m_knobRotCenter->hide();
        m_knobRot->hide();
    }
}

void WMDatasetManipulator::manipulatorMoved()
{
    wmath::WPosition newOffset( m_knobCenter->getPosition() - m_posCenter );
    m_knobx1->setPosition( m_knobx1->getPosition() + newOffset );
    m_knobx2->setPosition( m_knobx2->getPosition() + newOffset );
    m_knoby1->setPosition( m_knoby1->getPosition() + newOffset );
    m_knoby2->setPosition( m_knoby2->getPosition() + newOffset );
    m_knobz1->setPosition( m_knobz1->getPosition() + newOffset );
    m_knobz2->setPosition( m_knobz2->getPosition() + newOffset );

    wmath::WPosition stretch( 1.0, 1.0, 1.0 );

293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
    // write changes to the transformation stored in the module using a write lock
    // open a new scope for the write lock
    {
        boost::unique_lock< boost::mutex > lock( m_updateMutex );

        float orgsizex = static_cast<float>( ( m_posx2Orig - m_posx1Orig ).x() );
        float orgsizey = static_cast<float>( ( m_posy2Orig - m_posy1Orig ).y() );
        float orgsizez = static_cast<float>( ( m_posz2Orig - m_posz1Orig ).z() );

        wmath::WPosition translate( ( m_knobx1->getPosition().x() - m_posx1.x() ) *
                                    ( static_cast<float>( m_grid->getNbCoordsX() / orgsizex ) ),
                                    ( m_knoby1->getPosition().y() - m_posy1.y() ) *
                                    ( static_cast<float>( m_grid->getNbCoordsY() / orgsizey ) ),
                                    ( m_knobz1->getPosition().z() - m_posz1.z() ) *
                                    ( static_cast<float>( m_grid->getNbCoordsZ() / orgsizez ) ) );
308

309 310 311
        m_translationX->set( translate[ 0 ], true );
        m_translationY->set( translate[ 1 ], true );
        m_translationZ->set( translate[ 2 ], true );
312

313 314 315
        stretch.x() = ( m_knobx2->getPosition().x() - m_knobx1->getPosition().x() ) / orgsizex;
        stretch.y() = ( m_knoby2->getPosition().y() - m_knoby1->getPosition().y() ) / orgsizey;
        stretch.z() = ( m_knobz2->getPosition().z() - m_knobz1->getPosition().z() ) / orgsizez;
316

317 318 319
        m_stretchX->set( stretch.x(), true );
        m_stretchY->set( stretch.y(), true );
        m_stretchZ->set( stretch.z(), true );
320

321
        adjustManipulatorPositions();
322

323 324 325
        m_transform->translate( translate );
        m_transform->scale( stretch );
    } // write lock goes out of scope and is released
326

327 328 329
    // notify the module of changes to the transformation,
    // so it can do a thread-safe update of the grid
    notifyChanged();
330 331 332 333
}

void WMDatasetManipulator::manipulatorRotMoved()
{
334 335 336 337 338 339 340
    // write changes to the transformation stored in the module using a write lock
    // open a new scope for the write lock
    {
        boost::unique_lock< boost::mutex > lock( m_updateMutex );

        wmath::WPosition newOffset( m_knobRotCenter->getPosition() - m_posRotCenter );
        m_knobRot->setPosition( m_knobRot->getPosition() + newOffset );
341

342
        wmath::WPosition p1 = ( m_posRotCenter - m_posRot );
343

344 345
        m_posRotCenter = m_knobRotCenter->getPosition();
        m_posRot = m_knobRot->getPosition();
346

347
        wmath::WPosition p2 = ( m_posRotCenter - m_posRot );
348

349 350
        osg::Matrixf rot;
        rot.makeRotate( p2, p1 );
351

352 353
        // m_transform->rotate( rot, m_posRotCenter );
    } // write lock goes out of scope and is released
354

355 356 357
    // notify the module of changes to the transformation,
    // so it can do a thread-safe update of the grid
    notifyChanged();
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
}

void WMDatasetManipulator::adjustManipulatorPositions()
{
    float cx = m_knobx1->getPosition().x() + ( ( m_knobx2->getPosition().x() - m_knobx1->getPosition().x() ) / 2.0 );
    float cy = m_knoby1->getPosition().y() + ( ( m_knoby2->getPosition().y() - m_knoby1->getPosition().y() ) / 2.0 );
    float cz = m_knobz1->getPosition().z() + ( ( m_knobz2->getPosition().z() - m_knobz1->getPosition().z() ) / 2.0 );

    m_knobCenter->setPosition( wmath::WPosition( cx, cy, cz ) );
    m_knobx1->setY( cy );
    m_knobx2->setY( cy );
    m_knobx1->setZ( cz );
    m_knobx2->setZ( cz );

    m_knoby1->setX( cx );
    m_knoby2->setX( cx );
    m_knoby1->setZ( cz );
    m_knoby2->setZ( cz );

    m_knobz1->setY( cy );
    m_knobz2->setY( cy );
    m_knobz1->setX( cx );
    m_knobz2->setX( cx );

    m_posCenter = m_knobCenter->getPosition();
    m_posx1 = m_knobx1->getPosition();
    m_posx2 = m_knobx2->getPosition();
    m_posy1 = m_knoby1->getPosition();
    m_posy2 = m_knoby2->getPosition();
    m_posz1 = m_knobz1->getPosition();
    m_posz2 = m_knobz2->getPosition();
}

391 392 393 394 395 396 397
void WMDatasetManipulator::notifyChanged()
{
    boost::unique_lock< boost::mutex > lock( m_updateMutex );
    m_updated = true;
    m_transformChangedCondition->notify();
}

398 399 400 401 402 403
void WMDatasetManipulator::moduleMain()
{
    m_moduleState.setResetable( true, true );
    m_moduleState.add( m_propCondition );
    m_moduleState.add( m_active->getUpdateCondition() );
    m_moduleState.add( m_input->getDataChangedCondition() );
404
    m_moduleState.add( m_transformChangedCondition );
405 406 407 408 409 410 411 412 413 414 415 416

    ready();

    while ( !m_shutdownFlag() )
    {
        m_moduleState.wait();

        if ( m_shutdownFlag() )
        {
            break;
        }

417
        // test if any updates were made to the transformation
418
        {
419 420
            boost::unique_lock< boost::mutex > lock( m_updateMutex );
            if( m_updated )
421
            {
422 423 424 425 426
                m_updated = false;
                boost::shared_ptr< WGrid > newGrid( new WGridRegular3D( m_grid->getNbCoordsX(),
                                                                        m_grid->getNbCoordsY(),
                                                                        m_grid->getNbCoordsZ(),
                                                                        *m_transform ) );
427 428 429 430
                // NOTE: we NEED to use clone here as we need to keep the dynamic type of the input data set.
                // This means: clone a WDataSetSingle which was constructed as WDataSetVector -> result is a WDataSetSingle which was constructed
                // as WDataSetVector too.
                m_output->updateData( m_dataSet->clone( newGrid ) );
431 432
            }

433 434 435
            boost::shared_ptr< WDataSetSingle > newDataSet = m_input->getData();
            bool dataChanged = ( m_dataSet != newDataSet );
            bool dataValid   = ( newDataSet );
436

437 438 439 440 441 442 443 444 445 446 447
            if( dataValid )
            {
                if( dataChanged )
                {
                    {
                        m_dataSet = newDataSet;
                        init();
                        debugLog() << "New dataset on input connector.";
                    }
                }
            }
448 449 450 451 452 453
        }

        if ( m_active->changed() || m_showManipulators->changed() )
        {
            if ( m_active->get( true ) && m_showManipulators->get( true ) )
            {
454
                boost::unique_lock< boost::mutex > lock( m_updateMutex );
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
                setManipulatorMode();
            }
            else
            {
                m_knobCenter->hide();
                m_knobx1->hide();
                m_knobx2->hide();
                m_knoby1->hide();
                m_knoby2->hide();
                m_knobz1->hide();
                m_knobz2->hide();
                m_knobRotCenter->hide();
                m_knobRot->hide();
            }
        }

        if ( m_rotationMode->changed() )
        {
473
            boost::unique_lock< boost::mutex > lock( m_updateMutex );
474 475 476 477 478
            setManipulatorMode();
        }

        if ( m_translationX->changed() || m_translationY->changed() || m_translationZ->changed() )
        {
479 480 481 482 483 484 485
            {
                boost::unique_lock< boost::mutex > lock( m_updateMutex );
                wmath::WPosition pos( m_translationX->get( true ), m_translationY->get( true ), m_translationZ->get( true ) );
                m_transform->translate( pos );
                setManipulatorsFromBoundingBox();
            }
            notifyChanged();
486 487 488
        }
        if ( m_stretchX->changed() || m_stretchY->changed() || m_stretchZ->changed() )
        {
489 490 491 492 493 494 495
            {
                boost::unique_lock< boost::mutex > lock( m_updateMutex );
                wmath::WPosition str( m_stretchX->get( true ), m_stretchY->get( true ), m_stretchZ->get( true ) );
                m_transform->scale( str );
                setManipulatorsFromBoundingBox();
            }
            notifyChanged();
496 497 498 499 500 501 502 503
        }
        if ( m_rotationX->changed() || m_rotationY->changed() || m_rotationZ->changed() )
        {
            float pi = 3.14159265;
            float rotx = static_cast<float>( m_rotationX->get( true ) ) / 180. * pi;
            float roty = static_cast<float>( m_rotationY->get( true ) ) / 180. * pi;
            float rotz = static_cast<float>( m_rotationZ->get( true ) ) / 180. * pi;

504 505 506 507 508 509 510
            {
                boost::unique_lock< boost::mutex > lock( m_updateMutex );
                // m_transform->rotateX( rotx );
                // m_transform->rotateY( rotx );
                // m_transform->rotateZ( rotx );
            }
            notifyChanged();
511 512
        }
    }
513

514 515 516 517 518 519 520
    m_knobCenter->removeROIChangeNotifier( m_changeRoiSignal );
    m_knobx1->removeROIChangeNotifier( m_changeRoiSignal );
    m_knobx2->removeROIChangeNotifier( m_changeRoiSignal );
    m_knoby1->removeROIChangeNotifier( m_changeRoiSignal );
    m_knoby2->removeROIChangeNotifier( m_changeRoiSignal );
    m_knobz1->removeROIChangeNotifier( m_changeRoiSignal );
    m_knobz2->removeROIChangeNotifier( m_changeRoiSignal );
521

522 523
    m_knobRotCenter->removeROIChangeNotifier( m_changeRotRoiSignal );
    m_knobRot->removeROIChangeNotifier( m_changeRotRoiSignal );
524 525
}