-/* $Id$
-
-This file is part of R²C²
-Copyright © 2006-2010 Mikkosoft Productions, Mikko Rasa
-Distributed under the GPL
-*/
-
#include <cmath>
#include "block.h"
#include "catalogue.h"
rot(0),
slope(0),
flex(false),
- turnout_id(type.is_turnout() ? layout.allocate_turnout_id(type.is_double_address()) : 0),
+ turnout_id(0),
sensor_id(0),
links(type.get_endpoints().size()),
active_path(0)
{
+ if(type.is_turnout())
+ turnout_id = layout.allocate_turnout_id();
+
layout.add_track(*this);
if(layout.has_driver())
void Track::set_block(Block *b)
{
if(b && !b->has_track(*this))
- throw InvalidParameterValue("Track is not in the Block");
+ throw logic_error("track not in block");
if(!b && block && block->has_track(*this))
- throw InvalidState("Track is still in a Block");
+ throw logic_error("track still in block");
block = b;
}
Block &Track::get_block() const
{
if(!block)
- throw InvalidState("No Block");
+ throw logic_error("!block");
return *block;
}
-void Track::set_position(const Point &p)
+void Track::set_position(const Vector &p)
{
pos = p;
}
if(links[0] && links[1])
{
- Point epp0 = links[0]->get_endpoint_position(links[0]->get_endpoint_by_link(*this));
- Point epp1 = links[1]->get_endpoint_position(links[1]->get_endpoint_by_link(*this));
+ 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;
}
slope = 0;
if(links[0])
{
- Point epp = links[0]->get_endpoint_position(links[0]->get_endpoint_by_link(*this));
+ Vector epp = links[0]->get_endpoint_position(links[0]->get_endpoint_by_link(*this));
pos.z = epp.z;
}
else if(links[1])
{
- Point epp = links[1]->get_endpoint_position(links[1]->get_endpoint_by_link(*this));
+ Vector epp = links[1]->get_endpoint_position(links[1]->get_endpoint_by_link(*this));
pos.z = epp.z;
}
}
void Track::set_turnout_id(unsigned i)
{
if(!type.is_turnout())
- throw InvalidState("Not a turnout");
+ throw logic_error("not a turnout");
turnout_id = i;
layout.create_blocks(*this);
layout.update_routes();
if(layout.has_driver() && turnout_id)
- {
- layout.get_driver().add_turnout(turnout_id);
- if(type.is_double_address())
- layout.get_driver().add_turnout(turnout_id+1);
- }
+ layout.get_driver().add_turnout(turnout_id, type);
}
void Track::set_sensor_id(unsigned i)
{
if(type.is_turnout())
- throw InvalidState("Can't set sensor on a turnout");
+ throw logic_error("is a turnout");
sensor_id = i;
layout.create_blocks(*this);
void Track::set_active_path(unsigned p)
{
if(!turnout_id)
- throw InvalidState("Not a turnout");
+ throw logic_error("not a turnout");
if(!(type.get_paths()&(1<<p)))
- throw InvalidParameterValue("Invalid path");
+ throw invalid_argument("Track::set_active_path");
- layout.get_driver().set_turnout(turnout_id, p&1);
- if(type.is_double_address())
- layout.get_driver().set_turnout(turnout_id+1, p&2);
- else if(type.get_n_paths()>2)
- active_path = (active_path&1) | (p&2);
+ layout.get_driver().set_turnout(turnout_id, p);
}
int Track::get_endpoint_by_link(Track &other) const
return -1;
}
-Point Track::get_endpoint_position(unsigned epi) const
+Vector Track::get_endpoint_position(unsigned epi) const
{
const vector<TrackType::Endpoint> &eps = type.get_endpoints();
if(epi>=eps.size())
- throw InvalidParameterValue("TrackType::Endpoint index out of range");
+ throw out_of_range("Track::get_endpoint_position");
const TrackType::Endpoint &ep = eps[epi];
float c = cos(rot);
float s = sin(rot);
- Point p(pos.x+c*ep.pos.x-s*ep.pos.y, pos.y+s*ep.pos.x+c*ep.pos.y, pos.z);
+ Vector p(pos.x+c*ep.pos.x-s*ep.pos.y, pos.y+s*ep.pos.x+c*ep.pos.y, pos.z);
if(eps.size()==2 && epi==1)
p.z += slope;
return p;
{
const vector<TrackType::Endpoint> &eps = type.get_endpoints();
if(epi>=eps.size())
- throw InvalidParameterValue("TrackType::Endpoint index out of range");
+ throw out_of_range("Track::get_endpoint_direction");
const TrackType::Endpoint &ep = eps[epi];
for(unsigned i=0; i<eps.size(); ++i)
{
- Point epp = get_endpoint_position(i);
+ Vector epp = get_endpoint_position(i);
for(unsigned j=0; j<other_eps.size(); ++j)
{
if(other.get_link(j))
continue;
- Point epp2 = other.get_endpoint_position(j);
+ Vector epp2 = other.get_endpoint_position(j);
float dx = epp2.x-epp.x;
float dy = epp2.y-epp.y;
float dz = epp2.z-epp.z;
if(!link || (!flex && !other.get_flex()))
{
set_rotation(other.rot+other_eps[j].dir-eps[i].dir+M_PI);
- Point p(epp2.x-(eps[i].pos.x*cos(rot)-eps[i].pos.y*sin(rot)),
+ Vector p(epp2.x-(eps[i].pos.x*cos(rot)-eps[i].pos.y*sin(rot)),
epp2.y-(eps[i].pos.y*cos(rot)+eps[i].pos.x*sin(rot)),
epp2.z);
if(eps.size()==2 && i==1)
links[i] = &other;
other.links[j] = this;
layout.create_blocks(*this);
+
+ signal_link_changed.emit(i, &other);
+ other.signal_link_changed.emit(j, this);
}
return true;
return false;
}
-bool Track::snap(Point &pt, float &d) const
+bool Track::snap(Vector &pt, float &d) const
{
const vector<TrackType::Endpoint> &eps = type.get_endpoints();
for(unsigned i=0; i<eps.size(); ++i)
{
- Point epp = get_endpoint_position(i);
+ Vector epp = get_endpoint_position(i);
float dx = pt.x-epp.x;
float dy = pt.y-epp.y;
if(dx*dx+dy*dy<1e-4)
trk.break_link(*this);
// XXX Creates the blocks twice
layout.create_blocks(*this);
+ signal_link_changed.emit(i-links.begin(), 0);
return;
}
}
Track *Track::get_link(unsigned i) const
{
if(i>links.size())
- throw InvalidParameterValue("Link index out of range");
+ throw out_of_range("Track::get_link");
return links[i];
}
float c = cos(rot);
float s = sin(rot);
- p.pos = Point(pos.x+c*p.pos.x-s*p.pos.y, pos.y+s*p.pos.x+c*p.pos.y, pos.z);
+ 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;
if(type.get_endpoints().size()==2)
{
return get_point(epi, active_path, d);
}
+bool Track::collide_ray(const Vector &start, const Vector &ray)
+{
+ Vector local_start(start.x-pos.x, start.y-pos.y, start.z-pos.z);
+ float c = cos(rot);
+ float s = sin(rot);
+ 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);
+
+ float width = layout.get_catalogue().get_ballast_profile().get_width();
+
+ return type.collide_ray(local_start, local_ray, width);
+}
+
void Track::save(list<DataFile::Statement> &st) const
{
st.push_back((DataFile::Statement("position"), pos.x, pos.y, pos.z));
st.push_back((DataFile::Statement("flex"), true));
}
-void Track::turnout_event(unsigned addr, bool state)
+void Track::turnout_event(unsigned addr, unsigned state)
{
if(!turnout_id)
return;
if(addr==turnout_id)
- active_path = (active_path&2) | (state ? 1 : 0);
- else if(type.is_double_address() && addr==turnout_id+1)
- active_path = (active_path&1) | (state ? 2 : 0);
- else
- return;
-
- signal_path_changed.emit(active_path);
+ {
+ active_path = state;
+ signal_path_changed.emit(active_path);
+ }
}
Track::Loader::Loader(Track &t):
- DataFile::BasicLoader<Track>(t)
+ DataFile::ObjectLoader<Track>(t)
{
add("position", &Loader::position);
add("rotation", &Track::rot);
void Track::Loader::position(float x, float y, float z)
{
- obj.pos = Point(x, y, z);
+ obj.pos = Vector(x, y, z);
}
void Track::Loader::sensor_id(unsigned id)