Commit c0c50594 authored by Alexander Wiebel's avatar Alexander Wiebel
Browse files

Merge branch 'master'

parents 2a535023 5c40c0a3
......@@ -175,3 +175,79 @@ bool WGEZoomTrackballManipulator::getThrow() const
{
return m_allowThrow;
}
void WGEZoomTrackballManipulator::computeHomePosition( const osg::Camera*, bool useBoundingBox )
{
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();
}
m_radius = osg::maximum( ( double ) boundingSphere.radius(), 1e-6 );
// 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 ),
boundingSphere.center(),
osg::Vec3d( 0.0, 0.0, 1.0 ),
_autoComputeHomePosition );
}
}
void WGEZoomTrackballManipulator::fitToScreen( const osg::Camera* camera )
{
if( !camera )
{
return;
}
computeHomePosition( camera, true );
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 );
}
......@@ -27,6 +27,8 @@
#include <osgGA/TrackballManipulator>
#include "WGEZoomTrackballNodeVisitor.h"
/**
* New OSG manipulator: TrackballManipulator with added mouse wheel zoom.
......@@ -118,6 +120,19 @@ public:
*/
bool getThrow() const;
/**
* Computes the home position.
* \param camera The camera from which to compute.
* \param useBoundingBox if boundingboxes should be used.
*/
virtual void computeHomePosition( const osg::Camera* camera, bool useBoundingBox );
/**
* Zooms and centers to fit screen.
* \param camera The camera to zoom.
*/
void fitToScreen( const osg::Camera* camera );
protected:
private:
/**
......@@ -133,6 +148,8 @@ private:
bool m_allowThrow; //!< Do we want the auto-rotation thingy?
int m_paintMode; //!<paint mode
double m_radius; //!< The radius of the bounding sphere
};
inline double WGEZoomTrackballManipulator::getZoom() const
......
//---------------------------------------------------------------------------
//
// 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/>.
//
//---------------------------------------------------------------------------
#include <iostream>
#include <osg/Camera>
#include <osg/Drawable>
#include <osg/Geometry>
#include <osg/MatrixTransform>
#include <osg/Projection>
#include "geodes/WGEGridNode.h"
#include "WGEZoomTrackballNodeVisitor.h"
WGEZoomTrackballNodeVisitor::WGEZoomTrackballNodeVisitor( TraversalMode traversalMode ): osg::NodeVisitor( traversalMode )
{
}
void WGEZoomTrackballNodeVisitor::reset()
{
m_bb.init();
}
void WGEZoomTrackballNodeVisitor::apply( osg::Camera& node ) // NOLINT
{
// Ignore all cameras that are a 2D orthogonal projection.
double left, right, bottom, top, zNear, zFar;
if( node.getProjectionMatrixAsOrtho( left, right, bottom, top, zNear, zFar ) && zNear == -1 && zFar == 1 )
{
return;
}
traverse( node );
}
void WGEZoomTrackballNodeVisitor::apply( osg::Projection& node ) // NOLINT
{
// Ignore all 2D orthogonal projections.
double left, right, bottom, top, zNear, zFar;
if( node.getMatrix().getOrtho( left, right, bottom, top, zNear, zFar ) && zNear == -1 && zFar == 1 )
{
return;
}
traverse( node );
}
void WGEZoomTrackballNodeVisitor::apply( osg::Drawable& node ) // NOLINT
{
osg::BoundingBox bb = node.getBoundingBox();
if( bb.valid() )
{
m_bb.expandBy( bb );
}
}
void WGEZoomTrackballNodeVisitor::apply( osg::MatrixTransform& node ) // NOLINT
{
// Ignore grids.
if( !dynamic_cast< WGEGridNode* >( &node ) )
{
traverse( node );
}
}
osg::BoundingBox& WGEZoomTrackballNodeVisitor::getBoundingBox()
{
return m_bb;
}
//---------------------------------------------------------------------------
//
// 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/>.
//
//---------------------------------------------------------------------------
#ifndef WGEZOOMTRACKBALLNODEVISITOR_H
#define WGEZOOMTRACKBALLNODEVISITOR_H
#include <limits>
#include <osg/NodeVisitor>
#include <osg/BoundingBox>
/**
* A visitor that ignores specific nodes that are irrelevant for the trackball.
*/
class WGEZoomTrackballNodeVisitor : public osg::NodeVisitor
{
public:
/**
* Creates a new node visitor.
* \param traversalMode The traversal mode.
*/
explicit WGEZoomTrackballNodeVisitor( TraversalMode traversalMode = TRAVERSE_ACTIVE_CHILDREN );
/**
* Resets this visitor.
*/
virtual void reset();
/**
* Handles camera nodes.
* \param node The camera node.
*/
void apply( osg::Camera& node ); // NOLINT
/**
* Handles projection nodes.
* \param node The projection node.
*/
void apply( osg::Projection& node ); // NOLINT
/**
* Handles drawable nodes.
* \param node The drawable node.
*/
void apply( osg::Drawable& node ); // NOLINT
/**
* Handles matrixtransform nodes.
* \param node The matrixtransform node.
*/
void apply( osg::MatrixTransform& node ); // NOLINT
/**
* Gets the bounding box.
* \return osg::BoundingBox& The bounding box.
*/
osg::BoundingBox& getBoundingBox();
private:
osg::BoundingBox m_bb; //!< The bounding box that is generated.
};
#endif // WGEZOOMTRACKBALLNODEVISITOR_H
......@@ -190,6 +190,7 @@ void WMainWindow::setupGUI()
m_iconManager.addMapping( "axial icon", "orientation_axial" );
m_iconManager.addMapping( "coronal icon", "orientation_coronal" );
m_iconManager.addMapping( "sagittal icon", "orientation_sagittal" );
m_iconManager.addMapping( "center icon", "center_scene" );
m_iconManager.addMapping( "DefaultModuleIcon", "default" );
m_iconManager.addMapping( "missingModule", "question" );
m_iconManager.addMapping( "view", "camera" );
......@@ -1089,6 +1090,9 @@ bool WMainWindow::event( QEvent* event )
);
}
}
// Loading projects loses home position calculation
m_mainGLWidget->getViewer()->getCameraManipulator()->setAutoComputeHomePosition( true );
}
}
......
......@@ -121,6 +121,12 @@ WQtGLWidget::WQtGLWidget( std::string nameOfViewer, QWidget* parent, WGECamera::
QKeySequence( Qt::CTRL + Qt::SHIFT + Qt::Key_P ) );
tmpAction->setIconVisibleInMenu( true );
m_cameraPresetResetMenu->addAction( tmpAction );
tmpAction = m_cameraPresetMenu->addAction( WQtGui::getIconManager()->getIcon( "center icon" ), "Fit screen", this,
SLOT( setFitScreenPosition() ),
QKeySequence( Qt::CTRL + Qt::SHIFT + Qt::Key_F ) );
tmpAction->setIconVisibleInMenu( true );
m_cameraPresetResetMenu->addAction( tmpAction );
}
WQtGLWidget::~WQtGLWidget()
......@@ -502,6 +508,26 @@ void WQtGLWidget::setPresetViewPosterior()
}
}
void WQtGLWidget::setFitScreenPosition()
{
if( m_Viewer )
{
osg::ref_ptr<WGEZoomTrackballManipulator> cm = osg::dynamic_pointer_cast<WGEZoomTrackballManipulator>( m_Viewer->getCameraManipulator() );
if( cm )
{
osg::Vec3d eye, center, up;
cm->getHomePosition( eye, center, up );
cm->setCenter( center );
cm->fitToScreen( WKernel::getRunningKernel()->getGraphicsEngine()->getViewer()->getCamera() );
}
else
{
wlog::warn( "WQtGLWidget(" + m_Viewer->getName() + ")" ) << "GL Widget does not use a TrackballManipulator. Preset cannot be used.";
}
}
}
QMenu* WQtGLWidget::getCameraPresetsMenu()
{
return m_cameraPresetMenu;
......
......@@ -190,6 +190,11 @@ public slots:
*/
void setPresetViewPosterior();
/**
* Zooms and centers the view to fit the viewer.
*/
void setFitScreenPosition();
protected:
/**
* The viewer to the scene.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment