1 /* === S Y N F I G ========================================================= */
3 ** \brief Implementation of the "Plant" layer
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 2007, 2008 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,N_("Plant"));
69 SYNFIG_LAYER_SET_CATEGORY(Plant,N_("Other"));
70 SYNFIG_LAYER_SET_VERSION(Plant,"0.2");
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 ======================================================= */
80 split_angle(Angle::deg(10)),
89 bounding_rect=Rect::zero();
91 random.set_seed(time(NULL));
93 bline.push_back(BLinePoint());
94 bline.push_back(BLinePoint());
95 bline.push_back(BLinePoint());
96 bline[0].set_vertex(Point(0,1));
97 bline[1].set_vertex(Point(0,-1));
98 bline[2].set_vertex(Point(1,0));
99 bline[0].set_tangent(bline[1].get_vertex()-bline[2].get_vertex()*0.5f);
100 bline[1].set_tangent(bline[2].get_vertex()-bline[0].get_vertex()*0.5f);
101 bline[2].set_tangent(bline[0].get_vertex()-bline[1].get_vertex()*0.5f);
102 bline[0].set_width(1.0f);
103 bline[1].set_width(1.0f);
104 bline[2].set_width(1.0f);
117 Plant::branch(int n,int depth,float t, float stunt_growth, synfig::Point position,synfig::Vector vel)const
119 float next_split((1.0-t)/(splits-depth)+t/*+random_factor*random(40+depth,t*splits,0,0)/splits*/);
120 for(;t<next_split;t+=step)
122 vel[0]+=gravity[0]*step;
123 vel[1]+=gravity[1]*step;
124 vel*=(1.0-(drag)*step);
125 position[0]+=vel[0]*step;
126 position[1]+=vel[1]*step;
128 particle_list.push_back(Particle(position, gradient(t)));
129 if (particle_list.size() % 1000000 == 0)
130 synfig::info("constructed %d million particles...", particle_list.size()/1000000);
132 bounding_rect.expand(position);
135 if(t>=1.0-stunt_growth)return;
137 synfig::Real sin_v=synfig::Angle::cos(split_angle).get();
138 synfig::Real cos_v=synfig::Angle::sin(split_angle).get();
140 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),
141 vel[0]*cos_v + vel[1]*sin_v + random_factor*random(Random::SMOOTH_COSINE, 32+n+depth, t*splits, 0.0f, 0.0f));
142 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),
143 -vel[0]*cos_v + vel[1]*sin_v + random_factor*random(Random::SMOOTH_COSINE, 33+n+depth, t*splits, 0.0f, 0.0f));
145 Plant::branch(n,depth+1,t,stunt_growth,position,velocity1);
146 Plant::branch(n,depth+1,t,stunt_growth,position,velocity2);
150 Plant::calc_bounding_rect()const
152 std::vector<synfig::BLinePoint>::const_iterator iter,next;
154 bounding_rect=Rect::zero();
156 // Bline must have at least 2 points in it
167 for(;next!=bline.end();iter=next++)
169 bounding_rect.expand(iter->get_vertex());
170 bounding_rect.expand(next->get_vertex());
171 bounding_rect.expand(iter->get_vertex()+iter->get_tangent2()*0.3333333333333);
172 bounding_rect.expand(next->get_vertex()-next->get_tangent1()*0.3333333333333);
173 bounding_rect.expand(next->get_vertex()+next->get_tangent2()*velocity);
175 bounding_rect.expand_x(gravity[0]);
176 bounding_rect.expand_y(gravity[1]);
177 bounding_rect.expand_x(size);
178 bounding_rect.expand_y(size);
184 Mutex::Lock lock(mutex);
185 if (!needs_sync_) return;
186 time_t start_time; time(&start_time);
187 particle_list.clear();
189 bounding_rect=Rect::zero();
191 // Bline must have at least 2 points in it
198 std::vector<synfig::BLinePoint>::const_iterator iter,next;
200 etl::hermite<Vector> curve;
202 Real step(abs(this->step));
208 if(bline_loop) iter=--bline.end(); // iter is the last bline in the list; next is the first bline in the list
209 else iter=next++; // iter is the first bline in the list; next is the second bline in the list
211 // loop through the bline; seg counts the blines as we do so; stop before iter is the last bline in the list
212 for(;next!=bline.end();iter=next++,seg++)
214 float iterw=iter->get_width(); // the width value of the iter vertex
215 float nextw=next->get_width(); // the width value of the next vertex
216 float width; // the width at an intermediate position
217 curve.p1()=iter->get_vertex();
218 curve.t1()=iter->get_tangent2();
219 curve.p2()=next->get_vertex();
220 curve.t2()=next->get_tangent1();
222 etl::derivative<etl::hermite<Vector> > deriv(curve);
226 int i=0, branch_count = 0, steps = round_to_int(1.0/step);
227 if (steps < 1) steps = 1;
228 for(f=0.0;f<1.0;f+=step,i++)
230 Point point(curve(f));
232 particle_list.push_back(Particle(point, gradient(0)));
233 if (particle_list.size() % 1000000 == 0)
234 synfig::info("constructed %d million particles...", particle_list.size()/1000000);
236 bounding_rect.expand(point);
238 Real stunt_growth(random_factor * (random(Random::SMOOTH_COSINE,i,f+seg,0.0f,0.0f)/2.0+0.5));
239 stunt_growth*=stunt_growth;
241 if((((i+1)*sprouts + steps/2) / steps) > branch_count) {
242 Vector branch_velocity(deriv(f).norm()*velocity + deriv(f).perp().norm()*perp_velocity);
244 if (isnan(branch_velocity[0]) || isnan(branch_velocity[1]))
247 branch_velocity[0] += random_factor * random(Random::SMOOTH_COSINE, 1, f*splits, 0.0f, 0.0f);
248 branch_velocity[1] += random_factor * random(Random::SMOOTH_COSINE, 2, f*splits, 0.0f, 0.0f);
252 width = iterw+(nextw-iterw)*f; // calculate the width based on the current position
254 branch_velocity[0] *= width; // scale the velocity accordingly to the current width
255 branch_velocity[1] *= width;
259 branch(i, 0, 0, // time
260 stunt_growth, // stunt growth
261 point, branch_velocity);
266 time_t end_time; time(&end_time);
267 if (end_time-start_time > 4)
268 synfig::info("Plant::sync() constructed %d particles in %d seconds\n",
269 particle_list.size(), int(end_time-start_time));
274 Plant::set_param(const String & param, const ValueBase &value)
276 if(param=="bline" && value.get_type()==ValueBase::TYPE_LIST)
279 bline_loop=value.get_loop();
284 if(param=="seed" && value.same_type_as(int()))
286 random.set_seed(value.get(int()));
291 IMPORT_PLUS(split_angle,needs_sync_=true);
292 IMPORT_PLUS(gravity,needs_sync_=true);
293 IMPORT_PLUS(gradient,needs_sync_=true);
294 IMPORT_PLUS(velocity,needs_sync_=true);
295 IMPORT_PLUS(perp_velocity,needs_sync_=true);
299 step=0.01; // user is probably clueless - give a good default
300 else if (step < 0.00001)
301 step=0.00001; // 100K should be enough for anyone
310 IMPORT_PLUS(sprouts,needs_sync_=true);
311 IMPORT_PLUS(random_factor,needs_sync_=true);
312 IMPORT_PLUS(drag,needs_sync_=true);
314 IMPORT(size_as_alpha);
318 IMPORT_AS(origin,"offset");
320 return Layer_Composite::set_param(param,value);
324 Plant::set_time(Context context, Time time)const
331 //const_cast<Plant*>(this)->sync();
332 context.set_time(time);
336 Plant::set_time(Context context, Time time, Vector pos)const
343 //const_cast<Plant*>(this)->sync();
344 context.set_time(time,pos);
348 Plant::get_param(const String& param)const
351 return random.get_seed();
357 EXPORT(perp_velocity);
362 EXPORT(random_factor);
365 EXPORT(size_as_alpha);
371 if(param=="Version" || param=="version" || param=="version__")
374 return Layer_Composite::get_param(param);
378 Plant::get_param_vocab()const
380 Layer::Vocab ret(Layer_Composite::get_param_vocab());
382 ret.push_back(ParamDesc("bline")
383 .set_local_name(_("Vertices"))
384 .set_description(_("A list of BLine Points"))
385 .set_origin("origin")
389 ret.push_back(ParamDesc("origin")
390 .set_local_name(_("Origin"))
393 ret.push_back(ParamDesc("gradient")
394 .set_local_name(_("Gradient"))
395 .set_description(_("Gradient to be used for coloring the plant"))
398 ret.push_back(ParamDesc("split_angle")
399 .set_local_name(_("Split Angle"))
400 .set_description(_("Angle by which each split deviates from its parent"))
403 ret.push_back(ParamDesc("gravity")
404 .set_local_name(_("Gravity"))
405 .set_description(_("Direction in which the shoots tend to face"))
409 ret.push_back(ParamDesc("velocity")
410 .set_local_name(_("Tangential Velocity"))
411 .set_description(_("Amount to which shoots tend to grow along the tangent to the BLine"))
414 ret.push_back(ParamDesc("perp_velocity")
415 .set_local_name(_("Perpendicular Velocity"))
416 .set_description(_("Amount to which shoots tend to grow perpendicular to the tangent to the BLine"))
419 ret.push_back(ParamDesc("size")
420 .set_local_name(_("Stem Size"))
421 .set_description(_("Size of the stem"))
425 ret.push_back(ParamDesc("size_as_alpha")
426 .set_local_name(_("Size As Alpha"))
427 .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"))
430 ret.push_back(ParamDesc("reverse")
431 .set_local_name(_("Reverse"))
432 .set_description(_("If enabled, render the plant in the opposite direction"))
435 ret.push_back(ParamDesc("step")
436 .set_local_name(_("Step"))
437 .set_description(_("Measure of the distance between points when rendering"))
440 ret.push_back(ParamDesc("seed")
441 .set_local_name(_("Seed"))
442 .set_description(_("Used to seed the pseudo-random number generator"))
445 ret.push_back(ParamDesc("splits")
446 .set_local_name(_("Splits"))
447 .set_description(_("Maximum number of times that each sprout can sprout recursively"))
450 ret.push_back(ParamDesc("sprouts")
451 .set_local_name(_("Sprouts"))
452 .set_description(_("Number of places that growth occurs on each bline section"))
455 ret.push_back(ParamDesc("random_factor")
456 .set_local_name(_("Random Factor"))
457 .set_description(_("Used to scale down all random effects. Set to zero to disable randomness"))
460 ret.push_back(ParamDesc("drag")
461 .set_local_name(_("Drag"))
462 .set_description(_("Drag slows the growth"))
465 ret.push_back(ParamDesc("use_width")
466 .set_local_name(_("Use Width"))
467 .set_description(_("Scale the velocity by the bline's width"))
474 Plant::set_version(const String &ver)
478 if (version == "0.1")
485 Plant::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
487 bool ret(context.accelerated_render(surface,quality,renddesc,cb));
488 if(is_disabled() || !ret)
491 Surface dest_surface;
492 dest_surface.set_wh(surface->get_w(),surface->get_h());
493 dest_surface.clear();
495 const Point tl(renddesc.get_tl()-origin);
496 const Point br(renddesc.get_br()-origin);
498 const int w(renddesc.get_w());
499 const int h(renddesc.get_h());
501 const int surface_width(surface->get_w());
502 const int surface_height(surface->get_h());
504 // Width and Height of a pixel
505 const Real pw = (br[0] - tl[0]) / w;
506 const Real ph = (br[1] - tl[1]) / h;
508 if (isinf(pw) || isinf(ph))
511 if(needs_sync_==true)
514 if (particle_list.begin() != particle_list.end())
516 std::vector<Particle>::iterator iter;
519 float radius(size*sqrt(1.0f/(abs(pw)*abs(ph))));
523 if (reverse) iter = particle_list.end();
524 else iter = particle_list.begin();
528 if (reverse) particle = &(*(iter-1));
529 else particle = &(*iter);
531 float scaled_radius(radius);
532 Color color(particle->color);
535 scaled_radius*=color.get_a();
539 // previously, radius was multiplied by sqrt(step)*12 only if
540 // the radius came out at less than 1 (pixel):
541 // if (radius<=1.0f) radius*=sqrt(step)*12.0f;
542 // seems a little arbitrary - does it help?
544 // calculate the box that this particle will be drawn as
545 float x1f=(particle->point[0]-tl[0])/pw-(scaled_radius*0.5);
546 float x2f=(particle->point[0]-tl[0])/pw+(scaled_radius*0.5);
547 float y1f=(particle->point[1]-tl[1])/ph-(scaled_radius*0.5);
548 float y2f=(particle->point[1]-tl[1])/ph+(scaled_radius*0.5);
550 x2=ceil_to_int(x2f)-1;
552 y2=ceil_to_int(y2f)-1;
554 // if the box isn't entirely off the canvas, draw it
555 if(x1<=surface_width && y1<=surface_height && x2>=0 && y2>=0)
557 float x1e=x1-x1f, x2e=x2f-x2, y1e=y1-y1f, y2e=y2f-y2;
558 // printf("x1e %.4f x2e %.4f y1e %.4f y2e %.4f\n", x1e, x2e, y1e, y2e);
560 // adjust the box so it's entirely on the canvas
561 if(x1<=0) { x1=0; x1e=0; }
562 if(y1<=0) { y1=0; y1e=0; }
563 if(x2>=surface_width) { x2=surface_width; x2e=0; }
564 if(y2>=surface_height) { y2=surface_height; y2e=0; }
566 int w(x2-x1), h(y2-y1);
568 Surface::alpha_pen surface_pen(dest_surface.get_pen(x1,y1),1.0f);
570 dest_surface.fill(color,surface_pen,w,h);
572 /* the rectangle doesn't cross any vertical pixel boundaries so we don't
573 * need to draw any top or bottom edges
577 // case 1 - a single pixel
580 surface_pen.move_to(x2,y2);
581 surface_pen.set_alpha((x2f-x1f)*(y2f-y1f));
582 surface_pen.put_value(color);
584 // case 2 - a single vertical column of pixels
587 surface_pen.move_to(x2,y1-1);
588 if (y1e!=0) // maybe draw top pixel
590 surface_pen.set_alpha(y1e*(x2f-x1f));
591 surface_pen.put_value(color);
594 surface_pen.set_alpha(x2f-x1f);
595 for(int i=y1; i<y2; i++) // maybe draw pixels between
597 surface_pen.put_value(color);
600 if (y2e!=0) // maybe draw bottom pixel
602 surface_pen.set_alpha(y2e*(x2f-x1f));
603 surface_pen.put_value(color);
609 // case 3 - a single horizontal row of pixels
612 surface_pen.move_to(x1-1,y2);
613 if (x1e!=0) // maybe draw left pixel
615 surface_pen.set_alpha(x1e*(y2f-y1f));
616 surface_pen.put_value(color);
619 surface_pen.set_alpha(y2f-y1f);
620 for(int i=x1; i<x2; i++) // maybe draw pixels between
622 surface_pen.put_value(color);
625 if (x2e!=0) // maybe draw right pixel
627 surface_pen.set_alpha(x2e*(y2f-y1f));
628 surface_pen.put_value(color);
631 // case 4 - a proper block of pixels
634 if (x1e!=0) // maybe draw left edge
636 surface_pen.move_to(x1-1,y1-1);
637 if (y1e!=0) // maybe draw top left pixel
639 surface_pen.set_alpha(x1e*y1e);
640 surface_pen.put_value(color);
643 surface_pen.set_alpha(x1e);
644 for(int i=y1; i<y2; i++) // maybe draw pixels along the left edge
646 surface_pen.put_value(color);
649 if (y2e!=0) // maybe draw bottom left pixel
651 surface_pen.set_alpha(x1e*y2e);
652 surface_pen.put_value(color);
657 surface_pen.move_to(x1,y2);
659 if (y2e!=0) // maybe draw bottom edge
661 surface_pen.set_alpha(y2e);
662 for(int i=x1; i<x2; i++) // maybe draw pixels along the bottom edge
664 surface_pen.put_value(color);
667 if (x2e!=0) // maybe draw bottom right pixel
669 surface_pen.set_alpha(x2e*y2e);
670 surface_pen.put_value(color);
675 surface_pen.move_to(x2,y2-1);
677 if (x2e!=0) // maybe draw right edge
679 surface_pen.set_alpha(x2e);
680 for(int i=y1; i<y2; i++) // maybe draw pixels along the right edge
682 surface_pen.put_value(color);
685 if (y1e!=0) // maybe draw top right pixel
687 surface_pen.set_alpha(x2e*y1e);
688 surface_pen.put_value(color);
693 surface_pen.move_to(x2-1,y1-1);
695 if (y1e!=0) // maybe draw top edge
697 surface_pen.set_alpha(y1e);
698 for(int i=x1; i<x2; i++) // maybe draw pixels along the top edge
700 surface_pen.put_value(color);
710 if (--iter == particle_list.begin())
715 if (++iter == particle_list.end())
721 Surface::alpha_pen pen(surface->get_pen(0,0),get_amount(),get_blend_method());
722 dest_surface.blit_to(pen);
728 Plant::get_bounding_rect(Context context)const
730 if(needs_sync_==true)
736 if(Color::is_onto(get_blend_method()))
737 return context.get_full_bounding_rect() & bounding_rect;
739 //if(get_blend_method()==Color::BLEND_BEHIND)
740 // return context.get_full_bounding_rect() | bounding_rect;
741 return bounding_rect;