X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flibr2c2%2Ftrack.cpp;h=b679af783867f72746793f205179f004a9e02baa;hb=f950f5a77714a155e57c868101911e181a617818;hp=2cb95186e6f32237a61b16e092a5eac279a7ba13;hpb=621c5c938d70ba0d155e0eda91a708db0a52c0dc;p=r2c2.git diff --git a/source/libr2c2/track.cpp b/source/libr2c2/track.cpp index 2cb9518..b679af7 100644 --- a/source/libr2c2/track.cpp +++ b/source/libr2c2/track.cpp @@ -12,16 +12,16 @@ using namespace Msp; namespace R2C2 { Track::Track(Layout &l, const TrackType &t): - layout(l), + Object(l), type(t), block(0), - rot(0), slope(0), flex(false), turnout_id(0), sensor_id(0), links(type.get_endpoints().size()), - active_path(0) + active_path(0), + path_changing(false) { if(type.is_turnout()) turnout_id = layout.allocate_turnout_id(); @@ -40,6 +40,14 @@ Track::~Track() layout.remove_track(*this); } +Track *Track::clone(Layout *to_layout) const +{ + Track *track = new Track((to_layout ? *to_layout : layout), type); + track->set_position(position); + track->set_rotation(rotation); + return track; +} + void Track::set_block(Block *b) { if(b && !b->has_track(*this)) @@ -60,16 +68,16 @@ Block &Track::get_block() const void Track::set_position(const Vector &p) { - pos = p; + position = p; } void Track::set_rotation(float r) { - rot = r; - while(rot<0) - rot += M_PI*2; - while(rot>M_PI*2) - rot -= M_PI*2; + rotation = r; + while(rotation<0) + rotation += M_PI*2; + while(rotation>M_PI*2) + rotation -= M_PI*2; } void Track::set_slope(float s) @@ -94,8 +102,8 @@ void Track::check_slope() { Vector epp0 = links[0]->get_endpoint_position(links[0]->get_endpoint_by_link(*this)); Vector epp1 = links[1]->get_endpoint_position(links[1]->get_endpoint_by_link(*this)); - pos.z = epp0.z; - slope = epp1.z-pos.z; + position.z = epp0.z; + slope = epp1.z-position.z; } else { @@ -103,12 +111,12 @@ void Track::check_slope() if(links[0]) { Vector epp = links[0]->get_endpoint_position(links[0]->get_endpoint_by_link(*this)); - pos.z = epp.z; + position.z = epp.z; } else if(links[1]) { Vector epp = links[1]->get_endpoint_position(links[1]->get_endpoint_by_link(*this)); - pos.z = epp.z; + position.z = epp.z; } } } @@ -143,6 +151,7 @@ void Track::set_active_path(unsigned p) if(!(type.get_paths()&(1<
links.size())
+ if(i>=links.size())
throw out_of_range("Track::get_link");
return links[i];
@@ -297,11 +306,11 @@ Track *Track::get_link(unsigned i) const
TrackPoint Track::get_point(unsigned epi, unsigned path, float d) const
{
TrackPoint p = type.get_point(epi, path, d);
- float c = cos(rot);
- float s = sin(rot);
+ float c = cos(rotation);
+ float s = sin(rotation);
- p.pos = Vector(pos.x+c*p.pos.x-s*p.pos.y, pos.y+s*p.pos.x+c*p.pos.y, pos.z);
- p.dir += rot;
+ p.pos = Vector(position.x+c*p.pos.x-s*p.pos.y, position.y+s*p.pos.x+c*p.pos.y, position.z);
+ p.dir += rotation;
if(type.get_endpoints().size()==2)
{
float len = type.get_path_length(path);
@@ -326,11 +335,24 @@ TrackPoint Track::get_point(unsigned epi, float d) const
return get_point(epi, active_path, d);
}
-bool Track::collide_ray(const Vector &start, const Vector &ray)
+TrackPoint Track::get_nearest_point(const Vector &p) const
{
- Vector local_start(start.x-pos.x, start.y-pos.y, start.z-pos.z);
- float c = cos(rot);
- float s = sin(rot);
+ Vector local(p.x-position.x, p.y-position.y, p.z-position.z);
+ float c = cos(rotation);
+ float s = sin(rotation);
+ local = Vector(c*local.x+s*local.y, c*local.y-s*local.x, local.z);
+
+ TrackPoint tp = type.get_nearest_point(local);
+ tp.pos = Vector(position.x+tp.pos.x*c-tp.pos.y*s, position.y+tp.pos.y*c+tp.pos.x*s, position.z+tp.pos.z);
+ tp.dir += rotation;
+ return tp;
+}
+
+bool Track::collide_ray(const Vector &start, const Vector &ray) const
+{
+ Vector local_start(start.x-position.x, start.y-position.y, start.z-position.z);
+ float c = cos(rotation);
+ float s = sin(rotation);
local_start = Vector(c*local_start.x+s*local_start.y, c*local_start.y-s*local_start.x, local_start.z);
Vector local_ray(c*ray.x+s*ray.y, c*ray.y-s*ray.x, ray.z);
@@ -341,8 +363,8 @@ bool Track::collide_ray(const Vector &start, const Vector &ray)
void Track::save(list