From: Mikko Rasa Date: Sat, 6 Jul 2013 18:32:19 +0000 (+0300) Subject: Use generic objects in Layout3D X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=4049f5bdacfdb61247f3c74fc5f748990be7bd4d;p=r2c2.git Use generic objects in Layout3D --- diff --git a/source/3d/layout.cpp b/source/3d/layout.cpp index 2927e44..db419d3 100644 --- a/source/3d/layout.cpp +++ b/source/3d/layout.cpp @@ -26,68 +26,34 @@ Layout3D::Layout3D(Layout &l): Layout3D::~Layout3D() { - while(!signals.empty()) - delete signals.begin()->second; - while(!tracks.empty()) - delete tracks.begin()->second; - while(!vehicles.empty()) - delete vehicles.begin()->second; + while(!objects.empty()) + delete objects.begin()->second; } void Layout3D::get_bounds(Vector &minp, Vector &maxp) const { Geometry::BoundingBox bbox; - for(TrackMap::const_iterator i=tracks.begin(); i!=tracks.end(); ++i) - bbox = bbox|i->second->get_track().get_type().get_shape()->get_axis_aligned_bounding_box(); + for(ObjectMap::const_iterator i=objects.begin(); i!=objects.end(); ++i) + bbox = bbox|i->second->get_object().get_type().get_shape()->get_axis_aligned_bounding_box(); minp = bbox.get_minimum_point(); maxp = bbox.get_maximum_point(); } -void Layout3D::add_track(Track3D &t) +void Layout3D::add(Object3D &o) { - insert_unique(tracks, &t.get_track(), &t); + insert_unique(objects, &o.get_object(), &o); } -void Layout3D::remove_track(Track3D &t) +Object3D &Layout3D::get(Object &o) const { - tracks.erase(&t.get_track()); + return *get_item(objects, &o); } -Track3D &Layout3D::get_track(Track &t) const +void Layout3D::remove(Object3D &o) { - return *get_item(tracks, &t); -} - -void Layout3D::add_vehicle(Vehicle3D &v) -{ - insert_unique(vehicles, &v.get_vehicle(), &v); -} - -void Layout3D::remove_vehicle(Vehicle3D &v) -{ - vehicles.erase(&v.get_vehicle()); -} - -Vehicle3D &Layout3D::get_vehicle(Vehicle &v) const -{ - return *get_item(vehicles, &v); -} - -void Layout3D::add_signal(Signal3D &s) -{ - insert_unique(signals, &s.get_signal(), &s); -} - -void Layout3D::remove_signal(Signal3D &s) -{ - signals.erase(&s.get_signal()); -} - -Signal3D &Layout3D::get_signal(Signal &s) const -{ - return *get_item(signals, &s); + objects.erase(&o.get_object()); } void Layout3D::object_added(Object &o) @@ -102,24 +68,9 @@ void Layout3D::object_added(Object &o) void Layout3D::object_removed(Object &o) { - if(Track *t = dynamic_cast(&o)) - { - TrackMap::iterator i = tracks.find(t); - if(i!=tracks.end()) - delete i->second; - } - else if(Signal *s = dynamic_cast(&o)) - { - SignalMap::iterator i = signals.find(s); - if(i!=signals.end()) - delete i->second; - } - else if(Vehicle *v = dynamic_cast(&o)) - { - VehicleMap::iterator i = vehicles.find(v); - if(i!=vehicles.end()) - delete i->second; - } + ObjectMap::iterator i = objects.find(&o); + if(i!=objects.end()) + delete i->second; } } // namespace R2C2 diff --git a/source/3d/layout.h b/source/3d/layout.h index 8456fa8..908751a 100644 --- a/source/3d/layout.h +++ b/source/3d/layout.h @@ -11,23 +11,17 @@ namespace R2C2 { -class Signal3D; -class Track3D; -class Vehicle3D; +class Object3D; class Layout3D: public sigc::trackable { public: - typedef std::map TrackMap; - typedef std::map VehicleMap; - typedef std::map SignalMap; + typedef std::map ObjectMap; private: Layout &layout; Catalogue3D catalogue; - TrackMap tracks; - VehicleMap vehicles; - SignalMap signals; + ObjectMap objects; Msp::GL::InstanceScene scene; Msp::GL::SimpleScene ep_scene; Msp::GL::InstanceScene path_scene; @@ -42,18 +36,14 @@ public: const Catalogue3D &get_catalogue() const { return catalogue; } void get_bounds(Vector &, Vector &) const; - void add_track(Track3D &); - void remove_track(Track3D &); - const TrackMap &get_tracks() const { return tracks; } - Track3D &get_track(Track &) const; + void add(Object3D &); + Object3D &get(Object &) const; + const ObjectMap &get_all() const { return objects; } + void remove(Object3D &); - void add_vehicle(Vehicle3D &); - void remove_vehicle(Vehicle3D &); - Vehicle3D &get_vehicle(Vehicle &) const; - - void add_signal(Signal3D &); - void remove_signal(Signal3D &); - Signal3D &get_signal(Signal &) const; + template + T &get(Object &o) const + { return dynamic_cast(get(o)); } Msp::GL::Scene &get_scene() { return scene; } Msp::GL::Scene &get_endpoint_scene() { return ep_scene; } diff --git a/source/3d/object.cpp b/source/3d/object.cpp index fcbf213..b901a72 100644 --- a/source/3d/object.cpp +++ b/source/3d/object.cpp @@ -1,13 +1,22 @@ +#include "layout.h" #include "object.h" using namespace Msp; namespace R2C2 { -Object3D::Object3D(Object &o): +Object3D::Object3D(Layout3D &l, Object &o): + layout(l), object(o) { object.signal_moved.connect(sigc::mem_fun(this, &Object3D::moved)); + + layout.add(*this); +} + +Object3D::~Object3D() +{ + layout.remove(*this); } void Object3D::moved() diff --git a/source/3d/object.h b/source/3d/object.h index 5696627..aa6ce80 100644 --- a/source/3d/object.h +++ b/source/3d/object.h @@ -8,15 +8,21 @@ namespace R2C2 { +class Layout3D; + class Object3D: public sigc::trackable { protected: + Layout3D &layout; Object &object; Msp::GL::Matrix matrix; - Object3D(Object &); + Object3D(Layout3D &, Object &); public: - virtual ~Object3D() { } + virtual ~Object3D(); + + Layout3D &get_layout() const { return layout; } + Object &get_object() const { return object; } const Msp::GL::Matrix &get_matrix() const { return matrix; } virtual Vector get_node() const = 0; diff --git a/source/3d/signal.cpp b/source/3d/signal.cpp index 8e0fea8..5872dc8 100644 --- a/source/3d/signal.cpp +++ b/source/3d/signal.cpp @@ -9,18 +9,15 @@ using namespace Msp; namespace R2C2 { Signal3D::Signal3D(Layout3D &l, Signal &s): - Object3D(s), + Object3D(l, s), GL::ObjectInstance(l.get_catalogue().get_signal(s.get_type()).get_object()), - layout(l), signal(s) { - layout.add_signal(*this); layout.get_scene().add(*this); } Signal3D::~Signal3D() { - layout.remove_signal(*this); layout.get_scene().remove(*this); } diff --git a/source/3d/signal.h b/source/3d/signal.h index dc4c510..47505fc 100644 --- a/source/3d/signal.h +++ b/source/3d/signal.h @@ -7,12 +7,9 @@ namespace R2C2 { -class Layout3D; - class Signal3D: public Object3D, public Msp::GL::ObjectInstance { private: - Layout3D &layout; Signal &signal; public: diff --git a/source/3d/track.cpp b/source/3d/track.cpp index 210791f..0fa6344 100644 --- a/source/3d/track.cpp +++ b/source/3d/track.cpp @@ -14,14 +14,12 @@ using namespace Msp; namespace R2C2 { Track3D::Track3D(Layout3D &l, Track &t): - Object3D(t), + Object3D(l, t), GL::ObjectInstance(l.get_catalogue().get_track(t.get_type()).get_object()), - layout(l), track(t), type(layout.get_catalogue().get_track(track.get_type())), path(new Path3D(*this)) { - layout.add_track(*this); layout.get_scene().add(*this); const vector &type_eps = track.get_type().get_endpoints(); @@ -41,7 +39,6 @@ Track3D::~Track3D() { delete path; - layout.remove_track(*this); layout.get_scene().remove(*this); for(vector::iterator i=endpoints.begin(); i!=endpoints.end(); ++i) diff --git a/source/3d/track.h b/source/3d/track.h index cc06cf1..c66a65f 100644 --- a/source/3d/track.h +++ b/source/3d/track.h @@ -12,14 +12,12 @@ namespace R2C2 { class Endpoint3D; -class Layout3D; class Path3D; class TrackType3D; class Track3D: public Object3D, public Msp::GL::ObjectInstance { private: - Layout3D &layout; Track &track; const TrackType3D &type; std::vector endpoints; diff --git a/source/3d/vehicle.cpp b/source/3d/vehicle.cpp index e33f643..e4bab87 100644 --- a/source/3d/vehicle.cpp +++ b/source/3d/vehicle.cpp @@ -14,9 +14,8 @@ using namespace Msp; namespace R2C2 { Vehicle3D::Vehicle3D(Layout3D &l, Vehicle &v): - Object3D(v), + Object3D(l, v), GL::ObjectInstance(*l.get_catalogue().get_vehicle(v.get_type()).get_body_object()), - layout(l), vehicle(v), type(layout.get_catalogue().get_vehicle(vehicle.get_type())) { @@ -56,13 +55,11 @@ Vehicle3D::Vehicle3D(Layout3D &l, Vehicle &v): layout.get_scene().add(*r); } - layout.add_vehicle(*this); layout.get_scene().add(*this); } Vehicle3D::~Vehicle3D() { - layout.remove_vehicle(*this); layout.get_scene().remove(*this); for(vector::iterator i=axles.begin(); i!=axles.end(); ++i) { diff --git a/source/3d/vehicle.h b/source/3d/vehicle.h index aa5042e..4b14553 100644 --- a/source/3d/vehicle.h +++ b/source/3d/vehicle.h @@ -9,14 +9,12 @@ namespace R2C2 { class Axle3D; class Bogie3D; -class Layout3D; class Rod3D; class VehicleType3D; class Vehicle3D: public Object3D, public Msp::GL::ObjectInstance { private: - Layout3D &layout; Vehicle &vehicle; const VehicleType3D &type; std::vector axles; diff --git a/source/designer/designer.cpp b/source/designer/designer.cpp index 61f0d9c..3db5d50 100644 --- a/source/designer/designer.cpp +++ b/source/designer/designer.cpp @@ -154,9 +154,9 @@ Designer::Designer(int argc, char **argv): cat_view->view_all(true); main_view->view_all(); - 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); + const set &tracks = layout->get_all(); + for(set::const_iterator i=tracks.begin(); i!=tracks.end(); ++i) + update_track_icon(layout_3d->get(**i)); } Designer::~Designer() @@ -213,7 +213,7 @@ void Designer::erase_tracks() selection.clear(); for(set::iterator i=tracks.begin(); i!=tracks.end(); ++i) { - overlay->clear(layout_3d->get_track(**i)); + overlay->clear(layout_3d->get(**i)); delete *i; } } @@ -362,7 +362,7 @@ void Designer::tick() camera_ctl->tick(dt); for(list::iterator i=new_tracks.begin(); i!=new_tracks.end(); ++i) - layout_3d->get_track(**i).get_path().set_mask(0); + layout_3d->get(**i).get_path().set_mask(0); new_tracks.clear(); render(); @@ -435,7 +435,7 @@ void Designer::key_press(unsigned key) if(*j!=*i) (*i)->snap_to(**j, true); - update_track_icon(layout_3d->get_track(**i)); + update_track_icon(layout_3d->get(**i)); } } else if(key==Msp::Input::KEY_F) @@ -619,7 +619,7 @@ void Designer::track_properties_response(int) { const set &tracks = selection.get_objects(); for(set::const_iterator i=tracks.begin(); i!=tracks.end(); ++i) - update_track_icon(layout_3d->get_track(**i)); + update_track_icon(layout_3d->get(**i)); } void Designer::route_name_accept(const string &text) @@ -661,7 +661,7 @@ void Designer::clear_paths() const set <racks = layout->get_all(); for(set::iterator i=ltracks.begin(); i!=ltracks.end(); ++i) { - Track3D &t3d = layout_3d->get_track(**i); + Track3D &t3d = layout_3d->get(**i); t3d.get_path().set_mask(0); } } @@ -673,7 +673,7 @@ void Designer::show_route(const Route &route) const set &rtracks = route.get_tracks(); for(set::iterator i=rtracks.begin(); i!=rtracks.end(); ++i) { - Track3D &t3d = layout_3d->get_track(**i); + Track3D &t3d = layout_3d->get(**i); t3d.get_path().set_color(GL::Color(0.5, 0.8, 1.0)); int path = -1; if(unsigned tid = (*i)->get_turnout_id()) @@ -692,7 +692,7 @@ void Designer::show_zone(const Zone &zone) const Zone::TrackSet &ztracks = zone.get_tracks(); for(Zone::TrackSet::const_iterator i=ztracks.begin(); i!=ztracks.end(); ++i) { - Track3D &t3d = layout_3d->get_track(**i); + Track3D &t3d = layout_3d->get(**i); t3d.get_path().set_color(GL::Color(0.8, 1.0, 0.5)); t3d.get_path().set_mask((*i)->get_type().get_paths()); } diff --git a/source/engineer/engineer.cpp b/source/engineer/engineer.cpp index 61dd49a..1fb4adf 100644 --- a/source/engineer/engineer.cpp +++ b/source/engineer/engineer.cpp @@ -206,7 +206,7 @@ void Engineer::tick() picking_entry = 0; delete picking_path; - picking_path = new Path3D(layout_3d.get_track(*track)); + picking_path = new Path3D(layout_3d.get(*track)); if(picking_entry>=0) picking_path->set_mask(picking_track->get_type().get_endpoint(picking_entry).paths); else @@ -304,7 +304,7 @@ void Engineer::axis_motion(unsigned axis, float value, float) void Engineer::view_all() { - const Layout3D::TrackMap &tracks = layout_3d.get_tracks(); + const Layout3D::ObjectMap &objects = layout_3d.get_all(); float view_aspect = float(window.get_width())/window.get_height(); float view_height = tan(camera.get_field_of_view()/2.0f)*2.0f; @@ -315,8 +315,8 @@ void Engineer::view_all() { Transform trans = Transform::rotation(-angle, Vector(0, 0, 1)); BoundingBox bbox; - for(Layout3D::TrackMap::const_iterator i=tracks.begin(); i!=tracks.end(); ++i) - bbox = bbox|trans.transform(i->second->get_track().get_bounding_box()); + for(Layout3D::ObjectMap::const_iterator i=objects.begin(); i!=objects.end(); ++i) + bbox = bbox|trans.transform(i->second->get_object().get_bounding_box()); const Vector &minp = bbox.get_minimum_point(); const Vector &maxp = bbox.get_maximum_point(); @@ -353,7 +353,7 @@ void Engineer::set_block_color(const Block &block, const GL::Color &color) { const set &tracks = block.get_tracks(); for(set::const_iterator i=tracks.begin(); i!=tracks.end(); ++i) - layout_3d.get_track(**i).get_path().set_color(color); + layout_3d.get(**i).get_path().set_color(color); } void Engineer::reset_block_color(const Block &block) @@ -391,7 +391,7 @@ Object *Engineer::pick_object(const Vector &p) void Engineer::process_new_train(Train &train) { - Vehicle3D &loco3d = layout_3d.get_vehicle(train.get_vehicle(0)); + Vehicle3D &loco3d = layout_3d.get(train.get_vehicle(0)); overlay->set_label(loco3d, train.get_name()); train.signal_name_changed.connect(sigc::bind<0>(sigc::mem_fun(overlay, &Overlay3D::set_label), sigc::ref(loco3d))); }