From 8f92dfd0c0e9fdb69478f430a117c6ec71540ecc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jacob=20St=C3=B8ren?= Date: Wed, 9 Dec 2015 15:17:04 +0100 Subject: [PATCH] Caf: WIP: Created a base class with trackball related navigation stuff. Refactored cad and ceetron plus nav to use the common base --- Fwk/AppFwk/cafViewer/CMakeLists.txt | 2 + Fwk/AppFwk/cafViewer/cafCadNavigation.cpp | 95 ++---- Fwk/AppFwk/cafViewer/cafCadNavigation.h | 23 +- .../cafViewer/cafCeetronPlusNavigation.cpp | 142 ++------ .../cafViewer/cafCeetronPlusNavigation.h | 71 ++-- .../cafViewer/cafTrackBallBasedNavigation.cpp | 302 ++++++++++++++++++ .../cafViewer/cafTrackBallBasedNavigation.h | 78 +++++ Fwk/AppFwk/cafViewer/cafViewer.cpp | 1 + 8 files changed, 475 insertions(+), 239 deletions(-) create mode 100644 Fwk/AppFwk/cafViewer/cafTrackBallBasedNavigation.cpp create mode 100644 Fwk/AppFwk/cafViewer/cafTrackBallBasedNavigation.h diff --git a/Fwk/AppFwk/cafViewer/CMakeLists.txt b/Fwk/AppFwk/cafViewer/CMakeLists.txt index 09960ee2ee..b0402fd991 100644 --- a/Fwk/AppFwk/cafViewer/CMakeLists.txt +++ b/Fwk/AppFwk/cafViewer/CMakeLists.txt @@ -33,6 +33,8 @@ add_library( ${PROJECT_NAME} cafCeetronNavigation.h cafCeetronPlusNavigation.cpp cafCeetronPlusNavigation.h + cafTrackBallBasedNavigation.cpp + cafTrackBallBasedNavigation.h cafNavigationPolicy.cpp cafNavigationPolicy.h cafOpenGLWidget.cpp diff --git a/Fwk/AppFwk/cafViewer/cafCadNavigation.cpp b/Fwk/AppFwk/cafViewer/cafCadNavigation.cpp index d9ad91dee4..7acd9d01da 100644 --- a/Fwk/AppFwk/cafViewer/cafCadNavigation.cpp +++ b/Fwk/AppFwk/cafViewer/cafCadNavigation.cpp @@ -38,26 +38,20 @@ #include "cafCadNavigation.h" #include "cafViewer.h" #include "cvfCamera.h" -#include "cvfScene.h" -#include "cvfModel.h" #include "cvfViewport.h" #include "cvfHitItemCollection.h" #include "cvfRay.h" +#include "cvfManipulatorTrackball.h" #include -#include - -using cvf::ManipulatorTrackball; //-------------------------------------------------------------------------------------------------- /// //-------------------------------------------------------------------------------------------------- void caf::CadNavigation::init() { - m_trackball = new cvf::ManipulatorTrackball; - m_trackball->setCamera(m_viewer->mainCamera()); - m_isRotCenterInitialized = false; - m_isRotating = false; + caf::TrackBallBasedNavigation::init(); + m_navigationUpdated = false; } //-------------------------------------------------------------------------------------------------- @@ -81,7 +75,7 @@ bool caf::CadNavigation::handleInputEvent(QInputEvent* inputEvent) if (me->modifiers() & Qt::ShiftModifier) { m_trackball->startNavigation(cvf::ManipulatorTrackball::PAN, translatedMousePosX, translatedMousePosY); - m_isRotating = true; + m_isNavigating = true; isEventHandled = true; } else if (me->modifiers() == Qt::NoModifier) @@ -101,23 +95,30 @@ bool caf::CadNavigation::handleInputEvent(QInputEvent* inputEvent) m_trackball->startNavigation(cvf::ManipulatorTrackball::ROTATE, translatedMousePosX, translatedMousePosY); //m_viewer->setCursor(RiuCursors::get(RiuCursors::ROTATE)); - m_isRotating = true; + m_isNavigating = true; isEventHandled = true; } } + + if (isEventHandled) + { + m_navigationUpdated = false; + } } break; case QEvent::MouseButtonRelease: { - if (m_isRotating) + if (m_isNavigating) { QMouseEvent * me = static_cast( inputEvent); if (me->button() == Qt::MidButton) { m_trackball->endNavigation(); //m_viewer->setCursor(RiuCursors::get(RiuCursors::PICK)); - m_isRotating = false; - isEventHandled = true; + m_isNavigating = false; + + isEventHandled = m_navigationUpdated; + m_navigationUpdated = false; } } } @@ -131,12 +132,13 @@ bool caf::CadNavigation::handleInputEvent(QInputEvent* inputEvent) int translatedMousePosX = me->x(); int translatedMousePosY = m_viewer->height() - me->y(); - if (m_isRotating) + if (m_isNavigating) { bool needRedraw = m_trackball->updateNavigation(translatedMousePosX, translatedMousePosY); if (needRedraw) { m_viewer->navigationPolicyUpdate(); + m_navigationUpdated = true; } isEventHandled = true; } @@ -171,6 +173,7 @@ bool caf::CadNavigation::handleInputEvent(QInputEvent* inputEvent) cvf::Vec3d newVrp = vrp + trans; m_viewer->mainCamera()->setFromLookAt(newPos,newVrp, up ); + m_viewer->updateParallelProjectionHeightFromMoveZoom(m_pointOfInterest); m_viewer->navigationPolicyUpdate(); } @@ -183,65 +186,3 @@ bool caf::CadNavigation::handleInputEvent(QInputEvent* inputEvent) return isEventHandled; } - -//-------------------------------------------------------------------------------------------------- -/// -//-------------------------------------------------------------------------------------------------- -void caf::CadNavigation::initializeRotationCenter() -{ - if (m_isRotCenterInitialized - || m_trackball.isNull() - || !m_viewer->currentScene()->boundingBox().isValid()) - { - return; - } - - cvf::Vec3d pointOfInterest = m_viewer->currentScene()->boundingBox().center(); - - this->setPointOfInterest(pointOfInterest); -} - -//-------------------------------------------------------------------------------------------------- -/// Repositions and orients the camera to view the rotation point along the -/// direction "alongDirection". The distance to the rotation point is maintained. -/// -//-------------------------------------------------------------------------------------------------- -void caf::CadNavigation::setView( const cvf::Vec3d& alongDirection, const cvf::Vec3d& upDirection ) -{ - m_trackball->setView(alongDirection, upDirection); - /* - if (m_camera.isNull()) return; - - Vec3d dir = alongDirection; - if (!dir.normalize()) return; - Vec3d up = upDirection; - if(!up.normalize()) up = Vec3d::Z_AXIS; - - if((up * dir) < 1e-2) up = dir.perpendicularVector(); - - Vec3d cToE = m_camera->position() - m_rotationPoint; - Vec3d newEye = m_rotationPoint - cToE.length() * dir; - - m_camera->setFromLookAt(newEye, m_rotationPoint, upDirection); - */ -} - -//-------------------------------------------------------------------------------------------------- -/// -//-------------------------------------------------------------------------------------------------- -cvf::Vec3d caf::CadNavigation::pointOfInterest() -{ - initializeRotationCenter(); - return m_pointOfInterest; -} - -//-------------------------------------------------------------------------------------------------- -/// -//-------------------------------------------------------------------------------------------------- -void caf::CadNavigation::setPointOfInterest(cvf::Vec3d poi) -{ - m_pointOfInterest = poi; - m_trackball->setRotationPoint(poi); - m_isRotCenterInitialized = true; - m_viewer->updateParallelProjectionCameraPosFromPointOfInterestMove(m_pointOfInterest); -} diff --git a/Fwk/AppFwk/cafViewer/cafCadNavigation.h b/Fwk/AppFwk/cafViewer/cafCadNavigation.h index 9cbae679be..8285dfec87 100644 --- a/Fwk/AppFwk/cafViewer/cafCadNavigation.h +++ b/Fwk/AppFwk/cafViewer/cafCadNavigation.h @@ -37,31 +37,18 @@ #pragma once -#include "cafNavigationPolicy.h" -#include "cvfManipulatorTrackball.h" +#include "cafTrackBallBasedNavigation.h" namespace caf { -class CadNavigation : public NavigationPolicy +class CadNavigation : public TrackBallBasedNavigation { protected: - // General navigation policy reimplememtation - virtual void init(); - virtual bool handleInputEvent(QInputEvent* inputEvent); - virtual void setView( const cvf::Vec3d& alongDirection, const cvf::Vec3d& upDirection ); - virtual cvf::Vec3d pointOfInterest(); - virtual void setPointOfInterest(cvf::Vec3d poi); + virtual void init(); + virtual bool handleInputEvent(QInputEvent* inputEvent); - - // Cad navigation specific - void initializeRotationCenter(); - - cvf::ref - m_trackball; - bool m_isRotCenterInitialized; - bool m_isRotating; - cvf::Vec3d m_pointOfInterest; + bool m_navigationUpdated; }; } // End namespace caf diff --git a/Fwk/AppFwk/cafViewer/cafCeetronPlusNavigation.cpp b/Fwk/AppFwk/cafViewer/cafCeetronPlusNavigation.cpp index 24f978050c..700b473c8b 100644 --- a/Fwk/AppFwk/cafViewer/cafCeetronPlusNavigation.cpp +++ b/Fwk/AppFwk/cafViewer/cafCeetronPlusNavigation.cpp @@ -1,38 +1,48 @@ //################################################################################################## // -// Copyright (C) 2011, Ceetron AS -// This is UNPUBLISHED PROPRIETARY SOURCE CODE of Ceetron AS. The contents of this file may -// not be disclosed to third parties, copied or duplicated in any form, in whole or in part, -// without the prior written permission of Ceetron AS. +// Custom Visualization Core library +// Copyright (C) Ceetron Solutions AS +// +// This library may be used under the terms of either the GNU General Public License or +// the GNU Lesser General Public License as follows: +// +// GNU General Public License Usage +// This library is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This library 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 General Public License at <> +// for more details. +// +// GNU Lesser General Public License Usage +// This library 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 2.1 of the License, or +// (at your option) any later version. +// +// This library 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 at <> +// for more details. +// //################################################################################################## + #include "cafCeetronPlusNavigation.h" #include "cafViewer.h" #include "cvfCamera.h" -#include "cvfScene.h" -#include "cvfModel.h" #include "cvfViewport.h" #include "cvfHitItemCollection.h" #include "cvfRay.h" +#include "cvfManipulatorTrackball.h" #include -#include - -using cvf::ManipulatorTrackball; - -//-------------------------------------------------------------------------------------------------- -/// -//-------------------------------------------------------------------------------------------------- -void caf::CeetronPlusNavigation::init() -{ - m_trackball = new cvf::ManipulatorTrackball; - m_trackball->setCamera(m_viewer->mainCamera()); - m_isRotCenterInitialized = false; - m_hasMovedMouseDuringNavigation = false; - m_isNavigating = false; - m_isZooming = false; - m_lastPosX = 0; - m_lastPosY = 0; -} //-------------------------------------------------------------------------------------------------- /// @@ -183,87 +193,3 @@ bool caf::CeetronPlusNavigation::handleInputEvent(QInputEvent* inputEvent) return false;//isEventHandled; } - -//-------------------------------------------------------------------------------------------------- -/// -//-------------------------------------------------------------------------------------------------- -void caf::CeetronPlusNavigation::initializeRotationCenter() -{ - if (m_isRotCenterInitialized - || m_trackball.isNull() - || !m_viewer->currentScene()->boundingBox().isValid()) - { - return; - } - - cvf::Vec3d pointOfInterest = m_viewer->currentScene()->boundingBox().center(); - - this->setPointOfInterest(pointOfInterest); -} - -//-------------------------------------------------------------------------------------------------- -/// Repositions and orients the camera to view the rotation point along the -/// direction "alongDirection". The distance to the rotation point is maintained. -/// -//-------------------------------------------------------------------------------------------------- -void caf::CeetronPlusNavigation::setView( const cvf::Vec3d& alongDirection, const cvf::Vec3d& upDirection ) -{ - m_trackball->setView(alongDirection, upDirection); - /* - if (m_camera.isNull()) return; - - Vec3d dir = alongDirection; - if (!dir.normalize()) return; - Vec3d up = upDirection; - if(!up.normalize()) up = Vec3d::Z_AXIS; - - if((up * dir) < 1e-2) up = dir.perpendicularVector(); - - Vec3d cToE = m_camera->position() - m_rotationPoint; - Vec3d newEye = m_rotationPoint - cToE.length() * dir; - - m_camera->setFromLookAt(newEye, m_rotationPoint, upDirection); - */ -} - -//-------------------------------------------------------------------------------------------------- -/// -//-------------------------------------------------------------------------------------------------- -cvf::Vec3d caf::CeetronPlusNavigation::pointOfInterest() -{ - initializeRotationCenter(); - return m_pointOfInterest; -} - -//-------------------------------------------------------------------------------------------------- -/// -//-------------------------------------------------------------------------------------------------- -void caf::CeetronPlusNavigation::setPointOfInterest(cvf::Vec3d poi) -{ - m_pointOfInterest = poi; - m_trackball->setRotationPoint(poi); - m_isRotCenterInitialized = true; - m_viewer->updateParallelProjectionCameraPosFromPointOfInterestMove(m_pointOfInterest); - -} - -//-------------------------------------------------------------------------------------------------- -/// -//-------------------------------------------------------------------------------------------------- -void caf::CeetronPlusNavigation::zoomAlongRay(cvf::Ray* ray, int delta) -{ - if (ray && abs(delta) > 0) - { - cvf::Vec3d pos, vrp, up; - m_viewer->mainCamera()->toLookAt(&pos, &vrp, &up); - - double scale = delta/8.0 * 1.0/150 * (pos - m_pointOfInterest).length(); - cvf::Vec3d trans = scale * ray->direction(); - cvf::Vec3d newPos = pos + trans; - cvf::Vec3d newVrp = vrp + trans; - - m_viewer->mainCamera()->setFromLookAt(newPos, newVrp, up ); - m_viewer->updateParallelProjectionHeightFromMoveZoom(m_pointOfInterest); - m_viewer->navigationPolicyUpdate(); - } -} diff --git a/Fwk/AppFwk/cafViewer/cafCeetronPlusNavigation.h b/Fwk/AppFwk/cafViewer/cafCeetronPlusNavigation.h index caed0c66c6..62d077a134 100644 --- a/Fwk/AppFwk/cafViewer/cafCeetronPlusNavigation.h +++ b/Fwk/AppFwk/cafViewer/cafCeetronPlusNavigation.h @@ -1,50 +1,49 @@ //################################################################################################## // -// Copyright (C) 2011, Ceetron AS -// This is UNPUBLISHED PROPRIETARY SOURCE CODE of Ceetron AS. The contents of this file may -// not be disclosed to third parties, copied or duplicated in any form, in whole or in part, -// without the prior written permission of Ceetron AS. +// Custom Visualization Core library +// Copyright (C) Ceetron Solutions AS +// +// This library may be used under the terms of either the GNU General Public License or +// the GNU Lesser General Public License as follows: +// +// GNU General Public License Usage +// This library is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This library 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 General Public License at <> +// for more details. +// +// GNU Lesser General Public License Usage +// This library 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 2.1 of the License, or +// (at your option) any later version. +// +// This library 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 at <> +// for more details. +// //################################################################################################## - #pragma once -#include "cvfBase.h" -#include "cafNavigationPolicy.h" -#include "cvfManipulatorTrackball.h" -#include "cvfRay.h" - - +#include "cafTrackBallBasedNavigation.h" namespace caf { -class CeetronPlusNavigation : public NavigationPolicy +class CeetronPlusNavigation : public TrackBallBasedNavigation { protected: - // General navigation policy overrides - virtual void init(); - virtual bool handleInputEvent(QInputEvent* inputEvent); - - virtual void setView( const cvf::Vec3d& alongDirection, const cvf::Vec3d& upDirection ); - virtual cvf::Vec3d pointOfInterest(); - virtual void setPointOfInterest(cvf::Vec3d poi); - - // PdvNavigation specific - void initializeRotationCenter(); - cvf::ref m_trackball; - bool m_isRotCenterInitialized; - cvf::Vec3d m_pointOfInterest; - - bool m_isNavigating; - bool m_hasMovedMouseDuringNavigation; - - // Handle mid mouse button zoom - void zoomAlongRay( cvf::Ray* ray, int delta ); - bool m_isZooming; - cvf::ref m_zoomRay; - int m_lastPosX; /// Previous mouse position - int m_lastPosY; - + virtual bool handleInputEvent(QInputEvent* inputEvent); }; } // End namespace caf diff --git a/Fwk/AppFwk/cafViewer/cafTrackBallBasedNavigation.cpp b/Fwk/AppFwk/cafViewer/cafTrackBallBasedNavigation.cpp new file mode 100644 index 0000000000..d23f92ee95 --- /dev/null +++ b/Fwk/AppFwk/cafViewer/cafTrackBallBasedNavigation.cpp @@ -0,0 +1,302 @@ +//################################################################################################## +// +// Custom Visualization Core library +// Copyright (C) 2015 Ceetron Solutions AS +// +// This library may be used under the terms of either the GNU General Public License or +// the GNU Lesser General Public License as follows: +// +// GNU General Public License Usage +// This library is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This library 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 General Public License at <> +// for more details. +// +// GNU Lesser General Public License Usage +// This library 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 2.1 of the License, or +// (at your option) any later version. +// +// This library 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 at <> +// for more details. +// +//################################################################################################## + +#include "cafTrackBallBasedNavigation.h" + +#include "cafViewer.h" +#include "cvfCamera.h" +#include "cvfScene.h" +#include "cvfModel.h" +#include "cvfViewport.h" +#include "cvfHitItemCollection.h" +#include "cvfRay.h" +#include "cvfManipulatorTrackball.h" + +#include + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void caf::TrackBallBasedNavigation::init() +{ + m_trackball = new cvf::ManipulatorTrackball; + m_trackball->setCamera(m_viewer->mainCamera()); + m_isRotCenterInitialized = false; + m_hasMovedMouseDuringNavigation = false; + m_isNavigating = false; + m_isZooming = false; + m_lastPosX = 0; + m_lastPosY = 0; +} + +#if 0 +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +bool caf::TrackBallBasedNavigation::handleInputEvent(QInputEvent* inputEvent) +{ + if (! inputEvent) return false; + bool isEventHandled = false; + switch (inputEvent->type()) + { + case QEvent::MouseButtonPress: + { + QMouseEvent * me = static_cast( inputEvent); + int translatedMousePosX = me->x(); + int translatedMousePosY = m_viewer->height() - me->y(); + + if (me->button() == Qt::RightButton) + { + cvf::HitItemCollection hic; + bool hitSomething = m_viewer->rayPick(me->x(), me->y(), &hic); + + if (hitSomething) + { + cvf::Vec3d pointOfInterest = hic.firstItem()->intersectionPoint(); + this->setPointOfInterest(pointOfInterest); + } + else + { + initializeRotationCenter(); + } + + m_trackball->startNavigation(cvf::ManipulatorTrackball::ROTATE, translatedMousePosX, translatedMousePosY); + //m_viewer->setCursor(RICursors::get(RICursors::ROTATE)); + m_isNavigating = true; + m_hasMovedMouseDuringNavigation = false; + isEventHandled = true; + } + else if (me->button() == Qt::LeftButton) + { + if (me->modifiers() == Qt::NoModifier) + { + m_trackball->startNavigation(cvf::ManipulatorTrackball::PAN, translatedMousePosX, translatedMousePosY); + m_isNavigating = true; + m_hasMovedMouseDuringNavigation = false; + isEventHandled = true; + } + } + else if (me->button() == Qt::MidButton) + { + if (me->modifiers() == Qt::NoModifier) + { + QMouseEvent* we = static_cast ( inputEvent); + m_lastPosX = we->x(); + m_lastPosY = we->y(); + + m_zoomRay = m_viewer->mainCamera()->rayFromWindowCoordinates(translatedMousePosX, translatedMousePosY); + + m_isNavigating = true; + m_hasMovedMouseDuringNavigation = false; + isEventHandled = true; + m_isZooming = true; + } + } + } + break; + case QEvent::MouseButtonRelease: + { + if (m_isNavigating) + { + QMouseEvent * me = static_cast( inputEvent); + if (me->button() == Qt::RightButton || me->button() == Qt::LeftButton ) + { + m_trackball->endNavigation(); + + m_isNavigating = false; + if (m_hasMovedMouseDuringNavigation) isEventHandled = true; + m_hasMovedMouseDuringNavigation = false; + } + else if ( me->button() == Qt::MidButton ) + { + m_isZooming = false; + + m_isNavigating = false; + if (m_hasMovedMouseDuringNavigation) isEventHandled = true; + m_hasMovedMouseDuringNavigation = false; + } + } + } + break; + case QEvent::MouseMove: + { + initializeRotationCenter(); + if (m_isRotCenterInitialized) + { + QMouseEvent * me = static_cast( inputEvent); + int translatedMousePosX = me->x(); + int translatedMousePosY = m_viewer->height() - me->y(); + + if (m_isNavigating) + { + if (m_isZooming) + { + int delta = 3*(m_lastPosY - me->y()); + this->zoomAlongRay(m_zoomRay.p(), delta); + m_lastPosX = me->x(); + m_lastPosY = me->y(); + } + else + { + bool needRedraw = m_trackball->updateNavigation(translatedMousePosX, translatedMousePosY); + if (needRedraw) + { + m_viewer->navigationPolicyUpdate(); + } + } + isEventHandled = true; + m_hasMovedMouseDuringNavigation = true; + } + } + } + break; + case QEvent::Wheel: + { + if (inputEvent->modifiers() == Qt::NoModifier) + { + initializeRotationCenter(); + if (m_isRotCenterInitialized) + { + QWheelEvent* we = static_cast ( inputEvent); + int translatedMousePosX = we->x(); + int translatedMousePosY = m_viewer->height() - we->y(); + int delta = we->delta(); + + cvf::ref ray; + if (delta < 0) + ray = m_viewer->mainCamera()->rayFromWindowCoordinates(translatedMousePosX, translatedMousePosY); + else + ray = m_viewer->mainCamera()->rayFromWindowCoordinates((int)(1.0*translatedMousePosX), (int)(1.0*translatedMousePosY)); + + zoomAlongRay(ray.p(), delta); + + } + isEventHandled = true; + } + } + break; + } + + return false;//isEventHandled; +} + +#endif + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void caf::TrackBallBasedNavigation::initializeRotationCenter() +{ + if (m_isRotCenterInitialized + || m_trackball.isNull() + || !m_viewer->currentScene()->boundingBox().isValid()) + { + return; + } + + cvf::Vec3d pointOfInterest = m_viewer->currentScene()->boundingBox().center(); + + this->setPointOfInterest(pointOfInterest); +} + +//-------------------------------------------------------------------------------------------------- +/// Repositions and orients the camera to view the rotation point along the +/// direction "alongDirection". The distance to the rotation point is maintained. +/// +//-------------------------------------------------------------------------------------------------- +void caf::TrackBallBasedNavigation::setView( const cvf::Vec3d& alongDirection, const cvf::Vec3d& upDirection ) +{ + m_trackball->setView(alongDirection, upDirection); + /* + if (m_camera.isNull()) return; + + Vec3d dir = alongDirection; + if (!dir.normalize()) return; + Vec3d up = upDirection; + if(!up.normalize()) up = Vec3d::Z_AXIS; + + if((up * dir) < 1e-2) up = dir.perpendicularVector(); + + Vec3d cToE = m_camera->position() - m_rotationPoint; + Vec3d newEye = m_rotationPoint - cToE.length() * dir; + + m_camera->setFromLookAt(newEye, m_rotationPoint, upDirection); + */ +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +cvf::Vec3d caf::TrackBallBasedNavigation::pointOfInterest() +{ + initializeRotationCenter(); + return m_pointOfInterest; +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void caf::TrackBallBasedNavigation::setPointOfInterest(cvf::Vec3d poi) +{ + m_pointOfInterest = poi; + m_trackball->setRotationPoint(poi); + m_isRotCenterInitialized = true; + m_viewer->updateParallelProjectionCameraPosFromPointOfInterestMove(m_pointOfInterest); + +} + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +void caf::TrackBallBasedNavigation::zoomAlongRay(cvf::Ray* ray, int delta) +{ + if (ray && abs(delta) > 0) + { + cvf::Vec3d pos, vrp, up; + m_viewer->mainCamera()->toLookAt(&pos, &vrp, &up); + + double scale = delta/8.0 * 1.0/150 * (pos - m_pointOfInterest).length(); + cvf::Vec3d trans = scale * ray->direction(); + cvf::Vec3d newPos = pos + trans; + cvf::Vec3d newVrp = vrp + trans; + + m_viewer->mainCamera()->setFromLookAt(newPos, newVrp, up ); + m_viewer->updateParallelProjectionHeightFromMoveZoom(m_pointOfInterest); + m_viewer->navigationPolicyUpdate(); + } +} + + diff --git a/Fwk/AppFwk/cafViewer/cafTrackBallBasedNavigation.h b/Fwk/AppFwk/cafViewer/cafTrackBallBasedNavigation.h new file mode 100644 index 0000000000..c0cc22a570 --- /dev/null +++ b/Fwk/AppFwk/cafViewer/cafTrackBallBasedNavigation.h @@ -0,0 +1,78 @@ +//################################################################################################## +// +// Custom Visualization Core library +// Copyright (C) 2015 Ceetron Solutions AS +// +// This library may be used under the terms of either the GNU General Public License or +// the GNU Lesser General Public License as follows: +// +// GNU General Public License Usage +// This library is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This library 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 General Public License at <> +// for more details. +// +// GNU Lesser General Public License Usage +// This library 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 2.1 of the License, or +// (at your option) any later version. +// +// This library 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 at <> +// for more details. +// +//################################################################################################## +#pragma once + +#include "cafNavigationPolicy.h" + +namespace cvf { + class ManipulatorTrackball; + class Ray; +} + +namespace caf +{ + +class TrackBallBasedNavigation: public NavigationPolicy +{ +protected: + // General navigation policy overrides + virtual void init(); + + virtual void setView( const cvf::Vec3d& alongDirection, const cvf::Vec3d& upDirection ); + virtual cvf::Vec3d pointOfInterest(); + virtual void setPointOfInterest(cvf::Vec3d poi); + + // Track ball navigation specific + void initializeRotationCenter(); + cvf::ref m_trackball; + bool m_isRotCenterInitialized; + cvf::Vec3d m_pointOfInterest; + + bool m_isNavigating; + bool m_hasMovedMouseDuringNavigation; + + // Zooming towards cursor + void zoomAlongRay( cvf::Ray* ray, int delta ); + bool m_isZooming; + cvf::ref m_zoomRay; + int m_lastPosX; /// Previous mouse position + int m_lastPosY; + +}; + +} // End namespace caf + + diff --git a/Fwk/AppFwk/cafViewer/cafViewer.cpp b/Fwk/AppFwk/cafViewer/cafViewer.cpp index 788fac3e38..c957fc0f02 100644 --- a/Fwk/AppFwk/cafViewer/cafViewer.cpp +++ b/Fwk/AppFwk/cafViewer/cafViewer.cpp @@ -56,6 +56,7 @@ #include "cvfTransform.h" #include "cvfRayIntersectSpec.h" #include "cvfHitItemCollection.h" +#include "cvfManipulatorTrackball.h" #include "cvfDebugTimer.h" #include "cvfqtPerformanceInfoHud.h"