4 #include "trackattachment.h"
10 TrackAttachment::TrackAttachment(Layout &l):
15 TrackAttachment::~TrackAttachment()
18 track->remove_attachment(*this);
21 void TrackAttachment::attach_to(const TrackIter &t, float o)
24 track->remove_attachment(*this);
30 track->add_attachment(*this);
33 void TrackAttachment::attach_to_closest(float limit)
35 const set<Track *> &tracks = layout.get_all<Track>();
39 for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
40 if(!(*i)->get_type().is_turnout())
43 sn.position = position;
44 sn.rotation = rotation;
45 if((*i)->snap(sn, limit, SNAP_SEGMENT))
47 float d = distance(position, sn.position);
51 closest = sn.position;
59 unsigned nsn = trk->get_n_snap_nodes();
60 for(unsigned i=0; i<nsn; ++i)
62 Snap sn = trk->get_snap_node(i);
63 Angle adiff = abs(wrap_balanced(sn.rotation-rotation));
64 if(adiff<Angle::quarter_turn())
66 TrackIter iter(trk, i);
67 attach_to(iter, find_offset(iter, closest));
73 attach_to(TrackIter(), 0);
76 float TrackAttachment::find_offset(const TrackIter &trk, const Vector &point)
78 float margin = 0.01*layout.get_catalogue().get_scale();
79 Vector epp = trk->get_snap_node(trk.entry()).position;
81 float dist = distance(epp, point);
85 TrackPoint tp = trk->get_point(trk.entry(), offs);
86 float diff = dist-distance(epp, tp.pos);