]> git.tdb.fi Git - r2c2.git/blobdiff - source/libmarklin/profile.cpp
Split mesh generation from Track3D to TrackType3D
[r2c2.git] / source / libmarklin / profile.cpp
diff --git a/source/libmarklin/profile.cpp b/source/libmarklin/profile.cpp
new file mode 100644 (file)
index 0000000..f4dcf18
--- /dev/null
@@ -0,0 +1,58 @@
+/* $Id$
+
+This file is part of the MSP Märklin suite
+Copyright © 2010 Mikkosoft Productions, Mikko Rasa
+Distributed under the GPL
+*/
+
+#include <cmath>
+#include "profile.h"
+
+using namespace std;
+using namespace Msp;
+
+namespace Marklin {
+
+const Point &Profile::get_point(unsigned i) const
+{
+       if(i>=points.size())
+               throw InvalidParameterValue("Index out of range");
+       return points[i];
+}
+
+Point Profile::get_edge_normal(unsigned i) const
+{
+       if(i+1>=points.size())
+               throw InvalidParameterValue("Index out of range");
+       float dx = points[i+1].x-points[i].x;
+       float dy = points[i+1].y-points[i].y;
+       float len = sqrt(dx*dx+dy*dy);
+       return Point(-dx/len, dy/len);
+}
+
+
+Profile::Loader::Loader(Profile &p):
+       DataFile::ObjectLoader<Profile>(p)
+{
+       add("point", &Loader::point);
+}
+
+void Profile::Loader::finish()
+{
+       obj.min_coords = obj.points[0];
+       obj.max_coords = obj.points[0];
+       for(unsigned i=1; i<obj.points.size(); ++i)
+       {
+               obj.min_coords.x = min(obj.min_coords.x, obj.points[i].x);
+               obj.min_coords.y = min(obj.min_coords.y, obj.points[i].y);
+               obj.max_coords.x = max(obj.max_coords.x, obj.points[i].x);
+               obj.max_coords.y = max(obj.max_coords.y, obj.points[i].y);
+       }
+}
+
+void Profile::Loader::point(float x, float y)
+{
+       obj.points.push_back(Point(x/1000, y/1000));
+}
+
+} // namespace Marklin