/* $Id$
-This file is part of the MSP Märklin suite
+This file is part of R²C²
Copyright © 2006-2010 Mikkosoft Productions, Mikko Rasa
Distributed under the GPL
*/
#include <algorithm>
#include <cmath>
#include <msp/strings/formatter.h>
-#include "libmarklin/tracktype.h"
+#include "libr2c2/tracktype.h"
#include "designer.h"
#include "manipulator.h"
#include "selection.h"
using namespace std;
-using namespace Marklin;
+using namespace R2C2;
using namespace Msp;
-Manipulator::Manipulator(Designer &d, Selection &s):
+Manipulator::Manipulator(Designer &d, Graphics::EventSource &es, Selection &s):
designer(d),
+ event_source(es),
selection(s),
mode(NONE),
angle(0)
{
+ event_source.signal_button_press.connect(sigc::mem_fun(this, &Manipulator::button_press));
+ event_source.signal_pointer_motion.connect(sigc::mem_fun(this, &Manipulator::pointer_motion));
selection.signal_changed.connect(sigc::mem_fun(this, &Manipulator::selection_changed));
}
mode = ELEVATE;
}
+void Manipulator::start_extend()
+{
+ if(mode)
+ cancel();
+
+ mode = EXTEND;
+}
+
void Manipulator::duplicate()
{
if(mode)
list<Track *> new_tracks;
for(vector<MTrack>::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);
update_tracks();
}
+void Manipulator::connect()
+{
+ if(tracks.size()!=2)
+ {
+ signal_status.emit("Exactly two tracks must be selected");
+ return;
+ }
+
+ float limit = designer.get_layout().get_catalogue().get_gauge()/10;
+
+ Track *track1 = tracks.front().track;
+ Point pos1;
+ float dir1;
+ Track *track2 = tracks.back().track;
+ bool ok = false;
+ float gap = 0;
+ 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 aligned endpoints found");
+ return;
+ }
+
+ vector<Track *> trks = create_straight(pos1, dir1, gap, limit);
+
+ if(trks.empty())
+ {
+ signal_status.emit("No connection possible");
+ return;
+ }
+
+ trks.front()->snap_to(*track1, true);
+ trks.back()->snap_to(*track2, true);
+
+ selection.replace(trks.begin(), trks.end());
+}
+
void Manipulator::cancel()
{
if(!mode)
for(set<Track *>::iterator i=neighbors.begin(); i!=neighbors.end(); ++i)
(*i)->check_slope();
+ for(vector<Track *>::iterator i=extend_tracks.begin(); i!=extend_tracks.end(); ++i)
+ delete *i;
+ extend_tracks.clear();
+
angle = 0;
signal_done.emit(false);
}
-void Manipulator::button_press(int, int, float, float, unsigned btn)
+void Manipulator::button_press(int, int, unsigned btn, unsigned)
{
+ if(!mode)
+ return;
+
if(btn==3)
cancel();
- else if(mode)
+ else if(btn==1)
{
+ 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();
+ const set<Track *> <racks = designer.get_layout().get_tracks();
for(set<Track *>::const_iterator i=ltracks.begin(); i!=ltracks.end(); ++i)
{
bool ok = true;
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);
}
}
-void Manipulator::pointer_motion(int, int y, float gx, float gy)
+void Manipulator::pointer_motion(int x, int y)
{
pointer_y = y;
- gpointer = Point(gx, gy, 0);
+ gpointer = designer.map_pointer_to_ground(x, event_source.get_height()-1-y);
if(mode==MOVE)
{
i->track->set_rotation(i->rot);
}
- const set<Track *> <racks = designer.get_layout()->get_tracks();
+ const set<Track *> <racks = designer.get_layout().get_tracks();
+ float limit = max(designer.get_layout().get_catalogue().get_gauge(),
+ designer.get_camera_controller().get_view_scale()*5/event_source.get_height());
MTrack *snapped = 0;
for(set<Track *>::const_iterator i=ltracks.begin(); (i!=ltracks.end() && !snapped); ++i)
{
if(!ok) continue;
for(vector<MTrack>::iterator j=tracks.begin(); (j!=tracks.end() && !snapped); ++j)
- if(j->track->snap_to(**i, false))
+ if(j->track->snap_to(**i, false, limit))
snapped = &*j;
}
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);
}
}
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 = gpointer.x-ep_pos.x;
+ float dy = gpointer.y-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 R2C2::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()),