WGEZoomTrackballManipulator.cpp 7.99 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 26
#include "WGEZoomTrackballManipulator.h"
#include "WGraphicsEngine.h"
27 28

WGEZoomTrackballManipulator::WGEZoomTrackballManipulator():
29
    TrackballManipulator( 0x02 ),
30 31 32
    m_zoom( 1.0 ),
    m_allowThrow( false ),
    m_paintMode( 0 )
33
{
34
    setTrackballSize( .3 ); // changes the effect of a mouse move for rotation
35 36 37 38 39
}

void WGEZoomTrackballManipulator::setByMatrix( const osg::Matrixd& matrix )
{
    m_zoom = 1.0 / matrix.getScale()[0];
40 41

    // The zoom needs to be undone before forwarding the matrix.
42
    TrackballManipulator::setByMatrix( osg::Matrixd::inverse( osg::Matrixd::scale( 1.0 / m_zoom, 1.0 / m_zoom, 1.0 / m_zoom ) ) * matrix );
43 44 45 46
}

osg::Matrixd WGEZoomTrackballManipulator::getMatrix() const
{
47 48 49 50 51 52
    return osg::Matrixd::scale( 1.0 / m_zoom, 1.0 / m_zoom, 1.0 / m_zoom ) * TrackballManipulator::getMatrix();
}

osg::Matrixd WGEZoomTrackballManipulator::getMatrixWithoutZoom() const
{
    return TrackballManipulator::getMatrix();
53 54 55 56
}

osg::Matrixd WGEZoomTrackballManipulator::getInverseMatrix() const
{
57
    return TrackballManipulator::getInverseMatrix() * osg::Matrixd::scale( m_zoom, m_zoom, m_zoom );
58 59
}

Alexander Wiebel's avatar
Alexander Wiebel committed
60
void WGEZoomTrackballManipulator::home( double /* currentTime */ )
61 62
{
    m_zoom = 1.0;
Alexander Wiebel's avatar
Alexander Wiebel committed
63
    TrackballManipulator::home( 0 /* currentTime */ );
64 65
}

Alexander Wiebel's avatar
Alexander Wiebel committed
66
bool WGEZoomTrackballManipulator::zoom( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us )
67
{
Alexander Wiebel's avatar
Alexander Wiebel committed
68 69 70 71
    double zoomDelta = 0.0;

    if( ea.getKey() && ea.getEventType() == osgGA::GUIEventAdapter::KEYDOWN )
    {
Alexander Wiebel's avatar
Alexander Wiebel committed
72
        if( ea.getKey() == osgGA::GUIEventAdapter::KEY_Minus )
Alexander Wiebel's avatar
Alexander Wiebel committed
73 74 75
        {
            zoomDelta = -0.05;
        }
Alexander Wiebel's avatar
Alexander Wiebel committed
76
        if( ea.getKey() == osgGA::GUIEventAdapter::KEY_Plus )
Alexander Wiebel's avatar
Alexander Wiebel committed
77 78 79
        {
            zoomDelta = 0.05;
        }
80
        if( zoomDelta != 0.0 )
Alexander Wiebel's avatar
Alexander Wiebel committed
81 82 83 84 85 86
        {
            m_zoom *= 1.0 + zoomDelta;
            us.requestRedraw();
        }
    }
    else
87
    {
88 89 90 91 92
        if( ea.getHandled() )
        {
            return true;
        }

93 94 95
        switch( ea.getScrollingMotion() )
        {
            case osgGA::GUIEventAdapter::SCROLL_UP:
96
                zoomDelta = 0.05;
97 98
                break;
            case osgGA::GUIEventAdapter::SCROLL_DOWN:
99
                zoomDelta = -0.05;
100 101
                break;
            case osgGA::GUIEventAdapter::SCROLL_2D:
102
                zoomDelta = 0.05 / 120.0 * ea.getScrollingDeltaY();
103
                break;
Alexander Wiebel's avatar
Alexander Wiebel committed
104 105 106
                // case osgGA::GUIEventAdapter::SCROLL_LEFT:
                // case osgGA::GUIEventAdapter::SCROLL_RIGHT:
                // case osgGA::GUIEventAdapter::SCROLL_NONE:
107 108 109 110 111
            default:
                // do nothing
                zoomDelta = 0.0;
                break;
        }
Alexander Wiebel's avatar
Alexander Wiebel committed
112
    }
113

114
    if( zoomDelta != 0.0 )
Alexander Wiebel's avatar
Alexander Wiebel committed
115 116 117 118 119 120 121 122
    {
        m_zoom *= 1.0 + zoomDelta;
        us.requestRedraw();
    }

    us.requestContinuousUpdate( false );
    return true;
}
123

Alexander Wiebel's avatar
Alexander Wiebel committed
124 125
bool WGEZoomTrackballManipulator::handle( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us )
{
126 127
    _thrown &= m_allowThrow; // By default we do not want the auto-rotation thingy.

128 129 130 131 132 133 134
    if( WGraphicsEngine::getGraphicsEngine()->getScene()->isHomePositionRequested() )
    {
        // We set the scene to the manipulator home position if the scene
        // requests to do so. See WGEScene for more details.
        home( 0 );
        return true;
    }
Alexander Wiebel's avatar
Alexander Wiebel committed
135 136 137
    else if( ea.getEventType() == osgGA::GUIEventAdapter::SCROLL
             || ea.getKey() == osgGA::GUIEventAdapter::KEY_Minus
             ||  ea.getKey() == osgGA::GUIEventAdapter::KEY_Plus )
Alexander Wiebel's avatar
Alexander Wiebel committed
138 139
    {
        return zoom( ea, us );
140
    }
141 142
    // NOTE: we need to ignore the right mouse-button drag! This manipulates the underlying Trackball Manipulator while, at the same time, is
    // used for moving ROIS! Zooming is done using Scroll Wheel or +/- keys.
143
    else if( ( ea.getEventType() == osgGA::GUIEventAdapter::DRAG ) || ( ea.getEventType() == osgGA::GUIEventAdapter::PUSH ) )
144
    {
145
        if( ea.getButtonMask() == osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON )
146 147 148
        {
            return true;
        }
149
        else if(  ( ea.getButtonMask() == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON ) && ( m_paintMode == 1 ) )
150 151 152 153 154 155 156
        {
            return true;
        }
        else
        {
            return TrackballManipulator::handle( ea, us );
        }
157
    }
158 159 160 161 162
    else
    {
        return TrackballManipulator::handle( ea, us );
    }
}
163 164 165 166 167

void WGEZoomTrackballManipulator::setPaintMode( int mode )
{
    m_paintMode = mode;
}
168 169 170 171 172 173 174 175 176 177

void WGEZoomTrackballManipulator::setThrow( bool allowThrow )
{
    m_allowThrow = allowThrow;
}

bool WGEZoomTrackballManipulator::getThrow() const
{
    return m_allowThrow;
}
Robin Eschbach's avatar
Robin Eschbach committed
178

179
void WGEZoomTrackballManipulator::computeHomePosition( const osg::Camera*, bool useBoundingBox )
Robin Eschbach's avatar
Robin Eschbach committed
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
{
    if( getNode() )
    {
        osg::BoundingSphere boundingSphere;

        if( useBoundingBox )
        {
            WGEZoomTrackballNodeVisitor cbVisitor;
            getNode()->accept( cbVisitor );
            osg::BoundingBox& bb = cbVisitor.getBoundingBox();

            if( bb.valid() )
            {
                boundingSphere.expandBy( bb );
            }
            else
            {
                boundingSphere = getNode()->getBound();
            }
        }
        else
        {
            boundingSphere = getNode()->getBound();
        }

205
        m_radius = osg::maximum( ( double ) boundingSphere.radius(), 1e-6 );
Robin Eschbach's avatar
Robin Eschbach committed
206

207 208
        // The more far away the number from 0 is, the stronger is the pan
        setHomePosition( boundingSphere.center() + osg::Vec3d( 0.0, -m_radius, 0.0 ),
Robin Eschbach's avatar
Robin Eschbach committed
209 210 211 212 213 214 215 216 217 218 219 220 221
                         boundingSphere.center(),
                         osg::Vec3d( 0.0, 0.0, 1.0 ),
                         _autoComputeHomePosition );
    }
}

void WGEZoomTrackballManipulator::fitToScreen( const osg::Camera* camera )
{
    if( !camera )
    {
        return;
    }

222 223
    computeHomePosition( camera, true );

Robin Eschbach's avatar
Robin Eschbach committed
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
    double radiusFac[] = {
         1,  1,  1,
        -1,  1,  1,
         1, -1,  1,
        -1, -1,  1,
         1,  1, -1,
        -1,  1, -1,
         1, -1, -1,
        -1, -1, -1
    };
    osg::Vec3 center = _homeCenter;
    osg::Matrix transf = camera->getProjectionMatrix() * camera->getViewport()->computeWindowMatrix();
    double fac = INFINITY;
    for( int i = 0; i < 8; i++ )
    {
        osg::Vec3d vec = osg::Vec3( center.x() + m_radius * radiusFac[ i * 3 ],
                                    center.y() + m_radius * radiusFac[ i * 3 + 1 ],
                                    center.z() + m_radius * radiusFac[ i * 3 + 2 ] );
        osg::Vec3d screenCoords = vec * transf;

        double facX = abs( camera->getViewport()->width() / screenCoords.x() );
        double facY = abs( camera->getViewport()->height() / screenCoords.y() );

        fac = fmin( fac, facX );
        fac = fmin( fac, facY );
    }

    m_zoom = fac;
    setCenter( center );
}
254 255 256 257 258 259 260 261 262

bool WGEZoomTrackballManipulator::performMovementMiddleMouseButton( const double eventTimeDelta, const double dx, const double dy )
{
    // scale panning based on zoom
    float scale = -0.5 * ( _distance / m_zoom ) * getThrowScale( eventTimeDelta );
    panModel( dx * scale, dy * scale );
    return true;
}