CAVAPA-GUI  30.5.2014
 All Classes Namespaces Functions Variables Typedefs Enumerations Pages
sourcescene.h
1 /****************************************************************************
2  * Copyright (c) 2014, Joel Kivelä, Erkki Koskenkorva, Oskari Leppäaho,
3  * Mika Lehtinen and Petri Partanen.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * * Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * * Neither the name of the copyright holders nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 ****************************************************************************/
30 #ifndef SOURCESCENE_H
31 #define SOURCESCENE_H
32 
33 #include <QGLWidget>
34 #include <QGLFunctions>
35 #include <QMatrix4x4>
36 #include <QQuaternion>
37 #include <QVector2D>
38 #include <QBasicTimer>
39 #include <QGLShaderProgram>
40 
41 #include "common.h"
42 #include "gridengine.h"
43 #include "videoengine.h"
44 #include "calibrationpoints.h"
45 #include "cavapa/cavapa.h"
46 
47 namespace cavapa_gui {
48 
49 class GridEngine;
50 
67 class SourceScene : public QGLWidget, protected QOpenGLFunctions
68 {
69  Q_OBJECT
70 
71 public:
76  explicit SourceScene(QWidget *parent = 0);
77  ~SourceScene();
78 
83  float getCameraXRot() { return xRotEuler; }
84 
89  float getCameraYRot() { return yRotEuler; }
90 
95  float getCameraZRot(){ return zRotEuler; }
96 
101  float getCameraHeight(){ return cameraPos.y(); }
102 
107  float getCameraFov(){ return fov; }
108 
114  QVector3D getCameraLookDirection();
115 
120  QVector3D getCameraPosition();
121 
126  cavapa::camera getCameraSettings();
127 
132  void setCameraSettings(cavapa::camera settings);
133 
138  CalibrationPoints getCalibrationPoints() { return calibrationPoints; }
139 
145  void addCalibrationlPoint(QPointF point)
146  {
147  calibrationPoints.push_back(point);
148  }
149 
155  {
156  this->isAddCalibrationPointsEnabled = enabled;
157  }
158 
164  void updateImage(const cv::Mat& image);
165 
166 public slots:
167 
168 
173  void setCameraXRot(float value);
174 
175 
180  void setCameraYRot(float value);
181 
182 
187  void setCameraZRot(float value);
188 
193  void setCameraHeight(float value);
194 
199  void setCameraFov(float value);
200 
205  void setDrawGridEnabled(bool enabled) { isDrawGridEnabled = enabled; }
206 
212  void setDrawCalibrationPointsEnabled(bool enabled) {
213  isDrawCalibrationPointsEnabled = enabled;
214  }
215 
216 
217 protected:
218 
226  void mouseReleaseEvent(QMouseEvent *e);
227 
234  void mousePressEvent(QMouseEvent *e);
235 
240  void timerEvent(QTimerEvent *e);
241 
245  void initializeGL();
246 
252  void resizeGL(int w, int h);
253 
257  void drawBackgroundImage();
258 
262  void drawGrid();
263 
267  void drawCalibrationPoints(QPainter &painter);
268 
272  void paintGL();
273 
277  void initShaders();
278 
279  //void initTextures();
280 
284  inline void setGlViewport();
285 
289  void calculateViewPort();
290 
297  inline QPointF getRelativeVideoPosition(QPoint widgetPoint);
298 
299 private slots:
300 
305  void handleContextMenuAction(QAction* action);
306 
307 private:
308  QImage background;
309  cv::Mat background_mat;
310 
311  QBasicTimer timer; // A timer for updating the scene
312  QGLShaderProgram program;
313  QGLShaderProgram gridProgram;
314  GridEngine geometries;
315  VideoEngine videoengine;
316 
317  QMatrix4x4 gridProjection;
318  QMatrix4x4 videoSurfaceProjection;
319 
320  QPoint newCalibrationPointLocation;
321 
322  qreal fov;
323 
324  QQuaternion cameraYRotation;
325  QQuaternion cameraXRotation;
326  QQuaternion cameraZRotation;
327  QVector3D cameraPos;
328 
329  float xRotEuler = 0;
330  float yRotEuler = 0;
331  float zRotEuler = 0;
332 
333  qreal aspect, zNear, zFar;
334 
335  bool isDrawGridEnabled = false;
336  bool isDrawCalibrationPointsEnabled = false;
337 
338  float videoW;
339  float videoH;
340  int drawAreaX;
341  int drawAreaY;
342 
343  QRect viewPort;
344  CalibrationPoints calibrationPoints;
345 
346  QAction* actionAddCalibrationPoint;
347  QAction* actionRemoveSelectedCalibrationPoint;
348  QAction* actionClearCalibartionPoints;
349 
350  bool isAddCalibrationPointsEnabled = false;
351 };
352 
353 }
354 
355 #endif // SOURCESCENE_H
void drawGrid()
Draws the grid.
Definition: sourcescene.cpp:361
Draws a grid with OpenGL.
Definition: gridengine.h:48
void setCameraYRot(float value)
Sets the camera rotation around Y axis.
Definition: sourcescene.cpp:154
void resizeGL(int w, int h)
Resizes the GL viewport.
Definition: sourcescene.cpp:297
void setCameraHeight(float value)
Sets the camera height.
Definition: sourcescene.cpp:142
A class for drawing the video frame with OpenGL.
Definition: videoengine.h:44
CalibrationPoints getCalibrationPoints()
Gets the calibration points.
Definition: sourcescene.h:138
QVector3D getCameraPosition()
Gets the camera position.
Definition: sourcescene.cpp:255
float getCameraYRot()
Returns the camera rotation around Y axis.
Definition: sourcescene.h:89
void timerEvent(QTimerEvent *e)
The timer event.
Definition: sourcescene.cpp:173
SourceScene draws a video stream and configures camera parameters.
Definition: sourcescene.h:67
void mouseReleaseEvent(QMouseEvent *e)
Reacts to a mouse release event: If right mouse button was released, display a context menu that has ...
Definition: sourcescene.cpp:70
cavapa::camera getCameraSettings()
Gets the camera settings.
Definition: sourcescene.cpp:259
float getCameraZRot()
Returns the camera rotation around Z axis.
Definition: sourcescene.h:95
void setCameraFov(float value)
Sets the camera field of view.
Definition: sourcescene.cpp:168
void setCameraXRot(float value)
Sets the camera rotation around X axis.
Definition: sourcescene.cpp:147
void updateImage(const cv::Mat &image)
Updates the background image.
Definition: sourcescene.cpp:282
CalibrationPoints stores a collection of calibration points that can be drawn on a QPainter...
Definition: calibrationpoints.h:52
void setCameraSettings(cavapa::camera settings)
Sets camera settings.
Definition: sourcescene.cpp:272
void addCalibrationlPoint(QPointF point)
Adds a new calibration point.
Definition: sourcescene.h:145
void paintGL()
Paints the GL scene.
Definition: sourcescene.cpp:408
float getCameraHeight()
Returns the camera height.
Definition: sourcescene.h:101
void setDrawGridEnabled(bool enabled)
Sets if the grid should be drawn.
Definition: sourcescene.h:205
void drawCalibrationPoints(QPainter &painter)
Draws the calibration points.
Definition: sourcescene.cpp:391
void setAddCalibrationPointsEnabled(bool enabled)
Sets if adding calibration points should be possible.
Definition: sourcescene.h:154
void setGlViewport()
Sets the GL viewport.
Definition: sourcescene.cpp:316
void setDrawCalibrationPointsEnabled(bool enabled)
Sets if the calibration points should be drawn.
Definition: sourcescene.h:212
float getCameraFov()
Sets the field of view of the camera.
Definition: sourcescene.h:107
void calculateViewPort()
Calculates the viewport maintaining the video aspect ratio.
Definition: sourcescene.cpp:320
void initShaders()
Initializes the shaders.
Definition: sourcescene.cpp:198
void mousePressEvent(QMouseEvent *e)
Reacts to a mouse press event by highlighting the closest calibration point.
Definition: sourcescene.cpp:112
QPointF getRelativeVideoPosition(QPoint widgetPoint)
Calculates the relative position of a point in the video.
Definition: sourcescene.cpp:134
void push_back(QPointF newPoint)
Adds a new calibration point.
Definition: calibrationpoints.cpp:38
SourceScene(QWidget *parent=0)
Constructs a new SourceScene having the specified parent widget.
Definition: sourcescene.cpp:45
void initializeGL()
Initializes the OpenGL scene.
Definition: sourcescene.cpp:185
void setCameraZRot(float value)
Sets the camera rotation around Z axis.
Definition: sourcescene.cpp:161
float getCameraXRot()
Returns the camera rotation around X axis.
Definition: sourcescene.h:83
void drawBackgroundImage()
Draws the background image.
Definition: sourcescene.cpp:347
QVector3D getCameraLookDirection()
Gets the camera look direction.
Definition: sourcescene.cpp:251