4 #include "trackattachment.h"
10 TrackAttachment::TrackAttachment(Layout &l):
14 layout.signal_object_removed.connect(sigc::mem_fun(this, &TrackAttachment::object_removed));
17 TrackAttachment::~TrackAttachment()
20 track->remove_attachment(*this);
23 void TrackAttachment::attach_to(const TrackIter &t, float o)
26 track->remove_attachment(*this);
32 track->add_attachment(*this);
35 void TrackAttachment::attach_to_closest(float limit)
37 const set<Track *> &tracks = layout.get_all<Track>();
41 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
42 if(!(*i)->get_type().is_turnout())
45 sn.position = position;
46 sn.rotation = rotation;
47 if((*i)->snap(sn, limit, SNAP_SEGMENT))
49 float d = distance(position, sn.position);
53 closest = sn.position;
61 unsigned nsn = trk->get_n_snap_nodes();
62 for(unsigned i=0; i<nsn; ++i)
64 Snap sn = trk->get_snap_node(i);
65 Angle adiff = abs(wrap_balanced(sn.rotation-rotation));
66 if(adiff<Angle::quarter_turn())
68 TrackIter iter(trk, i);
69 attach_to(iter, find_offset(iter, closest));
75 attach_to(TrackIter(), 0);
78 float TrackAttachment::find_offset(const TrackIter &trk, const Vector &point)
80 float margin = 0.01*layout.get_catalogue().get_scale();
81 Vector epp = trk->get_snap_node(trk.entry()).position;
83 float dist = distance(epp, point);
84 float offs = min(dist, trk->get_type().get_path_length(0));
87 TrackPoint tp = trk->get_point(trk.entry(), offs);
88 float diff = dist-distance(epp, tp.pos);
97 void TrackAttachment::object_removed(Object &o)