1 /* === S Y N F I G ========================================================= */
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
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.
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.
21 /* ========================================================================= */
23 /* === H E A D E R S ======================================================= */
32 #include <synfig/angle.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>
43 #include <ETL/calculus>
45 #include <ETL/hermite>
48 #include <synfig/valuenode_bline.h>
54 /* === M A C R O S ========================================================= */
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)
63 /* === G L O B A L S ======================================================= */
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$");
72 /* === P R O C E D U R E S ================================================= */
74 /* === M E T H O D S ======================================================= */
78 split_angle(Angle::deg(10)),
84 bounding_rect=Rect::zero();
86 random.set_seed(time(NULL));
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);
111 Plant::branch(int n,int depth,float t, float stunt_growth, synfig::Point position,synfig::Vector vel)const
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)
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;
122 particle_list.push_back(Particle(
126 bounding_rect.expand(position);
129 if(t>=1.0-stunt_growth)return;
131 synfig::Real sin_v=synfig::Angle::cos(split_angle).get();
132 synfig::Real cos_v=synfig::Angle::sin(split_angle).get();
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));
139 Plant::branch(n,depth+1,t,stunt_growth,position,velocity1);
140 Plant::branch(n,depth+1,t,stunt_growth,position,velocity2);
144 Plant::calc_bounding_rect()const
146 std::vector<synfig::BLinePoint>::const_iterator iter,next;
148 bounding_rect=Rect::zero();
150 // Bline must have at least 2 points in it
161 for(;next!=bline.end();iter=next++)
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);
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);
178 Mutex::Lock lock(mutex);
179 if (!needs_sync_) return;
180 particle_list.clear();
182 bounding_rect=Rect::zero();
184 // Bline must have at least 2 points in it
188 std::vector<synfig::BLinePoint>::const_iterator iter,next;
190 etl::hermite<Vector> curve;
192 Real step(abs(this->step));
203 for(;next!=bline.end();iter=next++,seg++)
205 curve.p1()=iter->get_vertex();
206 curve.t1()=iter->get_tangent2();
207 curve.p2()=next->get_vertex();
208 curve.t2()=next->get_tangent1();
210 etl::derivative<etl::hermite<Vector> > deriv(curve);
213 int i(0), b(round_to_int((1.0/step)/(float)sprouts-1));
215 for(f=0.0;f<1.0;f+=step,i++)
217 Point point(curve(f));
219 particle_list.push_back(Particle(
224 bounding_rect.expand(point);
226 Real stunt_growth(random(Random::SMOOTH_COSINE,i,f+seg,0.0f,0.0f)/2.0+0.5);
227 stunt_growth*=stunt_growth;
229 Vector branch_velocity(deriv(f).norm()*velocity);
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);
239 stunt_growth, // stunt growth
240 point,branch_velocity
249 Plant::set_param(const String & param, const ValueBase &value)
251 if(param=="bline" && value.get_type()==ValueBase::TYPE_LIST)
254 bline_loop=value.get_loop();
259 if(param=="seed" && value.same_type_as(int()))
261 random.set_seed(value.get(int()));
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);
275 IMPORT(size_as_alpha);
277 return Layer_Composite::set_param(param,value);
281 Plant::set_time(Context context, Time time)const
288 //const_cast<Plant*>(this)->sync();
289 context.set_time(time);
293 Plant::set_time(Context context, Time time, Vector pos)const
300 //const_cast<Plant*>(this)->sync();
301 context.set_time(time,pos);
305 Plant::get_param(const String& param)const
308 return random.get_seed();
317 EXPORT(random_factor);
321 EXPORT(size_as_alpha);
326 return Layer_Composite::get_param(param);
330 Plant::get_param_vocab()const
332 Layer::Vocab ret(Layer_Composite::get_param_vocab());
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"))
341 ret.push_back(ParamDesc("gradient")
342 .set_local_name(_("Gradient"))
345 ret.push_back(ParamDesc("split_angle")
346 .set_local_name(_("Split Angle"))
349 ret.push_back(ParamDesc("gravity")
350 .set_local_name(_("Gravity"))
354 ret.push_back(ParamDesc("velocity")
355 .set_local_name(_("Velocity"))
358 ret.push_back(ParamDesc("size")
359 .set_local_name(_("Stem Size"))
363 ret.push_back(ParamDesc("size_as_alpha")
364 .set_local_name(_("Size As Alpha"))
367 ret.push_back(ParamDesc("step")
368 .set_local_name(_("Step"))
371 ret.push_back(ParamDesc("seed")
372 .set_local_name(_("Seed"))
375 ret.push_back(ParamDesc("splits")
376 .set_local_name(_("Splits"))
379 ret.push_back(ParamDesc("sprouts")
380 .set_local_name(_("Sprouts"))
383 ret.push_back(ParamDesc("random_factor")
384 .set_local_name(_("Random Factor"))
387 ret.push_back(ParamDesc("drag")
388 .set_local_name(_("Drag"))
396 Plant::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
398 bool ret(context.accelerated_render(surface,quality,renddesc,cb));
399 if(is_disabled() || !ret)
402 Surface dest_surface;
403 dest_surface.set_wh(surface->get_w(),surface->get_h());
404 dest_surface.clear();
406 const Point tl(renddesc.get_tl());
407 const Point br(renddesc.get_br());
409 const int w(renddesc.get_w());
410 const int h(renddesc.get_h());
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;
416 if(needs_sync_==true)
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;
427 for(iter=particle_list.rbegin();iter!=particle_list.rend();++iter)
429 temp_radius = radius;
430 float radius(temp_radius);
431 Color color(iter->color);
434 radius*=color.get_a();
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);
443 if(x1>=surface->get_w() || y1>=surface->get_h())
449 if(x2>=surface->get_w())
451 if(y2>=surface->get_h())
459 int w(min(round_to_int(radius),x2-x1));
460 int h(min(round_to_int(radius),y2-y1));
465 Surface::alpha_pen surface_pen(dest_surface.get_pen(x1,y1),1.0f);
467 dest_surface.fill(color,surface_pen,w,h);
473 radius*=sqrt(step)*12.0f;
476 for(iter=particle_list.rbegin();iter!=particle_list.rend();++iter)
478 temp_radius = radius;
479 float radius(temp_radius);
480 Color color(iter->color);
483 radius*=color.get_a();
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);
490 if(x>=surface->get_w()-1 || y>=surface->get_h()-1 || x<0 || y<0)
495 a=((iter->point[0]-tl[0])/pw-0.5f-x)*radius;
496 b=((iter->point[1]-tl[1])/ph-0.5f-y)*radius;
500 Surface::alpha_pen surface_pen(dest_surface.get_pen(x,y),1.0f);
502 surface_pen.set_alpha(c*d);
503 surface_pen.put_value(color);
505 surface_pen.set_alpha(a*d);
506 surface_pen.put_value(color);
508 surface_pen.set_alpha(a*b);
509 surface_pen.put_value(color);
511 surface_pen.set_alpha(c*b);
512 surface_pen.put_value(color);
516 Surface::alpha_pen pen(surface->get_pen(0,0),get_amount(),get_blend_method());
517 dest_surface.blit_to(pen);
523 Plant::get_bounding_rect(Context context)const
525 if(needs_sync_==true)
531 if(Color::is_onto(get_blend_method()))
532 return context.get_full_bounding_rect() & bounding_rect;
534 //if(get_blend_method()==Color::BLEND_BEHIND)
535 // return context.get_full_bounding_rect() | bounding_rect;
536 return bounding_rect;