]> git.tdb.fi Git - r2c2.git/blob - source/designer/designer.cpp
Add Track::get_endpoint_position to avoid duplicating calculations
[r2c2.git] / source / designer / designer.cpp
1 #include <signal.h>
2 #include <cmath>
3 #include <iostream>
4 #include <GL/gl.h>
5 #include <SDL.h>
6 #include <msp/gl/rendermode.h>
7 #include <msp/gl/select.h>
8 #include <msp/strings/codec.h>
9 #include <msp/strings/lexicalcast.h>
10 #include <msp/strings/utf8.h>
11 #include <msp/strings/utils.h>
12 #include <msp/time/units.h>
13 #include <msp/time/utils.h>
14 #include "designer.h"
15 #include "input.h"
16 #include "manipulator.h"
17 #include "measure.h"
18 #include "selection.h"
19
20 using namespace std;
21 using namespace Marklin;
22 using namespace Msp;
23
24 Designer::Designer(int argc, char **argv):
25         screen_w(1280),
26         screen_h(960),
27         input(0),
28         mode(SELECT),
29         cam_yaw(M_PI/2),
30         cam_pitch(-M_PI/4),
31         cam_pos(0, -0.5, 0.5),
32         shift(false),
33         move_x(0),
34         move_y(0),
35         zoom(0),
36         rotate(0),
37         pitch(0)
38 {
39         catalogue.load("tracks.dat");
40
41         cat_layout=new Layout(catalogue);
42         cat_layout_3d=new Layout3D(*cat_layout);
43
44         const Catalogue::TrackMap &ctracks=catalogue.get_tracks();
45         unsigned n=0;
46         for(Catalogue::TrackMap::const_iterator i=ctracks.begin(); i!=ctracks.end(); ++i, ++n)
47         {
48                 Track *track=i->second->copy();
49                 track->set_position(Point((n%11)*0.1-0.5, 0.2-n/11*0.3, 0));
50                 track->set_rotation(M_PI/2);
51                 cat_layout->add_track(track);
52         }
53
54         manipulator=new Manipulator(*this);
55         manipulator->signal_status.connect(sigc::mem_fun(this, &Designer::manipulation_status));
56         manipulator->signal_done.connect(sigc::mem_fun(this, &Designer::manipulation_done));
57
58         layout=new Layout(catalogue);
59         layout_3d=new Layout3D(*layout);
60
61         if(argc>1)
62         {
63                 layout->load(argv[1]);
64                 const Track3DSeq &ltracks=layout_3d->get_tracks();
65                 for(Track3DSeq::const_iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
66                 {
67                         if((*i)->get_track().get_sensor_id())
68                                 (*i)->set_color(Color(1, 1, 0.5));
69                         else if((*i)->get_track().get_turnout_id())
70                                 (*i)->set_color(Color(0.5, 1, 1));
71                         else if((*i)->get_track().get_flex())
72                                 (*i)->set_color(Color(1, 0.5, 1));
73                 }
74         }
75
76         selection=new Selection;
77         manipulator->set_selection(selection);
78
79         measure=new Measure(*this);
80         measure->signal_changed.connect(sigc::mem_fun(this, &Designer::measure_changed));
81         measure->signal_done.connect(sigc::mem_fun(this, &Designer::measure_done));
82 }
83
84 int Designer::main()
85 {
86         setenv("__GL_SYNC_TO_VBLANK", "1", 0);
87         SDL_Init(SDL_INIT_VIDEO);
88         SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
89         SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4);
90         SDL_SetVideoMode(screen_w, screen_h, 32, SDL_OPENGL);
91         SDL_EnableUNICODE(1);
92
93         glEnableClientState(GL_VERTEX_ARRAY);
94         glEnable(GL_DEPTH_TEST);
95         glEnable(GL_BLEND);
96         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
97         glEnable(GL_CULL_FACE);
98
99         font=new GL::Font();
100         Parser::load(*font, "dejavu-20.font");
101
102         mode=SELECT;
103
104         Application::main();
105
106         delete font;
107         delete input;
108
109         SDL_Quit();
110
111         return exit_code;
112 }
113
114 void Designer::map_pointer_coords(int x, int y, float &gx, float &gy)
115 {
116         float cos_pitch=cos(cam_pitch);
117         float sin_pitch=sin(cam_pitch);
118         float cos_yaw=cos(cam_yaw);
119         float sin_yaw=sin(cam_yaw);
120
121         float rx=sin_yaw*0.55228;
122         float ry=-cos_yaw*0.55228;
123
124         float ux=cos_yaw*-sin_pitch*0.41421;
125         float uy=sin_yaw*-sin_pitch*0.41421;
126         float uz=cos_pitch*0.41421;
127
128         float xf=(float)x*2/screen_w-1;
129         float yf=1-(float)y*2/screen_h;
130
131         float vx=cos_yaw*cos_pitch + xf*rx + yf*ux;
132         float vy=sin_yaw*cos_pitch + xf*ry + yf*uy;
133         float vz=sin_pitch + yf*uz;
134
135         gx=cam_pos.x-vx*cam_pos.z/vz;
136         gy=cam_pos.y-vy*cam_pos.z/vz;
137 }
138
139 void Designer::tick()
140 {
141         SDL_Event event;
142         while(SDL_PollEvent(&event))
143         {
144                 float gx,gy;
145                 switch(event.type)
146                 {
147                 case SDL_MOUSEBUTTONDOWN:
148                         map_pointer_coords(event.button.x, event.button.y, gx, gy);
149                         button_press(event.button.x, screen_h-1-event.button.y, gx, gy, event.button.button);
150                         break;
151                 case SDL_MOUSEMOTION:
152                         map_pointer_coords(event.motion.x, event.motion.y, gx, gy);
153                         pointer_motion(event.motion.x, screen_h-1-event.motion.y, gx, gy);
154                         break;
155                 case SDL_KEYDOWN:
156                         key_press(event.key.keysym.sym, event.key.keysym.mod, event.key.keysym.unicode);
157                         break;
158                 case SDL_KEYUP:
159                         key_release(event.key.keysym.sym, event.key.keysym.mod);
160                         break;
161                 case SDL_QUIT:
162                         exit(0);
163                         break;
164                 }
165         }
166
167         const Time::TimeStamp t=Time::now();
168         float dt=(t-last_tick)/Time::sec;
169         last_tick=t;
170
171         if(move_y)
172         {
173                 cam_pos.x+=cos(cam_yaw)*dt*move_y;
174                 cam_pos.y+=sin(cam_yaw)*dt*move_y;
175         }
176         if(move_x)
177         {
178                 cam_pos.x+=sin(cam_yaw)*dt*move_x;
179                 cam_pos.y+=-cos(cam_yaw)*dt*move_x;
180         }
181         if(zoom)
182         {
183                 cam_pos.x+=cos(cam_yaw)*cos(cam_pitch)*dt*zoom;
184                 cam_pos.y+=sin(cam_yaw)*cos(cam_pitch)*dt*zoom;
185                 cam_pos.z+=sin(cam_pitch)*dt*zoom;
186         }
187         if(rotate)
188         {
189                 float vx=cos(cam_yaw)*cos(cam_pitch);
190                 float vy=sin(cam_yaw)*cos(cam_pitch);
191                 float vz=sin(cam_pitch);
192
193                 float gx=cam_pos.x-vx*cam_pos.z/vz;
194                 float gy=cam_pos.y-vy*cam_pos.z/vz;
195                 float d=sqrt(vx*vx+vy*vy)*cam_pos.z/vz;
196
197                 cam_yaw+=M_PI*dt*rotate;
198                 if(cam_yaw>M_PI*2)
199                         cam_yaw-=M_PI*2;
200                 else if(cam_yaw<0)
201                         cam_yaw+=M_PI*2;
202
203                 cam_pos.x=gx+cos(cam_yaw)*d;
204                 cam_pos.y=gy+sin(cam_yaw)*d;
205         }
206         if(pitch)
207         {
208                 cam_pitch+=M_PI/2*dt*pitch;
209                 if(cam_pitch>M_PI/12)
210                         cam_pitch=M_PI/12;
211                 else if(cam_pitch<-M_PI/2)
212                         cam_pitch=-M_PI/2;
213         }
214
215         if(tooltip_timeout && t>tooltip_timeout)
216         {
217                 Track3D *t3d=0;
218
219                 if(mode==CATALOGUE)
220                         t3d=pick_track(pointer_x, pointer_y);
221                 else
222                         t3d=pick_track(pointer_x, pointer_y);
223
224                 if(t3d)
225                 {
226                         const Track &track=t3d->get_track();
227                         ostringstream ss;
228                         ss.precision(2);
229                         ss<<track.get_article_number()<<' '<<track.get_description();
230                         if(mode!=CATALOGUE)
231                                 ss<<" (slope "<<track.get_slope()/track.get_length()*100<<"%)";
232                         if(track.get_turnout_id())
233                                 ss<<" (turnout "<<track.get_turnout_id()<<')';
234                         else if(track.get_sensor_id())
235                                 ss<<" (sensor "<<track.get_sensor_id()<<')';
236                         tooltip=decode<Utf8>(ss.str());
237
238                         move_tooltip(pointer_x, pointer_y);
239                 }
240                 else
241                         tooltip=L"";
242
243                 tooltip_timeout=Time::TimeStamp();
244         }
245
246         render();
247
248         SDL_GL_SwapBuffers();
249 }
250
251 Designer::~Designer()
252 {
253         delete manipulator;
254         delete selection;
255         delete layout;
256         delete layout_3d;
257         delete cat_layout;
258         delete cat_layout_3d;
259         delete measure;
260 }
261
262 /*** private ***/
263
264 void Designer::key_press(unsigned key, unsigned mod, wchar_t ch)
265 {
266         if(mode==INPUT)
267         {
268                 input->key_press(key, mod, ch);
269                 return;
270         }
271
272         if(key==SDLK_RSHIFT || key==SDLK_LSHIFT)
273                 shift=true;
274
275         if(key==SDLK_n)
276                 mode=CATALOGUE;
277         else if(key==SDLK_g)
278         {
279                 manipulator->start_move();
280                 mode=MANIPULATE;
281         }
282         else if(key==SDLK_r)
283         {
284                 manipulator->start_rotate();
285                 mode=MANIPULATE;
286         }
287         else if(key==SDLK_d)
288         {
289                 manipulator->duplicate();
290                 manipulator->start_move();
291                 mode=MANIPULATE;
292         }
293         else if(key==SDLK_w)
294         {
295                 input=new Input(*this, "Filename");
296                 input->signal_cancel.connect(sigc::mem_fun(this, &Designer::input_dismiss));
297                 input->signal_accept.connect(sigc::mem_fun(this, &Designer::save_accept));
298                 mode=INPUT;
299         }
300         else if(key==SDLK_PLUS)
301                 selection->select_more();
302         else if(key==SDLK_l && (mod&KMOD_SHIFT))
303         {
304                 const TrackSeq &tracks=layout->get_tracks();
305                 float len=0;
306                 for(TrackSeq::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
307                         len+=(*i)->get_total_length();
308                 cout<<"Total length: "<<len<<"m\n";
309         }
310         else if(key==SDLK_l)
311                 selection->select_linked();
312         else if(key==SDLK_m)
313         {
314                 measure->start();
315                 mode=MEASURE;
316         }
317         else if(key==SDLK_z)
318         {
319                 manipulator->start_elevate();
320                 mode=MANIPULATE;
321         }
322         else if(key==SDLK_ESCAPE)
323         {
324                 if(mode==MANIPULATE)
325                         manipulator->cancel();
326                 else if(mode==CATALOGUE)
327                         mode=SELECT;
328                 else
329                         selection->clear();
330         }
331         else if(key==SDLK_x)
332         {
333                 Selection::TrackSet tracks=selection->get_tracks();
334                 selection->clear();
335                 for(Selection::TrackSet::iterator i=tracks.begin(); i!=tracks.end(); ++i)
336                 {
337                         layout->remove_track(*i);
338                         delete *i;
339                 }
340         }
341         else if(key==SDLK_f && (mod&KMOD_SHIFT))
342         {
343                 const Selection::TrackSet &tracks=selection->get_tracks();
344                 const TrackSeq &ltracks=layout->get_tracks();
345                 for(Selection::TrackSet::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
346                 {
347                         (*i)->set_flex(!(*i)->get_flex());
348                         (*i)->break_links();
349                         for(TrackSeq::const_iterator j=ltracks.begin(); j!=ltracks.end(); ++j)
350                                 if(*j!=*i)
351                                         (*i)->snap_to(**j, true);
352
353                         Track3D *t3d=layout_3d->get_track(*i);
354                         if((*i)->get_flex())
355                                 t3d->set_color(Color(1, 0.5, 1));
356                         else
357                                 t3d->set_color(Color(1, 1, 1));
358                 }
359         }
360         else if(key==SDLK_f)
361                 manipulator->flatten();
362         else if(key==SDLK_e && (mod&KMOD_SHIFT))
363                 manipulator->even_slope(true);
364         else if(key==SDLK_e)
365                 manipulator->even_slope();
366         else if(key==SDLK_t)
367         {
368                 Track *track=selection->get_track();
369                 if(selection->size()==1 && track->get_n_routes()>1)
370                 {
371                         ostringstream ss;
372                         ss<<track->get_turnout_id();
373                         input=new Input(*this, "Turnout ID", ss.str());
374                         input->signal_cancel.connect(sigc::mem_fun(this, &Designer::input_dismiss));
375                         input->signal_accept.connect(sigc::mem_fun(this, &Designer::turnout_id_accept));
376                         mode=INPUT;
377                 }
378         }
379         else if(key==SDLK_s)
380         {
381                 Track *track=selection->get_track();
382                 if(selection->size()==1 && track->get_n_routes()==1)
383                 {
384                         ostringstream ss;
385                         ss<<track->get_sensor_id();
386                         input=new Input(*this, "Sensor ID", ss.str());
387                         input->signal_cancel.connect(sigc::mem_fun(this, &Designer::input_dismiss));
388                         input->signal_accept.connect(sigc::mem_fun(this, &Designer::sensor_id_accept));
389                         mode=INPUT;
390                 }
391         }
392         else if(key==SDLK_RIGHT)
393                 rotate=-1;
394         else if(key==SDLK_LEFT)
395                 rotate=1;
396         else if(key==SDLK_UP)
397                 move_y=1;
398         else if(key==SDLK_DOWN)
399                 move_y=-1;
400         else if(key==SDLK_INSERT)
401                 zoom=-1;
402         else if(key==SDLK_PAGEUP)
403                 zoom=1;
404         else if(key==SDLK_HOME)
405                 pitch=1;
406         else if(key==SDLK_END)
407                 pitch=-1;
408         else if(key==SDLK_DELETE)
409                 move_x=-1;
410         else if(key==SDLK_PAGEDOWN)
411                 move_x=1;
412 }
413
414 void Designer::key_release(unsigned key, unsigned)
415 {
416         if(mode==INPUT)
417                 return;
418
419         if(key==SDLK_RSHIFT || key==SDLK_LSHIFT)
420                 shift=false;
421         else if(key==SDLK_RIGHT || key==SDLK_LEFT)
422                 rotate=0;
423         else if(key==SDLK_UP || key==SDLK_DOWN)
424                 move_y=0;
425         else if(key==SDLK_INSERT || key==SDLK_PAGEUP)
426                 zoom=0;
427         else if(key==SDLK_HOME || key==SDLK_END)
428                 pitch=0;
429         else if(key==SDLK_DELETE || key==SDLK_PAGEDOWN)
430                 move_x=0;
431 }
432
433 void Designer::button_press(int x, int y, float gx, float gy, unsigned btn)
434 {
435         if(mode==CATALOGUE)
436         {
437                 if(btn==1)
438                 {
439                         Track3D *ctrack=pick_track(x, y);
440                         if(ctrack)
441                         {
442                                 Track *track=ctrack->get_track().copy();
443                                 track->set_position(Point(gx, gy, 0));
444                                 layout->add_track(track);
445
446                                 selection->clear();
447                                 selection->add_track(track);
448
449                                 mode=SELECT;
450                         }
451                 }
452                 else
453                         mode=SELECT;
454         }
455         else if(mode==SELECT)
456         {
457                 if(btn==1)
458                 {
459                         Track3D *track=pick_track(x, y);
460                         if(track)
461                         {
462                                 if(!shift)
463                                         selection->clear();
464                                 selection->toggle_track(&track->get_track());
465                         }
466                 }
467         }
468         else if(mode==MANIPULATE)
469                 manipulator->button_press(x, y, gx, gy, btn);
470         else if(mode==MEASURE)
471                 measure->button_press(x, y, gx, gy, btn);
472 }
473
474 void Designer::pointer_motion(int x, int y, float gx, float gy)
475 {
476         if(mode==SELECT || mode==CATALOGUE)
477         {
478                 pointer_x=x;
479                 pointer_y=y;
480                 tooltip_timeout=Time::now()+100*Time::msec;
481         }
482
483         if(mode!=INPUT)
484         {
485                 manipulator->pointer_motion(x, y, gx, gy);
486                 measure->pointer_motion(x, y, gx, gy);
487         }
488
489         if(mode==MEASURE || mode==MANIPULATE)
490                 move_tooltip(x, y);
491 }
492
493 void Designer::project_3d()
494 {
495         glMatrixMode(GL_PROJECTION);
496         glLoadIdentity();
497         glFrustum(-0.055228, 0.055228, -0.041421, 0.041421, 0.1, 10);
498         glMatrixMode(GL_MODELVIEW);
499 }
500
501 void Designer::apply_camera()
502 {
503         glLoadIdentity();
504         if(mode==CATALOGUE)
505                 glTranslatef(0, 0, -1);
506         else
507         {
508                 glRotatef(-cam_pitch*180/M_PI-90, 1, 0, 0);
509                 glRotatef(90-cam_yaw*180/M_PI, 0, 0, 1);
510                 glTranslatef(-cam_pos.x, -cam_pos.y, -cam_pos.z);
511         }
512 }
513
514 void Designer::render()
515 {
516         glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
517         glEnable(GL_DEPTH_TEST);
518
519         project_3d();
520         apply_camera();
521         if(mode==CATALOGUE)
522                 cat_layout_3d->render();
523         else
524         {
525                 layout_3d->render(true);
526                 manipulator->render();
527                 if(mode==MEASURE)
528                         measure->render();
529         }
530
531         /*glBegin(GL_LINES);
532         glColor4f(1, 0, 0, 1);
533         glVertex3f(0, 0, 0);
534         glVertex3f(0.5, 0, 0);
535         glColor4f(0, 1, 0, 1);
536         glVertex3f(0, 0, 0);
537         glVertex3f(0, 0.5, 0);
538         glColor4f(0, 0, 1, 1);
539         glVertex3f(0, 0, 0);
540         glVertex3f(0, 0, 0.5);
541         glEnd();*/
542
543         glMatrixMode(GL_PROJECTION);
544         glLoadIdentity();
545         glOrtho(0, screen_w, 0, screen_h, 0, 1);
546         glMatrixMode(GL_MODELVIEW);
547         glLoadIdentity();
548
549         glDisable(GL_DEPTH_TEST);
550
551         if(!tooltip.empty())
552         {
553                 glTranslatef(tooltip_x, tooltip_y, 0);
554                 glScalef(20, 20, 20);
555                 float width=font->get_string_width(tooltip);
556                 glColor4f(0, 0, 0, 0.5);
557                 glBegin(GL_QUADS);
558                 glVertex2f(0, 0);
559                 glVertex2f(width, 0);
560                 glVertex2f(width, 1);
561                 glVertex2f(0, 1);
562                 glEnd();
563                 glColor4f(1, 1, 1, 1);
564                 font->draw_string(tooltip);
565         }
566
567         if(mode==INPUT)
568                 input->render();
569 }
570
571 Track3D *Designer::pick_track(int x, int y)
572 {
573         Layout3D *l=layout_3d;
574         if(mode==CATALOGUE)
575                 l=cat_layout_3d;
576
577         float xx=((float)(x-(int)screen_w/2)/screen_h)*0.82843;
578         float yy=((float)y/screen_h-0.5)*0.82843;
579         float size=(float)4/screen_h*0.82843;
580
581         project_3d();
582         apply_camera();
583
584         return l->pick_track(xx, yy, size);
585 }
586
587 void Designer::manipulation_status(const string &status)
588 {
589         tooltip=decode<Utf8>(status);
590 }
591
592 void Designer::manipulation_done(bool)
593 {
594         mode=SELECT;
595 }
596
597 void Designer::measure_changed()
598 {
599         float pard=measure->get_parallel_distance()*1000;
600         float perpd=measure->get_perpendicular_distance()*1000;
601         float d=sqrt(pard*pard+perpd*perpd);
602         float adiff=measure->get_angle_difference()*180/M_PI;
603         ostringstream ss;
604         ss.precision(3);
605         ss<<"Par "<<pard<<"mm - Perp "<<perpd<<"mm - Total "<<d<<"mm - Angle "<<adiff<<"°";
606         tooltip=decode<Utf8>(ss.str());
607 }
608
609 void Designer::measure_done()
610 {
611         mode=SELECT;
612 }
613
614 void Designer::move_tooltip(int x, int y)
615 {
616         int w=(int)(font->get_string_width(tooltip)*20);
617         tooltip_x=max(min((int)screen_w-w, x), 0);
618         tooltip_y=max(min((int)screen_h-20, y), 0);
619 }
620
621 void Designer::save_accept()
622 {
623         layout->save(input->get_text());
624
625         input_dismiss();
626 }
627
628 void Designer::turnout_id_accept()
629 {
630         Track *track=selection->get_track();
631         unsigned id=lexical_cast<unsigned>(input->get_text());
632         track->set_turnout_id(id);
633
634         Track3D *t3d=layout_3d->get_track(track);
635         if(id)
636                 t3d->set_color(Color(0.5, 1, 1));
637         else
638                 t3d->set_color(Color(1, 1, 1));
639
640         input_dismiss();
641 }
642
643 void Designer::sensor_id_accept()
644 {
645         Track *track=selection->get_track();
646         unsigned id=lexical_cast<unsigned>(input->get_text());
647         track->set_sensor_id(id);
648
649         Track3D *t3d=layout_3d->get_track(track);
650         if(id)
651                 t3d->set_color(Color(1, 1, 0.5));
652         else
653                 t3d->set_color(Color(1, 1, 1));
654
655         input_dismiss();
656 }
657
658 void Designer::input_dismiss()
659 {
660         delete input;
661         input=0;
662         mode=SELECT;
663 }
664
665 Application::RegApp<Designer> Designer::reg;