]> git.tdb.fi Git - r2c2.git/blob - source/libr2c2/profile.cpp
b44ee3606c6018ccf9918e9d4b32cfe263c12488
[r2c2.git] / source / libr2c2 / profile.cpp
1 /* $Id$
2
3 This file is part of R²C²
4 Copyright © 2010 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
6 */
7
8 #include <cmath>
9 #include "profile.h"
10
11 using namespace std;
12 using namespace Msp;
13
14 namespace R2C2 {
15
16 void Profile::append_point(const Point &p)
17 {
18         points.push_back(p);
19         if(points.size()==1)
20         {
21                 min_coords = p;
22                 max_coords = p;
23         }
24         else
25         {
26                 min_coords.x = min(min_coords.x, p.x);
27                 min_coords.y = min(min_coords.y, p.y);
28                 max_coords.x = max(max_coords.x, p.x);
29                 max_coords.y = max(max_coords.y, p.y);
30         }
31 }
32
33 const Point &Profile::get_point(unsigned i) const
34 {
35         if(i>=points.size())
36                 throw InvalidParameterValue("Index out of range");
37         return points[i];
38 }
39
40 Point Profile::get_edge_normal(unsigned i) const
41 {
42         if(i+1>=points.size())
43                 throw InvalidParameterValue("Index out of range");
44         float dx = points[i+1].x-points[i].x;
45         float dy = points[i+1].y-points[i].y;
46         float len = sqrt(dx*dx+dy*dy);
47         return Point(dy/len, -dx/len);
48 }
49
50
51 Profile::Loader::Loader(Profile &p):
52         DataFile::ObjectLoader<Profile>(p)
53 {
54         add("point", &Loader::point);
55 }
56
57 void Profile::Loader::point(float x, float y)
58 {
59         obj.append_point(Point(x/1000, y/1000));
60 }
61
62 } // namespace R2C2