1 /* === S Y N F I G ========================================================= */
3 ** \brief Implementation of the "Outline" layer
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 2007, 2008 Chris Moore
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.
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.
22 /* ========================================================================= */
24 //! \note This whole file should be rewritten at some point (darco)
26 /* === H E A D E R S ======================================================= */
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>
45 #include <ETL/calculus>
47 #include <ETL/hermite>
50 #include <synfig/valuenode_bline.h>
56 /* === M A C R O S ========================================================= */
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)
66 /* === G L O B A L S ======================================================= */
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$");
75 /* === P R O C E D U R E S ================================================= */
77 // This function was adapted from what was
78 // described on http://www.whisqu.se/per/docs/math28.htm
79 Point line_intersection(
86 const float& x0(p1[0]);
87 const float& y0(p1[1]);
89 const float x1(p1[0]+t1[0]);
90 const float y1(p1[1]+t1[1]);
92 const float& x2(p2[0]);
93 const float& y2(p2[1]);
95 const float x3(p2[0]+t2[0]);
96 const float y3(p2[1]+t2[1]);
98 const float near_infinity((float)1e+10);
100 float m1,m2; // the slopes of each line
102 // compute slopes, note the kluge for infinity, however, this will
106 m1 = (y1-y0)/(x1-x0);
111 m2 = (y3-y2)/(x3-x2);
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);
123 // compute the inverse of the determinate
124 const float det_inv(1.0f/(a1*b2 - a2*b1));
126 // use Kramers rule to compute the intersection
128 ((b1*c2 - b2*c1)*det_inv),
129 ((a2*c1 - a1*c2)*det_inv)
131 } // end Intersect_Lines
133 /* === M E T H O D S ======================================================= */
145 homogeneous_width=true;
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;
165 Layer::Vocab voc(get_param_vocab());
166 Layer::fill_static(voc);
170 /*! The Sync() function takes the values
171 ** and creates a polygon to be rendered
172 ** with the polygon layer.
179 if (!bline.get_list().size())
181 synfig::warning(string("Outline::sync():")+N_("No vertices in outline " + string("\"") + get_description() + string("\"")));
188 const bool loop(bline.get_loop());
190 ValueNode_BLine::Handle bline_valuenode;
191 if (bline.get_contained_type() == ValueBase::TYPE_SEGMENT)
193 bline_valuenode = ValueNode_BLine::create(bline);
194 bline = (*bline_valuenode)(0);
197 const vector<synfig::BLinePoint> bline_(bline.get_list().begin(),bline.get_list().end());
200 vector<BLinePoint>::const_iterator
204 const vector<BLinePoint>::const_iterator
221 Vector first_tangent=bline.front().get_tangent2();
222 Vector last_tangent=iter->get_tangent1();
224 // if we are looped and drawing sharp cusps, we'll need a value for the incoming tangent
225 if (loop && sharp_cusps && last_tangent.is_equal_to(Vector::zero()))
227 hermite<Vector> curve((iter-1)->get_vertex(), iter->get_vertex(), (iter-1)->get_tangent2(), iter->get_tangent1());
228 const derivative< hermite<Vector> > deriv(curve);
229 last_tangent=deriv(1.0-CUSP_TANGENT_ADJUST);
232 // `first' is for making the cusps; don't do that for the first point if we're not looped
233 for(bool first=!loop; next!=end; iter=next++)
235 Vector prev_t(iter->get_tangent1());
236 Vector iter_t(iter->get_tangent2());
237 Vector next_t(next->get_tangent1());
239 bool split_flag(iter->get_split_tangent_flag());
241 // if iter.t2 == 0 and next.t1 == 0, this is a straight line
242 if(iter_t.is_equal_to(Vector::zero()) && next_t.is_equal_to(Vector::zero()))
244 iter_t=next_t=next->get_vertex()-iter->get_vertex();
247 // if the two points are on top of each other, ignore this segment
248 // leave `first' true if was before
249 if (iter_t.is_equal_to(Vector::zero()))
254 hermite<Vector> curve(
262 iter_w((iter->get_width()*width)*0.5f+expand),
263 next_w((next->get_width()*width)*0.5f+expand);
265 const derivative< hermite<Vector> > deriv(curve);
268 first_tangent = deriv(CUSP_TANGENT_ADJUST);
270 // Make cusps as necessary
271 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()))
273 Vector curr_tangent(deriv(CUSP_TANGENT_ADJUST));
275 const Vector t1(last_tangent.perp().norm());
276 const Vector t2(curr_tangent.perp().norm());
278 Real cross(t1*t2.perp());
279 Real perp((t1-t2).mag());
280 if(cross>CUSP_THRESHOLD)
282 const Point p1(iter->get_vertex()+t1*iter_w);
283 const Point p2(iter->get_vertex()+t2*iter_w);
285 side_a.push_back(line_intersection(p1,last_tangent,p2,curr_tangent));
287 else if(cross<-CUSP_THRESHOLD)
289 const Point p1(iter->get_vertex()-t1*iter_w);
290 const Point p2(iter->get_vertex()-t2*iter_w);
292 side_b.push_back(line_intersection(p1,last_tangent,p2,curr_tangent));
294 else if(cross>0 && perp>1)
296 float amount(max(0.0f,(float)(cross/CUSP_THRESHOLD))*(SPIKE_AMOUNT-1)+1);
298 side_a.push_back(iter->get_vertex()+(t1+t2).norm()*iter_w*amount);
300 else if(cross<0 && perp>1)
302 float amount(max(0.0f,(float)(-cross/CUSP_THRESHOLD))*(SPIKE_AMOUNT-1)+1);
304 side_b.push_back(iter->get_vertex()-(t1+t2).norm()*iter_w*amount);
309 if(homogeneous_width)
311 const float length(curve.length());
314 for(float n=0.0f;n<0.999999f;n+=1.0f/SAMPLES)
316 const Vector d(deriv(n>CUSP_TANGENT_ADJUST?n:CUSP_TANGENT_ADJUST).perp().norm());
317 const Vector p(curve(n));
320 dist+=(p-lastpoint).mag();
322 const float w(((next_w-iter_w)*(dist/length)+iter_w));
324 side_a.push_back(p+d*w);
325 side_b.push_back(p-d*w);
331 for(float n=0.0f;n<0.999999f;n+=1.0f/SAMPLES)
333 const Vector d(deriv(n>CUSP_TANGENT_ADJUST?n:CUSP_TANGENT_ADJUST).perp().norm());
334 const Vector p(curve(n));
335 const float w(((next_w-iter_w)*n+iter_w));
337 side_a.push_back(p+d*w);
338 side_b.push_back(p-d*w);
340 last_tangent=deriv(1.0-CUSP_TANGENT_ADJUST);
341 side_a.push_back(curve(1.0)+last_tangent.perp().norm()*next_w);
342 side_b.push_back(curve(1.0)-last_tangent.perp().norm()*next_w);
349 reverse(side_b.begin(),side_b.end());
355 // Insert code for adding end tip
356 if(round_tip[1] && !loop && side_a.size())
358 // remove the last point
361 const Point vertex(bline.back().get_vertex());
362 const Vector tangent(last_tangent.norm());
363 const float w((bline.back().get_width()*width)*0.5f+expand);
365 hermite<Vector> curve(
366 vertex+tangent.perp()*w,
367 vertex-tangent.perp()*w,
368 tangent*w*ROUND_END_FACTOR,
369 -tangent*w*ROUND_END_FACTOR
372 for(float n=0.0f;n<0.999999f;n+=1.0f/SAMPLES)
373 side_a.push_back(curve(n));
376 for(;!side_b.empty();side_b.pop_back())
377 side_a.push_back(side_b.back());
379 // Insert code for adding begin tip
380 if(round_tip[0] && !loop && side_a.size())
382 // remove the last point
385 const Point vertex(bline.front().get_vertex());
386 const Vector tangent(first_tangent.norm());
387 const float w((bline.front().get_width()*width)*0.5f+expand);
389 hermite<Vector> curve(
390 vertex-tangent.perp()*w,
391 vertex+tangent.perp()*w,
392 -tangent*w*ROUND_END_FACTOR,
393 tangent*w*ROUND_END_FACTOR
396 for(float n=0.0f;n<0.999999f;n+=1.0f/SAMPLES)
397 side_a.push_back(curve(n));
406 if(bline.get_contained_type()==ValueBase::TYPE_BLINEPOINT)
408 ValueBase value(bline);
412 value.set_loop(false);
416 loop_=value.get_loop();
418 segment_list=convert_bline_to_segment_list(value);
419 width_list=convert_bline_to_width_list(value);
429 if(segment_list.empty())
431 synfig::warning("Outline: segment_list is empty, layer disabled");
437 // Repair the width list if we need to
440 if(width_list.empty())
443 default_width=width_list.back();
445 while(width_list.size()<segment_list.size()+1)
446 width_list.push_back(default_width);
447 while(width_list.size()>segment_list.size()+1)
448 width_list.pop_back();
452 // Repair the zero tangents (if any)
454 vector<Segment>::iterator iter;
455 for(iter=segment_list.begin();iter!=segment_list.end();++iter)
457 if(iter->t1.mag_squared()<=EPSILON && iter->t2.mag_squared()<=EPSILON)
458 iter->t1=iter->t2=iter->p2-iter->p1;
462 vector<Real>::iterator iter;
463 vector<Real> scaled_width_list;
464 for(iter=width_list.begin();iter!=width_list.end();++iter)
466 scaled_width_list.push_back((*iter*width+expand)*0.5f);
469 Vector::value_type n;
470 etl::hermite<Vector> curve;
471 vector<Point> vector_list;
472 Vector last_tangent(segment_list.back().t2);
476 last_tangent=NO_LOOP_COOKIE;
479 vector<Segment>::iterator iter;
480 vector<Real>::iterator witer;
482 iter=segment_list.begin(),
483 witer=scaled_width_list.begin();
484 iter!=segment_list.end();
487 if(iter->t1.mag_squared()<=EPSILON && iter->t2.mag_squared()<=EPSILON)
489 vector_list.push_back(iter->p1-(iter->p2-iter->p1).perp().norm()*witer[0]);
490 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]));
491 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]));
492 vector_list.push_back(iter->p2-(iter->p2-iter->p1).perp().norm()*witer[1]);
502 etl::derivative<etl::hermite<Vector> > deriv(curve);
504 // without this if statement, the broken tangents would
506 if(sharp_cusps && last_tangent!=NO_LOOP_COOKIE && !last_tangent.is_equal_to(iter->t1))
508 //Vector curr_tangent(iter->t1);
509 Vector curr_tangent(deriv(CUSP_TANGENT_ADJUST));
511 const Vector t1(last_tangent.perp().norm());
512 const Vector t2(curr_tangent.perp().norm());
514 Point p1(iter->p1+t1*witer[0]);
515 Point p2(iter->p1+t2*witer[0]);
517 Real cross(t1*t2.perp());
519 if(cross>CUSP_THRESHOLD)
520 vector_list.push_back(line_intersection(p1,last_tangent,p2,curr_tangent));
523 float amount(max(0.0f,(float)(cross/CUSP_THRESHOLD))*(SPIKE_AMOUNT-1)+1);
524 // Push back something to make it look vaguely round;
525 //vector_list.push_back(iter->p1+(t1*1.25+t2).norm()*witer[0]*amount);
526 vector_list.push_back(iter->p1+(t1+t2).norm()*witer[0]*amount);
527 //vector_list.push_back(iter->p1+(t1+t2*1.25).norm()*witer[0]*amount);
530 //last_tangent=iter->t2;
531 last_tangent=deriv(1.0f-CUSP_TANGENT_ADJUST);
533 for(n=0.0f;n<1.0f;n+=1.0f/SAMPLES)
534 vector_list.push_back(curve(n)+deriv(n>CUSP_TANGENT_ADJUST?n:CUSP_TANGENT_ADJUST).perp().norm()*((witer[1]-witer[0])*n+witer[0]) );
535 vector_list.push_back(curve(1.0)+deriv(1.0-CUSP_TANGENT_ADJUST).perp().norm()*witer[1]);
539 if(round_tip[1] && !loop_/* && (!sharp_cusps || segment_list.front().p1!=segment_list.back().p2)*/)
541 // remove the last point
542 vector_list.pop_back();
546 curve.p1()=iter->p2+Vector(last_tangent[1],-last_tangent[0]).norm()*(*witer);
547 curve.p2()=iter->p2-(Vector(last_tangent[1],-last_tangent[0]).norm()*(*witer));
548 curve.t2()=-(curve.t1()=last_tangent/last_tangent.mag()*(*witer)*ROUND_END_FACTOR);
550 for(n=0.0f;n<1.0f;n+=1.0f/SAMPLES)
551 vector_list.push_back(curve(n));
553 // remove the last point
554 vector_list.pop_back();
559 last_tangent=NO_LOOP_COOKIE;
562 add_polygon(vector_list);
564 last_tangent=segment_list.front().t1;
568 // last_tangent=segment_list.back().t2;
571 vector<Segment>::reverse_iterator iter;
572 vector<Real>::reverse_iterator witer;
574 iter=segment_list.rbegin(),
575 witer=scaled_width_list.rbegin(),++witer;
576 !(iter==segment_list.rend());
580 if(iter->t1.mag_squared()<=EPSILON && iter->t2.mag_squared()<=EPSILON)
582 vector_list.push_back(iter->p2+(iter->p2-iter->p1).perp().norm()*witer[0]);
583 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]));
584 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]));
585 vector_list.push_back(iter->p1+(iter->p2-iter->p1).perp().norm()*witer[-1]);
595 etl::derivative<etl::hermite<Vector> > deriv(curve);
597 // without this if statement, the broken tangents would
599 if(sharp_cusps && last_tangent!=NO_LOOP_COOKIE && !last_tangent.is_equal_to(iter->t2))
601 //Vector curr_tangent(iter->t2);
602 Vector curr_tangent(deriv(1.0f-CUSP_TANGENT_ADJUST));
604 const Vector t1(last_tangent.perp().norm());
605 const Vector t2(curr_tangent.perp().norm());
607 Point p1(iter->p2-t1*witer[-1]);
608 Point p2(iter->p2-t2*witer[-1]);
610 Real cross(t1*t2.perp());
612 //if(last_tangent.perp().norm()*curr_tangent.norm()<-CUSP_THRESHOLD)
613 if(cross>CUSP_THRESHOLD)
614 vector_list.push_back(line_intersection(p1,last_tangent,p2,curr_tangent));
617 float amount(max(0.0f,(float)(cross/CUSP_THRESHOLD))*(SPIKE_AMOUNT-1)+1);
618 // Push back something to make it look vaguely round;
619 //vector_list.push_back(iter->p2-(t1*1.25+t2).norm()*witer[-1]*amount);
620 vector_list.push_back(iter->p2-(t1+t2).norm()*witer[-1]*amount);
621 //vector_list.push_back(iter->p2-(t1+t2*1.25).norm()*witer[-1]*amount);
624 //last_tangent=iter->t1;
625 last_tangent=deriv(CUSP_TANGENT_ADJUST);
627 for(n=1.0f;n>CUSP_TANGENT_ADJUST;n-=1.0f/SAMPLES)
628 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]) );
629 vector_list.push_back(curve(0.0f)-deriv(CUSP_TANGENT_ADJUST).perp().norm()*witer[0]);
632 if(round_tip[0] && !loop_/* && (!sharp_cusps || segment_list.front().p1!=segment_list.back().p2)*/)
634 // remove the last point
635 vector_list.pop_back();
639 curve.p1()=iter->p1+Vector(last_tangent[1],-last_tangent[0]).norm()*(*witer);
640 curve.p2()=iter->p1-(Vector(last_tangent[1],-last_tangent[0]).norm()*(*witer));
641 curve.t1()=-(curve.t2()=last_tangent/last_tangent.mag()*(*witer)*ROUND_END_FACTOR);
644 for(n=1.0;n>0.0;n-=1.0/SAMPLES)
645 vector_list.push_back(curve(n));
647 // remove the last point
648 vector_list.pop_back();
653 // reverse(vector_list.begin(),vector_list.end());
657 vector<Point>::iterator iter;
658 for(iter=vector_list.begin();iter!=vector_list.end();++iter)
659 if(!iter->is_valid())
661 synfig::error("Outline::sync(): Bad point in vector_list!");
663 //synfig::info("BLEHH__________--- x:%f, y:%f",vector_list.front()[0],vector_list.front()[1]);
667 add_polygon(vector_list);
671 } catch (...) { synfig::error("Outline::sync(): Exception thrown"); throw; }
677 Outline::set_param(const String & param, const ValueBase &value)
679 if(param=="segment_list")
681 if(dynamic_param_list().count("segment_list"))
683 connect_dynamic_param("bline",dynamic_param_list().find("segment_list")->second);
684 disconnect_dynamic_param("segment_list");
685 synfig::warning("Outline::set_param(): Updated valuenode connection to use the new \"bline\" parameter.");
688 synfig::warning("Outline::set_param(): The parameter \"segment_list\" is deprecated. Use \"bline\" instead.");
691 if( (param=="segment_list" || param=="bline") && value.get_type()==ValueBase::TYPE_LIST)
693 //if(value.get_contained_type()!=ValueBase::TYPE_BLINEPOINT)
701 if( param=="seg" && value.get_type()==ValueBase::TYPE_SEGMENT)
703 if(!segment_list.empty())
704 segment_list.clear();
706 segment_list.push_back(value.get(Segment()));
711 if( param=="w[0]" && value.get_type()==ValueBase::TYPE_REAL)
713 if(width_list.size()<2)
715 width_list.push_back(value.get(Real()));
716 width_list.push_back(value.get(Real()));
720 width_list[0]=value.get(Real());
727 if( param=="w[1]" && value.get_type()==ValueBase::TYPE_REAL)
729 if(width_list.size()<2)
731 width_list.push_back(value.get(Real()));
732 width_list.push_back(value.get(Real()));
736 width_list[1]=value.get(Real());
743 if( param=="width_list" && value.same_type_as(width_list))
751 IMPORT(round_tip[0]);
752 IMPORT(round_tip[1]);
754 IMPORT_PLUS(width,if(old_version){width*=2.0;});
757 IMPORT(homogeneous_width);
759 if(param!="vector_list")
760 return Layer_Polygon::set_param(param,value);
766 Outline::set_time(Context context, Time time)const
768 const_cast<Outline*>(this)->sync();
769 context.set_time(time);
773 Outline::set_time(Context context, Time time, Vector pos)const
775 const_cast<Outline*>(this)->sync();
776 context.set_time(time,pos);
780 Outline::get_param(const String& param)const
784 //EXPORT(width_list);
785 //EXPORT(segment_list);
786 EXPORT(homogeneous_width);
787 EXPORT(round_tip[0]);
788 EXPORT(round_tip[1]);
796 if(param!="vector_list")
797 return Layer_Polygon::get_param(param);
802 Outline::get_param_vocab()const
804 Layer::Vocab ret(Layer_Polygon::get_param_vocab());
806 // Pop off the polygon parameter from the polygon vocab
809 ret.push_back(ParamDesc("bline")
810 .set_local_name(_("Vertices"))
811 .set_origin("origin")
813 .set_description(_("A list of BLine Points"))
817 ret.push_back(ParamDesc("width_list")
818 .set_local_name(_("Point Widths"))
819 .set_origin("segment_list")
825 ret.push_back(ParamDesc("width")
827 .set_local_name(_("Outline Width"))
830 ret.push_back(ParamDesc("expand")
832 .set_local_name(_("Expand"))
835 ret.push_back(ParamDesc("sharp_cusps")
836 .set_local_name(_("Sharp Cusps"))
837 .set_description(_("Determines cusp type"))
840 ret.push_back(ParamDesc("round_tip[0]")
841 .set_local_name(_("Rounded Begin"))
842 .set_description(_("Round off the tip"))
845 ret.push_back(ParamDesc("round_tip[1]")
846 .set_local_name(_("Rounded End"))
847 .set_description(_("Round off the tip"))
849 ret.push_back(ParamDesc("loopyness")
850 .set_local_name(_("Loopyness"))
852 ret.push_back(ParamDesc("homogeneous_width")
853 .set_local_name(_("Homogeneous"))