1 /* === S I N F G =========================================================== */
3 ** \brief Template Header
5 ** $Id: circle.cpp,v 1.2 2005/01/24 03:08:17 darco Exp $
8 ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
10 ** This software and associated documentation
11 ** are CONFIDENTIAL and PROPRIETARY property of
12 ** the above-mentioned copyright holder.
14 ** You may not copy, print, publish, or in any
15 ** other way distribute this software without
16 ** a prior written agreement with
17 ** the copyright holder.
20 /* ========================================================================= */
22 /* === H E A D E R S ======================================================= */
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>
45 using namespace sinfg;
49 /* -- G L O B A L S --------------------------------------------------------- */
51 SINFG_LAYER_INIT(Circle);
52 SINFG_LAYER_SET_NAME(Circle,"circle");
53 SINFG_LAYER_SET_LOCAL_NAME(Circle,_("Circle"));
54 SINFG_LAYER_SET_CATEGORY(Circle,_("Geometry"));
55 SINFG_LAYER_SET_VERSION(Circle,"0.1");
56 SINFG_LAYER_SET_CVS_ID(Circle,"$Id: circle.cpp,v 1.2 2005/01/24 03:08:17 darco Exp $");
58 /* -- F U N C T I O N S ----------------------------------------------------- */
61 Layer_Composite (1.0,Color::BLEND_STRAIGHT),
62 color (Color::black()),
67 falloff (FALLOFF_INTERPOLATION_LINEAR)
73 Circle::ImportParameters(const String ¶m, const ValueBase &value)
82 return Layer_Composite::set_param(param,value);
86 Circle::set_param(const String ¶m, const ValueBase &value)
88 if(ImportParameters(param,value))
98 Circle::get_param(const String ¶m)const
110 return Layer_Composite::get_param(param);
114 Circle::get_param_vocab()const
116 Layer::Vocab ret(Layer_Composite::get_param_vocab());
118 ret.push_back(ParamDesc("color")
119 .set_local_name(_("Color"))
121 ret.push_back(ParamDesc("radius")
122 .set_local_name(_("Radius"))
126 ret.push_back(ParamDesc("feather")
127 .set_local_name(_("Feather"))
130 ret.push_back(ParamDesc("pos")
131 .set_local_name(_("Center"))
133 ret.push_back(ParamDesc("invert")
134 .set_local_name(_("Invert"))
135 .set_description(_("Invert the circle"))
138 ret.push_back(ParamDesc("falloff")
139 .set_local_name(_("Falloff"))
140 .set_description(_("Determines the falloff function for the feather"))
142 .add_enum_value(FALLOFF_INTERPOLATION_LINEAR,"linear",_("Linear"))
143 .add_enum_value(FALLOFF_SQUARED,"squared",_("Squared"))
144 .add_enum_value(FALLOFF_SQRT,"sqrt",_("Square Root"))
145 .add_enum_value(FALLOFF_SIGMOND,"sigmond",_("Sigmond"))
146 .add_enum_value(FALLOFF_COSINE,"cosine",_("Cosine"))
153 Circle::hit_check(sinfg::Context context, const sinfg::Point &point)const
155 Point temp=pos-point;
158 return context.hit_check(point);
160 bool in_circle(temp.mag_squared() <= radius*radius);
164 in_circle=!in_circle;
165 if(in_circle && get_amount()-(feather/radius)<=0.1 && get_blend_method()!=Color::BLEND_STRAIGHT)
170 if(get_amount()-(feather/radius)<=0.0)
176 sinfg::Layer::Handle tmp;
177 if(get_blend_method()==Color::BLEND_BEHIND && (tmp=context.hit_check(point)))
179 if(Color::is_onto(get_blend_method()) && !(tmp=context.hit_check(point)))
181 return const_cast<Circle*>(this);
184 return context.hit_check(point);
188 Real Circle::SqdFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
190 //squared proportional falloff
191 return (c.outer_radius_sqd - mag_sqd) / c.diff_sqd;
194 Real Circle::InvSqdFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
196 //squared proportional falloff
197 return 1.0 - (c.outer_radius_sqd - mag_sqd) / c.diff_sqd;
201 Real Circle::SqrtFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
203 //linear distance falloff
204 Real ret = ( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather;
205 //then take the square root of it
210 Real Circle::InvSqrtFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
212 //linear distance falloff
213 Real ret = ( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather;
214 //then take the square root of it
215 ret = 1.0 - sqrt(ret);
219 Real Circle::LinearFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
221 //linear distance falloff
222 return ( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather;
225 Real Circle::InvLinearFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
227 return 1.0 - ( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather;
228 //linear distance falloff
231 Real Circle::SigmondFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
233 //linear distance falloff
234 Real ret = ( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather;
235 // inverse exponential of the linear falloff (asymptotes at 0 and 1)
236 // \frac{1.0}{ 1 + e^{- \( a*10-5 \)}}
237 ret = 1.0 / (1 + exp(-(ret*10-5)) );
241 Real Circle::InvSigmondFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
243 //linear distance falloff
244 Real ret = ( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather;
245 // inverse exponential of the linear falloff (asymptotes at 0 and 1)
246 // \frac{1.0}{ 1 + e^{- \( a*10-5 \)}}
247 ret = 1.0 - 1.0 / (1 + exp(-(ret*10-5)) );
253 Circle::CosineFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
255 //Cosine distance falloff
256 return (1.0f-cos((( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather)*3.1415927))*0.5f;
260 Circle::InvCosineFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
262 return 1.0f-(1.0f-cos((( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather)*3.1415927))*0.5f;
263 //Cosine distance falloff
266 void Circle::constructcache()
268 cache.inner_radius = radius - feather;
269 if(cache.inner_radius < 0)
270 cache.inner_radius = 0;
272 cache.outer_radius = radius + feather;
274 cache.inner_radius_sqd = cache.inner_radius > 0 ? (radius-feather)*(radius-feather) : 0;
275 cache.outer_radius_sqd = (radius+feather)*(radius+feather);
277 cache.diff_sqd = feather*feather*4.0;
278 cache.double_feather = feather*2.0;
280 falloff_func = GetFalloffFunc();
283 Circle::FALLOFF_FUNC *Circle::GetFalloffFunc()const
287 case FALLOFF_SQUARED: return invert?InvSqdFalloff:SqdFalloff;
289 case FALLOFF_SQRT: return invert?InvSqrtFalloff:SqrtFalloff;
291 case FALLOFF_INTERPOLATION_LINEAR: return invert?InvLinearFalloff:LinearFalloff;
293 case FALLOFF_SIGMOND: return invert?InvSigmondFalloff:SigmondFalloff;
296 default: return invert?InvCosineFalloff:CosineFalloff;
301 Circle::get_color(Context context, const Point &point)const
303 if(radius==0 || is_disabled())
304 return context.get_color(point);
307 Point temp=pos-point;
309 /*const Real inner_radius = radius-feather;
310 const Real outer_radius = radius+feather;
312 const Real inner_radius_sqd = inner_radius > 0 ? (radius-feather)*(radius-feather) : 0;
313 const Real outer_radius_sqd = (radius+feather)*(radius+feather);
315 const Real diff_radii_sqd = outer_radius_sqd - inner_radius_sqd;
316 const Real double_feather = feather*2.0;*/
318 /*const Real &inner_radius = cache.inner_radius;
319 const Real &outer_radius = cache.outer_radius;*/
321 const Real &inner_radius_sqd = cache.inner_radius_sqd;
322 const Real &outer_radius_sqd = cache.outer_radius_sqd;
324 /*const Real &diff_radii_sqd = cache.diff_radii_sqd;
325 const Real &double_feather = cache.double_feather;*/
327 const Vector::value_type mag_squared = temp.mag_squared();
329 //Outside the circle, with feathering enabled
330 if( mag_squared > outer_radius_sqd )
332 // inverted -> outside == colored in
335 if(get_amount() == 1 && get_blend_method() == Color::BLEND_STRAIGHT)
338 return Color::blend(color,context.get_color(point),get_amount(),get_blend_method());
341 return context.get_color(point);
344 //inside the circle's solid area (with feathering)
345 else if(mag_squared <= inner_radius_sqd)
347 // !invert -> solid area
349 if(get_amount() == 1 && get_blend_method() == Color::BLEND_STRAIGHT)
352 return Color::blend(color,context.get_color(point),get_amount(),get_blend_method());
354 return context.get_color(point);
357 //If we get here, the pixel is within the feathering area, and is thus subject to falloff
360 Color::value_type alpha;
365 case FALLOFF_SQUARED:
366 //squared proportional falloff
367 alpha = (outer_radius_sqd - mag_squared) / diff_radii_sqd;
371 //linear distance falloff
372 alpha = ( outer_radius - sqrt(mag_squared) ) / double_feather;
373 //then take the square root of it
377 case FALLOFF_INTERPOLATION_LINEAR:
378 //linear distance falloff
379 alpha = ( outer_radius - sqrt(mag_squared) ) / double_feather;
382 case FALLOFF_SIGMOND:
384 //linear distance falloff
385 alpha = ( outer_radius - sqrt(mag_squared) ) / double_feather;
386 // inverse exponential of the linear falloff (asymptotes at 0 and 1)
387 // \frac{1.0}{ 1 + e^{- \( a*10-5 \)}}
388 alpha = 1.0 / (1 + exp(-(alpha*10-5)) );
392 //If we're inverted, we need to invert the falloff value
396 alpha = falloff_func(cache,mag_squared);
398 //Compose falloff value with amount from the composite layer, and that is the blend value
399 alpha *= get_amount();
401 return Color::blend(color,context.get_color(point),alpha,get_blend_method());
405 Color NormalBlend(Color a, Color b, float amount)
407 return (b-a)*amount+a;
412 Circle::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
415 if(is_disabled() || (radius==0 && invert==false))
416 return context.accelerated_render(surface,quality, renddesc, cb);
418 // Another trivial case
419 if(invert && radius==0 && is_solid_color())
421 surface->set_wh(renddesc.get_w(),renddesc.get_h());
422 surface->fill(color);
423 if(cb && !cb->amount_complete(10000,10000))
429 const Point tl(renddesc.get_tl());
430 const Point br(renddesc.get_br());
431 const int w(renddesc.get_w());
432 const int h(renddesc.get_h());
434 const Real x_neg = tl[0] > br[0] ? -1 : 1;
435 const Real y_neg = tl[1] > br[1] ? -1 : 1;
437 // Width and Height of a pixel
438 const Real pw = (br[0] - tl[0]) / w;
439 const Real ph = (br[1] - tl[1]) / h;
441 // Increasing the feather amount by the size of
442 // a pixel will create an anti-aliased appearance
443 const Real newfeather=feather + (abs(ph)+abs(pw))/4.0;
446 int left = (int) floor( (pos[0] - x_neg*(radius+newfeather) - tl[0]) / pw );
447 int right = (int) ceil( (pos[0] + x_neg*(radius+newfeather) - tl[0]) / pw );
448 int top = (int) floor( (pos[1] - y_neg*(radius+newfeather) - tl[1]) / ph );
449 int bottom = (int) ceil( (pos[1] + y_neg*(radius+newfeather) - tl[1]) / ph );
451 //clip the rectangle bounds
461 const Real inner_radius = radius-newfeather>0 ? radius-newfeather : 0;
462 const Real outer_radius = radius+newfeather;
464 const Real inner_radius_sqd = inner_radius*inner_radius;
465 const Real outer_radius_sqd = outer_radius*outer_radius;
467 const Real diff_radii_sqd = 4*newfeather*std::max(newfeather,radius);//4.0*radius*newfeather;
468 const Real double_feather = newfeather * 2.0;
470 //Compile the temporary cache for the falloff calculations
471 FALLOFF_FUNC *func = GetFalloffFunc();
473 const CircleDataCache cache =
475 inner_radius,outer_radius,
476 inner_radius_sqd,outer_radius_sqd,
477 diff_radii_sqd,double_feather
480 //info("Circle: Initialized everything");
482 //let the rendering begin
483 SuperCallback supercb(cb,0,9000,10000);
485 //if it's a degenerate circle, do what we need to do, and then leave
486 if(left >= right || top >= bottom)
490 if(get_amount() == 1 && get_blend_method() == Color::BLEND_STRAIGHT)
492 surface->set_wh(w,h);
493 surface->fill(color);
497 // Render what is behind us
498 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
500 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
504 Surface::alpha_pen p(surface->begin(),get_amount(),_BlendFunc(get_blend_method()));
512 // Render what is behind us
513 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
515 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
522 if( (pos[0] - tl[0])*(pos[0] - tl[0]) + (pos[1] - tl[1])*(pos[1] - tl[1]) < inner_radius_sqd
523 && (pos[0] - br[0])*(pos[0] - br[0]) + (pos[1] - br[1])*(pos[1] - br[1]) < inner_radius_sqd
524 && (pos[0] - tl[0])*(pos[0] - tl[0]) + (pos[1] - br[1])*(pos[1] - br[1]) < inner_radius_sqd
525 && (pos[0] - br[0])*(pos[0] - br[0]) + (pos[1] - tl[1])*(pos[1] - tl[1]) < inner_radius_sqd )
529 // Render what is behind us
530 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
532 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
537 if(get_amount() == 1 && get_blend_method() == Color::BLEND_STRAIGHT)
539 surface->set_wh(w,h);
540 surface->fill(color);
546 //info("Circle: Non degenerate, rasterize %c", invert);
548 //we start in the middle of the left-top pixel
549 Real leftf = (left + 0.5)*pw + tl[0];
550 Real topf = (top + 0.5)*ph + tl[1];
552 //the looping variables
556 //Loop normally, since we are not inverted
559 // Render what is behind us
560 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
562 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
566 //make topf and leftf relative to the center of the circle
573 //Loop over the valid y-values in the bounding square
574 for(;j <= bottom; j++, y += ph)
579 //for each y-value, Loop over the bounding x-values in the bounding square
580 for(;i <= right; i++, x += pw)
582 //for each pixel, figure out the distance and blend
585 //if in the inner circle then the full color shows through
586 if(r <= inner_radius_sqd)
588 if(get_amount() == 1 && get_blend_method() == Color::BLEND_STRAIGHT)
589 (*surface)[j][i]=color;
591 (*surface)[j][i]=Color::blend(color,(*surface)[j][i],get_amount(),get_blend_method());
593 //if it's within the outer circle then it's in the feathering range
594 else if(r <= outer_radius_sqd)
600 case FALLOFF_SQUARED:
601 myamount = (outer_radius_sqd - r) / diff_radii_sqd;
605 myamount = (outer_radius - sqrt(r)) / double_feather;
606 myamount = sqrt(myamount);
609 case FALLOFF_INTERPOLATION_LINEAR:
610 myamount = (outer_radius - sqrt(r)) / double_feather;
613 case FALLOFF_SIGMOND:
615 myamount = (outer_radius - sqrt(r)) / double_feather;
616 myamount = 1.0 / ( 1 + exp(-(myamount*10 - 5)) );
620 Real myamount = func(cache,r);
622 //if(myamount<0.0)myamount=0.0;
623 //if(myamount>1.0)myamount=1.0;
624 myamount *= get_amount();
625 (*surface)[j][i] = Color::blend(color,(*surface)[j][i],myamount,get_blend_method());
633 RendDesc desc(renddesc);
636 int offset_x=0,offset_y=0;
638 //fill the surface with the background color initially
639 surface->set_wh(w,h);
640 surface->fill(color);
642 //then render the background to an alternate surface
643 if(get_amount() == 1 && get_blend_method() == Color::BLEND_STRAIGHT)
648 //if there is no background showing through we are done
649 if(right < left || bottom < top)
652 desc.set_subwindow(left,top,right-left+1,bottom-top+1);
654 // Render what is behind us
655 if(!context.accelerated_render(&background,quality,desc,&supercb))
657 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
668 leftf = /*0.5*pw +*/ tl[0];
669 topf = /*0.5*ph +*/ tl[1];
671 // Render what is behind us
672 if(!context.accelerated_render(&background,quality,renddesc,&supercb))
674 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
685 for(;j <= bottom; j++, y+=ph)
690 for(;i <= right; i++, x+=pw)
692 Vector::value_type r = x*x + y*y;
694 if(r < inner_radius_sqd)
696 (*surface)[j][i] = background[j-offset_y][i-offset_x];
698 else if(r < outer_radius_sqd)
704 case FALLOFF_SQUARED:
705 amount = (r - inner_radius_sqd) / diff_radii_sqd;
707 case FALLOFF_INTERPOLATION_LINEAR:
708 amount = (sqrt(r) - inner_radius) / double_feather;
711 amount = (outer_radius - sqrt(r)) / double_feather;
712 amount = 1.0 - sqrt(amount);
714 case FALLOFF_SIGMOND:
716 amount = (outer_radius - sqrt(r)) / double_feather;
717 amount = 1.0 - ( 1.0/( 1 + exp(-(amount*10-5)) ) );
721 Real amount = func(cache,r);
723 if(amount<0.0)amount=0.0;
724 if(amount>1.0)amount=1.0;
726 amount*=get_amount();
728 (*surface)[j][i]=Color::blend(color,background[j-offset_y][i-offset_x],amount,get_blend_method());
729 }else if(get_amount() != 1 || get_blend_method() != Color::BLEND_STRAIGHT)
731 (*surface)[j][i]=Color::blend(color,background[j][i],get_amount(),get_blend_method());
737 // Mark our progress as finished
738 if(cb && !cb->amount_complete(10000,10000))
745 Circle::get_bounding_rect()const
748 return Rect::full_plane();
751 pos[0]+(radius+feather),
752 pos[1]+(radius+feather),
753 pos[0]-(radius+feather),
754 pos[1]-(radius+feather)
761 Circle::get_full_bounding_rect(Context context)const
765 if(is_solid_color() && color.get_a()==0)
768 pos[0]+(radius+feather),
769 pos[1]+(radius+feather),
770 pos[0]-(radius+feather),
771 pos[1]-(radius+feather)
773 return bounds & context.get_full_bounding_rect();
775 return Rect::full_plane();
778 return Layer_Composite::get_full_bounding_rect(context);