Rename get_param_vocab to get_children_vocab and use a wrapper for the pure virtual...
[synfig.git] / synfig-core / src / synfig / valuenode_bline.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuenode_bline.cpp
3 **      \brief Implementation of the "BLine" valuenode conversion.
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2007, 2008 Chris Moore
10 **
11 **      This package is free software; you can redistribute it and/or
12 **      modify it under the terms of the GNU General Public License as
13 **      published by the Free Software Foundation; either version 2 of
14 **      the License, or (at your option) any later version.
15 **
16 **      This package is distributed in the hope that it will be useful,
17 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
18 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 **      General Public License for more details.
20 **      \endlegal
21 */
22 /* ========================================================================= */
23
24 /* === H E A D E R S ======================================================= */
25
26 #ifdef USING_PCH
27 #       include "pch.h"
28 #else
29 #ifdef HAVE_CONFIG_H
30 #       include <config.h>
31 #endif
32
33 #include "valuenode_bline.h"
34 #include "valuenode_const.h"
35 #include "valuenode_composite.h"
36 #include "general.h"
37 #include "exception.h"
38 #include "blinepoint.h"
39 #include <vector>
40 #include <list>
41 #include <algorithm>
42 #include <ETL/hermite>
43 #include <ETL/calculus>
44 #include "segment.h"
45 #include "curve_helper.h"
46
47 #endif
48
49 /* === U S I N G =========================================================== */
50
51 using namespace std;
52 using namespace etl;
53 using namespace synfig;
54
55 /* === M A C R O S ========================================================= */
56
57 #define EPSILON 0.0000001f
58
59 /* === G L O B A L S ======================================================= */
60
61 /* === P R O C E D U R E S ================================================= */
62
63 inline float
64 linear_interpolation(const float& a, const float& b, float c)
65 { return (b-a)*c+a; }
66
67 inline Vector
68 linear_interpolation(const Vector& a, const Vector& b, float c)
69 { return (b-a)*c+a; }
70
71 inline Vector
72 radial_interpolation(const Vector& a, const Vector& b, float c)
73 {
74         // if either extreme is zero then use linear interpolation instead
75         if (a.is_equal_to(Vector::zero()) || b.is_equal_to(Vector::zero()))
76                 return linear_interpolation(a, b, c);
77
78         affine_combo<Real,float> mag_combo;
79         affine_combo<Angle,float> ang_combo;
80
81         Real mag(mag_combo(a.mag(),b.mag(),c));
82         Angle angle_a(Angle::tan(a[1],a[0]));
83         Angle angle_b(Angle::tan(b[1],b[0]));
84         float diff = Angle::deg(angle_b - angle_a).get();
85         if (diff < -180) angle_b += Angle::deg(360);
86         else if (diff > 180) angle_a += Angle::deg(360);
87         Angle ang(ang_combo(angle_a, angle_b, c));
88
89         return Point( mag*Angle::cos(ang).get(),mag*Angle::sin(ang).get() );
90 }
91
92 inline void
93 transform_coords(Vector in, Vector& out, const Point& coord_origin, const Point *coord_sys)
94 {
95         in -= coord_origin;
96         out[0] = in * coord_sys[0];
97         out[1] = in * coord_sys[1];
98 }
99
100 inline void
101 untransform_coords(const Vector& in, Vector& out, const Point& coord_origin, const Point *coord_sys)
102 {
103         out[0] = in * coord_sys[0];
104         out[1] = in * coord_sys[1];
105         out += coord_origin;
106 }
107
108 ValueBase
109 synfig::convert_bline_to_segment_list(const ValueBase& bline)
110 {
111         std::vector<Segment> ret;
112
113 //      std::vector<BLinePoint> list(bline.operator std::vector<BLinePoint>());
114         //std::vector<BLinePoint> list(bline);
115         std::vector<BLinePoint> list(bline.get_list().begin(),bline.get_list().end());
116         std::vector<BLinePoint>::const_iterator iter;
117
118         BLinePoint prev,first;
119
120         //start with prev = first and iter on the second...
121
122         if(list.empty()) return ValueBase(ret,bline.get_loop());
123         first = prev = list.front();
124
125         for(iter=++list.begin();iter!=list.end();++iter)
126         {
127                 ret.push_back(
128                         Segment(
129                                 prev.get_vertex(),
130                                 prev.get_tangent2(),
131                                 iter->get_vertex(),
132                                 iter->get_tangent1()
133                         )
134                 );
135                 prev=*iter;
136         }
137         if(bline.get_loop())
138         {
139                 ret.push_back(
140                         Segment(
141                                 prev.get_vertex(),
142                                 prev.get_tangent2(),
143                                 first.get_vertex(),
144                                 first.get_tangent1()
145                         )
146                 );
147         }
148         return ValueBase(ret,bline.get_loop());
149 }
150
151 ValueBase
152 synfig::convert_bline_to_width_list(const ValueBase& bline)
153 {
154         std::vector<Real> ret;
155 //      std::vector<BLinePoint> list(bline.operator std::vector<BLinePoint>());
156         //std::vector<BLinePoint> list(bline);
157         std::vector<BLinePoint> list(bline.get_list().begin(),bline.get_list().end());
158         std::vector<BLinePoint>::const_iterator iter;
159
160         if(bline.empty())
161                 return ValueBase(ValueBase::TYPE_LIST);
162
163         for(iter=list.begin();iter!=list.end();++iter)
164                 ret.push_back(iter->get_width());
165
166         if(bline.get_loop())
167                 ret.push_back(list.front().get_width());
168
169         return ValueBase(ret,bline.get_loop());
170 }
171
172 Real
173 synfig::find_closest_point(const ValueBase &bline, const Point &pos, Real &radius, bool loop, Point *out_point)
174 {
175         Real d,step;
176         float time = 0;
177         float best_time = 0;
178         int best_index = -1;
179         synfig::Point best_point;
180
181         if(radius==0)radius=10000000;
182         Real closest(10000000);
183
184         int i=0;
185         std::vector<BLinePoint> list(bline.get_list().begin(),bline.get_list().end());
186         typedef std::vector<BLinePoint>::const_iterator iterT;
187         iterT iter, prev, first;
188         for(iter=list.begin(); iter!=list.end(); ++i, ++iter)
189         {
190                 if( first == iterT() )
191                         first = iter;
192
193                 if( prev != iterT() )
194                 {
195                         bezier<Point>   curve;
196
197                         curve[0] = (*prev).get_vertex();
198                         curve[1] = curve[0] + (*prev).get_tangent2()/3;
199                         curve[3] = (*iter).get_vertex();
200                         curve[2] = curve[3] - (*iter).get_tangent1()/3;
201                         curve.sync();
202
203                         #if 0
204                         // I don't know why this doesn't work
205                         time=curve.find_closest(pos,6);
206                         d=((curve(time)-pos).mag_squared());
207
208                         #else
209                         //set the step size based on the size of the picture
210                         d = (curve[1] - curve[0]).mag() + (curve[2]-curve[1]).mag()     + (curve[3]-curve[2]).mag();
211
212                         step = d/(2*radius); //want to make the distance between lines happy
213
214                         step = max(step,0.01); //100 samples should be plenty
215                         step = min(step,0.1); //10 is minimum
216
217                         d = find_closest(curve,pos,step,&closest,&time);
218                         #endif
219
220                         if(d < closest)
221                         {
222                                 closest = d;
223                                 best_time = time;
224                                 best_index = i;
225                                 best_point = curve(best_time);
226                         }
227
228                 }
229
230                 prev = iter;
231         }
232
233         // Loop if necessary
234         if( loop && ( first != iterT() ) && ( prev != iterT() ) )
235         {
236                 bezier<Point>   curve;
237
238                 curve[0] = (*prev).get_vertex();
239                 curve[1] = curve[0] + (*prev).get_tangent2()/3;
240                 curve[3] = (*first).get_vertex();
241                 curve[2] = curve[3] - (*first).get_tangent1()/3;
242                 curve.sync();
243
244                 #if 0
245                 // I don't know why this doesn't work
246                 time=curve.find_closest(pos,6);
247                 d=((curve(time)-pos).mag_squared());
248
249                 #else
250                 //set the step size based on the size of the picture
251                 d = (curve[1] - curve[0]).mag() + (curve[2]-curve[1]).mag()     + (curve[3]-curve[2]).mag();
252
253                 step = d/(2*radius); //want to make the distance between lines happy
254
255                 step = max(step,0.01); //100 samples should be plenty
256                 step = min(step,0.1); //10 is minimum
257
258                         d = find_closest(curve,pos,step,&closest,&time);
259                 #endif
260
261                 if(d < closest)
262                 {
263                         closest = d;
264                         best_time = time;
265                         best_index = 0;
266                         best_point = curve(best_time);
267                 }
268         }
269
270         if(best_index != -1)
271         {
272                 if(out_point)
273                         *out_point = best_point;
274
275                 int loop_adjust(loop ? 0 : -1);
276                 int size = list.size();
277                 Real amount = (best_index + best_time + loop_adjust) / (size + loop_adjust);
278                 return amount;
279         }
280
281         return 0.0;
282
283 }
284
285 /* === M E T H O D S ======================================================= */
286
287
288 ValueNode_BLine::ValueNode_BLine():
289         ValueNode_DynamicList(ValueBase::TYPE_BLINEPOINT)
290 {
291 }
292
293 ValueNode_BLine::~ValueNode_BLine()
294 {
295 }
296
297 ValueNode_BLine*
298 ValueNode_BLine::create(const ValueBase &value)
299 {
300         if(value.get_type()!=ValueBase::TYPE_LIST)
301                 return 0;
302
303         ValueNode_BLine* value_node(new ValueNode_BLine());
304
305         if(!value.empty())
306         {
307                 switch(value.get_contained_type())
308                 {
309                 case ValueBase::TYPE_BLINEPOINT:
310                 {
311 //                      std::vector<BLinePoint> bline_points(value.operator std::vector<BLinePoint>());
312                         //std::vector<BLinePoint> bline_points(value);
313                         std::vector<BLinePoint> bline_points(value.get_list().begin(),value.get_list().end());
314                         std::vector<BLinePoint>::const_iterator iter;
315
316                         for(iter=bline_points.begin();iter!=bline_points.end();iter++)
317                         {
318                                 value_node->add(ValueNode::Handle(ValueNode_Composite::create(*iter)));
319                         }
320                         value_node->set_loop(value.get_loop());
321                 }
322                         break;
323                 case ValueBase::TYPE_SEGMENT:
324                 {
325                         // Here, we want to convert a list of segments
326                         // into a list of BLinePoints. We make an assumption
327                         // that the segment list is continuous(sp), but not necessarily
328                         // smooth.
329
330                         value_node->set_loop(false);
331 //                      std::vector<Segment> segments(value.operator std::vector<Segment>());
332 //                      std::vector<Segment> segments(value);
333                         std::vector<Segment> segments(value.get_list().begin(),value.get_list().end());
334                         std::vector<Segment>::const_iterator iter,last(segments.end());
335                         --last;
336                         ValueNode_Const::Handle prev,first;
337
338                         for(iter=segments.begin();iter!=segments.end();iter++)
339                         {
340 #define PREV_POINT      prev->get_value().get(BLinePoint())
341 #define FIRST_POINT     first->get_value().get(BLinePoint())
342 #define CURR_POINT      curr->get_value().get(BLinePoint())
343                                 if(iter==segments.begin())
344                                 {
345                                         prev=ValueNode_Const::create(ValueBase::TYPE_BLINEPOINT);
346                                         {
347                                                 BLinePoint prev_point(PREV_POINT);
348                                                 prev_point.set_vertex(iter->p1);
349                                                 prev_point.set_tangent1(iter->t1);
350                                                 prev_point.set_width(0.01);
351                                                 prev_point.set_origin(0.5);
352                                                 prev_point.set_split_tangent_flag(false);
353                                                 prev->set_value(prev_point);
354                                         }
355                                         first=prev;
356                                         value_node->add(ValueNode::Handle(prev));
357
358                                 }
359                                 if(iter==last && iter->p2.is_equal_to(FIRST_POINT.get_vertex()))
360                                 {
361                                         value_node->set_loop(true);
362                                         if(!iter->t2.is_equal_to(FIRST_POINT.get_tangent1()))
363                                         {
364                                                 BLinePoint first_point(FIRST_POINT);
365                                                 first_point.set_tangent1(iter->t2);
366                                                 first->set_value(first_point);
367                                         }
368                                         continue;
369                                 }
370
371                                 ValueNode_Const::Handle curr;
372                                 curr=ValueNode_Const::create(ValueBase::TYPE_BLINEPOINT);
373                                 {
374                                         BLinePoint curr_point(CURR_POINT);
375                                         curr_point.set_vertex(iter->p2);
376                                         curr_point.set_tangent1(iter->t2);
377                                         curr_point.set_width(0.01);
378                                         curr_point.set_origin(0.5);
379                                         curr_point.set_split_tangent_flag(false);
380                                         curr->set_value(curr_point);
381                                 }
382                                 if(!PREV_POINT.get_tangent1().is_equal_to(iter->t1))
383                                 {
384                                         BLinePoint prev_point(PREV_POINT);
385                                         prev_point.set_split_tangent_flag(true);
386                                         prev_point.set_tangent2(iter->t1);
387                                         prev->set_value(prev_point);
388                                 }
389                                 value_node->add(ValueNode::Handle(curr));
390                                 prev=curr;
391                         }
392
393                 }
394                         break;
395                 default:
396                         // We got a list of who-knows-what. We don't have any idea
397                         // what to do with it.
398                         return 0;
399                         break;
400                 }
401         }
402
403         return value_node;
404 }
405
406 ValueNode_BLine::ListEntry
407 ValueNode_BLine::create_list_entry(int index, Time time, Real origin)
408 {
409         ValueNode_BLine::ListEntry ret;
410
411         synfig::BLinePoint prev,next;
412
413         int prev_i,next_i;
414
415         index=index%link_count();
416
417         assert(index>=0);
418         ret.index=index;
419         ret.set_parent_value_node(this);
420
421         if(!list[index].status_at_time(time))
422                 next_i=find_next_valid_entry(index,time);
423         else
424                 next_i=index;
425         prev_i=find_prev_valid_entry(index,time);
426
427         //synfig::info("index=%d, next_i=%d, prev_i=%d",index,next_i,prev_i);
428
429         next=(*list[next_i].value_node)(time);
430         prev=(*list[prev_i].value_node)(time);
431
432         etl::hermite<Vector> curve(prev.get_vertex(),next.get_vertex(),prev.get_tangent2(),next.get_tangent1());
433         etl::derivative< etl::hermite<Vector> > deriv(curve);
434
435         synfig::BLinePoint bline_point;
436         bline_point.set_vertex(curve(origin));
437         bline_point.set_width((next.get_width()-prev.get_width())*origin+prev.get_width());
438         bline_point.set_tangent1(deriv(origin)*min(1.0-origin,origin));
439         bline_point.set_tangent2(bline_point.get_tangent1());
440         bline_point.set_split_tangent_flag(false);
441         bline_point.set_origin(origin);
442
443         ret.value_node=ValueNode_Composite::create(bline_point);
444
445         return ret;
446 }
447
448 // Remove this variable because it is not used.
449 //static int instance_count;
450
451 ValueBase
452 ValueNode_BLine::operator()(Time t)const
453 {
454         if (getenv("SYNFIG_DEBUG_VALUENODE_OPERATORS"))
455                 printf("%s:%d operator()\n", __FILE__, __LINE__);
456
457         std::vector<BLinePoint> ret_list;
458
459         std::vector<ListEntry>::const_iterator iter,first_iter;
460         bool first_flag(true);
461         bool rising;
462         int index(0);
463         float next_scale(1.0f);
464
465         BLinePoint prev,first;
466         first.set_origin(100.0f);
467
468         // loop through all the list's entries
469         for(iter=list.begin();iter!=list.end();++iter,index++)
470         {
471                 // how 'on' is this vertex?
472                 float amount(iter->amount_at_time(t,&rising));
473
474                 assert(amount>=0.0f);
475                 assert(amount<=1.0f);
476
477                 // it's fully on
478                 if (amount > 1.0f - EPSILON)
479                 {
480                         if(first_flag)
481                         {
482                                 first_iter=iter;
483                                 first=prev=(*iter->value_node)(t).get(prev);
484                                 first_flag=false;
485                                 ret_list.push_back(first);
486                                 continue;
487                         }
488
489                         BLinePoint curr;
490                         curr=(*iter->value_node)(t).get(prev);
491
492                         if(next_scale!=1.0f)
493                         {
494                                 ret_list.back().set_split_tangent_flag(true);
495                                 ret_list.back().set_tangent2(prev.get_tangent2()*next_scale);
496
497                                 ret_list.push_back(curr);
498
499                                 ret_list.back().set_split_tangent_flag(true);
500                                 ret_list.back().set_tangent2(curr.get_tangent2());
501                                 ret_list.back().set_tangent1(curr.get_tangent1()*next_scale);
502
503                                 next_scale=1.0f;
504                         }
505                         else
506                         {
507                                 ret_list.push_back(curr);
508                         }
509
510                         prev=curr;
511                 }
512                 // it's partly on
513                 else if(amount>0.0f)
514                 {
515                         std::vector<ListEntry>::const_iterator begin_iter,end_iter;
516
517                         // This is where the interesting stuff happens
518                         // We need to seek forward in the list to see what the next
519                         // active point is
520
521                         BLinePoint blp_here_on;  // the current vertex, when fully on
522                         BLinePoint blp_here_off; // the current vertex, when fully off
523                         BLinePoint blp_here_now; // the current vertex, right now (between on and off)
524                         BLinePoint blp_prev_off; // the beginning of dynamic group when fully off
525                         BLinePoint blp_next_off; // the end of the dynamic group when fully off
526
527                         int dist_from_begin(0), dist_from_end(0);
528                         Time off_time, on_time;
529
530                         if(!rising)     // if not rising, then we were fully on in the past, and will be fully off in the future
531                         {
532                                 try{ on_time=iter->find_prev(t)->get_time(); }
533                                 catch(...) { on_time=Time::begin(); }
534                                 try{ off_time=iter->find_next(t)->get_time(); }
535                                 catch(...) { off_time=Time::end(); }
536                         }
537                         else // otherwise we were fully off in the past, and will be fully on in the future
538                         {
539                                 try{ off_time=iter->find_prev(t)->get_time(); }
540                                 catch(...) { off_time=Time::begin(); }
541                                 try{ on_time=iter->find_next(t)->get_time(); }
542                                 catch(...) { on_time=Time::end(); }
543                         }
544
545                         blp_here_on=(*iter->value_node)(on_time).get(blp_here_on);
546 //                      blp_here_on=(*iter->value_node)(t).get(blp_here_on);
547
548                         // Find "end" of dynamic group - ie. search forward along
549                         // the bline from the current point until we find a point
550                         // which is more 'on' than the current one
551                         end_iter=iter;
552 //                      for(++end_iter;begin_iter!=list.end();++end_iter)
553                         for(++end_iter;end_iter!=list.end();++end_iter)
554                                 if(end_iter->amount_at_time(t)>amount)
555                                         break;
556
557                         // If we did not find an end of the dynamic group...
558                         // Writeme!  at least now it doesn't crash if first_iter
559                         // isn't set yet
560                         if(end_iter==list.end())
561                         {
562                                 if(get_loop() && !first_flag)
563                                         end_iter=first_iter;
564                                 else
565                                         end_iter=--list.end();
566                         }
567
568                         blp_next_off=(*end_iter->value_node)(off_time).get(prev);
569
570                         // Find "begin" of dynamic group
571                         begin_iter=iter;
572                         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
573                         do
574                         {
575                                 if(begin_iter==list.begin())
576                                 {
577                                         if(get_loop())
578                                                 begin_iter=list.end();
579                                         else
580                                                 break;
581                                 }
582
583                                 --begin_iter;
584                                 dist_from_begin++;
585
586                                 // if we've gone all around the loop, give up
587                                 if(begin_iter==iter)
588                                         break;
589
590                                 if(begin_iter->amount_at_time(t)>amount)
591                                 {
592                                         blp_prev_off=(*begin_iter->value_node)(off_time).get(prev);
593                                         break;
594                                 }
595                         }while(true);
596
597                         // If we did not find a begin
598                         if(blp_prev_off.get_origin()==100.0f)
599                         {
600                                 // Writeme! - this needs work, but at least now it
601                                 // doesn't crash
602                                 if(first_flag)
603                                         begin_iter=list.begin();
604                                 else
605                                         begin_iter=first_iter;
606                                 blp_prev_off=(*begin_iter->value_node)(off_time).get(prev);
607                         }
608
609                         // this is how the curve looks when we have completely vanished
610                         etl::hermite<Vector> curve(blp_prev_off.get_vertex(),   blp_next_off.get_vertex(),
611                                                                            blp_prev_off.get_tangent2(), blp_next_off.get_tangent1());
612                         etl::derivative< etl::hermite<Vector> > deriv(curve);
613
614                         // where would we be on this curve, how wide will we be, and
615                         // where will our tangents point (all assuming that we hadn't vanished)
616                         blp_here_off.set_vertex(curve(blp_here_on.get_origin()));
617                         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());
618                         blp_here_off.set_tangent1(deriv(blp_here_on.get_origin()));
619                         blp_here_off.set_tangent2(deriv(blp_here_on.get_origin()));
620
621                         float prev_tangent_scalar(1.0f);
622                         float next_tangent_scalar(1.0f);
623
624                         //synfig::info("index_%d:dist_from_begin=%d",index,dist_from_begin);
625                         //synfig::info("index_%d:dist_from_end=%d",index,dist_from_end);
626
627                         // If we are the next to the begin
628                         if(begin_iter==--std::vector<ListEntry>::const_iterator(iter) || dist_from_begin==1)
629                                 prev_tangent_scalar=linear_interpolation(blp_here_on.get_origin(), 1.0f, amount);
630                         else
631                                 prev_tangent_scalar=linear_interpolation(blp_here_on.get_origin()-prev.get_origin(), 1.0f, amount);
632
633                         // If we are the next to the end
634                         if(end_iter==++std::vector<ListEntry>::const_iterator(iter) || dist_from_end==1)
635                                 next_tangent_scalar=linear_interpolation(1.0-blp_here_on.get_origin(), 1.0f, amount);
636                         else if(list.end()!=++std::vector<ListEntry>::const_iterator(iter))
637                         {
638                                 BLinePoint next;
639                                 next=((*(++std::vector<ListEntry>::const_iterator(iter))->value_node)(t).get(prev));
640                                 next_tangent_scalar=linear_interpolation(next.get_origin()-blp_here_on.get_origin(), 1.0f, amount);
641                         }
642                         else
643                                 //! \todo this isn't quite right; we should handle looped blines identically no matter where the loop happens
644                                 //! and we currently don't.  this at least makes it a lot better than it was before
645                                 next_tangent_scalar=linear_interpolation(blp_next_off.get_origin()-blp_here_on.get_origin(), 1.0f, amount);
646                         next_scale=next_tangent_scalar;
647
648                         //blp_here_now.set_vertex(linear_interpolation(blp_here_off.get_vertex(), blp_here_on.get_vertex(), amount));
649                         // if(false)
650                         // {
651                         //      // My first try
652                         //      Point ref_point_begin(((*begin_iter->value_node)(off_time).get(prev).get_vertex() +
653                         //                                                 (*end_iter->value_node)(off_time).get(prev).get_vertex()) * 0.5);
654                         //      Point ref_point_end(((*begin_iter->value_node)(on_time).get(prev).get_vertex() +
655                         //                                               (*end_iter->value_node)(on_time).get(prev).get_vertex()) * 0.5);
656                         //      Point ref_point_now(((*begin_iter->value_node)(t).get(prev).get_vertex() +
657                         //                                               (*end_iter->value_node)(t).get(prev).get_vertex()) * 0.5);
658                         //      Point ref_point_linear(linear_interpolation(ref_point_begin, ref_point_end, amount));
659                         //
660                         //      blp_here_now.set_vertex(linear_interpolation(blp_here_off.get_vertex(), blp_here_on.get_vertex(), amount) +
661                         //                                                      (ref_point_now-ref_point_linear));
662                         //      blp_here_now.set_tangent1(linear_interpolation(blp_here_off.get_tangent1(), blp_here_on.get_tangent1(), amount));
663                         //      blp_here_now.set_split_tangent_flag(blp_here_on.get_split_tangent_flag());
664                         //      if(blp_here_now.get_split_tangent_flag())
665                         //              blp_here_now.set_tangent2(linear_interpolation(blp_here_off.get_tangent2(), blp_here_on.get_tangent2(), amount));
666                         // }
667                         // else
668                         {
669                                 // My second try
670
671                                 // define 3 coordinate systems:
672                                 Point off_coord_sys[2],   off_coord_origin; // when the current vertex is completely off
673                                 Point on_coord_sys[2] ,    on_coord_origin; // when the current vertex is completely on
674                                 Point curr_coord_sys[2], curr_coord_origin; // the current state - somewhere in between
675
676                                 // for each of the 3 systems, the origin is half way between the previous and next active point
677                                 // and the axes are based on a vector from the next active point to the previous
678                                 {
679                                         const Point   end_pos_at_off_time((  *end_iter->value_node)(off_time).get(prev).get_vertex());
680                                         const Point begin_pos_at_off_time((*begin_iter->value_node)(off_time).get(prev).get_vertex());
681                                         off_coord_origin=(begin_pos_at_off_time + end_pos_at_off_time)/2;
682                                         off_coord_sys[0]=(begin_pos_at_off_time - end_pos_at_off_time).norm();
683                                         off_coord_sys[1]=off_coord_sys[0].perp();
684
685                                         const Point   end_pos_at_on_time((  *end_iter->value_node)(on_time).get(prev).get_vertex());
686                                         const Point begin_pos_at_on_time((*begin_iter->value_node)(on_time).get(prev).get_vertex());
687                                         on_coord_origin=(begin_pos_at_on_time + end_pos_at_on_time)/2;
688                                         on_coord_sys[0]=(begin_pos_at_on_time - end_pos_at_on_time).norm();
689                                         on_coord_sys[1]=on_coord_sys[0].perp();
690
691                                         const Point   end_pos_at_current_time((  *end_iter->value_node)(t).get(prev).get_vertex());
692                                         const Point begin_pos_at_current_time((*begin_iter->value_node)(t).get(prev).get_vertex());
693                                         curr_coord_origin=(begin_pos_at_current_time + end_pos_at_current_time)/2;
694                                         curr_coord_sys[0]=(begin_pos_at_current_time - end_pos_at_current_time).norm();
695                                         curr_coord_sys[1]=curr_coord_sys[0].perp();
696
697                                         // Invert (transpose) the last of these matrices, since we use it for transform back
698                                         swap(curr_coord_sys[0][1],curr_coord_sys[1][0]);
699                                 }
700
701                                 /* The code that was here before used just end_iter as the origin, rather than the mid-point */
702
703                                 // We know our location and tangent(s) when fully on and fully off
704                                 // Transform each of these into their corresponding coordinate system
705                                 Point trans_on_point, trans_off_point;
706                                 Vector trans_on_t1, trans_on_t2, trans_off_t1, trans_off_t2;
707
708                                 transform_coords(blp_here_on.get_vertex(),  trans_on_point,  on_coord_origin,  on_coord_sys);
709                                 transform_coords(blp_here_off.get_vertex(), trans_off_point, off_coord_origin, off_coord_sys);
710
711 #define COORD_SYS_RADIAL_TAN_INTERP 1
712
713 #ifdef COORD_SYS_RADIAL_TAN_INTERP
714                                 transform_coords(blp_here_on.get_tangent1(),  trans_on_t1,  Point::zero(), on_coord_sys);
715                                 transform_coords(blp_here_off.get_tangent1(), trans_off_t1, Point::zero(), off_coord_sys);
716
717                                 if(blp_here_on.get_split_tangent_flag())
718                                 {
719                                         transform_coords(blp_here_on.get_tangent2(),  trans_on_t2,  Point::zero(), on_coord_sys);
720                                         transform_coords(blp_here_off.get_tangent2(), trans_off_t2, Point::zero(), off_coord_sys);
721                                 }
722 #endif
723
724                                 {
725                                         // Interpolate between the 'on' point and the 'off' point and untransform to get our point's location
726                                         Point tmp;
727                                         untransform_coords(linear_interpolation(trans_off_point, trans_on_point, amount),
728                                                                            tmp, curr_coord_origin, curr_coord_sys);
729                                         blp_here_now.set_vertex(tmp);
730                                 }
731
732 #define INTERP_FUNCTION         radial_interpolation
733 //#define INTERP_FUNCTION       linear_interpolation
734
735 #ifdef COORD_SYS_RADIAL_TAN_INTERP
736                                 {
737                                         Vector tmp;
738                                         untransform_coords(INTERP_FUNCTION(trans_off_t1,trans_on_t1,amount), tmp, Point::zero(), curr_coord_sys);
739                                         blp_here_now.set_tangent1(tmp);
740                                 }
741 #else
742                                 blp_here_now.set_tangent1(radial_interpolation(blp_here_off.get_tangent1(),blp_here_on.get_tangent1(),amount));
743 #endif
744
745                                 if (blp_here_on.get_split_tangent_flag())
746                                 {
747                                         blp_here_now.set_split_tangent_flag(true);
748 #ifdef COORD_SYS_RADIAL_TAN_INTERP
749                                         {
750                                                 Vector tmp;
751                                                 untransform_coords(INTERP_FUNCTION(trans_off_t2,trans_on_t2,amount), tmp, Point::zero(), curr_coord_sys);
752                                                 blp_here_now.set_tangent2(tmp);
753                                         }
754 #else
755                                         blp_here_now.set_tangent2(radial_interpolation(blp_here_off.get_tangent2(),blp_here_on.get_tangent2(),amount));
756 #endif
757                                 }
758                                 else
759                                         blp_here_now.set_split_tangent_flag(false);
760                         }
761
762                         blp_here_now.set_origin(blp_here_on.get_origin());
763                         blp_here_now.set_width(linear_interpolation(blp_here_off.get_width(), blp_here_on.get_width(), amount));
764
765                         // Handle the case where we are the first vertex
766                         if(first_flag)
767                         {
768                                 blp_here_now.set_tangent1(blp_here_now.get_tangent1()*prev_tangent_scalar);
769                                 first_iter=iter;
770                                 first=prev=blp_here_now;
771                                 first_flag=false;
772                                 ret_list.push_back(blp_here_now);
773                                 continue;
774                         }
775
776                         ret_list.back().set_split_tangent_flag(true);
777                         ret_list.back().set_tangent2(prev.get_tangent2()*prev_tangent_scalar);
778                         ret_list.push_back(blp_here_now);
779                         ret_list.back().set_split_tangent_flag(true);
780                         //ret_list.back().set_tangent2(blp_here_now.get_tangent1());
781                         ret_list.back().set_tangent1(blp_here_now.get_tangent1()*prev_tangent_scalar);
782
783                         prev=blp_here_now;
784                 }
785         }
786
787         if(next_scale!=1.0f)
788         {
789                 ret_list.back().set_split_tangent_flag(true);
790                 ret_list.back().set_tangent2(prev.get_tangent2()*next_scale);
791         }
792
793 /*
794         if(get_loop() && !first_flag)
795         {
796                 ret_list.push_back(
797                         Segment(
798                         prev.get_vertex(),
799                         prev.get_tangent2(),
800                         first.get_vertex(),
801                         first.get_tangent1()
802                         )
803                 );
804         }
805 */
806
807         if(list.empty())
808                 synfig::warning(string("ValueNode_BLine::operator()():")+_("No entries in list"));
809         else
810         if(ret_list.empty())
811                 synfig::warning(string("ValueNode_BLine::operator()():")+_("No entries in ret_list"));
812
813         return ValueBase(ret_list,get_loop());
814 }
815
816 String
817 ValueNode_BLine::link_local_name(int i)const
818 {
819         assert(i>=0 && (unsigned)i<list.size());
820         return etl::strprintf(_("Vertex %03d"),i+1);
821 }
822
823 String
824 ValueNode_BLine::get_name()const
825 {
826         return "bline";
827 }
828
829 String
830 ValueNode_BLine::get_local_name()const
831 {
832         return _("BLine");
833 }
834
835 LinkableValueNode*
836 ValueNode_BLine::create_new()const
837 {
838         return new ValueNode_BLine();
839 }
840
841 bool
842 ValueNode_BLine::check_type(ValueBase::Type type)
843 {
844         return type==ValueBase::TYPE_LIST;
845 }
846
847 LinkableValueNode::Vocab
848 ValueNode_BLine::get_children_vocab_vfunc()const
849 {
850         LinkableValueNode::Vocab ret;
851         for(unsigned int i=0; i<list.size();i++)
852         {
853                 ret.push_back(ParamDesc(ValueBase(),strprintf("item%04d",i))
854                         .set_local_name(etl::strprintf(_("Vertex %03d"),i+1))
855                 );
856         }
857
858         return ret;
859 }