Roomba App  1.0
glWidget.cpp
Go to the documentation of this file.
1 
12 #include "glWidget.hh"
13 
14 GLWidget::GLWidget(QWidget* parent) : QGLViewer(parent) {}
15 
16 void GLWidget::obstacleDataReady(const std::map<float, int>& obstacleData)
17 {
18  _obstacleCoordinates.clear();
19  _obstacleRadius.clear();
20 
21  std::vector<float> tmp_vector;
22 
23  for(const auto& [angle, distance] : obstacleData)
24  {
25  float radianAngle = ((angle + 1) * PI) / 180.0;
26 
27  float x = distance * __scale * cos(radianAngle);
28  float y = distance * __scale * sin(radianAngle);
29  float z = __lidarHeight;
30  _obstacleCoordinates.push_back({x, y, z});
31  tmp_vector.push_back(distance);
32  }
33 
34  float min = *std::min_element(std::begin(tmp_vector), std::end(tmp_vector));
35  float max = *std::max_element(std::begin(tmp_vector), std::end(tmp_vector));
36 
37  for(unsigned long i = 0; i < tmp_vector.size(); i++)
38  {
39  float value = tmp_vector[i];
40  float colour = (value - min) / (max - min) * 255.0;
41  _obstacleRadius.push_back(colour);
42  }
43 
44  updateGL();
45 }
46 
48 {
49  // Background
50  glClearColor(0.5, 0.5, 0.5, 1);
51 
52  drawRobot();
53 
54  // for(const auto& coordinates : _obstacleCoordinates)
55  for(unsigned long i = 0; i < _obstacleCoordinates.size(); i++)
56  {
58  _obstacleRadius[i]);
59  }
60 
61  glFlush();
62 }
63 
65 {
66  restoreStateFromFile();
67  // help();
68 }
69 
70 QString GLWidget::helpString() const
71 {
72  QString text("<h2>S i m p l e V i e w e r</h2>");
73  text += "Use the mouse to move the camera around the object. ";
74  text += "You can respectively revolve around, zoom and translate with the three mouse buttons. ";
75  text += "Left and middle buttons pressed together rotate around the camera view direction axis<br><br>";
76  text += "Pressing <b>Alt</b> and one of the function keys (<b>F1</b>..<b>F12</b>) defines a camera keyFrame. ";
77  text += "Simply press the function key again to restore it. Several keyFrames define a ";
78  text += "camera path. Paths are saved when you quit the application and restored at next start.<br><br>";
79  text += "Press <b>F</b> to display the frame rate, <b>A</b> for the world axis, ";
80  text += "<b>Alt+Return</b> for full screen mode and <b>Control+S</b> to save a snapshot. ";
81  text += "See the <b>Keyboard</b> tab in this window for a complete shortcut list.<br><br>";
82  text += "Double clicks automates single click actions: A left button double click aligns the closer axis with the "
83  "camera (if close enough). ";
84  text +=
85  "A middle button double click fits the zoom of the camera and the right button re-centers the scene.<br><br>";
86  text += "A left button double click while holding right button pressed defines the camera <i>Revolve Around "
87  "Point</i>. ";
88  text += "See the <b>Mouse</b> tab and the documentation web pages for details.<br><br>";
89  text += "Press <b>Escape</b> to exit the viewer.";
90  return text;
91 }
92 
94 {
96 
97  glPushMatrix();
98  glPushMatrix();
99 
100  glTranslatef(__robotRadius, 0, __robotWheelRadius);
101  glRotatef(90, 0, 1, 0);
103 
104  glPopMatrix();
105 
106  glTranslatef(-__robotRadius, 0, __robotWheelRadius);
107  glRotatef(-90, 0, 1, 0);
109 
110  glPopMatrix();
111 }
112 
113 void GLWidget::drawOneObstacle(GLfloat x, GLfloat y, GLfloat z, GLfloat color)
114 {
115  glPushMatrix();
116  glTranslatef(x, y, z);
117 
118  _drawCube(__obstacleSize, 0, color, 0);
119  glPopMatrix();
120 }
121 
122 void GLWidget::_drawCube(GLfloat size, GLubyte R, GLubyte G, GLubyte B)
123 {
124  glPushMatrix();
125 
126  glColor3ub(R, G, B);
127  glBegin(GL_POLYGON);
128  glVertex3f(size, -size, size);
129  glVertex3f(size, size, size);
130  glVertex3f(-size, size, size);
131  glVertex3f(-size, -size, size);
132  glEnd();
133 
134  glColor3ub(R, G, B);
135  glBegin(GL_POLYGON);
136  glVertex3f(size, -size, -size);
137  glVertex3f(size, size, -size);
138  glVertex3f(size, size, size);
139  glVertex3f(size, -size, size);
140  glEnd();
141 
142  glColor3ub(R, G, B);
143  glBegin(GL_POLYGON);
144  glVertex3f(-size, -size, size);
145  glVertex3f(-size, size, size);
146  glVertex3f(-size, size, -size);
147  glVertex3f(-size, -size, -size);
148  glEnd();
149 
150  glColor3ub(R, G, B);
151  glBegin(GL_POLYGON);
152  glVertex3f(size, size, size);
153  glVertex3f(size, size, -size);
154  glVertex3f(-size, size, -size);
155  glVertex3f(-size, size, size);
156  glEnd();
157 
158  glColor3ub(R, G, B);
159  glBegin(GL_POLYGON);
160  glVertex3f(size, -size, -size);
161  glVertex3f(size, -size, size);
162  glVertex3f(-size, -size, size);
163  glVertex3f(-size, -size, -size);
164  glEnd();
165 
166  glPopMatrix();
167 }
168 
169 void GLWidget::_drawCylinder(GLfloat radius, GLfloat height, GLubyte R, GLubyte G, GLubyte B)
170 {
171  GLfloat x = 0.0;
172  GLfloat y = 0.0;
173  GLfloat angle = 0.0;
174  GLfloat angle_stepsize = 0.1;
175 
176  // Tube
177  glColor3ub(R, G, B);
178  glBegin(GL_QUAD_STRIP);
179  angle = 0.0;
180  while(angle < 2 * PI)
181  {
182  x = radius * cos(angle);
183  y = radius * sin(angle);
184  glVertex3f(x, y, height);
185  glVertex3f(x, y, 0.0);
186  angle = angle + angle_stepsize;
187  }
188  glVertex3f(radius, 0.0, height);
189  glVertex3f(radius, 0.0, 0.0);
190  glEnd();
191 
192  // Top circle
193  glColor3ub(R, G, B);
194  glBegin(GL_POLYGON);
195  angle = 0.0;
196  while(angle < 2 * PI)
197  {
198  x = radius * cos(angle);
199  y = radius * sin(angle);
200  glVertex3f(x, y, height);
201  angle = angle + angle_stepsize;
202  }
203  glVertex3f(radius, 0.0, height);
204  glEnd();
205 
206  // Bottom circle
207  glColor3ub(R, G, B);
208  glBegin(GL_POLYGON);
209  angle = 0.0;
210  while(angle < 2 * PI)
211  {
212  x = radius * cos(angle);
213  y = radius * sin(angle);
214  glVertex3f(x, y, 0);
215  angle = angle + angle_stepsize;
216  }
217  glVertex3f(radius, 0.0, 0);
218  glEnd();
219 }
const float __scale
Definition: glWidget.hh:102
const float __robotHeight
Definition: glWidget.hh:104
const float __robotRadius
Definition: glWidget.hh:105
void _drawCylinder(GLfloat radius, GLfloat height, GLubyte R, GLubyte G, GLubyte B)
Draw cylinder shape.
Definition: glWidget.cpp:169
void obstacleDataReady(const std::map< float, int > &lidarData)
Slot to get lidar data when parser sent signal that this data is ready.
Definition: glWidget.cpp:16
void drawRobot()
Draw robot shape.
Definition: glWidget.cpp:93
void drawOneObstacle(GLfloat x, GLfloat y, GLfloat z, GLfloat color)
Draw one obstacle.
Definition: glWidget.cpp:113
GLWidget(QWidget *parent)
Construct a new GLWidget object.
Definition: glWidget.cpp:14
const float __robotWheelRadius
Definition: glWidget.hh:107
std::vector< std::vector< float > > _obstacleCoordinates
All obstacles coordinates.
Definition: glWidget.hh:89
const float __obstacleSize
Definition: glWidget.hh:112
void _drawCube(GLfloat size, GLubyte R, GLubyte G, GLubyte B)
Draw cube shape.
Definition: glWidget.cpp:122
virtual void init() override
Initialize openGL widget.
Definition: glWidget.cpp:64
const float __lidarHeight
Definition: glWidget.hh:110
std::vector< float > _obstacleRadius
_obstacleRadius All obstacles radius
Definition: glWidget.hh:94
virtual void draw() override
Draw content in openGL widget.
Definition: glWidget.cpp:47
virtual QString helpString() const override
Construct help string for openGL widget.
Definition: glWidget.cpp:70
const float __robotWheelThickness
Definition: glWidget.hh:108
#define PI
Definition: glWidget.hh:17