Remove spaces and tabs at end of lines.
[synfig.git] / synfig-core / trunk / src / synfig / valuenode_bline.cpp
index 7b95a20..83eb419 100644 (file)
@@ -1,11 +1,12 @@
 /* === S Y N F I G ========================================================= */
 /*!    \file valuenode_bline.cpp
-**     \brief Template File
+**     \brief Implementation of the "BLine" valuenode conversion.
 **
 **     $Id$
 **
 **     \legal
 **     Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
+**     Copyright (c) 2007, 2008 Chris Moore
 **
 **     This package is free software; you can redistribute it and/or
 **     modify it under the terms of the GNU General Public License as
@@ -41,6 +42,7 @@
 #include <ETL/hermite>
 #include <ETL/calculus>
 #include "segment.h"
+#include "curve_helper.h"
 
 #endif
 
@@ -52,10 +54,16 @@ using namespace synfig;
 
 /* === M A C R O S ========================================================= */
 
+#define EPSILON 0.0000001f
+
 /* === G L O B A L S ======================================================= */
 
 /* === P R O C E D U R E S ================================================= */
 
+inline float
+linear_interpolation(const float& a, const float& b, float c)
+{ return (b-a)*c+a; }
+
 inline Vector
 linear_interpolation(const Vector& a, const Vector& b, float c)
 { return (b-a)*c+a; }
@@ -71,12 +79,31 @@ radial_interpolation(const Vector& a, const Vector& b, float c)
        affine_combo<Angle,float> ang_combo;
 
        Real mag(mag_combo(a.mag(),b.mag(),c));
-       Angle ang(ang_combo(Angle::tan(a[1],a[0]),Angle::tan(b[1],b[0]),c));
+       Angle angle_a(Angle::tan(a[1],a[0]));
+       Angle angle_b(Angle::tan(b[1],b[0]));
+       float diff = Angle::deg(angle_b - angle_a).get();
+       if (diff < -180) angle_b += Angle::deg(360);
+       else if (diff > 180) angle_a += Angle::deg(360);
+       Angle ang(ang_combo(angle_a, angle_b, c));
 
        return Point( mag*Angle::cos(ang).get(),mag*Angle::sin(ang).get() );
 }
 
+inline void
+transform_coords(Vector in, Vector& out, const Point& coord_origin, const Point *coord_sys)
+{
+       in -= coord_origin;
+       out[0] = in * coord_sys[0];
+       out[1] = in * coord_sys[1];
+}
 
+inline void
+untransform_coords(const Vector& in, Vector& out, const Point& coord_origin, const Point *coord_sys)
+{
+       out[0] = in * coord_sys[0];
+       out[1] = in * coord_sys[1];
+       out += coord_origin;
+}
 
 ValueBase
 synfig::convert_bline_to_segment_list(const ValueBase& bline)
@@ -142,6 +169,118 @@ synfig::convert_bline_to_width_list(const ValueBase& bline)
        return ValueBase(ret,bline.get_loop());
 }
 
+Real
+synfig::find_closest_point(const ValueBase &bline, const Point &pos, Real &radius, bool loop, Point *out_point)
+{
+       Real d,step;
+       float time = 0;
+       float best_time = 0;
+       int best_index = -1;
+       synfig::Point best_point;
+
+       if(radius==0)radius=10000000;
+       Real closest(10000000);
+
+       int i=0;
+       std::vector<BLinePoint> list(bline.get_list().begin(),bline.get_list().end());
+       typedef std::vector<BLinePoint>::const_iterator iterT;
+       iterT iter, prev, first;
+       for(iter=list.begin(); iter!=list.end(); ++i, ++iter)
+       {
+               if( first == iterT() )
+                       first = iter;
+
+               if( prev != iterT() )
+               {
+                       bezier<Point>   curve;
+
+                       curve[0] = (*prev).get_vertex();
+                       curve[1] = curve[0] + (*prev).get_tangent2()/3;
+                       curve[3] = (*iter).get_vertex();
+                       curve[2] = curve[3] - (*iter).get_tangent1()/3;
+                       curve.sync();
+
+                       #if 0
+                       // I don't know why this doesn't work
+                       time=curve.find_closest(pos,6);
+                       d=((curve(time)-pos).mag_squared());
+
+                       #else
+                       //set the step size based on the size of the picture
+                       d = (curve[1] - curve[0]).mag() + (curve[2]-curve[1]).mag()     + (curve[3]-curve[2]).mag();
+
+                       step = d/(2*radius); //want to make the distance between lines happy
+
+                       step = max(step,0.01); //100 samples should be plenty
+                       step = min(step,0.1); //10 is minimum
+
+                       d = find_closest(curve,pos,step,&closest,&time);
+                       #endif
+
+                       if(d < closest)
+                       {
+                               closest = d;
+                               best_time = time;
+                               best_index = i;
+                               best_point = curve(best_time);
+                       }
+
+               }
+
+               prev = iter;
+       }
+
+       // Loop if necessary
+       if( loop && ( first != iterT() ) && ( prev != iterT() ) )
+       {
+               bezier<Point>   curve;
+
+               curve[0] = (*prev).get_vertex();
+               curve[1] = curve[0] + (*prev).get_tangent2()/3;
+               curve[3] = (*first).get_vertex();
+               curve[2] = curve[3] - (*first).get_tangent1()/3;
+               curve.sync();
+
+               #if 0
+               // I don't know why this doesn't work
+               time=curve.find_closest(pos,6);
+               d=((curve(time)-pos).mag_squared());
+
+               #else
+               //set the step size based on the size of the picture
+               d = (curve[1] - curve[0]).mag() + (curve[2]-curve[1]).mag()     + (curve[3]-curve[2]).mag();
+
+               step = d/(2*radius); //want to make the distance between lines happy
+
+               step = max(step,0.01); //100 samples should be plenty
+               step = min(step,0.1); //10 is minimum
+
+                       d = find_closest(curve,pos,step,&closest,&time);
+               #endif
+
+               if(d < closest)
+               {
+                       closest = d;
+                       best_time = time;
+                       best_index = 0;
+                       best_point = curve(best_time);
+               }
+       }
+
+       if(best_index != -1)
+       {
+               if(out_point)
+                       *out_point = best_point;
+
+               int loop_adjust(loop ? 0 : -1);
+               int size = list.size();
+               Real amount = (best_index + best_time + loop_adjust) / (size + loop_adjust);
+               return amount;
+       }
+
+       return 0.0;
+
+}
 
 /* === M E T H O D S ======================================================= */
 
@@ -261,7 +400,6 @@ ValueNode_BLine::create(const ValueBase &value)
                }
        }
 
-
        return value_node;
 }
 
@@ -270,7 +408,6 @@ ValueNode_BLine::create_list_entry(int index, Time time, Real origin)
 {
        ValueNode_BLine::ListEntry ret;
 
-
        synfig::BLinePoint prev,next;
 
        int prev_i,next_i;
@@ -287,7 +424,7 @@ ValueNode_BLine::create_list_entry(int index, Time time, Real origin)
                next_i=index;
        prev_i=find_prev_valid_entry(index,time);
 
-       synfig::info("index=%d, next_i=%d, prev_i=%d",index,next_i,prev_i);
+       //synfig::info("index=%d, next_i=%d, prev_i=%d",index,next_i,prev_i);
 
        next=(*list[next_i].value_node)(time);
        prev=(*list[prev_i].value_node)(time);
@@ -322,14 +459,17 @@ ValueNode_BLine::operator()(Time t)const
        BLinePoint prev,first;
        first.set_origin(100.0f);
 
+       // loop through all the list's entries
        for(iter=list.begin();iter!=list.end();++iter,index++)
        {
+               // how 'on' is this vertex?
                float amount(iter->amount_at_time(t,&rising));
 
                assert(amount>=0.0f);
                assert(amount<=1.0f);
 
-               if(amount==1.0f)
+               // it's fully on
+               if (amount > 1.0f - EPSILON)
                {
                        if(first_flag)
                        {
@@ -363,8 +503,8 @@ ValueNode_BLine::operator()(Time t)const
 
                        prev=curr;
                }
-               else
-               if(amount>0.0f)
+               // it's partly on
+               else if(amount>0.0f)
                {
                        std::vector<ListEntry>::const_iterator begin_iter,end_iter;
 
@@ -372,22 +512,23 @@ ValueNode_BLine::operator()(Time t)const
                        // We need to seek forward in the list to see what the next
                        // active point is
 
-                       BLinePoint curr;
-                       BLinePoint begin;       // begin of dynamic group
-                       BLinePoint end;         // end of dynamic group
-                       int dist_from_begin(0), dist_from_end(0);
-                       BLinePoint ret;
+                       BLinePoint blp_here_on;  // the current vertex, when fully on
+                       BLinePoint blp_here_off; // the current vertex, when fully off
+                       BLinePoint blp_here_now; // the current vertex, right now (between on and off)
+                       BLinePoint blp_prev_off; // the beginning of dynamic group when fully off
+                       BLinePoint blp_next_off; // the end of the dynamic group when fully off
 
+                       int dist_from_begin(0), dist_from_end(0);
                        Time off_time, on_time;
 
-                       if(!rising)
+                       if(!rising)     // if not rising, then we were fully on in the past, and will be fully off in the future
                        {
                                try{ on_time=iter->find_prev(t)->get_time(); }
                                catch(...) { on_time=Time::begin(); }
                                try{ off_time=iter->find_next(t)->get_time(); }
                                catch(...) { off_time=Time::end(); }
                        }
-                       else
+                       else // otherwise we were fully off in the past, and will be fully on in the future
                        {
                                try{ off_time=iter->find_prev(t)->get_time(); }
                                catch(...) { off_time=Time::begin(); }
@@ -395,40 +536,34 @@ ValueNode_BLine::operator()(Time t)const
                                catch(...) { on_time=Time::end(); }
                        }
 
-                       curr=(*iter->value_node)(on_time).get(curr);
-//                     curr=(*iter->value_node)(t).get(curr);
+                       blp_here_on=(*iter->value_node)(on_time).get(blp_here_on);
+//                     blp_here_on=(*iter->value_node)(t).get(blp_here_on);
 
-                       // Find "end" of dynamic group
+                       // Find "end" of dynamic group - ie. search forward along
+                       // the bline from the current point until we find a point
+                       // which is more 'on' than the current one
                        end_iter=iter;
 //                     for(++end_iter;begin_iter!=list.end();++end_iter)
                        for(++end_iter;end_iter!=list.end();++end_iter)
                                if(end_iter->amount_at_time(t)>amount)
-                               {
-                                       end=(*end_iter->value_node)(off_time).get(prev);
                                        break;
-                               }
 
                        // If we did not find an end of the dynamic group...
+                       // Writeme!  at least now it doesn't crash if first_iter
+                       // isn't set yet
                        if(end_iter==list.end())
                        {
-                               if(get_loop())
-                               {
+                               if(get_loop() && !first_flag)
                                        end_iter=first_iter;
-                                       end=(*end_iter->value_node)(off_time).get(prev);
-//                                     end=first;
-                               }
                                else
-                               {
-                                       // Writeme!
-                                       end_iter=first_iter;
-                                       end=(*end_iter->value_node)(off_time).get(prev);
-//                                     end=first;
-                               }
+                                       end_iter=--list.end();
                        }
 
+                       blp_next_off=(*end_iter->value_node)(off_time).get(prev);
+
                        // Find "begin" of dynamic group
                        begin_iter=iter;
-                       begin.set_origin(100.0f); // set the origin to 100 (which is crazy) so that we can check to see if it was found
+                       blp_prev_off.set_origin(100.0f); // set the origin to 100 (which is crazy) so that we can check to see if it was found
                        do
                        {
                                if(begin_iter==list.begin())
@@ -442,43 +577,40 @@ ValueNode_BLine::operator()(Time t)const
                                --begin_iter;
                                dist_from_begin++;
 
+                               // if we've gone all around the loop, give up
                                if(begin_iter==iter)
                                        break;
 
                                if(begin_iter->amount_at_time(t)>amount)
                                {
-                                       begin=(*begin_iter->value_node)(off_time).get(prev);
+                                       blp_prev_off=(*begin_iter->value_node)(off_time).get(prev);
                                        break;
                                }
-                       }while(begin_iter!=iter);
+                       }while(true);
 
                        // If we did not find a begin
-                       if(begin.get_origin()==100.0f)
+                       if(blp_prev_off.get_origin()==100.0f)
                        {
-                               if(get_loop())
-                               {
-                                       begin_iter=first_iter;
-                                       begin=(*begin_iter->value_node)(off_time).get(prev);
-//                                     begin=first;
-                               }
+                               // Writeme! - this needs work, but at least now it
+                               // doesn't crash
+                               if(first_flag)
+                                       begin_iter=list.begin();
                                else
-                               {
-                                       // Writeme!
                                        begin_iter=first_iter;
-                                       begin=(*begin_iter->value_node)(off_time).get(prev);
-//                                     begin=first;
-                               }
+                               blp_prev_off=(*begin_iter->value_node)(off_time).get(prev);
                        }
 
-                       etl::hermite<Vector> curve(begin.get_vertex(),end.get_vertex(),begin.get_tangent2(),end.get_tangent1());
+                       // this is how the curve looks when we have completely vanished
+                       etl::hermite<Vector> curve(blp_prev_off.get_vertex(),   blp_next_off.get_vertex(),
+                                                                          blp_prev_off.get_tangent2(), blp_next_off.get_tangent1());
                        etl::derivative< etl::hermite<Vector> > deriv(curve);
 
-                       ret.set_vertex(curve(curr.get_origin()));
-
-                       ret.set_width((end.get_width()-begin.get_width())*curr.get_origin()+begin.get_width());
-
-                       ret.set_tangent1(deriv(curr.get_origin()));
-                       ret.set_tangent2(deriv(curr.get_origin()));
+                       // where would we be on this curve, how wide will we be, and
+                       // where will our tangents point (all assuming that we hadn't vanished)
+                       blp_here_off.set_vertex(curve(blp_here_on.get_origin()));
+                       blp_here_off.set_width((blp_next_off.get_width()-blp_prev_off.get_width())*blp_here_on.get_origin()+blp_prev_off.get_width());
+                       blp_here_off.set_tangent1(deriv(blp_here_on.get_origin()));
+                       blp_here_off.set_tangent2(deriv(blp_here_on.get_origin()));
 
                        float prev_tangent_scalar(1.0f);
                        float next_tangent_scalar(1.0f);
@@ -488,72 +620,45 @@ ValueNode_BLine::operator()(Time t)const
 
                        // If we are the next to the begin
                        if(begin_iter==--std::vector<ListEntry>::const_iterator(iter) || dist_from_begin==1)
-                       {
-                               prev_tangent_scalar=(1.0f-curr.get_origin())*amount+curr.get_origin();
-                       }
+                               prev_tangent_scalar=linear_interpolation(blp_here_on.get_origin(), 1.0f, amount);
                        else
-                       {
-                               float origin=curr.get_origin()-prev.get_origin();
-                               prev_tangent_scalar=(1.0f-origin)*amount+origin;
-                       }
+                               prev_tangent_scalar=linear_interpolation(blp_here_on.get_origin()-prev.get_origin(), 1.0f, amount);
 
                        // If we are the next to the end
                        if(end_iter==++std::vector<ListEntry>::const_iterator(iter) || dist_from_end==1)
-                       {
-                               float origin=1.0-curr.get_origin();
-                               next_tangent_scalar=(1.0f-origin)*amount+origin;
-                       }
-                       else
-                       if(list.end()!=++std::vector<ListEntry>::const_iterator(iter))
+                               next_tangent_scalar=linear_interpolation(1.0-blp_here_on.get_origin(), 1.0f, amount);
+                       else if(list.end()!=++std::vector<ListEntry>::const_iterator(iter))
                        {
                                BLinePoint next;
                                next=((*(++std::vector<ListEntry>::const_iterator(iter))->value_node)(t).get(prev));
-                               float origin=next.get_origin()-curr.get_origin();
-                               next_tangent_scalar=(1.0f-origin)*amount+origin;
+                               next_tangent_scalar=linear_interpolation(next.get_origin()-blp_here_on.get_origin(), 1.0f, amount);
                        }
                        else
-                       {
                                //! \todo this isn't quite right; we should handle looped blines identically no matter where the loop happens
                                //! and we currently don't.  this at least makes it a lot better than it was before
-                               float origin=end.get_origin()-curr.get_origin();
-                               next_tangent_scalar=(1.0f-origin)*amount+origin;
-                       }
+                               next_tangent_scalar=linear_interpolation(blp_next_off.get_origin()-blp_here_on.get_origin(), 1.0f, amount);
                        next_scale=next_tangent_scalar;
 
-                       //ret.set_vertex((curr.get_vertex()-ret.get_vertex())*amount+ret.get_vertex());
-//                     if(false)
-//                     {
-//                             // My first try
-//                             Point ref_point_begin(
-//                                     (
-//                                             (*begin_iter->value_node)(off_time).get(prev).get_vertex() +
-//                                             (*end_iter->value_node)(off_time).get(prev).get_vertex()
-//                                     ) * 0.5
-//                             );
-//                             Point ref_point_end(
-//                                     (
-//                                             (*begin_iter->value_node)(on_time).get(prev).get_vertex() +
-//                                             (*end_iter->value_node)(on_time).get(prev).get_vertex()
-//                                     ) * 0.5
-//                             );
-//                             Point ref_point_now(
-//                                     (
-//                                             (*begin_iter->value_node)(t).get(prev).get_vertex() +
-//                                             (*end_iter->value_node)(t).get(prev).get_vertex()
-//                                     ) * 0.5
-//                             );
-//                             Point ref_point_linear((ref_point_end-ref_point_begin)*amount+ref_point_begin);
-//
-//                             ret.set_vertex(
-//                                     (curr.get_vertex()-ret.get_vertex())*amount+ret.get_vertex() +
-//                                     (ref_point_now-ref_point_linear)
-//                             );
-//                             ret.set_tangent1((curr.get_tangent1()-ret.get_tangent1())*amount+ret.get_tangent1());
-//                             ret.set_split_tangent_flag(curr.get_split_tangent_flag());
-//                             if(ret.get_split_tangent_flag())
-//                                     ret.set_tangent2((curr.get_tangent2()-ret.get_tangent2())*amount+ret.get_tangent2());
-//                     }
-//                     else
+                       //blp_here_now.set_vertex(linear_interpolation(blp_here_off.get_vertex(), blp_here_on.get_vertex(), amount));
+                       // if(false)
+                       // {
+                       //      // My first try
+                       //      Point ref_point_begin(((*begin_iter->value_node)(off_time).get(prev).get_vertex() +
+                       //                                                 (*end_iter->value_node)(off_time).get(prev).get_vertex()) * 0.5);
+                       //      Point ref_point_end(((*begin_iter->value_node)(on_time).get(prev).get_vertex() +
+                       //                                               (*end_iter->value_node)(on_time).get(prev).get_vertex()) * 0.5);
+                       //      Point ref_point_now(((*begin_iter->value_node)(t).get(prev).get_vertex() +
+                       //                                               (*end_iter->value_node)(t).get(prev).get_vertex()) * 0.5);
+                       //      Point ref_point_linear(linear_interpolation(ref_point_begin, ref_point_end, amount));
+                       //
+                       //      blp_here_now.set_vertex(linear_interpolation(blp_here_off.get_vertex(), blp_here_on.get_vertex(), amount) +
+                       //                                                      (ref_point_now-ref_point_linear));
+                       //      blp_here_now.set_tangent1(linear_interpolation(blp_here_off.get_tangent1(), blp_here_on.get_tangent1(), amount));
+                       //      blp_here_now.set_split_tangent_flag(blp_here_on.get_split_tangent_flag());
+                       //      if(blp_here_now.get_split_tangent_flag())
+                       //              blp_here_now.set_tangent2(linear_interpolation(blp_here_off.get_tangent2(), blp_here_on.get_tangent2(), amount));
+                       // }
+                       // else
                        {
                                // My second try
 
@@ -582,127 +687,94 @@ ValueNode_BLine::operator()(Time t)const
                                        curr_coord_origin=(begin_pos_at_current_time + end_pos_at_current_time)/2;
                                        curr_coord_sys[0]=(begin_pos_at_current_time - end_pos_at_current_time).norm();
                                        curr_coord_sys[1]=curr_coord_sys[0].perp();
+
+                                       // Invert (transpose) the last of these matrices, since we use it for transform back
+                                       swap(curr_coord_sys[0][1],curr_coord_sys[1][0]);
                                }
 
                                /* The code that was here before used just end_iter as the origin, rather than the mid-point */
 
-                               // For each of the 3 coordinate systems we've just defined, we convert a point and tangent(s) into that system
+                               // We know our location and tangent(s) when fully on and fully off
+                               // Transform each of these into their corresponding coordinate system
+                               Point trans_on_point, trans_off_point;
+                               Vector trans_on_t1, trans_on_t2, trans_off_t1, trans_off_t2;
+
+                               transform_coords(blp_here_on.get_vertex(),  trans_on_point,  on_coord_origin,  on_coord_sys);
+                               transform_coords(blp_here_off.get_vertex(), trans_off_point, off_coord_origin, off_coord_sys);
 
-                               // Convert point where vertex is fully 'off'
-                               Point trans_off_point;
-                               Vector trans_off_t1,trans_off_t2;
-                               {
-                                       Point tmp(ret.get_vertex()-off_coord_origin);
-                                       trans_off_point[0]=tmp*off_coord_sys[0];
-                                       trans_off_point[1]=tmp*off_coord_sys[1];
 #define COORD_SYS_RADIAL_TAN_INTERP 1
 
 #ifdef COORD_SYS_RADIAL_TAN_INTERP
-                                       tmp=ret.get_tangent1()+ret.get_vertex()-off_coord_origin;
-                                       trans_off_t1[0]=tmp*off_coord_sys[0];
-                                       trans_off_t1[1]=tmp*off_coord_sys[1];
+                               transform_coords(blp_here_on.get_tangent1(),  trans_on_t1,  Point::zero(), on_coord_sys);
+                               transform_coords(blp_here_off.get_tangent1(), trans_off_t1, Point::zero(), off_coord_sys);
 
-                                       if(curr.get_split_tangent_flag())
-                                       {
-                                               tmp=ret.get_tangent2()+ret.get_vertex()-off_coord_origin;
-                                               trans_off_t2[0]=tmp*off_coord_sys[0];
-                                               trans_off_t2[1]=tmp*off_coord_sys[1];
-                                       }
-#endif
-                               }
-
-                               // Convert point where vertex is fully 'on'
-                               Point trans_on_point;
-                               Vector trans_on_t1,trans_on_t2;
+                               if(blp_here_on.get_split_tangent_flag())
                                {
-                                       Point tmp(curr.get_vertex()-on_coord_origin);
-                                       trans_on_point[0]=tmp*on_coord_sys[0];
-                                       trans_on_point[1]=tmp*on_coord_sys[1];
-
-#ifdef COORD_SYS_RADIAL_TAN_INTERP
-                                       tmp=curr.get_tangent1()+curr.get_vertex()-on_coord_origin;
-                                       trans_on_t1[0]=tmp*on_coord_sys[0];
-                                       trans_on_t1[1]=tmp*on_coord_sys[1];
-
-                                       if(curr.get_split_tangent_flag())
-                                       {
-                                               tmp=curr.get_tangent2()+curr.get_vertex()-on_coord_origin;
-                                               trans_on_t2[0]=tmp*on_coord_sys[0];
-                                               trans_on_t2[1]=tmp*on_coord_sys[1];
-                                       }
-#endif
+                                       transform_coords(blp_here_on.get_tangent2(),  trans_on_t2,  Point::zero(), on_coord_sys);
+                                       transform_coords(blp_here_off.get_tangent2(), trans_off_t2, Point::zero(), off_coord_sys);
                                }
+#endif
 
-                               // Convert current point
-                               Point trans_curr_point;
-                               Vector trans_curr_t1,trans_curr_t2;
                                {
-                                       // Transpose (invert)
-                                       swap(curr_coord_sys[0][1],curr_coord_sys[1][0]);
-
-                                       Point tmp((trans_on_point-trans_off_point)*amount+trans_off_point);
-                                       trans_curr_point[0]=tmp*curr_coord_sys[0];
-                                       trans_curr_point[1]=tmp*curr_coord_sys[1];
-                                       trans_curr_point+=curr_coord_origin;
+                                       // Interpolate between the 'on' point and the 'off' point and untransform to get our point's location
+                                       Point tmp;
+                                       untransform_coords(linear_interpolation(trans_off_point, trans_on_point, amount),
+                                                                          tmp, curr_coord_origin, curr_coord_sys);
+                                       blp_here_now.set_vertex(tmp);
+                               }
 
 #define INTERP_FUNCTION                radial_interpolation
-//#define INTERP_FUNCTION              linear_interpolation
+//#define INTERP_FUNCTION      linear_interpolation
 
 #ifdef COORD_SYS_RADIAL_TAN_INTERP
-                                       tmp=INTERP_FUNCTION(trans_off_t1,trans_on_t1,amount);
-                                       trans_curr_t1[0]=tmp*curr_coord_sys[0];
-                                       trans_curr_t1[1]=tmp*curr_coord_sys[1];
-                                       trans_curr_t1+=curr_coord_origin;
-                                       trans_curr_t1-=trans_curr_point;
+                               {
+                                       Vector tmp;
+                                       untransform_coords(INTERP_FUNCTION(trans_off_t1,trans_on_t1,amount), tmp, Point::zero(), curr_coord_sys);
+                                       blp_here_now.set_tangent1(tmp);
+                               }
+#else
+                               blp_here_now.set_tangent1(radial_interpolation(blp_here_off.get_tangent1(),blp_here_on.get_tangent1(),amount));
+#endif
 
-                                       if(curr.get_split_tangent_flag())
+                               if (blp_here_on.get_split_tangent_flag())
+                               {
+                                       blp_here_now.set_split_tangent_flag(true);
+#ifdef COORD_SYS_RADIAL_TAN_INTERP
                                        {
-                                               tmp=INTERP_FUNCTION(trans_off_t2,trans_on_t2,amount);
-                                               trans_curr_t2[0]=tmp*curr_coord_sys[0];
-                                               trans_curr_t2[1]=tmp*curr_coord_sys[1];
-                                               trans_curr_t2+=curr_coord_origin;
-                                               trans_curr_t2-=trans_curr_point;
+                                               Vector tmp;
+                                               untransform_coords(INTERP_FUNCTION(trans_off_t2,trans_on_t2,amount), tmp, Point::zero(), curr_coord_sys);
+                                               blp_here_now.set_tangent2(tmp);
                                        }
-#endif
-                               }
-
-                               ret.set_vertex(trans_curr_point);
-#ifndef COORD_SYS_RADIAL_TAN_INTERP
-                               ret.set_tangent1(radial_interpolation(ret.get_tangent1(),curr.get_tangent1(),amount));
-                               ret.set_split_tangent_flag(curr.get_split_tangent_flag());
-                               if(ret.get_split_tangent_flag())
-                                       ret.set_tangent2(radial_interpolation(ret.get_tangent2(),curr.get_tangent2(),amount));
 #else
-                               ret.set_tangent1(trans_curr_t1);
-                               ret.set_split_tangent_flag(curr.get_split_tangent_flag());
-                               if(ret.get_split_tangent_flag())
-                                       ret.set_tangent2(trans_curr_t2);
+                                       blp_here_now.set_tangent2(radial_interpolation(blp_here_off.get_tangent2(),blp_here_on.get_tangent2(),amount));
 #endif
+                               }
+                               else
+                                       blp_here_now.set_split_tangent_flag(false);
                        }
 
-                       ret.set_origin(curr.get_origin());
-                       ret.set_width((curr.get_width()-ret.get_width())*amount+ret.get_width());
-
+                       blp_here_now.set_origin(blp_here_on.get_origin());
+                       blp_here_now.set_width(linear_interpolation(blp_here_off.get_width(), blp_here_on.get_width(), amount));
 
                        // Handle the case where we are the first vertex
                        if(first_flag)
                        {
-                               ret.set_tangent1(ret.get_tangent1()*prev_tangent_scalar);
+                               blp_here_now.set_tangent1(blp_here_now.get_tangent1()*prev_tangent_scalar);
                                first_iter=iter;
-                               first=prev=ret;
+                               first=prev=blp_here_now;
                                first_flag=false;
-                               ret_list.push_back(ret);
+                               ret_list.push_back(blp_here_now);
                                continue;
                        }
 
                        ret_list.back().set_split_tangent_flag(true);
                        ret_list.back().set_tangent2(prev.get_tangent2()*prev_tangent_scalar);
-                       ret_list.push_back(ret);
+                       ret_list.push_back(blp_here_now);
                        ret_list.back().set_split_tangent_flag(true);
-                       //ret_list.back().set_tangent2(ret.get_tangent1());
-                       ret_list.back().set_tangent1(ret.get_tangent1()*prev_tangent_scalar);
+                       //ret_list.back().set_tangent2(blp_here_now.get_tangent1());
+                       ret_list.back().set_tangent1(blp_here_now.get_tangent1()*prev_tangent_scalar);
 
-                       prev=ret;
+                       prev=blp_here_now;
                }
        }
 
@@ -742,35 +814,6 @@ ValueNode_BLine::link_local_name(int i)const
        return etl::strprintf(_("Vertex %03d"),i+1);
 }
 
-ValueNode*
-ValueNode_BLine::clone(const GUID& deriv_guid)const
-{
-       { ValueNode* x(find_value_node(get_guid()^deriv_guid).get()); if(x)return x; }
-
-       ValueNode_BLine* ret=new ValueNode_BLine();
-       ret->set_guid(get_guid()^deriv_guid);
-
-       std::vector<ListEntry>::const_iterator iter;
-
-       for(iter=list.begin();iter!=list.end();++iter)
-       {
-               if(iter->value_node->is_exported())
-                       ret->add(*iter);
-               else
-               {
-                       ListEntry list_entry(*iter);
-                       //list_entry.value_node=find_value_node(iter->value_node->get_guid()^deriv_guid).get();
-                       //if(!list_entry.value_node)
-                               list_entry.value_node=iter->value_node->clone(deriv_guid);
-                       ret->add(list_entry);
-                       //ret->list.back().value_node=iter->value_node.clone();
-               }
-       }
-       ret->set_loop(get_loop());
-
-       return ret;
-}
-
 String
 ValueNode_BLine::get_name()const
 {
@@ -786,8 +829,7 @@ ValueNode_BLine::get_local_name()const
 LinkableValueNode*
 ValueNode_BLine::create_new()const
 {
-       assert(0);
-       return 0;
+       return new ValueNode_BLine();
 }
 
 bool