moreupdates
[synfig.git] / synfig-core / trunk / src / modules / mod_geometry / circle.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file circle.cpp
3 **      \brief Template Header
4 **
5 **      $Id: circle.cpp,v 1.2 2005/01/24 03:08:17 darco Exp $
6 **
7 **      \legal
8 **      Copyright (c) 2002 Robert B. Quattlebaum Jr.
9 **
10 **      This software and associated documentation
11 **      are CONFIDENTIAL and PROPRIETARY property of
12 **      the above-mentioned copyright holder.
13 **
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.
18 **      \endlegal
19 */
20 /* ========================================================================= */
21
22 /* === H E A D E R S ======================================================= */
23
24 #ifdef USING_PCH
25 #       include "pch.h"
26 #else
27 #ifdef HAVE_CONFIG_H
28 #       include <config.h>
29 #endif
30
31 #include "circle.h"
32 #include <synfig/string.h>
33 #include <synfig/time.h>
34 #include <synfig/context.h>
35 #include <synfig/paramdesc.h>
36 #include <synfig/renddesc.h>
37 #include <synfig/surface.h>
38 #include <synfig/value.h>
39 #include <synfig/valuenode.h>
40
41 #include <cmath>
42
43 #endif
44
45 using namespace synfig;
46 using namespace std;
47 using namespace etl;
48
49 /* -- G L O B A L S --------------------------------------------------------- */
50
51 SYNFIG_LAYER_INIT(Circle);
52 SYNFIG_LAYER_SET_NAME(Circle,"circle");
53 SYNFIG_LAYER_SET_LOCAL_NAME(Circle,_("Circle"));
54 SYNFIG_LAYER_SET_CATEGORY(Circle,_("Geometry"));
55 SYNFIG_LAYER_SET_VERSION(Circle,"0.1");
56 SYNFIG_LAYER_SET_CVS_ID(Circle,"$Id: circle.cpp,v 1.2 2005/01/24 03:08:17 darco Exp $");
57
58 /* -- F U N C T I O N S ----------------------------------------------------- */
59
60 Circle::Circle():
61         Layer_Composite (1.0,Color::BLEND_STRAIGHT),
62         color                   (Color::black()),
63         pos                             (0,0),
64         radius                  (1),
65         feather                 (0),
66         invert                  (false),
67         falloff                 (FALLOFF_INTERPOLATION_LINEAR)
68 {
69         constructcache();
70 }
71         
72 bool
73 Circle::ImportParameters(const String &param, const ValueBase &value)
74 {
75         IMPORT(color);
76         IMPORT(radius);
77         IMPORT(feather);
78         IMPORT(invert);
79         IMPORT(pos);
80         IMPORT(falloff);
81                 
82         return Layer_Composite::set_param(param,value);
83 }
84
85 bool
86 Circle::set_param(const String &param, const ValueBase &value)
87 {
88         if(ImportParameters(param,value))
89         {
90                 constructcache();
91                 return true;
92         }
93         
94         return false;
95 }
96
97 ValueBase
98 Circle::get_param(const String &param)const
99 {
100         EXPORT(color);
101         EXPORT(radius);
102         EXPORT(feather);
103         EXPORT(invert);
104         EXPORT(pos);
105         EXPORT(falloff);
106
107         EXPORT_NAME();
108         EXPORT_VERSION();
109                 
110         return Layer_Composite::get_param(param);
111 }
112
113 Layer::Vocab
114 Circle::get_param_vocab()const
115 {
116         Layer::Vocab ret(Layer_Composite::get_param_vocab());
117         
118         ret.push_back(ParamDesc("color")
119                 .set_local_name(_("Color"))
120         );
121         ret.push_back(ParamDesc("radius")
122                 .set_local_name(_("Radius"))
123                 .set_origin("pos")
124                 .set_is_distance()
125         );
126         ret.push_back(ParamDesc("feather")
127                 .set_local_name(_("Feather"))
128                 .set_is_distance()
129         );
130         ret.push_back(ParamDesc("pos")
131                 .set_local_name(_("Center"))
132         );      
133         ret.push_back(ParamDesc("invert")
134                 .set_local_name(_("Invert"))
135                 .set_description(_("Invert the circle"))
136         );
137
138         ret.push_back(ParamDesc("falloff")
139                 .set_local_name(_("Falloff"))
140                 .set_description(_("Determines the falloff function for the feather"))
141                 .set_hint("enum")
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"))
147         );
148         
149         return ret;
150 }
151
152 synfig::Layer::Handle
153 Circle::hit_check(synfig::Context context, const synfig::Point &point)const
154 {
155         Point temp=pos-point;
156
157         if(get_amount()==0)
158                 return context.hit_check(point);
159                 
160         bool in_circle(temp.mag_squared() <= radius*radius);
161
162         if(invert)
163         {
164                 in_circle=!in_circle;
165                 if(in_circle && get_amount()-(feather/radius)<=0.1 && get_blend_method()!=Color::BLEND_STRAIGHT)
166                         in_circle=false;
167         }
168         else
169         {
170                 if(get_amount()-(feather/radius)<=0.0)
171                         in_circle=false;
172         }
173                         
174         if(in_circle)
175         {
176                 synfig::Layer::Handle tmp;
177                 if(get_blend_method()==Color::BLEND_BEHIND && (tmp=context.hit_check(point)))
178                         return tmp;
179                 if(Color::is_onto(get_blend_method()) && !(tmp=context.hit_check(point)))
180                         return 0;
181                 return const_cast<Circle*>(this);
182         }
183
184         return context.hit_check(point);
185 }
186
187 //falloff functions             
188 Real    Circle::SqdFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
189 {
190         //squared proportional falloff
191         return (c.outer_radius_sqd - mag_sqd) / c.diff_sqd;
192 }
193
194 Real    Circle::InvSqdFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
195 {
196         //squared proportional falloff
197         return 1.0 - (c.outer_radius_sqd - mag_sqd) / c.diff_sqd;
198 }
199
200                         
201 Real    Circle::SqrtFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
202 {
203         //linear distance falloff
204         Real ret = ( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather;
205         //then take the square root of it
206         ret = sqrt(ret);
207         return ret;
208 }
209
210 Real    Circle::InvSqrtFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
211 {
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);
216         return ret;
217 }
218                 
219 Real    Circle::LinearFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
220 {
221         //linear distance falloff
222         return ( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather;
223 }
224
225 Real    Circle::InvLinearFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
226 {
227         return 1.0 - ( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather;
228         //linear distance falloff
229 }
230                 
231 Real    Circle::SigmondFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
232 {
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)) );
238         return ret;
239 }
240
241 Real    Circle::InvSigmondFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
242 {
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)) );
248         return ret;
249 }
250
251
252 Real
253 Circle::CosineFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
254 {
255         //Cosine distance falloff
256         return (1.0f-cos((( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather)*3.1415927))*0.5f;
257 }
258
259 Real
260 Circle::InvCosineFalloff(const Circle::CircleDataCache &c, const Real &mag_sqd)
261 {
262         return 1.0f-(1.0f-cos((( c.outer_radius - sqrt(mag_sqd) ) / c.double_feather)*3.1415927))*0.5f;
263         //Cosine distance falloff
264 }
265
266 void Circle::constructcache()
267 {
268         cache.inner_radius = radius - feather;
269         if(cache.inner_radius < 0)
270                 cache.inner_radius = 0;
271         
272         cache.outer_radius = radius + feather;
273         
274         cache.inner_radius_sqd = cache.inner_radius > 0 ? (radius-feather)*(radius-feather) : 0;
275         cache.outer_radius_sqd = (radius+feather)*(radius+feather);
276         
277         cache.diff_sqd = feather*feather*4.0;
278         cache.double_feather = feather*2.0;
279         
280         falloff_func = GetFalloffFunc();
281 }
282
283 Circle::FALLOFF_FUNC *Circle::GetFalloffFunc()const
284 {
285         switch(falloff)
286         {       
287         case FALLOFF_SQUARED:   return invert?InvSqdFalloff:SqdFalloff;
288         
289         case FALLOFF_SQRT:              return invert?InvSqrtFalloff:SqrtFalloff;
290         
291         case FALLOFF_INTERPOLATION_LINEAR:      return invert?InvLinearFalloff:LinearFalloff;
292         
293         case FALLOFF_SIGMOND:   return invert?InvSigmondFalloff:SigmondFalloff;
294
295         case FALLOFF_COSINE:
296         default:                                return invert?InvCosineFalloff:CosineFalloff;
297         }
298 }
299
300 Color
301 Circle::get_color(Context context, const Point &point)const
302 {
303         if(radius==0 || is_disabled())
304                 return context.get_color(point);
305
306                 
307         Point temp=pos-point;
308                 
309         /*const Real inner_radius = radius-feather;
310         const Real outer_radius = radius+feather;
311         
312         const Real inner_radius_sqd = inner_radius > 0 ? (radius-feather)*(radius-feather) : 0;
313         const Real outer_radius_sqd = (radius+feather)*(radius+feather);
314         
315         const Real diff_radii_sqd = outer_radius_sqd - inner_radius_sqd;
316         const Real double_feather = feather*2.0;*/
317         
318         /*const Real &inner_radius = cache.inner_radius;
319         const Real &outer_radius = cache.outer_radius;*/
320         
321         const Real &inner_radius_sqd = cache.inner_radius_sqd;
322         const Real &outer_radius_sqd = cache.outer_radius_sqd;
323         
324         /*const Real &diff_radii_sqd = cache.diff_radii_sqd;
325         const Real &double_feather = cache.double_feather;*/
326         
327         const Vector::value_type mag_squared = temp.mag_squared();
328         
329         //Outside the circle, with feathering enabled
330         if( mag_squared > outer_radius_sqd )
331         {
332                 // inverted -> outside == colored in
333                 if(invert)
334                 {       
335                         if(get_amount() == 1 && get_blend_method() == Color::BLEND_STRAIGHT)
336                                 return color;
337                         else
338                                 return Color::blend(color,context.get_color(point),get_amount(),get_blend_method());
339                 }
340                 else
341                         return context.get_color(point);
342         }
343         
344         //inside the circle's solid area (with feathering)
345         else if(mag_squared <= inner_radius_sqd)
346         {
347                 // !invert -> solid area
348                 if(!invert)
349                         if(get_amount() == 1 && get_blend_method() == Color::BLEND_STRAIGHT)
350                                 return color;
351                         else
352                                 return Color::blend(color,context.get_color(point),get_amount(),get_blend_method());
353                 else
354                         return context.get_color(point);
355         }
356         
357         //If we get here, the pixel is within the feathering area, and is thus subject to falloff
358         else
359         {
360                 Color::value_type alpha;
361         
362                 /*switch(falloff)
363                 {
364                 
365                 case FALLOFF_SQUARED:
366                         //squared proportional falloff
367                         alpha = (outer_radius_sqd - mag_squared) / diff_radii_sqd;
368                         break;
369                 
370                 case FALLOFF_SQRT:
371                         //linear distance falloff
372                         alpha = ( outer_radius - sqrt(mag_squared) ) / double_feather;
373                         //then take the square root of it
374                         alpha = sqrt(alpha);
375                         break;
376                 
377                 case FALLOFF_INTERPOLATION_LINEAR:
378                         //linear distance falloff
379                         alpha = ( outer_radius - sqrt(mag_squared) ) / double_feather;
380                         break;
381                 
382                 case FALLOFF_SIGMOND:
383                 default:
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)) );
389                         break;
390                 }
391                 
392                 //If we're inverted, we need to invert the falloff value
393                 if(invert)
394                         alpha=1.0-alpha;*/
395                         
396                 alpha = falloff_func(cache,mag_squared);
397                 
398                 //Compose falloff value with amount from the composite layer, and that is the blend value
399                 alpha *= get_amount();
400                 
401                 return Color::blend(color,context.get_color(point),alpha,get_blend_method());
402         }
403 }
404
405 Color NormalBlend(Color a, Color b, float amount)
406 {
407         return (b-a)*amount+a;
408 }
409
410
411 bool
412 Circle::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
413 {       
414         // trivial case
415         if(is_disabled() || (radius==0 && invert==false))
416                 return context.accelerated_render(surface,quality, renddesc, cb);
417
418         // Another trivial case
419         if(invert && radius==0 && is_solid_color())
420         {
421                 surface->set_wh(renddesc.get_w(),renddesc.get_h());
422                 surface->fill(color);
423                 if(cb && !cb->amount_complete(10000,10000))
424                         return false;
425                 return true;
426         }
427
428         // Window Boundaries
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());
433         
434         const Real x_neg = tl[0] > br[0] ? -1 : 1;
435         const Real y_neg = tl[1] > br[1] ? -1 : 1;
436                         
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;
440         
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;
444                 
445         //int u,v;
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 );
450                 
451         //clip the rectangle bounds
452         if(left < 0)
453                 left = 0;
454         if(top < 0)
455                 top = 0;
456         if(right >= w)
457                 right = w-1;
458         if(bottom >= h)
459                 bottom = h-1;
460         
461         const Real inner_radius = radius-newfeather>0 ? radius-newfeather : 0;
462         const Real outer_radius = radius+newfeather;
463         
464         const Real inner_radius_sqd = inner_radius*inner_radius;
465         const Real outer_radius_sqd = outer_radius*outer_radius;
466         
467         const Real diff_radii_sqd = 4*newfeather*std::max(newfeather,radius);//4.0*radius*newfeather;
468         const Real double_feather = newfeather * 2.0;
469         
470         //Compile the temporary cache for the falloff calculations
471         FALLOFF_FUNC *func = GetFalloffFunc();
472         
473         const CircleDataCache cache = 
474         {
475                 inner_radius,outer_radius,
476                 inner_radius_sqd,outer_radius_sqd,
477                 diff_radii_sqd,double_feather
478         };      
479         
480         //info("Circle: Initialized everything");
481         
482         //let the rendering begin
483         SuperCallback supercb(cb,0,9000,10000);
484         
485         //if it's a degenerate circle, do what we need to do, and then leave
486         if(left >= right || top >= bottom)
487         {
488                 if(invert)
489                 {                       
490                         if(get_amount() == 1 && get_blend_method() == Color::BLEND_STRAIGHT)
491                         {
492                                 surface->set_wh(w,h);
493                                 surface->fill(color);
494                                 return true;
495                         }else
496                         {
497                                 // Render what is behind us
498                                 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
499                                 {
500                                         if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
501                                         return false;
502                                 }                       
503                                 
504                                 Surface::alpha_pen p(surface->begin(),get_amount(),_BlendFunc(get_blend_method()));
505                                 
506                                 p.set_value(color);
507                                 p.put_block(h,w);
508                                 return true;
509                         }
510                 }else
511                 {
512                         // Render what is behind us
513                         if(!context.accelerated_render(surface,quality,renddesc,&supercb))
514                         {
515                                 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
516                                 return false;
517                         }
518                         return true;                    
519                 }
520         }
521         
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     )
526         {
527                 if(invert)
528                 {
529                         // Render what is behind us
530                         if(!context.accelerated_render(surface,quality,renddesc,&supercb))
531                         {
532                                 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
533                                 return false;
534                         }
535                 }else
536                 {
537                         if(get_amount() == 1 && get_blend_method() == Color::BLEND_STRAIGHT)
538                         {
539                                 surface->set_wh(w,h);
540                                 surface->fill(color);
541                                 return true;
542                         }                                               
543                 }               
544         }
545         
546         //info("Circle: Non degenerate, rasterize %c", invert); 
547
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];
551         
552         //the looping variables
553         Real            x,y;
554         int                     i,j;
555
556         //Loop normally, since we are not inverted
557         if(!invert)
558         {
559                 // Render what is behind us
560                 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
561                 {
562                         if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
563                         return false;
564                 }
565                 
566                 //make topf and leftf relative to the center of the circle
567                 leftf   -=      pos[0];
568                 topf    -=      pos[1];
569                 
570                 j = top;
571                 y = topf;
572                 
573                 //Loop over the valid y-values in the bounding square
574                 for(;j <= bottom; j++, y += ph)
575                 {
576                         i = left;
577                         x = leftf;
578                         
579                         //for each y-value, Loop over the bounding x-values in the bounding square
580                         for(;i <= right; i++, x += pw)
581                         {
582                                 //for each pixel, figure out the distance and blend
583                                 Real    r = x*x + y*y;
584                                 
585                                 //if in the inner circle then the full color shows through
586                                 if(r <= inner_radius_sqd)
587                                 {
588                                         if(get_amount() == 1 && get_blend_method() == Color::BLEND_STRAIGHT)
589                                                 (*surface)[j][i]=color;
590                                         else
591                                                 (*surface)[j][i]=Color::blend(color,(*surface)[j][i],get_amount(),get_blend_method());
592                                 }
593                                 //if it's within the outer circle then it's in the feathering range
594                                 else if(r <= outer_radius_sqd)
595                                 {
596                                         /*float myamount;
597
598                                         switch(falloff)
599                                         {
600                                         case FALLOFF_SQUARED:
601                                                 myamount = (outer_radius_sqd - r) / diff_radii_sqd;
602                                                 break;
603                                         
604                                         case FALLOFF_SQRT:
605                                                 myamount = (outer_radius - sqrt(r)) / double_feather;
606                                                 myamount = sqrt(myamount);
607                                                 break;
608                                         
609                                         case FALLOFF_INTERPOLATION_LINEAR:
610                                                 myamount = (outer_radius - sqrt(r)) / double_feather;
611                                                 break;
612                                         
613                                         case FALLOFF_SIGMOND:
614                                         default:
615                                                 myamount = (outer_radius - sqrt(r)) / double_feather;
616                                                 myamount = 1.0 / ( 1 + exp(-(myamount*10 - 5)) );
617                                                 break;
618                                         }*/
619                                         
620                                         Real    myamount = func(cache,r);
621
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());
626                                 }                                                               
627                         }
628                 }
629         }
630         else
631         {
632                 Surface background;
633                 RendDesc desc(renddesc);
634                 desc.set_flags(0);
635                 
636                 int offset_x=0,offset_y=0;
637                 
638                 //fill the surface with the background color initially
639                 surface->set_wh(w,h);
640                 surface->fill(color);
641                                 
642                 //then render the background to an alternate surface
643                 if(get_amount() == 1 && get_blend_method() == Color::BLEND_STRAIGHT)
644                 {
645                         offset_x = left;
646                         offset_y = top;
647                         
648                         //if there is no background showing through we are done
649                         if(right < left || bottom < top)
650                                 return true;
651                         
652                         desc.set_subwindow(left,top,right-left+1,bottom-top+1);
653                                                                         
654                         // Render what is behind us
655                         if(!context.accelerated_render(&background,quality,desc,&supercb))
656                         {
657                                 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
658                                 return false;
659                         }
660                 }
661                 else
662                 {
663                         left = 0;
664                         right = w-1;
665                         top = 0;
666                         bottom = h-1;
667                         
668                         leftf = /*0.5*pw +*/ tl[0];
669                         topf = /*0.5*ph +*/ tl[1];
670                         
671                         // Render what is behind us
672                         if(!context.accelerated_render(&background,quality,renddesc,&supercb))
673                         {
674                                 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
675                                 return false;
676                         }
677                 }
678                 
679                 topf -= pos[1];
680                 leftf-= pos[0];
681                 
682                 j = top;
683                 y = topf;
684                 
685                 for(;j <= bottom; j++, y+=ph)
686                 {
687                         i = left;
688                         x = leftf;
689                         
690                         for(;i <= right; i++, x+=pw)
691                         {                               
692                                 Vector::value_type r = x*x + y*y;
693         
694                                 if(r < inner_radius_sqd)
695                                 {
696                                         (*surface)[j][i] = background[j-offset_y][i-offset_x];
697                                 }
698                                 else if(r < outer_radius_sqd)
699                                 {
700                                         /*float amount;
701
702                                         switch(falloff)
703                                         {
704                                         case FALLOFF_SQUARED:
705                                                 amount = (r - inner_radius_sqd) / diff_radii_sqd;
706                                                 break;
707                                         case FALLOFF_INTERPOLATION_LINEAR:
708                                                 amount = (sqrt(r) - inner_radius) / double_feather;
709                                                 break;
710                                         case FALLOFF_SQRT:
711                                                 amount = (outer_radius - sqrt(r)) / double_feather;
712                                                 amount = 1.0 - sqrt(amount);
713                                                 break;
714                                         case FALLOFF_SIGMOND:
715                                         default:
716                                                 amount = (outer_radius - sqrt(r)) / double_feather;
717                                                 amount = 1.0 - ( 1.0/( 1 + exp(-(amount*10-5)) ) );
718                                                 break;
719                                         }*/
720                                         
721                                         Real amount = func(cache,r);
722                                         
723                                         if(amount<0.0)amount=0.0;
724                                         if(amount>1.0)amount=1.0;
725                                                 
726                                         amount*=get_amount();
727                                         
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)
730                                 {
731                                         (*surface)[j][i]=Color::blend(color,background[j][i],get_amount(),get_blend_method());                                  
732                                 }                               
733                         }
734                 }
735     }
736
737         // Mark our progress as finished
738         if(cb && !cb->amount_complete(10000,10000))
739                 return false;
740
741         return true;
742 }
743
744 Rect
745 Circle::get_bounding_rect()const
746 {
747         if(invert)
748                 return Rect::full_plane();
749
750         Rect bounds(
751                 pos[0]+(radius+feather),
752                 pos[1]+(radius+feather),
753                 pos[0]-(radius+feather),
754                 pos[1]-(radius+feather)
755         );
756
757         return bounds;
758 }
759
760 Rect
761 Circle::get_full_bounding_rect(Context context)const
762 {
763         if(invert)
764         {
765                 if(is_solid_color() && color.get_a()==0)
766                 {
767                         Rect bounds(
768                                 pos[0]+(radius+feather),
769                                 pos[1]+(radius+feather),
770                                 pos[0]-(radius+feather),
771                                 pos[1]-(radius+feather)
772                         );
773                         return bounds & context.get_full_bounding_rect();
774                 }
775                 return Rect::full_plane();                      
776         }
777
778         return Layer_Composite::get_full_bounding_rect(context);
779 }