Don't keep trying to sync() a plant layer if its bline has less than 2 points.
[synfig.git] / synfig-core / trunk / src / modules / mod_particle / plant.cpp
index 80fde0b..1e7a948 100644 (file)
@@ -1,8 +1,8 @@
 /* === S Y N F I G ========================================================= */
-/*!    \file bline.cpp
+/*!    \file plant.cpp
 **     \brief Template
 **
-**     $Id: plant.cpp,v 1.2 2005/01/13 06:48:39 darco Exp $
+**     $Id$
 **
 **     \legal
 **     Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
@@ -65,9 +65,9 @@ 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: plant.cpp,v 1.2 2005/01/13 06:48:39 darco Exp $");
+SYNFIG_LAYER_SET_CVS_ID(Plant,"$Id$");
 
 /* === P R O C E D U R E S ================================================= */
 
@@ -102,6 +102,7 @@ Plant::Plant():
        splits=5;
        drag=0.1;
        size=0.015;
+       needs_sync_=true;
        sync();
        size_as_alpha=false;
 }
@@ -130,8 +131,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 +148,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 +175,18 @@ Plant::calc_bounding_rect()const
 void
 Plant::sync()const
 {
+       Mutex::Lock lock(mutex);
+       if (!needs_sync_) return;
        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;
 
@@ -218,13 +226,13 @@ Plant::sync()const
 
                        bounding_rect.expand(point);
 
-                       Real stunt_growth(random(2,i,f+seg,0.0f,0.0f)/2.0+0.5);
+                       Real stunt_growth(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);
 
-                       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(
@@ -251,7 +259,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;
@@ -356,7 +364,7 @@ Plant::get_param_vocab()const
        );
 
        ret.push_back(ParamDesc("size_as_alpha")
-               .set_local_name(_("SizeAsAlpha"))
+               .set_local_name(_("Size As Alpha"))
        );
 
        ret.push_back(ParamDesc("step")
@@ -413,7 +421,7 @@ Plant::accelerated_render(Context context,Surface *surface,int quality, const Re
 
        std::vector<Particle>::reverse_iterator iter;
        const float size_factor(1);
-       float radius(size_factor*size*sqrt(1.0f/(abs(pw)*abs(ph))));
+       float radius(size_factor*size*sqrt(1.0f/(abs(pw)*abs(ph)))), temp_radius;
 
        if(radius>1.0f)
        {
@@ -421,7 +429,8 @@ Plant::accelerated_render(Context context,Surface *surface,int quality, const Re
                int x1,y1,x2,y2;
                for(iter=particle_list.rbegin();iter!=particle_list.rend();++iter)
                {
-                       float radius(radius);
+                       temp_radius = radius;
+                       float radius(temp_radius);
                        Color color(iter->color);
                        if(size_as_alpha)
                        {
@@ -469,7 +478,8 @@ Plant::accelerated_render(Context context,Surface *surface,int quality, const Re
                float a,b,c,d;
                for(iter=particle_list.rbegin();iter!=particle_list.rend();++iter)
                {
-                       float radius(radius);
+                       temp_radius = radius;
+                       float radius(temp_radius);
                        Color color(iter->color);
                        if(size_as_alpha)
                        {