Q3DWidget.h 8.24 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 <osgViewer/Viewer>
lm's avatar
lm committed
42

43 44
#include "GCManipulator.h"

45 46 47 48 49 50 51 52
/**
 * @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
 */
53
class Q3DWidget : public QGLWidget, public osgViewer::Viewer
lm's avatar
lm committed
54
{
55
    Q_OBJECT
lm's avatar
lm committed
56 57

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

    /**
     * Destructor.
     */
67
    virtual ~Q3DWidget();
lm's avatar
lm committed
68

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

    /**
     * @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.
     */
82
    void setCameraParams(float minZoomRange, float cameraFov,
83
                         float minClipRange, float maxClipRange);
lm's avatar
lm committed
84

85 86 87 88 89 90 91 92
    /**
     * @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.
     */
    void moveCamera(float dx, float dy, float dz);

93 94 95
    /**
     * @brief Recenters the camera at (x,y,z).
     */
96
    void recenterCamera(float x, float y, float z);
lm's avatar
lm committed
97

98 99 100 101
    /**
     * @brief Sets up 3D display mode.
     */
    void setDisplayMode3D(void);
lm's avatar
lm committed
102

103 104 105 106 107 108 109 110 111 112 113 114
    /**
     * @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.
     * @param z z-coordinate of the point in the plane.
     * @return A pair of values containing the world 3D cursor coordinates.
     */
    std::pair<double,double> getGlobalCursorPosition(int32_t cursorX,
                                                     int32_t cursorY,
115
                                                     double z);
lm's avatar
lm committed
116

117
protected slots:
118 119 120
    /**
     * @brief Updates the widget.
     */
121
    void redraw(void);
122 123

protected:
124 125 126 127
    /**
     * @brief Get base robot geode.
     * @return Smart pointer to the geode.
     */
128
    osg::ref_ptr<osg::Geode> createRobot(void);
129 130 131 132 133

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

136 137 138 139
    /**
     * @brief Get screen x-coordinate of mouse cursor.
     * @return screen x-coordinate of mouse cursor.
     */
140 141
    int getMouseX(void);

142 143 144 145 146
    /**
     * @brief Get screen y-coordinate of mouse cursor.
     * @return screen y-coordinate of mouse cursor.
     */
    int getMouseY(void);
147

148 149 150 151 152
    /**
     * @brief Handle widget resize event.
     * @param width New width of widget.
     * @param height New height of widget.
     */
153
    virtual void resizeGL(int width, int height);
154 155 156 157

    /**
     * @brief Handle widget paint event.
     */
158
    virtual void paintGL(void);
159 160 161 162 163 164 165 166 167 168 169 170 171

    /**
     * @brief This function is a container for user-defined rendering.
     * All code to render objects should be in this function.
     */
    virtual void display(void);

    /**
     * @brief Processes key press events.
     * If this handler is reimplemented, it is very important that you call the
     * base class implementation.
     * @param event Key press event.
     */
172
    virtual void keyPressEvent(QKeyEvent* event);
173 174 175 176 177 178 179

    /**
     * @brief Processes key release events.
     * If this handler is reimplemented, it is very important that you call the
     * base class implementation.
     * @param event Key release event.
     */
180
    virtual void keyReleaseEvent(QKeyEvent* event);
181 182 183 184 185 186 187

    /**
     * @brief Processes mouse press events.
     * If this handler is reimplemented, it is very important that you call the
     * base class implementation.
     * @param event Mouse press event.
     */
188
    virtual void mousePressEvent(QMouseEvent* event);
189 190 191 192 193 194 195

    /**
     * @brief Processes mouse release events.
     * If this handler is reimplemented, it is very important that you call the
     * base class implementation.
     * @param event Mouse release event.
     */
196
    virtual void mouseReleaseEvent(QMouseEvent* event);
197 198 199 200 201 202 203

    /**
     * @brief Processes mouse move events.
     * If this handler is reimplemented, it is very important that you call the
     * base class implementation.
     * @param event Mouse move event.
     */
204
    virtual void mouseMoveEvent(QMouseEvent* event);
205 206 207 208 209 210 211

    /**
     * @brief Processes mouse wheel events.
     * If this handler is reimplemented, it is very important that you call the
     * base class implementation.
     * @param event Mouse wheel event.
     */
212 213
    virtual void wheelEvent(QWheelEvent* event);

214 215 216 217 218
    /**
     * @brief Converts radians to degrees.
     * @param angle Angle in radians.
     * @return angle in degrees.
     */
219
    float r2d(float angle);
220 221 222 223 224 225

    /**
     * @brief Converts degrees to radians.
     * @param angle Angle in degrees.
     * @return angle in radians.
     */
226
    float d2r(float angle);
227 228 229 230 231 232

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

    /**
     * @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.
     */
241 242 243 244
    bool getPlaneLineIntersection(const osg::Vec4d& plane,
                                  const osg::LineSegment& line,
                                  osg::Vec3d& isect);

245
    osg::ref_ptr<osg::Group> root; /**< Root node of scene graph. */
246 247 248 249 250 251
    osg::ref_ptr<osg::Switch> allocentricMap;
    osg::ref_ptr<osg::Switch> rollingMap;
    osg::ref_ptr<osg::Switch> egocentricMap;
    osg::ref_ptr<osg::PositionAttitudeTransform> robotPosition;
    osg::ref_ptr<osg::PositionAttitudeTransform> robotAttitude;

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

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

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

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

261 262 263 264 265 266 267
    struct CameraParams
    {
        float minZoomRange;
        float cameraFov;
        float minClipRange;
        float maxClipRange;
    };
268

269
    CameraParams cameraParams; /**< Struct representing camera parameters. */
lm's avatar
lm committed
270 271
};

272
#endif // Q3DWIDGET_H