mode = ELEVATE;
}
+void Manipulator::start_extend()
+{
+ if(mode)
+ cancel();
+
+ mode = EXTEND;
+}
+
void Manipulator::duplicate()
{
if(mode)
update_tracks();
}
-void Manipulator::cancel()
-{
- if(!mode)
- return;
- mode = NONE;
-
- for(vector<MTrack>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
- {
- i->track->set_position(Point(center.x+i->pos.x, center.y+i->pos.y, center.z+i->pos.z));
- i->track->set_rotation(i->rot);
- }
-
- for(set<Track *>::iterator i=neighbors.begin(); i!=neighbors.end(); ++i)
- (*i)->check_slope();
-
- angle = 0;
-
- signal_done.emit(false);
-}
-
void Manipulator::connect()
{
if(tracks.size()!=2)
if(!ok)
{
- signal_status.emit("No matching endpoints found");
+ signal_status.emit("No aligned endpoints found");
return;
}
- const Catalogue::TrackMap &track_types = designer.get_catalogue().get_tracks();
- std::map<float, const TrackType *> types_by_length;
- unsigned preference = 0;
- for(Catalogue::TrackMap::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<Track *> trks = create_straight(pos1, dir1, gap, limit);
- vector<float> lengths;
- float removed = 0;
- while(gap>limit)
+ if(trks.empty())
{
- 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;
- }
- }
+ signal_status.emit("No connection possible");
+ return;
+ }
- if(found)
- continue;
+ trks.front()->snap_to(*track1, true);
+ trks.back()->snap_to(*track2, true);
- if(lengths.empty())
- {
- if(preference>0)
- {
- --preference;
- removed = 0;
- continue;
- }
- break;
- }
-
- gap += lengths.back();
- removed = lengths.back();
- lengths.pop_back();
- }
+ selection.replace(trks.begin(), trks.end());
+}
- if(lengths.empty())
- {
- signal_status.emit("No connection possible");
+void Manipulator::cancel()
+{
+ if(!mode)
return;
- }
+ mode = NONE;
- float c = cos(dir1);
- float s = sin(dir1);
- for(vector<float>::iterator i=lengths.begin(); i!=lengths.end(); ++i)
+ for(vector<MTrack>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
{
- map<float, const TrackType *>::iterator j = types_by_length.find(*i);
- if(j==types_by_length.end())
- throw LogicError("Internal error");
+ i->track->set_position(Point(center.x+i->pos.x, center.y+i->pos.y, center.z+i->pos.z));
+ i->track->set_rotation(i->rot);
+ }
- Track *track = new Track(designer.get_layout(), *j->second);
- track->set_position(pos1);
- track->set_rotation(dir1);
+ for(set<Track *>::iterator i=neighbors.begin(); i!=neighbors.end(); ++i)
+ (*i)->check_slope();
- track->snap_to(*track1, true);
- track1 = track;
+ for(vector<Track *>::iterator i=extend_tracks.begin(); i!=extend_tracks.end(); ++i)
+ delete *i;
+ extend_tracks.clear();
- pos1.x += c**i;
- pos1.y += s**i;
- }
+ angle = 0;
- track1->snap_to(*track2, true);
+ signal_done.emit(false);
}
void Manipulator::button_press(int, int, float, float, unsigned btn)
{
if(btn==3)
cancel();
- else if(mode)
+ else if(btn==1 && mode)
{
+ Mode m = mode;
mode = NONE;
angle = 0;
- for(set<Track *>::iterator i=neighbors.begin(); i!=neighbors.end(); ++i)
- for(vector<MTrack>::iterator j=tracks.begin(); j!=tracks.end(); ++j)
- j->track->break_link(**i);
+ if(m!=EXTEND)
+ {
+ for(set<Track *>::iterator i=neighbors.begin(); i!=neighbors.end(); ++i)
+ for(vector<MTrack>::iterator j=tracks.begin(); j!=tracks.end(); ++j)
+ j->track->break_link(**i);
+ }
const set<Track *> <racks = designer.get_layout().get_tracks();
for(set<Track *>::const_iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
j->track->snap_to(**i, true);
}
- update_tracks();
- update_neighbors();
+ if(m==EXTEND)
+ {
+ selection.replace(extend_tracks.begin(), extend_tracks.end());
+ extend_tracks.clear();
+ }
+ else
+ {
+ update_tracks();
+ update_neighbors();
+ }
signal_done.emit(true);
}
for(set<Track *>::iterator i=neighbors.begin(); i!=neighbors.end(); ++i)
(*i)->check_slope();
}
+ else if(mode==EXTEND)
+ {
+ Point pos;
+ float dir = 0;
+ float length = 0;
+ for(vector<MTrack>::iterator i=tracks.begin(); i!=tracks.end(); ++i)
+ {
+ unsigned n_endpoints = i->track->get_type().get_endpoints().size();
+ for(unsigned j=0; j<n_endpoints; ++j)
+ {
+ if(i->track->get_link(j))
+ continue;
+
+ Point ep_pos = i->track->get_endpoint_position(j);
+ float ep_dir = i->track->get_endpoint_direction(j);
+ float c = cos(ep_dir);
+ float s = sin(ep_dir);
+ float dx = gx-ep_pos.x;
+ float dy = gy-ep_pos.y;
+
+ float len = dx*c+dy*s;
+ if(len<length)
+ continue;
+
+ pos = ep_pos;
+ dir = ep_dir;
+ length = len;
+ }
+ }
+
+ if(length)
+ {
+ vector<Track *> trks = create_straight(pos, dir, length, max(length/500, 0.001f));
+
+ if(!trks.empty())
+ {
+ for(vector<Track *>::iterator i=extend_tracks.begin(); i!=extend_tracks.end(); ++i)
+ delete *i;
+ extend_tracks = trks;
+
+ length = 0;
+ for(vector<Track *>::iterator i=extend_tracks.begin(); i!=extend_tracks.end(); ++i)
+ length += (*i)->get_type().get_total_length();
+
+ signal_status.emit(format("Extend: %.0fmm", length*1000));
+ }
+ }
+ }
}
void Manipulator::selection_changed()
}
}
+vector<Track *> Manipulator::create_straight(const Marklin::Point &start, float dir, float length, float limit)
+{
+ const Catalogue::TrackMap &track_types = designer.get_catalogue().get_tracks();
+ std::map<float, const TrackType *> types_by_length;
+ unsigned preference = 0;
+ for(Catalogue::TrackMap::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(length>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<length+limit)
+ {
+ unsigned n = static_cast<unsigned>((length+limit)/i->first);
+ lengths.insert(lengths.end(), n, i->first);
+ length -= n*i->first;
+ found = true;
+ break;
+ }
+ }
+
+ if(found)
+ continue;
+
+ if(lengths.empty())
+ {
+ if(preference>0)
+ {
+ --preference;
+ removed = 0;
+ continue;
+ }
+ break;
+ }
+
+ length += lengths.back();
+ removed = lengths.back();
+ lengths.pop_back();
+ }
+
+ vector<Track *> trks;
+
+ if(!lengths.empty())
+ {
+ Point pos = start;
+ float c = cos(dir);
+ float s = sin(dir);
+ 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(pos);
+ track->set_rotation(dir);
+
+ if(!trks.empty())
+ track->snap_to(*trks.back(), true);
+ trks.push_back(track);
+
+ pos.x += c**i;
+ pos.y += s**i;
+ }
+ }
+
+ return trks;
+}
+
+
Manipulator::MTrack::MTrack(Track *t):
track(t),
pos(track->get_position()),