Release 0.61.08
[synfig.git] / synfig-core / tags / stable / src / modules / mod_particle / plant.cpp
index 6dbd106..bd628a7 100644 (file)
@@ -6,6 +6,7 @@
 **
 **     \legal
 **     Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
+**     Copyright (c) 2007 Chris Moore
 **
 **     This package is free software; you can redistribute it and/or
 **     modify it under the terms of the GNU General Public License as
@@ -65,7 +66,7 @@ using namespace etl;
 SYNFIG_LAYER_INIT(Plant);
 SYNFIG_LAYER_SET_NAME(Plant,"plant");
 SYNFIG_LAYER_SET_LOCAL_NAME(Plant,_("Plant"));
-SYNFIG_LAYER_SET_CATEGORY(Plant,_("Particle Systems"));
+SYNFIG_LAYER_SET_CATEGORY(Plant,_("Other"));
 SYNFIG_LAYER_SET_VERSION(Plant,"0.1");
 SYNFIG_LAYER_SET_CVS_ID(Plant,"$Id$");
 
@@ -78,6 +79,7 @@ Plant::Plant():
        split_angle(Angle::deg(10)),
        gravity(0,-0.1),
        velocity(0.3),
+       perp_velocity(0.0),
        step(0.01),
        sprouts(10)
 {
@@ -102,6 +104,7 @@ Plant::Plant():
        splits=5;
        drag=0.1;
        size=0.015;
+       needs_sync_=true;
        sync();
        size_as_alpha=false;
 }
@@ -118,10 +121,10 @@ Plant::branch(int n,int depth,float t, float stunt_growth, synfig::Point positio
                position[0]+=vel[0]*step;
                position[1]+=vel[1]*step;
 
-               particle_list.push_back(Particle(
-                       position,
-                       gradient(t)
-               ));
+               particle_list.push_back(Particle(position, gradient(t)));
+               if (particle_list.size() % 1000000 == 0)
+                       synfig::info("constructed %d million particles...", particle_list.size()/1000000);
+
                bounding_rect.expand(position);
        }
 
@@ -130,8 +133,10 @@ Plant::branch(int n,int depth,float t, float stunt_growth, synfig::Point positio
        synfig::Real sin_v=synfig::Angle::cos(split_angle).get();
        synfig::Real cos_v=synfig::Angle::sin(split_angle).get();
 
-       synfig::Vector velocity1(vel[0]*sin_v-vel[1]*cos_v+random_factor*random(2,30+n+depth,t*splits,0.0f,0.0f),vel[0]*cos_v+vel[1]*sin_v+random_factor*random(2,32+n+depth,t*splits,0.0f,0.0f));
-       synfig::Vector velocity2(vel[0]*sin_v+vel[1]*cos_v+random_factor*random(2,31+n+depth,t*splits,0.0f,0.0f),-vel[0]*cos_v+vel[1]*sin_v+random_factor*random(2,33+n+depth,t*splits,0.0f,0.0f));
+       synfig::Vector velocity1(vel[0]*sin_v - vel[1]*cos_v + random_factor*random(Random::SMOOTH_COSINE, 30+n+depth, t*splits, 0.0f, 0.0f),
+                                                        vel[0]*cos_v + vel[1]*sin_v + random_factor*random(Random::SMOOTH_COSINE, 32+n+depth, t*splits, 0.0f, 0.0f));
+       synfig::Vector velocity2(vel[0]*sin_v + vel[1]*cos_v + random_factor*random(Random::SMOOTH_COSINE, 31+n+depth, t*splits, 0.0f, 0.0f),
+                                                       -vel[0]*cos_v + vel[1]*sin_v + random_factor*random(Random::SMOOTH_COSINE, 33+n+depth, t*splits, 0.0f, 0.0f));
 
        Plant::branch(n,depth+1,t,stunt_growth,position,velocity1);
        Plant::branch(n,depth+1,t,stunt_growth,position,velocity2);
@@ -145,7 +150,7 @@ Plant::calc_bounding_rect()const
        bounding_rect=Rect::zero();
 
        // Bline must have at least 2 points in it
-       if(bline.size()<=2)
+       if(bline.size()<2)
                return;
 
        next=bline.begin();
@@ -172,13 +177,19 @@ Plant::calc_bounding_rect()const
 void
 Plant::sync()const
 {
+       Mutex::Lock lock(mutex);
+       if (!needs_sync_) return;
+       time_t start_time; time(&start_time);
        particle_list.clear();
 
        bounding_rect=Rect::zero();
 
        // Bline must have at least 2 points in it
-       if(bline.size()<=2)
+       if(bline.size()<2)
+       {
+               needs_sync_=false;
                return;
+       }
 
        std::vector<synfig::BLinePoint>::const_iterator iter,next;
 
@@ -190,11 +201,10 @@ Plant::sync()const
 
        next=bline.begin();
 
-       if(bline_loop)
-               iter=--bline.end();
-       else
-               iter=next++;
+       if(bline_loop)  iter=--bline.end(); // iter is the last  bline in the list; next is the first  bline in the list
+       else                    iter=next++;            // iter is the first bline in the list; next is the second bline in the list
 
+       // loop through the bline; seg counts the blines as we do so; stop before iter is the last bline in the list
        for(;next!=bline.end();iter=next++,seg++)
        {
                curve.p1()=iter->get_vertex();
@@ -205,38 +215,39 @@ Plant::sync()const
                etl::derivative<etl::hermite<Vector> > deriv(curve);
 
                Real f;
-               int i(0), b(round_to_int((1.0/step)/(float)sprouts-1));
-               if(b<=0)b=1;
+
+               int i=0, branch_count = 0, steps = round_to_int(1.0/step);
                for(f=0.0;f<1.0;f+=step,i++)
                {
                        Point point(curve(f));
 
-                       particle_list.push_back(Particle(
-                               point,
-                               gradient(0)
-                       ));
+                       particle_list.push_back(Particle(point, gradient(0)));
+                       if (particle_list.size() % 1000000 == 0)
+                               synfig::info("constructed %d million particles...", particle_list.size()/1000000);
 
                        bounding_rect.expand(point);
 
-                       Real stunt_growth(random(2,i,f+seg,0.0f,0.0f)/2.0+0.5);
+                       Real stunt_growth(random_factor * (random(Random::SMOOTH_COSINE,i,f+seg,0.0f,0.0f)/2.0+0.5));
                        stunt_growth*=stunt_growth;
 
-                       Vector branch_velocity(deriv(f).norm()*velocity);
+                       if((((i+1)*sprouts + steps/2) / steps) > branch_count) {
+                               Vector branch_velocity(deriv(f).norm()*velocity + deriv(f).perp().norm()*perp_velocity);
 
-                       branch_velocity[0]+=random_factor*random(2,1,f*splits,0.0f,0.0f);
-                       branch_velocity[1]+=random_factor*random(2,2,f*splits,0.0f,0.0f);
+                               branch_velocity[0] += random_factor * random(Random::SMOOTH_COSINE, 1, f*splits, 0.0f, 0.0f);
+                               branch_velocity[1] += random_factor * random(Random::SMOOTH_COSINE, 2, f*splits, 0.0f, 0.0f);
 
-                       if(i%b==0)
-                               branch(
-                                       i,
-                                       0,
-                                       0,      // time
-                                       stunt_growth, // stunt growth
-                                       point,branch_velocity
-                               );
+                               branch_count++;
+                               branch(i, 0, 0,          // time
+                                          stunt_growth, // stunt growth
+                                          point, branch_velocity);
+                       }
                }
        }
 
+       time_t end_time; time(&end_time);
+       if (end_time-start_time > 4)
+               synfig::info("Plant::sync() constructed %d particles in %d seconds\n",
+                                        particle_list.size(), int(end_time-start_time));
        needs_sync_=false;
 }
 
@@ -251,7 +262,7 @@ Plant::set_param(const String & param, const ValueBase &value)
 
                return true;
        }
-       if(param=="seed" && value.same_as(int()))
+       if(param=="seed" && value.same_type_as(int()))
        {
                random.set_seed(value.get(int()));
                needs_sync_=true;
@@ -261,6 +272,7 @@ Plant::set_param(const String & param, const ValueBase &value)
        IMPORT_PLUS(gravity,needs_sync_=true);
        IMPORT_PLUS(gradient,needs_sync_=true);
        IMPORT_PLUS(velocity,needs_sync_=true);
+       IMPORT_PLUS(perp_velocity,needs_sync_=true);
        IMPORT_PLUS(step,needs_sync_=true);
        IMPORT_PLUS(splits,needs_sync_=true);
        IMPORT_PLUS(sprouts,needs_sync_=true);
@@ -305,6 +317,7 @@ Plant::get_param(const String& param)const
        EXPORT(split_angle);
        EXPORT(gravity);
        EXPORT(velocity);
+       EXPORT(perp_velocity);
        EXPORT(step);
        EXPORT(gradient);
        EXPORT(splits);
@@ -328,62 +341,78 @@ Plant::get_param_vocab()const
 
        ret.push_back(ParamDesc("bline")
                .set_local_name(_("Vertices"))
+               .set_description(_("A list of BLine Points"))
                //.set_origin("offset")
                //.set_scalar("width")
-               .set_description(_("A list of BLine Points"))
        );
 
        ret.push_back(ParamDesc("gradient")
                .set_local_name(_("Gradient"))
+               .set_description(_("Gradient to be used for coloring the plant"))
        );
 
        ret.push_back(ParamDesc("split_angle")
                .set_local_name(_("Split Angle"))
+               .set_description(_("Angle by which each split deviates from its parent"))
        );
 
        ret.push_back(ParamDesc("gravity")
                .set_local_name(_("Gravity"))
+               .set_description(_("Direction in which the shoots tend to face"))
                .set_is_distance()
        );
 
        ret.push_back(ParamDesc("velocity")
-               .set_local_name(_("Velocity"))
+               .set_local_name(_("Tangential Velocity"))
+               .set_description(_("Amount to which shoots tend to grow along the tangent to the BLine"))
+       );
+
+       ret.push_back(ParamDesc("perp_velocity")
+               .set_local_name(_("Perpendicular Velocity"))
+               .set_description(_("Amount to which shoots tend to grow perpendicular to the tangent to the BLine"))
        );
 
        ret.push_back(ParamDesc("size")
                .set_local_name(_("Stem Size"))
+               .set_description(_("Size of the stem"))
                .set_is_distance()
        );
 
        ret.push_back(ParamDesc("size_as_alpha")
-               .set_local_name(_("SizeAsAlpha"))
+               .set_local_name(_("Size As Alpha"))
+               .set_description(_("If enabled, the alpha channel from the gradient is multiplied by the stem size, and an alpha of 1.0 is used when rendering"))
        );
 
        ret.push_back(ParamDesc("step")
                .set_local_name(_("Step"))
+               .set_description(_("Measure of the distance between points when rendering"))
        );
 
        ret.push_back(ParamDesc("seed")
                .set_local_name(_("Seed"))
+               .set_description(_("Used to seed the pseudo-random number generator"))
        );
 
        ret.push_back(ParamDesc("splits")
                .set_local_name(_("Splits"))
+               .set_description(_("Maximum number of times that each sprout can sprout recursively"))
        );
 
        ret.push_back(ParamDesc("sprouts")
                .set_local_name(_("Sprouts"))
+               .set_description(_("Number of places that growth occurs on each bline section"))
        );
 
        ret.push_back(ParamDesc("random_factor")
                .set_local_name(_("Random Factor"))
+               .set_description(_("Used to scale down all random effects.  Set to zero to disable randomness"))
        );
 
        ret.push_back(ParamDesc("drag")
                .set_local_name(_("Drag"))
+               .set_description(_("Drag slows the growth"))
        );
 
-
        return ret;
 }
 
@@ -404,6 +433,9 @@ Plant::accelerated_render(Context context,Surface *surface,int quality, const Re
        const int       w(renddesc.get_w());
        const int       h(renddesc.get_h());
 
+       const int       surface_width(surface->get_w());
+       const int       surface_height(surface->get_h());
+
        // Width and Height of a pixel
        const Real pw = (br[0] - tl[0]) / w;
        const Real ph = (br[1] - tl[1]) / h;
@@ -412,97 +444,186 @@ Plant::accelerated_render(Context context,Surface *surface,int quality, const Re
                sync();
 
        std::vector<Particle>::reverse_iterator iter;
-       const float size_factor(1);
-       float radius(size_factor*size*sqrt(1.0f/(abs(pw)*abs(ph))));
 
-       if(radius>1.0f)
+       float radius(size*sqrt(1.0f/(abs(pw)*abs(ph))));
+
+       int x1,y1,x2,y2;
+       for(iter=particle_list.rbegin();iter!=particle_list.rend();++iter)
        {
-               radius*=1.0;
-               int x1,y1,x2,y2;
-               for(iter=particle_list.rbegin();iter!=particle_list.rend();++iter)
+               float scaled_radius(radius);
+               Color color(iter->color);
+               if(size_as_alpha)
                {
-                       float radius(radius);
-                       Color color(iter->color);
-                       if(size_as_alpha)
-                       {
-                               radius*=color.get_a();
-                               color.set_a(1);
-                       }
-
-                       x1=ceil_to_int((iter->point[0]-tl[0])/pw-(radius*0.5));
-                       y1=ceil_to_int((iter->point[1]-tl[1])/ph-(radius*0.5));
-                       x2=x1+round_to_int(radius);
-                       y2=y1+round_to_int(radius);
-
-                       if(x1>=surface->get_w() || y1>=surface->get_h())
-                               continue;
-
-                       if(x2<0 || y2<0)
-                               continue;
-
-                       if(x2>=surface->get_w())
-                               x2=surface->get_w();
-                       if(y2>=surface->get_h())
-                               y2=surface->get_h();
-
-                       if(x1<0)
-                               x1=0;
-                       if(y1<0)
-                               y1=0;
-
-                       int w(min(round_to_int(radius),x2-x1));
-                       int h(min(round_to_int(radius),y2-y1));
-
-                       if(w<=0 || h<=0)
-                               continue;
-
-                       Surface::alpha_pen surface_pen(dest_surface.get_pen(x1,y1),1.0f);
+                       scaled_radius*=color.get_a();
+                       color.set_a(1);
+               }
 
+               // previously, radius was multiplied by sqrt(step)*12 only if
+               // the radius came out at less than 1 (pixel):
+               //   if (radius<=1.0f) radius*=sqrt(step)*12.0f;
+               // seems a little arbitrary - does it help?
+
+               // calculate the box that this particle will be drawn as
+               float x1f=(iter->point[0]-tl[0])/pw-(scaled_radius*0.5);
+               float x2f=(iter->point[0]-tl[0])/pw+(scaled_radius*0.5);
+               float y1f=(iter->point[1]-tl[1])/ph-(scaled_radius*0.5);
+               float y2f=(iter->point[1]-tl[1])/ph+(scaled_radius*0.5);
+               x1=ceil_to_int(x1f);
+               x2=ceil_to_int(x2f)-1;
+               y1=ceil_to_int(y1f);
+               y2=ceil_to_int(y2f)-1;
+
+               // if the box is entirely off the canvas, go to the next particle
+               if(x1>surface_width || y1>surface_height || x2<0 || y2<0) continue;
+
+               float x1e=x1-x1f, x2e=x2f-x2, y1e=y1-y1f, y2e=y2f-y2;
+               // printf("x1e %.4f x2e %.4f y1e %.4f y2e %.4f\n", x1e, x2e, y1e, y2e);
+
+               // adjust the box so it's entirely on the canvas
+               if(x1<=0) { x1=0; x1e=0; }
+               if(y1<=0) { y1=0; y1e=0; }
+               if(x2>=surface_width)  { x2=surface_width;  x2e=0; }
+               if(y2>=surface_height) { y2=surface_height; y2e=0; }
+
+               int w(x2-x1), h(y2-y1);
+
+               Surface::alpha_pen surface_pen(dest_surface.get_pen(x1,y1),1.0f);
+               if(w>0 && h>0)
                        dest_surface.fill(color,surface_pen,w,h);
+
+               /* the rectangle doesn't cross any vertical pixel boundaries so we don't
+                * need to draw any top or bottom edges
+                */
+               if(x2<x1)
+               {
+                       // case 1 - a single pixel
+                       if(y2<y1)
+                       {
+                               surface_pen.move_to(x2,y2);
+                               surface_pen.set_alpha((x2f-x1f)*(y2f-y1f));
+                               surface_pen.put_value(color);
+                       }
+                       // case 2 - a single vertical column of pixels
+                       else
+                       {
+                               surface_pen.move_to(x2,y1-1);
+                               if (y1e!=0)     // maybe draw top pixel
+                               {
+                                       surface_pen.set_alpha(y1e*(x2f-x1f));
+                                       surface_pen.put_value(color);
+                               }
+                               surface_pen.inc_y();
+                               surface_pen.set_alpha(x2f-x1f);
+                               for(int i=y1; i<y2; i++) // maybe draw pixels between
+                               {
+                                       surface_pen.put_value(color);
+                                       surface_pen.inc_y();
+                               }
+                               if (y2e!=0)     // maybe draw bottom pixel
+                               {
+                                       surface_pen.set_alpha(y2e*(x2f-x1f));
+                                       surface_pen.put_value(color);
+                               }
+                       }
                }
-       }
-       else
-       {
-               //radius/=0.01;
-               radius*=sqrt(step)*12.0f;
-               int x,y;
-               float a,b,c,d;
-               for(iter=particle_list.rbegin();iter!=particle_list.rend();++iter)
+               else
                {
-                       float radius(radius);
-                       Color color(iter->color);
-                       if(size_as_alpha)
+                       // case 3 - a single horizontal row of pixels
+                       if(y2<y1)
                        {
-                               radius*=color.get_a();
-                               color.set_a(1);
+                               surface_pen.move_to(x1-1,y2);
+                               if (x1e!=0)     // maybe draw left pixel
+                               {
+                                       surface_pen.set_alpha(x1e*(y2f-y1f));
+                                       surface_pen.put_value(color);
+                               }
+                               surface_pen.inc_x();
+                               surface_pen.set_alpha(y2f-y1f);
+                               for(int i=x1; i<x2; i++) // maybe draw pixels between
+                               {
+                                       surface_pen.put_value(color);
+                                       surface_pen.inc_x();
+                               }
+                               if (x2e!=0)     // maybe draw right pixel
+                               {
+                                       surface_pen.set_alpha(x2e*(y2f-y1f));
+                                       surface_pen.put_value(color);
+                               }
                        }
-
-                       x=floor_to_int((iter->point[0]-tl[0])/pw-0.5f);
-                       y=floor_to_int((iter->point[1]-tl[1])/ph-0.5f);
-
-                       if(x>=surface->get_w()-1 || y>=surface->get_h()-1 || x<0 || y<0)
+                       // case 4 - a proper block of pixels
+                       else
                        {
-                               continue;
+                               if (x1e!=0)     // maybe draw left edge
+                               {
+                                       surface_pen.move_to(x1-1,y1-1);
+                                       if (y1e!=0)     // maybe draw top left pixel
+                                       {
+                                               surface_pen.set_alpha(x1e*y1e);
+                                               surface_pen.put_value(color);
+                                       }
+                                       surface_pen.inc_y();
+                                       surface_pen.set_alpha(x1e);
+                                       for(int i=y1; i<y2; i++) // maybe draw pixels along the left edge
+                                       {
+                                               surface_pen.put_value(color);
+                                               surface_pen.inc_y();
+                                       }
+                                       if (y2e!=0)     // maybe draw bottom left pixel
+                                       {
+                                               surface_pen.set_alpha(x1e*y2e);
+                                               surface_pen.put_value(color);
+                                       }
+                                       surface_pen.inc_x();
+                               }
+                               else
+                                       surface_pen.move_to(x1,y2);
+
+                               if (y2e!=0)     // maybe draw bottom edge
+                               {
+                                       surface_pen.set_alpha(y2e);
+                                       for(int i=x1; i<x2; i++) // maybe draw pixels along the bottom edge
+                                       {
+                                               surface_pen.put_value(color);
+                                               surface_pen.inc_x();
+                                       }
+                                       if (x2e!=0)     // maybe draw bottom right pixel
+                                       {
+                                               surface_pen.set_alpha(x2e*y2e);
+                                               surface_pen.put_value(color);
+                                       }
+                                       surface_pen.dec_y();
+                               }
+                               else
+                                       surface_pen.move_to(x2,y2-1);
+
+                               if (x2e!=0)     // maybe draw right edge
+                               {
+                                       surface_pen.set_alpha(x2e);
+                                       for(int i=y1; i<y2; i++) // maybe draw pixels along the right edge
+                                       {
+                                               surface_pen.put_value(color);
+                                               surface_pen.dec_y();
+                                       }
+                                       if (y1e!=0)     // maybe draw top right pixel
+                                       {
+                                               surface_pen.set_alpha(x2e*y1e);
+                                               surface_pen.put_value(color);
+                                       }
+                                       surface_pen.dec_x();
+                               }
+                               else
+                                       surface_pen.move_to(x2-1,y1-1);
+
+                               if (y1e!=0)     // maybe draw top edge
+                               {
+                                       surface_pen.set_alpha(y1e);
+                                       for(int i=x1; i<x2; i++) // maybe draw pixels along the top edge
+                                       {
+                                               surface_pen.put_value(color);
+                                               surface_pen.dec_x();
+                                       }
+                               }
                        }
-
-                       a=((iter->point[0]-tl[0])/pw-0.5f-x)*radius;
-                       b=((iter->point[1]-tl[1])/ph-0.5f-y)*radius;
-                       c=radius-a;
-                       d=radius-b;
-
-                       Surface::alpha_pen surface_pen(dest_surface.get_pen(x,y),1.0f);
-
-                       surface_pen.set_alpha(c*d);
-                       surface_pen.put_value(color);
-                       surface_pen.inc_x();
-                       surface_pen.set_alpha(a*d);
-                       surface_pen.put_value(color);
-                       surface_pen.inc_y();
-                       surface_pen.set_alpha(a*b);
-                       surface_pen.put_value(color);
-                       surface_pen.dec_x();
-                       surface_pen.set_alpha(c*b);
-                       surface_pen.put_value(color);
                }
        }