4 * @author Olavi Pulkkinen <olavi.pulkkinen@fudeco.com>
5 * @copyright (c) 2010 Speed Freak team
6 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
9 #include "routedialog.h"
10 #include "ui_routedialog.h"
14 #include <QMessageBox>
24 Vector() { x=0.; y=0. ; z=0.; };
25 Vector( qreal initX, qreal initY, qreal initZ) { x = initX, y = initY; z = initZ; };
26 void setX( qreal newX) { x = newX; };
27 void setY( qreal newY) { y = newY; };
28 void setZ( qreal newZ) { z = newZ; };
29 qreal getX() { return x; };
30 qreal getY() { return y; };
31 qreal getZ() { return z; };
32 qreal length() { return sqrt(x*x+y*y+z*z); };
33 Vector operator+(Vector v)
35 x = x + v.x; y = y + v.y; z = z + v.z;
38 Vector operator-(Vector v)
40 x = x - v.x; y = y - v.y; z = z - v.z;
43 Vector operator/(qreal c)
45 x = x/c; y = y/c; z = z/c;
48 Vector crossProduct( Vector a, Vector b)
50 x = a.y*b.z - a.z*b.y;
51 y = a.z*b.x - a.x*b.z;
52 z = a.x*b.y - a.y*b.x;
60 Vector atPoint, fromPoint, up, a1, a2, a3;
61 qreal offsx, offsy, offsz;
65 qreal getOffsx() { return offsx; };
66 qreal getOffsy() { return offsx; };
67 qreal getOffsz() { return offsz; };
68 qreal getDval() { return dval; };
69 void setAngle( qreal newA) { angle = newA; };
70 void setUp( qreal newUpX, qreal newUpY, qreal newUpZ)
72 up.setX(newUpX); up.setY(newUpY); up.setZ(newUpZ);
74 void setAtPoint( qreal newX, qreal newY, qreal newZ)
76 atPoint.setX(newX); atPoint.setY(newY); atPoint.setZ(newZ);
78 void setFromPoint(qreal newX, qreal newY, qreal newZ)
80 fromPoint.setX(newX); fromPoint.setY(newY); fromPoint.setZ(newZ);
84 double amarkmag, tempmag;
87 dval = cos(angle/2.0)/sin(angle/2.0);
88 dist = atPoint-fromPoint;
89 amarkmag = dist.length();
92 temp.crossProduct( dist, up);
93 tempmag = temp.length();
96 temp.crossProduct( a1, a3);
97 tempmag = temp.length();
100 offsx = -a1.getX()*fromPoint.getX() - a1.getY()*fromPoint.getY() - a1.getZ()*fromPoint.getZ();
101 offsy = -a2.getX()*fromPoint.getX() - a2.getY()*fromPoint.getY() - a2.getZ()*fromPoint.getZ();
102 offsz = -a3.getX()*fromPoint.getX() - a3.getY()*fromPoint.getY() - a3.getZ()*fromPoint.getZ();
103 QString jono2 = QString("offsx %1 offsy %2 offsz %3").arg(offsx).arg(offsy).arg(offsz);
104 QMessageBox::about(0,"offs x y z", jono2);
106 Vector getAtPoint() { return atPoint; };
107 Vector getFromPoint() { return fromPoint; };
108 Vector getA1() { return a1; };
109 Vector getA2() { return a2; };
110 Vector getA3() { return a3; };
114 qreal xmax, xmin, ymin, ymax; // Limits in world coordinates
116 QList<Vector> vertexList; // Vertecies of route
118 //Vector atPoint, fromPoint, up, a1, a2, a3;
119 //qreal offsx, offsy, offsz;
121 qreal objxmin, objxmax, objymin, objymax, objzmin, objzmax;
123 qreal a, b,c,d; //, dval;
125 #define maxof(val1,val2) ((val1>val2)?val1:val2)
126 #define toradians( degrees) (degrees*0.017453293)
130 int length = 34; // 24;
133 void setAtPoint( Viewing *v);
134 void setFromPoint( Viewing *v);
139 void dataMinMax( void);
143 RouteDialog::RouteDialog(QWidget *parent) :
145 ui(new Ui::RouteDialog)
148 left = 5; top = 5; right = 395; bottom = 195; // Limits in screen coordinates
151 RouteDialog::~RouteDialog()
156 void RouteDialog::changeEvent(QEvent *e)
158 QDialog::changeEvent(e);
160 case QEvent::LanguageChange:
161 ui->retranslateUi(this);
170 * Draws route to the route dialog
174 void RouteDialog::paintEvent(QPaintEvent *)
177 int startx, starty; // Starting point of the route
179 qreal x1, y1, x2, y2;
180 int scx1, scy1, scx2, scy2;
182 QPainter painter(this);
184 painter.setRenderHint(QPainter::Antialiasing, true);
185 painter.setPen(QPen((Qt::black),2));
186 painter.setBrush(QBrush((Qt::yellow), Qt::SolidPattern));
188 // Draw route window frame
189 /*painter.drawLine(left,top,right,top);
190 painter.drawLine(right,top,right,bottom);
191 painter.drawLine(left,top,left,bottom);
192 painter.drawLine(left,bottom,right,bottom);
195 maxi = vertexList.size()-1; // -1 to remove wrong last point !!!
196 for (i=0; i<maxi-1; i++)
198 v1 = vertexList.at(i);
199 v2 = vertexList.at(i+1);
200 x1 = v1.getX(); y1 = v1.getY();
201 x2 = v2.getX(); y2 = v2.getY();
202 //QString jono = QString("x: %1 y: %2").arg(x1).arg(y1);
203 //QMessageBox::about(0,"Tark",jono);
205 scx1 = left + (x1-xmin)/(xmax-xmin)*(right-left);
206 scy1 = top + (ymax-y1)/(ymax-ymin)*(bottom-top);
207 scx2 = left + (x2-xmin)/(xmax-xmin)*(right-left);
208 scy2 = top + (ymax-y2)/(ymax-ymin)*(bottom-top);
210 // Show with circle if starting point
214 startx = scx1; starty = scy1;
215 painter.drawEllipse( scx1-5, scy1-5, 10, 10);
217 painter.drawLine( scx1, scy1, scx2, scy2);
219 // Show the endig point if different than the starting point
220 if (scx2 != startx || scy2 != starty)
222 painter.drawEllipse( scx2-5, scy2-5, 10, 10);
227 void RouteDialog::on_closePushButton_clicked()
232 bool RouteDialog::readRouteFromFile( QString &filename)
239 //file.setFileName("route.txt");
240 file.setFileName( filename);
241 if (!file.open(QIODevice::ReadOnly))
243 QMessageBox::about(0, "Error", "File not found");
249 //while( file.canReadLine())
252 QString str1, str2, str3;
253 rivi = file.readLine();
255 str1 = rivi.section(" ", 0, 0);
256 if (str1.compare("Start:") == 0 || str1.compare("Stop:") == 0)
262 //QMessageBox::about(0, "LUKEE", file.readLine());
263 str1 = rivi.section(" ", 2, 2); // latitude y-value
264 str2 = rivi.section(" ", 4, 4); // longitude x-value
265 str3 = rivi.section(" ", 6, 6); // altitude z-value
266 QString str = QString("la: %1 lo: %2 al: %3").arg(str1).arg(str2).arg(str3);
267 //QMessageBox::about(0, "LUKEE", str);
273 temp.setX( x); // Longitude
274 temp.setY( y);// Latitude
275 temp.setZ( z);// altitude
277 vertexList.append(temp);
287 /*vertexList.append(Vector(0.0, 0.0, 0.0));
288 vertexList.append(Vector(1.0, 1.0, 1.0));
289 vertexList.append(Vector(1.0, 1.0, 0.0));
291 vertexList.append(Vector(1.0, 0.0, 0.0));
292 vertexList.append(Vector(1.0, 0.0, 1.0));
293 vertexList.append(Vector(0.0, 1.0, 1.0));
295 vertexList.append(Vector(0.0, 1.0, 0.0));
296 vertexList.append(Vector(0.0, 0.0, 0.0));
297 vertexList.append(Vector(0.0, 0.0, 1.0));
305 /* connection[0] = 0;
306 connection[1] = 1; connection[2] = 5; connection[3] = 8; connection[4] = -4;
307 connection[5] = 5; connection[6] = 6; connection[7] = 7; connection[8] = -8;
308 connection[9] = 6; connection[10] = 2; connection[11] = 3; connection[12] = -7;
309 connection[13] = 1; connection[14] = 4; connection[15] = 3; connection[16] = -2;
310 connection[17] = 8; connection[18] = 7; connection[19] = 3; connection[20] = -4;
311 connection[21] = 6; connection[22] = 5; connection[23] = 1; connection[24] = -2;*/
313 /******** in 3d use only */
318 //angle = toradians(60);
320 view3d.setUp( 0.0, 0.0, 1.0);
321 view3d.setAngle(toradians(60));
322 setAtPoint( &view3d);
323 xmin = objxmin; xmax = objxmax; ymin = objymin; ymax = objymax;
324 setFromPoint( &view3d);
326 /****** end of 3d *****/
331 * Find out data range for x-, y- and z-coordinates
333 void dataMinMax( void)
339 temp = vertexList.at(0);
340 objxmax = objxmin = temp.getX();
341 objymax = objymin = temp.getY();
342 objzmax = objzmin = temp.getZ();
344 maxi = vertexList.size()-1; // Wrong last data
346 for (i=1; i<maxi; i++)
348 temp = vertexList.at(i);
357 else if (x > objxmax)
366 else if (y > objymax)
375 else if (z > objzmax)
380 //QString jono = QString("ojxmin %1 objxmax %2").arg(objxmin).arg(objxmax);
381 //QString jono = QString("ojymin %1 objymax %2").arg(objymin).arg(objymax);
382 //QString jono = QString("ojzmin %1 objzmax %2").arg(objzmin).arg(objzmax);
383 //QMessageBox::about(0,"Tark", jono);
387 void setAtPoint( Viewing *v)
393 x = (objxmax+objxmin)/2.0;
395 y= (objymax+objymin)/2.0;
397 z= (objzmax+objzmin)/2.0;
398 v->setAtPoint( x, y, z);
399 //QString jono = QString("AtX %1 Aty %2 AtZ %3").arg(atPoint.x()).arg(atPoint.y()).arg(atPoint.z());
400 //QString jono = QString("AtX %1 Aty %2 AtZ %3").arg(atPoint.x).arg(atPoint.y).arg(atPoint.z);
403 test = v->getAtPoint();
404 QString jono = QString("AtX %1 Aty %2 AtZ %3").arg(test.getX()).arg(test.getY()).arg(test.getZ());
405 QMessageBox::about(0,"At point", jono);
409 void setFromPoint( Viewing *v)
413 point = v->getAtPoint();
415 //fromPoint.setX( atPoint.getX() + (objxmax-objxmin)/2.0 + WIDTH*maxof((objzmax-objzmin)/2.0,(objymax-objymin)/2.0));
418 x = point.getX() + 300; //25;
422 z = point.getZ(); // + 150;
423 v->setFromPoint(x,y,z);
424 //QString jono = QString("FromX %1 FromY %2 FromZ %3").arg(fromPoint.x()).arg(fromPoint.y()).arg(fromPoint.z());
425 //QString jono = QString("FromX %1 FromY %2 FromZ %3").arg(fromPoint.x).arg(fromPoint.y).arg(fromPoint.z);
427 test = v->getFromPoint();
428 QString jono = QString("FromX %1 FromY %2 FromZ %3").arg(test.getX()).arg(test.getY()).arg(test.getZ());
429 QMessageBox::about(0,"From point", jono); // (1.9, 0.5, 0.5)
435 double amarkmag, tempmag;
438 dval = cos(angle/2.0)/sin(angle/2.0);
439 dist = atPoint-fromPoint;
440 amarkmag = dist.length();
443 temp.crossProduct( dist, up);
444 tempmag = temp.length();
447 temp.crossProduct( a1, a3);
448 tempmag = temp.length();
451 offsx = -a1.getX()*fromPoint.getX() - a1.getY()*fromPoint.getY() - a1.getZ()*fromPoint.getZ();
452 offsy = -a2.getX()*fromPoint.getX() - a2.getY()*fromPoint.getY() - a2.getZ()*fromPoint.getZ();
453 offsz = -a3.getX()*fromPoint.getX() - a3.getY()*fromPoint.getY() - a3.getZ()*fromPoint.getZ();
454 //QString jono2 = QString("offsx %1 offsy %2 offsz %3").arg(offsx).arg(offsy).arg(offsz);
455 //QMessageBox::about(0,"offs x y z", jono2);
459 #define LEFTEDGE 0x01
460 #define RIGHTEDGE 0x02
461 #define BOTTOMEDGE 0x04
464 * Returns a code specifying which edge in the viewing pyramid was crossed.
465 * There may be more than one.
467 int code( qreal x, qreal y, qreal z)
472 if (x<-z) c |= LEFTEDGE;
473 if (x>z) c |= RIGHTEDGE;
474 if (y<-z) c |= BOTTOMEDGE;
475 if (y>z) c |= TOPEDGE;
480 * Converts clipped world coordinates to screen coordinates.
482 void WORLDtoSCREEN( qreal xWorld, qreal yWorld, int *xScreen, int *yScreen)
484 *xScreen = (int) (a*xWorld+b);
485 *yScreen = (int) (c*yWorld+d);
489 * Clips the line segment in three-dimensional coordinates to the
492 void clip3d( qreal x1, qreal y1, qreal z1, qreal x2, qreal y2, qreal z2, int *xscreen1, int *yscreen1, int *xscreen2, int *yscreen2)
500 while (c1!= NOEDGE || c2 != NOEDGE)
502 if ((c1 & c2 ) != NOEDGE) return;
504 if (c == NOEDGE) c = c2;
505 if ((c&LEFTEDGE) == LEFTEDGE)
508 t = (z1+x1)/((x1-x2)-(z2-z1));
513 else if ((c&RIGHTEDGE) == RIGHTEDGE)
515 // Crosses right edge
516 t = (z1-x1)/((x2-x1)-(z2-z1));
521 else if ((c&BOTTOMEDGE) == BOTTOMEDGE)
523 // Crosses bottom edge
524 t = (z1+y1)/((y1-y2)-(z2-z1));
529 else if ((c&TOPEDGE) == TOPEDGE)
532 t = (z1-y1)/((y2-y1)-(z2-z1));
552 WORLDtoSCREEN(x1/z1,y1/z1,xscreen1, yscreen1);
553 WORLDtoSCREEN(x2/z2,y2/z2,xscreen2, yscreen2);
557 WORLDtoSCREEN(x1,y1,xscreen1, yscreen1);
558 WORLDtoSCREEN(x2,y2,xscreen2, yscreen2);
560 //line( xscreen1, yscreen1, xscreen2, yscreen2);
564 * Transform the segment connecting the two vectors into the viewing plane.
565 * clip3d() clips the line if needed.
567 void transformseg( Viewing *v, Vector *v1, Vector *v2, int *xscreen1, int *yscreen1, int *xscreen2, int *yscreen2 )
570 qreal x1, y1, z1, x2, y2, z2;
571 qreal offsx, offsy, offsz;
577 offsx = v->getOffsx();
578 offsy = v->getOffsy();
579 offsz = v->getOffsz();
582 x1 = (a1.getX()*v1->getX() + a1.getY()*v1->getY() + a1.getZ()*v1->getZ() + offsx)*dval;
583 y1 = (a2.getX()*v1->getX() + a2.getY()*v1->getY() + a2.getZ()*v1->getZ() + offsy)*dval;
584 z1 = a3.getX()*v1->getX() + a3.getY()*v1->getY() + a3.getZ()*v1->getZ() + offsz;
586 x2 = (a1.getX()*v2->getX() + a1.getY()*v2->getY() + a1.getZ()*v2->getZ() + offsx)*dval;
587 y2 = (a2.getX()*v2->getX() + a2.getY()*v2->getY() + a2.getZ()*v2->getZ() + offsy)*dval;
588 z2 = a3.getX()*v2->getX() + a3.getY()*v2->getY() + a3.getZ()*v2->getZ() + offsz;
590 clip3d(x1,y1,z1,x2,y2,z2, xscreen1, yscreen1, xscreen2, yscreen2 );
597 void RouteDialog::paintEvent(QPaintEvent *)
600 int xscreen1, yscreen1, xscreen2, yscreen2;
603 QPainter painter(this);
605 painter.setRenderHint(QPainter::Antialiasing, true);
606 painter.setPen(QPen((Qt::black),2));
607 painter.setBrush(QBrush((Qt::yellow), Qt::SolidPattern));
609 // Draw route window frsme
610 //painter.drawLine(left,top,right,top);
611 //painter.drawLine(right,top,right,bottom);
612 //painter.drawLine(left,top,left,bottom);
613 //painter.drawLine(left,bottom,right,bottom);
618 temp1 = vertexList.at(connection[i-1]);
619 temp2 = vertexList.at(connection[i]);
620 transformseg( &view3d, &temp1,&temp2, &xscreen1, &yscreen1, &xscreen2, &yscreen2);
622 painter.drawLine(xscreen1, yscreen1, xscreen2, yscreen2);
625 painter.drawEllipse( xscreen1-5, yscreen1-5, 10, 10);
635 while (connection[i] > 0)
637 //transformseg( &pa[connection[i-1]],&pa[connection[i]], &xpc1, &ypc1, &xpc2, &ypc2);
638 temp1 = vertexList.at(connection[i-1]);
639 temp2 = vertexList.at(connection[i]);
640 transformseg( &view3d, &temp1,&temp2, &xscreen1, &yscreen1, &xscreen2, &yscreen2);
642 painter.drawLine(xscreen1, yscreen1, xscreen2, yscreen2);
645 painter.drawEllipse( xscreen1-5, yscreen1-5, 10, 10);
650 //transformseg( &pa[connection[i-1]],&pa[-connection[i]],&xpc1, &ypc1, &xpc2, &ypc2);
651 temp1 = vertexList.at(connection[i-1]);
652 temp2 = vertexList.at(-connection[i]);
653 transformseg( &view3d, &temp1,&temp2, &xscreen1, &yscreen1, &xscreen2, &yscreen2);
654 painter.drawLine(xscreen1, yscreen1, xscreen2, yscreen2);
655 // from last segemt to start
656 //transformseg( &pa[-connection[i]],&pa[connection[startofside]],&xpc1, &ypc1, &xpc2, &ypc2);
657 temp1 = vertexList.at(-connection[i]);
658 temp2 = vertexList.at(connection[startofside]);
659 transformseg( &view3d, &temp1,&temp2, &xscreen1, &yscreen1, &xscreen2, &yscreen2);
660 painter.drawLine(xscreen1, yscreen1, xscreen2, yscreen2);