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;
167 /*! The Sync() function takes the values
168 ** and creates a polygon to be rendered
169 ** with the polygon layer.
176 if (!bline.get_list().size())
178 synfig::warning(string("Outline::sync():")+N_("No vertices in outline " + string("\"") + get_description() + string("\"")));
185 const bool loop(bline.get_loop());
187 ValueNode_BLine::Handle bline_valuenode;
188 if (bline.get_contained_type() == ValueBase::TYPE_SEGMENT)
190 bline_valuenode = ValueNode_BLine::create(bline);
191 bline = (*bline_valuenode)(0);
194 const vector<synfig::BLinePoint> bline_(bline.get_list().begin(),bline.get_list().end());
197 vector<BLinePoint>::const_iterator
201 const vector<BLinePoint>::const_iterator
218 Vector first_tangent=bline.front().get_tangent2();
219 Vector last_tangent=iter->get_tangent1();
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()))
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);
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++)
232 Vector prev_t(iter->get_tangent1());
233 Vector iter_t(iter->get_tangent2());
234 Vector next_t(next->get_tangent1());
236 bool split_flag(iter->get_split_tangent_flag());
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()))
241 iter_t=next_t=next->get_vertex()-iter->get_vertex();
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()))
251 hermite<Vector> curve(
259 iter_w((iter->get_width()*width)*0.5f+expand),
260 next_w((next->get_width()*width)*0.5f+expand);
262 const derivative< hermite<Vector> > deriv(curve);
265 first_tangent = deriv(CUSP_TANGENT_ADJUST);
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()))
270 Vector curr_tangent(deriv(CUSP_TANGENT_ADJUST));
272 const Vector t1(last_tangent.perp().norm());
273 const Vector t2(curr_tangent.perp().norm());
275 Real cross(t1*t2.perp());
276 Real perp((t1-t2).mag());
277 if(cross>CUSP_THRESHOLD)
279 const Point p1(iter->get_vertex()+t1*iter_w);
280 const Point p2(iter->get_vertex()+t2*iter_w);
282 side_a.push_back(line_intersection(p1,last_tangent,p2,curr_tangent));
284 else if(cross<-CUSP_THRESHOLD)
286 const Point p1(iter->get_vertex()-t1*iter_w);
287 const Point p2(iter->get_vertex()-t2*iter_w);
289 side_b.push_back(line_intersection(p1,last_tangent,p2,curr_tangent));
291 else if(cross>0 && perp>1)
293 float amount(max(0.0f,(float)(cross/CUSP_THRESHOLD))*(SPIKE_AMOUNT-1)+1);
295 side_a.push_back(iter->get_vertex()+(t1+t2).norm()*iter_w*amount);
297 else if(cross<0 && perp>1)
299 float amount(max(0.0f,(float)(-cross/CUSP_THRESHOLD))*(SPIKE_AMOUNT-1)+1);
301 side_b.push_back(iter->get_vertex()-(t1+t2).norm()*iter_w*amount);
306 if(homogeneous_width)
308 const float length(curve.length());
311 for(float n=0.0f;n<0.999999f;n+=1.0f/SAMPLES)
313 const Vector d(deriv(n>CUSP_TANGENT_ADJUST?n:CUSP_TANGENT_ADJUST).perp().norm());
314 const Vector p(curve(n));
317 dist+=(p-lastpoint).mag();
319 const float w(((next_w-iter_w)*(dist/length)+iter_w));
321 side_a.push_back(p+d*w);
322 side_b.push_back(p-d*w);
328 for(float n=0.0f;n<0.999999f;n+=1.0f/SAMPLES)
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));
334 side_a.push_back(p+d*w);
335 side_b.push_back(p-d*w);
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);
346 reverse(side_b.begin(),side_b.end());
352 // Insert code for adding end tip
353 if(round_tip[1] && !loop && side_a.size())
355 // remove the last point
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);
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
369 for(float n=0.0f;n<0.999999f;n+=1.0f/SAMPLES)
370 side_a.push_back(curve(n));
373 for(;!side_b.empty();side_b.pop_back())
374 side_a.push_back(side_b.back());
376 // Insert code for adding begin tip
377 if(round_tip[0] && !loop && side_a.size())
379 // remove the last point
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);
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
393 for(float n=0.0f;n<0.999999f;n+=1.0f/SAMPLES)
394 side_a.push_back(curve(n));
403 if(bline.get_contained_type()==ValueBase::TYPE_BLINEPOINT)
405 ValueBase value(bline);
409 value.set_loop(false);
413 loop_=value.get_loop();
415 segment_list=convert_bline_to_segment_list(value);
416 width_list=convert_bline_to_width_list(value);
426 if(segment_list.empty())
428 synfig::warning("Outline: segment_list is empty, layer disabled");
434 // Repair the width list if we need to
437 if(width_list.empty())
440 default_width=width_list.back();
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();
449 // Repair the zero tangents (if any)
451 vector<Segment>::iterator iter;
452 for(iter=segment_list.begin();iter!=segment_list.end();++iter)
454 if(iter->t1.mag_squared()<=EPSILON && iter->t2.mag_squared()<=EPSILON)
455 iter->t1=iter->t2=iter->p2-iter->p1;
459 vector<Real>::iterator iter;
460 vector<Real> scaled_width_list;
461 for(iter=width_list.begin();iter!=width_list.end();++iter)
463 scaled_width_list.push_back((*iter*width+expand)*0.5f);
466 Vector::value_type n;
467 etl::hermite<Vector> curve;
468 vector<Point> vector_list;
469 Vector last_tangent(segment_list.back().t2);
473 last_tangent=NO_LOOP_COOKIE;
476 vector<Segment>::iterator iter;
477 vector<Real>::iterator witer;
479 iter=segment_list.begin(),
480 witer=scaled_width_list.begin();
481 iter!=segment_list.end();
484 if(iter->t1.mag_squared()<=EPSILON && iter->t2.mag_squared()<=EPSILON)
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]);
499 etl::derivative<etl::hermite<Vector> > deriv(curve);
501 // without this if statement, the broken tangents would
503 if(sharp_cusps && last_tangent!=NO_LOOP_COOKIE && !last_tangent.is_equal_to(iter->t1))
505 //Vector curr_tangent(iter->t1);
506 Vector curr_tangent(deriv(CUSP_TANGENT_ADJUST));
508 const Vector t1(last_tangent.perp().norm());
509 const Vector t2(curr_tangent.perp().norm());
511 Point p1(iter->p1+t1*witer[0]);
512 Point p2(iter->p1+t2*witer[0]);
514 Real cross(t1*t2.perp());
516 if(cross>CUSP_THRESHOLD)
517 vector_list.push_back(line_intersection(p1,last_tangent,p2,curr_tangent));
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);
527 //last_tangent=iter->t2;
528 last_tangent=deriv(1.0f-CUSP_TANGENT_ADJUST);
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]);
536 if(round_tip[1] && !loop_/* && (!sharp_cusps || segment_list.front().p1!=segment_list.back().p2)*/)
538 // remove the last point
539 vector_list.pop_back();
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);
547 for(n=0.0f;n<1.0f;n+=1.0f/SAMPLES)
548 vector_list.push_back(curve(n));
550 // remove the last point
551 vector_list.pop_back();
556 last_tangent=NO_LOOP_COOKIE;
559 add_polygon(vector_list);
561 last_tangent=segment_list.front().t1;
565 // last_tangent=segment_list.back().t2;
568 vector<Segment>::reverse_iterator iter;
569 vector<Real>::reverse_iterator witer;
571 iter=segment_list.rbegin(),
572 witer=scaled_width_list.rbegin(),++witer;
573 !(iter==segment_list.rend());
577 if(iter->t1.mag_squared()<=EPSILON && iter->t2.mag_squared()<=EPSILON)
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]);
592 etl::derivative<etl::hermite<Vector> > deriv(curve);
594 // without this if statement, the broken tangents would
596 if(sharp_cusps && last_tangent!=NO_LOOP_COOKIE && !last_tangent.is_equal_to(iter->t2))
598 //Vector curr_tangent(iter->t2);
599 Vector curr_tangent(deriv(1.0f-CUSP_TANGENT_ADJUST));
601 const Vector t1(last_tangent.perp().norm());
602 const Vector t2(curr_tangent.perp().norm());
604 Point p1(iter->p2-t1*witer[-1]);
605 Point p2(iter->p2-t2*witer[-1]);
607 Real cross(t1*t2.perp());
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));
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);
621 //last_tangent=iter->t1;
622 last_tangent=deriv(CUSP_TANGENT_ADJUST);
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]);
629 if(round_tip[0] && !loop_/* && (!sharp_cusps || segment_list.front().p1!=segment_list.back().p2)*/)
631 // remove the last point
632 vector_list.pop_back();
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);
641 for(n=1.0;n>0.0;n-=1.0/SAMPLES)
642 vector_list.push_back(curve(n));
644 // remove the last point
645 vector_list.pop_back();
650 // reverse(vector_list.begin(),vector_list.end());
654 vector<Point>::iterator iter;
655 for(iter=vector_list.begin();iter!=vector_list.end();++iter)
656 if(!iter->is_valid())
658 synfig::error("Outline::sync(): Bad point in vector_list!");
660 //synfig::info("BLEHH__________--- x:%f, y:%f",vector_list.front()[0],vector_list.front()[1]);
664 add_polygon(vector_list);
668 } catch (...) { synfig::error("Outline::sync(): Exception thrown"); throw; }
674 Outline::set_param(const String & param, const ValueBase &value)
676 if(param=="segment_list")
678 if(dynamic_param_list().count("segment_list"))
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.");
685 synfig::warning("Outline::set_param(): The parameter \"segment_list\" is deprecated. Use \"bline\" instead.");
688 if( (param=="segment_list" || param=="bline") && value.get_type()==ValueBase::TYPE_LIST)
690 //if(value.get_contained_type()!=ValueBase::TYPE_BLINEPOINT)
698 if( param=="seg" && value.get_type()==ValueBase::TYPE_SEGMENT)
700 if(!segment_list.empty())
701 segment_list.clear();
703 segment_list.push_back(value.get(Segment()));
708 if( param=="w[0]" && value.get_type()==ValueBase::TYPE_REAL)
710 if(width_list.size()<2)
712 width_list.push_back(value.get(Real()));
713 width_list.push_back(value.get(Real()));
717 width_list[0]=value.get(Real());
724 if( param=="w[1]" && value.get_type()==ValueBase::TYPE_REAL)
726 if(width_list.size()<2)
728 width_list.push_back(value.get(Real()));
729 width_list.push_back(value.get(Real()));
733 width_list[1]=value.get(Real());
740 if( param=="width_list" && value.same_type_as(width_list))
748 IMPORT(round_tip[0]);
749 IMPORT(round_tip[1]);
751 IMPORT_PLUS(width,if(old_version){width*=2.0;});
754 IMPORT(homogeneous_width);
756 if(param!="vector_list")
757 return Layer_Polygon::set_param(param,value);
763 Outline::set_time(Context context, Time time)const
765 const_cast<Outline*>(this)->sync();
766 context.set_time(time);
770 Outline::set_time(Context context, Time time, Vector pos)const
772 const_cast<Outline*>(this)->sync();
773 context.set_time(time,pos);
777 Outline::get_param(const String& param)const
781 //EXPORT(width_list);
782 //EXPORT(segment_list);
783 EXPORT(homogeneous_width);
784 EXPORT(round_tip[0]);
785 EXPORT(round_tip[1]);
793 if(param!="vector_list")
794 return Layer_Polygon::get_param(param);
799 Outline::get_param_vocab()const
801 Layer::Vocab ret(Layer_Polygon::get_param_vocab());
803 // Pop off the polygon parameter from the polygon vocab
806 ret.push_back(ParamDesc("bline")
807 .set_local_name(_("Vertices"))
808 .set_origin("origin")
810 .set_description(_("A list of BLine Points"))
814 ret.push_back(ParamDesc("width_list")
815 .set_local_name(_("Point Widths"))
816 .set_origin("segment_list")
822 ret.push_back(ParamDesc("width")
824 .set_local_name(_("Outline Width"))
827 ret.push_back(ParamDesc("expand")
829 .set_local_name(_("Expand"))
832 ret.push_back(ParamDesc("sharp_cusps")
833 .set_local_name(_("Sharp Cusps"))
834 .set_description(_("Determines cusp type"))
837 ret.push_back(ParamDesc("round_tip[0]")
838 .set_local_name(_("Rounded Begin"))
839 .set_description(_("Round off the tip"))
842 ret.push_back(ParamDesc("round_tip[1]")
843 .set_local_name(_("Rounded End"))
844 .set_description(_("Round off the tip"))
846 ret.push_back(ParamDesc("loopyness")
847 .set_local_name(_("Loopyness"))
849 ret.push_back(ParamDesc("homogeneous_width")
850 .set_local_name(_("Homogeneous"))