]> git.tdb.fi Git - r2c2.git/blob - source/libmarklin/tracktype.cpp
Major code refactoring:
[r2c2.git] / source / libmarklin / tracktype.cpp
1 #include <cmath>
2 #include "tracktype.h"
3
4 using namespace std;
5
6 namespace Marklin {
7
8 TrackType::TrackType(unsigned a):
9         art_nr(a)
10 { }
11
12 float TrackType::get_total_length() const
13 {
14         float len=0;
15         for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
16         {
17                 float l=i->length;
18                 if(i->radius)
19                         l*=i->radius;
20                 len+=l;
21         }
22         return len;
23 }
24
25 unsigned TrackType::get_n_routes() const
26 {
27         unsigned n=1;
28         for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
29                 if(i->route>=n)
30                         n=i->route+1;
31         return n;
32 }
33
34 void TrackType::collect_endpoints()
35 {
36         endpoints.clear();
37
38         for(vector<TrackPart>::iterator i=parts.begin(); i!=parts.end(); ++i)
39                 i->collect_endpoints(endpoints);
40
41         for(vector<Endpoint>::iterator i=endpoints.begin(); i!=endpoints.end();)
42         {
43                 bool rm=false;
44                 for(vector<Endpoint>::iterator j=i+1; j!=endpoints.end();)
45                 {
46                         float dx=i->x-j->x;
47                         float dy=i->y-j->y;
48                         if(dx*dx+dy*dy<0.0001)
49                         {
50                                 float da=i->dir-j->dir;
51                                 if(da<-M_PI)
52                                         da+=M_PI*2;
53                                 if(da>M_PI)
54                                         da-=M_PI*2;
55                                 if(da<-3.1 || da>3.1)
56                                         rm=true;
57                                 i->routes|=j->routes;
58                                 j=endpoints.erase(j);
59                         }
60                         else
61                                 ++j;
62                 }
63
64                 if(rm)
65                         i=endpoints.erase(i);
66                 else
67                         ++i;
68         }
69 }
70
71
72 TrackType::Loader::Loader(TrackType &t):
73         ttype(t)
74 {
75         add("description", &TrackType::description);
76         add("part",        &Loader::part);
77 }
78
79 void TrackType::Loader::finish()
80 {
81         ttype.collect_endpoints();
82 }
83
84 void TrackType::Loader::part()
85 {
86         TrackPart p;
87         load_sub(p);
88         ttype.parts.push_back(p);
89 }
90
91 } // namespace Marklin