Don't render feathering of circles when quality is 10.
[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 **
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,N_("Circle"));
55 SYNFIG_LAYER_SET_CATEGORY(Circle,N_("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         // don't render feathering at all when quality is 10
445         const Real newfeather = (quality == 10) ? 0 : feather + (abs(ph)+abs(pw))/4.0;
446
447         //int u,v;
448         int left =      (int)   floor( (pos[0] - x_neg*(radius+newfeather) - tl[0]) / pw );
449         int right = (int)       ceil( (pos[0] + x_neg*(radius+newfeather) - tl[0]) / pw );
450         int top =       (int)   floor( (pos[1] - y_neg*(radius+newfeather) - tl[1]) / ph );
451         int bottom = (int)      ceil( (pos[1] + y_neg*(radius+newfeather) - tl[1]) / ph );
452
453         //clip the rectangle bounds
454         if(left < 0)
455                 left = 0;
456         if(top < 0)
457                 top = 0;
458         if(right >= w)
459                 right = w-1;
460         if(bottom >= h)
461                 bottom = h-1;
462
463         const Real inner_radius = radius-newfeather>0 ? radius-newfeather : 0;
464         const Real outer_radius = radius+newfeather;
465
466         const Real inner_radius_sqd = inner_radius*inner_radius;
467         const Real outer_radius_sqd = outer_radius*outer_radius;
468
469         const Real diff_radii_sqd = 4*newfeather*std::max(newfeather,radius);//4.0*radius*newfeather;
470         const Real double_feather = newfeather * 2.0;
471
472         //Compile the temporary cache for the falloff calculations
473         FALLOFF_FUNC *func = GetFalloffFunc();
474
475         const CircleDataCache cache =
476         {
477                 inner_radius,outer_radius,
478                 inner_radius_sqd,outer_radius_sqd,
479                 diff_radii_sqd,double_feather
480         };
481
482         //info("Circle: Initialized everything");
483
484         //let the rendering begin
485         SuperCallback supercb(cb,0,9000,10000);
486
487         //if it's a degenerate circle, do what we need to do, and then leave
488         if(left >= right || top >= bottom)
489         {
490                 if(invert)
491                 {
492                         if(get_amount() == 1 && get_blend_method() == Color::BLEND_STRAIGHT)
493                         {
494                                 surface->set_wh(w,h);
495                                 surface->fill(color);
496                                 return true;
497                         }else
498                         {
499                                 // Render what is behind us
500                                 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
501                                 {
502                                         if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
503                                         return false;
504                                 }
505
506                                 Surface::alpha_pen p(surface->begin(),get_amount(),_BlendFunc(get_blend_method()));
507
508                                 p.set_value(color);
509                                 p.put_block(h,w);
510                                 return true;
511                         }
512                 }else
513                 {
514                         // Render what is behind us
515                         if(!context.accelerated_render(surface,quality,renddesc,&supercb))
516                         {
517                                 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
518                                 return false;
519                         }
520                         return true;
521                 }
522         }
523
524         if( (pos[0] - tl[0])*(pos[0] - tl[0]) + (pos[1] - tl[1])*(pos[1] - tl[1]) < inner_radius_sqd
525                 && (pos[0] - br[0])*(pos[0] - br[0]) + (pos[1] - br[1])*(pos[1] - br[1]) < inner_radius_sqd
526                 && (pos[0] - tl[0])*(pos[0] - tl[0]) + (pos[1] - br[1])*(pos[1] - br[1]) < inner_radius_sqd
527                 && (pos[0] - br[0])*(pos[0] - br[0]) + (pos[1] - tl[1])*(pos[1] - tl[1]) < inner_radius_sqd     )
528         {
529                 if(invert)
530                 {
531                         // Render what is behind us
532                         if(!context.accelerated_render(surface,quality,renddesc,&supercb))
533                         {
534                                 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
535                                 return false;
536                         }
537                 }else
538                 {
539                         if(get_amount() == 1 && get_blend_method() == Color::BLEND_STRAIGHT)
540                         {
541                                 surface->set_wh(w,h);
542                                 surface->fill(color);
543                                 return true;
544                         }
545                 }
546         }
547
548         //info("Circle: Non degenerate, rasterize %c", invert);
549
550         //we start in the middle of the left-top pixel
551         Real leftf      = (left + 0.5)*pw + tl[0];
552         Real topf       = (top + 0.5)*ph + tl[1];
553
554         //the looping variables
555         Real            x,y;
556         int                     i,j;
557
558         //Loop normally, since we are not inverted
559         if(!invert)
560         {
561                 // Render what is behind us
562                 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
563                 {
564                         if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
565                         return false;
566                 }
567
568                 //make topf and leftf relative to the center of the circle
569                 leftf   -=      pos[0];
570                 topf    -=      pos[1];
571
572                 j = top;
573                 y = topf;
574
575                 //Loop over the valid y-values in the bounding square
576                 for(;j <= bottom; j++, y += ph)
577                 {
578                         i = left;
579                         x = leftf;
580
581                         //for each y-value, Loop over the bounding x-values in the bounding square
582                         for(;i <= right; i++, x += pw)
583                         {
584                                 //for each pixel, figure out the distance and blend
585                                 Real    r = x*x + y*y;
586
587                                 //if in the inner circle then the full color shows through
588                                 if(r <= inner_radius_sqd)
589                                 {
590                                         if(get_amount() == 1 && get_blend_method() == Color::BLEND_STRAIGHT)
591                                                 (*surface)[j][i]=color;
592                                         else
593                                                 (*surface)[j][i]=Color::blend(color,(*surface)[j][i],get_amount(),get_blend_method());
594                                 }
595                                 //if it's within the outer circle then it's in the feathering range
596                                 else if(r <= outer_radius_sqd)
597                                 {
598                                         /*float myamount;
599
600                                         switch(falloff)
601                                         {
602                                         case FALLOFF_SQUARED:
603                                                 myamount = (outer_radius_sqd - r) / diff_radii_sqd;
604                                                 break;
605
606                                         case FALLOFF_SQRT:
607                                                 myamount = (outer_radius - sqrt(r)) / double_feather;
608                                                 myamount = sqrt(myamount);
609                                                 break;
610
611                                         case FALLOFF_INTERPOLATION_LINEAR:
612                                                 myamount = (outer_radius - sqrt(r)) / double_feather;
613                                                 break;
614
615                                         case FALLOFF_SIGMOND:
616                                         default:
617                                                 myamount = (outer_radius - sqrt(r)) / double_feather;
618                                                 myamount = 1.0 / ( 1 + exp(-(myamount*10 - 5)) );
619                                                 break;
620                                         }*/
621
622                                         Real    myamount = func(cache,r);
623
624                                         //if(myamount<0.0)myamount=0.0;
625                                         //if(myamount>1.0)myamount=1.0;
626                                         myamount *= get_amount();
627                                         (*surface)[j][i] = Color::blend(color,(*surface)[j][i],myamount,get_blend_method());
628                                 }
629                         }
630                 }
631         }
632         else
633         {
634                 Surface background;
635                 RendDesc desc(renddesc);
636                 desc.set_flags(0);
637
638                 int offset_x=0,offset_y=0;
639
640                 //fill the surface with the background color initially
641                 surface->set_wh(w,h);
642                 surface->fill(color);
643
644                 //then render the background to an alternate surface
645                 if(get_amount() == 1 && get_blend_method() == Color::BLEND_STRAIGHT)
646                 {
647                         offset_x = left;
648                         offset_y = top;
649
650                         //if there is no background showing through we are done
651                         if(right < left || bottom < top)
652                                 return true;
653
654                         desc.set_subwindow(left,top,right-left+1,bottom-top+1);
655
656                         // Render what is behind us
657                         if(!context.accelerated_render(&background,quality,desc,&supercb))
658                         {
659                                 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
660                                 return false;
661                         }
662                 }
663                 else
664                 {
665                         left = 0;
666                         right = w-1;
667                         top = 0;
668                         bottom = h-1;
669
670                         leftf = /*0.5*pw +*/ tl[0];
671                         topf = /*0.5*ph +*/ tl[1];
672
673                         // Render what is behind us
674                         if(!context.accelerated_render(&background,quality,renddesc,&supercb))
675                         {
676                                 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
677                                 return false;
678                         }
679                 }
680
681                 topf -= pos[1];
682                 leftf-= pos[0];
683
684                 j = top;
685                 y = topf;
686
687                 for(;j <= bottom; j++, y+=ph)
688                 {
689                         i = left;
690                         x = leftf;
691
692                         for(;i <= right; i++, x+=pw)
693                         {
694                                 Vector::value_type r = x*x + y*y;
695
696                                 if(r < inner_radius_sqd)
697                                 {
698                                         (*surface)[j][i] = background[j-offset_y][i-offset_x];
699                                 }
700                                 else if(r < outer_radius_sqd)
701                                 {
702                                         /*float amount;
703
704                                         switch(falloff)
705                                         {
706                                         case FALLOFF_SQUARED:
707                                                 amount = (r - inner_radius_sqd) / diff_radii_sqd;
708                                                 break;
709                                         case FALLOFF_INTERPOLATION_LINEAR:
710                                                 amount = (sqrt(r) - inner_radius) / double_feather;
711                                                 break;
712                                         case FALLOFF_SQRT:
713                                                 amount = (outer_radius - sqrt(r)) / double_feather;
714                                                 amount = 1.0 - sqrt(amount);
715                                                 break;
716                                         case FALLOFF_SIGMOND:
717                                         default:
718                                                 amount = (outer_radius - sqrt(r)) / double_feather;
719                                                 amount = 1.0 - ( 1.0/( 1 + exp(-(amount*10-5)) ) );
720                                                 break;
721                                         }*/
722
723                                         Real amount = func(cache,r);
724
725                                         if(amount<0.0)amount=0.0;
726                                         if(amount>1.0)amount=1.0;
727
728                                         amount*=get_amount();
729
730                                         (*surface)[j][i]=Color::blend(color,background[j-offset_y][i-offset_x],amount,get_blend_method());
731                                 }else if(get_amount() != 1 || get_blend_method() != Color::BLEND_STRAIGHT)
732                                 {
733                                         (*surface)[j][i]=Color::blend(color,background[j][i],get_amount(),get_blend_method());
734                                 }
735                         }
736                 }
737     }
738
739         // Mark our progress as finished
740         if(cb && !cb->amount_complete(10000,10000))
741                 return false;
742
743         return true;
744 }
745
746 Rect
747 Circle::get_bounding_rect()const
748 {
749         if(invert)
750                 return Rect::full_plane();
751
752         Rect bounds(
753                 pos[0]+(radius+feather),
754                 pos[1]+(radius+feather),
755                 pos[0]-(radius+feather),
756                 pos[1]-(radius+feather)
757         );
758
759         return bounds;
760 }
761
762 Rect
763 Circle::get_full_bounding_rect(Context context)const
764 {
765         if(invert)
766         {
767                 if(is_solid_color() && color.get_a()==0)
768                 {
769                         Rect bounds(
770                                 pos[0]+(radius+feather),
771                                 pos[1]+(radius+feather),
772                                 pos[0]-(radius+feather),
773                                 pos[1]-(radius+feather)
774                         );
775                         return bounds & context.get_full_bounding_rect();
776                 }
777                 return Rect::full_plane();
778         }
779
780         return Layer_Composite::get_full_bounding_rect(context);
781 }