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