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