]> git.tdb.fi Git - r2c2.git/blob - source/designer/manipulator.cpp
42c422923ab7540c4e686263f1bc4a82040370f2
[r2c2.git] / source / designer / manipulator.cpp
1 /* $Id$
2
3 This file is part of the MSP Märklin suite
4 Copyright © 2006-2010  Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
6 */
7
8 #include <algorithm>
9 #include <cmath>
10 #include <msp/strings/formatter.h>
11 #include "libmarklin/tracktype.h"
12 #include "designer.h"
13 #include "manipulator.h"
14 #include "selection.h"
15
16 using namespace std;
17 using namespace Marklin;
18 using namespace Msp;
19
20 Manipulator::Manipulator(Designer &d, Selection &s):
21         designer(d),
22         selection(s),
23         mode(NONE),
24         angle(0)
25 {
26         selection.signal_changed.connect(sigc::mem_fun(this, &Manipulator::selection_changed));
27 }
28
29 void Manipulator::start_move()
30 {
31         if(mode)
32                 cancel();
33
34         move_origin = gpointer;
35
36         mode = MOVE;
37 }
38
39 void Manipulator::start_rotate()
40 {
41         if(mode)
42                 cancel();
43
44         rot_origin = atan2(gpointer.y-center.y, gpointer.x-center.x);
45
46         mode = ROTATE;
47 }
48
49 void Manipulator::start_elevate()
50 {
51         if(mode)
52                 cancel();
53
54         elev_origin = pointer_y;
55
56         mode = ELEVATE;
57 }
58
59 void Manipulator::duplicate()
60 {
61         if(mode)
62                 cancel();
63
64         list<Track *> new_tracks;
65         for(vector<MTrack>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
66         {
67                 Track *track = new Track(*designer.get_layout(), i->track->get_type());
68                 track->set_position(i->track->get_position());
69                 track->set_rotation(i->track->get_rotation());
70                 new_tracks.push_back(track);
71         }
72
73         selection.clear();
74         for(list<Track *>::iterator i=new_tracks.begin(); i!=new_tracks.end(); ++i)
75         {
76                 selection.add_track(*i);
77                 for(list<Track *>::iterator j=i; j!=new_tracks.end(); ++j)
78                         if(j!=i)
79                                 (*i)->snap_to(**j, true);
80         }
81 }
82
83 void Manipulator::flatten()
84 {
85         if(mode)
86                 cancel();
87
88         if(tracks.empty()) return;
89
90         float z = 0;
91         for(vector<MTrack>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
92                 z += i->track->get_position().z+i->track->get_slope()/2;
93         z /= tracks.size();
94
95         for(vector<MTrack>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
96         {
97                 Point p = i->track->get_position();
98                 i->track->set_position(Point(p.x, p.y, z));
99                 i->track->set_slope(0);
100         }
101
102         for(set<Track *>::iterator i=neighbors.begin(); i!=neighbors.end(); ++i)
103                 (*i)->check_slope();
104
105         update_tracks();
106 }
107
108 void Manipulator::even_slope(bool smooth)
109 {
110         if(mode)
111                 cancel();
112
113         if(neighbors.size()!=2)
114                 return;
115
116         for(vector<MTrack>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
117                 if(i->track->get_type().get_endpoints().size()!=2)
118                         return;
119
120         list<Track *> tracks2;
121         for(vector<MTrack>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
122                 tracks2.push_back(i->track);
123
124         float total_len = 0;
125
126         list<TrackOrder> order;
127         Track *cur = *neighbors.begin();
128         while(tracks2.size())
129         {
130                 bool rev = false;
131                 for(list<Track *>::iterator i=tracks2.begin(); i!=tracks2.end(); ++i)
132                 {
133                         const vector<Track *> &links = (*i)->get_links();
134                         if(links[0]==cur)
135                         {
136                                 cur = *i;
137                                 tracks2.erase(i);
138                                 break;
139                         }
140                         else if(links[1]==cur)
141                         {
142                                 cur = *i;
143                                 rev = true;
144                                 tracks2.erase(i);
145                                 break;
146                         }
147                 }
148                 order.push_back(TrackOrder(cur, rev));
149                 total_len += cur->get_type().get_total_length();
150         }
151
152         set<Track *>::iterator nb = neighbors.begin();
153         int epi = (*nb)->get_endpoint_by_link(*order.front().track);
154         float start_z = (*nb)->get_endpoint_position(epi).z;
155         ++nb;
156         epi = (*nb)->get_endpoint_by_link(*order.back().track);
157         float end_z = (*nb)->get_endpoint_position(epi).z;
158
159         if(smooth)
160         {
161                 float dir = (end_z>start_z)?1:-1;
162                 float cur_slope = 0;
163                 while((end_z-start_z)*dir/total_len>cur_slope+0.025 && order.size()>2)
164                 {
165                         cur_slope += 0.025;
166
167                         float dz = order.front().track->get_type().get_total_length()*dir*cur_slope;
168                         set_slope(order.front(), start_z, dz);
169                         start_z += dz;
170                         total_len -= order.front().track->get_type().get_total_length();
171                         order.erase(order.begin());
172
173                         dz = order.back().track->get_type().get_total_length()*dir*cur_slope;
174                         set_slope(order.back(), end_z-dz, dz);
175                         end_z -= dz;
176                         total_len -= order.back().track->get_type().get_total_length();
177                         order.erase(--order.end());
178                 }
179         }
180
181         float cur_z = start_z;
182         for(list<TrackOrder>::iterator i=order.begin(); i!=order.end(); ++i)
183         {
184                 float dz = i->track->get_type().get_total_length()*(end_z-start_z)/total_len;
185                 set_slope(*i, cur_z, dz);
186                 cur_z += dz;
187         }
188
189         for(set<Track *>::iterator i=neighbors.begin(); i!=neighbors.end(); ++i)
190                 (*i)->check_slope();
191
192         update_tracks();
193 }
194
195 void Manipulator::cancel()
196 {
197         if(!mode)
198                 return;
199         mode = NONE;
200
201         for(vector<MTrack>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
202         {
203                 i->track->set_position(Point(center.x+i->pos.x, center.y+i->pos.y, center.z+i->pos.z));
204                 i->track->set_rotation(i->rot);
205         }
206
207         for(set<Track *>::iterator i=neighbors.begin(); i!=neighbors.end(); ++i)
208                 (*i)->check_slope();
209
210         angle = 0;
211
212         signal_done.emit(false);
213 }
214
215 void Manipulator::connect()
216 {
217         if(tracks.size()!=2)
218         {
219                 signal_status.emit("Exactly two tracks must be selected");
220                 return;
221         }
222
223         float limit = 0.001;
224
225         Track *track1 = tracks.front().track;
226         Point pos1;
227         float dir1;
228         Track *track2 = tracks.back().track;
229         bool ok = false;
230         float gap;
231         for(unsigned i=0; i<track1->get_type().get_endpoints().size(); ++i)
232         {
233                 if(track1->get_link(i))
234                         continue;
235
236                 pos1 = track1->get_endpoint_position(i);
237                 dir1 = track1->get_endpoint_direction(i);
238
239                 for(unsigned j=0; j<track2->get_type().get_endpoints().size(); ++j)
240                 {
241                         if(track2->get_link(j))
242                                 continue;
243
244                         Point pos2 = track2->get_endpoint_position(j);
245                         float dir2 = track2->get_endpoint_direction(j);
246
247                         float dz = pos2.z-pos1.z;
248                         if(abs(dz)>0.02)
249                                 continue;
250
251                         float adiff = dir1+M_PI-dir2;
252                         while(adiff<-M_PI)
253                                 adiff += M_PI*2;
254                         while(adiff>M_PI)
255                                 adiff -= M_PI*2;
256                         if(abs(adiff)>0.01)
257                                 continue;
258
259                         float c = cos(dir1);
260                         float s = sin(dir1);
261                         float dx = pos2.x-pos1.x;
262                         float dy = pos2.y-pos1.y;
263                         if(abs(dx*s-dy*c)>limit)
264                                 continue;
265
266                         gap = dx*c+dy*s;
267                         if(gap<0)
268                                 continue;
269
270                         ok = true;
271                 }
272
273                 if(ok)
274                         break;
275         }
276
277         if(!ok)
278         {
279                 signal_status.emit("No matching endpoints found");
280                 return;
281         }
282
283         const map<unsigned, TrackType *> &track_types = designer.get_catalogue().get_tracks();
284         std::map<float, const TrackType *> types_by_length;
285         unsigned preference = 0;
286         for(map<unsigned, TrackType *>::const_iterator i=track_types.begin(); i!=track_types.end(); ++i)
287         {
288                 const vector<TrackPart> &parts = i->second->get_parts();
289                 if(parts.size()!=1)
290                         continue;
291                 if(parts.front().is_curved() || parts.front().is_dead_end())
292                         continue;
293
294                 types_by_length[parts.front().get_length()] = i->second;
295                 preference = max(preference, i->second->get_autofit_preference());
296         }
297
298         vector<float> lengths;
299         float removed = 0;
300         while(gap>limit)
301         {
302                 bool found = false;
303                 for(map<float, const TrackType *>::iterator i=types_by_length.end(); i!=types_by_length.begin(); )
304                 {
305                         --i;
306                         if(i->second->get_autofit_preference()<preference)
307                                 continue;
308                         if((!removed || i->first<removed) && i->first<gap+limit)
309                         {
310                                 unsigned n = static_cast<unsigned>((gap+limit)/i->first);
311                                 lengths.insert(lengths.end(), n, i->first);
312                                 gap -= n*i->first;
313                                 found = true;
314                                 break;
315                         }
316                 }
317
318                 if(found)
319                         continue;
320
321                 if(lengths.empty())
322                 {
323                         if(preference>0)
324                         {
325                                 --preference;
326                                 removed = 0;
327                                 continue;
328                         }
329                         break;
330                 }
331
332                 gap += lengths.back();
333                 removed = lengths.back();
334                 lengths.pop_back();
335         }
336
337         if(lengths.empty())
338         {
339                 signal_status.emit("No connection possible");
340                 return;
341         }
342
343         float c = cos(dir1);
344         float s = sin(dir1);
345         for(vector<float>::iterator i=lengths.begin(); i!=lengths.end(); ++i)
346         {
347                 map<float, const TrackType *>::iterator j = types_by_length.find(*i);
348                 if(j==types_by_length.end())
349                         throw LogicError("Internal error");
350
351                 Track *track = new Track(*designer.get_layout(), *j->second);
352                 track->set_position(pos1);
353                 track->set_rotation(dir1);
354
355                 track->snap_to(*track1, true);
356                 track1 = track;
357
358                 pos1.x += c**i;
359                 pos1.y += s**i;
360         }
361
362         track1->snap_to(*track2, true);
363 }
364
365 void Manipulator::button_press(int, int, float, float, unsigned btn)
366 {
367         if(btn==3)
368                 cancel();
369         else if(mode)
370         {
371                 mode = NONE;
372                 angle = 0;
373
374                 for(set<Track *>::iterator i=neighbors.begin(); i!=neighbors.end(); ++i)
375                         for(vector<MTrack>::iterator j=tracks.begin(); j!=tracks.end(); ++j)
376                                 j->track->break_link(**i);
377
378                 const set<Track *> &ltracks = designer.get_layout()->get_tracks();
379                 for(set<Track *>::const_iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
380                 {
381                         bool ok = true;
382                         for(vector<MTrack>::iterator j=tracks.begin(); (j!=tracks.end() && ok); ++j)
383                                 ok = (j->track!=*i);
384                         if(!ok) continue;
385
386                         for(vector<MTrack>::iterator j=tracks.begin(); j!=tracks.end(); ++j)
387                                 j->track->snap_to(**i, true);
388                 }
389
390                 update_tracks();
391                 update_neighbors();
392
393                 signal_done.emit(true);
394         }
395 }
396
397 void Manipulator::pointer_motion(int, int y, float gx, float gy)
398 {
399         pointer_y = y;
400         gpointer = Point(gx, gy, 0);
401
402         if(mode==MOVE)
403         {
404                 Point delta(gpointer.x-move_origin.x, gpointer.y-move_origin.y, 0);
405                 Point offset(center.x+delta.x, center.y+delta.y, center.z);
406                 for(vector<MTrack>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
407                 {
408                         i->track->set_position(Point(offset.x+i->pos.x, offset.y+i->pos.y, offset.z+i->pos.z));
409                         i->track->set_rotation(i->rot);
410                 }
411
412                 const set<Track *> &ltracks = designer.get_layout()->get_tracks();
413                 MTrack *snapped = 0;
414                 for(set<Track *>::const_iterator i=ltracks.begin(); (i!=ltracks.end() && !snapped); ++i)
415                 {
416                         bool ok = true;
417                         for(vector<MTrack>::iterator j=tracks.begin(); (j!=tracks.end() && ok); ++j)
418                                 ok = (j->track!=*i);
419                         if(!ok) continue;
420
421                         for(vector<MTrack>::iterator j=tracks.begin(); (j!=tracks.end() && !snapped); ++j)
422                                 if(j->track->snap_to(**i, false))
423                                         snapped = &*j;
424                 }
425
426                 if(snapped)
427                 {
428                         float da = snapped->track->get_rotation()-snapped->rot;
429                         float c = cos(da);
430                         float s = sin(da);
431                         const Point &sp = snapped->track->get_position();
432                         for(vector<MTrack>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
433                         {
434                                 if(&*i==snapped)
435                                         continue;
436
437                                 Point dp(i->pos.x-snapped->pos.x, i->pos.y-snapped->pos.y, 0);
438                                 i->track->set_position(Point(sp.x+c*dp.x-s*dp.y, sp.y+s*dp.x+c*dp.y, sp.z+i->pos.z-snapped->pos.z));
439                                 i->track->set_rotation(i->rot+da);
440                         }
441                 }
442         }
443         else if(mode==ROTATE)
444         {
445                 float a = atan2(gpointer.y-center.y, gpointer.x-center.x);
446                 angle += a-rot_origin;
447                 rot_origin = a;
448
449                 for(vector<MTrack>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
450                 {
451                         float c = cos(angle);
452                         float s = sin(angle);
453                         i->track->set_position(Point(center.x+c*i->pos.x-s*i->pos.y, center.y+s*i->pos.x+c*i->pos.y, center.z+i->pos.z));
454                         i->track->set_rotation(angle+i->rot);
455                 }
456         }
457         else if(mode==ELEVATE)
458         {
459                 float dz = (y-elev_origin)/1000.;
460
461                 signal_status.emit(format("Elevation: %+.0fmm (%.0fmm)", dz*1000, (center.z+dz)*1000));
462
463                 for(vector<MTrack>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
464                         i->track->set_position(Point(center.x+i->pos.x, center.y+i->pos.y, center.z+i->pos.z+dz));
465
466                 for(set<Track *>::iterator i=neighbors.begin(); i!=neighbors.end(); ++i)
467                         (*i)->check_slope();
468         }
469 }
470
471 void Manipulator::selection_changed()
472 {
473         if(mode)
474                 cancel();
475
476         tracks.clear();
477         const set<Track *> &stracks = selection.get_tracks();
478         tracks.insert(tracks.end(), stracks.begin(), stracks.end());
479
480         update_neighbors();
481         update_tracks();
482 }
483
484 void Manipulator::update_tracks()
485 {
486         Point minp, maxp;
487         for(vector<MTrack>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
488         {
489                 unsigned n_endpoints = i->track->get_type().get_endpoints().size();
490                 for(unsigned j=0; j<n_endpoints; ++j)
491                 {
492                         Point p = i->track->get_endpoint_position(j);
493                         if(i==tracks.begin() && j==0)
494                                 minp = maxp = p;
495                         else
496                         {
497                                 minp.x = min(minp.x, p.x);
498                                 maxp.x = max(maxp.x, p.x);
499                                 minp.y = min(minp.y, p.y);
500                                 maxp.y = max(maxp.y, p.y);
501                                 minp.z = min(minp.z, p.z);
502                         }
503                 }
504         }
505
506         center = Point((minp.x+maxp.x)/2, (minp.y+maxp.y)/2, minp.z);
507         for(vector<MTrack>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
508         {
509                 const Point &tp = i->track->get_position();
510                 i->pos = Point(tp.x-center.x, tp.y-center.y, tp.z-center.z);
511                 i->rot = i->track->get_rotation();
512         }
513 }
514
515 void Manipulator::update_neighbors()
516 {
517         neighbors.clear();
518         for(vector<MTrack>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
519         {
520                 const vector<Track *> &links = i->track->get_links();
521                 for(vector<Track *>::const_iterator j=links.begin(); j!=links.end(); ++j)
522                 {
523                         if(!*j)
524                                 continue;
525                         if(neighbors.count(*j))
526                                 continue;
527
528                         bool ok = true;
529                         for(vector<MTrack>::iterator k=tracks.begin(); (k!=tracks.end() && ok); ++k)
530                                 ok = (k->track!=*j);
531
532                         if(ok)
533                                 neighbors.insert(*j);
534                 }
535         }
536 }
537
538 void Manipulator::set_slope(TrackOrder &track, float z, float dz)
539 {
540         const Point &p = track.track->get_position();
541         if(track.rev)
542         {
543                 track.track->set_position(Point(p.x, p.y, z+dz));
544                 track.track->set_slope(-dz);
545         }
546         else
547         {
548                 track.track->set_position(Point(p.x, p.y, z));
549                 track.track->set_slope(dz);
550         }
551 }
552
553 Manipulator::MTrack::MTrack(Track *t):
554         track(t),
555         pos(track->get_position()),
556         rot(track->get_rotation())
557 { }