]> git.tdb.fi Git - r2c2.git/commitdiff
Use generic objects in Layout3D
authorMikko Rasa <tdb@tdb.fi>
Sat, 6 Jul 2013 18:32:19 +0000 (21:32 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sat, 6 Jul 2013 18:32:19 +0000 (21:32 +0300)
12 files changed:
source/3d/layout.cpp
source/3d/layout.h
source/3d/object.cpp
source/3d/object.h
source/3d/signal.cpp
source/3d/signal.h
source/3d/track.cpp
source/3d/track.h
source/3d/vehicle.cpp
source/3d/vehicle.h
source/designer/designer.cpp
source/engineer/engineer.cpp

index 2927e446741df4e1ad2ae194949831f449cc2afc..db419d3639065c2e76a574702ebfe062085b6eae 100644 (file)
@@ -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<float, 3> 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<Track *>(&o))
-       {
-               TrackMap::iterator i = tracks.find(t);
-               if(i!=tracks.end())
-                       delete i->second;
-       }
-       else if(Signal *s = dynamic_cast<Signal *>(&o))
-       {
-               SignalMap::iterator i = signals.find(s);
-               if(i!=signals.end())
-                       delete i->second;
-       }
-       else if(Vehicle *v = dynamic_cast<Vehicle *>(&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
index 8456fa8e7404d62fbcec19a0792e3b35cc56e56a..908751a27be6cafa29584c2d7982e290fbd512e6 100644 (file)
 
 namespace R2C2 {
 
-class Signal3D;
-class Track3D;
-class Vehicle3D;
+class Object3D;
 
 class Layout3D: public sigc::trackable
 {
 public:
-       typedef std::map<Track *, Track3D *> TrackMap;
-       typedef std::map<Vehicle *, Vehicle3D *> VehicleMap;
-       typedef std::map<Signal *, Signal3D *> SignalMap;
+       typedef std::map<Object *, Object3D *> 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<typename T>
+       T &get(Object &o) const
+       { return dynamic_cast<T &>(get(o)); }
 
        Msp::GL::Scene &get_scene() { return scene; }
        Msp::GL::Scene &get_endpoint_scene() { return ep_scene; }
index fcbf2136e7158a98c2dbf7bfc00cd1529549a1cc..b901a7294f9f38c775621f69ecc7e0f469333ee6 100644 (file)
@@ -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()
index 5696627f7e8e59a9285178dbdb43d6c13b939eea..aa6ce80aaf81641a05b1df13d420852240fbfeb1 100644 (file)
@@ -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;
index 8e0fea841aee940b62687bf03c1749fadbc807d8..5872dc850785c6f87294272898b934c7ec188fb1 100644 (file)
@@ -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);
 }
 
index dc4c5107b5f63b61adb616a3888555b4a883c71c..47505fc94cc5cda4909ef08d99761556d27438e3 100644 (file)
@@ -7,12 +7,9 @@
 
 namespace R2C2 {
 
-class Layout3D;
-
 class Signal3D: public Object3D, public Msp::GL::ObjectInstance
 {
 private:
-       Layout3D &layout;
        Signal &signal;
 
 public:
index 210791feeaf6c77fd0bab3ed96172ef3ce741963..0fa6344ce7709b9faab370566d05cb0e119517d5 100644 (file)
@@ -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<TrackType::Endpoint> &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<Endpoint3D *>::iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
index cc06cf1593f24073c42ccf779480cc3b262ce36c..c66a65f969bae0b7dc08f23c4768401e17e502ea 100644 (file)
 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<Endpoint3D *> endpoints;
index e33f643c8ddc69d710a63ef3019d988a593bab0e..e4bab870f856434b1e7e79f9b6c54543986921ee 100644 (file)
@@ -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<Axle3D *>::iterator i=axles.begin(); i!=axles.end(); ++i)
        {
index aa5042e4b73c3d2643c2850106ac8ab4a7c10eae..4b145536f724f566e4e41d9919f201659c1f2ff6 100644 (file)
@@ -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<Axle3D *> axles;
index 61f0d9c00fb495ca08b98ec6654858a2c550d0b4..3db5d502d12f985928260077962a9c74bdaa9ffa 100644 (file)
@@ -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<Track *> &tracks = layout->get_all<Track>();
+       for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
+               update_track_icon(layout_3d->get<Track3D>(**i));
 }
 
 Designer::~Designer()
@@ -213,7 +213,7 @@ void Designer::erase_tracks()
        selection.clear();
        for(set<Track *>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
        {
-               overlay->clear(layout_3d->get_track(**i));
+               overlay->clear(layout_3d->get<Track3D>(**i));
                delete *i;
        }
 }
@@ -362,7 +362,7 @@ void Designer::tick()
        camera_ctl->tick(dt);
 
        for(list<Track *>::iterator i=new_tracks.begin(); i!=new_tracks.end(); ++i)
-               layout_3d->get_track(**i).get_path().set_mask(0);
+               layout_3d->get<Track3D>(**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<Track3D>(**i));
                }
        }
        else if(key==Msp::Input::KEY_F)
@@ -619,7 +619,7 @@ void Designer::track_properties_response(int)
 {
        const set<Track *> &tracks = selection.get_objects<Track>();
        for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
-               update_track_icon(layout_3d->get_track(**i));
+               update_track_icon(layout_3d->get<Track3D>(**i));
 }
 
 void Designer::route_name_accept(const string &text)
@@ -661,7 +661,7 @@ void Designer::clear_paths()
        const set<Track *> &ltracks = layout->get_all<Track>();
        for(set<Track *>::iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
        {
-               Track3D &t3d = layout_3d->get_track(**i);
+               Track3D &t3d = layout_3d->get<Track3D>(**i);
                t3d.get_path().set_mask(0);
        }
 }
@@ -673,7 +673,7 @@ void Designer::show_route(const Route &route)
        const set<Track *> &rtracks = route.get_tracks();
        for(set<Track *>::iterator i=rtracks.begin(); i!=rtracks.end(); ++i)
        {
-               Track3D &t3d = layout_3d->get_track(**i);
+               Track3D &t3d = layout_3d->get<Track3D>(**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<Track3D>(**i);
                t3d.get_path().set_color(GL::Color(0.8, 1.0, 0.5));
                t3d.get_path().set_mask((*i)->get_type().get_paths());
        }
index 61dd49a1c72d86136baa9fd462e05b2458b398e5..1fb4adf75653a852905aae77bafaff338d15d18e 100644 (file)
@@ -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<Track3D>(*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<Track *> &tracks = block.get_tracks();
        for(set<Track *>::const_iterator i=tracks.begin(); i!=tracks.end(); ++i)
-               layout_3d.get_track(**i).get_path().set_color(color);
+               layout_3d.get<Track3D>(**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<Vehicle3D>(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)));
 }