1b97582e2e623135447398e6437a05aacac8b88e
[synfig.git] /
1 /* === S Y N F I G ========================================================= */
2 /*!     \file outline.cpp
3 **      \brief Implementation of the "Outline" layer
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 //! \note This whole file should be rewritten at some point (darco)
25
26 /* === H E A D E R S ======================================================= */
27
28 #ifdef USING_PCH
29 #       include "pch.h"
30 #else
31 #ifdef HAVE_CONFIG_H
32 #       include <config.h>
33 #endif
34
35 #include "outline.h"
36 #include <synfig/string.h>
37 #include <synfig/time.h>
38 #include <synfig/context.h>
39 #include <synfig/paramdesc.h>
40 #include <synfig/renddesc.h>
41 #include <synfig/surface.h>
42 #include <synfig/value.h>
43 #include <synfig/valuenode.h>
44
45 #include <ETL/calculus>
46 #include <ETL/bezier>
47 #include <ETL/hermite>
48 #include <vector>
49
50 #include <synfig/valuenode_bline.h>
51
52 #endif
53
54 using namespace etl;
55
56 /* === M A C R O S ========================================================= */
57
58 #define SAMPLES         50
59 #define ROUND_END_FACTOR        (4)
60 #define CUSP_THRESHOLD          (0.40)
61 #define SPIKE_AMOUNT            (4)
62 #define NO_LOOP_COOKIE          synfig::Vector(84951305,7836658)
63 #define EPSILON                         (0.000000001)
64 #define CUSP_TANGENT_ADJUST     (0.025)
65
66 /* === G L O B A L S ======================================================= */
67
68 SYNFIG_LAYER_INIT(Outline);
69 SYNFIG_LAYER_SET_NAME(Outline,"outline");
70 SYNFIG_LAYER_SET_LOCAL_NAME(Outline,N_("Outline"));
71 SYNFIG_LAYER_SET_CATEGORY(Outline,N_("Geometry"));
72 SYNFIG_LAYER_SET_VERSION(Outline,"0.2");
73 SYNFIG_LAYER_SET_CVS_ID(Outline,"$Id$");
74
75 /* === P R O C E D U R E S ================================================= */
76
77 // This function was adapted from what was
78 // described on http://www.whisqu.se/per/docs/math28.htm
79 Point line_intersection(
80         const Point& p1,
81         const Vector& t1,
82         const Point& p2,
83         const Vector& t2
84 )
85 {
86         const float& x0(p1[0]);
87         const float& y0(p1[1]);
88
89         const float x1(p1[0]+t1[0]);
90         const float y1(p1[1]+t1[1]);
91
92         const float& x2(p2[0]);
93         const float& y2(p2[1]);
94
95         const float x3(p2[0]+t2[0]);
96         const float y3(p2[1]+t2[1]);
97
98         const float near_infinity((float)1e+10);
99
100         float m1,m2;    // the slopes of each line
101
102         // compute slopes, note the kluge for infinity, however, this will
103         // be close enough
104
105         if ((x1-x0)!=0)
106            m1 = (y1-y0)/(x1-x0);
107         else
108            m1 = near_infinity;
109
110         if ((x3-x2)!=0)
111            m2 = (y3-y2)/(x3-x2);
112         else
113            m2 = near_infinity;
114
115         // compute constants
116         const float& a1(m1);
117         const float& a2(m2);
118         const float b1(-1.0f);
119         const float b2(-1.0f);
120         const float c1(y0-m1*x0);
121         const float c2(y2-m2*x2);
122
123         // compute the inverse of the determinate
124         const float det_inv(1.0f/(a1*b2 - a2*b1));
125
126         // use Kramers rule to compute the intersection
127         return Point(
128                 ((b1*c2 - b2*c1)*det_inv),
129                 ((a2*c1 - a1*c2)*det_inv)
130         );
131 } // end Intersect_Lines
132
133 /* === M E T H O D S ======================================================= */
134
135
136 Outline::Outline()
137 {
138         old_version=false;
139         round_tip[0]=true;
140         round_tip[1]=true;
141         sharp_cusps=true;
142         width=1.0f;
143         loopyness=1.0f;
144         expand=0;
145         homogeneous_width=true;
146         clear();
147
148         vector<BLinePoint> bline_point_list;
149         bline_point_list.push_back(BLinePoint());
150         bline_point_list.push_back(BLinePoint());
151         bline_point_list.push_back(BLinePoint());
152         bline_point_list[0].set_vertex(Point(0,1));
153         bline_point_list[1].set_vertex(Point(0,-1));
154         bline_point_list[2].set_vertex(Point(1,0));
155         bline_point_list[0].set_tangent(bline_point_list[1].get_vertex()-bline_point_list[2].get_vertex()*0.5f);
156         bline_point_list[1].set_tangent(bline_point_list[2].get_vertex()-bline_point_list[0].get_vertex()*0.5f);
157         bline_point_list[2].set_tangent(bline_point_list[0].get_vertex()-bline_point_list[1].get_vertex()*0.5f);
158         bline_point_list[0].set_width(1.0f);
159         bline_point_list[1].set_width(1.0f);
160         bline_point_list[2].set_width(1.0f);
161         bline=bline_point_list;
162
163         needs_sync=true;
164 }
165
166
167 /*! The Sync() function takes the values
168 **      and creates a polygon to be rendered
169 **      with the polygon layer.
170 */
171 void
172 Outline::sync()
173 {
174         clear();
175
176         if (!bline.get_list().size())
177         {
178                 synfig::warning(string("Outline::sync():")+N_("No vertices in outline " + string("\"") + get_description() + string("\"")));
179                 return;
180         }
181                 
182         try {
183 #if 1
184
185         const bool loop(bline.get_loop());
186
187         ValueNode_BLine::Handle bline_valuenode;
188         if (bline.get_contained_type() == ValueBase::TYPE_SEGMENT)
189         {
190                 bline_valuenode = ValueNode_BLine::create(bline);
191                 bline = (*bline_valuenode)(0);
192         }
193
194         const vector<synfig::BLinePoint> bline_(bline.get_list().begin(),bline.get_list().end());
195 #define bline bline_
196
197         vector<BLinePoint>::const_iterator
198                 iter,
199                 next(bline.begin());
200
201         const vector<BLinePoint>::const_iterator
202                 end(bline.end());
203
204         vector<Point>
205                 side_a,
206                 side_b;
207
208         if(loop)
209                 iter=--bline.end();
210         else
211                 iter=next++;
212
213         //                              iter    next
214         //                              ----    ----
215         // looped               nth             1st
216         // !looped              1st             2nd
217
218         Vector first_tangent=bline.front().get_tangent2();
219         Vector last_tangent=iter->get_tangent1();
220
221         // if we are looped and drawing sharp cusps, we'll need a value for the incoming tangent
222         if (loop && sharp_cusps && last_tangent.is_equal_to(Vector::zero()))
223         {
224                 hermite<Vector> curve((iter-1)->get_vertex(), iter->get_vertex(), (iter-1)->get_tangent2(), iter->get_tangent1());
225                 const derivative< hermite<Vector> > deriv(curve);
226                 last_tangent=deriv(1.0-CUSP_TANGENT_ADJUST);
227         }
228
229         // `first' is for making the cusps; don't do that for the first point if we're not looped
230         for(bool first=!loop; next!=end; iter=next++)
231         {
232                 Vector prev_t(iter->get_tangent1());
233                 Vector iter_t(iter->get_tangent2());
234                 Vector next_t(next->get_tangent1());
235
236                 bool split_flag(iter->get_split_tangent_flag());
237
238                 // if iter.t2 == 0 and next.t1 == 0, this is a straight line
239                 if(iter_t.is_equal_to(Vector::zero()) && next_t.is_equal_to(Vector::zero()))
240                 {
241                         iter_t=next_t=next->get_vertex()-iter->get_vertex();
242                         // split_flag=true;
243
244                         // if the two points are on top of each other, ignore this segment
245                         // leave `first' true if was before
246                         if (iter_t.is_equal_to(Vector::zero()))
247                                 continue;
248                 }
249
250                 // Setup the curve
251                 hermite<Vector> curve(
252                         iter->get_vertex(),
253                         next->get_vertex(),
254                         iter_t,
255                         next_t
256                 );
257
258                 const float
259                         iter_w((iter->get_width()*width)*0.5f+expand),
260                         next_w((next->get_width()*width)*0.5f+expand);
261
262                 const derivative< hermite<Vector> > deriv(curve);
263
264                 if (first)
265                         first_tangent = deriv(CUSP_TANGENT_ADJUST);
266
267                 // Make cusps as necessary
268                 if(!first && sharp_cusps && split_flag && (!prev_t.is_equal_to(iter_t) || iter_t.is_equal_to(Vector::zero())) && !last_tangent.is_equal_to(Vector::zero()))
269                 {
270                         Vector curr_tangent(deriv(CUSP_TANGENT_ADJUST));
271
272                         const Vector t1(last_tangent.perp().norm());
273                         const Vector t2(curr_tangent.perp().norm());
274
275                         Real cross(t1*t2.perp());
276                         Real perp((t1-t2).mag());
277                         if(cross>CUSP_THRESHOLD)
278                         {
279                                 const Point p1(iter->get_vertex()+t1*iter_w);
280                                 const Point p2(iter->get_vertex()+t2*iter_w);
281
282                                 side_a.push_back(line_intersection(p1,last_tangent,p2,curr_tangent));
283                         }
284                         else if(cross<-CUSP_THRESHOLD)
285                         {
286                                 const Point p1(iter->get_vertex()-t1*iter_w);
287                                 const Point p2(iter->get_vertex()-t2*iter_w);
288
289                                 side_b.push_back(line_intersection(p1,last_tangent,p2,curr_tangent));
290                         }
291                         else if(cross>0 && perp>1)
292                         {
293                                 float amount(max(0.0f,(float)(cross/CUSP_THRESHOLD))*(SPIKE_AMOUNT-1)+1);
294
295                                 side_a.push_back(iter->get_vertex()+(t1+t2).norm()*iter_w*amount);
296                         }
297                         else if(cross<0 && perp>1)
298                         {
299                                 float amount(max(0.0f,(float)(-cross/CUSP_THRESHOLD))*(SPIKE_AMOUNT-1)+1);
300
301                                 side_b.push_back(iter->get_vertex()-(t1+t2).norm()*iter_w*amount);
302                         }
303                 }
304
305                 // Make the outline
306                 if(homogeneous_width)
307                 {
308                         const float length(curve.length());
309                         float dist(0);
310                         Point lastpoint;
311                         for(float n=0.0f;n<0.999999f;n+=1.0f/SAMPLES)
312                         {
313                                 const Vector d(deriv(n>CUSP_TANGENT_ADJUST?n:CUSP_TANGENT_ADJUST).perp().norm());
314                                 const Vector p(curve(n));
315
316                                 if(n)
317                                         dist+=(p-lastpoint).mag();
318
319                                 const float w(((next_w-iter_w)*(dist/length)+iter_w));
320
321                                 side_a.push_back(p+d*w);
322                                 side_b.push_back(p-d*w);
323
324                                 lastpoint=p;
325                         }
326                 }
327                 else
328                         for(float n=0.0f;n<0.999999f;n+=1.0f/SAMPLES)
329                         {
330                                 const Vector d(deriv(n>CUSP_TANGENT_ADJUST?n:CUSP_TANGENT_ADJUST).perp().norm());
331                                 const Vector p(curve(n));
332                                 const float w(((next_w-iter_w)*n+iter_w));
333
334                                 side_a.push_back(p+d*w);
335                                 side_b.push_back(p-d*w);
336                         }
337                 last_tangent=deriv(1.0-CUSP_TANGENT_ADJUST);
338                 side_a.push_back(curve(1.0)+last_tangent.perp().norm()*next_w);
339                 side_b.push_back(curve(1.0)-last_tangent.perp().norm()*next_w);
340
341                 first=false;
342         }
343
344         if(loop)
345         {
346                 reverse(side_b.begin(),side_b.end());
347                 add_polygon(side_a);
348                 add_polygon(side_b);
349                 return;
350         }
351
352         // Insert code for adding end tip
353         if(round_tip[1] && !loop && side_a.size())
354         {
355                 // remove the last point
356                 side_a.pop_back();
357
358                 const Point vertex(bline.back().get_vertex());
359                 const Vector tangent(last_tangent.norm());
360                 const float w((bline.back().get_width()*width)*0.5f+expand);
361
362                 hermite<Vector> curve(
363                         vertex+tangent.perp()*w,
364                         vertex-tangent.perp()*w,
365                         tangent*w*ROUND_END_FACTOR,
366                         -tangent*w*ROUND_END_FACTOR
367                 );
368
369                 for(float n=0.0f;n<0.999999f;n+=1.0f/SAMPLES)
370                         side_a.push_back(curve(n));
371         }
372
373         for(;!side_b.empty();side_b.pop_back())
374                 side_a.push_back(side_b.back());
375
376         // Insert code for adding begin tip
377         if(round_tip[0] && !loop && side_a.size())
378         {
379                 // remove the last point
380                 side_a.pop_back();
381
382                 const Point vertex(bline.front().get_vertex());
383                 const Vector tangent(first_tangent.norm());
384                 const float w((bline.front().get_width()*width)*0.5f+expand);
385
386                 hermite<Vector> curve(
387                         vertex-tangent.perp()*w,
388                         vertex+tangent.perp()*w,
389                         -tangent*w*ROUND_END_FACTOR,
390                         tangent*w*ROUND_END_FACTOR
391                 );
392
393                 for(float n=0.0f;n<0.999999f;n+=1.0f/SAMPLES)
394                         side_a.push_back(curve(n));
395         }
396
397         add_polygon(side_a);
398
399
400 #else /* 1 */
401
402         bool loop_;
403         if(bline.get_contained_type()==ValueBase::TYPE_BLINEPOINT)
404         {
405                 ValueBase value(bline);
406
407                 if(loopyness<0.5f)
408                 {
409                         value.set_loop(false);
410                         loop_=false;
411                 }
412                 else
413                         loop_=value.get_loop();
414
415                 segment_list=convert_bline_to_segment_list(value);
416                 width_list=convert_bline_to_width_list(value);
417         }
418         else
419         {
420                 clear();
421                 return;
422         }
423
424
425
426         if(segment_list.empty())
427         {
428                 synfig::warning("Outline: segment_list is empty, layer disabled");
429                 clear();
430                 return;
431         }
432
433
434         // Repair the width list if we need to
435         {
436                 Real default_width;
437                 if(width_list.empty())
438                         default_width=0.01;
439                 else
440                         default_width=width_list.back();
441
442                 while(width_list.size()<segment_list.size()+1)
443                         width_list.push_back(default_width);
444                 while(width_list.size()>segment_list.size()+1)
445                         width_list.pop_back();
446
447         }
448
449         // Repair the zero tangents (if any)
450         {
451                 vector<Segment>::iterator iter;
452                 for(iter=segment_list.begin();iter!=segment_list.end();++iter)
453                 {
454                         if(iter->t1.mag_squared()<=EPSILON && iter->t2.mag_squared()<=EPSILON)
455                                 iter->t1=iter->t2=iter->p2-iter->p1;
456                 }
457         }
458
459         vector<Real>::iterator iter;
460         vector<Real> scaled_width_list;
461         for(iter=width_list.begin();iter!=width_list.end();++iter)
462         {
463                 scaled_width_list.push_back((*iter*width+expand)*0.5f);
464         }
465
466         Vector::value_type n;
467         etl::hermite<Vector> curve;
468         vector<Point> vector_list;
469         Vector last_tangent(segment_list.back().t2);
470         clear();
471
472         if(!loop_)
473                 last_tangent=NO_LOOP_COOKIE;
474
475         {
476                 vector<Segment>::iterator iter;
477                 vector<Real>::iterator witer;
478                 for(
479                         iter=segment_list.begin(),
480                         witer=scaled_width_list.begin();
481                         iter!=segment_list.end();
482                         ++iter,++witer)
483                 {
484                         if(iter->t1.mag_squared()<=EPSILON && iter->t2.mag_squared()<=EPSILON)
485                         {
486                                 vector_list.push_back(iter->p1-(iter->p2-iter->p1).perp().norm()*witer[0]);
487                                 vector_list.push_back((iter->p2-iter->p1)*0.05+iter->p1-(iter->p2-iter->p1).perp().norm()*((witer[1]-witer[0])*0.05+witer[0]));
488                                 vector_list.push_back((iter->p2-iter->p1)*0.95+iter->p1-(iter->p2-iter->p1).perp().norm()*((witer[1]-witer[0])*0.95+witer[0]));
489                                 vector_list.push_back(iter->p2-(iter->p2-iter->p1).perp().norm()*witer[1]);
490                         }
491                         else
492                         {
493                                 curve.p1()=iter->p1;
494                                 curve.t1()=iter->t1;
495                                 curve.p2()=iter->p2;
496                                 curve.t2()=iter->t2;
497                                 curve.sync();
498
499                                 etl::derivative<etl::hermite<Vector> > deriv(curve);
500
501                                 // without this if statement, the broken tangents would
502                                 // have boxed edges
503                                 if(sharp_cusps && last_tangent!=NO_LOOP_COOKIE && !last_tangent.is_equal_to(iter->t1))
504                                 {
505                                         //Vector curr_tangent(iter->t1);
506                                         Vector curr_tangent(deriv(CUSP_TANGENT_ADJUST));
507
508                                         const Vector t1(last_tangent.perp().norm());
509                                         const Vector t2(curr_tangent.perp().norm());
510
511                                         Point p1(iter->p1+t1*witer[0]);
512                                         Point p2(iter->p1+t2*witer[0]);
513
514                                         Real cross(t1*t2.perp());
515
516                                         if(cross>CUSP_THRESHOLD)
517                                                 vector_list.push_back(line_intersection(p1,last_tangent,p2,curr_tangent));
518                                         else if(cross>0)
519                                         {
520                                                 float amount(max(0.0f,(float)(cross/CUSP_THRESHOLD))*(SPIKE_AMOUNT-1)+1);
521                                                 // Push back something to make it look vaguely round;
522                                                 //vector_list.push_back(iter->p1+(t1*1.25+t2).norm()*witer[0]*amount);
523                                                 vector_list.push_back(iter->p1+(t1+t2).norm()*witer[0]*amount);
524                                                 //vector_list.push_back(iter->p1+(t1+t2*1.25).norm()*witer[0]*amount);
525                                         }
526                                 }
527                                 //last_tangent=iter->t2;
528                                 last_tangent=deriv(1.0f-CUSP_TANGENT_ADJUST);
529
530                                 for(n=0.0f;n<1.0f;n+=1.0f/SAMPLES)
531                                         vector_list.push_back(curve(n)+deriv(n>CUSP_TANGENT_ADJUST?n:CUSP_TANGENT_ADJUST).perp().norm()*((witer[1]-witer[0])*n+witer[0]) );
532                                 vector_list.push_back(curve(1.0)+deriv(1.0-CUSP_TANGENT_ADJUST).perp().norm()*witer[1]);
533
534                         }
535                 }
536                 if(round_tip[1] && !loop_/* && (!sharp_cusps || segment_list.front().p1!=segment_list.back().p2)*/)
537                 {
538                         // remove the last point
539                         vector_list.pop_back();
540
541                         iter--;
542
543                         curve.p1()=iter->p2+Vector(last_tangent[1],-last_tangent[0]).norm()*(*witer);
544                         curve.p2()=iter->p2-(Vector(last_tangent[1],-last_tangent[0]).norm()*(*witer));
545                         curve.t2()=-(curve.t1()=last_tangent/last_tangent.mag()*(*witer)*ROUND_END_FACTOR);
546                         curve.sync();
547                         for(n=0.0f;n<1.0f;n+=1.0f/SAMPLES)
548                                 vector_list.push_back(curve(n));
549
550                         // remove the last point
551                         vector_list.pop_back();
552                 }
553         }
554
555         if(!loop_)
556                 last_tangent=NO_LOOP_COOKIE;
557         else
558         {
559                 add_polygon(vector_list);
560                 vector_list.clear();
561                 last_tangent=segment_list.front().t1;
562         }
563
564         //else
565         //      last_tangent=segment_list.back().t2;
566
567         {
568                 vector<Segment>::reverse_iterator iter;
569                 vector<Real>::reverse_iterator witer;
570                 for(
571                         iter=segment_list.rbegin(),
572                         witer=scaled_width_list.rbegin(),++witer;
573                         !(iter==segment_list.rend());
574                         ++iter,++witer)
575                 {
576
577                         if(iter->t1.mag_squared()<=EPSILON && iter->t2.mag_squared()<=EPSILON)
578                         {
579                                 vector_list.push_back(iter->p2+(iter->p2-iter->p1).perp().norm()*witer[0]);
580                                 vector_list.push_back((iter->p2-iter->p1)*0.95+iter->p1+(iter->p2-iter->p1).perp().norm()*((witer[-1]-witer[0])*0.95+witer[0]));
581                                 vector_list.push_back((iter->p2-iter->p1)*0.05+iter->p1+(iter->p2-iter->p1).perp().norm()*((witer[-1]-witer[0])*0.05+witer[0]));
582                                 vector_list.push_back(iter->p1+(iter->p2-iter->p1).perp().norm()*witer[-1]);
583                         }
584                         else
585                         {
586                                 curve.p1()=iter->p1;
587                                 curve.t1()=iter->t1;
588                                 curve.p2()=iter->p2;
589                                 curve.t2()=iter->t2;
590                                 curve.sync();
591
592                                 etl::derivative<etl::hermite<Vector> > deriv(curve);
593
594                                 // without this if statement, the broken tangents would
595                                 // have boxed edges
596                                 if(sharp_cusps && last_tangent!=NO_LOOP_COOKIE && !last_tangent.is_equal_to(iter->t2))
597                                 {
598                                         //Vector curr_tangent(iter->t2);
599                                         Vector curr_tangent(deriv(1.0f-CUSP_TANGENT_ADJUST));
600
601                                         const Vector t1(last_tangent.perp().norm());
602                                         const Vector t2(curr_tangent.perp().norm());
603
604                                         Point p1(iter->p2-t1*witer[-1]);
605                                         Point p2(iter->p2-t2*witer[-1]);
606
607                                         Real cross(t1*t2.perp());
608
609                                         //if(last_tangent.perp().norm()*curr_tangent.norm()<-CUSP_THRESHOLD)
610                                         if(cross>CUSP_THRESHOLD)
611                                                 vector_list.push_back(line_intersection(p1,last_tangent,p2,curr_tangent));
612                                         else if(cross>0)
613                                         {
614                                                 float amount(max(0.0f,(float)(cross/CUSP_THRESHOLD))*(SPIKE_AMOUNT-1)+1);
615                                                 // Push back something to make it look vaguely round;
616                                                 //vector_list.push_back(iter->p2-(t1*1.25+t2).norm()*witer[-1]*amount);
617                                                 vector_list.push_back(iter->p2-(t1+t2).norm()*witer[-1]*amount);
618                                                 //vector_list.push_back(iter->p2-(t1+t2*1.25).norm()*witer[-1]*amount);
619                                         }
620                                 }
621                                 //last_tangent=iter->t1;
622                                 last_tangent=deriv(CUSP_TANGENT_ADJUST);
623
624                                 for(n=1.0f;n>CUSP_TANGENT_ADJUST;n-=1.0f/SAMPLES)
625                                         vector_list.push_back(curve(n)-deriv(1-n>CUSP_TANGENT_ADJUST?n:1-CUSP_TANGENT_ADJUST).perp().norm()*((witer[-1]-witer[0])*n+witer[0]) );
626                                 vector_list.push_back(curve(0.0f)-deriv(CUSP_TANGENT_ADJUST).perp().norm()*witer[0]);
627                         }
628                 }
629                 if(round_tip[0] && !loop_/* && (!sharp_cusps || segment_list.front().p1!=segment_list.back().p2)*/)
630                 {
631                         // remove the last point
632                         vector_list.pop_back();
633                         iter--;
634                         witer--;
635
636                         curve.p1()=iter->p1+Vector(last_tangent[1],-last_tangent[0]).norm()*(*witer);
637                         curve.p2()=iter->p1-(Vector(last_tangent[1],-last_tangent[0]).norm()*(*witer));
638                         curve.t1()=-(curve.t2()=last_tangent/last_tangent.mag()*(*witer)*ROUND_END_FACTOR);
639                         curve.sync();
640
641                         for(n=1.0;n>0.0;n-=1.0/SAMPLES)
642                                 vector_list.push_back(curve(n));
643
644                         // remove the last point
645                         vector_list.pop_back();
646                 }
647         }
648
649         //if(loop_)
650         //      reverse(vector_list.begin(),vector_list.end());
651
652 #ifdef _DEBUG
653         {
654                 vector<Point>::iterator iter;
655                 for(iter=vector_list.begin();iter!=vector_list.end();++iter)
656                         if(!iter->is_valid())
657                         {
658                                 synfig::error("Outline::sync(): Bad point in vector_list!");
659                         }
660                 //synfig::info("BLEHH__________--- x:%f, y:%f",vector_list.front()[0],vector_list.front()[1]);
661         }
662 #endif /* _DEBUG */
663
664         add_polygon(vector_list);
665
666
667 #endif /* 1 */
668         } catch (...) { synfig::error("Outline::sync(): Exception thrown"); throw; }
669 }
670
671 #undef bline
672
673 bool
674 Outline::set_param(const String & param, const ValueBase &value)
675 {
676         if(param=="segment_list")
677         {
678                 if(dynamic_param_list().count("segment_list"))
679                 {
680                         connect_dynamic_param("bline",dynamic_param_list().find("segment_list")->second);
681                         disconnect_dynamic_param("segment_list");
682                         synfig::warning("Outline::set_param(): Updated valuenode connection to use the new \"bline\" parameter.");
683                 }
684                 else
685                         synfig::warning("Outline::set_param(): The parameter \"segment_list\" is deprecated. Use \"bline\" instead.");
686         }
687
688         if(     (param=="segment_list" || param=="bline") && value.get_type()==ValueBase::TYPE_LIST)
689         {
690                 //if(value.get_contained_type()!=ValueBase::TYPE_BLINEPOINT)
691                 //      return false;
692
693                 bline=value;
694
695                 return true;
696         }
697         /*
698         if(     param=="seg" && value.get_type()==ValueBase::TYPE_SEGMENT)
699         {
700                 if(!segment_list.empty())
701                         segment_list.clear();
702
703                 segment_list.push_back(value.get(Segment()));
704                 loop_=false;
705                 //sync();
706                 return true;
707         }
708         if(     param=="w[0]" && value.get_type()==ValueBase::TYPE_REAL)
709         {
710                 if(width_list.size()<2)
711                 {
712                         width_list.push_back(value.get(Real()));
713                         width_list.push_back(value.get(Real()));
714                 }
715                 else
716                 {
717                         width_list[0]=value.get(Real());
718                 }
719                 width=1;
720                 //sync();
721                 return true;
722         }
723
724         if(     param=="w[1]" && value.get_type()==ValueBase::TYPE_REAL)
725         {
726                 if(width_list.size()<2)
727                 {
728                         width_list.push_back(value.get(Real()));
729                         width_list.push_back(value.get(Real()));
730                 }
731                 else
732                 {
733                         width_list[1]=value.get(Real());
734                 }
735                 width=1;
736                 //sync();
737                 return true;
738         }
739
740         if(     param=="width_list" && value.same_type_as(width_list))
741         {
742                 width_list=value;
743                 //sync();
744                 return true;
745         }
746         */
747
748         IMPORT(round_tip[0]);
749         IMPORT(round_tip[1]);
750         IMPORT(sharp_cusps);
751         IMPORT_PLUS(width,if(old_version){width*=2.0;});
752         IMPORT(loopyness);
753         IMPORT(expand);
754         IMPORT(homogeneous_width);
755
756         if(param!="vector_list")
757                 return Layer_Polygon::set_param(param,value);
758
759         return false;
760 }
761
762 void
763 Outline::set_time(Context context, Time time)const
764 {
765         const_cast<Outline*>(this)->sync();
766         context.set_time(time);
767 }
768
769 void
770 Outline::set_time(Context context, Time time, Vector pos)const
771 {
772         const_cast<Outline*>(this)->sync();
773         context.set_time(time,pos);
774 }
775
776 ValueBase
777 Outline::get_param(const String& param)const
778 {
779         EXPORT(bline);
780         EXPORT(expand);
781         //EXPORT(width_list);
782         //EXPORT(segment_list);
783         EXPORT(homogeneous_width);
784         EXPORT(round_tip[0]);
785         EXPORT(round_tip[1]);
786         EXPORT(sharp_cusps);
787         EXPORT(width);
788         EXPORT(loopyness);
789
790         EXPORT_NAME();
791         EXPORT_VERSION();
792
793         if(param!="vector_list")
794                 return Layer_Polygon::get_param(param);
795         return ValueBase();
796 }
797
798 Layer::Vocab
799 Outline::get_param_vocab()const
800 {
801         Layer::Vocab ret(Layer_Polygon::get_param_vocab());
802
803         // Pop off the polygon parameter from the polygon vocab
804         ret.pop_back();
805
806         ret.push_back(ParamDesc("bline")
807                 .set_local_name(_("Vertices"))
808                 .set_origin("origin")
809                 .set_hint("width")
810                 .set_description(_("A list of BLine Points"))
811         );
812
813         /*
814         ret.push_back(ParamDesc("width_list")
815                 .set_local_name(_("Point Widths"))
816                 .set_origin("segment_list")
817                 .hidden()
818                 .not_critical()
819         );
820         */
821
822         ret.push_back(ParamDesc("width")
823                 .set_is_distance()
824                 .set_local_name(_("Outline Width"))
825         );
826
827         ret.push_back(ParamDesc("expand")
828                 .set_is_distance()
829                 .set_local_name(_("Expand"))
830         );
831
832         ret.push_back(ParamDesc("sharp_cusps")
833                 .set_local_name(_("Sharp Cusps"))
834                 .set_description(_("Determines cusp type"))
835         );
836
837         ret.push_back(ParamDesc("round_tip[0]")
838                 .set_local_name(_("Rounded Begin"))
839                 .set_description(_("Round off the tip"))
840         );
841
842         ret.push_back(ParamDesc("round_tip[1]")
843                 .set_local_name(_("Rounded End"))
844                 .set_description(_("Round off the tip"))
845         );
846         ret.push_back(ParamDesc("loopyness")
847                 .set_local_name(_("Loopyness"))
848         );
849         ret.push_back(ParamDesc("homogeneous_width")
850                 .set_local_name(_("Homogeneous"))
851         );
852
853         return ret;
854 }