1 /*! ========================================================================
4 ** $Id: curvegradient.cpp,v 1.2 2005/01/13 06:48:39 darco Exp $
6 ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
8 ** This software and associated documentation
9 ** are CONFIDENTIAL and PROPRIETARY property of
10 ** the above-mentioned copyright holder.
12 ** You may not copy, print, publish, or in any
13 ** other way distribute this software without
14 ** a prior written agreement with
15 ** the copyright holder.
17 ** === N O T E S ===========================================================
19 ** ========================================================================= */
21 /* === H E A D E R S ======================================================= */
30 #include "curvegradient.h"
32 #include <sinfg/string.h>
33 #include <sinfg/time.h>
34 #include <sinfg/context.h>
35 #include <sinfg/paramdesc.h>
36 #include <sinfg/renddesc.h>
37 #include <sinfg/surface.h>
38 #include <sinfg/value.h>
39 #include <sinfg/valuenode.h>
41 #include <ETL/hermite>
42 #include <ETL/calculus>
46 /* === M A C R O S ========================================================= */
48 /* === G L O B A L S ======================================================= */
50 SINFG_LAYER_INIT(CurveGradient);
51 SINFG_LAYER_SET_NAME(CurveGradient,"curve_gradient");
52 SINFG_LAYER_SET_LOCAL_NAME(CurveGradient,_("Curve Gradient"));
53 SINFG_LAYER_SET_CATEGORY(CurveGradient,_("Gradients"));
54 SINFG_LAYER_SET_VERSION(CurveGradient,"0.0");
55 SINFG_LAYER_SET_CVS_ID(CurveGradient,"$Id: curvegradient.cpp,v 1.2 2005/01/13 06:48:39 darco Exp $");
57 /* === P R O C E D U R E S ================================================= */
59 inline float calculate_distance(const sinfg::BLinePoint& a,const sinfg::BLinePoint& b)
62 const Point& c1(a.get_vertex());
63 const Point c2(a.get_vertex()+a.get_tangent2()/3);
64 const Point c3(b.get_vertex()-b.get_tangent1()/3);
65 const Point& c4(b.get_vertex());
66 return (c1-c2).mag()+(c2-c3).mag()+(c3-c4).mag();
71 inline float calculate_distance(const std::vector<sinfg::BLinePoint>& bline)
73 std::vector<sinfg::BLinePoint>::const_iterator iter,next,ret;
74 std::vector<sinfg::BLinePoint>::const_iterator end(bline.end());
81 // iter=--bline.end();
85 for(;next!=end;iter=next++)
88 etl::hermite<Vector> curve(
95 // dist+=calculate_distance(*iter,*next);
102 std::vector<sinfg::BLinePoint>::const_iterator
103 find_closest(const std::vector<sinfg::BLinePoint>& bline,const Point& p,bool loop=false,float *bline_dist_ret=0)
105 std::vector<sinfg::BLinePoint>::const_iterator iter,next,ret;
106 std::vector<sinfg::BLinePoint>::const_iterator end(bline.end());
109 float dist(100000000000.0);
113 float best_bline_dist(0);
114 float best_bline_len(0);
115 float total_bline_dist(0);
116 etl::hermite<Vector> best_curve;
125 for(;next!=end;iter=next++)
128 etl::hermite<Vector> curve(
131 iter->get_tangent2(),
136 const float t(curve.find_closest(p,6,0.01,0.99));
137 bp=curve(t);if((bp-p).mag_squared()<dist) { ret=iter; dist=(bp-p).mag_squared(); ret_t=t; }
144 //len=calculate_distance(*iter,*next);
148 #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; }
151 POINT_CHECK((1.0/6.0));
152 POINT_CHECK((2.0/6.0));
153 POINT_CHECK((3.0/6.0));
154 POINT_CHECK((4.0/6.0));
155 POINT_CHECK((5.0/6.0));
158 total_bline_dist+=len;
163 *bline_dist_ret=best_bline_dist+best_curve.find_distance(0,best_curve.find_closest(p));
164 // *bline_dist_ret=best_bline_dist+best_curve.find_closest(p)*best_bline_len;
170 /* === M E T H O D S ======================================================= */
173 CurveGradient::sync()
176 const Real mag(diff.inv_mag());
179 curve_length_=calculate_distance(bline);
183 CurveGradient::CurveGradient():
188 gradient(Color::black(), Color::white()),
193 bline.push_back(BLinePoint());
194 bline.push_back(BLinePoint());
195 bline.push_back(BLinePoint());
196 bline[0].set_vertex(Point(0,1));
197 bline[1].set_vertex(Point(0,-1));
198 bline[2].set_vertex(Point(1,0));
199 bline[0].set_tangent(bline[1].get_vertex()-bline[2].get_vertex()*0.5f);
200 bline[1].set_tangent(bline[2].get_vertex()-bline[0].get_vertex()*0.5f);
201 bline[2].set_tangent(bline[0].get_vertex()-bline[1].get_vertex()*0.5f);
202 bline[0].set_width(1.0f);
203 bline[1].set_width(1.0f);
204 bline[2].set_width(1.0f);
211 CurveGradient::color_func(const Point &point_, int quality, float supersample)const
222 return Color::alpha();
223 else if(bline.size()==1)
225 tangent=bline.front().get_tangent1();
226 p1=bline.front().get_vertex();
227 thickness=bline.front().get_width();
231 Point point(point_-offset);
233 std::vector<sinfg::BLinePoint>::const_iterator iter,next;
235 // Figure out the BLinePoints we will be using,
236 // Taking into account looping.
239 next=find_closest(bline,point,bline_loop,&perp_dist);
240 perp_dist/=curve_length_;
244 next=find_closest(bline,point,bline_loop);
247 if(next==bline.end()) next=bline.begin();
250 etl::hermite<Vector> curve(
253 iter->get_tangent2(),
257 // Setup the derivative function
258 etl::derivative<etl::hermite<Vector> > deriv(curve);
260 int search_iterations(7);
262 /*if(quality==0)search_iterations=8;
263 else if(quality<=2)search_iterations=10;
264 else if(quality<=4)search_iterations=8;
268 if(quality<=6)search_iterations=7;
269 else if(quality<=7)search_iterations=6;
270 else if(quality<=8)search_iterations=5;
271 else search_iterations=4;
279 // Figure out the closest point on the curve
280 const float t(curve.find_closest(point,search_iterations));
283 // Calculate our values
285 tangent=deriv(t).norm();
289 tangent*=curve_length_;
290 p1-=tangent*perp_dist;
291 tangent=-tangent.perp();
295 thickness=(next->get_width()-iter->get_width())*t+iter->get_width();
303 diff=tangent.perp()*thickness*width;
305 const Real mag(diff.inv_mag());
306 supersample=supersample*mag;
308 dist=((point_-offset)*diff-p1*diff);
315 /* diff=tangent.perp();
316 const Real mag(diff.inv_mag());
317 supersample=supersample*mag;
325 const Real mag(diff.inv_mag());
326 supersample=supersample*mag;
328 dist=((point_-offset)*diff-p1*diff);
339 if(dist>1)dist=2.0-dist;
344 if(dist+supersample*0.5>1.0)
346 Color pool(gradient(dist,supersample*0.5).premult_alpha()*(1.0-(dist-supersample*0.5)));
347 pool+=gradient((dist+supersample*0.5)-1.0,supersample*0.5).premult_alpha()*((dist+supersample*0.5)-1.0);
348 return pool.demult_alpha();
350 if(dist-supersample*0.5<0.0)
352 Color pool(gradient(dist,supersample*0.5).premult_alpha()*(dist+supersample*0.5));
353 pool+=gradient(1.0-(dist-supersample*0.5),supersample*0.5).premult_alpha()*(-(dist-supersample*0.5));
354 return pool.demult_alpha();
357 return gradient(dist,supersample);
361 CurveGradient::calc_supersample(const sinfg::Point &x, float pw,float ph)const
363 // return pw/(p2-p1).mag();
368 CurveGradient::hit_check(sinfg::Context context, const sinfg::Point &point)const
370 if(get_blend_method()==Color::BLEND_STRAIGHT && get_amount()>=0.5)
371 return const_cast<CurveGradient*>(this);
372 if(get_amount()==0.0)
373 return context.hit_check(point);
374 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)
375 return const_cast<CurveGradient*>(this);
376 return context.hit_check(point);
380 CurveGradient::set_param(const String & param, const ValueBase &value)
382 if(param=="p1" && value.same_as(p1))
388 if(param=="p2" && value.same_as(p2))
399 IMPORT(perpendicular);
401 if(param=="bline" && value.get_type()==ValueBase::TYPE_LIST)
404 bline_loop=value.get_loop();
414 return Layer_Composite::set_param(param,value);
418 CurveGradient::get_param(const String & param)const
428 EXPORT(perpendicular);
433 return Layer_Composite::get_param(param);
437 CurveGradient::get_param_vocab()const
439 Layer::Vocab ret(Layer_Composite::get_param_vocab());
441 ret.push_back(ParamDesc("offset")
442 .set_local_name(_("Offset"))
445 ret.push_back(ParamDesc("width")
447 .set_local_name(_("Width"))
450 ret.push_back(ParamDesc("bline")
451 .set_local_name(_("Vertices"))
452 .set_origin("offset")
454 .set_description(_("A list of BLine Points"))
458 ret.push_back(ParamDesc("gradient")
459 .set_local_name(_("Gradient"))
461 ret.push_back(ParamDesc("loop")
462 .set_local_name(_("Loop"))
464 ret.push_back(ParamDesc("zigzag")
465 .set_local_name(_("ZigZag"))
467 ret.push_back(ParamDesc("perpendicular")
468 .set_local_name(_("Perpendicular"))
475 CurveGradient::get_color(Context context, const Point &point)const
477 const Color color(color_func(point,0));
479 if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
482 return Color::blend(color,context.get_color(point),get_amount(),get_blend_method());
486 CurveGradient::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
488 SuperCallback supercb(cb,0,9500,10000);
490 if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
492 surface->set_wh(renddesc.get_w(),renddesc.get_h());
496 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
505 Surface::pen pen(surface->begin());
506 const Real pw(renddesc.get_pw()),ph(renddesc.get_ph());
508 Point tl(renddesc.get_tl());
509 const int w(surface->get_w());
510 const int h(surface->get_h());
512 if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
514 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
515 for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
516 pen.put_value(color_func(pos,quality,calc_supersample(pos,pw,ph)));
520 for(y=0,pos[1]=tl[1];y<h;y++,pen.inc_y(),pen.dec_x(x),pos[1]+=ph)
521 for(x=0,pos[0]=tl[0];x<w;x++,pen.inc_x(),pos[0]+=pw)
522 pen.put_value(Color::blend(color_func(pos,quality,calc_supersample(pos,pw,ph)),pen.get_value(),get_amount(),get_blend_method()));
525 // Mark our progress as finished
526 if(cb && !cb->amount_complete(10000,10000))