]> git.tdb.fi Git - r2c2.git/blob - source/libmarklin/profile.cpp
Split mesh generation from Track3D to TrackType3D
[r2c2.git] / source / libmarklin / profile.cpp
1 /* $Id$
2
3 This file is part of the MSP Märklin suite
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 Marklin {
15
16 const Point &Profile::get_point(unsigned i) const
17 {
18         if(i>=points.size())
19                 throw InvalidParameterValue("Index out of range");
20         return points[i];
21 }
22
23 Point Profile::get_edge_normal(unsigned i) const
24 {
25         if(i+1>=points.size())
26                 throw InvalidParameterValue("Index out of range");
27         float dx = points[i+1].x-points[i].x;
28         float dy = points[i+1].y-points[i].y;
29         float len = sqrt(dx*dx+dy*dy);
30         return Point(-dx/len, dy/len);
31 }
32
33
34 Profile::Loader::Loader(Profile &p):
35         DataFile::ObjectLoader<Profile>(p)
36 {
37         add("point", &Loader::point);
38 }
39
40 void Profile::Loader::finish()
41 {
42         obj.min_coords = obj.points[0];
43         obj.max_coords = obj.points[0];
44         for(unsigned i=1; i<obj.points.size(); ++i)
45         {
46                 obj.min_coords.x = min(obj.min_coords.x, obj.points[i].x);
47                 obj.min_coords.y = min(obj.min_coords.y, obj.points[i].y);
48                 obj.max_coords.x = max(obj.max_coords.x, obj.points[i].x);
49                 obj.max_coords.y = max(obj.max_coords.y, obj.points[i].y);
50         }
51 }
52
53 void Profile::Loader::point(float x, float y)
54 {
55         obj.points.push_back(Point(x/1000, y/1000));
56 }
57
58 } // namespace Marklin