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