]> git.tdb.fi Git - r2c2.git/blobdiff - source/libr2c2/profile.cpp
Allow custom objects for tracks
[r2c2.git] / source / libr2c2 / profile.cpp
index b44ee3606c6018ccf9918e9d4b32cfe263c12488..257b898f39d14fa390e8d58b3b918faf24cc520f 100644 (file)
@@ -13,10 +13,39 @@ using namespace Msp;
 
 namespace R2C2 {
 
-void Profile::append_point(const Point &p)
+void Profile::append_vertex(const Point &p, bool smooth)
 {
-       points.push_back(p);
-       if(points.size()==1)
+       if(vertices.size()>1 && !vertices.back().smooth)
+               vertices.push_back(vertices.back());
+
+       Vertex v;
+       v.pos = p;
+       v.smooth = (!vertices.empty() && smooth);
+
+       if(!vertices.empty())
+       {
+               float dx = p.x-vertices.back().pos.x;
+               float dy = p.y-vertices.back().pos.y;
+               float len = sqrt(dx*dx+dy*dy);
+               v.normal.x = dy/len;
+               v.normal.y = -dx/len;
+
+               if(vertices.back().smooth)
+               {
+                       Point &n = vertices.back().normal;
+                       n.x += v.normal.x;
+                       n.y += v.normal.y;
+                       len = sqrt(n.x*n.x+n.y*n.y);
+                       n.x /= len;
+                       n.y /= len;
+               }
+               else
+                       vertices.back().normal = v.normal;
+       }
+
+       vertices.push_back(v);
+
+       if(vertices.size()==1)
        {
                min_coords = p;
                max_coords = p;
@@ -30,21 +59,11 @@ void Profile::append_point(const Point &p)
        }
 }
 
-const Point &Profile::get_point(unsigned i) const
+const Profile::Vertex &Profile::get_vertex(unsigned i) const
 {
-       if(i>=points.size())
+       if(i>=vertices.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(dy/len, -dx/len);
+       return vertices[i];
 }
 
 
@@ -52,11 +71,17 @@ Profile::Loader::Loader(Profile &p):
        DataFile::ObjectLoader<Profile>(p)
 {
        add("point", &Loader::point);
+       add("smooth_point", &Loader::smooth_point);
 }
 
 void Profile::Loader::point(float x, float y)
 {
-       obj.append_point(Point(x/1000, y/1000));
+       obj.append_vertex(Point(x/1000, y/1000), false);
+}
+
+void Profile::Loader::smooth_point(float x, float y)
+{
+       obj.append_vertex(Point(x/1000, y/1000), true);
 }
 
 } // namespace R2C2