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>
16 #include <QFileDialog>
20 * In starting Qt 4.6 there is QVector3D.
21 * Later (updating Qt version) this class can be removed.
27 Vector() { x=0.; y=0. ; z=0.; };
28 Vector( qreal initX, qreal initY, qreal initZ) { x = initX, y = initY; z = initZ; };
29 void setX( qreal newX) { x = newX; };
30 void setY( qreal newY) { y = newY; };
31 void setZ( qreal newZ) { z = newZ; };
32 qreal getX() { return x; };
33 qreal getY() { return y; };
34 qreal getZ() { return z; };
35 qreal length() { return sqrt(x*x+y*y+z*z); };
36 Vector operator+(Vector v)
38 x = x + v.x; y = y + v.y; z = z + v.z;
41 Vector operator-(Vector v)
43 x = x - v.x; y = y - v.y; z = z - v.z;
46 Vector operator/(qreal c)
48 x = x/c; y = y/c; z = z/c;
51 Vector crossProduct( Vector a, Vector b)
53 x = a.y*b.z - a.z*b.y;
54 y = a.z*b.x - a.x*b.z;
55 z = a.x*b.y - a.y*b.x;
63 Vector atPoint, fromPoint, up, a1, a2, a3;
64 qreal offsx, offsy, offsz;
68 qreal getOffsx() { return offsx; };
69 qreal getOffsy() { return offsy; };
70 qreal getOffsz() { return offsz; };
71 qreal getDval() { return dval; };
72 void setAngle( qreal newA) { angle = newA; };
73 void setUp( qreal newUpX, qreal newUpY, qreal newUpZ)
75 up.setX(newUpX); up.setY(newUpY); up.setZ(newUpZ);
77 void setAtPoint( qreal newX, qreal newY, qreal newZ)
79 atPoint.setX(newX); atPoint.setY(newY); atPoint.setZ(newZ);
81 void setFromPoint(qreal newX, qreal newY, qreal newZ)
83 fromPoint.setX(newX); fromPoint.setY(newY); fromPoint.setZ(newZ);
87 double amarkmag, tempmag;
90 dval = cos(angle/2.0)/sin(angle/2.0);
91 dist = atPoint-fromPoint;
92 amarkmag = dist.length();
95 temp.crossProduct( dist, up);
96 tempmag = temp.length();
99 temp.crossProduct( a1, a3);
100 tempmag = temp.length();
103 offsx = -a1.getX()*fromPoint.getX() - a1.getY()*fromPoint.getY() - a1.getZ()*fromPoint.getZ();
104 offsy = -a2.getX()*fromPoint.getX() - a2.getY()*fromPoint.getY() - a2.getZ()*fromPoint.getZ();
105 offsz = -a3.getX()*fromPoint.getX() - a3.getY()*fromPoint.getY() - a3.getZ()*fromPoint.getZ();
106 //QString jono2 = QString("offsx %1 offsy %2 offsz %3").arg(offsx).arg(offsy).arg(offsz);
107 //QMessageBox::about(0,"offs x y z", jono2);
109 Vector getAtPoint() { return atPoint; };
110 Vector getFromPoint() { return fromPoint; };
111 Vector getA1() { return a1; };
112 Vector getA2() { return a2; };
113 Vector getA3() { return a3; };
117 qreal xmax, xmin, ymin, ymax; // Limits in world coordinates
119 QList<Vector> vertexList; // Vertecies of route
121 qreal objxmin, objxmax, objymin, objymax, objzmin, objzmax; // data ranges
123 #define maxof(val1,val2) ((val1>val2)?val1:val2)
124 #define toradians( degrees) (degrees*0.017453293)
126 #define WIDTH 1.8 // For 3d viewing only
127 qreal a, b,c,d; // Used for 3d viewing to calculate screen coordinates
129 Viewing view3d; // Viewing settings for 3d
131 // Function prototypes
132 void dataMinMax( void);
133 void setAtPoint( Viewing *v);
134 void setFromPoint( Viewing *v);
135 void transformseg( Viewing *v, Vector *v1, Vector *v2, int *xscreen1, int *yscreen1, int *xscreen2, int *yscreen2 );
137 #define R 6378.140 // The radius of the earth by kilometers
139 * count distance of two points (defined by longitude & latitude)
140 * on the surface of the earth.
142 qreal countDistance(Vector *p1, Vector *p2)
144 qreal dLon, dLat; // delta of longitude & latitude
147 dLon = p2->getX() - p1->getX(); // longitude difference
148 dLat = p2->getY() - p1->getY(); // latitude difference
149 if (dLon <0) dLon = -dLon;
150 if (dLat <0) dLat = -dLat;
152 dLon = dLon*3.14/180;
153 dLat = dLat*3.14/180;
154 a = (sin(dLat/2.))*(sin(dLat/2.)) +
155 (cos(p1->getY())*3.14/180)*(cos(p2->getY())*3.14/180)*(sin(dLon/2.))*(sin(dLon/2.));
156 c = 2.*atan(sqrt(a)/sqrt(1-a)); // c is angle between points p1 & p2 with circel by radius 1.
158 return R*c; // Return distance in kilometers
161 RouteDialog::RouteDialog(QWidget *parent) :
163 ui(new Ui::RouteDialog)
166 left = 5; top = 5; right = 395; bottom = 195; // Limits in screen coordinates
169 RouteDialog::~RouteDialog()
174 void RouteDialog::changeEvent(QEvent *e)
176 QDialog::changeEvent(e);
178 case QEvent::LanguageChange:
179 ui->retranslateUi(this);
188 * Draws route to the route dialog.
189 * Type 0 is 2d viewing and type 1 is for 3d viewing
193 void RouteDialog::paintEvent(QPaintEvent *)
195 int type = 0; // 0 for 2d, 1 for 3d
196 int startx, starty; // Starting point of the route
198 qreal x1, y1, x2, y2;
199 int x1Screen, y1Screen, x2Screen, y2Screen;
201 QPainter painter(this);
203 painter.setRenderHint(QPainter::Antialiasing, true);
204 painter.setPen(QPen((Qt::black),2));
205 painter.setBrush(QBrush((Qt::yellow), Qt::SolidPattern));
207 // Draw route window frame
208 /*painter.drawLine(left,top,right,top);
209 painter.drawLine(right,top,right,bottom);
210 painter.drawLine(left,top,left,bottom);
211 painter.drawLine(left,bottom,right,bottom);
213 maxi = vertexList.size();
215 for (i=0; i<maxi-1; i++)
217 v1 = vertexList.at(i);
218 v2 = vertexList.at(i+1);
222 x1 = v1.getX(); y1 = v1.getY();
223 x2 = v2.getX(); y2 = v2.getY();
224 //QString jono = QString("x: %1 y: %2").arg(x1).arg(y1);
225 //QMessageBox::about(0,"Tark",jono);
227 x1Screen = left + (x1-xmin)/(xmax-xmin)*(right-left);
228 y1Screen = top + (ymax-y1)/(ymax-ymin)*(bottom-top);
229 x2Screen = left + (x2-xmin)/(xmax-xmin)*(right-left);
230 y2Screen = top + (ymax-y2)/(ymax-ymin)*(bottom-top);
234 transformseg( &view3d, &v1,&v2, &x1Screen, &y1Screen, &x2Screen, &y2Screen);
237 // Show with circle if starting point
241 startx = x1Screen; starty = y1Screen;
242 painter.drawEllipse( x1Screen-5, y1Screen-5, 10, 10);
244 painter.drawLine( x1Screen, y1Screen, x2Screen, y2Screen);
246 // Show the endig point if different than the starting point
247 if (x2Screen != startx || y2Screen != starty)
249 painter.drawEllipse( x2Screen-5, y2Screen-5, 10, 10);
253 void RouteDialog::on_closePushButton_clicked()
259 * Read route coordinates (latitude, longitude, altitude) from file.
261 bool RouteDialog::readRouteFromFile( QString &filename)
267 QString fileName = QFileDialog::getOpenFileName(this,
268 tr("Read Route"), "./", tr("Route Files (*.txt)"));
270 file.setFileName( fileName);
271 if (!file.open(QIODevice::ReadOnly))
273 QMessageBox::about(0, "Error", "File not found");
280 QString str1, str2, str3;
281 rivi = file.readLine();
283 str1 = rivi.section(" ", 0, 0);
284 if (str1.compare("Start:") != 0 && str1.compare("Stop:") != 0)
286 str1 = rivi.section(" ", 2, 2); // latitude y-value
287 str2 = rivi.section(" ", 4, 4); // longitude x-value
288 str3 = rivi.section(" ", 6, 6); // altitude z-value
289 //QString str = QString("la: %1 lo: %2 al: %3").arg(str1).arg(str2).arg(str3);
290 //QMessageBox::about(0, "LUKEE", str);
292 if (str1.length() > 0)
298 temp.setX( x); // Longitude
299 temp.setY( y); // Latitude
300 temp.setZ( z); // altitude
302 vertexList.append(temp);
309 /******** in 3d use only */
314 //angle = toradians(60);
316 view3d.setUp( 1.0, 0.0, 0.0);
317 view3d.setAngle(toradians(60));
318 setAtPoint( &view3d);
319 xmin = objxmin; xmax = objxmax; ymin = objymin; ymax = objymax; // 2d viewing needs this !!!!
320 setFromPoint( &view3d);
322 /****** end of 3d *****/
325 //Testing distance counting
328 //a1.setX( xmin); a1.setY( ymin);
329 //a2.setX( xmax); a2.setY( ymax);
330 a1.setX( 25.483); a1.setY( 65.017); // Oulu
331 a2.setX( 27.767); a2.setY( 64.283); // Kajaani
332 dist = countDistance( &a1, &a2);
333 QString str = QString("Min & Max datan välimatka %1").arg(dist);
334 QMessageBox::about( 0, "Testi", str);
341 * Find out data range for x-, y- and z-coordinates
343 void dataMinMax( void)
349 temp = vertexList.at(0);
350 objxmax = objxmin = temp.getX();
351 objymax = objymin = temp.getY();
352 objzmax = objzmin = temp.getZ();
354 maxi = vertexList.size();
355 for (i=1; i<maxi; i++)
357 temp = vertexList.at(i);
366 else if (x > objxmax)
375 else if (y > objymax)
384 else if (z > objzmax)
389 //QString jono = QString("ojxmin %1 objxmax %2").arg(objxmin).arg(objxmax);
390 //QString jono = QString("ojymin %1 objymax %2").arg(objymin).arg(objymax);
391 //QString jono = QString("ojzmin %1 objzmax %2").arg(objzmin).arg(objzmax);
392 //QMessageBox::about(0,"Tark", jono);
396 * Setting the point where the viewed object is. In the middle of datapoints.
398 void setAtPoint( Viewing *v)
404 x = (objxmax+objxmin)/2.0;
405 y= (objymax+objymin)/2.0;
406 z= (objzmax+objzmin)/2.0;
408 v->setAtPoint( x, y, z);
409 //QString jono = QString("AtX %1 Aty %2 AtZ %3").arg(atPoint.x()).arg(atPoint.y()).arg(atPoint.z());
410 //QString jono = QString("AtX %1 Aty %2 AtZ %3").arg(atPoint.x).arg(atPoint.y).arg(atPoint.z);
413 test = v->getAtPoint();
414 QString jono = QString("AtX %1 Aty %2 AtZ %3").arg(test.getX()).arg(test.getY()).arg(test.getZ());
415 QMessageBox::about(0,"At point", jono);
420 * Setting the point where the object is viewed by eye.
422 void setFromPoint( Viewing *v)
426 point = v->getAtPoint();
428 //fromPoint.setX( atPoint.getX() + (objxmax-objxmin)/2.0 + WIDTH*maxof((objzmax-objzmin)/2.0,(objymax-objymin)/2.0));
430 //x = point.getX() + (objxmax-objxmin)/2.0 + WIDTH*maxof((objzmax-objzmin)/2.0,(objymax-objymin)/2.0);
433 y = point.getY() + 40; // + (objymax-objymin)/2.0 + WIDTH*maxof((objzmax-objzmin)/2.0,(objxmax-objxmin)/2.0);
436 v->setFromPoint(x,y,z);
437 //QString jono = QString("FromX %1 FromY %2 FromZ %3").arg(fromPoint.x()).arg(fromPoint.y()).arg(fromPoint.z());
438 //QString jono = QString("FromX %1 FromY %2 FromZ %3").arg(fromPoint.x).arg(fromPoint.y).arg(fromPoint.z);
440 test = v->getFromPoint();
441 QString jono = QString("FromX %1 FromY %2 FromZ %3").arg(test.getX()).arg(test.getY()).arg(test.getZ());
442 QMessageBox::about(0,"From point", jono); // (1.9, 0.5, 0.5)
448 #define LEFTEDGE 0x01
449 #define RIGHTEDGE 0x02
450 #define BOTTOMEDGE 0x04
453 * Returns a code specifying which edge in the viewing pyramid was crossed.
454 * There may be more than one.
456 int code( qreal x, qreal y, qreal z)
461 if (x<-z) c |= LEFTEDGE;
462 if (x>z) c |= RIGHTEDGE;
463 if (y<-z) c |= BOTTOMEDGE;
464 if (y>z) c |= TOPEDGE;
470 * Converts clipped world coordinates to screen coordinates.
472 void WORLDtoSCREEN( qreal xWorld, qreal yWorld, int *xScreen, int *yScreen)
474 *xScreen = (int) (a*xWorld+b);
475 *yScreen = (int) (c*yWorld+d);
479 * Clips the line segment in three-dimensional coordinates to the
482 void clip3d( qreal x1, qreal y1, qreal z1, qreal x2, qreal y2, qreal z2, int *xscreen1, int *yscreen1, int *xscreen2, int *yscreen2)
490 while (c1!= NOEDGE || c2 != NOEDGE)
492 if ((c1 & c2 ) != NOEDGE) return;
494 if (c == NOEDGE) c = c2;
495 if ((c&LEFTEDGE) == LEFTEDGE)
498 t = (z1+x1)/((x1-x2)-(z2-z1));
503 else if ((c&RIGHTEDGE) == RIGHTEDGE)
505 // Crosses right edge
506 t = (z1-x1)/((x2-x1)-(z2-z1));
511 else if ((c&BOTTOMEDGE) == BOTTOMEDGE)
513 // Crosses bottom edge
514 t = (z1+y1)/((y1-y2)-(z2-z1));
519 else if ((c&TOPEDGE) == TOPEDGE)
522 t = (z1-y1)/((y2-y1)-(z2-z1));
542 WORLDtoSCREEN(x1/z1,y1/z1,xscreen1, yscreen1);
543 WORLDtoSCREEN(x2/z2,y2/z2,xscreen2, yscreen2);
547 WORLDtoSCREEN(x1,y1,xscreen1, yscreen1);
548 WORLDtoSCREEN(x2,y2,xscreen2, yscreen2);
550 //Now ready to draw line( xscreen1, yscreen1, xscreen2, yscreen2);
554 * Transform the segment connecting the two vectors into the viewing plane.
555 * clip3d() clips the line if needed.
557 void transformseg( Viewing *v, Vector *v1, Vector *v2, int *xscreen1, int *yscreen1, int *xscreen2, int *yscreen2)
560 qreal x1, y1, z1, x2, y2, z2;
567 x1 = (a1.getX()*v1->getX() + a1.getY()*v1->getY() + a1.getZ()*v1->getZ() + v->getOffsx())*v->getDval();
568 y1 = (a2.getX()*v1->getX() + a2.getY()*v1->getY() + a2.getZ()*v1->getZ() + v->getOffsy())*v->getDval();
569 z1 = a3.getX()*v1->getX() + a3.getY()*v1->getY() + a3.getZ()*v1->getZ() + v->getOffsz();
571 x2 = (a1.getX()*v2->getX() + a1.getY()*v2->getY() + a1.getZ()*v2->getZ() + v->getOffsx())*v->getDval();
572 y2 = (a2.getX()*v2->getX() + a2.getY()*v2->getY() + a2.getZ()*v2->getZ() + v->getOffsy())*v->getDval();
573 z2 = a3.getX()*v2->getX() + a3.getY()*v2->getY() + a3.getZ()*v2->getZ() + v->getOffsz();
575 clip3d(x1,y1,z1,x2,y2,z2, xscreen1, yscreen1, xscreen2, yscreen2 );