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