3 This file is part of the MSP Märklin suite
4 Copyright © 2006-2010 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
10 #include <msp/strings/formatter.h>
11 #include "libmarklin/tracktype.h"
13 #include "manipulator.h"
14 #include "selection.h"
17 using namespace Marklin;
20 Manipulator::Manipulator(Designer &d, Selection &s):
26 selection.signal_changed.connect(sigc::mem_fun(this, &Manipulator::selection_changed));
29 void Manipulator::start_move()
34 move_origin = gpointer;
39 void Manipulator::start_rotate()
44 rot_origin = atan2(gpointer.y-center.y, gpointer.x-center.x);
49 void Manipulator::start_elevate()
54 elev_origin = pointer_y;
59 void Manipulator::duplicate()
64 list<Track *> new_tracks;
65 for(vector<MTrack>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
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);
74 for(list<Track *>::iterator i=new_tracks.begin(); i!=new_tracks.end(); ++i)
76 selection.add_track(*i);
77 for(list<Track *>::iterator j=i; j!=new_tracks.end(); ++j)
79 (*i)->snap_to(**j, true);
83 void Manipulator::flatten()
88 if(tracks.empty()) return;
91 for(vector<MTrack>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
92 z += i->track->get_position().z+i->track->get_slope()/2;
95 for(vector<MTrack>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
97 Point p = i->track->get_position();
98 i->track->set_position(Point(p.x, p.y, z));
99 i->track->set_slope(0);
102 for(set<Track *>::iterator i=neighbors.begin(); i!=neighbors.end(); ++i)
108 void Manipulator::even_slope(bool smooth)
113 if(neighbors.size()!=2)
116 for(vector<MTrack>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
117 if(i->track->get_type().get_endpoints().size()!=2)
120 list<Track *> tracks2;
121 for(vector<MTrack>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
122 tracks2.push_back(i->track);
126 list<TrackOrder> order;
127 Track *cur = *neighbors.begin();
128 while(tracks2.size())
131 for(list<Track *>::iterator i=tracks2.begin(); i!=tracks2.end(); ++i)
133 const vector<Track *> &links = (*i)->get_links();
140 else if(links[1]==cur)
148 order.push_back(TrackOrder(cur, rev));
149 total_len += cur->get_type().get_total_length();
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;
156 epi = (*nb)->get_endpoint_by_link(*order.back().track);
157 float end_z = (*nb)->get_endpoint_position(epi).z;
161 float dir = (end_z>start_z)?1:-1;
163 while((end_z-start_z)*dir/total_len>cur_slope+0.025 && order.size()>2)
167 float dz = order.front().track->get_type().get_total_length()*dir*cur_slope;
168 set_slope(order.front(), start_z, dz);
170 total_len -= order.front().track->get_type().get_total_length();
171 order.erase(order.begin());
173 dz = order.back().track->get_type().get_total_length()*dir*cur_slope;
174 set_slope(order.back(), end_z-dz, dz);
176 total_len -= order.back().track->get_type().get_total_length();
177 order.erase(--order.end());
181 float cur_z = start_z;
182 for(list<TrackOrder>::iterator i=order.begin(); i!=order.end(); ++i)
184 float dz = i->track->get_type().get_total_length()*(end_z-start_z)/total_len;
185 set_slope(*i, cur_z, dz);
189 for(set<Track *>::iterator i=neighbors.begin(); i!=neighbors.end(); ++i)
195 void Manipulator::cancel()
201 for(vector<MTrack>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
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);
207 for(set<Track *>::iterator i=neighbors.begin(); i!=neighbors.end(); ++i)
212 signal_done.emit(false);
215 void Manipulator::connect()
219 signal_status.emit("Exactly two tracks must be selected");
225 Track *track1 = tracks.front().track;
228 Track *track2 = tracks.back().track;
231 for(unsigned i=0; i<track1->get_type().get_endpoints().size(); ++i)
233 if(track1->get_link(i))
236 pos1 = track1->get_endpoint_position(i);
237 dir1 = track1->get_endpoint_direction(i);
239 for(unsigned j=0; j<track2->get_type().get_endpoints().size(); ++j)
241 if(track2->get_link(j))
244 Point pos2 = track2->get_endpoint_position(j);
245 float dir2 = track2->get_endpoint_direction(j);
247 float dz = pos2.z-pos1.z;
251 float adiff = dir1+M_PI-dir2;
261 float dx = pos2.x-pos1.x;
262 float dy = pos2.y-pos1.y;
263 if(abs(dx*s-dy*c)>limit)
279 signal_status.emit("No matching endpoints found");
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)
288 const vector<TrackPart> &parts = i->second->get_parts();
291 if(parts.front().is_curved() || parts.front().is_dead_end())
294 types_by_length[parts.front().get_length()] = i->second;
295 preference = max(preference, i->second->get_autofit_preference());
298 vector<float> lengths;
303 for(map<float, const TrackType *>::iterator i=types_by_length.end(); i!=types_by_length.begin(); )
306 if(i->second->get_autofit_preference()<preference)
308 if((!removed || i->first<removed) && i->first<gap+limit)
310 unsigned n = static_cast<unsigned>((gap+limit)/i->first);
311 lengths.insert(lengths.end(), n, i->first);
332 gap += lengths.back();
333 removed = lengths.back();
339 signal_status.emit("No connection possible");
345 for(vector<float>::iterator i=lengths.begin(); i!=lengths.end(); ++i)
347 map<float, const TrackType *>::iterator j = types_by_length.find(*i);
348 if(j==types_by_length.end())
349 throw LogicError("Internal error");
351 Track *track = new Track(*designer.get_layout(), *j->second);
352 track->set_position(pos1);
353 track->set_rotation(dir1);
355 track->snap_to(*track1, true);
362 track1->snap_to(*track2, true);
365 void Manipulator::button_press(int, int, float, float, unsigned btn)
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);
378 const set<Track *> <racks = designer.get_layout()->get_tracks();
379 for(set<Track *>::const_iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
382 for(vector<MTrack>::iterator j=tracks.begin(); (j!=tracks.end() && ok); ++j)
386 for(vector<MTrack>::iterator j=tracks.begin(); j!=tracks.end(); ++j)
387 j->track->snap_to(**i, true);
393 signal_done.emit(true);
397 void Manipulator::pointer_motion(int, int y, float gx, float gy)
400 gpointer = Point(gx, gy, 0);
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)
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);
412 const set<Track *> <racks = designer.get_layout()->get_tracks();
414 for(set<Track *>::const_iterator i=ltracks.begin(); (i!=ltracks.end() && !snapped); ++i)
417 for(vector<MTrack>::iterator j=tracks.begin(); (j!=tracks.end() && ok); ++j)
421 for(vector<MTrack>::iterator j=tracks.begin(); (j!=tracks.end() && !snapped); ++j)
422 if(j->track->snap_to(**i, false))
428 float da = snapped->track->get_rotation()-snapped->rot;
431 const Point &sp = snapped->track->get_position();
432 for(vector<MTrack>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
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);
443 else if(mode==ROTATE)
445 float a = atan2(gpointer.y-center.y, gpointer.x-center.x);
446 angle += a-rot_origin;
449 for(vector<MTrack>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
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);
457 else if(mode==ELEVATE)
459 float dz = (y-elev_origin)/1000.;
461 signal_status.emit(format("Elevation: %+.0fmm (%.0fmm)", dz*1000, (center.z+dz)*1000));
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));
466 for(set<Track *>::iterator i=neighbors.begin(); i!=neighbors.end(); ++i)
471 void Manipulator::selection_changed()
477 const set<Track *> &stracks = selection.get_tracks();
478 tracks.insert(tracks.end(), stracks.begin(), stracks.end());
484 void Manipulator::update_tracks()
487 for(vector<MTrack>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
489 unsigned n_endpoints = i->track->get_type().get_endpoints().size();
490 for(unsigned j=0; j<n_endpoints; ++j)
492 Point p = i->track->get_endpoint_position(j);
493 if(i==tracks.begin() && j==0)
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);
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)
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();
515 void Manipulator::update_neighbors()
518 for(vector<MTrack>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
520 const vector<Track *> &links = i->track->get_links();
521 for(vector<Track *>::const_iterator j=links.begin(); j!=links.end(); ++j)
525 if(neighbors.count(*j))
529 for(vector<MTrack>::iterator k=tracks.begin(); (k!=tracks.end() && ok); ++k)
533 neighbors.insert(*j);
538 void Manipulator::set_slope(TrackOrder &track, float z, float dz)
540 const Point &p = track.track->get_position();
543 track.track->set_position(Point(p.x, p.y, z+dz));
544 track.track->set_slope(-dz);
548 track.track->set_position(Point(p.x, p.y, z));
549 track.track->set_slope(dz);
553 Manipulator::MTrack::MTrack(Track *t):
555 pos(track->get_position()),
556 rot(track->get_rotation())