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 float TrackAttachment::get_offset_from_endpoint(unsigned epi) const
25 if(epi==track.entry())
28 return track->get_type().get_path_length(0)-offset;
31 void TrackAttachment::attach_to(const TrackIter &t, float o)
34 track->remove_attachment(*this);
40 track->add_attachment(*this);
43 void TrackAttachment::attach_to_closest(float limit)
45 const set<Track *> &tracks = layout.get_all<Track>();
49 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
50 if(!(*i)->get_type().is_turnout())
53 sn.position = position;
54 sn.rotation = rotation;
55 if((*i)->snap(sn, limit, SNAP_SEGMENT))
57 float d = distance(position, sn.position);
61 closest = sn.position;
69 unsigned nsn = trk->get_n_snap_nodes();
70 for(unsigned i=0; i<nsn; ++i)
72 Snap sn = trk->get_snap_node(i);
73 Angle adiff = abs(wrap_balanced(sn.rotation-rotation));
74 if(adiff<Angle::quarter_turn())
76 TrackIter iter(trk, i);
77 attach_to(iter, find_offset(iter, closest));
83 attach_to(TrackIter(), 0);
86 float TrackAttachment::find_offset(const TrackIter &trk, const Vector &point)
88 float margin = 0.01*layout.get_catalogue().get_scale();
89 Vector epp = trk->get_snap_node(trk.entry()).position;
91 float dist = distance(epp, point);
92 float offs = min(dist, trk->get_type().get_path_length(0));
95 TrackPoint tp = trk->get_point(trk.entry(), offs);
96 float diff = dist-distance(epp, tp.pos);
105 void TrackAttachment::object_removed(Object &o)
107 if(&o==track.track())