]> git.tdb.fi Git - r2c2.git/blobdiff - source/libr2c2/tracktype.cpp
Rename the project to R²C²
[r2c2.git] / source / libr2c2 / tracktype.cpp
diff --git a/source/libr2c2/tracktype.cpp b/source/libr2c2/tracktype.cpp
new file mode 100644 (file)
index 0000000..4ac0967
--- /dev/null
@@ -0,0 +1,194 @@
+/* $Id$
+
+This file is part of R²C²
+Copyright © 2006-2010  Mikkosoft Productions, Mikko Rasa
+Distributed under the GPL
+*/
+
+#include <cmath>
+#include "tracktype.h"
+
+using namespace std;
+using namespace Msp;
+
+namespace R2C2 {
+
+TrackType::TrackType(const ArticleNumber &an):
+       art_nr(an),
+       double_address(false),
+       autofit_preference(1)
+{ }
+
+float TrackType::get_total_length() const
+{
+       return get_path_length(-1);
+}
+
+float TrackType::get_path_length(int p) const
+{
+       float len = 0;
+       for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
+               if(p<0 || i->get_path()==static_cast<unsigned>(p))
+                       len += i->get_length();
+       return len;
+}
+
+unsigned TrackType::get_paths() const
+{
+       unsigned mask = 0;
+       for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
+               mask |= 1<<i->get_path();
+       return mask;
+}
+
+unsigned TrackType::get_n_paths() const
+{
+       unsigned n = 0;
+       for(unsigned mask = get_paths(); mask; ++n)
+               mask &= mask-1;
+       return n;
+}
+
+bool TrackType::is_turnout() const
+{
+       return endpoints.size()>2;
+}
+
+bool TrackType::is_dead_end() const
+{
+       return endpoints.size()<2;
+}
+
+const TrackType::Endpoint &TrackType::get_endpoint(unsigned i) const
+{
+       if(i>=endpoints.size())
+               throw InvalidParameterValue("Endpoint index out of range");
+
+       return endpoints[i];
+}
+
+TrackPoint TrackType::get_point(unsigned epi, unsigned path, float d) const
+{
+       if(epi>=endpoints.size())
+               throw InvalidParameterValue("Endpoint index out of range");
+
+       const TrackPart *part = 0;
+       unsigned part_ep = 0;
+       for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
+       {
+               if((endpoints[epi].paths&(1<<path)) && i->get_path()!=path)
+                       continue;
+
+               unsigned n_part_eps = (i->is_dead_end() ? 1 : 2);
+               for(unsigned j=0; j<n_part_eps; ++j)
+               {
+                       TrackPoint p = i->get_point(j ? i->get_length() : 0);
+                       float dx = p.pos.x-endpoints[epi].pos.x;
+                       float dy = p.pos.y-endpoints[epi].pos.y;
+                       if(dx*dx+dy*dy<1e-6)
+                       {
+                               part = &*i;
+                               part_ep = j;
+                       }
+               }
+       }
+
+       if(!part)
+               throw Exception("Internal error (endpoint does not match any part)");
+
+       while(1)
+       {
+               float plen = part->get_length();
+               if(d<=plen)
+               {
+                       if(part_ep==1)
+                               d = plen-d;
+                       TrackPoint p = part->get_point(d);
+                       if(part_ep==1)
+                               p.dir += M_PI;
+                       return p;
+               }
+               else
+               {
+                       d -= plen;
+                       TrackPart *next = part->get_link(1-part_ep);
+                       if(!next)
+                               throw InvalidParameterValue("Distance out of range");
+                       part_ep = (next->get_link(0)==part ? 0 : 1);
+                       part = next;
+               }
+       }
+}
+
+void TrackType::collect_endpoints()
+{
+       endpoints.clear();
+
+       for(vector<TrackPart>::iterator i=parts.begin(); i!=parts.end(); ++i)
+       {
+               for(vector<TrackPart>::iterator j=i; ++j!=parts.end();)
+                       i->check_link(*j);
+
+               unsigned n_part_eps = (i->is_dead_end() ? 1 : 2);
+               for(unsigned j=0; j<n_part_eps; ++j)
+                       if(!i->get_link(j))
+                       {
+                               TrackPoint p = i->get_point(j ? i->get_length() : 0);
+                               if(j==0)
+                                       p.dir += M_PI;
+
+                               bool found = false;
+                               for(vector<Endpoint>::iterator k=endpoints.begin(); k!=endpoints.end(); ++k)
+                               {
+                                       float dx = k->pos.x-p.pos.x;
+                                       float dy = k->pos.y-p.pos.y;
+
+                                       float da = k->dir-p.dir;
+                                       while(da>M_PI)
+                                               da -= M_PI*2;
+                                       while(da<-M_PI)
+                                               da += M_PI*2;
+
+                                       if(dx*dx+dy*dy<1e-6 && da>-0.01 && da<0.01)
+                                       {
+                                               k->paths |= 1<<i->get_path();
+                                               found = true;
+                                               break;
+                                       }
+                               }
+
+                               if(!found)
+                                       endpoints.push_back(Endpoint(p.pos.x, p.pos.y, p.dir, 1<<i->get_path()));
+                       }
+       }
+}
+
+TrackType::Endpoint::Endpoint(float x, float y, float d, unsigned p):
+       pos(x, y),
+       dir(d),
+       paths(p)
+{ }
+
+
+TrackType::Loader::Loader(TrackType &t):
+       Msp::DataFile::BasicLoader<TrackType>(t)
+{
+       add("autofit_preference", &TrackType::autofit_preference);
+       add("description", &TrackType::description);
+       add("double_address", &TrackType::double_address);
+       add("part",        &Loader::part);
+}
+
+void TrackType::Loader::finish()
+{
+       obj.collect_endpoints();
+}
+
+void TrackType::Loader::part()
+{
+       TrackPart p;
+       load_sub(p);
+       obj.parts.push_back(p);
+}
+
+} // namespace R2C2