Q3DWidget.h 7.62 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 25 26 27 28 29 30 31
/*=====================================================================

QGroundControl Open Source Ground Control Station

(c) 2009, 2010 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>

This file is part of the QGROUNDCONTROL project

    QGROUNDCONTROL 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.

    QGROUNDCONTROL 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 for more details.

    You should have received a copy of the GNU General Public License
    along with QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.

======================================================================*/

/**
 * @file
 *   @brief Definition of the class Q3DWidget.
 *
 *   @author Lionel Heng <hengli@student.ethz.ch>
 *
 */

32 33
#ifndef Q3DWIDGET_H
#define Q3DWIDGET_H
lm's avatar
lm committed
34 35

#include <QtOpenGL>
pixhawk's avatar
pixhawk committed
36
#include <inttypes.h>
37

38 39
#include <osg/LineSegment>
#include <osg/PositionAttitudeTransform>
40
#include <osgGA/TrackballManipulator>
41
#include <osgText/Font>
42
#include <osgViewer/Viewer>
lm's avatar
lm committed
43

44
#include "CameraParams.h"
45
#include "GCManipulator.h"
46
#include "SystemGroupNode.h"
47

48 49 50 51 52 53 54 55
/**
 * @brief Definition of the class Q3DWidget.
 * The Q3DWidget widget uses the OpenSceneGraph framework to render
 * user-defined objects in 3D. The world coordinate system in OSG is defined:
 * - x-axis points to the east
 * - y-axis points to the north
 * - z-axis points upwards
 */
56
class Q3DWidget : public QGLWidget, public osgViewer::Viewer
lm's avatar
lm committed
57
{
58
    Q_OBJECT
lm's avatar
lm committed
59 60

public:
61 62 63 64
    /**
     * Constructor.
     * @param parent Parent widget.
     */
65
    Q3DWidget(QWidget* parent = 0);
66 67 68 69

    /**
     * Destructor.
     */
70
    virtual ~Q3DWidget();
lm's avatar
lm committed
71

72 73 74 75
    /**
     * @brief Initializes the widget.
     * @param fps Frames per second.
     */
76
    void init(float fps);
77 78 79 80 81 82 83 84

    /**
     * @brief Sets the camera parameters.
     * @param minZoomRange Minimum distance from the viewer to the camera.
     * @param cameraFov Camera field of view.
     * @param minClipRange Distance from the viewer to the near clipping range.
     * @param maxClipRange Distance from the viewer to the far clipping range.
     */
85
    void setCameraParams(float minZoomRange, float cameraFov,
86
                         float minClipRange, float maxClipRange);
lm's avatar
lm committed
87

88 89 90 91 92 93
    /**
     * @brief Moves the camera by [dx,dy,dz].
     * @param dx Translation along the x-axis in meters.
     * @param dy Translation along the y-axis in meters.
     * @param dz Translation along the z-axis in meters.
     */
94
    void moveCamera(double dx, double dy, double dz);
95

96 97 98
    /**
     * @brief Recenters the camera at (x,y,z).
     */
99
    void recenterCamera(double x, double y, double z);
lm's avatar
lm committed
100

101 102 103 104 105
    /**
      * @brief Rotates the camera.
      */
    void rotateCamera(double roll, double pitch, double yaw);

106 107 108 109 110
    /**
      * @brief Sets the camera distance.
      */
    void setCameraDistance(double distance);

111 112 113 114
    /**
     * @brief Sets up 3D display mode.
     */
    void setDisplayMode3D(void);
lm's avatar
lm committed
115

116 117 118 119 120 121 122 123
    osg::ref_ptr<osg::Switch>& hudGroup(void);

    /**
     * @brief Get screen coordinates of mouse cursor.
     * @return screen coordinates of mouse cursor.
     */
    QPoint mouseCursorCoords(void);

124 125 126 127 128 129 130
    /**
     * @brief Gets the world 3D coordinates of the cursor.
     * The function projects the 2D cursor position to a line in world space
     * and returns the intersection of that line and the horizontal plane
     * which contains the point (0, 0, z);
     * @param cursorX x-coordinate of the cursor.
     * @param cursorY y-coordinate of the cursor.
131 132
     * @param worldZ z-coordinate of the point in the plane.
     * @return World (x,y) cursor coordinates.
133
     */
134
     QPointF worldCursorPosition(const QPoint& cursorPos, double worldZ) const;
lm's avatar
lm committed
135

136
     CameraParams& cameraParams(void);
137

138
     osg::ref_ptr<GCManipulator>& cameraManipulator(void);
139

140
     osg::ref_ptr<osgText::Font>& font(void);
141

142 143
     osg::ref_ptr<osg::Switch>& worldMap(void);
     osg::ref_ptr<SystemGroupNode>& systemGroup(int systemId);
144

145
     bool& handleDeviceEvents(void);
146

147 148 149 150 151 152 153 154 155
     void handleKeyPressEvent(QKeyEvent* event);

     void handleKeyReleaseEvent(QKeyEvent* event);

     void handleMousePressEvent(QMouseEvent* event);

     void handleMouseReleaseEvent(QMouseEvent* event);

     void handleMouseMoveEvent(QMouseEvent* event);
156

157 158 159
     void handleWheelEvent(QWheelEvent* event);

protected slots:
160
    /**
161
     * @brief Updates the widget.
162
     */
163
    void redraw(void);
164

165 166 167 168 169
signals:
    void sizeChanged(int width, int height);
    void update(void);

protected:
170 171 172 173 174
    /**
     * @brief Handle widget resize event.
     * @param width New width of widget.
     * @param height New height of widget.
     */
175
    virtual void resizeGL(int width, int height);
176 177 178 179 180

    /**
     * @brief Processes key press events.
     * @param event Key press event.
     */
181
    virtual void keyPressEvent(QKeyEvent* event);
182 183 184 185 186

    /**
     * @brief Processes key release events.
     * @param event Key release event.
     */
187
    virtual void keyReleaseEvent(QKeyEvent* event);
188 189 190 191 192

    /**
     * @brief Processes mouse press events.
     * @param event Mouse press event.
     */
193
    virtual void mousePressEvent(QMouseEvent* event);
194 195 196 197 198

    /**
     * @brief Processes mouse release events.
     * @param event Mouse release event.
     */
199
    virtual void mouseReleaseEvent(QMouseEvent* event);
200 201 202 203 204

    /**
     * @brief Processes mouse move events.
     * @param event Mouse move event.
     */
205
    virtual void mouseMoveEvent(QMouseEvent* event);
206 207 208 209 210

    /**
     * @brief Processes mouse wheel events.
     * @param event Mouse wheel event.
     */
211 212
    virtual void wheelEvent(QWheelEvent* event);

213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
    /** @brief Start widget updating */
    void showEvent(QShowEvent* event);
    /** @brief Stop widget updating */
    void hideEvent(QHideEvent* event);

    /**
     * @brief Get base HUD geode.
     * @return Smart pointer to the geode.
     */
    osg::ref_ptr<osg::Node> createHUD(void);

    /**
     * @brief Handle widget paint event.
     */
    virtual void paintGL(void);

229 230 231 232 233
    /**
     * @brief Converts Qt-defined key to OSG-defined key.
     * @param key Qt-defined key.
     * @return OSG-defined key.
     */
234
    osgGA::GUIEventAdapter::KeySymbol convertKey(int key) const;
235 236 237 238 239 240 241

    /**
     * @brief Computes intersection of line and plane.
     * @param plane Vector which represents the plane.
     * @param line Line representation.
     * @return 3D point which lies at the intersection of the line and plane.
     */
242 243 244
    bool planeLineIntersection(const osg::Vec4d& plane,
                               const osg::LineSegment& line,
                               osg::Vec3d& isect) const;
245

246
    bool mHandleDeviceEvents;
247

248 249 250
    osg::ref_ptr<osg::Group> mRoot; /**< Root node of scene graph. */
    osg::ref_ptr<osg::Switch> mWorldMap;
    QMap<int, osg::ref_ptr<SystemGroupNode> > mSystemGroups;
251

252 253
    osg::ref_ptr<osg::Switch> mHudGroup; /**< A group which contains renderable HUD objects. */
    osg::ref_ptr<osg::Projection> mHudProjectionMatrix; /**< An orthographic projection matrix for HUD display. */
254

255
    osg::ref_ptr<osgViewer::GraphicsWindowEmbedded> mOsgGW; /**< A class which manages OSG graphics windows and events. */
256

257
    osg::ref_ptr<GCManipulator> mCameraManipulator; /**< Camera manipulator. */
258

259
    QTimer mTimer; /**< Timer which draws graphics based on specified fps. */
260

261 262
    CameraParams mCameraParams; /**< Struct representing camera parameters. */
    float mFps;
263

264
    osg::ref_ptr<osgText::Font> mFont;
lm's avatar
lm committed
265 266
};

267
#endif // Q3DWIDGET_H