Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / synfig_0_61_03 / synfig-core / src / modules / mod_particle / plant.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file bline.cpp
3 **      \brief Template
4 **
5 **      $Id: plant.cpp,v 1.2 2005/01/13 06:48:39 darco Exp $
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 <synfig/angle.h>
33 #include "plant.h"
34 #include <synfig/string.h>
35 #include <synfig/time.h>
36 #include <synfig/context.h>
37 #include <synfig/paramdesc.h>
38 #include <synfig/renddesc.h>
39 #include <synfig/surface.h>
40 #include <synfig/value.h>
41 #include <synfig/valuenode.h>
42
43 #include <ETL/calculus>
44 #include <ETL/bezier>
45 #include <ETL/hermite>
46 #include <vector>
47
48 #include <synfig/valuenode_bline.h>
49
50 #endif
51
52 using namespace etl;
53
54 /* === M A C R O S ========================================================= */
55
56 #define SAMPLES         300
57 #define ROUND_END_FACTOR        (4)
58 #define CUSP_THRESHOLD          (0.15)
59 #define NO_LOOP_COOKIE          synfig::Vector(84951305,7836658)
60 #define EPSILON                         (0.000000001)
61 #define CUSP_TANGENT_ADJUST     (0.025)
62
63 /* === G L O B A L S ======================================================= */
64
65 SYNFIG_LAYER_INIT(Plant);
66 SYNFIG_LAYER_SET_NAME(Plant,"plant");
67 SYNFIG_LAYER_SET_LOCAL_NAME(Plant,_("Plant"));
68 SYNFIG_LAYER_SET_CATEGORY(Plant,_("Particle Systems"));
69 SYNFIG_LAYER_SET_VERSION(Plant,"0.1");
70 SYNFIG_LAYER_SET_CVS_ID(Plant,"$Id: plant.cpp,v 1.2 2005/01/13 06:48:39 darco Exp $");
71
72 /* === P R O C E D U R E S ================================================= */
73
74 /* === M E T H O D S ======================================================= */
75
76
77 Plant::Plant():
78         split_angle(Angle::deg(10)),
79         gravity(0,-0.1),
80         velocity(0.3),
81         step(0.01),
82         sprouts(10)
83 {
84         bounding_rect=Rect::zero();
85         random_factor=0.2;
86         random.set_seed(time(NULL));
87
88         bline.push_back(BLinePoint());
89         bline.push_back(BLinePoint());
90         bline.push_back(BLinePoint());
91         bline[0].set_vertex(Point(0,1));        
92         bline[1].set_vertex(Point(0,-1));       
93         bline[2].set_vertex(Point(1,0));
94         bline[0].set_tangent(bline[1].get_vertex()-bline[2].get_vertex()*0.5f); 
95         bline[1].set_tangent(bline[2].get_vertex()-bline[0].get_vertex()*0.5f); 
96         bline[2].set_tangent(bline[0].get_vertex()-bline[1].get_vertex()*0.5f); 
97         bline[0].set_width(1.0f);       
98         bline[1].set_width(1.0f);       
99         bline[2].set_width(1.0f);       
100         bline_loop=true;
101         mass=(0.5);
102         splits=5;
103         drag=0.1;
104         size=0.015;
105         sync();
106         size_as_alpha=false;
107 }
108
109 void
110 Plant::branch(int n,int depth,float t, float stunt_growth, synfig::Point position,synfig::Vector vel)const
111 {
112         float next_split((1.0-t)/(splits-depth)+t/*+random_factor*random(40+depth,t*splits,0,0)/splits*/);
113         for(;t<next_split;t+=step)
114         {
115                 vel[0]+=gravity[0]*step;
116                 vel[1]+=gravity[1]*step;
117                 vel*=(1.0-(drag)*step);
118                 position[0]+=vel[0]*step;
119                 position[1]+=vel[1]*step;
120
121                 particle_list.push_back(Particle(
122                         position,
123                         gradient(t)
124                 ));
125                 bounding_rect.expand(position);
126         }
127         
128         if(t>=1.0-stunt_growth)return;
129
130         synfig::Real sin_v=synfig::Angle::cos(split_angle).get();
131         synfig::Real cos_v=synfig::Angle::sin(split_angle).get();
132         
133         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));
134         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));
135         
136         Plant::branch(n,depth+1,t,stunt_growth,position,velocity1);
137         Plant::branch(n,depth+1,t,stunt_growth,position,velocity2);
138 }
139
140 void
141 Plant::calc_bounding_rect()const
142 {
143         std::vector<synfig::BLinePoint>::const_iterator iter,next;
144
145         bounding_rect=Rect::zero();
146
147         // Bline must have at least 2 points in it
148         if(bline.size()<=2)
149                 return;
150
151         next=bline.begin();
152
153         if(bline_loop)
154                 iter=--bline.end();
155         else
156                 iter=next++;
157         
158         for(;next!=bline.end();iter=next++)
159         {
160                 bounding_rect.expand(iter->get_vertex());
161                 bounding_rect.expand(next->get_vertex());
162                 bounding_rect.expand(iter->get_vertex()+iter->get_tangent2()*0.3333333333333);
163                 bounding_rect.expand(next->get_vertex()-next->get_tangent1()*0.3333333333333);
164                 bounding_rect.expand(next->get_vertex()+next->get_tangent2()*velocity);
165         }
166         bounding_rect.expand_x(gravity[0]);
167         bounding_rect.expand_y(gravity[1]);
168         bounding_rect.expand_x(size);
169         bounding_rect.expand_y(size);
170 }
171
172 void
173 Plant::sync()const
174 {
175         particle_list.clear();
176         
177         bounding_rect=Rect::zero();
178
179         // Bline must have at least 2 points in it
180         if(bline.size()<=2)
181                 return;
182         
183         std::vector<synfig::BLinePoint>::const_iterator iter,next;
184
185         etl::hermite<Vector> curve;
186         
187         Real step(abs(this->step));
188         
189         int seg(0);
190
191         next=bline.begin();
192         
193         if(bline_loop)
194                 iter=--bline.end();
195         else
196                 iter=next++;
197         
198         for(;next!=bline.end();iter=next++,seg++)
199         {
200                 curve.p1()=iter->get_vertex();
201                 curve.t1()=iter->get_tangent2();
202                 curve.p2()=next->get_vertex();
203                 curve.t2()=next->get_tangent1();
204                 curve.sync();
205                 etl::derivative<etl::hermite<Vector> > deriv(curve);
206                 
207                 Real f;
208                 int i(0), b(round_to_int((1.0/step)/(float)sprouts-1));
209                 if(b<=0)b=1;
210                 for(f=0.0;f<1.0;f+=step,i++)
211                 {
212                         Point point(curve(f));
213                         
214                         particle_list.push_back(Particle(
215                                 point,
216                                 gradient(0)
217                         ));
218                         
219                         bounding_rect.expand(point);
220                         
221                         Real stunt_growth(random(2,i,f+seg,0.0f,0.0f)/2.0+0.5);
222                         stunt_growth*=stunt_growth;
223                         
224                         Vector branch_velocity(deriv(f).norm()*velocity);
225                         
226                         branch_velocity[0]+=random_factor*random(2,1,f*splits,0.0f,0.0f);
227                         branch_velocity[1]+=random_factor*random(2,2,f*splits,0.0f,0.0f);
228                         
229                         if(i%b==0)
230                                 branch(
231                                         i,
232                                         0,
233                                         0,      // time
234                                         stunt_growth, // stunt growth
235                                         point,branch_velocity
236                                 );
237                 }
238         }
239         
240         needs_sync_=false;
241 }
242
243 bool
244 Plant::set_param(const String & param, const ValueBase &value)
245 {
246         if(param=="bline" && value.get_type()==ValueBase::TYPE_LIST)
247         {
248                 bline=value;
249                 bline_loop=value.get_loop();
250                 needs_sync_=true;
251                 
252                 return true;
253         }
254         if(param=="seed" && value.same_as(int()))
255         {
256                 random.set_seed(value.get(int()));
257                 needs_sync_=true;
258                 return true;
259         }
260         IMPORT_PLUS(split_angle,needs_sync_=true);
261         IMPORT_PLUS(gravity,needs_sync_=true);
262         IMPORT_PLUS(gradient,needs_sync_=true);
263         IMPORT_PLUS(velocity,needs_sync_=true);
264         IMPORT_PLUS(step,needs_sync_=true);
265         IMPORT_PLUS(splits,needs_sync_=true);
266         IMPORT_PLUS(sprouts,needs_sync_=true);
267         IMPORT_PLUS(random_factor,needs_sync_=true);
268         IMPORT_PLUS(drag,needs_sync_=true);
269         IMPORT(size);
270         IMPORT(size_as_alpha);
271         
272         return Layer_Composite::set_param(param,value);
273 }
274 /*
275 void
276 Plant::set_time(Context context, Time time)const
277 {
278         if(needs_sync==true)
279         {
280                 sync();
281                 needs_sync_=false;
282         }
283         //const_cast<Plant*>(this)->sync();
284         context.set_time(time);
285 }
286
287 void
288 Plant::set_time(Context context, Time time, Vector pos)const
289 {
290         if(needs_sync==true)
291         {
292                 sync();
293                 needs_sync_=false;
294         }
295         //const_cast<Plant*>(this)->sync();
296         context.set_time(time,pos);
297 }
298 */
299 ValueBase
300 Plant::get_param(const String& param)const
301 {
302         if(param=="seed")
303                 return random.get_seed();
304         EXPORT(bline);
305         EXPORT(split_angle);
306         EXPORT(gravity);
307         EXPORT(velocity);
308         EXPORT(step);
309         EXPORT(gradient);
310         EXPORT(splits);
311         EXPORT(sprouts);
312         EXPORT(random_factor);
313         EXPORT(drag);
314         EXPORT(size);
315
316         EXPORT(size_as_alpha);
317         
318         EXPORT_NAME();
319         EXPORT_VERSION();
320
321         return Layer_Composite::get_param(param);
322 }
323
324 Layer::Vocab
325 Plant::get_param_vocab()const
326 {
327         Layer::Vocab ret(Layer_Composite::get_param_vocab());
328
329         ret.push_back(ParamDesc("bline")
330                 .set_local_name(_("Vertices"))
331                 //.set_origin("offset")
332                 //.set_scalar("width")
333                 .set_description(_("A list of BLine Points"))
334         );
335
336         ret.push_back(ParamDesc("gradient")
337                 .set_local_name(_("Gradient"))
338         );
339
340         ret.push_back(ParamDesc("split_angle")
341                 .set_local_name(_("Split Angle"))
342         );
343
344         ret.push_back(ParamDesc("gravity")
345                 .set_local_name(_("Gravity"))
346                 .set_is_distance()
347         );
348
349         ret.push_back(ParamDesc("velocity")
350                 .set_local_name(_("Velocity"))
351         );
352
353         ret.push_back(ParamDesc("size")
354                 .set_local_name(_("Stem Size"))
355                 .set_is_distance()
356         );
357
358         ret.push_back(ParamDesc("size_as_alpha")
359                 .set_local_name(_("SizeAsAlpha"))
360         );
361
362         ret.push_back(ParamDesc("step")
363                 .set_local_name(_("Step"))
364         );
365
366         ret.push_back(ParamDesc("seed")
367                 .set_local_name(_("Seed"))
368         );
369
370         ret.push_back(ParamDesc("splits")
371                 .set_local_name(_("Splits"))
372         );
373
374         ret.push_back(ParamDesc("sprouts")
375                 .set_local_name(_("Sprouts"))
376         );
377
378         ret.push_back(ParamDesc("random_factor")
379                 .set_local_name(_("Random Factor"))
380         );
381
382         ret.push_back(ParamDesc("drag")
383                 .set_local_name(_("Drag"))
384         );
385
386
387         return ret;
388 }
389
390 bool
391 Plant::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
392 {       
393         bool ret(context.accelerated_render(surface,quality,renddesc,cb));
394         if(is_disabled() || !ret)
395                 return ret;
396         
397         Surface dest_surface;
398         dest_surface.set_wh(surface->get_w(),surface->get_h());
399         dest_surface.clear();
400         
401         const Point     tl(renddesc.get_tl());
402         const Point br(renddesc.get_br());
403
404         const int       w(renddesc.get_w());
405         const int       h(renddesc.get_h());
406
407         // Width and Height of a pixel
408         const Real pw = (br[0] - tl[0]) / w;
409         const Real ph = (br[1] - tl[1]) / h;
410
411         if(needs_sync_==true)
412                 sync();
413
414         std::vector<Particle>::reverse_iterator iter;
415         const float size_factor(1);
416         float radius(size_factor*size*sqrt(1.0f/(abs(pw)*abs(ph))));
417
418         if(radius>1.0f)
419         {
420                 radius*=1.0;
421                 int x1,y1,x2,y2;
422                 for(iter=particle_list.rbegin();iter!=particle_list.rend();++iter)
423                 {
424                         float radius(radius);
425                         Color color(iter->color);
426                         if(size_as_alpha)
427                         {
428                                 radius*=color.get_a();
429                                 color.set_a(1);
430                         }
431                         
432                         x1=ceil_to_int((iter->point[0]-tl[0])/pw-(radius*0.5));
433                         y1=ceil_to_int((iter->point[1]-tl[1])/ph-(radius*0.5));
434                         x2=x1+round_to_int(radius);
435                         y2=y1+round_to_int(radius);
436                                         
437                         if(x1>=surface->get_w() || y1>=surface->get_h())
438                                 continue;
439
440                         if(x2<0 || y2<0)
441                                 continue;
442
443                         if(x2>=surface->get_w())
444                                 x2=surface->get_w();
445                         if(y2>=surface->get_h())
446                                 y2=surface->get_h();
447                         
448                         if(x1<0)
449                                 x1=0;
450                         if(y1<0)
451                                 y1=0;
452                         
453                         int w(min(round_to_int(radius),x2-x1));
454                         int h(min(round_to_int(radius),y2-y1));
455                         
456                         if(w<=0 || h<=0)
457                                 continue;
458                                 
459                         Surface::alpha_pen surface_pen(dest_surface.get_pen(x1,y1),1.0f);
460                         
461                         dest_surface.fill(color,surface_pen,w,h);
462                 }
463         }
464         else
465         {
466                 //radius/=0.01;
467                 radius*=sqrt(step)*12.0f;
468                 int x,y;
469                 float a,b,c,d;
470                 for(iter=particle_list.rbegin();iter!=particle_list.rend();++iter)
471                 {
472                         float radius(radius);
473                         Color color(iter->color);
474                         if(size_as_alpha)
475                         {
476                                 radius*=color.get_a();
477                                 color.set_a(1);
478                         }
479
480                         x=floor_to_int((iter->point[0]-tl[0])/pw-0.5f);
481                         y=floor_to_int((iter->point[1]-tl[1])/ph-0.5f);
482                                         
483                         if(x>=surface->get_w()-1 || y>=surface->get_h()-1 || x<0 || y<0)
484                         {
485                                 continue;
486                         }
487         
488                         a=((iter->point[0]-tl[0])/pw-0.5f-x)*radius;
489                         b=((iter->point[1]-tl[1])/ph-0.5f-y)*radius;
490                         c=radius-a;
491                         d=radius-b;
492                         
493                         Surface::alpha_pen surface_pen(dest_surface.get_pen(x,y),1.0f);
494                         
495                         surface_pen.set_alpha(c*d);
496                         surface_pen.put_value(color);
497                         surface_pen.inc_x();
498                         surface_pen.set_alpha(a*d);
499                         surface_pen.put_value(color);
500                         surface_pen.inc_y();
501                         surface_pen.set_alpha(a*b);
502                         surface_pen.put_value(color);
503                         surface_pen.dec_x();
504                         surface_pen.set_alpha(c*b);
505                         surface_pen.put_value(color);           
506                 }
507         }
508                 
509         Surface::alpha_pen pen(surface->get_pen(0,0),get_amount(),get_blend_method());
510         dest_surface.blit_to(pen);
511         
512         return true;
513 }
514
515 Rect
516 Plant::get_bounding_rect(Context context)const
517 {
518         if(needs_sync_==true)
519                 sync();
520         
521         if(is_disabled())
522                 return Rect::zero();
523         
524         if(Color::is_onto(get_blend_method()))
525                 return context.get_full_bounding_rect() & bounding_rect;
526
527         //if(get_blend_method()==Color::BLEND_BEHIND)
528         //      return context.get_full_bounding_rect() | bounding_rect;
529         return bounding_rect;
530 }