]> git.tdb.fi Git - r2c2.git/blob - source/libr2c2/profile.cpp
Fix remaining exception class names
[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                 float dx = p.x-vertices.back().pos.x;
21                 float dy = p.y-vertices.back().pos.y;
22                 float len = sqrt(dx*dx+dy*dy);
23                 v.normal.x = dy/len;
24                 v.normal.y = -dx/len;
25
26                 if(vertices.back().smooth)
27                 {
28                         Vector &n = vertices.back().normal;
29                         n.x += v.normal.x;
30                         n.y += v.normal.y;
31                         len = sqrt(n.x*n.x+n.y*n.y);
32                         n.x /= len;
33                         n.y /= len;
34                 }
35                 else
36                         vertices.back().normal = v.normal;
37         }
38
39         vertices.push_back(v);
40
41         if(vertices.size()==1)
42         {
43                 min_coords = p;
44                 max_coords = p;
45         }
46         else
47         {
48                 min_coords.x = min(min_coords.x, p.x);
49                 min_coords.y = min(min_coords.y, p.y);
50                 max_coords.x = max(max_coords.x, p.x);
51                 max_coords.y = max(max_coords.y, p.y);
52         }
53 }
54
55 const Profile::Vertex &Profile::get_vertex(unsigned i) const
56 {
57         if(i>=vertices.size())
58                 throw out_of_range("Profile::get_vertex");
59         return vertices[i];
60 }
61
62
63 Profile::Loader::Loader(Profile &p):
64         DataFile::ObjectLoader<Profile>(p)
65 {
66         add("point", &Loader::point);
67         add("smooth_point", &Loader::smooth_point);
68 }
69
70 void Profile::Loader::point(float x, float y)
71 {
72         obj.append_vertex(Vector(x/1000, y/1000), false);
73 }
74
75 void Profile::Loader::smooth_point(float x, float y)
76 {
77         obj.append_vertex(Vector(x/1000, y/1000), true);
78 }
79
80 } // namespace R2C2