1 /*! ========================================================================
4 ** $Id: curvegradient.cpp,v 1.2 2005/01/13 06:48:39 darco Exp $
6 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
8 ** This package is free software; you can redistribute it and/or
9 ** modify it under the terms of the GNU General Public License as
10 ** published by the Free Software Foundation; either version 2 of
11 ** the License, or (at your option) any later version.
13 ** This package is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 ** General Public License for more details.
18 ** === N O T E S ===========================================================
20 ** ========================================================================= */
22 /* === H E A D E R S ======================================================= */
31 #include "curvegradient.h"
33 #include <synfig/string.h>
34 #include <synfig/time.h>
35 #include <synfig/context.h>
36 #include <synfig/paramdesc.h>
37 #include <synfig/renddesc.h>
38 #include <synfig/surface.h>
39 #include <synfig/value.h>
40 #include <synfig/valuenode.h>
42 #include <ETL/hermite>
43 #include <ETL/calculus>
47 /* === M A C R O S ========================================================= */
49 /* === G L O B A L S ======================================================= */
51 SYNFIG_LAYER_INIT(CurveGradient);
52 SYNFIG_LAYER_SET_NAME(CurveGradient,"curve_gradient");
53 SYNFIG_LAYER_SET_LOCAL_NAME(CurveGradient,_("Curve Gradient"));
54 SYNFIG_LAYER_SET_CATEGORY(CurveGradient,_("Gradients"));
55 SYNFIG_LAYER_SET_VERSION(CurveGradient,"0.0");
56 SYNFIG_LAYER_SET_CVS_ID(CurveGradient,"$Id: curvegradient.cpp,v 1.2 2005/01/13 06:48:39 darco Exp $");
58 /* === P R O C E D U R E S ================================================= */
60 inline float calculate_distance(const synfig::BLinePoint& a,const synfig::BLinePoint& b)
63 const Point& c1(a.get_vertex());
64 const Point c2(a.get_vertex()+a.get_tangent2()/3);
65 const Point c3(b.get_vertex()-b.get_tangent1()/3);
66 const Point& c4(b.get_vertex());
67 return (c1-c2).mag()+(c2-c3).mag()+(c3-c4).mag();
72 inline float calculate_distance(const std::vector<synfig::BLinePoint>& bline)
74 std::vector<synfig::BLinePoint>::const_iterator iter,next,ret;
75 std::vector<synfig::BLinePoint>::const_iterator end(bline.end());
82 // iter=--bline.end();
86 for(;next!=end;iter=next++)
89 etl::hermite<Vector> curve(
96 // dist+=calculate_distance(*iter,*next);
103 std::vector<synfig::BLinePoint>::const_iterator
104 find_closest(const std::vector<synfig::BLinePoint>& bline,const Point& p,bool loop=false,float *bline_dist_ret=0)
106 std::vector<synfig::BLinePoint>::const_iterator iter,next,ret;
107 std::vector<synfig::BLinePoint>::const_iterator end(bline.end());
110 float dist(100000000000.0);
114 float best_bline_dist(0);
115 float best_bline_len(0);
116 float total_bline_dist(0);
117 etl::hermite<Vector> best_curve;
126 for(;next!=end;iter=next++)
129 etl::hermite<Vector> curve(
132 iter->get_tangent2(),
137 const float t(curve.find_closest(p,6,0.01,0.99));
138 bp=curve(t);if((bp-p).mag_squared()<dist) { ret=iter; dist=(bp-p).mag_squared(); ret_t=t; }
145 //len=calculate_distance(*iter,*next);
149 #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; }
152 POINT_CHECK((1.0/6.0));
153 POINT_CHECK((2.0/6.0));
154 POINT_CHECK((3.0/6.0));
155 POINT_CHECK((4.0/6.0));
156 POINT_CHECK((5.0/6.0));
159 total_bline_dist+=len;
164 *bline_dist_ret=best_bline_dist+best_curve.find_distance(0,best_curve.find_closest(p));
165 // *bline_dist_ret=best_bline_dist+best_curve.find_closest(p)*best_bline_len;
171 /* === M E T H O D S ======================================================= */
174 CurveGradient::sync()
177 const Real mag(diff.inv_mag());
180 curve_length_=calculate_distance(bline);
184 CurveGradient::CurveGradient():
189 gradient(Color::black(), Color::white()),
194 bline.push_back(BLinePoint());
195 bline.push_back(BLinePoint());
196 bline.push_back(BLinePoint());
197 bline[0].set_vertex(Point(0,1));
198 bline[1].set_vertex(Point(0,-1));
199 bline[2].set_vertex(Point(1,0));
200 bline[0].set_tangent(bline[1].get_vertex()-bline[2].get_vertex()*0.5f);
201 bline[1].set_tangent(bline[2].get_vertex()-bline[0].get_vertex()*0.5f);
202 bline[2].set_tangent(bline[0].get_vertex()-bline[1].get_vertex()*0.5f);
203 bline[0].set_width(1.0f);
204 bline[1].set_width(1.0f);
205 bline[2].set_width(1.0f);
212 CurveGradient::color_func(const Point &point_, int quality, float supersample)const
223 return Color::alpha();
224 else if(bline.size()==1)
226 tangent=bline.front().get_tangent1();
227 p1=bline.front().get_vertex();
228 thickness=bline.front().get_width();
232 Point point(point_-offset);
234 std::vector<synfig::BLinePoint>::const_iterator iter,next;
236 // Figure out the BLinePoints we will be using,
237 // Taking into account looping.
240 next=find_closest(bline,point,bline_loop,&perp_dist);
241 perp_dist/=curve_length_;
245 next=find_closest(bline,point,bline_loop);
248 if(next==bline.end()) next=bline.begin();
251 etl::hermite<Vector> curve(
254 iter->get_tangent2(),
258 // Setup the derivative function
259 etl::derivative<etl::hermite<Vector> > deriv(curve);
261 int search_iterations(7);
263 /*if(quality==0)search_iterations=8;
264 else if(quality<=2)search_iterations=10;
265 else if(quality<=4)search_iterations=8;
269 if(quality<=6)search_iterations=7;
270 else if(quality<=7)search_iterations=6;
271 else if(quality<=8)search_iterations=5;
272 else search_iterations=4;
280 // Figure out the closest point on the curve
281 const float t(curve.find_closest(point,search_iterations));
284 // Calculate our values
286 tangent=deriv(t).norm();
290 tangent*=curve_length_;
291 p1-=tangent*perp_dist;
292 tangent=-tangent.perp();
296 thickness=(next->get_width()-iter->get_width())*t+iter->get_width();
304 diff=tangent.perp()*thickness*width;
306 const Real mag(diff.inv_mag());
307 supersample=supersample*mag;
309 dist=((point_-offset)*diff-p1*diff);
316 /* diff=tangent.perp();
317 const Real mag(diff.inv_mag());
318 supersample=supersample*mag;
326 const Real mag(diff.inv_mag());
327 supersample=supersample*mag;
329 dist=((point_-offset)*diff-p1*diff);
340 if(dist>1)dist=2.0-dist;
345 if(dist+supersample*0.5>1.0)
347 Color pool(gradient(dist,supersample*0.5).premult_alpha()*(1.0-(dist-supersample*0.5)));
348 pool+=gradient((dist+supersample*0.5)-1.0,supersample*0.5).premult_alpha()*((dist+supersample*0.5)-1.0);
349 return pool.demult_alpha();
351 if(dist-supersample*0.5<0.0)
353 Color pool(gradient(dist,supersample*0.5).premult_alpha()*(dist+supersample*0.5));
354 pool+=gradient(1.0-(dist-supersample*0.5),supersample*0.5).premult_alpha()*(-(dist-supersample*0.5));
355 return pool.demult_alpha();
358 return gradient(dist,supersample);
362 CurveGradient::calc_supersample(const synfig::Point &x, float pw,float ph)const
364 // return pw/(p2-p1).mag();
368 synfig::Layer::Handle
369 CurveGradient::hit_check(synfig::Context context, const synfig::Point &point)const
371 if(get_blend_method()==Color::BLEND_STRAIGHT && get_amount()>=0.5)
372 return const_cast<CurveGradient*>(this);
373 if(get_amount()==0.0)
374 return context.hit_check(point);
375 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)
376 return const_cast<CurveGradient*>(this);
377 return context.hit_check(point);
381 CurveGradient::set_param(const String & param, const ValueBase &value)
383 if(param=="p1" && value.same_as(p1))
389 if(param=="p2" && value.same_as(p2))
400 IMPORT(perpendicular);
402 if(param=="bline" && value.get_type()==ValueBase::TYPE_LIST)
405 bline_loop=value.get_loop();
415 return Layer_Composite::set_param(param,value);
419 CurveGradient::get_param(const String & param)const
429 EXPORT(perpendicular);
434 return Layer_Composite::get_param(param);
438 CurveGradient::get_param_vocab()const
440 Layer::Vocab ret(Layer_Composite::get_param_vocab());
442 ret.push_back(ParamDesc("offset")
443 .set_local_name(_("Offset"))
446 ret.push_back(ParamDesc("width")
448 .set_local_name(_("Width"))
451 ret.push_back(ParamDesc("bline")
452 .set_local_name(_("Vertices"))
453 .set_origin("offset")
455 .set_description(_("A list of BLine Points"))
459 ret.push_back(ParamDesc("gradient")
460 .set_local_name(_("Gradient"))
462 ret.push_back(ParamDesc("loop")
463 .set_local_name(_("Loop"))
465 ret.push_back(ParamDesc("zigzag")
466 .set_local_name(_("ZigZag"))
468 ret.push_back(ParamDesc("perpendicular")
469 .set_local_name(_("Perpendicular"))
476 CurveGradient::get_color(Context context, const Point &point)const
478 const Color color(color_func(point,0));
480 if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
483 return Color::blend(color,context.get_color(point),get_amount(),get_blend_method());
487 CurveGradient::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
489 SuperCallback supercb(cb,0,9500,10000);
491 if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
493 surface->set_wh(renddesc.get_w(),renddesc.get_h());
497 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
506 Surface::pen pen(surface->begin());
507 const Real pw(renddesc.get_pw()),ph(renddesc.get_ph());
509 Point tl(renddesc.get_tl());
510 const int w(surface->get_w());
511 const int h(surface->get_h());
513 if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
515 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
516 for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
517 pen.put_value(color_func(pos,quality,calc_supersample(pos,pw,ph)));
521 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
522 for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
523 pen.put_value(Color::blend(color_func(pos,quality,calc_supersample(pos,pw,ph)),pen.get_value(),get_amount(),get_blend_method()));
526 // Mark our progress as finished
527 if(cb && !cb->amount_complete(10000,10000))