3 This file is part of R²C²
4 Copyright © 2006-2010 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
10 #include <msp/strings/formatter.h>
11 #include "libr2c2/tracktype.h"
13 #include "manipulator.h"
14 #include "selection.h"
20 Manipulator::Manipulator(Designer &d, Graphics::EventSource &es, Selection &s):
27 event_source.signal_button_press.connect(sigc::mem_fun(this, &Manipulator::button_press));
28 event_source.signal_pointer_motion.connect(sigc::mem_fun(this, &Manipulator::pointer_motion));
29 selection.signal_changed.connect(sigc::mem_fun(this, &Manipulator::selection_changed));
32 void Manipulator::start_move()
37 move_origin = gpointer;
42 void Manipulator::start_rotate()
47 rot_origin = atan2(gpointer.y-center.y, gpointer.x-center.x);
52 void Manipulator::start_elevate()
57 elev_origin = pointer_y;
62 bool Manipulator::start_extend()
68 const set<Track *> &stracks = selection.get_tracks();
69 for(set<Track *>::const_iterator i=stracks.begin(); (!ok && i!=stracks.end()); ++i)
71 const vector<Track *> &links = (*i)->get_links();
72 for(vector<Track *>::const_iterator j=links.begin(); (!ok && j!=links.end()); ++j)
79 signal_status.emit("No free endpoints");
84 void Manipulator::duplicate()
89 list<Track *> new_tracks;
90 for(vector<MTrack>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
92 Track *track = new Track(designer.get_layout(), i->track->get_type());
93 track->set_position(i->track->get_position());
94 track->set_rotation(i->track->get_rotation());
95 new_tracks.push_back(track);
99 for(list<Track *>::iterator i=new_tracks.begin(); i!=new_tracks.end(); ++i)
101 selection.add_track(*i);
102 for(list<Track *>::iterator j=i; j!=new_tracks.end(); ++j)
104 (*i)->snap_to(**j, true);
108 void Manipulator::flatten()
113 if(tracks.empty()) return;
116 for(vector<MTrack>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
117 z += i->track->get_position().z+i->track->get_slope()/2;
120 for(vector<MTrack>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
122 Vector p = i->track->get_position();
123 i->track->set_position(Vector(p.x, p.y, z));
124 i->track->set_slope(0);
127 for(set<Track *>::iterator i=neighbors.begin(); i!=neighbors.end(); ++i)
133 void Manipulator::even_slope(bool smooth)
138 if(neighbors.size()!=2)
141 for(vector<MTrack>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
142 if(i->track->get_type().get_endpoints().size()!=2)
145 list<Track *> tracks2;
146 for(vector<MTrack>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
147 tracks2.push_back(i->track);
151 list<TrackOrder> order;
152 Track *cur = *neighbors.begin();
153 while(tracks2.size())
156 for(list<Track *>::iterator i=tracks2.begin(); i!=tracks2.end(); ++i)
158 const vector<Track *> &links = (*i)->get_links();
165 else if(links[1]==cur)
173 order.push_back(TrackOrder(cur, rev));
174 total_len += cur->get_type().get_total_length();
177 set<Track *>::iterator nb = neighbors.begin();
178 int epi = (*nb)->get_endpoint_by_link(*order.front().track);
179 float start_z = (*nb)->get_endpoint_position(epi).z;
181 epi = (*nb)->get_endpoint_by_link(*order.back().track);
182 float end_z = (*nb)->get_endpoint_position(epi).z;
186 float dir = (end_z>start_z)?1:-1;
188 while((end_z-start_z)*dir/total_len>cur_slope+0.025 && order.size()>2)
192 float dz = order.front().track->get_type().get_total_length()*dir*cur_slope;
193 set_slope(order.front(), start_z, dz);
195 total_len -= order.front().track->get_type().get_total_length();
196 order.erase(order.begin());
198 dz = order.back().track->get_type().get_total_length()*dir*cur_slope;
199 set_slope(order.back(), end_z-dz, dz);
201 total_len -= order.back().track->get_type().get_total_length();
202 order.erase(--order.end());
206 float cur_z = start_z;
207 for(list<TrackOrder>::iterator i=order.begin(); i!=order.end(); ++i)
209 float dz = i->track->get_type().get_total_length()*(end_z-start_z)/total_len;
210 set_slope(*i, cur_z, dz);
214 for(set<Track *>::iterator i=neighbors.begin(); i!=neighbors.end(); ++i)
220 void Manipulator::connect()
224 signal_status.emit("Exactly two tracks must be selected");
228 float limit = designer.get_layout().get_catalogue().get_gauge()/10;
230 Track *track1 = tracks.front().track;
233 Track *track2 = tracks.back().track;
236 for(unsigned i=0; i<track1->get_type().get_endpoints().size(); ++i)
238 if(track1->get_link(i))
241 pos1 = track1->get_endpoint_position(i);
242 dir1 = track1->get_endpoint_direction(i);
244 for(unsigned j=0; j<track2->get_type().get_endpoints().size(); ++j)
246 if(track2->get_link(j))
249 Vector pos2 = track2->get_endpoint_position(j);
250 float dir2 = track2->get_endpoint_direction(j);
252 float dz = pos2.z-pos1.z;
256 float adiff = dir1+M_PI-dir2;
266 float dx = pos2.x-pos1.x;
267 float dy = pos2.y-pos1.y;
268 if(abs(dx*s-dy*c)>limit)
284 signal_status.emit("No aligned endpoints found");
288 vector<Track *> trks = create_straight(pos1, dir1, gap, limit);
292 signal_status.emit("No connection possible");
296 trks.front()->snap_to(*track1, true);
297 trks.back()->snap_to(*track2, true);
299 selection.replace(trks.begin(), trks.end());
302 void Manipulator::cancel()
308 for(vector<MTrack>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
310 i->track->set_position(Vector(center.x+i->pos.x, center.y+i->pos.y, center.z+i->pos.z));
311 i->track->set_rotation(i->rot);
314 for(set<Track *>::iterator i=neighbors.begin(); i!=neighbors.end(); ++i)
317 for(vector<Track *>::iterator i=extend_tracks.begin(); i!=extend_tracks.end(); ++i)
319 extend_tracks.clear();
323 signal_done.emit(false);
326 void Manipulator::button_press(int, int, unsigned btn, unsigned)
341 for(set<Track *>::iterator i=neighbors.begin(); i!=neighbors.end(); ++i)
342 for(vector<MTrack>::iterator j=tracks.begin(); j!=tracks.end(); ++j)
343 j->track->break_link(**i);
346 const set<Track *> <racks = designer.get_layout().get_tracks();
347 for(set<Track *>::const_iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
350 for(vector<MTrack>::iterator j=tracks.begin(); (j!=tracks.end() && ok); ++j)
354 for(vector<MTrack>::iterator j=tracks.begin(); j!=tracks.end(); ++j)
355 j->track->snap_to(**i, true);
360 selection.replace(extend_tracks.begin(), extend_tracks.end());
361 extend_tracks.clear();
369 signal_done.emit(true);
373 void Manipulator::pointer_motion(int x, int y)
375 pointer_y = event_source.get_height()-1-y;
376 gpointer = designer.map_pointer_to_ground(x, pointer_y);
380 Vector delta(gpointer.x-move_origin.x, gpointer.y-move_origin.y, 0);
381 Vector offset(center.x+delta.x, center.y+delta.y, center.z);
382 for(vector<MTrack>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
384 i->track->set_position(Vector(offset.x+i->pos.x, offset.y+i->pos.y, offset.z+i->pos.z));
385 i->track->set_rotation(i->rot);
388 const set<Track *> <racks = designer.get_layout().get_tracks();
389 float limit = max(designer.get_layout().get_catalogue().get_gauge(),
390 designer.get_camera_controller().get_view_scale()*5/event_source.get_height());
392 for(set<Track *>::const_iterator i=ltracks.begin(); (i!=ltracks.end() && !snapped); ++i)
395 for(vector<MTrack>::iterator j=tracks.begin(); (j!=tracks.end() && ok); ++j)
399 for(vector<MTrack>::iterator j=tracks.begin(); (j!=tracks.end() && !snapped); ++j)
400 if(j->track->snap_to(**i, false, limit))
406 float da = snapped->track->get_rotation()-snapped->rot;
409 const Vector &sp = snapped->track->get_position();
410 for(vector<MTrack>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
415 Vector dp(i->pos.x-snapped->pos.x, i->pos.y-snapped->pos.y, 0);
416 i->track->set_position(Vector(sp.x+c*dp.x-s*dp.y, sp.y+s*dp.x+c*dp.y, sp.z+i->pos.z-snapped->pos.z));
417 i->track->set_rotation(i->rot+da);
421 else if(mode==ROTATE)
423 float a = atan2(gpointer.y-center.y, gpointer.x-center.x);
424 angle += a-rot_origin;
427 for(vector<MTrack>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
429 float c = cos(angle);
430 float s = sin(angle);
431 i->track->set_position(Vector(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));
432 i->track->set_rotation(angle+i->rot);
435 else if(mode==ELEVATE)
437 float dz = (pointer_y-elev_origin)/1000.;
439 signal_status.emit(format("Elevation: %+.0fmm (%.0fmm)", dz*1000, (center.z+dz)*1000));
441 for(vector<MTrack>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
442 i->track->set_position(Vector(center.x+i->pos.x, center.y+i->pos.y, center.z+i->pos.z+dz));
444 for(set<Track *>::iterator i=neighbors.begin(); i!=neighbors.end(); ++i)
447 else if(mode==EXTEND)
452 for(vector<MTrack>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
454 unsigned n_endpoints = i->track->get_type().get_endpoints().size();
455 for(unsigned j=0; j<n_endpoints; ++j)
457 if(i->track->get_link(j))
460 Vector ep_pos = i->track->get_endpoint_position(j);
461 float ep_dir = i->track->get_endpoint_direction(j);
462 float c = cos(ep_dir);
463 float s = sin(ep_dir);
464 float dx = gpointer.x-ep_pos.x;
465 float dy = gpointer.y-ep_pos.y;
467 float len = dx*c+dy*s;
479 vector<Track *> trks = create_straight(pos, dir, length, max(length/500, 0.001f));
483 for(vector<Track *>::iterator i=extend_tracks.begin(); i!=extend_tracks.end(); ++i)
485 extend_tracks = trks;
487 map<ArticleNumber, unsigned> counts;
489 for(vector<Track *>::iterator i=extend_tracks.begin(); i!=extend_tracks.end(); ++i)
491 length += (*i)->get_type().get_total_length();
492 ++counts[(*i)->get_type().get_article_number()];
496 for(map<ArticleNumber, unsigned>::const_iterator i=counts.begin(); i!=counts.end(); ++i)
500 detail += format("%dx %s", i->second, i->first);
503 signal_status.emit(format("Extend: %.0fmm (%s)", length*1000, detail));
509 void Manipulator::selection_changed()
515 const set<Track *> &stracks = selection.get_tracks();
516 tracks.insert(tracks.end(), stracks.begin(), stracks.end());
522 void Manipulator::update_tracks()
525 for(vector<MTrack>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
527 unsigned n_endpoints = i->track->get_type().get_endpoints().size();
528 for(unsigned j=0; j<n_endpoints; ++j)
530 Vector p = i->track->get_endpoint_position(j);
531 if(i==tracks.begin() && j==0)
535 minp.x = min(minp.x, p.x);
536 maxp.x = max(maxp.x, p.x);
537 minp.y = min(minp.y, p.y);
538 maxp.y = max(maxp.y, p.y);
539 minp.z = min(minp.z, p.z);
544 center = Vector((minp.x+maxp.x)/2, (minp.y+maxp.y)/2, minp.z);
545 for(vector<MTrack>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
547 const Vector &tp = i->track->get_position();
548 i->pos = Vector(tp.x-center.x, tp.y-center.y, tp.z-center.z);
549 i->rot = i->track->get_rotation();
553 void Manipulator::update_neighbors()
556 for(vector<MTrack>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
558 const vector<Track *> &links = i->track->get_links();
559 for(vector<Track *>::const_iterator j=links.begin(); j!=links.end(); ++j)
563 if(neighbors.count(*j))
567 for(vector<MTrack>::iterator k=tracks.begin(); (k!=tracks.end() && ok); ++k)
571 neighbors.insert(*j);
576 void Manipulator::set_slope(TrackOrder &track, float z, float dz)
578 const Vector &p = track.track->get_position();
581 track.track->set_position(Vector(p.x, p.y, z+dz));
582 track.track->set_slope(-dz);
586 track.track->set_position(Vector(p.x, p.y, z));
587 track.track->set_slope(dz);
591 vector<Track *> Manipulator::create_straight(const R2C2::Vector &start, float dir, float length, float limit)
593 const Catalogue::TrackMap &track_types = designer.get_catalogue().get_tracks();
594 std::map<float, const TrackType *> types_by_length;
595 unsigned preference = 0;
596 for(Catalogue::TrackMap::const_iterator i=track_types.begin(); i!=track_types.end(); ++i)
598 const vector<TrackPart> &parts = i->second->get_parts();
601 if(parts.front().is_curved() || parts.front().is_dead_end())
604 types_by_length[parts.front().get_length()] = i->second;
605 preference = max(preference, i->second->get_autofit_preference());
608 vector<float> lengths;
613 for(map<float, const TrackType *>::iterator i=types_by_length.end(); i!=types_by_length.begin(); )
616 if(i->second->get_autofit_preference()<preference)
618 if((!removed || i->first<removed) && i->first<length+limit)
620 unsigned n = static_cast<unsigned>((length+limit)/i->first);
621 lengths.insert(lengths.end(), n, i->first);
622 length -= n*i->first;
642 length += lengths.back();
643 removed = lengths.back();
647 vector<Track *> trks;
654 for(vector<float>::iterator i=lengths.begin(); i!=lengths.end(); ++i)
656 map<float, const TrackType *>::iterator j = types_by_length.find(*i);
657 if(j==types_by_length.end())
658 throw LogicError("Internal error");
660 Track *track = new Track(designer.get_layout(), *j->second);
661 track->set_position(pos);
662 track->set_rotation(dir);
665 track->snap_to(*trks.back(), true);
666 trks.push_back(track);
677 Manipulator::MTrack::MTrack(Track *t):
679 pos(track->get_position()),
680 rot(track->get_rotation())