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 float gauge = (*i)->get_type().get_gauge();
54 if((*i)->snap(sn, gauge*limit, SNAP_SEGMENT))
56 float d = distance(position, sn.position);
60 closest = sn.position;
68 unsigned nsn = trk->get_n_snap_nodes();
69 for(unsigned i=0; i<nsn; ++i)
71 Snap sn = trk->get_snap_node(i);
72 Angle adiff = abs(wrap_balanced(sn.rotation-rotation));
73 if(adiff<Angle::quarter_turn())
75 TrackIter iter(trk, i);
76 attach_to(TrackOffsetIter(iter, find_offset(iter, closest)));
82 attach_to(TrackOffsetIter());
85 float TrackAttachment::find_offset(const TrackIter &trk, const Vector &point)
87 float margin = 0.01*layout.get_catalogue().get_scale();
88 Vector epp = trk->get_snap_node(trk.entry()).position;
90 float dist = distance(epp, point);
91 float offs = min(dist, trk->get_type().get_path_length(0));
94 OrientedPoint p = trk->get_point(trk.entry(), offs);
95 float diff = dist-distance(epp, p.position);
104 void TrackAttachment::object_removed(Object &o)
106 if(&o==track.track())