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 /* === G L O B A L S ======================================================= */
55 SYNFIG_LAYER_INIT(CurveGradient);
56 SYNFIG_LAYER_SET_NAME(CurveGradient,"curve_gradient");
57 SYNFIG_LAYER_SET_LOCAL_NAME(CurveGradient,N_("Curve Gradient"));
58 SYNFIG_LAYER_SET_CATEGORY(CurveGradient,N_("Gradients"));
59 SYNFIG_LAYER_SET_VERSION(CurveGradient,"0.0");
60 SYNFIG_LAYER_SET_CVS_ID(CurveGradient,"$Id$");
62 /* === P R O C E D U R E S ================================================= */
64 inline float calculate_distance(const synfig::BLinePoint& a,const synfig::BLinePoint& b)
67 const Point& c1(a.get_vertex());
68 const Point c2(a.get_vertex()+a.get_tangent2()/3);
69 const Point c3(b.get_vertex()-b.get_tangent1()/3);
70 const Point& c4(b.get_vertex());
71 return (c1-c2).mag()+(c2-c3).mag()+(c3-c4).mag();
76 inline float calculate_distance(const std::vector<synfig::BLinePoint>& bline, bool bline_loop)
78 std::vector<synfig::BLinePoint>::const_iterator iter,next,ret;
79 std::vector<synfig::BLinePoint>::const_iterator end(bline.end());
83 if (bline.empty()) return dist;
92 for(;next!=end;iter=next++)
95 etl::hermite<Vector> curve(
99 next->get_tangent1());
101 // dist+=calculate_distance(*iter,*next);
102 dist+=curve.length();
108 std::vector<synfig::BLinePoint>::const_iterator
109 find_closest(bool fast, const std::vector<synfig::BLinePoint>& bline,const Point& p,float& t,bool loop=false,float *bline_dist_ret=0)
111 std::vector<synfig::BLinePoint>::const_iterator iter,next,ret;
112 std::vector<synfig::BLinePoint>::const_iterator end(bline.end());
115 float dist(100000000000.0);
119 float best_bline_dist(0);
120 float best_bline_len(0);
121 float total_bline_dist(0);
123 etl::hermite<Vector> best_curve;
132 for(;next!=end;iter=next++)
135 etl::hermite<Vector> curve(
138 iter->get_tangent2(),
139 next->get_tangent1());
142 const float t(curve.find_closest(p,6,0.01,0.99));
143 bp=curve(t);if((bp-p).mag_squared()<dist) { ret=iter; dist=(bp-p).mag_squared(); ret_t=t; }
150 //len=calculate_distance(*iter,*next);
156 #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; }
158 POINT_CHECK((1.0/6.0));
159 POINT_CHECK((2.0/6.0));
160 POINT_CHECK((3.0/6.0));
161 POINT_CHECK((4.0/6.0));
162 POINT_CHECK((5.0/6.0));
167 float pos = curve.find_closest(fast, p);
168 thisdist=(curve(pos)-p).mag_squared();
173 best_bline_dist=total_bline_dist;
180 total_bline_dist+=len;
187 //! \todo is this a redundant call to find_closest()?
188 // note bline_dist_ret is null except when 'perpendicular' is true
189 *bline_dist_ret=best_bline_dist+best_curve.find_distance(0,best_curve.find_closest(fast, p));
190 // *bline_dist_ret=best_bline_dist+best_curve.find_closest(fast, p)*best_bline_len;
196 /* === M E T H O D S ======================================================= */
199 CurveGradient::sync()
201 curve_length_=calculate_distance(bline, bline_loop);
205 CurveGradient::CurveGradient():
208 gradient(Color::black(), Color::white()),
211 perpendicular(false),
214 bline.push_back(BLinePoint());
215 bline.push_back(BLinePoint());
216 bline.push_back(BLinePoint());
217 bline[0].set_vertex(Point(0,1));
218 bline[1].set_vertex(Point(0,-1));
219 bline[2].set_vertex(Point(1,0));
220 bline[0].set_tangent(bline[1].get_vertex()-bline[2].get_vertex()*0.5f);
221 bline[1].set_tangent(bline[2].get_vertex()-bline[0].get_vertex()*0.5f);
222 bline[2].set_tangent(bline[0].get_vertex()-bline[1].get_vertex()*0.5f);
223 bline[0].set_width(1.0f);
224 bline[1].set_width(1.0f);
225 bline[2].set_width(1.0f);
232 CurveGradient::color_func(const Point &point_, int quality, float supersample)const
241 bool edge_case = false;
244 return Color::alpha();
245 else if(bline.size()==1)
247 tangent=bline.front().get_tangent1();
248 p1=bline.front().get_vertex();
249 thickness=bline.front().get_width();
254 Point point(point_-origin);
256 std::vector<synfig::BLinePoint>::const_iterator iter,next;
258 // Figure out the BLinePoints we will be using,
259 // Taking into account looping.
262 next=find_closest(fast,bline,point,t,bline_loop,&perp_dist);
263 perp_dist/=curve_length_;
265 else // not perpendicular
267 next=find_closest(fast,bline,point,t,bline_loop);
271 if(next==bline.end()) next=bline.begin();
274 etl::hermite<Vector> curve(
277 iter->get_tangent2(),
281 // Setup the derivative function
282 etl::derivative<etl::hermite<Vector> > deriv(curve);
284 int search_iterations(7);
286 /*if(quality==0)search_iterations=8;
287 else if(quality<=2)search_iterations=10;
288 else if(quality<=4)search_iterations=8;
295 else // not perpendicular
297 if(quality<=6)search_iterations=7;
298 else if(quality<=7)search_iterations=6;
299 else if(quality<=8)search_iterations=5;
300 else search_iterations=4;
303 // Figure out the closest point on the curve
305 t = curve.find_closest(fast, point,search_iterations);
307 // Calculate our values
308 p1=curve(t); // the closest point on the curve
309 tangent=deriv(t).norm(); // the unit tangent at that point
311 // if the point we're nearest to is at either end of the
312 // bline, our distance from the curve is the distance from the
313 // point on the curve. we need to know which side of the
314 // curve we're on, so find the average of the two tangents at
316 if (t<0.00001 || t>0.99999)
320 if (iter->get_split_tangent_flag())
322 tangent=(iter->get_tangent1().norm()+tangent).norm();
328 if (next->get_split_tangent_flag())
330 tangent=(next->get_tangent2().norm()+tangent).norm();
338 tangent*=curve_length_;
339 p1-=tangent*perp_dist;
340 tangent=-tangent.perp();
342 else // not perpendicular
343 // the width of the bline at the closest point on the curve
344 thickness=(next->get_width()-iter->get_width())*t+iter->get_width();
352 /* diff=tangent.perp();
353 const Real mag(diff.inv_mag());
354 supersample=supersample*mag;
362 const Real mag(diff.inv_mag());
363 supersample=supersample*mag;
365 dist=((point_-origin)*diff-p1*diff);
368 else // not perpendicular
372 diff=(p1-(point_-origin));
373 if(diff*tangent.perp()<0) diff=-diff;
374 diff=diff.norm()*thickness*width;
377 diff=tangent.perp()*thickness*width;
380 const Real mag(diff.inv_mag());
381 supersample=supersample*mag;
383 dist=((point_-origin)*diff-p1*diff);
393 if(dist>1)dist=2.0-dist;
398 if(dist+supersample*0.5>1.0)
400 float left(supersample*0.5-(dist-1.0));
401 float right(supersample*0.5+(dist-1.0));
402 Color pool(gradient(1.0-(left*0.5),left).premult_alpha()*left/supersample);
403 if (zigzag) pool+=gradient(1.0-right*0.5,right).premult_alpha()*right/supersample;
404 else pool+=gradient(right*0.5,right).premult_alpha()*right/supersample;
405 return pool.demult_alpha();
407 if(dist-supersample*0.5<0.0)
409 float left(supersample*0.5-dist);
410 float right(supersample*0.5+dist);
411 Color pool(gradient(right*0.5,right).premult_alpha()*right/supersample);
412 if (zigzag) pool+=gradient(left*0.5,left).premult_alpha()*left/supersample;
413 else pool+=gradient(1.0-left*0.5,left).premult_alpha()*left/supersample;
414 return pool.demult_alpha();
417 return gradient(dist,supersample);
421 CurveGradient::calc_supersample(const synfig::Point &/*x*/, float pw,float /*ph*/)const
426 synfig::Layer::Handle
427 CurveGradient::hit_check(synfig::Context context, const synfig::Point &point)const
429 if(get_blend_method()==Color::BLEND_STRAIGHT && get_amount()>=0.5)
430 return const_cast<CurveGradient*>(this);
431 if(get_amount()==0.0)
432 return context.hit_check(point);
433 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)
434 return const_cast<CurveGradient*>(this);
435 return context.hit_check(point);
439 CurveGradient::set_param(const String & param, const ValueBase &value)
444 IMPORT(perpendicular);
447 if(param=="bline" && value.get_type()==ValueBase::TYPE_LIST)
450 bline_loop=value.get_loop();
461 IMPORT_AS(origin,"offset");
463 return Layer_Composite::set_param(param,value);
467 CurveGradient::get_param(const String & param)const
475 EXPORT(perpendicular);
481 return Layer_Composite::get_param(param);
485 CurveGradient::get_param_vocab()const
487 Layer::Vocab ret(Layer_Composite::get_param_vocab());
489 ret.push_back(ParamDesc("origin")
490 .set_local_name(_("Origin")));
492 ret.push_back(ParamDesc("width")
494 .set_local_name(_("Width")));
496 ret.push_back(ParamDesc("bline")
497 .set_local_name(_("Vertices"))
498 .set_origin("origin")
500 .set_description(_("A list of BLine Points")));
502 ret.push_back(ParamDesc("gradient")
503 .set_local_name(_("Gradient")));
504 ret.push_back(ParamDesc("loop")
505 .set_local_name(_("Loop")));
506 ret.push_back(ParamDesc("zigzag")
507 .set_local_name(_("ZigZag")));
508 ret.push_back(ParamDesc("perpendicular")
509 .set_local_name(_("Perpendicular")));
510 ret.push_back(ParamDesc("fast")
511 .set_local_name(_("Fast")));
517 CurveGradient::get_color(Context context, const Point &point)const
519 const Color color(color_func(point,0));
521 if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
524 return Color::blend(color,context.get_color(point),get_amount(),get_blend_method());
528 CurveGradient::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
530 SuperCallback supercb(cb,0,9500,10000);
532 if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
534 surface->set_wh(renddesc.get_w(),renddesc.get_h());
538 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
547 Surface::pen pen(surface->begin());
548 const Real pw(renddesc.get_pw()),ph(renddesc.get_ph());
550 Point tl(renddesc.get_tl());
551 const int w(surface->get_w());
552 const int h(surface->get_h());
554 if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
556 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
557 for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
558 pen.put_value(color_func(pos,quality,calc_supersample(pos,pw,ph)));
562 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
563 for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
564 pen.put_value(Color::blend(color_func(pos,quality,calc_supersample(pos,pw,ph)),pen.get_value(),get_amount(),get_blend_method()));
567 // Mark our progress as finished
568 if(cb && !cb->amount_complete(10000,10000))