1 /* === S Y N F I G ========================================================= */
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 2007 Chris Moore
11 ** This package is free software; you can redistribute it and/or
12 ** modify it under the terms of the GNU General Public License as
13 ** published by the Free Software Foundation; either version 2 of
14 ** the License, or (at your option) any later version.
16 ** This package is distributed in the hope that it will be useful,
17 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ** General Public License for more details.
22 /* ========================================================================= */
24 /* === H E A D E R S ======================================================= */
33 #include <synfig/angle.h>
35 #include <synfig/string.h>
36 #include <synfig/time.h>
37 #include <synfig/context.h>
38 #include <synfig/paramdesc.h>
39 #include <synfig/renddesc.h>
40 #include <synfig/surface.h>
41 #include <synfig/value.h>
42 #include <synfig/valuenode.h>
44 #include <ETL/calculus>
46 #include <ETL/hermite>
49 #include <synfig/valuenode_bline.h>
55 /* === M A C R O S ========================================================= */
58 #define ROUND_END_FACTOR (4)
59 #define CUSP_THRESHOLD (0.15)
60 #define NO_LOOP_COOKIE synfig::Vector(84951305,7836658)
61 #define EPSILON (0.000000001)
62 #define CUSP_TANGENT_ADJUST (0.025)
64 /* === G L O B A L S ======================================================= */
66 SYNFIG_LAYER_INIT(Plant);
67 SYNFIG_LAYER_SET_NAME(Plant,"plant");
68 SYNFIG_LAYER_SET_LOCAL_NAME(Plant,_("Plant"));
69 SYNFIG_LAYER_SET_CATEGORY(Plant,_("Other"));
70 SYNFIG_LAYER_SET_VERSION(Plant,"0.1");
71 SYNFIG_LAYER_SET_CVS_ID(Plant,"$Id$");
73 /* === P R O C E D U R E S ================================================= */
75 /* === M E T H O D S ======================================================= */
79 split_angle(Angle::deg(10)),
86 bounding_rect=Rect::zero();
88 random.set_seed(time(NULL));
90 bline.push_back(BLinePoint());
91 bline.push_back(BLinePoint());
92 bline.push_back(BLinePoint());
93 bline[0].set_vertex(Point(0,1));
94 bline[1].set_vertex(Point(0,-1));
95 bline[2].set_vertex(Point(1,0));
96 bline[0].set_tangent(bline[1].get_vertex()-bline[2].get_vertex()*0.5f);
97 bline[1].set_tangent(bline[2].get_vertex()-bline[0].get_vertex()*0.5f);
98 bline[2].set_tangent(bline[0].get_vertex()-bline[1].get_vertex()*0.5f);
99 bline[0].set_width(1.0f);
100 bline[1].set_width(1.0f);
101 bline[2].set_width(1.0f);
113 Plant::branch(int n,int depth,float t, float stunt_growth, synfig::Point position,synfig::Vector vel)const
115 float next_split((1.0-t)/(splits-depth)+t/*+random_factor*random(40+depth,t*splits,0,0)/splits*/);
116 for(;t<next_split;t+=step)
118 vel[0]+=gravity[0]*step;
119 vel[1]+=gravity[1]*step;
120 vel*=(1.0-(drag)*step);
121 position[0]+=vel[0]*step;
122 position[1]+=vel[1]*step;
124 particle_list.push_back(Particle(position, gradient(t)));
125 if (particle_list.size() % 1000000 == 0)
126 synfig::info("constructed %d million particles...", particle_list.size()/1000000);
128 bounding_rect.expand(position);
131 if(t>=1.0-stunt_growth)return;
133 synfig::Real sin_v=synfig::Angle::cos(split_angle).get();
134 synfig::Real cos_v=synfig::Angle::sin(split_angle).get();
136 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),
137 vel[0]*cos_v + vel[1]*sin_v + random_factor*random(Random::SMOOTH_COSINE, 32+n+depth, t*splits, 0.0f, 0.0f));
138 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),
139 -vel[0]*cos_v + vel[1]*sin_v + random_factor*random(Random::SMOOTH_COSINE, 33+n+depth, t*splits, 0.0f, 0.0f));
141 Plant::branch(n,depth+1,t,stunt_growth,position,velocity1);
142 Plant::branch(n,depth+1,t,stunt_growth,position,velocity2);
146 Plant::calc_bounding_rect()const
148 std::vector<synfig::BLinePoint>::const_iterator iter,next;
150 bounding_rect=Rect::zero();
152 // Bline must have at least 2 points in it
163 for(;next!=bline.end();iter=next++)
165 bounding_rect.expand(iter->get_vertex());
166 bounding_rect.expand(next->get_vertex());
167 bounding_rect.expand(iter->get_vertex()+iter->get_tangent2()*0.3333333333333);
168 bounding_rect.expand(next->get_vertex()-next->get_tangent1()*0.3333333333333);
169 bounding_rect.expand(next->get_vertex()+next->get_tangent2()*velocity);
171 bounding_rect.expand_x(gravity[0]);
172 bounding_rect.expand_y(gravity[1]);
173 bounding_rect.expand_x(size);
174 bounding_rect.expand_y(size);
180 Mutex::Lock lock(mutex);
181 if (!needs_sync_) return;
182 time_t start_time; time(&start_time);
183 particle_list.clear();
185 bounding_rect=Rect::zero();
187 // Bline must have at least 2 points in it
194 std::vector<synfig::BLinePoint>::const_iterator iter,next;
196 etl::hermite<Vector> curve;
198 Real step(abs(this->step));
204 if(bline_loop) iter=--bline.end(); // iter is the last bline in the list; next is the first bline in the list
205 else iter=next++; // iter is the first bline in the list; next is the second bline in the list
207 // loop through the bline; seg counts the blines as we do so; stop before iter is the last bline in the list
208 for(;next!=bline.end();iter=next++,seg++)
210 curve.p1()=iter->get_vertex();
211 curve.t1()=iter->get_tangent2();
212 curve.p2()=next->get_vertex();
213 curve.t2()=next->get_tangent1();
215 etl::derivative<etl::hermite<Vector> > deriv(curve);
219 int i=0, branch_count = 0, steps = round_to_int(1.0/step);
220 for(f=0.0;f<1.0;f+=step,i++)
222 Point point(curve(f));
224 particle_list.push_back(Particle(point, gradient(0)));
225 if (particle_list.size() % 1000000 == 0)
226 synfig::info("constructed %d million particles...", particle_list.size()/1000000);
228 bounding_rect.expand(point);
230 Real stunt_growth(random_factor * (random(Random::SMOOTH_COSINE,i,f+seg,0.0f,0.0f)/2.0+0.5));
231 stunt_growth*=stunt_growth;
233 if((((i+1)*sprouts + steps/2) / steps) > branch_count) {
234 Vector branch_velocity(deriv(f).norm()*velocity + deriv(f).perp().norm()*perp_velocity);
236 branch_velocity[0] += random_factor * random(Random::SMOOTH_COSINE, 1, f*splits, 0.0f, 0.0f);
237 branch_velocity[1] += random_factor * random(Random::SMOOTH_COSINE, 2, f*splits, 0.0f, 0.0f);
240 branch(i, 0, 0, // time
241 stunt_growth, // stunt growth
242 point, branch_velocity);
247 time_t end_time; time(&end_time);
248 if (end_time-start_time > 4)
249 synfig::info("Plant::sync() constructed %d particles in %d seconds\n",
250 particle_list.size(), int(end_time-start_time));
255 Plant::set_param(const String & param, const ValueBase &value)
257 if(param=="bline" && value.get_type()==ValueBase::TYPE_LIST)
260 bline_loop=value.get_loop();
265 if(param=="seed" && value.same_type_as(int()))
267 random.set_seed(value.get(int()));
271 IMPORT_PLUS(split_angle,needs_sync_=true);
272 IMPORT_PLUS(gravity,needs_sync_=true);
273 IMPORT_PLUS(gradient,needs_sync_=true);
274 IMPORT_PLUS(velocity,needs_sync_=true);
275 IMPORT_PLUS(perp_velocity,needs_sync_=true);
276 IMPORT_PLUS(step,needs_sync_=true);
277 IMPORT_PLUS(splits,needs_sync_=true);
278 IMPORT_PLUS(sprouts,needs_sync_=true);
279 IMPORT_PLUS(random_factor,needs_sync_=true);
280 IMPORT_PLUS(drag,needs_sync_=true);
282 IMPORT(size_as_alpha);
284 return Layer_Composite::set_param(param,value);
288 Plant::set_time(Context context, Time time)const
295 //const_cast<Plant*>(this)->sync();
296 context.set_time(time);
300 Plant::set_time(Context context, Time time, Vector pos)const
307 //const_cast<Plant*>(this)->sync();
308 context.set_time(time,pos);
312 Plant::get_param(const String& param)const
315 return random.get_seed();
320 EXPORT(perp_velocity);
325 EXPORT(random_factor);
329 EXPORT(size_as_alpha);
334 return Layer_Composite::get_param(param);
338 Plant::get_param_vocab()const
340 Layer::Vocab ret(Layer_Composite::get_param_vocab());
342 ret.push_back(ParamDesc("bline")
343 .set_local_name(_("Vertices"))
344 .set_description(_("A list of BLine Points"))
345 //.set_origin("offset")
346 //.set_scalar("width")
349 ret.push_back(ParamDesc("gradient")
350 .set_local_name(_("Gradient"))
351 .set_description(_("Gradient to be used for coloring the plant"))
354 ret.push_back(ParamDesc("split_angle")
355 .set_local_name(_("Split Angle"))
356 .set_description(_("Angle by which each split deviates from its parent"))
359 ret.push_back(ParamDesc("gravity")
360 .set_local_name(_("Gravity"))
361 .set_description(_("Direction in which the shoots tend to face"))
365 ret.push_back(ParamDesc("velocity")
366 .set_local_name(_("Tangential Velocity"))
367 .set_description(_("Amount to which shoots tend to grow along the tangent to the BLine"))
370 ret.push_back(ParamDesc("perp_velocity")
371 .set_local_name(_("Perpendicular Velocity"))
372 .set_description(_("Amount to which shoots tend to grow perpendicular to the tangent to the BLine"))
375 ret.push_back(ParamDesc("size")
376 .set_local_name(_("Stem Size"))
377 .set_description(_("Size of the stem"))
381 ret.push_back(ParamDesc("size_as_alpha")
382 .set_local_name(_("Size As Alpha"))
383 .set_description(_("If enabled, the alpha channel from the gradient is multiplied by the stem size, and an alpha of 1.0 is used when rendering"))
386 ret.push_back(ParamDesc("step")
387 .set_local_name(_("Step"))
388 .set_description(_("Measure of the distance between points when rendering"))
391 ret.push_back(ParamDesc("seed")
392 .set_local_name(_("Seed"))
393 .set_description(_("Used to seed the pseudo-random number generator"))
396 ret.push_back(ParamDesc("splits")
397 .set_local_name(_("Splits"))
398 .set_description(_("Maximum number of times that each sprout can sprout recursively"))
401 ret.push_back(ParamDesc("sprouts")
402 .set_local_name(_("Sprouts"))
403 .set_description(_("Number of places that growth occurs on each bline section"))
406 ret.push_back(ParamDesc("random_factor")
407 .set_local_name(_("Random Factor"))
408 .set_description(_("Used to scale down all random effects. Set to zero to disable randomness"))
411 ret.push_back(ParamDesc("drag")
412 .set_local_name(_("Drag"))
413 .set_description(_("Drag slows the growth"))
420 Plant::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
422 bool ret(context.accelerated_render(surface,quality,renddesc,cb));
423 if(is_disabled() || !ret)
426 Surface dest_surface;
427 dest_surface.set_wh(surface->get_w(),surface->get_h());
428 dest_surface.clear();
430 const Point tl(renddesc.get_tl());
431 const Point br(renddesc.get_br());
433 const int w(renddesc.get_w());
434 const int h(renddesc.get_h());
436 const int surface_width(surface->get_w());
437 const int surface_height(surface->get_h());
439 // Width and Height of a pixel
440 const Real pw = (br[0] - tl[0]) / w;
441 const Real ph = (br[1] - tl[1]) / h;
443 if(needs_sync_==true)
446 std::vector<Particle>::reverse_iterator iter;
448 float radius(size*sqrt(1.0f/(abs(pw)*abs(ph))));
451 for(iter=particle_list.rbegin();iter!=particle_list.rend();++iter)
453 float scaled_radius(radius);
454 Color color(iter->color);
457 scaled_radius*=color.get_a();
461 // previously, radius was multiplied by sqrt(step)*12 only if
462 // the radius came out at less than 1 (pixel):
463 // if (radius<=1.0f) radius*=sqrt(step)*12.0f;
464 // seems a little arbitrary - does it help?
466 // calculate the box that this particle will be drawn as
467 float x1f=(iter->point[0]-tl[0])/pw-(scaled_radius*0.5);
468 float x2f=(iter->point[0]-tl[0])/pw+(scaled_radius*0.5);
469 float y1f=(iter->point[1]-tl[1])/ph-(scaled_radius*0.5);
470 float y2f=(iter->point[1]-tl[1])/ph+(scaled_radius*0.5);
472 x2=ceil_to_int(x2f)-1;
474 y2=ceil_to_int(y2f)-1;
476 // if the box is entirely off the canvas, go to the next particle
477 if(x1>surface_width || y1>surface_height || x2<0 || y2<0) continue;
479 float x1e=x1-x1f, x2e=x2f-x2, y1e=y1-y1f, y2e=y2f-y2;
480 // printf("x1e %.4f x2e %.4f y1e %.4f y2e %.4f\n", x1e, x2e, y1e, y2e);
482 // adjust the box so it's entirely on the canvas
483 if(x1<=0) { x1=0; x1e=0; }
484 if(y1<=0) { y1=0; y1e=0; }
485 if(x2>=surface_width) { x2=surface_width; x2e=0; }
486 if(y2>=surface_height) { y2=surface_height; y2e=0; }
488 int w(x2-x1), h(y2-y1);
490 Surface::alpha_pen surface_pen(dest_surface.get_pen(x1,y1),1.0f);
492 dest_surface.fill(color,surface_pen,w,h);
494 /* the rectangle doesn't cross any vertical pixel boundaries so we don't
495 * need to draw any top or bottom edges
499 // case 1 - a single pixel
502 surface_pen.move_to(x2,y2);
503 surface_pen.set_alpha((x2f-x1f)*(y2f-y1f));
504 surface_pen.put_value(color);
506 // case 2 - a single vertical column of pixels
509 surface_pen.move_to(x2,y1-1);
510 if (y1e!=0) // maybe draw top pixel
512 surface_pen.set_alpha(y1e*(x2f-x1f));
513 surface_pen.put_value(color);
516 surface_pen.set_alpha(x2f-x1f);
517 for(int i=y1; i<y2; i++) // maybe draw pixels between
519 surface_pen.put_value(color);
522 if (y2e!=0) // maybe draw bottom pixel
524 surface_pen.set_alpha(y2e*(x2f-x1f));
525 surface_pen.put_value(color);
531 // case 3 - a single horizontal row of pixels
534 surface_pen.move_to(x1-1,y2);
535 if (x1e!=0) // maybe draw left pixel
537 surface_pen.set_alpha(x1e*(y2f-y1f));
538 surface_pen.put_value(color);
541 surface_pen.set_alpha(y2f-y1f);
542 for(int i=x1; i<x2; i++) // maybe draw pixels between
544 surface_pen.put_value(color);
547 if (x2e!=0) // maybe draw right pixel
549 surface_pen.set_alpha(x2e*(y2f-y1f));
550 surface_pen.put_value(color);
553 // case 4 - a proper block of pixels
556 if (x1e!=0) // maybe draw left edge
558 surface_pen.move_to(x1-1,y1-1);
559 if (y1e!=0) // maybe draw top left pixel
561 surface_pen.set_alpha(x1e*y1e);
562 surface_pen.put_value(color);
565 surface_pen.set_alpha(x1e);
566 for(int i=y1; i<y2; i++) // maybe draw pixels along the left edge
568 surface_pen.put_value(color);
571 if (y2e!=0) // maybe draw bottom left pixel
573 surface_pen.set_alpha(x1e*y2e);
574 surface_pen.put_value(color);
579 surface_pen.move_to(x1,y2);
581 if (y2e!=0) // maybe draw bottom edge
583 surface_pen.set_alpha(y2e);
584 for(int i=x1; i<x2; i++) // maybe draw pixels along the bottom edge
586 surface_pen.put_value(color);
589 if (x2e!=0) // maybe draw bottom right pixel
591 surface_pen.set_alpha(x2e*y2e);
592 surface_pen.put_value(color);
597 surface_pen.move_to(x2,y2-1);
599 if (x2e!=0) // maybe draw right edge
601 surface_pen.set_alpha(x2e);
602 for(int i=y1; i<y2; i++) // maybe draw pixels along the right edge
604 surface_pen.put_value(color);
607 if (y1e!=0) // maybe draw top right pixel
609 surface_pen.set_alpha(x2e*y1e);
610 surface_pen.put_value(color);
615 surface_pen.move_to(x2-1,y1-1);
617 if (y1e!=0) // maybe draw top edge
619 surface_pen.set_alpha(y1e);
620 for(int i=x1; i<x2; i++) // maybe draw pixels along the top edge
622 surface_pen.put_value(color);
630 Surface::alpha_pen pen(surface->get_pen(0,0),get_amount(),get_blend_method());
631 dest_surface.blit_to(pen);
637 Plant::get_bounding_rect(Context context)const
639 if(needs_sync_==true)
645 if(Color::is_onto(get_blend_method()))
646 return context.get_full_bounding_rect() & bounding_rect;
648 //if(get_blend_method()==Color::BLEND_BEHIND)
649 // return context.get_full_bounding_rect() | bounding_rect;
650 return bounding_rect;