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>
23 Vector() { x=0.; y=0. ; z=0.; };
24 Vector( qreal initX, qreal initY, qreal initZ) { x = initX, y = initY; z = initZ; };
25 void setX( qreal newX) { x = newX; };
26 void setY( qreal newY) { y = newY; };
27 void setZ( qreal newZ) { z = newZ; };
28 qreal getX() { return x; };
29 qreal getY() { return y; };
30 qreal getZ() { return z; };
31 qreal length() { return sqrt(x*x+y*y+z*z); };
32 Vector operator+(Vector v)
34 x = x + v.x; y = y + v.y; z = z + v.z;
37 Vector operator-(Vector v)
39 x = x - v.x; y = y - v.y; z = z - v.z;
42 Vector operator/(qreal c)
44 x = x/c; y = y/c; z = z/c;
47 Vector crossProduct( Vector a, Vector b)
49 x = a.y*b.z - a.z*b.y;
50 y = a.z*b.x - a.x*b.z;
51 z = a.x*b.y - a.y*b.x;
56 int left, top, right, bottom; // Limits in screen coordinates
57 qreal xmax, xmin, ymin, ymax; // Limits in world coordinates
59 QList<Vector> vertexList; // Vertecies of route
61 Vector atPoint, fromPoint, up, a1, a2, a3;
62 qreal offsx, offsy, offsz;
64 qreal objxmin, objxmax, objymin, objymax, objzmin, objzmax;
68 #define maxof(val1,val2) ((val1>val2)?val1:val2)
69 #define toradians( degrees) (degrees*0.017453293)
80 RouteDialog::RouteDialog(QWidget *parent) :
82 ui(new Ui::RouteDialog)
85 //left = 50; top = 50; right = 350; bottom = 200;
86 left = 5; top = 5; right = 395; bottom = 295;
87 xmin =0.0; xmax=100.0; ymin = 0.0; ymax = 20.0;
88 // Ruudun koko 400 x 300
90 /*vertexList.append(QVector3D(40.02, 10.02, 10.02));
91 vertexList.append(QVector3D(50.01, 5.01, 10));
92 vertexList.append(QVector3D(69.98, 4.98, 10));
93 vertexList.append(QVector3D(80.02, 9.98, 10));
94 vertexList.append(QVector3D(70.01, 15.01, 10));
95 vertexList.append(QVector3D(49.99, 14.97, 10));
96 vertexList.append(QVector3D(40.01, 10.01, 10.02));
98 vertexList.append(Vector(0.0, 0.0, 0.0));
99 vertexList.append(Vector(1.0, 1.0, 1.0));
100 vertexList.append(Vector(1.0, 1.0, 0.0));
102 vertexList.append(Vector(1.0, 0.0, 0.0));
103 vertexList.append(Vector(1.0, 0.0, 1.0));
104 vertexList.append(Vector(0.0, 1.0, 1.0));
106 vertexList.append(Vector(0.0, 1.0, 0.0));
107 vertexList.append(Vector(0.0, 0.0, 0.0));
108 vertexList.append(Vector(0.0, 0.0, 1.0));
111 connection[1] = 1; connection[2] = 5; connection[3] = 8; connection[4] = -4;
112 connection[5] = 5; connection[6] = 6; connection[7] = 7; connection[8] = -8;
113 connection[9] = 6; connection[10] = 2; connection[11] = 3; connection[12] = -7;
114 connection[13] = 1; connection[14] = 4; connection[15] = 3; connection[16] = -2;
115 connection[17] = 8; connection[18] = 7; connection[19] = 3; connection[20] = -4;
116 connection[21] = 6; connection[22] = 5; connection[23] = 1; connection[24] = -2;
118 fromPoint.setX( 1.0); fromPoint.setY( 0.0); fromPoint.setZ( 0.0);
119 atPoint.setX( 0.0); atPoint.setY( 0.0); atPoint.setZ( 0.0);
120 up.setX( 0.0); up.setY( 0.0); up.setZ(1.0);
126 angle = toradians(60);
133 RouteDialog::~RouteDialog()
138 void RouteDialog::changeEvent(QEvent *e)
140 QDialog::changeEvent(e);
142 case QEvent::LanguageChange:
143 ui->retranslateUi(this);
152 * Draws route to the route dialog
155 void RouteDialog::paintEvent(QPaintEvent *)
159 qreal x1, y1, x2, y2;
160 int scx1, scy1, scx2, scy2;
162 QPainter painter(this);
164 painter.setRenderHint(QPainter::Antialiasing, true);
165 painter.setPen(QPen((Qt::black),2));
166 painter.setBrush(QBrush((Qt::yellow), Qt::SolidPattern));
168 // Draw route window frsme
169 painter.drawLine(left,top,right,top);
170 painter.drawLine(right,top,right,bottom);
171 painter.drawLine(left,top,left,bottom);
172 painter.drawLine(left,bottom,right,bottom);
174 //maxi = vertexList.size();
175 for (i=0; i<maxi-1; i++)
177 x1 = vertexList.at(i).x();
178 y1 = vertexList.at(i).y();
179 x2 = vertexList.at(i+1).x();
180 y2 = vertexList.at(i+1).y();
182 scx1 = left + (x1-xmin)/(xmax-xmin)*(right-left);
183 scy1 = top + (ymax-y1)/(ymax-ymin)*(bottom-top);
184 scx2 = left + (x2-xmin)/(xmax-xmin)*(right-left);
185 scy2 = top + (ymax-y2)/(ymax-ymin)*(bottom-top);
187 painter.drawLine( scx1, scy1, scx2, scy2);
192 void RouteDialog::on_closePushButton_clicked()
197 // Next functions below for 3D
198 void dataMinMax( void)
204 temp = vertexList.at(0);
205 objxmax = objxmin = temp.getX();
206 objymax = objymin = temp.getY();
207 objzmax = objzmin = temp.getZ();
209 maxi = vertexList.size();
211 for (i=1; i<maxi; i++)
213 temp = vertexList.at(i);
222 else if (x > objxmax)
231 else if (y > objymax)
240 else if (z > objzmax)
245 //QString jono = QString("ojxmin %1 objxmax %2").arg(objxmin).arg(objxmax);
246 //QString jono = QString("ojymin %1 objymax %2").arg(objymin).arg(objymax);
247 //QString jono = QString("ojzmin %1 objzmax %2").arg(objzmin).arg(objzmax);
248 //QMessageBox::about(0,"Tark", jono);
254 atPoint.setX((objxmax+objxmin)/2.0);
255 atPoint.setY((objymax+objymin)/2.0);
256 atPoint.setZ((objzmax+objzmin)/2.0);
257 //QString jono = QString("AtX %1 Aty %2 AtZ %3").arg(atPoint.x()).arg(atPoint.y()).arg(atPoint.z());
258 //QString jono = QString("AtX %1 Aty %2 AtZ %3").arg(atPoint.x).arg(atPoint.y).arg(atPoint.z);
259 //QMessageBox::about(0,"At point", jono);
264 //fromPoint.setX( atPoint.getX() + (objxmax-objxmin)/2.0 + WIDTH*maxof((objzmax-objzmin)/2.0,(objymax-objymin)/2.0));
266 fromPoint.setY( atPoint.getY());
267 fromPoint.setZ( atPoint.getZ());
268 //QString jono = QString("FromX %1 FromY %2 FromZ %3").arg(fromPoint.x()).arg(fromPoint.y()).arg(fromPoint.z());
269 //QString jono = QString("FromX %1 FromY %2 FromZ %3").arg(fromPoint.x).arg(fromPoint.y).arg(fromPoint.z);
270 //QMessageBox::about(0,"From point", jono); // (1.9, 0.5, 0.5)
275 double amarkmag, tempmag;
278 dval = cos(angle/2.0)/sin(angle/2.0);
279 dist = atPoint-fromPoint;
280 amarkmag = dist.length();
283 temp.crossProduct( dist, up);
284 tempmag = temp.length();
287 temp.crossProduct( a1, a3);
288 tempmag = temp.length();
291 offsx = -a1.getX()*fromPoint.getX() - a1.getY()*fromPoint.getY() - a1.getZ()*fromPoint.getZ();
292 offsy = -a2.getX()*fromPoint.getX() - a2.getY()*fromPoint.getY() - a2.getZ()*fromPoint.getZ();
293 offsz = -a3.getX()*fromPoint.getX() - a3.getY()*fromPoint.getY() - a3.getZ()*fromPoint.getZ();
294 //QString jono2 = QString("offsx %1 offsy %2 offsz %3").arg(offsx).arg(offsy).arg(offsz);
295 //QMessageBox::about(0,"offs x y z", jono2);
299 #define LEFTEDGE 0x01
300 #define RIGHTEDGE 0x02
301 #define BOTTOMEDGE 0x04
304 * Returns a code specifying which edge in the viewing pyramid was crossed.
305 * There may be more than one.
307 int code( qreal x, qreal y, qreal z)
312 if (x<-z) c |= LEFTEDGE;
313 if (x>z) c |= RIGHTEDGE;
314 if (y<-z) c |= BOTTOMEDGE;
315 if (y>z) c |= TOPEDGE;
320 * Converts clipped world coordinates to screen coordinates.
322 void WORLDtoSCREEN( qreal xWorld, qreal yWorld, int *xScreen, int *yScreen)
324 *xScreen = (int) (a*xWorld+b);
325 *yScreen = (int) (c*yWorld+d);
329 * Clips the line segment in three-dimensional coordinates to the
332 void clip3d( qreal x1, qreal y1, qreal z1, qreal x2, qreal y2, qreal z2, int *xscreen1, int *yscreen1, int *xscreen2, int *yscreen2)
340 while (c1!= NOEDGE || c2 != NOEDGE)
342 if ((c1 & c2 ) != NOEDGE) return;
344 if (c == NOEDGE) c = c2;
345 if ((c&LEFTEDGE) == LEFTEDGE)
348 t = (z1+x1)/((x1-x2)-(z2-z1));
353 else if ((c&RIGHTEDGE) == RIGHTEDGE)
355 // Crosses right edge
356 t = (z1-x1)/((x2-x1)-(z2-z1));
361 else if ((c&BOTTOMEDGE) == BOTTOMEDGE)
363 // Crosses bottom edge
364 t = (z1+y1)/((y1-y2)-(z2-z1));
369 else if ((c&TOPEDGE) == TOPEDGE)
372 t = (z1-y1)/((y2-y1)-(z2-z1));
392 WORLDtoSCREEN(x1/z1,y1/z1,xscreen1, yscreen1);
393 WORLDtoSCREEN(x2/z2,y2/z2,xscreen2, yscreen2);
397 WORLDtoSCREEN(x1,y1,xscreen1, yscreen1);
398 WORLDtoSCREEN(x2,y2,xscreen2, yscreen2);
400 //line( xscreen1, yscreen1, xscreen2, yscreen2);
404 * Transform the segment connecting the two vectors into the viewing plane.
405 * clip3d() clips the line if needed.
407 void transformseg( Vector *v1, Vector *v2, int *xscreen1, int *yscreen1, int *xscreen2, int *yscreen2 )
410 qreal x1, y1, z1, x2, y2, z2;
412 x1 = (a1.getX()*v1->getX() + a1.getY()*v1->getY() + a1.getZ()*v1->getZ() + offsx)*dval;
413 y1 = (a2.getX()*v1->getX() + a2.getY()*v1->getY() + a2.getZ()*v1->getZ() + offsy)*dval;
414 z1 = a3.getX()*v1->getX() + a3.getY()*v1->getY() + a3.getZ()*v1->getZ() + offsz;
416 x2 = (a1.getX()*v2->getX() + a1.getY()*v2->getY() + a1.getZ()*v2->getZ() + offsx)*dval;
417 y2 = (a2.getX()*v2->getX() + a2.getY()*v2->getY() + a2.getZ()*v2->getZ() + offsy)*dval;
418 z2 = a3.getX()*v2->getX() + a3.getY()*v2->getY() + a3.getZ()*v2->getZ() + offsz;
420 clip3d(x1,y1,z1,x2,y2,z2, xscreen1, yscreen1, xscreen2, yscreen2 );
423 void RouteDialog::paintEvent(QPaintEvent *)
426 qreal x1, y1, x2, y2;
427 int scx1, scy1, scx2, scy2;
428 int xpc1, ypc1, xpc2, ypc2;
431 QPainter painter(this);
433 painter.setRenderHint(QPainter::Antialiasing, true);
434 painter.setPen(QPen((Qt::black),2));
435 painter.setBrush(QBrush((Qt::yellow), Qt::SolidPattern));
437 // Draw route window frsme
438 //painter.drawLine(left,top,right,top);
439 //painter.drawLine(right,top,right,bottom);
440 //painter.drawLine(left,top,left,bottom);
441 //painter.drawLine(left,bottom,right,bottom);
448 while (connection[i] > 0)
450 //transformseg( &pa[connection[i-1]],&pa[connection[i]], &xpc1, &ypc1, &xpc2, &ypc2);
451 temp1 = vertexList.at(connection[i-1]);
452 temp2 = vertexList.at(connection[i]);
453 transformseg( &temp1,&temp2, &xpc1, &ypc1, &xpc2, &ypc2);
455 painter.drawLine(xpc1, ypc1, xpc2, ypc2);
459 //transformseg( &pa[connection[i-1]],&pa[-connection[i]],&xpc1, &ypc1, &xpc2, &ypc2);
460 temp1 = vertexList.at(connection[i-1]);
461 temp2 = vertexList.at(-connection[i]);
462 transformseg( &temp1,&temp2, &xpc1, &ypc1, &xpc2, &ypc2);
463 painter.drawLine(xpc1, ypc1, xpc2, ypc2);
464 // from last segemt to start
465 //transformseg( &pa[-connection[i]],&pa[connection[startofside]],&xpc1, &ypc1, &xpc2, &ypc2);
466 temp1 = vertexList.at(-connection[i]);
467 temp2 = vertexList.at(connection[startofside]);
468 transformseg( &temp1,&temp2, &xpc1, &ypc1, &xpc2, &ypc2);
469 painter.drawLine(xpc1, ypc1, xpc2, ypc2);