]> git.tdb.fi Git - r2c2.git/blob - source/libr2c2/profile.cpp
Don't crash if a train has no router
[r2c2.git] / source / libr2c2 / profile.cpp
1 #include <cmath>
2 #include "profile.h"
3
4 using namespace std;
5 using namespace Msp;
6
7 namespace R2C2 {
8
9 void Profile::append_vertex(const Vector &p, bool smooth)
10 {
11         if(vertices.size()>1 && !vertices.back().smooth)
12                 vertices.push_back(vertices.back());
13
14         Vertex v;
15         v.pos = p;
16         v.smooth = (!vertices.empty() && smooth);
17
18         if(!vertices.empty())
19         {
20                 Vector span = p-vertices.back().pos;
21                 span.normalize();
22                 v.normal.x = span.y;
23                 v.normal.y = -span.x;
24
25                 if(vertices.back().smooth)
26                 {
27                         Vector &n = vertices.back().normal;
28                         n += v.normal;
29                         n.normalize();
30                 }
31                 else
32                         vertices.back().normal = v.normal;
33         }
34
35         vertices.push_back(v);
36
37         if(vertices.size()==1)
38         {
39                 min_coords = p;
40                 max_coords = p;
41         }
42         else
43         {
44                 min_coords.x = min(min_coords.x, p.x);
45                 min_coords.y = min(min_coords.y, p.y);
46                 max_coords.x = max(max_coords.x, p.x);
47                 max_coords.y = max(max_coords.y, p.y);
48         }
49 }
50
51 const Profile::Vertex &Profile::get_vertex(unsigned i) const
52 {
53         if(i>=vertices.size())
54                 throw out_of_range("Profile::get_vertex");
55         return vertices[i];
56 }
57
58
59 Profile::Loader::Loader(Profile &p):
60         DataFile::ObjectLoader<Profile>(p)
61 {
62         add("point", &Loader::point);
63         add("smooth_point", &Loader::smooth_point);
64 }
65
66 void Profile::Loader::point(float x, float y)
67 {
68         obj.append_vertex(Vector(x/1000, y/1000, 0), false);
69 }
70
71 void Profile::Loader::smooth_point(float x, float y)
72 {
73         obj.append_vertex(Vector(x/1000, y/1000, 0), true);
74 }
75
76 } // namespace R2C2