void Layout3D::set_quality(unsigned q)
{
- quality=q;
+ quality = q;
for(list<Track3D *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
(*i)->set_quality(quality);
}
glLoadIdentity();
double clip[4];
- clip[0]=1;
- clip[1]=0;
- clip[2]=x-size;
- clip[3]=0;
+ clip[0] = 1;
+ clip[1] = 0;
+ clip[2] = x-size;
+ clip[3] = 0;
glClipPlane(GL_CLIP_PLANE0, clip);
glEnable(GL_CLIP_PLANE0);
- clip[0]=-1;
- clip[2]=-(x+size);
+ clip[0] = -1;
+ clip[2] = -(x+size);
glClipPlane(GL_CLIP_PLANE1, clip);
glEnable(GL_CLIP_PLANE1);
- clip[0]=0;
- clip[1]=1;
- clip[2]=y-size;
+ clip[0] = 0;
+ clip[1] = 1;
+ clip[2] = y-size;
glClipPlane(GL_CLIP_PLANE2, clip);
glEnable(GL_CLIP_PLANE2);
- clip[1]=-1;
- clip[2]=-(y+size);
+ clip[1] = -1;
+ clip[2] = -(y+size);
glClipPlane(GL_CLIP_PLANE3, clip);
glEnable(GL_CLIP_PLANE3);
glDisable(GL_CLIP_PLANE3);
GL::render_mode(GL::RENDER);
- Track3D *track=0;
- unsigned track_depth=numeric_limits<unsigned>::max();
+ Track3D *track = 0;
+ unsigned track_depth = numeric_limits<unsigned>::max();
for(vector<GL::SelectRecord>::iterator i=select_buf.begin(); i!=select_buf.end(); ++i)
if(i->min_depth<track_depth)
{
- track=reinterpret_cast<Track3D *>(i->names.back());
- track_depth=i->min_depth;
+ track = reinterpret_cast<Track3D *>(i->names.back());
+ track_depth = i->min_depth;
}
return track;
void Track3D::set_color(const Msp::GL::Color &c)
{
- color=c;
+ color = c;
}
void Track3D::set_quality(unsigned q)
{
- quality=q;
+ quality = q;
build_object();
}
void Track3D::get_bounds(float angle, Point &minp, Point &maxp) const
{
- const Point &pos=track.get_position();
- float rot=track.get_rotation();
+ const Point &pos = track.get_position();
+ float rot = track.get_rotation();
- float c=cos(-angle);
- float s=sin(-angle);
+ float c = cos(-angle);
+ float s = sin(-angle);
- minp.x=maxp.x=c*pos.x-s*pos.y;
- minp.y=maxp.y=s*pos.x+c*pos.y;
+ minp.x = maxp.x = c*pos.x-s*pos.y;
+ minp.y = maxp.y = s*pos.x+c*pos.y;
- float c2=cos(rot-angle);
- float s2=sin(rot-angle);
+ float c2 = cos(rot-angle);
+ float s2 = sin(rot-angle);
for(vector<Point>::const_iterator i=border.begin(); i!=border.end(); ++i)
{
- float x=c*pos.x-s*pos.y + c2*i->x-s2*i->y;
- float y=s*pos.x+c*pos.y + s2*i->x+c2*i->y;
+ float x = c*pos.x-s*pos.y + c2*i->x-s2*i->y;
+ float y = s*pos.x+c*pos.y + s2*i->x+c2*i->y;
- minp.x=min(minp.x, x);
- minp.y=min(minp.y, y);
- maxp.x=max(maxp.x, x);
- maxp.y=max(maxp.y, y);
+ minp.x = min(minp.x, x);
+ minp.y = min(minp.y, y);
+ maxp.x = max(maxp.x, x);
+ maxp.y = max(maxp.y, y);
}
}
{
prepare_render();
- const vector<Endpoint> &endpoints=track.get_type().get_endpoints();
+ const vector<Endpoint> &endpoints = track.get_type().get_endpoints();
for(unsigned i=0; i<endpoints.size(); ++i)
{
- const Endpoint &ep=endpoints[i];
+ const Endpoint &ep = endpoints[i];
GL::set(GL_CULL_FACE, track.get_link(i));
if(track.get_link(i))
glColor4f(0.5, 0, 1, 0.5);
else
glColor4f(1, 0, 0.5, 0.5);
- float c=cos(ep.dir);
- float s=sin(ep.dir);
+ float c = cos(ep.dir);
+ float s = sin(ep.dir);
glBegin(GL_QUADS);
glVertex3f(ep.pos.x-s*0.025, ep.pos.y+c*0.025, 0);
void Track3D::prepare_render() const
{
- const Point &pos=track.get_position();
- float rot=track.get_rotation();
+ const Point &pos = track.get_position();
+ float rot = track.get_rotation();
glPushMatrix();
glTranslatef(pos.x, pos.y, pos.z);
route_seq.clear();
route_seq.resize(track.get_type().get_n_routes());
- const vector<TrackPart> &parts=track.get_type().get_parts();
- unsigned index=0;
+ const vector<TrackPart> &parts = track.get_type().get_parts();
+ unsigned index = 0;
for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
build_part(*i, builder, index);
}
profile.push_back(Point(0, -0.002, 0.012));
profile.push_back(Point(0, 0.002, 0.012));
}
- static unsigned psize=profile.size();
+ static unsigned psize = profile.size();
- unsigned nsegs=(part.radius ? static_cast<unsigned>(part.length*(1<<quality))+1 : 1);
- float plen=part.length;
+ unsigned nsegs = (part.radius ? static_cast<unsigned>(part.length*(1<<quality))+1 : 1);
+ float plen = part.length;
if(part.radius)
- plen*=abs(part.radius);
+ plen *= abs(part.radius);
for(unsigned i=0; i<=nsegs; ++i)
{
- float a=part.dir+(part.radius ? i*plen/nsegs/part.radius : 0);
- float c=cos(a);
- float s=sin(a);
- Point p=part.get_point(i*plen/nsegs);
+ float a = part.dir+(part.radius ? i*plen/nsegs/part.radius : 0);
+ float c = cos(a);
+ float s = sin(a);
+ Point p = part.get_point(i*plen/nsegs);
- for(unsigned j=0; j<profile.size(); ++j)
+ for(unsigned j=0; j<psize; ++j)
{
- unsigned k=j&~1;
- float dy=profile[k+1].y-profile[k].y;
- float dz=profile[k+1].z-profile[k].z;
- float d=sqrt(dy*dy+dz*dz);
+ unsigned k = j&~1;
+ float dy = profile[k+1].y-profile[k].y;
+ float dz = profile[k+1].z-profile[k].z;
+ float d = sqrt(dy*dy+dz*dz);
va_builder.normal(s*dz/d, -c*dz/d, dy/d);
Point v(p.x+c*profile[j].x-s*profile[j].y, p.y+c*profile[j].y+s*profile[j].x, profile[j].z+i*track.get_slope()/nsegs);
route_seq[part.route].push_back(base_index+i*psize+19);
}
- base_index+=(nsegs+1)*psize;
+ base_index += (nsegs+1)*psize;
}
} // namespace Marklin
{
DataFile::load(catalogue, "tracks.dat");
- cat_layout=new Layout(catalogue);
- cat_layout_3d=new Layout3D(*cat_layout);
+ cat_layout = new Layout(catalogue);
+ cat_layout_3d = new Layout3D(*cat_layout);
- const map<unsigned, TrackType *> &ctracks=catalogue.get_tracks();
- unsigned n=0;
+ const map<unsigned, TrackType *> &ctracks = catalogue.get_tracks();
+ unsigned n = 0;
for(map<unsigned, TrackType *>::const_iterator i=ctracks.begin(); i!=ctracks.end(); ++i, ++n)
{
- Track *track=new Track(*i->second);
+ Track *track = new Track(*i->second);
track->set_position(Point((n%11)*0.1-0.5, 0.2-n/11*0.3, 0));
track->set_rotation(M_PI/2);
cat_layout->add_track(*track);
}
- manipulator=new Manipulator(*this);
+ manipulator = new Manipulator(*this);
manipulator->signal_status.connect(sigc::mem_fun(this, &Designer::manipulation_status));
manipulator->signal_done.connect(sigc::mem_fun(this, &Designer::manipulation_done));
- layout=new Layout(catalogue);
- layout_3d=new Layout3D(*layout);
+ layout = new Layout(catalogue);
+ layout_3d = new Layout3D(*layout);
if(argc>1)
{
DataFile::load(*layout, argv[1]);
- const list<Track3D *> <racks=layout_3d->get_tracks();
+ const list<Track3D *> <racks = layout_3d->get_tracks();
for(list<Track3D *>::const_iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
{
if((*i)->get_track().get_sensor_id())
if(!layout->get_base().empty())
{
- base_mesh=new GL::Mesh;
+ base_mesh = new GL::Mesh;
DataFile::load(*base_mesh, layout->get_base());
}
}
- selection=new Selection;
+ selection = new Selection;
manipulator->set_selection(selection);
- measure=new Measure(*this);
+ measure = new Measure(*this);
measure->signal_changed.connect(sigc::mem_fun(this, &Designer::measure_changed));
measure->signal_done.connect(sigc::mem_fun(this, &Designer::measure_done));
}
int Designer::main()
{
- dpy=new Graphics::Display;
- wnd=new Graphics::Window(*dpy, screen_w, screen_h);
- glc=new Graphics::GLContext(*wnd);
+ dpy = new Graphics::Display;
+ wnd = new Graphics::Window(*dpy, screen_w, screen_h);
+ glc = new Graphics::GLContext(*wnd);
wnd->signal_close.connect(sigc::bind(sigc::mem_fun(this, &Designer::exit), 0));
wnd->signal_key_press.connect(sigc::mem_fun(this, &Designer::key_press));
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_CULL_FACE);
- GL::Texture2D *font_tex=new GL::Texture2D;
+ GL::Texture2D *font_tex = new GL::Texture2D;
font_tex->set_min_filter(GL::LINEAR);
font_tex->load_image("dejavu-20.png");
- font=new GL::Font();
+ font = new GL::Font();
font->set_texture(*font_tex);
DataFile::load(*font, "dejavu-20.font");
- mode=SELECT;
+ mode = SELECT;
Application::main();
void Designer::map_pointer_coords(int x, int y, float &gx, float &gy)
{
- float cos_pitch=cos(cam_pitch);
- float sin_pitch=sin(cam_pitch);
- float cos_yaw=cos(cam_yaw);
- float sin_yaw=sin(cam_yaw);
+ float cos_pitch = cos(cam_pitch);
+ float sin_pitch = sin(cam_pitch);
+ float cos_yaw = cos(cam_yaw);
+ float sin_yaw = sin(cam_yaw);
- float rx=sin_yaw*0.55228;
- float ry=-cos_yaw*0.55228;
+ float rx = sin_yaw*0.55228;
+ float ry = -cos_yaw*0.55228;
- float ux=cos_yaw*-sin_pitch*0.41421;
- float uy=sin_yaw*-sin_pitch*0.41421;
- float uz=cos_pitch*0.41421;
+ float ux = cos_yaw*-sin_pitch*0.41421;
+ float uy = sin_yaw*-sin_pitch*0.41421;
+ float uz = cos_pitch*0.41421;
- float xf=static_cast<float>(x)*2/screen_w-1;
- float yf=static_cast<float>(y)*2/screen_h-1;
+ float xf = static_cast<float>(x)*2/screen_w-1;
+ float yf = static_cast<float>(y)*2/screen_h-1;
- float vx=cos_yaw*cos_pitch + xf*rx + yf*ux;
- float vy=sin_yaw*cos_pitch + xf*ry + yf*uy;
- float vz=sin_pitch + yf*uz;
+ float vx = cos_yaw*cos_pitch + xf*rx + yf*ux;
+ float vy = sin_yaw*cos_pitch + xf*ry + yf*uy;
+ float vz = sin_pitch + yf*uz;
- gx=cam_pos.x-vx*cam_pos.z/vz;
- gy=cam_pos.y-vy*cam_pos.z/vz;
+ gx = cam_pos.x-vx*cam_pos.z/vz;
+ gy = cam_pos.y-vy*cam_pos.z/vz;
}
void Designer::tick()
{
- const Msp::Time::TimeStamp t=Msp::Time::now();
- float dt=(t-last_tick)/Msp::Time::sec;
- last_tick=t;
+ const Msp::Time::TimeStamp t = Msp::Time::now();
+ float dt = (t-last_tick)/Msp::Time::sec;
+ last_tick = t;
dpy->tick();
if(move_y)
{
- cam_pos.x+=cos(cam_yaw)*dt*move_y;
- cam_pos.y+=sin(cam_yaw)*dt*move_y;
+ cam_pos.x += cos(cam_yaw)*dt*move_y;
+ cam_pos.y += sin(cam_yaw)*dt*move_y;
}
if(move_x)
{
- cam_pos.x+=sin(cam_yaw)*dt*move_x;
- cam_pos.y+=-cos(cam_yaw)*dt*move_x;
+ cam_pos.x += sin(cam_yaw)*dt*move_x;
+ cam_pos.y += -cos(cam_yaw)*dt*move_x;
}
if(zoom)
{
- cam_pos.x+=cos(cam_yaw)*cos(cam_pitch)*dt*zoom;
- cam_pos.y+=sin(cam_yaw)*cos(cam_pitch)*dt*zoom;
- cam_pos.z+=sin(cam_pitch)*dt*zoom;
+ cam_pos.x += cos(cam_yaw)*cos(cam_pitch)*dt*zoom;
+ cam_pos.y += sin(cam_yaw)*cos(cam_pitch)*dt*zoom;
+ cam_pos.z += sin(cam_pitch)*dt*zoom;
}
if(rotate)
{
- float vx=cos(cam_yaw)*cos(cam_pitch);
- float vy=sin(cam_yaw)*cos(cam_pitch);
- float vz=sin(cam_pitch);
+ float vx = cos(cam_yaw)*cos(cam_pitch);
+ float vy = sin(cam_yaw)*cos(cam_pitch);
+ float vz = sin(cam_pitch);
- float gx=cam_pos.x-vx*cam_pos.z/vz;
- float gy=cam_pos.y-vy*cam_pos.z/vz;
- float d=sqrt(vx*vx+vy*vy)*cam_pos.z/vz;
+ float gx = cam_pos.x-vx*cam_pos.z/vz;
+ float gy = cam_pos.y-vy*cam_pos.z/vz;
+ float d = sqrt(vx*vx+vy*vy)*cam_pos.z/vz;
- cam_yaw+=M_PI*dt*rotate;
+ cam_yaw += M_PI*dt*rotate;
if(cam_yaw>M_PI*2)
- cam_yaw-=M_PI*2;
+ cam_yaw -= M_PI*2;
else if(cam_yaw<0)
- cam_yaw+=M_PI*2;
+ cam_yaw += M_PI*2;
- cam_pos.x=gx+cos(cam_yaw)*d;
- cam_pos.y=gy+sin(cam_yaw)*d;
+ cam_pos.x = gx+cos(cam_yaw)*d;
+ cam_pos.y = gy+sin(cam_yaw)*d;
}
if(pitch)
{
- cam_pitch+=M_PI/2*dt*pitch;
+ cam_pitch += M_PI/2*dt*pitch;
if(cam_pitch>M_PI/12)
- cam_pitch=M_PI/12;
+ cam_pitch = M_PI/12;
else if(cam_pitch<-M_PI/2)
- cam_pitch=-M_PI/2;
+ cam_pitch = -M_PI/2;
}
if(tooltip_timeout && t>tooltip_timeout)
{
- Track3D *t3d=0;
+ Track3D *t3d = 0;
if(mode==CATALOGUE)
- t3d=pick_track(pointer_x, pointer_y);
+ t3d = pick_track(pointer_x, pointer_y);
else
- t3d=pick_track(pointer_x, pointer_y);
+ t3d = pick_track(pointer_x, pointer_y);
if(t3d)
{
- const Track &track=t3d->get_track();
- const TrackType &ttype=track.get_type();
+ const Track &track = t3d->get_track();
+ const TrackType &ttype = track.get_type();
ostringstream ss;
ss.precision(2);
ss<<ttype.get_article_number()<<' '<<ttype.get_description();
ss<<" (turnout "<<track.get_turnout_id()<<')';
else if(track.get_sensor_id())
ss<<" (sensor "<<track.get_sensor_id()<<')';
- tooltip=ss.str();
+ tooltip = ss.str();
move_tooltip(pointer_x, pointer_y);
}
else
- tooltip="";
+ tooltip.clear();
- tooltip_timeout=Msp::Time::TimeStamp();
+ tooltip_timeout = Msp::Time::TimeStamp();
}
render();
void Designer::key_press(unsigned code, unsigned mod, wchar_t ch)
{
- unsigned key=Msp::Input::key_from_sys(code);
+ unsigned key = Msp::Input::key_from_sys(code);
if(mode==INPUT)
{
}
if(key==Msp::Input::KEY_SHIFT_L || key==Msp::Input::KEY_SHIFT_R)
- shift=true;
+ shift = true;
if(key==Msp::Input::KEY_N)
- mode=CATALOGUE;
+ mode = CATALOGUE;
else if(key==Msp::Input::KEY_G)
{
manipulator->start_move();
- mode=MANIPULATE;
+ mode = MANIPULATE;
}
else if(key==Msp::Input::KEY_R)
{
manipulator->start_rotate();
- mode=MANIPULATE;
+ mode = MANIPULATE;
}
else if(key==Msp::Input::KEY_D)
{
manipulator->duplicate();
manipulator->start_move();
- mode=MANIPULATE;
+ mode = MANIPULATE;
}
else if(key==Msp::Input::KEY_W)
{
- input=new ::Input(*this, "Filename");
+ input = new ::Input(*this, "Filename");
input->signal_cancel.connect(sigc::mem_fun(this, &Designer::input_dismiss));
input->signal_accept.connect(sigc::mem_fun(this, &Designer::save_accept));
- mode=INPUT;
+ mode = INPUT;
}
else if(key==Msp::Input::KEY_PLUS)
selection->select_more();
else if(key==Msp::Input::KEY_L && (mod&1))
{
- const set<Track *> &tracks=layout->get_tracks();
- float len=0;
+ const set<Track *> &tracks = layout->get_tracks();
+ float len = 0;
for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
- len+=(*i)->get_type().get_total_length();
+ len += (*i)->get_type().get_total_length();
cout<<"Total length: "<<len<<"m\n";
}
else if(key==Msp::Input::KEY_L)
else if(key==Msp::Input::KEY_M)
{
measure->start();
- mode=MEASURE;
+ mode = MEASURE;
}
else if(key==Msp::Input::KEY_Z)
{
manipulator->start_elevate();
- mode=MANIPULATE;
+ mode = MANIPULATE;
}
else if(key==Msp::Input::KEY_ESC)
{
if(mode==MANIPULATE)
manipulator->cancel();
else if(mode==CATALOGUE)
- mode=SELECT;
+ mode = SELECT;
else
selection->clear();
}
else if(key==Msp::Input::KEY_X)
{
- set<Track *> tracks=selection->get_tracks();
+ set<Track *> tracks = selection->get_tracks();
selection->clear();
for(set<Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
{
}
else if(key==Msp::Input::KEY_F && (mod&1))
{
- const set<Track *> &tracks=selection->get_tracks();
- const set<Track *> <racks=layout->get_tracks();
+ const set<Track *> &tracks = selection->get_tracks();
+ const set<Track *> <racks = layout->get_tracks();
for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
{
(*i)->set_flex(!(*i)->get_flex());
if(*j!=*i)
(*i)->snap_to(**j, true);
- Track3D &t3d=layout_3d->get_track(**i);
+ Track3D &t3d = layout_3d->get_track(**i);
if((*i)->get_flex())
t3d.set_color(GL::Color(1, 0.5, 1));
else
manipulator->even_slope();
else if(key==Msp::Input::KEY_T)
{
- Track *track=selection->get_track();
+ Track *track = selection->get_track();
if(selection->size()==1 && track->get_type().get_n_routes()>1)
{
ostringstream ss;
ss<<track->get_turnout_id();
- input=new ::Input(*this, "Turnout ID", ss.str());
+ input = new ::Input(*this, "Turnout ID", ss.str());
input->signal_cancel.connect(sigc::mem_fun(this, &Designer::input_dismiss));
input->signal_accept.connect(sigc::mem_fun(this, &Designer::turnout_id_accept));
- mode=INPUT;
+ mode = INPUT;
}
}
else if(key==Msp::Input::KEY_S)
{
- const set<Track *> &tracks=selection->get_tracks();
- bool ok=false;
- int id=-1;
+ const set<Track *> &tracks = selection->get_tracks();
+ bool ok = false;
+ int id = -1;
for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
{
if((*i)->get_type().get_n_routes()==1)
- ok=true;
+ ok = true;
if(static_cast<int>((*i)->get_sensor_id())!=id)
{
if(id==-1)
- id=(*i)->get_sensor_id();
+ id = (*i)->get_sensor_id();
else
- id=-2;
+ id = -2;
}
}
if(ok)
ostringstream ss;
if(id>=0)
ss<<id;
- input=new ::Input(*this, "Sensor ID", ss.str());
+ input = new ::Input(*this, "Sensor ID", ss.str());
input->signal_cancel.connect(sigc::mem_fun(this, &Designer::input_dismiss));
input->signal_accept.connect(sigc::mem_fun(this, &Designer::sensor_id_accept));
- mode=INPUT;
+ mode = INPUT;
}
}
else if(key==Msp::Input::KEY_RIGHT)
- rotate=-1;
+ rotate = -1;
else if(key==Msp::Input::KEY_LEFT)
- rotate=1;
+ rotate = 1;
else if(key==Msp::Input::KEY_UP)
- move_y=1;
+ move_y = 1;
else if(key==Msp::Input::KEY_DOWN)
- move_y=-1;
+ move_y = -1;
else if(key==Msp::Input::KEY_INSERT)
- zoom=-1;
+ zoom = -1;
else if(key==Msp::Input::KEY_PGUP)
- zoom=1;
+ zoom = 1;
else if(key==Msp::Input::KEY_HOME)
- pitch=1;
+ pitch = 1;
else if(key==Msp::Input::KEY_END)
- pitch=-1;
+ pitch = -1;
else if(key==Msp::Input::KEY_DELETE)
- move_x=-1;
+ move_x = -1;
else if(key==Msp::Input::KEY_PGDN)
- move_x=1;
+ move_x = 1;
}
void Designer::key_release(unsigned code, unsigned)
{
- unsigned key=Msp::Input::key_from_sys(code);
+ unsigned key = Msp::Input::key_from_sys(code);
if(mode==INPUT)
return;
if(key==Msp::Input::KEY_SHIFT_L || key==Msp::Input::KEY_SHIFT_R)
- shift=false;
+ shift = false;
else if(key==Msp::Input::KEY_RIGHT || key==Msp::Input::KEY_LEFT)
- rotate=0;
+ rotate = 0;
else if(key==Msp::Input::KEY_UP || key==Msp::Input::KEY_DOWN)
- move_y=0;
+ move_y = 0;
else if(key==Msp::Input::KEY_INSERT || key==Msp::Input::KEY_PGUP)
- zoom=0;
+ zoom = 0;
else if(key==Msp::Input::KEY_HOME || key==Msp::Input::KEY_END)
- pitch=0;
+ pitch = 0;
else if(key==Msp::Input::KEY_DELETE || key==Msp::Input::KEY_PGDN)
- move_x=0;
+ move_x = 0;
}
void Designer::button_press(int x, int y, unsigned btn, unsigned)
{
- y=screen_h-y-1;
+ y = screen_h-y-1;
float gx, gy;
map_pointer_coords(x, y, gx, gy);
{
if(btn==1)
{
- Track3D *ctrack=pick_track(x, y);
+ Track3D *ctrack = pick_track(x, y);
if(ctrack)
{
- Track *track=ctrack->get_track().copy();
+ Track *track = ctrack->get_track().copy();
track->set_position(Point(gx, gy, 0));
layout->add_track(*track);
selection->clear();
selection->add_track(track);
- mode=SELECT;
+ mode = SELECT;
}
}
else
- mode=SELECT;
+ mode = SELECT;
}
else if(mode==SELECT)
{
if(btn==1)
{
- Track3D *track=pick_track(x, y);
+ Track3D *track = pick_track(x, y);
if(track)
{
if(!shift)
void Designer::pointer_motion(int x, int y)
{
- y=screen_h-y-1;
+ y = screen_h-y-1;
float gx, gy;
map_pointer_coords(x, y, gx, gy);
if(mode==SELECT || mode==CATALOGUE)
{
- pointer_x=x;
- pointer_y=y;
- tooltip_timeout=Msp::Time::now()+100*Msp::Time::msec;
+ pointer_x = x;
+ pointer_y = y;
+ tooltip_timeout = Msp::Time::now()+100*Msp::Time::msec;
}
if(mode!=INPUT)
{
glTranslatef(tooltip_x, tooltip_y, 0);
glScalef(20, 20, 20);
- float width=font->get_string_width(tooltip);
+ float width = font->get_string_width(tooltip);
glColor4f(0, 0, 0, 0.5);
glBegin(GL_QUADS);
glVertex2f(0, 0);
Track3D *Designer::pick_track(int x, int y)
{
- Layout3D *l=layout_3d;
+ Layout3D *l = layout_3d;
if(mode==CATALOGUE)
- l=cat_layout_3d;
+ l = cat_layout_3d;
- float xx=(static_cast<float>(x-static_cast<int>(screen_w)/2)/screen_h)*0.82843;
- float yy=(static_cast<float>(y)/screen_h-0.5)*0.82843;
- float size=4.0/screen_h*0.82843;
+ float xx = (static_cast<float>(x-static_cast<int>(screen_w)/2)/screen_h)*0.82843;
+ float yy = (static_cast<float>(y)/screen_h-0.5)*0.82843;
+ float size = 4.0/screen_h*0.82843;
project_3d();
apply_camera();
void Designer::manipulation_status(const string &status)
{
- tooltip=status;
+ tooltip = status;
}
void Designer::manipulation_done(bool)
{
- mode=SELECT;
+ mode = SELECT;
}
void Designer::measure_changed()
{
- float pard=measure->get_parallel_distance()*1000;
- float perpd=measure->get_perpendicular_distance()*1000;
- float d=sqrt(pard*pard+perpd*perpd);
- float adiff=measure->get_angle_difference()*180/M_PI;
+ float pard = measure->get_parallel_distance()*1000;
+ float perpd = measure->get_perpendicular_distance()*1000;
+ float d = sqrt(pard*pard+perpd*perpd);
+ float adiff = measure->get_angle_difference()*180/M_PI;
ostringstream ss;
ss.precision(3);
ss<<"Par "<<pard<<"mm - Perp "<<perpd<<"mm - Total "<<d<<"mm - Angle "<<adiff<<"°";
- tooltip=ss.str();
+ tooltip = ss.str();
}
void Designer::measure_done()
{
- mode=SELECT;
+ mode = SELECT;
}
void Designer::move_tooltip(int x, int y)
{
- int w=static_cast<int>(font->get_string_width(tooltip)*20);
- tooltip_x=max(min(static_cast<int>(screen_w)-w, x), 0);
- tooltip_y=max(min(static_cast<int>(screen_h)-20, y), 0);
+ int w = static_cast<int>(font->get_string_width(tooltip)*20);
+ tooltip_x = max(min(static_cast<int>(screen_w)-w, x), 0);
+ tooltip_y = max(min(static_cast<int>(screen_h)-20, y), 0);
}
void Designer::save_accept()
void Designer::turnout_id_accept()
{
- Track *track=selection->get_track();
- unsigned id=lexical_cast<unsigned>(input->get_text());
+ Track *track = selection->get_track();
+ unsigned id = lexical_cast<unsigned>(input->get_text());
track->set_turnout_id(id);
- Track3D &t3d=layout_3d->get_track(*track);
+ Track3D &t3d = layout_3d->get_track(*track);
if(id)
t3d.set_color(GL::Color(0.5, 1, 1));
else
void Designer::sensor_id_accept()
{
- const set<Track *> &tracks=selection->get_tracks();
- unsigned id=lexical_cast<unsigned>(input->get_text());
+ const set<Track *> &tracks = selection->get_tracks();
+ unsigned id = lexical_cast<unsigned>(input->get_text());
for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
{
(*i)->set_sensor_id(id);
- Track3D &t3d=layout_3d->get_track(**i);
+ Track3D &t3d = layout_3d->get_track(**i);
if(id)
t3d.set_color(GL::Color(1, 1, 0.5));
else
void Designer::input_dismiss()
{
delete input;
- input=0;
- mode=SELECT;
+ input = 0;
+ mode = SELECT;
}
{
selection_changed_conn.disconnect();
- selection=s;
+ selection = s;
if(selection)
- selection_changed_conn=selection->signal_changed.connect(sigc::mem_fun(this, &Manipulator::selection_changed));
+ selection_changed_conn = selection->signal_changed.connect(sigc::mem_fun(this, &Manipulator::selection_changed));
selection_changed();
}
if(mode)
cancel();
- move_origin=gpointer;
+ move_origin = gpointer;
- mode=MOVE;
+ mode = MOVE;
}
void Manipulator::start_rotate()
if(mode)
cancel();
- rot_origin=atan2(gpointer.y-center.y, gpointer.x-center.x);
+ rot_origin = atan2(gpointer.y-center.y, gpointer.x-center.x);
- mode=ROTATE;
+ mode = ROTATE;
}
void Manipulator::start_elevate()
if(mode)
cancel();
- elev_origin=pointer_y;
+ elev_origin = pointer_y;
- mode=ELEVATE;
+ mode = ELEVATE;
}
void Manipulator::duplicate()
list<Track *> new_tracks;
for(vector<MTrack>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
{
- Track *track=i->track->copy();
+ Track *track = i->track->copy();
designer.get_layout()->add_track(*track);
new_tracks.push_back(track);
}
if(tracks.empty()) return;
- float z=0;
+ float z = 0;
for(vector<MTrack>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
- z+=i->track->get_position().z+i->track->get_slope()/2;
- z/=tracks.size();
+ z += i->track->get_position().z+i->track->get_slope()/2;
+ z /= tracks.size();
for(vector<MTrack>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
{
- Point p=i->track->get_position();
+ Point p = i->track->get_position();
i->track->set_position(Point(p.x, p.y, z));
i->track->set_slope(0);
}
for(vector<MTrack>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
tracks2.push_back(i->track);
- float total_len=0;
+ float total_len = 0;
list<TrackOrder> order;
- Track *cur=*neighbors.begin();
+ Track *cur = *neighbors.begin();
while(tracks2.size())
{
- bool rev=false;
+ bool rev = false;
for(list<Track *>::iterator i=tracks2.begin(); i!=tracks2.end(); ++i)
{
- const vector<Track *> &links=(*i)->get_links();
+ const vector<Track *> &links = (*i)->get_links();
if(links[0]==cur)
{
- cur=*i;
+ cur = *i;
tracks2.erase(i);
break;
}
else if(links[1]==cur)
{
- cur=*i;
- rev=true;
+ cur = *i;
+ rev = true;
tracks2.erase(i);
break;
}
}
order.push_back(TrackOrder(cur, rev));
- total_len+=cur->get_type().get_total_length();
+ total_len += cur->get_type().get_total_length();
}
- set<Track *>::iterator nb=neighbors.begin();
- int epi=(*nb)->get_endpoint_by_link(*order.front().track);
- float start_z=(*nb)->get_endpoint_position(epi).z;
+ set<Track *>::iterator nb = neighbors.begin();
+ int epi = (*nb)->get_endpoint_by_link(*order.front().track);
+ float start_z = (*nb)->get_endpoint_position(epi).z;
++nb;
- epi=(*nb)->get_endpoint_by_link(*order.back().track);
- float end_z=(*nb)->get_endpoint_position(epi).z;
+ epi = (*nb)->get_endpoint_by_link(*order.back().track);
+ float end_z = (*nb)->get_endpoint_position(epi).z;
if(smooth)
{
- float dir=(end_z>start_z)?1:-1;
- float cur_slope=0;
+ float dir = (end_z>start_z)?1:-1;
+ float cur_slope = 0;
while((end_z-start_z)*dir/total_len>cur_slope+0.025 && order.size()>2)
{
- cur_slope+=0.025;
+ cur_slope += 0.025;
- float dz=order.front().track->get_type().get_total_length()*dir*cur_slope;
+ float dz = order.front().track->get_type().get_total_length()*dir*cur_slope;
set_slope(order.front(), start_z, dz);
- start_z+=dz;
- total_len-=order.front().track->get_type().get_total_length();
+ start_z += dz;
+ total_len -= order.front().track->get_type().get_total_length();
order.erase(order.begin());
- dz=order.back().track->get_type().get_total_length()*dir*cur_slope;
+ dz = order.back().track->get_type().get_total_length()*dir*cur_slope;
set_slope(order.back(), end_z-dz, dz);
- end_z-=dz;
- total_len-=order.back().track->get_type().get_total_length();
+ end_z -= dz;
+ total_len -= order.back().track->get_type().get_total_length();
order.erase(--order.end());
}
}
- float cur_z=start_z;
+ float cur_z = start_z;
for(list<TrackOrder>::iterator i=order.begin(); i!=order.end(); ++i)
{
- float dz=i->track->get_type().get_total_length()*(end_z-start_z)/total_len;
+ float dz = i->track->get_type().get_total_length()*(end_z-start_z)/total_len;
set_slope(*i, cur_z, dz);
- cur_z+=dz;
+ cur_z += dz;
}
for(set<Track *>::iterator i=neighbors.begin(); i!=neighbors.end(); ++i)
{
if(!mode)
return;
- mode=NONE;
+ mode = NONE;
- wrap_pos=center;
+ wrap_pos = center;
for(vector<MTrack>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
{
i->track->set_position(Point(center.x+i->pos.x, center.y+i->pos.y, center.z+i->pos.z));
for(set<Track *>::iterator i=neighbors.begin(); i!=neighbors.end(); ++i)
(*i)->check_slope();
- angle=0;
- wrap_rot=0;
+ angle = 0;
+ wrap_rot = 0;
signal_done.emit(false);
}
cancel();
else if(mode)
{
- mode=NONE;
+ mode = NONE;
update_wrap();
- angle=0;
+ angle = 0;
for(set<Track *>::iterator i=neighbors.begin(); i!=neighbors.end(); ++i)
for(vector<MTrack>::iterator j=tracks.begin(); j!=tracks.end(); ++j)
j->track->break_link(**i);
- const set<Track *> <racks=designer.get_layout()->get_tracks();
+ const set<Track *> <racks = designer.get_layout()->get_tracks();
for(set<Track *>::const_iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
{
- bool ok=true;
+ bool ok = true;
for(vector<MTrack>::iterator j=tracks.begin(); (j!=tracks.end() && ok); ++j)
- ok=(j->track!=*i);
+ ok = (j->track!=*i);
if(!ok) continue;
for(vector<MTrack>::iterator j=tracks.begin(); j!=tracks.end(); ++j)
}
for(vector<MTrack>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
- i->rot=i->track->get_rotation();
+ i->rot = i->track->get_rotation();
update_neighbors();
void Manipulator::pointer_motion(int, int y, float gx, float gy)
{
- pointer_y=y;
- gpointer=Point(gx, gy, 0);
+ pointer_y = y;
+ gpointer = Point(gx, gy, 0);
if(mode==MOVE)
{
Point delta(gpointer.x-move_origin.x, gpointer.y-move_origin.y, 0);
- wrap_pos=Point(center.x+delta.x, center.y+delta.y, center.z);
+ wrap_pos = Point(center.x+delta.x, center.y+delta.y, center.z);
for(vector<MTrack>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
{
i->track->set_position(Point(wrap_pos.x+i->pos.x, wrap_pos.y+i->pos.y, wrap_pos.z+i->pos.z));
i->track->set_rotation(i->rot);
}
- const set<Track *> <racks=designer.get_layout()->get_tracks();
- MTrack *snapped=0;
+ const set<Track *> <racks = designer.get_layout()->get_tracks();
+ MTrack *snapped = 0;
for(set<Track *>::const_iterator i=ltracks.begin(); (i!=ltracks.end() && !snapped); ++i)
{
- bool ok=true;
+ bool ok = true;
for(vector<MTrack>::iterator j=tracks.begin(); (j!=tracks.end() && ok); ++j)
- ok=(j->track!=*i);
+ ok = (j->track!=*i);
if(!ok) continue;
for(vector<MTrack>::iterator j=tracks.begin(); (j!=tracks.end() && !snapped); ++j)
if(j->track->snap_to(**i, false))
- snapped=&*j;
+ snapped = &*j;
}
if(snapped)
{
- float da=snapped->track->get_rotation()-snapped->rot;
- float c=cos(da);
- float s=sin(da);
- const Point &sp=snapped->track->get_position();
+ float da = snapped->track->get_rotation()-snapped->rot;
+ float c = cos(da);
+ float s = sin(da);
+ const Point &sp = snapped->track->get_position();
for(vector<MTrack>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
{
if(&*i==snapped)
}
else if(mode==ROTATE)
{
- float a=atan2(gpointer.y-center.y, gpointer.x-center.x);
- angle+=a-rot_origin;
- rot_origin=a;
+ float a = atan2(gpointer.y-center.y, gpointer.x-center.x);
+ angle += a-rot_origin;
+ rot_origin = a;
- wrap_rot=angle;
+ wrap_rot = angle;
for(vector<MTrack>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
{
- float c=cos(angle);
- float s=sin(angle);
+ float c = cos(angle);
+ float s = sin(angle);
i->track->set_position(Point(center.x+c*i->pos.x-s*i->pos.y, center.y+s*i->pos.x+c*i->pos.y, center.z*i->pos.z));
i->track->set_rotation(angle+i->rot);
}
}
else if(mode==ELEVATE)
{
- float dz=(y-elev_origin)/1000.;
+ float dz = (y-elev_origin)/1000.;
ostringstream ss;
ss.precision(3);
ss<<"Elevation: "<<dz*1000<<"mm ("<<(center.z+dz)*1000<<"mm)";
signal_status.emit(ss.str());
- wrap_pos.z=center.z+dz;
+ wrap_pos.z = center.z+dz;
for(vector<MTrack>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
i->track->set_position(Point(center.x+i->pos.x, center.y+i->pos.y, center.z+i->pos.z+dz));
tracks.clear();
if(selection)
{
- const set<Track *> &stracks=selection->get_tracks();
+ const set<Track *> &stracks = selection->get_tracks();
tracks.insert(tracks.end(), stracks.begin(), stracks.end());
}
void Manipulator::update_wrap()
{
wrap.clear();
- float min_x=0, max_x=0;
- float min_y=0, max_y=0;
+ float min_x = 0, max_x = 0;
+ float min_y = 0, max_y = 0;
for(vector<MTrack>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
{
- Track3D &t3d=designer.get_layout_3d()->get_track(*i->track);
+ Track3D &t3d = designer.get_layout_3d()->get_track(*i->track);
TrackWrap tw;
- float min_area=100;
+ float min_area = 100;
for(float a=0; a<M_PI; a+=0.01)
{
Point minp, maxp;
t3d.get_bounds(a, minp, maxp);
- float area=(maxp.x-minp.x)*(maxp.y-minp.y);
+ float area = (maxp.x-minp.x)*(maxp.y-minp.y);
if(area<min_area)
{
- float c=cos(a);
- float s=sin(a);
- float x=(minp.x+maxp.x)/2;
- float y=(minp.y+maxp.y)/2;
- tw.pos=Point(c*x-s*y, s*x+c*y, (minp.z+maxp.z)/2);
- tw.rot=a;
- tw.width=maxp.x-minp.x+0.01;
- tw.height=maxp.y-minp.y+0.01;
-
- min_area=area;
+ float c = cos(a);
+ float s = sin(a);
+ float x = (minp.x+maxp.x)/2;
+ float y = (minp.y+maxp.y)/2;
+ tw.pos = Point(c*x-s*y, s*x+c*y, (minp.z+maxp.z)/2);
+ tw.rot = a;
+ tw.width = maxp.x-minp.x+0.01;
+ tw.height = maxp.y-minp.y+0.01;
+
+ min_area = area;
}
}
if(i==tracks.begin())
{
- min_x=max_x=tw.pos.x;
- min_y=max_y=tw.pos.y;
+ min_x = max_x = tw.pos.x;
+ min_y = max_y = tw.pos.y;
}
else
{
- min_x=min(min_x, tw.pos.x);
- max_x=max(max_x, tw.pos.x);
- min_y=min(min_y, tw.pos.y);
- max_y=max(max_y, tw.pos.y);
+ min_x = min(min_x, tw.pos.x);
+ max_x = max(max_x, tw.pos.x);
+ min_y = min(min_y, tw.pos.y);
+ max_y = max(max_y, tw.pos.y);
}
wrap.push_back(tw);
}
- center=Point((min_x+max_x)/2, (min_y+max_y)/2, 0);
- wrap_pos=center;
- wrap_rot=0;
+ center = Point((min_x+max_x)/2, (min_y+max_y)/2, 0);
+ wrap_pos = center;
+ wrap_rot = 0;
for(vector<MTrack>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
{
- const Point &tp=i->track->get_position();
- i->pos=Point(tp.x-center.x, tp.y-center.y, tp.z);
+ const Point &tp = i->track->get_position();
+ i->pos = Point(tp.x-center.x, tp.y-center.y, tp.z);
}
for(list<TrackWrap>::iterator i=wrap.begin(); i!=wrap.end(); ++i)
{
- i->pos.x-=center.x;
- i->pos.y-=center.y;
+ i->pos.x -= center.x;
+ i->pos.y -= center.y;
}
}
neighbors.clear();
for(vector<MTrack>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
{
- const vector<Track *> &links=i->track->get_links();
+ const vector<Track *> &links = i->track->get_links();
for(vector<Track *>::const_iterator j=links.begin(); j!=links.end(); ++j)
{
if(!*j)
if(neighbors.count(*j))
continue;
- bool ok=true;
+ bool ok = true;
for(vector<MTrack>::iterator k=tracks.begin(); (k!=tracks.end() && ok); ++k)
- ok=(k->track!=*j);
+ ok = (k->track!=*j);
if(ok)
neighbors.insert(*j);
void Manipulator::set_slope(TrackOrder &track, float z, float dz)
{
- const Point &p=track.track->get_position();
+ const Point &p = track.track->get_position();
if(track.rev)
{
track.track->set_position(Point(p.x, p.y, z+dz));
void Measure::start()
{
- state=STARTING;
+ state = STARTING;
}
void Measure::snap_to_tracks(Point &pt, float &dir)
{
- const set<Track *> <racks=designer.get_layout()->get_tracks();
+ const set<Track *> <racks = designer.get_layout()->get_tracks();
for(set<Track *>::const_iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
if((*i)->snap(pt, dir))
return;
if(btn==1)
{
- spoint=Point(gx, gy, 0);
- sdir=0;
+ spoint = Point(gx, gy, 0);
+ sdir = 0;
snap_to_tracks(spoint, sdir);
- state=ACTIVE;
+ state = ACTIVE;
}
else if(btn==3)
{
if(state==ACTIVE)
- state=STARTING;
+ state = STARTING;
else
{
- state=NONE;
+ state = NONE;
signal_done.emit();
}
}
if(!state)
return;
- pointer=Point(gx, gy, 0);
- float dir=sdir;
+ pointer = Point(gx, gy, 0);
+ float dir = sdir;
snap_to_tracks(pointer, dir);
if(state!=STARTING)
{
Point delta(pointer.x-spoint.x, pointer.y-spoint.y, 0);
- float c=cos(sdir);
- float s=sin(sdir);
+ float c = cos(sdir);
+ float s = sin(sdir);
- par_dist=delta.x*c+delta.y*s;
- perp_dist=delta.x*s-delta.y*c;
+ par_dist = delta.x*c+delta.y*s;
+ perp_dist = delta.x*s-delta.y*c;
- adiff=dir-sdir+M_PI;
+ adiff = dir-sdir+M_PI;
while(adiff<-M_PI)
- adiff+=M_PI*2;
+ adiff += M_PI*2;
while(adiff>M_PI)
- adiff-=M_PI*2;
+ adiff -= M_PI*2;
signal_changed.emit();
}
glBegin(GL_QUAD_STRIP);
for(unsigned i=0; i<=16; ++i)
{
- float x=cos(i*M_PI/8)*0.005;
- float y=sin(i*M_PI/8)*0.005;
+ float x = cos(i*M_PI/8)*0.005;
+ float y = sin(i*M_PI/8)*0.005;
glVertex3f(x, y, 0);
glVertex3f(x, y, 0.01);
}
if(state==ACTIVE)
{
- float c=cos(sdir);
- float s=sin(sdir);
+ float c = cos(sdir);
+ float s = sin(sdir);
glBegin(GL_QUAD_STRIP);
glVertex3f(0, 0, 0);
glVertex3f(0, 0, 0.01);
set<Track *> new_tracks;
for(set<Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
{
- const vector<Track *> &links=(*i)->get_links();
+ const vector<Track *> &links = (*i)->get_links();
for(vector<Track *>::const_iterator j=links.begin(); j!=links.end(); ++j)
if(*j)
new_tracks.insert(*j);
}
- bool changed=false;
+ bool changed = false;
for(set<Track *>::iterator i=new_tracks.begin(); i!=new_tracks.end(); ++i)
if(tracks.insert(*i).second)
- changed=true;
+ changed = true;
if(changed)
signal_changed.emit();
void Selection::select_linked()
{
- bool changed=false;
+ bool changed = false;
list<Track *> queue(tracks.begin(), tracks.end());
while(!queue.empty())
{
- Track *track=queue.front();
+ Track *track = queue.front();
queue.erase(queue.begin());
- const vector<Track *> &links=track->get_links();
+ const vector<Track *> &links = track->get_links();
for(vector<Track *>::const_iterator j=links.begin(); j!=links.end(); ++j)
if(*j && tracks.insert(*j).second)
{
queue.push_back(*j);
- changed=true;
+ changed = true;
}
}
for(set<Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
{
- const vector<Track *> &links=(*i)->get_links();
+ const vector<Track *> &links = (*i)->get_links();
for(vector<Track *>::const_iterator j=links.begin(); j!=links.end(); ++j)
if(*j && tracks.insert(*j).second)
- changed=true;
+ changed = true;
}
if(changed)
simulate(false)
{
string res;
- bool debug=false;
- string device="/dev/ttyS0";
- unsigned quality=4;
+ bool debug = false;
+ string device = "/dev/ttyS0";
+ unsigned quality = 4;
GetOpt getopt;
getopt.add_option('r', "resolution", res, GetOpt::REQUIRED_ARG);
{
if(RegMatch m=Regex("([1-9][0-9]*)x([1-9][0-9]*)").match(res))
{
- screen_w=lexical_cast<unsigned>(m[1].str);
- screen_h=lexical_cast<unsigned>(m[2].str);
+ screen_w = lexical_cast<unsigned>(m[1].str);
+ screen_h = lexical_cast<unsigned>(m[2].str);
}
else
throw UsageError("Invalid resolution");
DataFile::load(catalogue, "tracks.dat");
DataFile::load(catalogue, "locos.dat");
- const vector<string> &args=getopt.get_args();
+ const vector<string> &args = getopt.get_args();
if(args.empty())
throw UsageError("No layout given");
DataFile::load(layout, args.front());
- trfc_mgr=new TrafficManager(control, layout);
+ trfc_mgr = new TrafficManager(control, layout);
trfc_mgr->signal_block_reserved.connect(sigc::mem_fun(this, &Engineer::block_reserved));
if(FS::exists("engineer.state"))
DataFile::load(*trfc_mgr, "engineer.state");
- const map<unsigned, Sensor *> &sensors=control.get_sensors();
+ const map<unsigned, Sensor *> &sensors = control.get_sensors();
for(map<unsigned, Sensor *>::const_iterator i=sensors.begin(); i!=sensors.end(); ++i)
i->second->signal_state_changed.connect(sigc::bind(sigc::mem_fun(this, &Engineer::sensor_event), i->second));
void Engineer::add_train()
{
- train_prop=new TrainProperties(*this, ui_res, 0);
+ train_prop = new TrainProperties(*this, ui_res, 0);
root->add(*train_prop);
train_prop->signal_ok.connect(sigc::mem_fun(this, &Engineer::dismiss_train_prop));
- train_prop_stale=false;
+ train_prop_stale = false;
train_prop->set_visible(true);
}
Train *Engineer::add_train(const LocoType &type, unsigned addr)
{
- Locomotive *loco=new Locomotive(type, control, addr);
- Train *train=new Train(*trfc_mgr, *loco);
+ Locomotive *loco = new Locomotive(type, control, addr);
+ Train *train = new Train(*trfc_mgr, *loco);
train->set_name(format("Train %d", trfc_mgr->get_trains().size()));
- TrainPanel *tpanel=new TrainPanel(*this, ui_res, *train);
+ TrainPanel *tpanel = new TrainPanel(*this, ui_res, *train);
root->add(*tpanel);
- int y=main_panel->get_geometry().y;
+ int y = main_panel->get_geometry().y;
for(list<TrainPanel *>::iterator i=train_panels.begin(); i!=train_panels.end(); ++i)
- y-=(*i)->get_geometry().h;
+ y -= (*i)->get_geometry().h;
tpanel->set_position(0, y-tpanel->get_geometry().h);
train_panels.push_back(tpanel);
tpanel->set_visible(true);
void Engineer::place_train(Train &train)
{
- placing_train=&train;
- placing_block=0;
+ placing_train = &train;
+ placing_block = 0;
main_panel->set_status_text("Select location");
}
int Engineer::main()
{
- dpy=new Graphics::Display;
+ dpy = new Graphics::Display;
Graphics::WindowOptions wopt;
- wopt.width=screen_w;
- wopt.height=screen_h;
- wopt.fullscreen=fullscreen;
- wnd=new Graphics::Window(*dpy, wopt);
+ wopt.width = screen_w;
+ wopt.height = screen_h;
+ wopt.fullscreen = fullscreen;
+ wnd = new Graphics::Window(*dpy, wopt);
Graphics::GLOptions glopt;
- //glopt.multisample=4;
- glc=new Graphics::GLContext(*wnd, glopt);
+ //glopt.multisample = 4;
+ glc = new Graphics::GLContext(*wnd, glopt);
wnd->signal_close.connect(sigc::bind(sigc::mem_fun(this, &Engineer::exit), 0));
wnd->signal_button_press.connect(sigc::mem_fun(this, &Engineer::button_press));
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
DataFile::load(ui_res, "marklin.res");
- root=new GLtk::Root(ui_res, *wnd);
+ root = new GLtk::Root(ui_res, *wnd);
root->set_visible(true);
- list<GL::Texture *> texs=ui_res.get_list<GL::Texture>();
+ list<GL::Texture *> texs = ui_res.get_list<GL::Texture>();
for(list<GL::Texture *>::iterator i=texs.begin(); i!=texs.end(); ++i)
{
(*i)->set_min_filter(GL::NEAREST);
(*i)->set_mag_filter(GL::NEAREST);
}
- main_panel=new MainPanel(*this, ui_res);
+ main_panel = new MainPanel(*this, ui_res);
root->add(*main_panel);
main_panel->set_position(0, screen_h-main_panel->get_geometry().h);
main_panel->set_visible(true);
- const list<Train *> &trains=trfc_mgr->get_trains();
- int y=main_panel->get_geometry().y;
+ const list<Train *> &trains = trfc_mgr->get_trains();
+ int y = main_panel->get_geometry().y;
for(list<Train *>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
{
- TrainPanel *tpanel=new TrainPanel(*this, ui_res, **i);
+ TrainPanel *tpanel = new TrainPanel(*this, ui_res, **i);
root->add(*tpanel);
tpanel->set_position(0, y-tpanel->get_geometry().h);
train_panels.push_back(tpanel);
tpanel->set_visible(true);
- y-=tpanel->get_geometry().h;
+ y -= tpanel->get_geometry().h;
}
wnd->show();
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
float params[4];
- params[0]=0;
- params[1]=-0.2;
- params[2]=1;
- params[3]=0;
+ params[0] = 0;
+ params[1] = -0.2;
+ params[2] = 1;
+ params[3] = 0;
glLightfv(GL_LIGHT0, GL_POSITION, params);
}
glDisable(GL_LIGHTING);
glColor4f(1, 1, 1, 1);
- const list<Track3D *> <racks=layout_3d.get_tracks();
+ const list<Track3D *> <racks = layout_3d.get_tracks();
for(list<Track3D *>::const_iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
{
- Track &track=(*i)->get_track();
+ Track &track = (*i)->get_track();
if(track.get_turnout_id())
{
- Turnout &trnt=control.get_turnout(track.get_turnout_id());
+ Turnout &trnt = control.get_turnout(track.get_turnout_id());
(*i)->render_route(trnt.get_route());
}
else
{
GL::push_matrix();
- const Marklin::Block::Endpoint &bep=placing_block->get_endpoints()[placing_entry];
- float rot=bep.track->get_endpoint_direction(bep.track_ep);
- Point pos=bep.track->get_endpoint_position(bep.track_ep);
+ const Marklin::Block::Endpoint &bep = placing_block->get_endpoints()[placing_entry];
+ float rot = bep.track->get_endpoint_direction(bep.track_ep);
+ Point pos = bep.track->get_endpoint_position(bep.track_ep);
GL::translate(pos.x, pos.y, pos.z+0.03);
GL::rotate(rot*180/M_PI+180, 0, 0, 1);
GL::pop_matrix();
}
- const list<Train *> &trains=trfc_mgr->get_trains();
+ const list<Train *> &trains = trfc_mgr->get_trains();
for(list<Train *>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
{
GL::PushMatrix _push;
- const Point &tp=(*i)->get_position();
+ const Point &tp = (*i)->get_position();
GL::translate(tp.x, tp.y, 0.02);
GL::Immediate imm((GL::COLOR4_UBYTE, GL::VERTEX2));
imm.color(0.8f, 0.8f, 1.0f);
if(train_prop_stale)
{
delete train_prop;
- train_prop=0;
+ train_prop = 0;
}
glc->swap_buffers();
set_block_color(*placing_block, GL::Color(1, 1, 1));
placing_train->place(placing_block, placing_entry);
- placing_train=0;
+ placing_train = 0;
main_panel->set_status_text(string());
}
else if(btn==3)
{
- const vector<Block::Endpoint> &endpoints=placing_block->get_endpoints();
- placing_entry=(placing_entry+1)%endpoints.size();
+ const vector<Block::Endpoint> &endpoints = placing_block->get_endpoints();
+ placing_entry = (placing_entry+1)%endpoints.size();
}
}
else
{
- Track3D *track=pick_track(x, screen_h-y-1);
+ Track3D *track = pick_track(x, screen_h-y-1);
if(track)
{
if(unsigned tid=track->get_track().get_turnout_id())
{
- Turnout &turnout=control.get_turnout(tid);
+ Turnout &turnout = control.get_turnout(tid);
try
{
turnout.set_route((turnout.get_route()+1)%track->get_track().get_type().get_n_routes());
{
if(unsigned sid=track->get_track().get_sensor_id())
{
- Sensor &sensor=control.get_sensor(sid);
+ Sensor &sensor = control.get_sensor(sid);
control.signal_sensor_event.emit(sid, !sensor.get_state());
}
}
{
if(placing_train)
{
- Track3D *track=pick_track(x, screen_h-y-1);
+ Track3D *track = pick_track(x, screen_h-y-1);
if(track && placing_train)
{
- Block &block=trfc_mgr->get_block_by_track(track->get_track());
+ Block &block = trfc_mgr->get_block_by_track(track->get_track());
if(&block!=placing_block)
{
if(placing_block)
reset_block_color(*placing_block);
- placing_block=█
- placing_entry=0;
+ placing_block = █
+ placing_entry = 0;
set_block_color(*placing_block, GL::Color(0.5, 1, 0.7));
}
}
void Engineer::view_all()
{
- const list<Track3D *> &tracks=layout_3d.get_tracks();
+ const list<Track3D *> &tracks = layout_3d.get_tracks();
- cam_rot=0;
- float best_height=-1;
- float mid_x=0;
- float mid_y=0;
+ cam_rot = 0;
+ float best_height = -1;
+ float mid_x = 0;
+ float mid_y = 0;
for(float angle=0; angle<M_PI; angle+=0.01)
{
- float min_x=0;
- float max_x=0;
- float min_y=0;
- float max_y=0;
+ float min_x = 0;
+ float max_x = 0;
+ float min_y = 0;
+ float max_y = 0;
for(list<Track3D *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
{
Point minp, maxp;
(*i)->get_bounds(angle, minp, maxp);
- min_x=min(min_x, minp.x);
- max_x=max(max_x, maxp.x);
- min_y=min(min_y, minp.y);
- max_y=max(max_y, maxp.y);
+ min_x = min(min_x, minp.x);
+ max_x = max(max_x, maxp.x);
+ min_y = min(min_y, minp.y);
+ max_y = max(max_y, maxp.y);
}
- float width=max_x-min_x;
- float height=max_y-min_y;
- height=max(height, width);
+ float width = max_x-min_x;
+ float height = max_y-min_y;
+ height = max(height, width);
if(height<best_height || best_height<0)
{
- cam_rot=angle;
- best_height=height;
- mid_x=(min_x+max_x)/2;
- mid_y=(min_y+max_y)/2;
+ cam_rot = angle;
+ best_height = height;
+ mid_x = (min_x+max_x)/2;
+ mid_y = (min_y+max_y)/2;
}
}
- float c=cos(cam_rot);
- float s=sin(cam_rot);
- cam_pos.x=c*mid_x-s*mid_y;
- cam_pos.y=s*mid_x+c*mid_y;
- cam_pos.z=max(best_height*1.05/0.82843, 0.15);
+ float c = cos(cam_rot);
+ float s = sin(cam_rot);
+ cam_pos.x = c*mid_x-s*mid_y;
+ cam_pos.y = s*mid_x+c*mid_y;
+ cam_pos.z = max(best_height*1.05/0.82843, 0.15);
}
void Engineer::set_block_color(const Block &block, const GL::Color &color)
{
- const set<Track *> &tracks=block.get_tracks();
+ const set<Track *> &tracks = block.get_tracks();
for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
layout_3d.get_track(**i).set_color(color);
}
{
if(unsigned sid=block.get_sensor_id())
{
- Sensor &sensor=control.get_sensor(sid);
+ Sensor &sensor = control.get_sensor(sid);
if(sensor.get_state())
{
set_block_color(block, GL::Color(1, 0.5, 0.3));
void Engineer::sensor_event(bool, Sensor *sensor)
{
- const list<Block *> &blocks=trfc_mgr->get_blocks();
+ const list<Block *> &blocks = trfc_mgr->get_blocks();
for(list<Block *>::const_iterator i=blocks.begin(); i!=blocks.end(); ++i)
if((*i)->get_sensor_id()==sensor->get_address())
reset_block_color(**i);
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
- float offset=200.0/screen_w*0.055228;
+ float offset = 200.0/screen_w*0.055228;
glFrustum(-0.055228-offset, 0.055228-offset, -0.041421, 0.041421, 0.1, 10);
glMatrixMode(GL_MODELVIEW);
}
Track3D *Engineer::pick_track(int x, int y)
{
- float xx=(static_cast<float>(x-static_cast<int>(screen_w)/2-100)/screen_h)*0.82843;
- float yy=(static_cast<float>(y)/screen_h-0.5)*0.82843;
- float size=4.0/screen_h*0.82843;
+ float xx = (static_cast<float>(x-static_cast<int>(screen_w)/2-100)/screen_h)*0.82843;
+ float yy = (static_cast<float>(y)/screen_h-0.5)*0.82843;
+ float size = 4.0/screen_h*0.82843;
project_3d();
glLoadIdentity();
void Engineer::dismiss_train_prop()
{
- train_prop_stale=true;
+ train_prop_stale = true;
}
Application::RegApp<Engineer> Engineer::reg;
GLtk::Button *btn;
- const map<unsigned, string> &funcs=train.get_locomotive().get_type().get_functions();
- unsigned x=10;
+ const map<unsigned, string> &funcs = train.get_locomotive().get_type().get_functions();
+ unsigned x = 10;
for(map<unsigned, string>::const_iterator i=funcs.begin(); i!=funcs.end(); ++i, x+=40)
{
- string fname=i->second;
- fname[0]=toupper(fname[0]);
+ string fname = i->second;
+ fname[0] = toupper(fname[0]);
add(*(btn=new GLtk::Button(res, fname)));
btn->set_geometry(GLtk::Geometry(x, 68, 40, 24));
btn->signal_clicked.connect(sigc::bind(sigc::mem_fun(this, &TrainPanel::func_clicked), i->first));
- GLtk::Indicator *ind=new GLtk::Indicator(res);
+ GLtk::Indicator *ind = new GLtk::Indicator(res);
add(*ind);
ind->set_geometry(GLtk::Geometry(x, 92, 40, 12));
ind->set_active(train.get_locomotive().get_function(i->first));
- ind_funcs[i->first]=ind;
+ ind_funcs[i->first] = ind;
}
train.get_locomotive().signal_function_changed.connect(sigc::mem_fun(this, &TrainPanel::loco_function_changed));
void TrainPanel::loco_function_changed(unsigned func, bool value)
{
- map<unsigned, GLtk::Indicator *>::iterator i=ind_funcs.find(func);
+ map<unsigned, GLtk::Indicator *>::iterator i = ind_funcs.find(func);
if(i!=ind_funcs.end())
i->second->set_active(value);
}
add(*(drp_type=new GLtk::Dropdown(res)));
drp_type->set_geometry(GLtk::Geometry(60, geom.h-30, geom.w-70, 20));
- const map<unsigned, LocoType *> &locos=engineer.get_catalogue().get_locomotives();
+ const map<unsigned, LocoType *> &locos = engineer.get_catalogue().get_locomotives();
for(map<unsigned, LocoType *>::const_iterator i=locos.begin(); i!=locos.end(); ++i)
drp_type->append(format("%d %s", i->second->get_article_number(), i->second->get_name()));
{
if(!train)
{
- const map<unsigned, LocoType *> &locos=engineer.get_catalogue().get_locomotives();
- map<unsigned, LocoType *>::const_iterator i=locos.begin();
+ const map<unsigned, LocoType *> &locos = engineer.get_catalogue().get_locomotives();
+ map<unsigned, LocoType *>::const_iterator i = locos.begin();
advance(i, drp_type->get_selected_index());
- train=engineer.add_train(*i->second, lexical_cast<unsigned>(ent_addr->get_text()));
+ train = engineer.add_train(*i->second, lexical_cast<unsigned>(ent_addr->get_text()));
}
train->set_name(ent_name->get_text());
namespace Marklin {
+unsigned Block::next_id = 1;
+
Block::Block(TrafficManager &tm, Track &start):
trfc_mgr(tm),
id(next_id++),
while(!queue.empty())
{
- Track *track=queue.front();
+ Track *track = queue.front();
queue.erase(queue.begin());
- const vector<Track *> &links=track->get_links();
+ const vector<Track *> &links = track->get_links();
for(unsigned i=0; i<links.size(); ++i)
if(links[i] && !tracks.count(links[i]))
{
for(unsigned i=0; i<endpoints.size(); ++i)
{
- unsigned route=1<<i;
- endpoints[i].routes|=route;
+ unsigned route = 1<<i;
+ endpoints[i].routes |= route;
set<Track *> visited;
find_routes(*endpoints[i].track, endpoints[i].track_ep, route, visited);
}
if(epi>=endpoints.size())
throw InvalidParameterValue("Endpoint index out of range");
- const Endpoint &ep=endpoints[epi];
- Track *track=ep.track;
- unsigned track_ep=ep.track_ep;
+ const Endpoint &ep = endpoints[epi];
+ Track *track = ep.track;
+ unsigned track_ep = ep.track_ep;
if(len)
- *len=0;
+ *len = 0;
while(1)
{
- unsigned cur_route=0;
- unsigned tid=track->get_turnout_id();
+ unsigned cur_route = 0;
+ unsigned tid = track->get_turnout_id();
if(tid)
{
- Turnout &turnout=trfc_mgr.get_control().get_turnout(tid);
- cur_route=turnout.get_route();
+ Turnout &turnout = trfc_mgr.get_control().get_turnout(tid);
+ cur_route = turnout.get_route();
}
if(len)
- *len+=track->get_type().get_route_length(cur_route);
+ *len += track->get_type().get_route_length(cur_route);
- int other_ep=track->traverse(track_ep, cur_route);
+ int other_ep = track->traverse(track_ep, cur_route);
if(other_ep<0)
return -1;
if(endpoints[i].track==track && endpoints[i].track_ep==static_cast<unsigned>(other_ep))
return i;
- Track *next=track->get_link(other_ep);
+ Track *next = track->get_link(other_ep);
if(tracks.count(next)==0)
return -1;
- track_ep=next->get_endpoint_by_link(*track);
- track=next;
+ track_ep = next->get_endpoint_by_link(*track);
+ track = next;
}
}
for(vector<Endpoint>::iterator j=other.endpoints.begin(); j!=other.endpoints.end(); ++j)
if(j->track==i->track->get_link(i->track_ep) && j->track->get_link(j->track_ep)==i->track && !j->link)
{
- i->link=&other;
- j->link=this;
+ i->link = &other;
+ j->link = this;
}
}
}
{
if(!t || !train)
{
- train=t;
+ train = t;
trfc_mgr.signal_block_reserved.emit(*this, train);
return true;
}
{
visited.insert(&track);
- const vector<Marklin::Endpoint> &eps=track.get_type().get_endpoints();
+ const vector<Marklin::Endpoint> &eps = track.get_type().get_endpoints();
for(unsigned i=0; i<eps.size(); ++i)
{
if(i==track_ep) continue;
- Track *link=track.get_link(i);
+ Track *link = track.get_link(i);
if(!link) continue;
if(visited.count(link)) continue;
if(!(eps[i].routes&eps[track_ep].routes)) continue;
{
for(vector<Endpoint>::iterator j=endpoints.begin(); j!=endpoints.end(); ++j)
if(j->track==&track && j->track_ep==i)
- j->routes|=route;
+ j->routes |= route;
}
}
}
-unsigned Block::next_id=1;
-
Block::Endpoint::Endpoint(Track *t, unsigned e):
track(t),
if(obj.locos.count(art_nr))
throw Exception("Duplicate locomotive number");
- RefPtr<LocoType> loco=new LocoType(art_nr);
+ RefPtr<LocoType> loco = new LocoType(art_nr);
load_sub(*loco);
- obj.locos[art_nr]=loco.release();
+ obj.locos[art_nr] = loco.release();
}
void Catalogue::Loader::track(unsigned art_nr)
if(obj.tracks.count(art_nr))
throw Exception("Duplicate track number");
- RefPtr<TrackType> trk=new TrackType(art_nr);
+ RefPtr<TrackType> trk = new TrackType(art_nr);
load_sub(*trk);
- obj.tracks[art_nr]=trk.release();
+ obj.tracks[art_nr] = trk.release();
}
} // namespace Marklin
void Control::open(const string &dev)
{
- serial_fd=::open(dev.c_str(), O_RDWR);
+ serial_fd = ::open(dev.c_str(), O_RDWR);
if(serial_fd<0)
throw Exception("Couldn't open serial port\n");
termios attr;
tcgetattr(serial_fd, &attr);
cfmakeraw(&attr);
- attr.c_cflag|=CSTOPB;
+ attr.c_cflag |= CSTOPB;
- bool ok=false;
- bool p50=false;
+ bool ok = false;
+ bool p50 = false;
for(unsigned i=0; baud[i]; i+=2)
{
cfsetospeed(&attr, baud[i+1]);
write(serial_fd, "\xC4", 1);
- pollfd pfd={serial_fd, POLLIN, 0};
+ pollfd pfd = { serial_fd, POLLIN, 0 };
if(poll(&pfd, 1, 500)>0)
{
cout<<"IB detected at "<<baud[i]<<" bits/s\n";
char buf[2];
- p50=(read(serial_fd, buf, 2)==2);
- ok=true;
+ p50 = (read(serial_fd, buf, 2)==2);
+ ok = true;
break;
}
}
void Control::set_debug(bool d)
{
- debug=d;
+ debug = d;
}
void Control::set_power(bool p)
{
- power=p;
+ power = p;
if(power)
command(CMD_POWER_ON);
else
void Control::add_turnout(Turnout &t)
{
- turnouts[t.get_address()]=&t;
+ turnouts[t.get_address()] = &t;
}
Turnout &Control::get_turnout(unsigned id) const
{
- map<unsigned, Turnout *>::const_iterator i=turnouts.find(id);
+ map<unsigned, Turnout *>::const_iterator i = turnouts.find(id);
if(i==turnouts.end())
throw KeyError("Unknown turnout");
void Control::add_locomotive(Locomotive &l)
{
- locomotives[l.get_address()]=&l;
+ locomotives[l.get_address()] = &l;
}
Locomotive &Control::get_locomotive(unsigned id) const
{
- map<unsigned, Locomotive *>::const_iterator i=locomotives.find(id);
+ map<unsigned, Locomotive *>::const_iterator i = locomotives.find(id);
if(i==locomotives.end())
throw KeyError("Unknown locomotive");
void Control::add_sensor(Sensor &s)
{
- sensors[s.get_address()]=&s;
- poll_sensors=true;
+ sensors[s.get_address()] = &s;
+ poll_sensors = true;
}
Sensor &Control::get_sensor(unsigned id) const
{
- map<unsigned, Sensor *>::const_iterator i=sensors.find(id);
+ map<unsigned, Sensor *>::const_iterator i = sensors.find(id);
if(i==sensors.end())
throw KeyError("Unknown sensor");
void Control::tick()
{
- const Time::TimeStamp t=Time::now();
+ const Time::TimeStamp t = Time::now();
for(map<unsigned, Sensor *>::const_iterator i=sensors.begin(); i!=sensors.end(); ++i)
i->second->tick();
if(t>next_event_query)
{
- next_event_query=t+200*Time::msec;
+ next_event_query = t+200*Time::msec;
command(CMD_EVENT).signal_done.connect(sigc::mem_fun(this, &Control::event_query_done));
}
if(poll_sensors)
{
- unsigned max_addr=(--sensors.end())->first;
+ unsigned max_addr = (--sensors.end())->first;
unsigned char data[2];
- data[0]=0;
- data[1]=(max_addr+7)/8;
+ data[0] = 0;
+ data[1] = (max_addr+7)/8;
command(CMD_SENSOR_PARAM_SET, data, 2);
command(CMD_SENSOR_REPORT);
- poll_sensors=false;
+ poll_sensors = false;
}
if(!queue.empty() && queue.front().is_sent())
{
- pollfd pfd={serial_fd, POLLIN, 0};
+ pollfd pfd = { serial_fd, POLLIN, 0 };
if(poll(&pfd, 1, 0)>0)
{
- Reply reply=Reply::read(serial_fd, queue.front().get_command());
+ Reply reply = Reply::read(serial_fd, queue.front().get_command());
if(debug)
cout<<"R: "<<reply<<'\n';
void Control::status_done(const Reply &reply)
{
- power=((reply.get_data()[0]&0x08)!=0);
+ power = ((reply.get_data()[0]&0x08)!=0);
signal_power_event.emit(power);
}
void Control::event_query_done(const Reply &reply)
{
- const unsigned char *data=reply.get_data();
+ const unsigned char *data = reply.get_data();
if(data[0]&0x01)
command(CMD_EVENT_LOK);
if(data[0]&0x20)
void Control::turnout_event_done(const Reply &reply)
{
- const unsigned char *data=reply.get_data();
- unsigned count=data[0];
+ const unsigned char *data = reply.get_data();
+ unsigned count = data[0];
for(unsigned i=0; i<count; ++i)
{
- unsigned addr=(data[i*2+1])+((data[i*2+2]&7)<<8);
- bool status=!(data[i*2+2]&0x80);
+ unsigned addr = (data[i*2+1])+((data[i*2+2]&7)<<8);
+ bool status = !(data[i*2+2]&0x80);
cout<<"Turnout "<<addr<<", status "<<status<<'\n';
signal_turnout_event.emit(addr, status);
}
void Control::sensor_event_done(const Reply &reply)
{
- const unsigned char *data=reply.get_data();
+ const unsigned char *data = reply.get_data();
for(unsigned i=0; data[i]; i+=3)
{
- unsigned module=data[i];
+ unsigned module = data[i];
cout<<"S88 module "<<module<<", status ";
for(unsigned j=0; j<16; ++j)
void Layout::Loader::track(unsigned art_nr)
{
- TrackType &type=obj.catalogue.get_track(art_nr);
+ TrackType &type = obj.catalogue.get_track(art_nr);
- RefPtr<Track> trk=new Track(type);
+ RefPtr<Track> trk = new Track(type);
load_sub(*trk);
obj.add_track(*trk.release());
}
void Locomotive::set_speed(unsigned spd)
{
- spd=min(spd, 14U);
+ spd = min(spd, 14U);
signal_speed_changing.emit(spd);
- speed=spd;
+ speed = spd;
send_command(false);
signal_speed_changed.emit(speed);
}
else
{
- reverse=rev;
+ reverse = rev;
send_command(false);
}
}
void Locomotive::set_function(unsigned func, bool state)
{
if(state)
- funcs|=1<<func;
+ funcs |= 1<<func;
else
- funcs&=~(1<<func);
+ funcs &= ~(1<<func);
send_command(true);
void Locomotive::refresh_status()
{
unsigned char data[2];
- data[0]=addr&0xFF;
- data[1]=(addr>>8)&0xFF;
+ data[0] = addr&0xFF;
+ data[1] = (addr>>8)&0xFF;
control.command(CMD_LOK_STATUS, data, 2).signal_done.connect(sigc::mem_fun(this, &Locomotive::status_reply));
}
void Locomotive::send_command(bool setf)
{
unsigned char data[4];
- data[0]=addr&0xFF;
- data[1]=(addr>>8)&0xFF;
+ data[0] = addr&0xFF;
+ data[1] = (addr>>8)&0xFF;
if(speed==0)
- data[2]=0;
+ data[2] = 0;
else if(speed==1)
- data[2]=2;
+ data[2] = 2;
else
- data[2]=(speed*19-18)/2;
+ data[2] = (speed*19-18)/2;
- data[3]=(reverse ? 0 : 0x20) | ((funcs&1) ? 0x10 : 0);
+ data[3] = (reverse ? 0 : 0x20) | ((funcs&1) ? 0x10 : 0);
if(setf)
{
- data[3]|=0x80;
+ data[3] |= 0x80;
for(unsigned i=0; i<4; ++i)
if((funcs>>i)&2)
- data[3]|=(1<<i);
+ data[3] |= (1<<i);
}
control.command(CMD_LOK, data, 4);
}
{
if(reply.get_error()==ERR_NO_ERROR)
{
- const unsigned char *data=reply.get_data();
+ const unsigned char *data = reply.get_data();
if(data[0]<=1)
- speed=0;
+ speed = 0;
else
- speed=data[0]*2/19+1;
+ speed = data[0]*2/19+1;
- reverse=(data[1]&0x20) ? false : true;
- funcs=(data[1]&0xF)<<1;
+ reverse = (data[1]&0x20) ? false : true;
+ funcs = (data[1]&0xF)<<1;
if(data[1]&0x10)
- funcs|=1;
+ funcs |= 1;
for(unsigned i=0; i<5; ++i)
signal_function_changed.emit(i, (funcs>>i)&1);
bool Locomotive::reverse_timeout()
{
- reverse=!reverse;
+ reverse = !reverse;
send_command(true);
return false;
}
void LocoType::Loader::function(unsigned i, const string &f)
{
- obj.funcs[i]=f;
+ obj.funcs[i] = f;
}
} // namespace Marklin
unsigned read_all(int fd, char *buf, unsigned size)
{
- unsigned pos=0;
+ unsigned pos = 0;
while(pos<size)
- pos+=read(fd, buf+pos, size-pos);
+ pos += read(fd, buf+pos, size-pos);
return pos;
}
{
Reply result;
- char *data=reinterpret_cast<char *>(result.data);
+ char *data = reinterpret_cast<char *>(result.data);
if(cmd==CMD_EVENT)
{
for(unsigned i=0; i<3; ++i)
{
- result.len+=read_all(fd, data+i, 1);
+ result.len += read_all(fd, data+i, 1);
if(!(result.data[i]&0x80))
break;
}
{
for(unsigned i=0;; i+=5)
{
- result.len+=read_all(fd, data+i, 1);
+ result.len += read_all(fd, data+i, 1);
if(result.data[i]&0x80)
break;
- result.len+=read_all(fd, data+i+1, 4);
+ result.len += read_all(fd, data+i+1, 4);
}
}
else if(cmd==CMD_EVENT_TURNOUT)
{
- result.len+=read_all(fd, data, 1);
- result.len+=read_all(fd, data+1, result.data[0]*2);
+ result.len += read_all(fd, data, 1);
+ result.len += read_all(fd, data+1, result.data[0]*2);
}
else if(cmd==CMD_EVENT_SENSOR)
{
for(unsigned i=0;; i+=3)
{
- result.len+=read_all(fd, data+i, 1);
+ result.len += read_all(fd, data+i, 1);
if(result.data[i]==0)
break;
- result.len+=read_all(fd, data+i+1, 2);
+ result.len += read_all(fd, data+i+1, 2);
}
}
else
{
- bool expect_errcode=(cmd!=CMD_STATUS);
+ bool expect_errcode = (cmd!=CMD_STATUS);
- unsigned expected_bytes=0;
+ unsigned expected_bytes = 0;
if(cmd==CMD_STATUS || cmd==CMD_FUNC_STATUS || cmd==CMD_TURNOUT_STATUS)
- expected_bytes=1;
+ expected_bytes = 1;
if(cmd==CMD_SENSOR_STATUS || cmd==CMD_TURNOUT_GROUP_STATUS)
- expected_bytes=2;
+ expected_bytes = 2;
if(cmd==CMD_LOK_STATUS)
- expected_bytes=3;
+ expected_bytes = 3;
if(cmd==CMD_LOK_CONFIG)
- expected_bytes=4;
+ expected_bytes = 4;
if(expect_errcode)
{
char c;
read_all(fd, &c, 1);
- result.err=static_cast<Error>(c);
+ result.err = static_cast<Error>(c);
}
if(result.err==ERR_NO_ERROR)
- result.len+=read_all(fd, data, expected_bytes);
+ result.len += read_all(fd, data, expected_bytes);
}
return result;
/* $Id$
This file is part of the MSP Märklin suite
-Copyright © 2006-2008 Mikkosoft Productions, Mikko Rasa
+Copyright © 2007-2008 Mikkosoft Productions, Mikko Rasa
Distributed under the GPL
*/
{
if(s)
{
- off_timeout=Time::TimeStamp();
+ off_timeout = Time::TimeStamp();
if(s!=state)
{
- state=s;
+ state = s;
signal_state_changed.emit(state);
}
}
else
- off_timeout=Time::now()+0.5*Time::sec;
+ off_timeout = Time::now()+0.5*Time::sec;
}
}
{
if(off_timeout)
{
- const Time::TimeStamp t=Time::now();
+ const Time::TimeStamp t = Time::now();
if(t>off_timeout)
{
- off_timeout=Time::TimeStamp();
+ off_timeout = Time::TimeStamp();
if(state)
{
- state=false;
+ state = false;
signal_state_changed.emit(state);
}
}
void Track::set_position(const Point &p)
{
- pos=p;
+ pos = p;
}
void Track::set_rotation(float r)
{
- rot=r;
+ rot = r;
while(rot<0)
- rot+=M_PI*2;
+ rot += M_PI*2;
while(rot>M_PI*2)
- rot-=M_PI*2;
+ rot -= M_PI*2;
}
void Track::set_slope(float s)
{
if(links.size()!=2) return;
- slope=s;
+ slope = s;
}
void Track::set_flex(bool f)
{
- flex=f;
+ flex = f;
}
void Track::check_slope()
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));
- pos.z=epp0.z;
- slope=epp1.z-pos.z;
+ 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));
+ pos.z = epp0.z;
+ slope = epp1.z-pos.z;
}
else
{
- slope=0;
+ slope = 0;
if(links[0])
{
- Point epp=links[0]->get_endpoint_position(links[0]->get_endpoint_by_link(*this));
- pos.z=epp.z;
+ Point 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));
- pos.z=epp.z;
+ Point epp = links[1]->get_endpoint_position(links[1]->get_endpoint_by_link(*this));
+ pos.z = epp.z;
}
}
}
void Track::set_turnout_id(unsigned i)
{
- turnout_id=i;
+ turnout_id = i;
}
void Track::set_sensor_id(unsigned i)
{
- sensor_id=i;
+ sensor_id = i;
}
int Track::get_endpoint_by_link(const Track &other) const
Point Track::get_endpoint_position(unsigned epi) const
{
- const vector<Endpoint> &eps=type.get_endpoints();
+ const vector<Endpoint> &eps = type.get_endpoints();
if(epi>=eps.size())
throw InvalidParameterValue("Endpoint index out of range");
- const Endpoint &ep=eps[epi];
+ const Endpoint &ep = eps[epi];
- float c=cos(rot);
- float s=sin(rot);
+ 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);
if(eps.size()==2 && epi==1)
- p.z+=slope;
+ p.z += slope;
return p;
}
float Track::get_endpoint_direction(unsigned epi) const
{
- const vector<Endpoint> &eps=type.get_endpoints();
+ const vector<Endpoint> &eps = type.get_endpoints();
if(epi>=eps.size())
throw InvalidParameterValue("Endpoint index out of range");
- const Endpoint &ep=eps[epi];
+ const Endpoint &ep = eps[epi];
return rot+ep.dir;
}
bool Track::snap_to(Track &other, bool link)
{
- float limit=(link && !flex) ? 1e-6 : 1e-4;
- const vector<Endpoint> &eps=type.get_endpoints();
- const vector<Endpoint> &other_eps=other.get_type().get_endpoints();
+ float limit = (link && !flex) ? 1e-6 : 1e-4;
+ const vector<Endpoint> &eps = type.get_endpoints();
+ const vector<Endpoint> &other_eps = other.get_type().get_endpoints();
for(unsigned i=0; i<eps.size(); ++i)
{
- Point epp=get_endpoint_position(i);
+ Point 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);
- float dx=epp2.x-epp.x;
- float dy=epp2.y-epp.y;
+ Point epp2 = other.get_endpoint_position(j);
+ float dx = epp2.x-epp.x;
+ float dy = epp2.y-epp.y;
if(dx*dx+dy*dy<limit)
{
set_rotation(other.rot+other_eps[j].dir-eps[i].dir+M_PI);
{
if(links[i])
break_link(*links[i]);
- links[i]=&other;
- other.links[j]=this;
+ links[i] = &other;
+ other.links[j] = this;
}
return true;
bool Track::snap(Point &pt, float &d) const
{
- const vector<Endpoint> &eps=type.get_endpoints();
+ const vector<Endpoint> &eps = type.get_endpoints();
for(unsigned i=0; i<eps.size(); ++i)
{
- Point epp=get_endpoint_position(i);
- float dx=pt.x-epp.x;
- float dy=pt.y-epp.y;
+ Point epp = get_endpoint_position(i);
+ float dx = pt.x-epp.x;
+ float dy = pt.y-epp.y;
if(dx*dx+dy*dy<1e-4)
{
- pt=epp;
- d=rot+eps[i].dir;
+ pt = epp;
+ d = rot+eps[i].dir;
return true;
}
}
for(vector<Track *>::iterator i=links.begin(); i!=links.end(); ++i)
if(*i==&trk)
{
- *i=0;
+ *i = 0;
trk.break_link(*this);
return;
}
for(vector<Track *>::iterator i=links.begin(); i!=links.end(); ++i)
if(Track *trk=*i)
{
- *i=0;
+ *i = 0;
trk->break_link(*this);
}
}
int Track::traverse(unsigned i, unsigned route) const
{
- const vector<Endpoint> &eps=type.get_endpoints();
+ const vector<Endpoint> &eps = type.get_endpoints();
if(i>=eps.size())
throw InvalidParameterValue("Endpoint index out of range");
- const Endpoint &ep=eps[i];
+ const Endpoint &ep = eps[i];
if(ep.routes&(1<<route))
{
Point Track::get_point(unsigned epi, unsigned route, float d) const
{
- const vector<Endpoint> &eps=type.get_endpoints();
+ const vector<Endpoint> &eps = type.get_endpoints();
if(epi>=eps.size())
throw InvalidParameterValue("Endpoint index out of range");
- float x=eps[epi].pos.x;
- float y=eps[epi].pos.y;
+ float x = eps[epi].pos.x;
+ float y = eps[epi].pos.y;
- const vector<TrackPart> &parts=type.get_parts();
- const TrackPart *last_part=0;
+ const vector<TrackPart> &parts = type.get_parts();
+ const TrackPart *last_part = 0;
while(1)
{
for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
i->collect_endpoints(part_eps);
for(unsigned j=0; j<part_eps.size(); ++j)
{
- float dx=part_eps[j].pos.x-x;
- float dy=part_eps[j].pos.y-y;
+ float dx = part_eps[j].pos.x-x;
+ float dy = part_eps[j].pos.y-y;
if(dx*dx+dy*dy<1e-6)
{
- float plen=i->length;
+ float plen = i->length;
if(i->radius)
- plen*=abs(i->radius);
+ plen *= abs(i->radius);
if(d<plen)
{
if(j==1)
- d=plen-d;
- Point p=i->get_point(d);
- float c=cos(rot);
- float s=sin(rot);
+ d = plen-d;
+ Point p = i->get_point(d);
+ float c = cos(rot);
+ float s = sin(rot);
return Point(pos.x+c*p.x-s*p.y, pos.y+c*p.y+s*p.x);
}
else if(part_eps.size()>1)
{
- d-=plen;
- x=part_eps[1-j].pos.x;
- y=part_eps[1-j].pos.y;
- last_part=&*i;
- i=parts.begin();
+ d -= plen;
+ x = part_eps[1-j].pos.x;
+ y = part_eps[1-j].pos.y;
+ last_part = &*i;
+ i = parts.begin();
break;
}
else
Track *Track::copy() const
{
- Track *trk=new Track(type);
+ Track *trk = new Track(type);
trk->set_position(pos);
trk->set_rotation(rot);
trk->set_slope(slope);
void Track::Loader::position(float x, float y, float z)
{
- obj.pos=Point(x, y, z);
+ obj.pos = Point(x, y, z);
}
} // namespace Marklin
;
else if(radius)
{
- float a=((radius<0) ? -length : length);
- Point p=get_point(length*abs(radius));
+ float a = ((radius<0) ? -length : length);
+ Point p = get_point(length*abs(radius));
eps.push_back(Endpoint(p.x, p.y, dir+a, 1<<route));
}
else
{
if(radius)
{
- float a=d/radius;
- float c=cos(a);
- float s=sin(a);
- float rx=radius*sin(dir);
- float ry=-radius*cos(dir);
+ float a = d/radius;
+ float c = cos(a);
+ float s = sin(a);
+ float rx = radius*sin(dir);
+ float ry = -radius*cos(dir);
return Point(pos.x+c*rx-s*ry-rx, pos.y+c*ry+s*rx-ry);
}
else
{
if(obj.radius)
{
- obj.length*=M_PI/180;
- obj.radius/=1000;
+ obj.length *= M_PI/180;
+ obj.radius /= 1000;
}
else
- obj.length/=1000;
+ obj.length /= 1000;
- obj.pos.x/=1000;
- obj.pos.y/=1000;
- obj.dir*=M_PI/180;
+ obj.pos.x /= 1000;
+ obj.pos.y /= 1000;
+ obj.dir *= M_PI/180;
}
void TrackPart::Loader::start(float x, float y, float d)
{
- obj.pos=Point(x, y);
- obj.dir=d;
+ obj.pos = Point(x, y);
+ obj.dir = d;
}
} // namespace Marklin
float TrackType::get_route_length(int r) const
{
- float len=0;
+ float len = 0;
for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
if(r<0 || i->route==static_cast<unsigned>(r))
{
- float l=i->length;
+ float l = i->length;
if(i->radius)
- l*=abs(i->radius);
- len+=l;
+ l *= abs(i->radius);
+ len += l;
}
return len;
}
unsigned TrackType::get_n_routes() const
{
- unsigned n=1;
+ unsigned n = 1;
for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
if(i->route>=n)
- n=i->route+1;
+ n = i->route+1;
return n;
}
for(vector<Endpoint>::iterator i=endpoints.begin(); i!=endpoints.end();)
{
- bool rm=false;
+ bool rm = false;
for(vector<Endpoint>::iterator j=i+1; j!=endpoints.end();)
{
- float dx=i->pos.x-j->pos.x;
- float dy=i->pos.y-j->pos.y;
+ float dx = i->pos.x-j->pos.x;
+ float dy = i->pos.y-j->pos.y;
if(dx*dx+dy*dy<0.0001)
{
- float da=i->dir-j->dir;
+ float da = i->dir-j->dir;
if(da<-M_PI)
- da+=M_PI*2;
+ da += M_PI*2;
if(da>M_PI)
- da-=M_PI*2;
+ da -= M_PI*2;
if(da<-3.1 || da>3.1)
- rm=true;
- i->routes|=j->routes;
- j=endpoints.erase(j);
+ rm = true;
+ i->routes |= j->routes;
+ j = endpoints.erase(j);
}
else
++j;
}
if(rm)
- i=endpoints.erase(i);
+ i = endpoints.erase(i);
else
++i;
}
control(c),
layout(l)
{
- const set<Track *> &tracks=layout.get_tracks();
+ const set<Track *> &tracks = layout.get_tracks();
set<Track *> used_tracks;
for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
if(used_tracks.count(*i)==0)
{
- Block *block=new Block(*this, **i);
+ Block *block = new Block(*this, **i);
blocks.push_back(block);
used_tracks.insert(block->get_tracks().begin(), block->get_tracks().end());
}
{
for(list<Block *>::const_iterator i=blocks.begin(); i!=blocks.end(); ++i)
{
- const set<Track *> &tracks=(*i)->get_tracks();
+ const set<Track *> &tracks = (*i)->get_tracks();
if(tracks.count(const_cast<Track *>(&t)))
return **i;
}
void TrafficManager::tick()
{
- Time::TimeStamp t=Time::now();
+ Time::TimeStamp t = Time::now();
Time::TimeDelta dt;
if(last_tick)
- dt=t-last_tick;
- last_tick=t;
+ dt = t-last_tick;
+ last_tick = t;
for(list<Train *>::iterator i=trains.begin(); i!=trains.end(); ++i)
(*i)->tick(t, dt);
DataFile::Writer writer(out);
for(list<Train *>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
{
- const Locomotive &loco=(*i)->get_locomotive();
+ const Locomotive &loco = (*i)->get_locomotive();
DataFile::Statement st("train");
st.append(loco.get_type().get_article_number());
st.append(loco.get_address());
void TrafficManager::Loader::train(unsigned art_nr, unsigned addr)
{
- Locomotive *loco=new Locomotive(obj.layout.get_catalogue().get_locomotive(art_nr), obj.control, addr);
- Train *trn=new Train(obj, *loco);
+ Locomotive *loco = new Locomotive(obj.layout.get_catalogue().get_locomotive(art_nr), obj.control, addr);
+ Train *trn = new Train(obj, *loco);
load_sub(*trn);
}
{
trfc_mgr.add_train(this);
- const map<unsigned, Sensor *> &sensors=trfc_mgr.get_control().get_sensors();
+ const map<unsigned, Sensor *> &sensors = trfc_mgr.get_control().get_sensors();
for(map<unsigned, Sensor *>::const_iterator i=sensors.begin(); i!=sensors.end(); ++i)
i->second->signal_state_changed.connect(sigc::bind(sigc::mem_fun(this, &Train::sensor_event), i->second));
- const map<unsigned, Turnout *> &turnouts=trfc_mgr.get_control().get_turnouts();
+ const map<unsigned, Turnout *> &turnouts = trfc_mgr.get_control().get_turnouts();
for(map<unsigned, Turnout *>::const_iterator i=turnouts.begin(); i!=turnouts.end(); ++i)
{
i->second->signal_route_changing.connect(sigc::bind(sigc::mem_fun(this, &Train::turnout_route_changing), i->second));
void Train::set_name(const string &n)
{
- name=n;
+ name = n;
signal_name_changed.emit(name);
}
void Train::set_speed(unsigned speed)
{
if(!target_speed && speed)
- travel_speed=static_cast<int>(round(speed*speed_scale*87*3.6/5))*5;
+ travel_speed = static_cast<int>(round(speed*speed_scale*87*3.6/5))*5;
- target_speed=speed;
+ target_speed = speed;
if(!target_speed)
{
// XXX We might roll onto a new sensor and get confused - should delay freeing blocks a bit
reserve_more();
update_speed();
- pure_speed=false;
+ pure_speed = false;
}
void Train::place(Block *block, unsigned entry)
for(list<BlockRef>::iterator i=rsv_blocks.begin(); i!=rsv_blocks.end();)
{
i->block->reserve(0);
- i=rsv_blocks.erase(i);
+ i = rsv_blocks.erase(i);
}
for(list<BlockRef>::iterator i=cur_blocks.begin(); i!=cur_blocks.end();)
{
i->block->reserve(0);
- i=cur_blocks.erase(i);
+ i = cur_blocks.erase(i);
}
if(!block->reserve(this))
bool Train::free_block(Block *block)
{
- unsigned nsens=0;
+ unsigned nsens = 0;
for(list<BlockRef>::iterator i=rsv_blocks.begin(); i!=rsv_blocks.end(); ++i)
{
if(i->block==block)
while(i!=rsv_blocks.end())
{
i->block->reserve(0);
- i=rsv_blocks.erase(i);
+ i = rsv_blocks.erase(i);
}
update_speed();
return true;
if(cur_track)
{
- unsigned route=0;
+ unsigned route = 0;
if(cur_track->get_turnout_id())
- route=trfc_mgr.get_control().get_turnout(cur_track->get_turnout_id()).get_route();
+ route = trfc_mgr.get_control().get_turnout(cur_track->get_turnout_id()).get_route();
- offset+=speed_scale*loco.get_speed()*(dt/Time::sec);
+ offset += speed_scale*loco.get_speed()*(dt/Time::sec);
if(offset>cur_track->get_type().get_route_length(route))
{
- int out=cur_track->traverse(cur_track_ep, route);
+ int out = cur_track->traverse(cur_track_ep, route);
if(out>=0)
{
- Track *next=cur_track->get_link(out);
+ Track *next = cur_track->get_link(out);
if(next)
- cur_track_ep=next->get_endpoint_by_link(*cur_track);
- cur_track=next;
- offset=0;
+ cur_track_ep = next->get_endpoint_by_link(*cur_track);
+ cur_track = next;
+ offset = 0;
}
else
- cur_track=0;
+ cur_track = 0;
}
if(cur_track)
- pos=cur_track->get_point(cur_track_ep, route, offset);
+ pos = cur_track->get_point(cur_track_ep, route, offset);
}
}
void Train::sensor_event(bool state, Sensor *sensor)
{
- unsigned addr=sensor->get_address();
+ unsigned addr = sensor->get_address();
if(state)
{
if(i!=rsv_blocks.begin())
{
- float travel_time_secs=(Time::now()-last_entry_time)/Time::sec;
- travel_speed=static_cast<int>(round(travel_dist/travel_time_secs*87*3.6/5))*5;
+ float travel_time_secs = (Time::now()-last_entry_time)/Time::sec;
+ travel_speed = static_cast<int>(round(travel_dist/travel_time_secs*87*3.6/5))*5;
if(pure_speed)
{
- float weight=loco.get_speed()*travel_dist;
+ float weight = loco.get_speed()*travel_dist;
if(weight)
{
- weight*=weight;
- float scale=travel_dist/travel_time_secs/loco.get_speed();
+ weight *= weight;
+ float scale = travel_dist/travel_time_secs/loco.get_speed();
cout<<"Updating speed_scale: "<<speed_scale<<'x'<<speed_scale_weight<<" + "<<scale<<'x'<<weight<<'\n';
- speed_scale=(speed_scale*speed_scale_weight+scale*weight)/(speed_scale_weight+weight);
- speed_scale_weight+=weight;
+ speed_scale = (speed_scale*speed_scale_weight+scale*weight)/(speed_scale_weight+weight);
+ speed_scale_weight += weight;
cout<<" Result: "<<speed_scale<<'x'<<speed_scale_weight<<'\n';
}
}
- travel_dist=0;
+ travel_dist = 0;
float block_len;
for(list<BlockRef>::iterator j=rsv_blocks.begin(); j!=i; ++j)
{
j->block->traverse(j->entry, &block_len);
- travel_dist+=block_len;
+ travel_dist += block_len;
}
- last_entry_time=Time::now();
- pure_speed=true;
+ last_entry_time = Time::now();
+ pure_speed = true;
cur_blocks.splice(cur_blocks.end(), rsv_blocks, rsv_blocks.begin(), i);
}
void Train::turnout_route_changing(unsigned, Turnout *turnout)
{
- unsigned tid=turnout->get_address();
+ unsigned tid = turnout->get_address();
for(list<BlockRef>::const_iterator i=cur_blocks.begin(); i!=cur_blocks.end(); ++i)
if(i->block->get_turnout_id()==tid)
throw TurnoutBusy(this);
- unsigned nsens=0;
+ unsigned nsens = 0;
for(list<BlockRef>::const_iterator i=rsv_blocks.begin(); i!=rsv_blocks.end(); ++i)
{
if(i->block->get_turnout_id()==tid)
void Train::turnout_route_changed(unsigned, Turnout *turnout)
{
- unsigned tid=turnout->get_address();
+ unsigned tid = turnout->get_address();
for(list<BlockRef>::iterator i=rsv_blocks.begin(); i!=rsv_blocks.end(); ++i)
if(i->block->get_turnout_id()==tid)
{
while(i!=rsv_blocks.end())
{
i->block->reserve(0);
- i=rsv_blocks.erase(i);
+ i = rsv_blocks.erase(i);
}
reserve_more();
update_speed();
unsigned Train::reserve_more()
{
- BlockRef *last=0;
+ BlockRef *last = 0;
if(!rsv_blocks.empty())
- last=&rsv_blocks.back();
+ last = &rsv_blocks.back();
else if(!cur_blocks.empty())
- last=&cur_blocks.back();
+ last = &cur_blocks.back();
if(!last)
return 0;
- unsigned nsens=0;
+ unsigned nsens = 0;
for(list<BlockRef>::const_iterator i=rsv_blocks.begin(); i!=rsv_blocks.end(); ++i)
if(i->block->get_sensor_id())
++nsens;
- bool result=false;
+ bool result = false;
while(nsens<2)
{
- int exit=last->block->traverse(last->entry);
+ int exit = last->block->traverse(last->entry);
if(exit>=0)
{
- Block *link=last->block->get_link(exit);
+ Block *link = last->block->get_link(exit);
if(link && link->reserve(this))
{
rsv_blocks.push_back(BlockRef(link, link->get_endpoint_by_link(*last->block)));
- last=&rsv_blocks.back();
+ last = &rsv_blocks.back();
if(last->block->get_sensor_id())
{
++nsens;
- result=true;
+ result = true;
}
}
else
last->block->reserve(0);
rsv_blocks.erase(--rsv_blocks.end());
if(!rsv_blocks.empty())
- last=&rsv_blocks.back();
+ last = &rsv_blocks.back();
else
- last=0;
+ last = 0;
}
return nsens;
if(!target_speed)
{
loco.set_speed(0);
- try_reserve=Time::TimeStamp();
+ try_reserve = Time::TimeStamp();
set_status("Stopped");
}
else
{
- unsigned nsens=0;
+ unsigned nsens = 0;
for(list<BlockRef>::const_iterator i=rsv_blocks.begin(); i!=rsv_blocks.end(); ++i)
if(i->block->get_sensor_id())
++nsens;
if(nsens==0)
{
loco.set_speed(0);
- pure_speed=false;
- try_reserve=Time::now()+2*Time::sec;
+ pure_speed = false;
+ try_reserve = Time::now()+2*Time::sec;
set_status("Blocked");
}
else if(nsens==1 && target_speed>3)
{
loco.set_speed(3);
- pure_speed=false;
- try_reserve=Time::now()+2*Time::sec;
+ pure_speed = false;
+ try_reserve = Time::now()+2*Time::sec;
set_status("Slow");
}
else
{
loco.set_speed(target_speed);
- try_reserve=Time::TimeStamp();
+ try_reserve = Time::TimeStamp();
set_status(format("Traveling %d kmh", travel_speed));
}
}
void Train::set_status(const string &s)
{
- status=s;
+ status = s;
signal_status_changed.emit(s);
}
void Train::set_position(const Block::Endpoint &bep)
{
- cur_track=bep.track;
- cur_track_ep=bep.track_ep;
- offset=0;
- pos=cur_track->get_endpoint_position(cur_track_ep);
+ cur_track = bep.track;
+ cur_track_ep = bep.track_ep;
+ offset = 0;
+ pos = cur_track->get_endpoint_position(cur_track_ep);
}
control.signal_turnout_event.connect(sigc::mem_fun(this, &Turnout::turnout_event));
unsigned char data[2];
- data[0]=addr&0xFF;
- data[1]=(addr>>8)&0xFF;
+ data[0] = addr&0xFF;
+ data[1] = (addr>>8)&0xFF;
control.command(CMD_TURNOUT_STATUS, data, 2).signal_done.connect(sigc::bind(sigc::mem_fun(this, &Turnout::status_reply), false));
if(dual)
{
- data[0]=(addr+1)&0xFF;
- data[1]=((addr+1)>>8)&0xFF;
+ data[0] = (addr+1)&0xFF;
+ data[1] = ((addr+1)>>8)&0xFF;
control.command(CMD_TURNOUT_STATUS, data, 2).signal_done.connect(sigc::bind(sigc::mem_fun(this, &Turnout::status_reply), true));
}
}
{
signal_route_changing.emit(r);
- route=r;
+ route = r;
command(true);
control.set_timer(200*Time::msec).signal_timeout.connect(sigc::mem_fun(this, &Turnout::switch_timeout));
void Turnout::command(bool on)
{
unsigned char data[2];
- data[0]=addr&0xFF;
- data[1]=((addr>>8)&0x7) | (on ? 0x40 : 0) | (route&1 ? 0 : 0x80);
+ data[0] = addr&0xFF;
+ data[1] = ((addr>>8)&0x7) | (on ? 0x40 : 0) | (route&1 ? 0 : 0x80);
control.command(CMD_TURNOUT, data, 2);
if(dual)
{
- data[0]=(addr+1)&0xFF;
- data[1]=(((addr+1)>>8)&0x7) | (on ? 0x40 : 0) | (route&2 ? 0 : 0x80);
+ data[0] = (addr+1)&0xFF;
+ data[1] = (((addr+1)>>8)&0x7) | (on ? 0x40 : 0) | (route&2 ? 0 : 0x80);
control.command(CMD_TURNOUT, data, 2);
}
}
{
if(reply.get_error()==ERR_NO_ERROR)
{
- bool v=!(reply.get_data()[0]&0x04);
- unsigned b=(high?2:1);
- route=(route&~b)|(v?b:0);
+ bool v = !(reply.get_data()[0]&0x04);
+ unsigned b = (high?2:1);
+ route = (route&~b)|(v?b:0);
signal_route_changed.emit(route);
}
}
{
if(a==addr && r!=(route&1))
{
- route=(route&2)|(r?1:0);
+ route = (route&2)|(r?1:0);
signal_route_changed.emit(route);
}
else if(dual && a==addr+1 && r!=((route>>1)&1))
{
- route=(route&1)|(r?2:0);
+ route = (route&1)|(r?2:0);
signal_route_changed.emit(route);
}
}