4 #include "trackattachment.h"
10 TrackAttachment::TrackAttachment(Layout &l):
13 layout.signal_object_removed.connect(sigc::mem_fun(this, &TrackAttachment::object_removed));
16 TrackAttachment::~TrackAttachment()
19 track->remove_attachment(*this);
22 float TrackAttachment::get_offset_from_endpoint(unsigned epi) const
24 if(epi==track.entry())
25 return track.offset();
27 return track->get_type().get_path_length(0)-track.offset();
30 void TrackAttachment::attach_to(const TrackOffsetIter &t)
33 track->remove_attachment(*this);
38 track->add_attachment(*this);
41 void TrackAttachment::attach_to_closest(float limit)
43 const set<Track *> &tracks = layout.get_all<Track>();
47 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
48 if(!(*i)->get_type().is_turnout())
51 sn.position = position;
52 sn.rotation = rotation;
53 if((*i)->snap(sn, limit, SNAP_SEGMENT))
55 float d = distance(position, sn.position);
59 closest = sn.position;
67 unsigned nsn = trk->get_n_snap_nodes();
68 for(unsigned i=0; i<nsn; ++i)
70 Snap sn = trk->get_snap_node(i);
71 Angle adiff = abs(wrap_balanced(sn.rotation-rotation));
72 if(adiff<Angle::quarter_turn())
74 TrackIter iter(trk, i);
75 attach_to(TrackOffsetIter(iter, find_offset(iter, closest)));
81 attach_to(TrackOffsetIter());
84 float TrackAttachment::find_offset(const TrackIter &trk, const Vector &point)
86 float margin = 0.01*layout.get_catalogue().get_scale();
87 Vector epp = trk->get_snap_node(trk.entry()).position;
89 float dist = distance(epp, point);
90 float offs = min(dist, trk->get_type().get_path_length(0));
93 OrientedPoint p = trk->get_point(trk.entry(), offs);
94 float diff = dist-distance(epp, p.position);
103 void TrackAttachment::object_removed(Object &o)
105 if(&o==track.track())