]> git.tdb.fi Git - r2c2.git/blobdiff - source/designer/manipulator.cpp
Add a feature to autofit straight runs of track
[r2c2.git] / source / designer / manipulator.cpp
index 35779a8ccc2896234f91e330b1872e7d75b7d58f..42c422923ab7540c4e686263f1bc4a82040370f2 100644 (file)
@@ -212,6 +212,156 @@ void Manipulator::cancel()
        signal_done.emit(false);
 }
 
+void Manipulator::connect()
+{
+       if(tracks.size()!=2)
+       {
+               signal_status.emit("Exactly two tracks must be selected");
+               return;
+       }
+
+       float limit = 0.001;
+
+       Track *track1 = tracks.front().track;
+       Point pos1;
+       float dir1;
+       Track *track2 = tracks.back().track;
+       bool ok = false;
+       float gap;
+       for(unsigned i=0; i<track1->get_type().get_endpoints().size(); ++i)
+       {
+               if(track1->get_link(i))
+                       continue;
+
+               pos1 = track1->get_endpoint_position(i);
+               dir1 = track1->get_endpoint_direction(i);
+
+               for(unsigned j=0; j<track2->get_type().get_endpoints().size(); ++j)
+               {
+                       if(track2->get_link(j))
+                               continue;
+
+                       Point pos2 = track2->get_endpoint_position(j);
+                       float dir2 = track2->get_endpoint_direction(j);
+
+                       float dz = pos2.z-pos1.z;
+                       if(abs(dz)>0.02)
+                               continue;
+
+                       float adiff = dir1+M_PI-dir2;
+                       while(adiff<-M_PI)
+                               adiff += M_PI*2;
+                       while(adiff>M_PI)
+                               adiff -= M_PI*2;
+                       if(abs(adiff)>0.01)
+                               continue;
+
+                       float c = cos(dir1);
+                       float s = sin(dir1);
+                       float dx = pos2.x-pos1.x;
+                       float dy = pos2.y-pos1.y;
+                       if(abs(dx*s-dy*c)>limit)
+                               continue;
+
+                       gap = dx*c+dy*s;
+                       if(gap<0)
+                               continue;
+
+                       ok = true;
+               }
+
+               if(ok)
+                       break;
+       }
+
+       if(!ok)
+       {
+               signal_status.emit("No matching endpoints found");
+               return;
+       }
+
+       const map<unsigned, TrackType *> &track_types = designer.get_catalogue().get_tracks();
+       std::map<float, const TrackType *> types_by_length;
+       unsigned preference = 0;
+       for(map<unsigned, TrackType *>::const_iterator i=track_types.begin(); i!=track_types.end(); ++i)
+       {
+               const vector<TrackPart> &parts = i->second->get_parts();
+               if(parts.size()!=1)
+                       continue;
+               if(parts.front().is_curved() || parts.front().is_dead_end())
+                       continue;
+
+               types_by_length[parts.front().get_length()] = i->second;
+               preference = max(preference, i->second->get_autofit_preference());
+       }
+
+       vector<float> lengths;
+       float removed = 0;
+       while(gap>limit)
+       {
+               bool found = false;
+               for(map<float, const TrackType *>::iterator i=types_by_length.end(); i!=types_by_length.begin(); )
+               {
+                       --i;
+                       if(i->second->get_autofit_preference()<preference)
+                               continue;
+                       if((!removed || i->first<removed) && i->first<gap+limit)
+                       {
+                               unsigned n = static_cast<unsigned>((gap+limit)/i->first);
+                               lengths.insert(lengths.end(), n, i->first);
+                               gap -= n*i->first;
+                               found = true;
+                               break;
+                       }
+               }
+
+               if(found)
+                       continue;
+
+               if(lengths.empty())
+               {
+                       if(preference>0)
+                       {
+                               --preference;
+                               removed = 0;
+                               continue;
+                       }
+                       break;
+               }
+
+               gap += lengths.back();
+               removed = lengths.back();
+               lengths.pop_back();
+       }
+
+       if(lengths.empty())
+       {
+               signal_status.emit("No connection possible");
+               return;
+       }
+
+       float c = cos(dir1);
+       float s = sin(dir1);
+       for(vector<float>::iterator i=lengths.begin(); i!=lengths.end(); ++i)
+       {
+               map<float, const TrackType *>::iterator j = types_by_length.find(*i);
+               if(j==types_by_length.end())
+                       throw LogicError("Internal error");
+
+               Track *track = new Track(*designer.get_layout(), *j->second);
+               track->set_position(pos1);
+               track->set_rotation(dir1);
+
+               track->snap_to(*track1, true);
+               track1 = track;
+
+               pos1.x += c**i;
+               pos1.y += s**i;
+       }
+
+       track1->snap_to(*track2, true);
+}
+
 void Manipulator::button_press(int, int, float, float, unsigned btn)
 {
        if(btn==3)