X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fdesigner%2Fmanipulator.cpp;h=850f79f8be25d857721757004b4ab2a3b8535ebf;hb=293bfd4d1a5dbd57c7d3c657e5f6467d9f2e3300;hp=64cbbd4ac68e70745512f4ce56e96809c17f9e0e;hpb=897f9bae5f647bae43e9786796eb9ea18325ec17;p=r2c2.git diff --git a/source/designer/manipulator.cpp b/source/designer/manipulator.cpp index 64cbbd4..850f79f 100644 --- a/source/designer/manipulator.cpp +++ b/source/designer/manipulator.cpp @@ -64,7 +64,7 @@ void Manipulator::duplicate() list new_tracks; for(vector::iterator i=tracks.begin(); i!=tracks.end(); ++i) { - Track *track = new Track(*designer.get_layout(), i->track->get_type()); + Track *track = new Track(designer.get_layout(), i->track->get_type()); track->set_position(i->track->get_position()); track->set_rotation(i->track->get_rotation()); new_tracks.push_back(track); @@ -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 = 0; + for(unsigned i=0; iget_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; jget_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 Catalogue::TrackMap &track_types = designer.get_catalogue().get_tracks(); + std::map types_by_length; + unsigned preference = 0; + for(Catalogue::TrackMap::const_iterator i=track_types.begin(); i!=track_types.end(); ++i) + { + const vector &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 lengths; + float removed = 0; + while(gap>limit) + { + bool found = false; + for(map::iterator i=types_by_length.end(); i!=types_by_length.begin(); ) + { + --i; + if(i->second->get_autofit_preference()firstfirst((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::iterator i=lengths.begin(); i!=lengths.end(); ++i) + { + map::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) @@ -225,7 +375,7 @@ void Manipulator::button_press(int, int, float, float, unsigned btn) for(vector::iterator j=tracks.begin(); j!=tracks.end(); ++j) j->track->break_link(**i); - const set <racks = designer.get_layout()->get_tracks(); + const set <racks = designer.get_layout().get_tracks(); for(set::const_iterator i=ltracks.begin(); i!=ltracks.end(); ++i) { bool ok = true; @@ -259,7 +409,7 @@ void Manipulator::pointer_motion(int, int y, float gx, float gy) i->track->set_rotation(i->rot); } - const set <racks = designer.get_layout()->get_tracks(); + const set <racks = designer.get_layout().get_tracks(); MTrack *snapped = 0; for(set::const_iterator i=ltracks.begin(); (i!=ltracks.end() && !snapped); ++i) { @@ -285,7 +435,7 @@ void Manipulator::pointer_motion(int, int y, float gx, float gy) continue; Point dp(i->pos.x-snapped->pos.x, i->pos.y-snapped->pos.y, 0); - i->track->set_position(Point(sp.x+c*dp.x-s*dp.y, sp.y+s*dp.x+c*dp.y, sp.z)); + i->track->set_position(Point(sp.x+c*dp.x-s*dp.y, sp.y+s*dp.x+c*dp.y, sp.z+i->pos.z-snapped->pos.z)); i->track->set_rotation(i->rot+da); } }