24 VectorDisplay::Color::Color(uint32_t col)
26 r=float((col & 0xFF0000ull)>>16)/255.0;
27 g=float((col & 0xFF00ull)>>8)/255.0;
28 b=float(col & 0xFFull)/255.0;
31 VectorDisplay::Color::Color(
float _r,
float _g,
float _b)
38 VectorDisplay::VectorDisplay(QWidget* parent) : QGLWidget(QGLFormat ( QGL::SampleBuffers ),parent)
43 robotLoc.set(0.0,0.0);
44 displayWindow = 100.0;
50 viewXOffset = viewYOffset = 0.0;
56 connect(
this, SIGNAL(redraw()),
this, SLOT(redrawHandler()));
60 void VectorDisplay::redrawHandler()
64 graphicsMutex.unlock();
67 void VectorDisplay::mousePressEvent(QMouseEvent* event)
69 leftButton =
event->buttons().testFlag(Qt::LeftButton);
70 midButton =
event->buttons().testFlag(Qt::MidButton);
71 rightButton =
event->buttons().testFlag(Qt::RightButton);
72 bool shiftKey =
event->modifiers().testFlag(Qt::ShiftModifier);
73 bool ctrlKey =
event->modifiers().testFlag(Qt::ControlModifier);
74 bool altKey =
event->modifiers().testFlag(Qt::AltModifier);
76 if(leftButton || midButton){
78 mouseStartX =
event->x();
79 mouseStartY =
event->y();
82 if( (ctrlKey || shiftKey || altKey) && leftButton ){
83 double scale = 2.0*viewScale*min(width(),height())/displayWindow;
84 double x = (double(event->x()) - 0.5*
double(width()) - viewXOffset)/scale;
85 double y = (0.5*double(height()) - double(event->y()) - viewYOffset)/scale;
88 mouseStartX =
event->x();
89 mouseStartY =
event->y();
94 void VectorDisplay::mouseReleaseEvent(QMouseEvent* event)
96 bool shiftKey =
event->modifiers().testFlag(Qt::ShiftModifier);
97 bool ctrlKey =
event->modifiers().testFlag(Qt::ControlModifier);
98 bool altKey =
event->modifiers().testFlag(Qt::AltModifier);
100 double scale = 2.0*viewScale*min(width(),height())/displayWindow;
101 double x = (double(event->x()) - 0.5*
double(width()) - viewXOffset)/scale;
102 double y = (0.5*double(height()) - double(event->y()) - viewYOffset)/scale;
105 if( shiftKey && leftButton ){
106 double setOrientation = atan2(-(event->y()-mouseStartY),event->x()-mouseStartX);
108 ptrCallback(setLocation,setLocation2,setOrientation, 3);
109 }
else if( ctrlKey && leftButton ){
110 double setOrientation = atan2(-(event->y()-mouseStartY),event->x()-mouseStartX);
112 ptrCallback(setLocation,setLocation2,setOrientation, 2);
113 }
else if( altKey && leftButton ){
114 double setOrientation = atan2(-(event->y()-mouseStartY),event->x()-mouseStartX);
116 ptrCallback(setLocation,setLocation2,setOrientation, 1);
120 void VectorDisplay::mouseMoveEvent(QMouseEvent* event)
122 static const bool debug =
false;
123 bool leftButton =
event->buttons().testFlag(Qt::LeftButton);
124 bool midButton =
event->buttons().testFlag(Qt::MidButton);
125 bool rightButton =
event->buttons().testFlag(Qt::RightButton);
126 bool shiftKey =
event->modifiers().testFlag(Qt::ShiftModifier);
127 bool ctrlKey =
event->modifiers().testFlag(Qt::ControlModifier);
128 bool altKey =
event->modifiers().testFlag(Qt::AltModifier);
130 if(debug) printf(
"MouseMove Event, Left:%d Mid:%d Right:%d\n", leftButton?1:0, midButton?1:0, rightButton?1:0);
131 if(shiftKey || ctrlKey || altKey){
137 viewXOffset += double(event->x() - mouseStartX);
138 viewYOffset -= double(event->y() - mouseStartY);
139 mouseStartX =
event->x();
140 mouseStartY =
event->y();
144 double zoomRatio = -double(event->y() - mouseStartY)/500.0;
145 double oldScale = viewScale;
146 viewScale = viewScale*(1.0+zoomRatio);
147 viewXOffset *= viewScale/oldScale;
148 viewYOffset *= viewScale/oldScale;
149 mouseStartX =
event->x();
150 mouseStartY =
event->y();
155 void VectorDisplay::wheelEvent(QWheelEvent* event)
157 static const bool debug =
false;
158 double zoomRatio = double(event->delta())/1000.0;
159 double oldScale = viewScale;
160 viewScale = viewScale*(1.0+zoomRatio);
161 viewXOffset *= viewScale/oldScale;
162 viewYOffset *= viewScale/oldScale;
163 if(debug) printf(
"Zoom: %5.3f\n",viewScale);
167 void VectorDisplay::keyPressEvent(QKeyEvent* event)
169 static const bool debug =
false;
171 bool ctrlKey =
event->modifiers().testFlag(Qt::ControlModifier);
172 if(debug) printf(
"KeyPress: 0x%08X\n",event->key());
173 if(event->key() == Qt::Key_Space)
175 if(event->key() == Qt::Key_Escape)
177 if(event->key() == Qt::Key_F){
178 followRobot = !followRobot;
180 viewXOffset = viewYOffset = 0.0;
183 if(event->key() == Qt::Key_R){
184 showRobot = !showRobot;
187 if(event->key() == Qt::Key_BracketLeft){
189 pointsSize = max(0.1,pointsSize-0.1);
191 lineThickness = max(0.1,lineThickness-0.1);
194 if(event->key() == Qt::Key_BracketRight){
198 lineThickness += 0.1;
203 void VectorDisplay::resetView()
206 viewXOffset = viewYOffset = 0.0;
210 void VectorDisplay::initializeGL()
212 glEnable(GL_MULTISAMPLE);
215 void VectorDisplay::setupViewport(
int width,
int height)
217 glViewport(0, 0, width, height);
218 glMatrixMode(GL_PROJECTION);
220 glOrtho(-0.5*viewScale*width+viewXOffset, 0.5*viewScale*width+viewXOffset, -0.5*viewScale*height+viewYOffset, 0.5*viewScale*height+viewYOffset, minZValue, maxZValue);
221 glMatrixMode(GL_MODELVIEW);
224 void VectorDisplay::resizeGL(
int width,
int height)
226 setupViewport(width, height);
229 void VectorDisplay::drawCircles(
float lineThickness)
231 static const float circleSize = 0.3;
233 bool coloredCircles = (circleColors.size()==circles.size());
235 for(
unsigned int i=0; i<circles.size(); i++){
236 glColor4f(circleColors[i].red(), circleColors[i].green(), circleColors[i].blue(), 1.0);
237 drawArc(circles[i].x, circles[i].y, circleSize,circleSize+lineThickness,0,M_2PI);
240 glColor4f(0.35,0.35,0.35,1.0);
241 for(
unsigned int i=0; i<circles.size(); i++){
242 drawArc(circles[i].x, circles[i].y, circleSize,circleSize+lineThickness,0,M_2PI);
250 drawLine<num>(l,lineWidth);
253 template <
class num>
void VectorDisplay::drawLine(
Line2d< num >& line, num lineWidth)
265 glVertex3f(V2COMP(p0),0.0);
266 glVertex3f(V2COMP(p1),0.0);
267 glVertex3f(V2COMP(p2),0.0);
268 glVertex3f(V2COMP(p3),0.0);
274 drawQuad(loc.x-pointSize, loc.y-pointSize, loc.x+pointSize, loc.y+pointSize, 0.2);
278 void VectorDisplay::drawLines(
float lineThickness)
280 bool coloredLines = (lineColors.size()==lines.size());
282 for(
unsigned int i=0; i<lines.size(); i++){
283 glColor4f(lineColors[i].red(), lineColors[i].green(), lineColors[i].blue(), 1.0);
284 drawLine(lines[i],lineThickness);
287 glColor4f(0.35,0.35,0.35,1.0);
288 for(
unsigned int i=0; i<lines.size(); i++){
289 drawLine(lines[i],lineThickness);
294 void VectorDisplay::drawPoints(
float pointsSize)
296 bool coloredPoints = (pointColors.size()==points.size());
298 for(
unsigned int i=0; i<points.size(); i++){
299 glColor4f(pointColors[i].red(), pointColors[i].green(), pointColors[i].blue(), 1.0);
300 drawPoint(points[i],pointsSize);
303 glColor4f(0.94,0.46,0.12,1.0);
305 for(
unsigned int i=0; i<points.size(); i++){
306 drawPoint(points[i],pointsSize);
314 graphicsMutex.lock();
316 glClearColor(BACKGROUND_COLOR);
317 glShadeModel(GL_SMOOTH);
318 glDisable(GL_LIGHTING);
319 glDisable(GL_CULL_FACE);
320 glEnable(GL_DEPTH_TEST);
321 glEnable(GL_MULTISAMPLE);
323 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
324 glMatrixMode(GL_MODELVIEW);
328 double scale = 2.0*viewScale*min(width(),height())/displayWindow;
329 glTranslatef(viewXOffset, viewYOffset, 0.0);
330 glScalef(scale,scale,scale);
333 glTranslatef(-(robotLoc.x),-(robotLoc.y),0.0);
335 drawLines(lineThickness/scale);
336 drawCircles(lineThickness/scale);
337 drawPoints(pointsSize/scale);
341 const double robotSize = 0.25;
344 vector2d p = robotLoc + 2.0*robotSize*h;
345 glColor4f(1.0, 0.416, 0.0, 1.0);
346 drawArc(robotLoc,robotSize, robotSize+1.5*lineThickness/scale,0.0,M_2PI,0.0,RAD(4.0));
347 drawLine(robotLoc,p, 0.75*lineThickness/scale);
353 graphicsMutex.unlock();
356 void VectorDisplay::updateLines(vector<line2f> _lines, vector<Color> _lineColors)
358 graphicsMutex.lock();
360 if(lines.size()==_lineColors.size())
361 lineColors = _lineColors;
364 graphicsMutex.unlock();
368 void VectorDisplay::updatePoints(vector< vector2f > _points, vector< Color > _pointColors)
370 graphicsMutex.lock();
372 if(points.size()==_pointColors.size())
373 pointColors = _pointColors;
376 graphicsMutex.unlock();
380 void VectorDisplay::updateCircles(vector< vector2f > _circles, vector< Color > _circleColors)
382 graphicsMutex.lock();
384 if(circles.size()==_circleColors.size())
385 circleColors = _circleColors;
387 circleColors.clear();
388 graphicsMutex.unlock();
392 void VectorDisplay::updateDisplay(
vector2d _robotLoc,
double _robotAngle,
double _displayWindow,
393 vector<line2f> _lines, vector<vector2f> _points, vector<vector2f> _circles,
394 vector<Color> _lineColors, vector<Color> _pointColors, vector<Color> _circleColors)
396 updateLines(_lines,_lineColors);
397 updatePoints(_points,_pointColors);
398 updateCircles(_circles,_circleColors);
399 graphicsMutex.lock();
400 robotAngle = _robotAngle;
401 robotLoc = _robotLoc;
402 displayWindow = _displayWindow;
403 graphicsMutex.unlock();
407 void VectorDisplay::resizeEvent(QResizeEvent* event)
409 QGLWidget::resizeEvent(event);
416 glVertex3d(loc1.x,loc1.y,z);
417 glVertex3d(loc2.x,loc1.y,z);
418 glVertex3d(loc2.x,loc2.y,z);
419 glVertex3d(loc1.x,loc2.y,z);
423 template <
class num>
void VectorDisplay::drawArc(
GVector::vector2d< num > loc, num r1, num r2, num theta1, num theta2, num z, num dTheta)
425 static const double tesselation = 0.1;
427 dTheta = tesselation/r2;
429 glBegin(GL_QUAD_STRIP);
430 for(
double theta=theta1; theta<theta2; theta+=dTheta){
431 double c1 = cos(theta), s1 = sin(theta);
432 glVertex3d(r2*c1+loc.x,r2*s1+loc.y,z);
433 glVertex3d(r1*c1+loc.x,r1*s1+loc.y,z);
435 double c1 = cos(theta2), s1 = sin(theta2);
436 glVertex3d(r2*c1+loc.x,r2*s1+loc.y,z);
437 glVertex3d(r1*c1+loc.x,r1*s1+loc.y,z);
void set(num nx, num ny)
set the components of the vector
A GUI for Vector Localization; C++ Interface: VectorDisplay.
void redraw()
Accept Signals to update display.
void paintEvent(QPaintEvent *event)
Thread-safe way of scheduling display updates.
void heading(num angle)
make a unit vector at given angle