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