Layout3D::~Layout3D()
{
while(!tracks.empty())
- delete tracks.front();
+ delete tracks.begin()->second;
while(!vehicles.empty())
- delete vehicles.front();
+ delete vehicles.begin()->second;
}
void Layout3D::add_track(Track3D &t)
{
- tracks.push_back(&t);
+ if(tracks.count(&t.get_track()))
+ throw KeyError("Duplicate track");
+
+ tracks[&t.get_track()] = &t;
}
void Layout3D::remove_track(Track3D &t)
{
- list<Track3D *>::iterator i = find(tracks.begin(), tracks.end(), &t);
- if(i!=tracks.end())
- tracks.erase(i);
+ tracks.erase(&t.get_track());
}
Track3D &Layout3D::get_track(Track &t) const
{
- for(list<Track3D *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
- if(&(*i)->get_track()==&t)
- return **i;
+ TrackMap::const_iterator i = tracks.find(&t);
+ if(i==tracks.end())
+ throw KeyError("Unknown track");
- throw KeyError("Unknown track");
+ return *i->second;
}
Track3D *Layout3D::pick_track(float x, float y, float size) const
void Layout3D::add_vehicle(Vehicle3D &v)
{
- vehicles.push_back(&v);
+ if(vehicles.count(&v.get_vehicle()))
+ throw KeyError("Duplicate vehicle");
+
+ vehicles[&v.get_vehicle()] = &v;
}
void Layout3D::remove_vehicle(Vehicle3D &v)
{
- list<Vehicle3D *>::iterator i = find(vehicles.begin(), vehicles.end(), &v);
- if(i!=vehicles.end())
- vehicles.erase(i);
+ vehicles.erase(&v.get_vehicle());
}
-Vehicle3D &Layout3D::get_vehicle(const Vehicle &v) const
+Vehicle3D &Layout3D::get_vehicle(Vehicle &v) const
{
- for(list<Vehicle3D *>::const_iterator i=vehicles.begin(); i!=vehicles.end(); ++i)
- if(&(*i)->get_vehicle()==&v)
- return **i;
+ VehicleMap::const_iterator i = vehicles.find(&v);
+ if(i==vehicles.end())
+ throw KeyError("Unknown vehicle");
- throw KeyError("Unknown vehicle");
+ return *i->second;
}
void Layout3D::track_added(Track &t)
void Layout3D::track_removed(Track &t)
{
- for(list<Track3D *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
- if(&(*i)->get_track()==&t)
- {
- delete *i;
- return;
- }
+ TrackMap::iterator i = tracks.find(&t);
+ if(i!=tracks.end())
+ delete i->second;
}
void Layout3D::vehicle_added(Vehicle &v)
class Layout3D: public sigc::trackable
{
+public:
+ typedef std::map<Track *, Track3D *> TrackMap;
+ typedef std::map<Vehicle *, Vehicle3D *> VehicleMap;
+
private:
Layout &layout;
Catalogue3D catalogue;
- std::list<Track3D *> tracks;
- std::list<Vehicle3D *> vehicles;
+ TrackMap tracks;
+ VehicleMap vehicles;
Msp::GL::Scene scene;
Msp::GL::Scene ep_scene;
Msp::GL::Scene path_scene;
void add_track(Track3D &);
void remove_track(Track3D &);
- const std::list<Track3D *> &get_tracks() const { return tracks; }
+ const TrackMap &get_tracks() const { return tracks; }
Track3D &get_track(Track &) const;
Track3D *pick_track(float, float, float) const;
void add_vehicle(Vehicle3D &);
void remove_vehicle(Vehicle3D &);
- Vehicle3D &get_vehicle(const Vehicle &) const;
+ Vehicle3D &get_vehicle(Vehicle &) const;
Msp::GL::Scene &get_scene() { return scene; }
Msp::GL::Scene &get_endpoint_scene() { return ep_scene; }
overlay = new Overlay3D(window, camera, ui_res.get_default_font());
- const list<Track3D *> &tracks = layout_3d->get_tracks();
- for(list<Track3D *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
- update_track_icon(**i);
+ const Layout3D::TrackMap &tracks = layout_3d->get_tracks();
+ for(Layout3D::TrackMap::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
+ update_track_icon(*i->second);
}
Designer::~Designer()
Point minp;
Point maxp;
- const list<Track3D *> &tracks = layout_3d->get_tracks();
- for(list<Track3D *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
+ const Layout3D::TrackMap &tracks = layout_3d->get_tracks();
+ for(Layout3D::TrackMap::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
{
Point tmin;
Point tmax;
- (*i)->get_bounds(0, tmin, tmax);
+ i->second->get_bounds(0, tmin, tmax);
minp.x = min(minp.x, tmin.x);
minp.y = min(minp.y, tmin.y);
maxp.x = max(maxp.x, tmax.x);
void Engineer::view_all()
{
- const list<Track3D *> &tracks = layout_3d.get_tracks();
+ const Layout3D::TrackMap &tracks = layout_3d.get_tracks();
float view_aspect = float(window.get_width()-200)/window.get_height();
float view_height = tan(camera.get_field_of_view()/2)*2;
float max_x = 0;
float min_y = 0;
float max_y = 0;
- for(list<Track3D *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
+ for(Layout3D::TrackMap::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
{
Point minp, maxp;
- (*i)->get_bounds(angle, minp, maxp);
+ i->second->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);