trackballnavigationeventlistener.h 7.61 KB
Newer Older
1
2
// ================================================================================================
// 
schultezub's avatar
schultezub committed
3
// This file is part of the CAMPVis Software Framework.
4
// 
5
// If not explicitly stated otherwise: Copyright (C) 2012-2014, all rights reserved,
schultezub's avatar
schultezub committed
6
//      Christian Schulte zu Berge <christian.szb@in.tum.de>
7
//      Chair for Computer Aided Medical Procedures
8
9
//      Technische Universitaet Muenchen
//      Boltzmannstr. 3, 85748 Garching b. Muenchen, Germany
10
// 
schultezub's avatar
schultezub committed
11
// For a full list of authors and contributors, please refer to the file "AUTHORS.txt".
12
// 
13
14
15
16
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 
// except in compliance with the License. You may obtain a copy of the License at
// 
// http://www.apache.org/licenses/LICENSE-2.0
17
// 
18
19
20
21
// Unless required by applicable law or agreed to in writing, software distributed under the 
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
// either express or implied. See the License for the specific language governing permissions 
// and limitations under the License.
22
23
24
// 
// ================================================================================================

25
26
27
#ifndef TRACKBALLNAVIGATIONEVENTHANDLER_H__
#define TRACKBALLNAVIGATIONEVENTHANDLER_H__

28
29
#include <sigslot/sigslot.h>

30
31
32
33
#include "cgt/bounds.h"
#include "cgt/logmanager.h"
#include "cgt/event/eventlistener.h"
#include "cgt/navigation/trackball.h"
34

35
#include "core/coreapi.h"
36
#include "core/datastructures/datahandle.h"
37
38
#include "core/properties/numericproperty.h"

39
40
#include <vector>

schultezub's avatar
schultezub committed
41
namespace campvis {
42
    class CameraProperty;
43
    class IHasWorldBounds;
44
    class VisualizationProcessor;
45

46
47
48
    /**
     * Wrapper to adapt a CameraProperty to the tgt::Trackball interface.
     */
49
    class CAMPVIS_CORE_API CamPropNavigationWrapper : public tgt::IHasCamera {
50
    public:
51
52
53
54
        /**
         * Constructor
         * \param   camProp     The CameraProperty to wrap around.
         */
55
        explicit CamPropNavigationWrapper(CameraProperty* camProp);
56
57

        /// Virtual Destructor
58
59
        virtual ~CamPropNavigationWrapper();

60
        /// \see tgt::IHasCamera::getCamera()
61
62
        virtual tgt::Camera* getCamera();

63
        /// \see tgt::IHasCamera::update()
64
        virtual void update();
65
66
67
68
69
70
71
72
73
74

    private:
        CameraProperty* _cameraProperty;    ///< CameraProperty this class wraps around

        /// Temporary copy of the property's camera which will be modified and written back to the property upon update().
        tgt::Camera _localCopy;
        /// Flag whether _localCopy is dirty (needs to be written back)
        bool _dirty;
    };

75
    /**
76
     * EventListener implementing a trackball navigation for a CameraProperty.
77
     * Implementation inspired by http://www.opengl.org/wiki/Trackball
78
79
80
     * 
     * \note    Also takes care of automatically adjusting the window ratio for the wrapped
     *          camera when the viewport size changes.
81
     */
82
    class CAMPVIS_CORE_API TrackballNavigationEventListener : public tgt::EventListener, public sigslot::has_slots {
83
84
    public:
        /**
85
         * Creates a TrackballNavigationEventListener.
86
87
88
89
         * \note    TrackballNavigationEventListener keeps and accesses \a viewportSizeProp during the whole
         *          lifetime. Hence make sure the pointer is valid at all times.
         * \param   cameraProperty      Pointer to the CameraProperty to apply the navigation to, must not be 0.
         * \param   viewportSizeProp    Pointer to the property defining the viewport size, must not be 0.
90
         */
91
        TrackballNavigationEventListener(CameraProperty* cameraProperty, IVec2Property* viewportSizeProp);
92
93
94
95

        /**
         * Virtual Destructor
         **/
96
        virtual ~TrackballNavigationEventListener();
97
98


99
100
        /// \see tgt::EventListener::onEvent()
        virtual void onEvent(tgt::Event* e);
101
        
102

103
104
105
106
107
        /**
         * Sets the property defining the viewport size.
         * \param   viewportSizeProp    The new property defining the viewport size, must not be 0.
         */
        void setViewportSizeProperty(IVec2Property* viewportSizeProp);
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126

        /**
         * Reinitializes the camera using the data in \a hwb.
         * If the scene bounds have changed, the camera setup is reinitialized positioning the 
         * camera in front of the data along the z-axis and looking at the center of the data.
         * 
         * \param   hwb     Data to use for reinitialization, must not be 0.
         */
        void reinitializeCamera(const IHasWorldBounds* hwb);

        /**
         * Reinitializes the camera using the data in \a hwb.
         * If the scene bounds have changed, the camera setup is reinitialized positioning the 
         * camera in front of the data along the z-axis and looking at the center of the data.
         * 
         * \param   hwb     Data to use for reinitialization, must not be 0.
         */
        void reinitializeCamera(const tgt::Bounds& worldBounds);

127
128
129
130
131
132
        /**
         * Reinitalizes the camera by the given parameters.
         * \param   position    New camera position
         * \param   focus       New camera focus
         * \param   upVector    New camera up vector
         */
133
134
        void reinitializeCamera(const tgt::vec3& position, const tgt::vec3& focus, const tgt::vec3& upVector);

135
136
137
138
        /**
         * Sets the trackball's center.
         * \param   center  The new trackball's center.
         */
139
140
        void setCenter(const tgt::vec3& center);

141
142
143
144
145
146
        /**
         * Sets the scene bounds for automatic near/far clipping plane adjustment.
         * \param   bounds  New bounds for the rendered scene.
         */
        void setSceneBounds(const tgt::Bounds& bounds);

147
148
149
150
151
152
        /**
         * Returns the current scene bounds for this trackball.
         * \return  _sceneBounds
         */
        const tgt::Bounds& getSceneBounds() const;

153
154
155
156
157
        /**
         * Returns the trackball of the event handler.
         */
        tgt::Trackball* getTrackball();

158
159
160
161
162
163
164
165
166
167
168
169
170
171
        /**
         * Adds \a vp to the list of LQ mode processors.
         * During interaction, TrackballNavigationEventListener will set the LQ mode flag of all
         * LQ mode processors.
         * \param   vp  VisualizationProcessor to add to the list of LQ mode processors.
         */
        void addLqModeProcessor(VisualizationProcessor* vp);

        /**
         * Removes \a vp from the list of LQ mode processors.
         * \param   vp  VisualizationProcessor to remove from the list of LQ mode processors.
         */
        void removeLqModeProcessor(VisualizationProcessor* vp);

172
    protected:
173
174


175
176
177
        /// Slot called when _viewportSizeProp changes 
        void onViewportSizePropChanged(const AbstractProperty* p);

178

179
        CameraProperty* _cameraProperty;        ///< The CameraProperty to apply the navigation to
180
        IVec2Property* _viewportSizeProp;       ///< Pointer to the property defining the viewport size
181
        CamPropNavigationWrapper _cpnw;         ///< The CamPropNavigationWrapper used to adapt to the tgt::Trackball interface
182

183
184
        tgt::Trackball* _trackball;             ///< The tgt::Trackball for the navigation logic
        tgt::Bounds _sceneBounds;               ///< The extent of the scene (in world coordinates)
185

186
187
188
        /// List of processors for which to enable LQ mode during interaction
        std::vector<VisualizationProcessor*> _lqModeProcessors;

189
190
191
192
193
194
        static const std::string loggerCat_;
    };

}

#endif // TRACKBALLNAVIGATIONEVENTHANDLER_H__