Remove .gitignore do nothing is ignored.
[synfig.git] / synfig-core / trunk / src / modules / mod_particle / plant.cpp
index 7affc15..47b13a3 100644 (file)
@@ -1,12 +1,12 @@
 /* === S Y N F I G ========================================================= */
 /*!    \file plant.cpp
-**     \brief Template
+**     \brief Implementation of the "Plant" layer
 **
 **     $Id$
 **
 **     \legal
 **     Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
-**     Copyright (c) 2007 Chris Moore
+**     Copyright (c) 2007, 2008 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,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,_("Other"));
-SYNFIG_LAYER_SET_VERSION(Plant,"0.1");
+SYNFIG_LAYER_SET_LOCAL_NAME(Plant,N_("Plant"));
+SYNFIG_LAYER_SET_CATEGORY(Plant,N_("Other"));
+SYNFIG_LAYER_SET_VERSION(Plant,"0.2");
 SYNFIG_LAYER_SET_CVS_ID(Plant,"$Id$");
 
 /* === P R O C E D U R E S ================================================= */
@@ -76,12 +76,15 @@ SYNFIG_LAYER_SET_CVS_ID(Plant,"$Id$");
 
 
 Plant::Plant():
+       origin(0,0),
        split_angle(Angle::deg(10)),
        gravity(0,-0.1),
        velocity(0.3),
        perp_velocity(0.0),
        step(0.01),
-       sprouts(10)
+       sprouts(10),
+       version(version__),
+       use_width(true)
 {
        bounding_rect=Rect::zero();
        random_factor=0.2;
@@ -208,6 +211,9 @@ Plant::sync()const
        // 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++)
        {
+               float iterw=iter->get_width();  // the width value of the iter vertex
+               float nextw=next->get_width();  // the width value of the next vertex
+               float width;                                    // the width at an intermediate position
                curve.p1()=iter->get_vertex();
                curve.t1()=iter->get_tangent2();
                curve.p2()=next->get_vertex();
@@ -235,9 +241,20 @@ Plant::sync()const
                        if((((i+1)*sprouts + steps/2) / steps) > branch_count) {
                                Vector branch_velocity(deriv(f).norm()*velocity + deriv(f).perp().norm()*perp_velocity);
 
+                               if (isnan(branch_velocity[0]) || isnan(branch_velocity[1]))
+                                       continue;
+
                                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 (use_width)
+                               {
+                                       width = iterw+(nextw-iterw)*f; // calculate the width based on the current position
+
+                                       branch_velocity[0] *= width; // scale the velocity accordingly to the current width
+                                       branch_velocity[1] *= width;
+                               }
+
                                branch_count++;
                                branch(i, 0, 0,          // time
                                           stunt_growth, // stunt growth
@@ -270,6 +287,7 @@ Plant::set_param(const String & param, const ValueBase &value)
                needs_sync_=true;
                return true;
        }
+       IMPORT(origin);
        IMPORT_PLUS(split_angle,needs_sync_=true);
        IMPORT_PLUS(gravity,needs_sync_=true);
        IMPORT_PLUS(gradient,needs_sync_=true);
@@ -295,6 +313,9 @@ Plant::set_param(const String & param, const ValueBase &value)
        IMPORT(size);
        IMPORT(size_as_alpha);
        IMPORT(reverse);
+       IMPORT(use_width);
+
+       IMPORT_AS(origin,"offset");
 
        return Layer_Composite::set_param(param,value);
 }
@@ -329,6 +350,7 @@ Plant::get_param(const String& param)const
        if(param=="seed")
                return random.get_seed();
        EXPORT(bline);
+       EXPORT(origin);
        EXPORT(split_angle);
        EXPORT(gravity);
        EXPORT(velocity);
@@ -340,12 +362,14 @@ Plant::get_param(const String& param)const
        EXPORT(random_factor);
        EXPORT(drag);
        EXPORT(size);
-
        EXPORT(size_as_alpha);
        EXPORT(reverse);
+       EXPORT(use_width);
 
        EXPORT_NAME();
-       EXPORT_VERSION();
+
+       if(param=="Version" || param=="version" || param=="version__")
+               return version;
 
        return Layer_Composite::get_param(param);
 }
@@ -358,8 +382,12 @@ 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_origin("origin")
+               .set_hint("width")
+       );
+
+       ret.push_back(ParamDesc("origin")
+               .set_local_name(_("Origin"))
        );
 
        ret.push_back(ParamDesc("gradient")
@@ -434,10 +462,26 @@ Plant::get_param_vocab()const
                .set_description(_("Drag slows the growth"))
        );
 
+       ret.push_back(ParamDesc("use_width")
+               .set_local_name(_("Use Width"))
+               .set_description(_("Scale the velocity by the bline's width"))
+       );
+
        return ret;
 }
 
 bool
+Plant::set_version(const String &ver)
+{
+       version = ver;
+
+       if (version == "0.1")
+               use_width = false;
+
+       return true;
+}
+
+bool
 Plant::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
 {
        bool ret(context.accelerated_render(surface,quality,renddesc,cb));
@@ -448,8 +492,8 @@ Plant::accelerated_render(Context context,Surface *surface,int quality, const Re
        dest_surface.set_wh(surface->get_w(),surface->get_h());
        dest_surface.clear();
 
-       const Point     tl(renddesc.get_tl());
-       const Point br(renddesc.get_br());
+       const Point     tl(renddesc.get_tl()-origin);
+       const Point br(renddesc.get_br()-origin);
 
        const int       w(renddesc.get_w());
        const int       h(renddesc.get_h());
@@ -461,6 +505,9 @@ Plant::accelerated_render(Context context,Surface *surface,int quality, const Re
        const Real pw = (br[0] - tl[0]) / w;
        const Real ph = (br[1] - tl[1]) / h;
 
+       if (isinf(pw) || isinf(ph))
+               return true;
+
        if(needs_sync_==true)
                sync();