1 /* === S Y N F I G ========================================================= */
2 /*! \file curvegradient.cpp
3 ** \brief Implementation of the "Curve Gradient" 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 ** === N O T E S ===========================================================
24 ** ========================================================================= */
26 /* === H E A D E R S ======================================================= */
35 #include "curvegradient.h"
37 #include <synfig/string.h>
38 #include <synfig/time.h>
39 #include <synfig/context.h>
40 #include <synfig/paramdesc.h>
41 #include <synfig/renddesc.h>
42 #include <synfig/surface.h>
43 #include <synfig/value.h>
44 #include <synfig/valuenode.h>
46 #include <ETL/hermite>
47 #include <ETL/calculus>
51 /* === M A C R O S ========================================================= */
53 #define FAKE_TANGENT_STEP 0.000001
55 /* === G L O B A L S ======================================================= */
57 SYNFIG_LAYER_INIT(CurveGradient);
58 SYNFIG_LAYER_SET_NAME(CurveGradient,"curve_gradient");
59 SYNFIG_LAYER_SET_LOCAL_NAME(CurveGradient,N_("Curve Gradient"));
60 SYNFIG_LAYER_SET_CATEGORY(CurveGradient,N_("Gradients"));
61 SYNFIG_LAYER_SET_VERSION(CurveGradient,"0.0");
62 SYNFIG_LAYER_SET_CVS_ID(CurveGradient,"$Id$");
64 /* === P R O C E D U R E S ================================================= */
66 inline float calculate_distance(const synfig::BLinePoint& a,const synfig::BLinePoint& b)
69 const Point& c1(a.get_vertex());
70 const Point c2(a.get_vertex()+a.get_tangent2()/3);
71 const Point c3(b.get_vertex()-b.get_tangent1()/3);
72 const Point& c4(b.get_vertex());
73 return (c1-c2).mag()+(c2-c3).mag()+(c3-c4).mag();
78 inline float calculate_distance(const std::vector<synfig::BLinePoint>& bline, bool bline_loop)
80 std::vector<synfig::BLinePoint>::const_iterator iter,next,ret;
81 std::vector<synfig::BLinePoint>::const_iterator end(bline.end());
85 if (bline.empty()) return dist;
94 for(;next!=end;iter=next++)
97 etl::hermite<Vector> curve(
100 iter->get_tangent2(),
101 next->get_tangent1());
103 // dist+=calculate_distance(*iter,*next);
104 dist+=curve.length();
110 std::vector<synfig::BLinePoint>::const_iterator
111 find_closest(bool fast, const std::vector<synfig::BLinePoint>& bline,const Point& p,float& t,bool loop=false,float *bline_dist_ret=0)
113 std::vector<synfig::BLinePoint>::const_iterator iter,next,ret;
114 std::vector<synfig::BLinePoint>::const_iterator end(bline.end());
117 float dist(100000000000.0);
121 float best_bline_dist(0);
122 float best_bline_len(0);
123 float total_bline_dist(0);
125 etl::hermite<Vector> best_curve;
134 for(;next!=end;iter=next++)
137 etl::hermite<Vector> curve(
140 iter->get_tangent2(),
141 next->get_tangent1());
144 const float t(curve.find_closest(p,6,0.01,0.99));
145 bp=curve(t);if((bp-p).mag_squared()<dist) { ret=iter; dist=(bp-p).mag_squared(); ret_t=t; }
152 //len=calculate_distance(*iter,*next);
158 #define POINT_CHECK(x) bp=curve(x); thisdist=(bp-p).mag_squared(); if(thisdist<dist) { ret=iter; dist=thisdist; best_bline_dist=total_bline_dist; best_bline_len=len; best_curve=curve; }
160 POINT_CHECK((1.0/6.0));
161 POINT_CHECK((2.0/6.0));
162 POINT_CHECK((3.0/6.0));
163 POINT_CHECK((4.0/6.0));
164 POINT_CHECK((5.0/6.0));
169 float pos = curve.find_closest(fast, p);
170 thisdist=(curve(pos)-p).mag_squared();
175 best_bline_dist=total_bline_dist;
182 total_bline_dist+=len;
189 //! \todo is this a redundant call to find_closest()?
190 // note bline_dist_ret is null except when 'perpendicular' is true
191 *bline_dist_ret=best_bline_dist+best_curve.find_distance(0,best_curve.find_closest(fast, p));
192 // *bline_dist_ret=best_bline_dist+best_curve.find_closest(fast, p)*best_bline_len;
198 /* === M E T H O D S ======================================================= */
201 CurveGradient::sync()
203 curve_length_=calculate_distance(bline, bline_loop);
207 CurveGradient::CurveGradient():
208 Layer_Composite(1.0,Color::BLEND_COMPOSITE),
211 gradient(Color::black(), Color::white()),
214 perpendicular(false),
217 bline.push_back(BLinePoint());
218 bline.push_back(BLinePoint());
219 bline.push_back(BLinePoint());
220 bline[0].set_vertex(Point(0,1));
221 bline[1].set_vertex(Point(0,-1));
222 bline[2].set_vertex(Point(1,0));
223 bline[0].set_tangent(bline[1].get_vertex()-bline[2].get_vertex()*0.5f);
224 bline[1].set_tangent(bline[2].get_vertex()-bline[0].get_vertex()*0.5f);
225 bline[2].set_tangent(bline[0].get_vertex()-bline[1].get_vertex()*0.5f);
226 bline[0].set_width(1.0f);
227 bline[1].set_width(1.0f);
228 bline[2].set_width(1.0f);
232 Layer::Vocab voc(get_param_vocab());
233 Layer::fill_static(voc);
237 CurveGradient::color_func(const Point &point_, int quality, float supersample)const
246 bool edge_case = false;
249 return Color::alpha();
250 else if(bline.size()==1)
252 tangent=bline.front().get_tangent1();
253 p1=bline.front().get_vertex();
254 thickness=bline.front().get_width();
259 Point point(point_-origin);
261 std::vector<synfig::BLinePoint>::const_iterator iter,next;
263 // Figure out the BLinePoints we will be using,
264 // Taking into account looping.
267 next=find_closest(fast,bline,point,t,bline_loop,&perp_dist);
268 perp_dist/=curve_length_;
270 else // not perpendicular
272 next=find_closest(fast,bline,point,t,bline_loop);
276 if(next==bline.end()) next=bline.begin();
279 etl::hermite<Vector> curve(
282 iter->get_tangent2(),
286 // Setup the derivative function
287 etl::derivative<etl::hermite<Vector> > deriv(curve);
289 int search_iterations(7);
291 /*if(quality==0)search_iterations=8;
292 else if(quality<=2)search_iterations=10;
293 else if(quality<=4)search_iterations=8;
300 else // not perpendicular
302 if(quality<=6)search_iterations=7;
303 else if(quality<=7)search_iterations=6;
304 else if(quality<=8)search_iterations=5;
305 else search_iterations=4;
308 // Figure out the closest point on the curve
310 t = curve.find_closest(fast, point,search_iterations);
312 // Calculate our values
313 p1=curve(t); // the closest point on the curve
314 tangent=deriv(t); // the tangent at that point
316 // if the point we're nearest to is at either end of the
317 // bline, our distance from the curve is the distance from the
318 // point on the curve. we need to know which side of the
319 // curve we're on, so find the average of the two tangents at
321 if (t<0.00001 || t>0.99999)
323 bool zero_tangent = (tangent[0] == 0 && tangent[1] == 0);
327 if (iter->get_split_tangent_flag() || zero_tangent)
329 // fake the current tangent if we need to
330 if (zero_tangent) tangent = curve(FAKE_TANGENT_STEP) - curve(0);
332 // calculate the other tangent
333 Vector other_tangent(iter->get_tangent1());
334 if (other_tangent[0] == 0 && other_tangent[1] == 0)
336 // find the previous blinepoint
337 std::vector<synfig::BLinePoint>::const_iterator prev;
338 if (iter != bline.begin()) (prev = iter)--;
339 else if (loop) (prev = bline.end())--;
342 etl::hermite<Vector> other_curve(prev->get_vertex(), iter->get_vertex(), prev->get_tangent2(), iter->get_tangent1());
343 other_tangent = other_curve(1) - other_curve(1-FAKE_TANGENT_STEP);
346 // normalise and sum the two tangents
347 tangent=(other_tangent.norm()+tangent.norm());
353 if (next->get_split_tangent_flag() || zero_tangent)
355 // fake the current tangent if we need to
356 if (zero_tangent) tangent = curve(1) - curve(1-FAKE_TANGENT_STEP);
358 // calculate the other tangent
359 Vector other_tangent(next->get_tangent2());
360 if (other_tangent[0] == 0 && other_tangent[1] == 0)
362 // find the next blinepoint
363 std::vector<synfig::BLinePoint>::const_iterator next2(next);
364 if (++next2 == bline.end())
366 if (loop) next2 = bline.begin();
370 etl::hermite<Vector> other_curve(next->get_vertex(), next2->get_vertex(), next->get_tangent2(), next2->get_tangent1());
371 other_tangent = other_curve(FAKE_TANGENT_STEP) - other_curve(0);
374 // normalise and sum the two tangents
375 tangent=(other_tangent.norm()+tangent.norm());
380 tangent = tangent.norm();
384 tangent*=curve_length_;
385 p1-=tangent*perp_dist;
386 tangent=-tangent.perp();
388 else // not perpendicular
389 // the width of the bline at the closest point on the curve
390 thickness=(next->get_width()-iter->get_width())*t+iter->get_width();
398 /* diff=tangent.perp();
399 const Real mag(diff.inv_mag());
400 supersample=supersample*mag;
408 const Real mag(diff.inv_mag());
409 supersample=supersample*mag;
411 dist=(point_-origin - p1)*diff;
414 else // not perpendicular
418 diff=(p1-(point_-origin));
419 if(diff*tangent.perp()<0) diff=-diff;
420 diff=diff.norm()*thickness*width;
423 diff=tangent.perp()*thickness*width;
426 const Real mag(diff.inv_mag());
427 supersample=supersample*mag;
429 dist=(point_-origin - p1)*diff;
439 if(dist>1)dist=2.0-dist;
444 if(dist+supersample*0.5>1.0)
446 float left(supersample*0.5-(dist-1.0));
447 float right(supersample*0.5+(dist-1.0));
448 Color pool(gradient(1.0-(left*0.5),left).premult_alpha()*left/supersample);
449 if (zigzag) pool+=gradient(1.0-right*0.5,right).premult_alpha()*right/supersample;
450 else pool+=gradient(right*0.5,right).premult_alpha()*right/supersample;
451 return pool.demult_alpha();
453 if(dist-supersample*0.5<0.0)
455 float left(supersample*0.5-dist);
456 float right(supersample*0.5+dist);
457 Color pool(gradient(right*0.5,right).premult_alpha()*right/supersample);
458 if (zigzag) pool+=gradient(left*0.5,left).premult_alpha()*left/supersample;
459 else pool+=gradient(1.0-left*0.5,left).premult_alpha()*left/supersample;
460 return pool.demult_alpha();
463 return gradient(dist,supersample);
467 CurveGradient::calc_supersample(const synfig::Point &/*x*/, float pw,float /*ph*/)const
472 synfig::Layer::Handle
473 CurveGradient::hit_check(synfig::Context context, const synfig::Point &point)const
475 if(get_blend_method()==Color::BLEND_STRAIGHT && get_amount()>=0.5)
476 return const_cast<CurveGradient*>(this);
477 if(get_amount()==0.0)
478 return context.hit_check(point);
479 if((get_blend_method()==Color::BLEND_STRAIGHT || get_blend_method()==Color::BLEND_COMPOSITE|| get_blend_method()==Color::BLEND_ONTO) && color_func(point).get_a()>0.5)
480 return const_cast<CurveGradient*>(this);
481 return context.hit_check(point);
485 CurveGradient::set_param(const String & param, const ValueBase &value)
490 IMPORT(perpendicular);
493 if(param=="bline" && value.get_type()==ValueBase::TYPE_LIST)
496 bline_loop=value.get_loop();
507 IMPORT_AS(origin,"offset");
509 return Layer_Composite::set_param(param,value);
513 CurveGradient::get_param(const String & param)const
521 EXPORT(perpendicular);
527 return Layer_Composite::get_param(param);
531 CurveGradient::get_param_vocab()const
533 Layer::Vocab ret(Layer_Composite::get_param_vocab());
535 ret.push_back(ParamDesc("origin")
536 .set_local_name(_("Origin")));
538 ret.push_back(ParamDesc("width")
540 .set_local_name(_("Width")));
542 ret.push_back(ParamDesc("bline")
543 .set_local_name(_("Vertices"))
544 .set_origin("origin")
546 .set_description(_("A list of BLine Points")));
548 ret.push_back(ParamDesc("gradient")
549 .set_local_name(_("Gradient")));
550 ret.push_back(ParamDesc("loop")
551 .set_local_name(_("Loop")));
552 ret.push_back(ParamDesc("zigzag")
553 .set_local_name(_("ZigZag")));
554 ret.push_back(ParamDesc("perpendicular")
555 .set_local_name(_("Perpendicular")));
556 ret.push_back(ParamDesc("fast")
557 .set_local_name(_("Fast")));
563 CurveGradient::get_color(Context context, const Point &point)const
565 const Color color(color_func(point,0));
567 if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
570 return Color::blend(color,context.get_color(point),get_amount(),get_blend_method());
574 CurveGradient::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
576 SuperCallback supercb(cb,0,9500,10000);
578 if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
580 surface->set_wh(renddesc.get_w(),renddesc.get_h());
584 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
593 Surface::pen pen(surface->begin());
594 const Real pw(renddesc.get_pw()),ph(renddesc.get_ph());
596 Point tl(renddesc.get_tl());
597 const int w(surface->get_w());
598 const int h(surface->get_h());
600 if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
602 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
603 for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
604 pen.put_value(color_func(pos,quality,calc_supersample(pos,pw,ph)));
608 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
609 for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
610 pen.put_value(Color::blend(color_func(pos,quality,calc_supersample(pos,pw,ph)),pen.get_value(),get_amount(),get_blend_method()));
613 // Mark our progress as finished
614 if(cb && !cb->amount_complete(10000,10000))