]> git.tdb.fi Git - r2c2.git/commitdiff
Add a generic link interface as well
authorMikko Rasa <tdb@tdb.fi>
Mon, 13 May 2013 19:54:47 +0000 (22:54 +0300)
committerMikko Rasa <tdb@tdb.fi>
Mon, 13 May 2013 20:56:01 +0000 (23:56 +0300)
source/designer/manipulator.cpp
source/designer/selection.cpp
source/libr2c2/layout.cpp
source/libr2c2/object.cpp
source/libr2c2/object.h
source/libr2c2/route.cpp
source/libr2c2/track.cpp
source/libr2c2/track.h
source/libr2c2/trackiter.cpp
source/libr2c2/vehicle.cpp
source/libr2c2/vehicle.h

index 6aca114aa5538ecfbceebf5ab328009c4b5a675b..5bf02e9760ab2765cc01e0842c111b760b89b429 100644 (file)
@@ -83,12 +83,8 @@ void Manipulator::duplicate()
        for(vector<MObject>::iterator i=objects.begin(); i!=objects.end(); ++i)
        {
                Object *obj = i->object->clone(&designer.get_layout());
-               if(Track *track = dynamic_cast<Track *>(obj))
-               {
-                       for(list<Object *>::iterator j=new_objs.begin(); j!=new_objs.end(); ++j)
-                               if(Track *track2 = dynamic_cast<Track *>(*j))
-                                       track->snap_to(*track2, true);
-               }
+               for(list<Object *>::iterator j=new_objs.begin(); j!=new_objs.end(); ++j)
+                       obj->link_to(**j);
                new_objs.push_back(obj);
        }
 
@@ -135,9 +131,8 @@ void Manipulator::even_slope(bool smooth)
                return;
 
        for(vector<MObject>::iterator i=objects.begin(); i!=objects.end(); ++i)
-               if(Track *track = dynamic_cast<Track *>(i->object))
-                       if(track->get_type().get_endpoints().size()!=2)
-                               return;
+               if(i->object->get_n_link_slots()!=2)
+                       return;
 
        list<Track *> tracks2;
        for(vector<MObject>::iterator i=objects.begin(); i!=objects.end(); ++i)
@@ -173,10 +168,10 @@ void Manipulator::even_slope(bool smooth)
        }
 
        set<Track *>::iterator nb = neighbors.begin();
-       int epi = (*nb)->get_endpoint_by_link(*order.front().track);
+       int epi = (*nb)->get_link_slot(*order.front().track);
        float start_z = (*nb)->get_snap_node(epi).position.z;
        ++nb;
-       epi = (*nb)->get_endpoint_by_link(*order.back().track);
+       epi = (*nb)->get_link_slot(*order.back().track);
        float end_z = (*nb)->get_snap_node(epi).position.z;
 
        if(smooth)
@@ -294,8 +289,8 @@ void Manipulator::connect()
                return;
        }
 
-       trks.front()->snap_to(*track1, true);
-       trks.back()->snap_to(*track2, true);
+       trks.front()->link_to(*track1);
+       trks.back()->link_to(*track2);
 
        selection.replace(trks.begin(), trks.end());
 }
@@ -341,8 +336,7 @@ void Manipulator::button_press(unsigned btn)
                {
                        for(set<Track *>::iterator i=neighbors.begin(); i!=neighbors.end(); ++i)
                                for(vector<MObject>::iterator j=objects.begin(); j!=objects.end(); ++j)
-                                       if(Track *track = dynamic_cast<Track *>(j->object))
-                                               track->break_link(**i);
+                                       j->object->break_link(**i);
                }
 
                const set<Track *> &ltracks = designer.get_layout().get_tracks();
@@ -354,8 +348,7 @@ void Manipulator::button_press(unsigned btn)
                        if(!ok) continue;
 
                        for(vector<MObject>::iterator j=objects.begin(); j!=objects.end(); ++j)
-                               if(Track *track = dynamic_cast<Track *>(j->object))
-                                       track->snap_to(**i, true);
+                               j->object->link_to(**i);
                }
 
                if(m==EXTEND)
@@ -404,9 +397,8 @@ void Manipulator::axis_motion(unsigned axis, float value, float)
                                continue;
 
                        for(vector<MObject>::iterator j=objects.begin(); (j!=objects.end() && !snapped); ++j)
-                               if(Track *track = dynamic_cast<Track *>(j->object))
-                                       if(track->snap_to(**i, false, limit))
-                                               snapped = &*j;
+                               if(j->object->snap_to(**i, limit))
+                                       snapped = &*j;
                }
 
                if(snapped)
@@ -459,17 +451,13 @@ void Manipulator::axis_motion(unsigned axis, float value, float)
                float length = 0;
                for(vector<MObject>::iterator i=objects.begin(); i!=objects.end(); ++i)
                {
-                       Track *track = dynamic_cast<Track *>(i->object);
-                       if(!track)
-                               continue;
-
-                       unsigned n_endpoints = track->get_type().get_endpoints().size();
-                       for(unsigned j=0; j<n_endpoints; ++j)
+                       unsigned nls = i->object->get_n_link_slots();
+                       for(unsigned j=0; j<nls; ++j)
                        {
-                               if(track->get_link(j))
+                               if(i->object->get_link(j))
                                        continue;
 
-                               Snap sn = track->get_snap_node(j);
+                               Snap sn = i->object->get_snap_node(j);
                                float c = cos(sn.rotation);
                                float s = sin(sn.rotation);
                                float dx = gpointer.x-sn.position.x;
@@ -698,7 +686,7 @@ vector<Track *> Manipulator::create_straight(const R2C2::Vector &start, float di
                        track->set_rotation(dir);
 
                        if(!trks.empty())
-                               track->snap_to(*trks.back(), true);
+                               track->link_to(*trks.back());
                        trks.push_back(track);
 
                        pos.x += c**i;
index c93dd3628e5840394e9c190aaa53d2419e06282f..b351b452953d24c75574c0998cc187247ed19614 100644 (file)
@@ -42,18 +42,17 @@ void Selection::toggle_object(Object *o)
 
 void Selection::select_more()
 {
-       set<Track *> new_tracks;
+       set<Object *> new_objects;
        for(set<Object *>::iterator i=objects.begin(); i!=objects.end(); ++i)
-               if(Track *track = dynamic_cast<Track *>(*i))
-               {
-                       const vector<Track *> &links = track->get_links();
-                       for(vector<Track *>::const_iterator j=links.begin(); j!=links.end(); ++j)
-                               if(*j)
-                                       new_tracks.insert(*j);
-               }
+       {
+               unsigned nls = (*i)->get_n_link_slots();
+               for(unsigned j=0; j<nls; ++j)
+                       if(Object *obj = (*i)->get_link(j))
+                               new_objects.insert(obj);
+       }
 
        bool changed = false;
-       for(set<Track *>::iterator i=new_tracks.begin(); i!=new_tracks.end(); ++i)
+       for(set<Object *>::iterator i=new_objects.begin(); i!=new_objects.end(); ++i)
                if(objects.insert(*i).second)
                        changed = true;
 
@@ -64,23 +63,21 @@ void Selection::select_more()
 void Selection::select_linked()
 {
        bool changed = false;
-       list<Track *> queue;
-       for(set<Object *>::iterator i=objects.begin(); i!=objects.end(); ++i)
-               if(Track *track = dynamic_cast<Track *>(*i))
-                       queue.push_back(track);
+       list<Object *> queue(objects.begin(), objects.end());
 
        while(!queue.empty())
        {
-               Track *track = queue.front();
-               queue.erase(queue.begin());
-
-               const vector<Track *> &links = track->get_links();
-               for(vector<Track *>::const_iterator j=links.begin(); j!=links.end(); ++j)
-                       if(*j && objects.insert(*j).second)
-                       {
-                               queue.push_back(*j);
-                               changed = true;
-                       }
+               Object *obj = queue.front();
+               queue.pop_front();
+
+               unsigned nls = obj->get_n_link_slots();
+               for(unsigned j=0; j<nls; ++j)
+                       if(Object *linked = obj->get_link(j))
+                               if(objects.insert(linked).second)
+                               {
+                                       queue.push_back(linked);
+                                       changed = true;
+                               }
        }
 
        if(changed)
index 4679fd9e166674fec43a8ba01947b015bc8c294c..a2c97b7cbdfaaa887992e8d23392c607355d5a2e 100644 (file)
@@ -442,7 +442,7 @@ void Layout::Loader::track(ArticleNumber art_nr)
        new_tracks = true;
        for(set<Track *>::iterator i=obj.tracks.begin(); i!=obj.tracks.end(); ++i)
                if(*i!=trk)
-                       trk->snap_to(**i, true);
+                       trk->link_to(**i);
 }
 
 void Layout::Loader::train(unsigned art_nr, unsigned addr, const std::string &proto)
index 1b48a862ae6b634c499fa49c094e54bc5c41cb05..ba1810f913ebbc20a59a53c50aa7d8936cdfdd63 100644 (file)
@@ -57,4 +57,26 @@ bool Object::snap_to(const Object &other, float limit, SnapType what)
        return false;
 }
 
+Object *Object::get_link(unsigned) const
+{
+       throw out_of_range("Object::get_link");
+}
+
+bool Object::break_link(Object &other)
+{
+       unsigned nls = get_n_link_slots();
+       for(unsigned i=0; i<nls; ++i)
+               if(get_link(i)==&other)
+                       return break_link(i);
+
+       return false;
+}
+
+void Object::break_links()
+{
+       unsigned nls = get_n_link_slots();
+       for(unsigned i=0; i<nls; ++i)
+               break_link(i);
+}
+
 } // namespace R2C2
index 8ce521ab5366df54375973e3db4715e11cc0c81e..33b99a1443acb3b2f7452c8236e91354ab5ed8a4 100644 (file)
@@ -37,6 +37,14 @@ protected:
        virtual SnapType get_default_snap_type_to(const Object &) const { return NO_SNAP; }
 
 public:
+       virtual unsigned get_n_link_slots() const { return 0; }
+       virtual Object *get_link(unsigned) const;
+       virtual int get_link_slot(const Object &) const { return -1; }
+       virtual bool link_to(Object &) { return false; }
+       virtual bool break_link(Object &);
+       virtual bool break_link(unsigned) { return false; }
+       virtual void break_links();
+
        virtual bool collide_ray(const Vector &, const Vector &) const = 0;
 };
 
index 77c4f9280532537e41614c37837f66a82cca67a6..e3dfe36961c66a5a9e37d518373cc927d18df4c2 100644 (file)
@@ -196,7 +196,7 @@ void Route::update_turnouts()
 
                                if(unsigned tid2 = links[j]->get_turnout_id())
                                {
-                                       const TrackType::Endpoint &ep = links[j]->get_type().get_endpoint(links[j]->get_endpoint_by_link(**i));
+                                       const TrackType::Endpoint &ep = links[j]->get_type().get_endpoint(links[j]->get_link_slot(**i));
                                        int p = get_turnout(tid2);
                                        if(p>=0 && !ep.has_path(p))
                                        {
@@ -345,7 +345,7 @@ RouteValidityMask Route::check_validity(Track &trk) const
 
                if(unsigned tid = (*i)->get_turnout_id())
                {
-                       const TrackType::Endpoint &ep = (*i)->get_type().get_endpoint((*i)->get_endpoint_by_link(trk));
+                       const TrackType::Endpoint &ep = (*i)->get_type().get_endpoint((*i)->get_link_slot(trk));
                        int path = get_turnout(tid);
                        if(path>=0)
                        {
@@ -364,7 +364,7 @@ RouteValidityMask Route::check_validity(Track &trk) const
                                                unsigned tid2 = tlinks[j]->get_turnout_id();
                                                if(tid2)
                                                {
-                                                       const TrackType::Endpoint &ep2 = tlinks[j]->get_type().get_endpoint(tlinks[j]->get_endpoint_by_link(**i));
+                                                       const TrackType::Endpoint &ep2 = tlinks[j]->get_type().get_endpoint(tlinks[j]->get_link_slot(**i));
                                                        path = get_turnout(tid2);
                                                        // Ignore a linked turnout with some other path set
                                                        if(path>=0 && !ep2.has_path(path))
@@ -444,7 +444,7 @@ void Route::Loader::finish()
                        {
                                Track *link = (*i)->get_link(k);
                                if(!obj.tracks.count(link))
-                                       obj.add_track_chain(*link, link->get_endpoint_by_link(**i), turnouts);
+                                       obj.add_track_chain(*link, link->get_link_slot(**i), turnouts);
                                if(!obj.tracks.count(*i))
                                        obj.add_track_chain(**i, k, turnouts);
                                break;
index e4d7badfb9f9e053d2dc7fa7650b039bf890cf90..7b9a4a2d70b30febaf96ebf917cb284153ba44ac 100644 (file)
@@ -100,8 +100,8 @@ void Track::check_slope()
 
        if(links[0] && links[1])
        {
-               Vector epp0 = links[0]->get_snap_node(links[0]->get_endpoint_by_link(*this)).position;
-               Vector epp1 = links[1]->get_snap_node(links[1]->get_endpoint_by_link(*this)).position;
+               Vector epp0 = links[0]->get_snap_node(links[0]->get_link_slot(*this)).position;
+               Vector epp1 = links[1]->get_snap_node(links[1]->get_link_slot(*this)).position;
                position.z = epp0.z;
                slope = epp1.z-position.z;
        }
@@ -110,12 +110,12 @@ void Track::check_slope()
                slope = 0;
                if(links[0])
                {
-                       Vector epp = links[0]->get_snap_node(links[0]->get_endpoint_by_link(*this)).position;
+                       Vector epp = links[0]->get_snap_node(links[0]->get_link_slot(*this)).position;
                        position.z = epp.z;
                }
                else if(links[1])
                {
-                       Vector epp = links[1]->get_snap_node(links[1]->get_endpoint_by_link(*this)).position;
+                       Vector epp = links[1]->get_snap_node(links[1]->get_link_slot(*this)).position;
                        position.z = epp.z;
                }
        }
@@ -155,106 +155,6 @@ void Track::set_active_path(unsigned p)
        layout.get_driver().set_turnout(turnout_id, p);
 }
 
-int Track::get_endpoint_by_link(Track &other) const
-{
-       for(unsigned i=0; i<links.size(); ++i)
-               if(links[i]==&other)
-                       return i;
-
-       return -1;
-}
-
-bool Track::snap_to(Track &other, bool link, float limit)
-{
-       if(!limit || link)
-       {
-               limit = layout.get_catalogue().get_gauge();
-               if(link && !flex && !other.get_flex())
-                       limit /= 10;
-       }
-       limit *= limit;
-
-       const vector<TrackType::Endpoint> &eps = type.get_endpoints();
-       const vector<TrackType::Endpoint> &other_eps = other.get_type().get_endpoints();
-
-       for(unsigned i=0; i<eps.size(); ++i)
-       {
-               Vector epp = get_snap_node(i).position;
-
-               for(unsigned j=0; j<other_eps.size(); ++j)
-               {
-                       if(other.get_link(j))
-                               continue;
-
-                       Vector epp2 = other.get_snap_node(j).position;
-                       float dx = epp2.x-epp.x;
-                       float dy = epp2.y-epp.y;
-                       float dz = epp2.z-epp.z;
-                       if(dx*dx+dy*dy<limit && dz*dz<limit)
-                       {
-                               if(!link || (!flex && !other.get_flex()))
-                               {
-                                       set_rotation(other.rotation+other_eps[j].dir-eps[i].dir+M_PI);
-                                       Vector p(epp2.x-(eps[i].pos.x*cos(rotation)-eps[i].pos.y*sin(rotation)),
-                                               epp2.y-(eps[i].pos.y*cos(rotation)+eps[i].pos.x*sin(rotation)),
-                                               epp2.z);
-                                       if(eps.size()==2 && i==1)
-                                               p.z -= slope;
-                                       set_position(p);
-                               }
-
-                               if(link)
-                               {
-                                       if(links[i])
-                                               break_link(*links[i]);
-                                       links[i] = &other;
-                                       other.links[j] = this;
-                                       layout.create_blocks(*this);
-
-                                       signal_link_changed.emit(i, &other);
-                                       other.signal_link_changed.emit(j, this);
-                               }
-
-                               return true;
-                       }
-               }
-       }
-
-       return false;
-}
-
-void Track::break_link(Track &trk)
-{
-       for(vector<Track *>::iterator i=links.begin(); i!=links.end(); ++i)
-               if(*i==&trk)
-               {
-                       *i = 0;
-                       trk.break_link(*this);
-                       // XXX Creates the blocks twice
-                       layout.create_blocks(*this);
-                       signal_link_changed.emit(i-links.begin(), 0);
-                       return;
-               }
-}
-
-void Track::break_links()
-{
-       for(vector<Track *>::iterator i=links.begin(); i!=links.end(); ++i)
-               if(Track *trk=*i)
-               {
-                       *i = 0;
-                       trk->break_link(*this);
-               }
-}
-
-Track *Track::get_link(unsigned i) const
-{
-       if(i>=links.size())
-               throw out_of_range("Track::get_link");
-
-       return links[i];
-}
-
 TrackPoint Track::get_point(unsigned epi, unsigned path, float d) const
 {
        TrackPoint p = type.get_point(epi, path, d);
@@ -346,6 +246,89 @@ SnapType Track::get_default_snap_type_to(const Object &other) const
        return NO_SNAP;
 }
 
+unsigned Track::get_n_link_slots() const
+{
+       return links.size();
+}
+
+Track *Track::get_link(unsigned i) const
+{
+       if(i>=links.size())
+               throw out_of_range("Track::get_link");
+
+       return links[i];
+}
+
+int Track::get_link_slot(const Object &other) const
+{
+       for(unsigned i=0; i<links.size(); ++i)
+               if(links[i]==&other)
+                       return i;
+
+       return -1;
+}
+
+bool Track::link_to(Object &other)
+{
+       Track *otrack = dynamic_cast<Track *>(&other);
+       if(!otrack)
+               return false;
+
+       float limit = layout.get_catalogue().get_gauge();
+       if(!flex && !otrack->get_flex())
+               limit /= 10;
+       limit *= limit;
+
+       unsigned nsn = get_n_snap_nodes();
+       unsigned other_nsn = other.get_n_snap_nodes();
+       for(unsigned i=0; i<nsn; ++i)
+       {
+               Snap sn = get_snap_node(i);
+               for(unsigned j=0; j<other_nsn; ++j)
+               {
+                       Snap osn = other.get_snap_node(j);
+                       Vector d(osn.position.x-sn.position.x, osn.position.y-sn.position.y, osn.position.z-sn.position.z);
+                       float da = osn.rotation-sn.rotation-M_PI;
+                       while(da<-M_PI)
+                               da += M_PI*2;
+                       while(da>M_PI)
+                               da -= M_PI*2;
+
+                       if(d.x*d.x+d.y*d.y<limit && d.z*d.z<limit && da>-0.01 && da<0.01)
+                       {
+                               break_link(i);
+                               links[i] = otrack;
+                               otrack->links[j] = this;
+                               layout.create_blocks(*this);
+
+                               signal_link_changed.emit(i, otrack);
+                               otrack->signal_link_changed.emit(j, this);
+                               return true;
+                       }
+               }
+       }
+
+       return false;
+}
+
+bool Track::break_link(unsigned i)
+{
+       if(i>=links.size())
+               throw out_of_range("Track::break_link");
+
+       Track *other = links[i];
+       if(!other)
+               return false;
+
+       links[i] = 0;
+       other->break_link(*this);
+       // XXX Creates the blocks twice, because the other track calls this too
+       layout.create_blocks(*this);
+       signal_link_changed.emit(i, 0);
+
+       return true;
+}
+
 bool Track::collide_ray(const Vector &start, const Vector &ray) const
 {
        Vector local_start(start.x-position.x, start.y-position.y, start.z-position.z);
index b8a9a3d330bc6f472818f24a006266e111c9552b..7ad7dd141907d18d48ad382fb7d3b300e56f3eeb 100644 (file)
@@ -69,12 +69,6 @@ public:
        unsigned get_active_path() const { return active_path; }
        bool is_path_changing() const { return path_changing; }
 
-       int get_endpoint_by_link(Track &) const;
-       bool snap_to(Track &, bool, float = 0);
-       void break_link(Track &);
-       void break_links();
-       const std::vector<Track *> &get_links() const { return links; }
-       Track *get_link(unsigned) const;
        TrackPoint get_point(unsigned, unsigned, float) const;
        TrackPoint get_point(unsigned, float) const;
 
@@ -85,6 +79,14 @@ private:
        virtual SnapType get_default_snap_type_to(const Object &) const;
 
 public:
+       virtual unsigned get_n_link_slots() const;
+       virtual Track *get_link(unsigned) const;
+       const std::vector<Track *> &get_links() const { return links; }
+       virtual int get_link_slot(const Object &) const;
+       virtual bool link_to(Object &);
+       using Object::break_link;
+       virtual bool break_link(unsigned);
+
        virtual bool collide_ray(const Vector &, const Vector &) const;
 
        void save(std::list<Msp::DataFile::Statement> &) const;
index edb0d73473c3de6ab2d08a1d9b56f1a618ab44d6..c3bb7d4ae5db5e8ab16def585547dfec090cb101 100644 (file)
@@ -95,7 +95,7 @@ TrackIter TrackIter::next(unsigned path) const
 
        TrackIter result;
        result._track = _track->get_link(exit);
-       result._entry = (result._track ? result._track->get_endpoint_by_link(*_track) : 0);
+       result._entry = (result._track ? result._track->get_link_slot(*_track) : 0);
 
        return result;
 }
@@ -127,7 +127,7 @@ TrackIter TrackIter::flip() const
 
        TrackIter result;
        result._track = _track->get_link(_entry);
-       result._entry = (result._track ? result._track->get_endpoint_by_link(*_track) : 0);
+       result._entry = (result._track ? result._track->get_link_slot(*_track) : 0);
 
        return result;
 }
index 250aefd568064e2d3ee979d354af27626803587e..d1819a0fbe5c9e652b7a165970ab0d54bdd19cae 100644 (file)
@@ -398,6 +398,29 @@ TrackPoint Vehicle::get_point(const TrackPosition &pos, float tdist, float ratio
        return get_point(front.get_point().pos, back.get_point().pos, ratio);
 }
 
+unsigned Vehicle::get_n_link_slots() const
+{
+       return 2;
+}
+
+Vehicle *Vehicle::get_link(unsigned i) const
+{
+       if(i>=2)
+               throw out_of_range("Vehicle::get_link");
+
+       return (i==0 ? prev : next);
+}
+
+int Vehicle::get_link_slot(const Object &other) const
+{
+       if(&other==prev)
+               return 0;
+       else if(&other==next)
+               return 1;
+       else
+               return -1;
+}
+
 
 Vehicle::Axle::Axle(const VehicleType::Axle &t):
        type(&t),
index 54054bb50e325cb1babf87408fbbf637999a3ca4..bbd156ceb332347269e6c0811253ec74d344f5c9 100644 (file)
@@ -121,6 +121,10 @@ private:
        TrackPoint get_point(const TrackPosition &, float, float = 0.5) const;
 
 public:
+       virtual unsigned get_n_link_slots() const;
+       virtual Vehicle *get_link(unsigned) const;
+       virtual int get_link_slot(const Object &) const;
+
        virtual bool collide_ray(const Vector &, const Vector &) const { return false; }
 };