WROIBox.cpp 13.7 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
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-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/>.
//
//---------------------------------------------------------------------------

25
#include <algorithm>
26
#include <string>
27
#include <utility>
28 29 30

#include <osg/LineWidth>
#include <osg/LightModel>
31
#include <osg/Geometry>
32

33
#include "../common/WLogger.h"
34
#include "shaders/WGEShader.h"
35

36
#include "WROIBox.h"
37
#include "WGraphicsEngine.h"
38
#include "WGEUtils.h"
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 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 95 96 97 98 99 100 101 102 103 104 105

size_t WROIBox::maxBoxId = 0;

void buildFacesFromPoints( osg::DrawElementsUInt* surfaceElements )
{
    surfaceElements->push_back( 0 );
    surfaceElements->push_back( 2 );
    surfaceElements->push_back( 3 );
    surfaceElements->push_back( 1 );

    surfaceElements->push_back( 2 );
    surfaceElements->push_back( 6 );
    surfaceElements->push_back( 7 );
    surfaceElements->push_back( 3 );

    surfaceElements->push_back( 6 );
    surfaceElements->push_back( 4 );
    surfaceElements->push_back( 5 );
    surfaceElements->push_back( 7 );

    surfaceElements->push_back( 4 );
    surfaceElements->push_back( 0 );
    surfaceElements->push_back( 1 );
    surfaceElements->push_back( 5 );

    surfaceElements->push_back( 1 );
    surfaceElements->push_back( 3 );
    surfaceElements->push_back( 7 );
    surfaceElements->push_back( 5 );

    surfaceElements->push_back( 0 );
    surfaceElements->push_back( 4 );
    surfaceElements->push_back( 6 );
    surfaceElements->push_back( 2 );
}

void buildLinesFromPoints( osg::DrawElementsUInt* surfaceElements )
{
    surfaceElements->push_back( 0 );
    surfaceElements->push_back( 2 );
    surfaceElements->push_back( 2 );
    surfaceElements->push_back( 3 );
    surfaceElements->push_back( 3 );
    surfaceElements->push_back( 1 );
    surfaceElements->push_back( 1 );
    surfaceElements->push_back( 0 );

    surfaceElements->push_back( 6 );
    surfaceElements->push_back( 4 );
    surfaceElements->push_back( 4 );
    surfaceElements->push_back( 5 );
    surfaceElements->push_back( 5 );
    surfaceElements->push_back( 7 );
    surfaceElements->push_back( 7 );
    surfaceElements->push_back( 6 );

    surfaceElements->push_back( 2 );
    surfaceElements->push_back( 6 );
    surfaceElements->push_back( 7 );
    surfaceElements->push_back( 3 );

    surfaceElements->push_back( 4 );
    surfaceElements->push_back( 0 );
    surfaceElements->push_back( 1 );
    surfaceElements->push_back( 5 );
}

106
void setVertices( osg::Vec3Array* vertices, WPosition minPos, WPosition maxPos )
107 108 109 110 111 112 113 114 115 116 117
{
    vertices->push_back( osg::Vec3( minPos[0], minPos[1], minPos[2] ) );
    vertices->push_back( osg::Vec3( minPos[0], minPos[1], maxPos[2] ) );
    vertices->push_back( osg::Vec3( minPos[0], maxPos[1], minPos[2] ) );
    vertices->push_back( osg::Vec3( minPos[0], maxPos[1], maxPos[2] ) );
    vertices->push_back( osg::Vec3( maxPos[0], minPos[1], minPos[2] ) );
    vertices->push_back( osg::Vec3( maxPos[0], minPos[1], maxPos[2] ) );
    vertices->push_back( osg::Vec3( maxPos[0], maxPos[1], minPos[2] ) );
    vertices->push_back( osg::Vec3( maxPos[0], maxPos[1], maxPos[2] ) );
}

118 119 120 121 122 123 124 125 126 127
void setNormals( osg::Vec3Array* normals )
{
    normals->push_back( osg::Vec3( -1.0, 0.0, 0.0 ) );
    normals->push_back( osg::Vec3( 0.0, 1.0, 0.0 ) );
    normals->push_back( osg::Vec3( 1.0, 0.0, 0.0 ) );
    normals->push_back( osg::Vec3( 0.0, -1.0, 1.0 ) );
    normals->push_back( osg::Vec3( 0.0, 0.0, -1.0 ) );
    normals->push_back( osg::Vec3( 0.0, 0.0, 1.0 ) );
}

128
WROIBox::WROIBox( WPosition minPos, WPosition maxPos ) :
129 130
    WROI(),
    boxId( maxBoxId++ ),
131 132
    m_pickNormal( WVector3d() ),
    m_oldPixelPosition( WVector2d::zero() ),
133
    m_oldScrollWheel( 0 ),
134 135
    m_color( osg::Vec4( 0.391f, 0.594f, 0.828f, 0.5f ) ),
    m_notColor( osg::Vec4( 0.828f, 0.391f, 0.391f, 0.5f ) )
136
{
137 138 139 140 141
    m_propGrp = m_properties->addPropertyGroup( "ROI Box", "Properties of this ROI Box" );
    m_minPos = m_propGrp->addProperty( "Min Position", "When a box is described by its diagonal, this is the lower, left, front corner of it.",
                                          minPos, boost::bind( &WROIBox::boxPropertiesChanged, this, _1 ) );
    m_maxPos = m_propGrp->addProperty( "Max Position", "When a box is described by its diagonal, this is the upper, right, back corner of it.",
                                          maxPos, boost::bind( &WROIBox::boxPropertiesChanged, this, _1 ) );
142

143 144
    boost::shared_ptr< WGraphicsEngine > ge = WGraphicsEngine::getGraphicsEngine();
    assert( ge );
145
    boost::shared_ptr< WGEViewer > viewer = ge->getViewerByName( "Main View" );
146
    assert( viewer );
147 148
    m_viewer = viewer;
    m_pickHandler = m_viewer->getPickHandler();
149
    m_pickHandler->getPickSignal()->connect( boost::bind( &WROIBox::registerRedrawRequest, this, _1 ) );
150

151 152
    m_surfaceGeometry = osg::ref_ptr<osg::Geometry>( new osg::Geometry() );
    m_surfaceGeometry->setDataVariance( osg::Object::DYNAMIC );
153 154

    std::stringstream ss;
schurade's avatar
[STYLE]  
schurade committed
155
    ss << "ROIBox" << boxId;
156

schurade's avatar
schurade committed
157
    setName( ss.str() );
158
    m_surfaceGeometry->setName( ss.str() );
159

160
    osg::ref_ptr<osg::Vec3Array> vertices = osg::ref_ptr<osg::Vec3Array>( new osg::Vec3Array );
161
    setVertices( vertices, minPos, maxPos );
162
    m_surfaceGeometry->setVertexArray( vertices );
163 164 165 166 167 168 169 170 171

    osg::DrawElementsUInt* surfaceElements;
    surfaceElements = new osg::DrawElementsUInt( osg::PrimitiveSet::QUADS, 0 );
    buildFacesFromPoints( surfaceElements );

    osg::DrawElementsUInt* lineElements;
    lineElements = new osg::DrawElementsUInt( osg::PrimitiveSet::LINES, 0 );
    buildLinesFromPoints( lineElements );

172 173
    m_surfaceGeometry->addPrimitiveSet( surfaceElements );
    m_surfaceGeometry->addPrimitiveSet( lineElements );
schurade's avatar
schurade committed
174 175
    addDrawable( m_surfaceGeometry );
    osg::StateSet* state = getOrCreateStateSet();
176 177 178 179 180 181 182 183
    state->setRenderingHint( osg::StateSet::TRANSPARENT_BIN );

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

    // ------------------------------------------------
    // colors
184
    osg::ref_ptr<osg::Vec4Array> colors = osg::ref_ptr<osg::Vec4Array>( new osg::Vec4Array );
185

186
    colors->push_back( osg::Vec4( 0.0f, 0.0f, 1.0f, 0.5f ) );
187 188
    m_surfaceGeometry->setColorArray( colors );
    m_surfaceGeometry->setColorBinding( osg::Geometry::BIND_OVERALL );
189

schurade's avatar
[STYLE]  
schurade committed
190
    osg::ref_ptr< osg::LightModel > lightModel = new osg::LightModel();
191
    lightModel->setTwoSided( true );
192
    //state->setAttributeAndModes( lightModel.get(), osg::StateAttribute::ON );
schurade's avatar
[STYLE]  
schurade committed
193
    state->setMode( GL_BLEND, osg::StateAttribute::ON );
194

195 196 197
    // add a simple default lighting shader
    m_lightShader = new WGEShader( "WGELighting" );

198 199 200 201
    // normals
    osg::ref_ptr<osg::Vec3Array> normals = osg::ref_ptr<osg::Vec3Array>( new osg::Vec3Array );
    setNormals( normals );
    m_surfaceGeometry->setNormalArray( normals );
202
     m_surfaceGeometry->setNormalBinding( osg::Geometry::BIND_PER_PRIMITIVE );
203

204
    m_not->set( false );
schurade's avatar
schurade committed
205

206
    assert( WGraphicsEngine::getGraphicsEngine() );
schurade's avatar
schurade committed
207
    WGraphicsEngine::getGraphicsEngine()->getScene()->addChild( this );
208

schurade's avatar
schurade committed
209 210
    setUserData( this );
    setUpdateCallback( osg::ref_ptr<ROIBoxNodeCallback>( new ROIBoxNodeCallback ) );
schurade's avatar
[STYLE]  
schurade committed
211

212 213
    m_lightShader->apply( this );

schurade's avatar
[STYLE]  
schurade committed
214
    setDirty();
schurade's avatar
schurade committed
215 216 217 218
}

WROIBox::~WROIBox()
{
219 220
}

221
WPosition WROIBox::getMinPos() const
222
{
223
    return m_minPos->get();
224 225
}

226
WPosition WROIBox::getMaxPos() const
227 228 229 230 231 232 233 234 235 236
{
    return m_maxPos->get();
}

WPropPosition WROIBox::getMinPosProperty()
{
    return m_minPos;
}

WPropPosition WROIBox::getMaxPosProperty()
237 238 239 240
{
    return m_maxPos;
}

241
void WROIBox::registerRedrawRequest( WPickInfo pickInfo )
242 243 244 245
{
    boost::unique_lock< boost::shared_mutex > lock;
    lock = boost::unique_lock< boost::shared_mutex >( m_updateLock );

246
    m_pickInfo = pickInfo;
247 248 249 250

    lock.unlock();
}

251 252 253 254 255 256 257
void WROIBox::boxPropertiesChanged( boost::shared_ptr< WPropertyBase > /* property */ )
{
    m_needVertexUpdate = true;
    // NOTE: this is probably not needed as the above flag will trigger a dirty
    // setDirty();
}

258
void WROIBox::updateGFX()
259
{
260 261
    boost::unique_lock< boost::shared_mutex > lock;
    lock = boost::unique_lock< boost::shared_mutex >( m_updateLock );
262 263

    std::stringstream ss;
264
    ss << "ROIBox" << boxId << "";
265
    if( m_pickInfo.getName() == ss.str() )
266
    {
267
        WVector2d newPixelPos( m_pickInfo.getPickPixel() );
268
        if( m_isPicked )
269
        {
270
            osg::Vec3 in( newPixelPos.x(), newPixelPos.y(), 0.0 );
271
            osg::Vec3 world = wge::unprojectFromScreen( in, m_viewer->getCamera() );
272

273 274 275 276 277 278 279 280
            // we want the vector pointing into the screen in world coordinates
            // NOTE: set w = 0 to use it as vector and ignore translation
            osg::Vec4 toDepth = wge::unprojectFromScreen( osg::Vec4( 0.0, 0.0, 1.0, 0.0 ), m_viewer->getCamera() );
            toDepth.normalize();
            WPosition toDepthWorld( toDepth[0], toDepth[1], toDepth[2] );

            float depthMove = m_pickInfo.getScrollWheel() - m_oldScrollWheel;

281 282
            WPosition newPixelWorldPos( world[0], world[1], world[2] );
            WPosition oldPixelWorldPos;
283
            if(  m_oldPixelPosition.x() == 0 && m_oldPixelPosition.y() == 0 )
284 285 286 287 288
            {
                oldPixelWorldPos = newPixelWorldPos;
            }
            else
            {
289
                osg::Vec3 in( m_oldPixelPosition.x(), m_oldPixelPosition.y(), 0.0 );
290
                osg::Vec3 world = wge::unprojectFromScreen( in, m_viewer->getCamera() );
291
                oldPixelWorldPos = WPosition( world[0], world[1], world[2] );
292 293
            }

294
            WVector3d moveVec = newPixelWorldPos - oldPixelWorldPos;
295

296
            // resize Box
297
            if( m_pickInfo.getModifierKey() == WPickInfo::SHIFT )
298
            {
299
                if( m_pickNormal[0] <= 0 && m_pickNormal[1] <= 0 && m_pickNormal[2] <= 0 )
300
                {
301
                    m_maxPos->set( m_maxPos->get() + ( m_pickNormal * dot( moveVec, m_pickNormal ) ) );
302
                }
303
                if( m_pickNormal[0] >= 0 && m_pickNormal[1] >= 0 && m_pickNormal[2] >= 0 )
304
                {
305
                    m_minPos->set( m_minPos->get() + ( m_pickNormal * dot( moveVec, m_pickNormal ) ) );
306
                }
307
                // NOTE: this sets m_needVertexUpdate
308 309 310 311 312
            }

            // move Box
            if( m_pickInfo.getModifierKey() == WPickInfo::NONE )
            {
313 314 315
                m_minPos->set( m_minPos->get() + moveVec + ( 2.0 * toDepthWorld * depthMove ) );
                m_maxPos->set( m_maxPos->get() + moveVec + ( 2.0 * toDepthWorld * depthMove ) );
                // NOTE: this sets m_needVertexUpdate
316
            }
317 318 319
        }
        else
        {
320
            m_pickNormal = m_pickInfo.getPickNormal();
321 322 323 324
            // color for moving box
            if( m_pickInfo.getModifierKey() == WPickInfo::NONE )
            {
                osg::ref_ptr<osg::Vec4Array> colors = osg::ref_ptr<osg::Vec4Array>( new osg::Vec4Array );
325
                if( m_not->get() )
326
                {
327
                    colors->push_back( m_notColor );
328 329 330
                }
                else
                {
331
                    colors->push_back( m_color );
332
                }
333 334
                m_surfaceGeometry->setColorArray( colors );
            }
335
            if( m_pickInfo.getModifierKey() == WPickInfo::SHIFT )
336 337
            {
                osg::ref_ptr<osg::Vec4Array> colors = osg::ref_ptr<osg::Vec4Array>( new osg::Vec4Array );
338
                colors->push_back( osg::Vec4( 0.0f, 1.0f, 0.0f, 0.4f ) );
339 340
                m_surfaceGeometry->setColorArray( colors );
            }
341 342

            m_oldScrollWheel = m_pickInfo.getScrollWheel();
343
        }
344
        m_oldPixelPosition = newPixelPos;
345
        setDirty();
346
        m_isPicked = true;
347
        m_oldScrollWheel = m_pickInfo.getScrollWheel();
348
    }
349
    if( m_isPicked && m_pickInfo.getName() == "unpick" )
350
    {
351 352
        // Perform all actions necessary for finishing a pick

353
        osg::ref_ptr<osg::Vec4Array> colors = osg::ref_ptr<osg::Vec4Array>( new osg::Vec4Array );
354
        if( m_not->get() )
355
        {
356
            colors->push_back( m_notColor );
357 358 359
        }
        else
        {
360
            colors->push_back( m_color );
361
        }
362
        m_surfaceGeometry->setColorArray( colors );
363
        m_pickNormal = WVector3d();
364 365
        m_isPicked = false;
    }
366

367 368 369 370 371 372 373 374 375 376
    if( m_needVertexUpdate )
    {
        osg::ref_ptr<osg::Vec3Array> vertices = osg::ref_ptr<osg::Vec3Array>( new osg::Vec3Array );
        setVertices( vertices, m_minPos->get(), m_maxPos->get() );
        m_surfaceGeometry->setVertexArray( vertices );

        setDirty();
        m_needVertexUpdate = false;
    }

377
    if( m_dirty->get() )
378 379
    {
        osg::ref_ptr<osg::Vec4Array> colors = osg::ref_ptr<osg::Vec4Array>( new osg::Vec4Array );
380
        if( m_not->get() )
381
        {
382
            colors->push_back( m_notColor );
383 384 385
        }
        else
        {
386
            colors->push_back( m_color );
387 388 389 390
        }
        m_surfaceGeometry->setColorArray( colors );
    }

391
    lock.unlock();
392
}
393 394 395 396 397 398 399 400 401 402

void WROIBox::setColor( osg::Vec4 color )
{
    m_color = color;
}

void WROIBox::setNotColor( osg::Vec4 color )
{
    m_notColor = color;
}