1 /* === S I N F G =========================================================== */
5 ** $Id: plant.cpp,v 1.2 2005/01/13 06:48:39 darco Exp $
8 ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
10 ** This software and associated documentation
11 ** are CONFIDENTIAL and PROPRIETARY property of
12 ** the above-mentioned copyright holder.
14 ** You may not copy, print, publish, or in any
15 ** other way distribute this software without
16 ** a prior written agreement with
17 ** the copyright holder.
20 /* ========================================================================= */
22 /* === H E A D E R S ======================================================= */
31 #include <sinfg/angle.h>
33 #include <sinfg/string.h>
34 #include <sinfg/time.h>
35 #include <sinfg/context.h>
36 #include <sinfg/paramdesc.h>
37 #include <sinfg/renddesc.h>
38 #include <sinfg/surface.h>
39 #include <sinfg/value.h>
40 #include <sinfg/valuenode.h>
42 #include <ETL/calculus>
44 #include <ETL/hermite>
47 #include <sinfg/valuenode_bline.h>
53 /* === M A C R O S ========================================================= */
56 #define ROUND_END_FACTOR (4)
57 #define CUSP_THRESHOLD (0.15)
58 #define NO_LOOP_COOKIE sinfg::Vector(84951305,7836658)
59 #define EPSILON (0.000000001)
60 #define CUSP_TANGENT_ADJUST (0.025)
62 /* === G L O B A L S ======================================================= */
64 SINFG_LAYER_INIT(Plant);
65 SINFG_LAYER_SET_NAME(Plant,"plant");
66 SINFG_LAYER_SET_LOCAL_NAME(Plant,_("Plant"));
67 SINFG_LAYER_SET_CATEGORY(Plant,_("Particle Systems"));
68 SINFG_LAYER_SET_VERSION(Plant,"0.1");
69 SINFG_LAYER_SET_CVS_ID(Plant,"$Id: plant.cpp,v 1.2 2005/01/13 06:48:39 darco Exp $");
71 /* === P R O C E D U R E S ================================================= */
73 /* === M E T H O D S ======================================================= */
77 split_angle(Angle::deg(10)),
83 bounding_rect=Rect::zero();
85 random.set_seed(time(NULL));
87 bline.push_back(BLinePoint());
88 bline.push_back(BLinePoint());
89 bline.push_back(BLinePoint());
90 bline[0].set_vertex(Point(0,1));
91 bline[1].set_vertex(Point(0,-1));
92 bline[2].set_vertex(Point(1,0));
93 bline[0].set_tangent(bline[1].get_vertex()-bline[2].get_vertex()*0.5f);
94 bline[1].set_tangent(bline[2].get_vertex()-bline[0].get_vertex()*0.5f);
95 bline[2].set_tangent(bline[0].get_vertex()-bline[1].get_vertex()*0.5f);
96 bline[0].set_width(1.0f);
97 bline[1].set_width(1.0f);
98 bline[2].set_width(1.0f);
109 Plant::branch(int n,int depth,float t, float stunt_growth, sinfg::Point position,sinfg::Vector vel)const
111 float next_split((1.0-t)/(splits-depth)+t/*+random_factor*random(40+depth,t*splits,0,0)/splits*/);
112 for(;t<next_split;t+=step)
114 vel[0]+=gravity[0]*step;
115 vel[1]+=gravity[1]*step;
116 vel*=(1.0-(drag)*step);
117 position[0]+=vel[0]*step;
118 position[1]+=vel[1]*step;
120 particle_list.push_back(Particle(
124 bounding_rect.expand(position);
127 if(t>=1.0-stunt_growth)return;
129 sinfg::Real sin_v=sinfg::Angle::cos(split_angle).get();
130 sinfg::Real cos_v=sinfg::Angle::sin(split_angle).get();
132 sinfg::Vector velocity1(vel[0]*sin_v-vel[1]*cos_v+random_factor*random(2,30+n+depth,t*splits,0.0f,0.0f),vel[0]*cos_v+vel[1]*sin_v+random_factor*random(2,32+n+depth,t*splits,0.0f,0.0f));
133 sinfg::Vector velocity2(vel[0]*sin_v+vel[1]*cos_v+random_factor*random(2,31+n+depth,t*splits,0.0f,0.0f),-vel[0]*cos_v+vel[1]*sin_v+random_factor*random(2,33+n+depth,t*splits,0.0f,0.0f));
135 Plant::branch(n,depth+1,t,stunt_growth,position,velocity1);
136 Plant::branch(n,depth+1,t,stunt_growth,position,velocity2);
140 Plant::calc_bounding_rect()const
142 std::vector<sinfg::BLinePoint>::const_iterator iter,next;
144 bounding_rect=Rect::zero();
146 // Bline must have at least 2 points in it
157 for(;next!=bline.end();iter=next++)
159 bounding_rect.expand(iter->get_vertex());
160 bounding_rect.expand(next->get_vertex());
161 bounding_rect.expand(iter->get_vertex()+iter->get_tangent2()*0.3333333333333);
162 bounding_rect.expand(next->get_vertex()-next->get_tangent1()*0.3333333333333);
163 bounding_rect.expand(next->get_vertex()+next->get_tangent2()*velocity);
165 bounding_rect.expand_x(gravity[0]);
166 bounding_rect.expand_y(gravity[1]);
167 bounding_rect.expand_x(size);
168 bounding_rect.expand_y(size);
174 particle_list.clear();
176 bounding_rect=Rect::zero();
178 // Bline must have at least 2 points in it
182 std::vector<sinfg::BLinePoint>::const_iterator iter,next;
184 etl::hermite<Vector> curve;
186 Real step(abs(this->step));
197 for(;next!=bline.end();iter=next++,seg++)
199 curve.p1()=iter->get_vertex();
200 curve.t1()=iter->get_tangent2();
201 curve.p2()=next->get_vertex();
202 curve.t2()=next->get_tangent1();
204 etl::derivative<etl::hermite<Vector> > deriv(curve);
207 int i(0), b(round_to_int((1.0/step)/(float)sprouts-1));
209 for(f=0.0;f<1.0;f+=step,i++)
211 Point point(curve(f));
213 particle_list.push_back(Particle(
218 bounding_rect.expand(point);
220 Real stunt_growth(random(2,i,f+seg,0.0f,0.0f)/2.0+0.5);
221 stunt_growth*=stunt_growth;
223 Vector branch_velocity(deriv(f).norm()*velocity);
225 branch_velocity[0]+=random_factor*random(2,1,f*splits,0.0f,0.0f);
226 branch_velocity[1]+=random_factor*random(2,2,f*splits,0.0f,0.0f);
233 stunt_growth, // stunt growth
234 point,branch_velocity
243 Plant::set_param(const String & param, const ValueBase &value)
245 if(param=="bline" && value.get_type()==ValueBase::TYPE_LIST)
248 bline_loop=value.get_loop();
253 if(param=="seed" && value.same_as(int()))
255 random.set_seed(value.get(int()));
259 IMPORT_PLUS(split_angle,needs_sync_=true);
260 IMPORT_PLUS(gravity,needs_sync_=true);
261 IMPORT_PLUS(gradient,needs_sync_=true);
262 IMPORT_PLUS(velocity,needs_sync_=true);
263 IMPORT_PLUS(step,needs_sync_=true);
264 IMPORT_PLUS(splits,needs_sync_=true);
265 IMPORT_PLUS(sprouts,needs_sync_=true);
266 IMPORT_PLUS(random_factor,needs_sync_=true);
267 IMPORT_PLUS(drag,needs_sync_=true);
269 IMPORT(size_as_alpha);
271 return Layer_Composite::set_param(param,value);
275 Plant::set_time(Context context, Time time)const
282 //const_cast<Plant*>(this)->sync();
283 context.set_time(time);
287 Plant::set_time(Context context, Time time, Vector pos)const
294 //const_cast<Plant*>(this)->sync();
295 context.set_time(time,pos);
299 Plant::get_param(const String& param)const
302 return random.get_seed();
311 EXPORT(random_factor);
315 EXPORT(size_as_alpha);
320 return Layer_Composite::get_param(param);
324 Plant::get_param_vocab()const
326 Layer::Vocab ret(Layer_Composite::get_param_vocab());
328 ret.push_back(ParamDesc("bline")
329 .set_local_name(_("Vertices"))
330 //.set_origin("offset")
331 //.set_scalar("width")
332 .set_description(_("A list of BLine Points"))
335 ret.push_back(ParamDesc("gradient")
336 .set_local_name(_("Gradient"))
339 ret.push_back(ParamDesc("split_angle")
340 .set_local_name(_("Split Angle"))
343 ret.push_back(ParamDesc("gravity")
344 .set_local_name(_("Gravity"))
348 ret.push_back(ParamDesc("velocity")
349 .set_local_name(_("Velocity"))
352 ret.push_back(ParamDesc("size")
353 .set_local_name(_("Stem Size"))
357 ret.push_back(ParamDesc("size_as_alpha")
358 .set_local_name(_("SizeAsAlpha"))
361 ret.push_back(ParamDesc("step")
362 .set_local_name(_("Step"))
365 ret.push_back(ParamDesc("seed")
366 .set_local_name(_("Seed"))
369 ret.push_back(ParamDesc("splits")
370 .set_local_name(_("Splits"))
373 ret.push_back(ParamDesc("sprouts")
374 .set_local_name(_("Sprouts"))
377 ret.push_back(ParamDesc("random_factor")
378 .set_local_name(_("Random Factor"))
381 ret.push_back(ParamDesc("drag")
382 .set_local_name(_("Drag"))
390 Plant::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
392 bool ret(context.accelerated_render(surface,quality,renddesc,cb));
393 if(is_disabled() || !ret)
396 Surface dest_surface;
397 dest_surface.set_wh(surface->get_w(),surface->get_h());
398 dest_surface.clear();
400 const Point tl(renddesc.get_tl());
401 const Point br(renddesc.get_br());
403 const int w(renddesc.get_w());
404 const int h(renddesc.get_h());
406 // Width and Height of a pixel
407 const Real pw = (br[0] - tl[0]) / w;
408 const Real ph = (br[1] - tl[1]) / h;
410 if(needs_sync_==true)
413 std::vector<Particle>::reverse_iterator iter;
414 const float size_factor(1);
415 float radius(size_factor*size*sqrt(1.0f/(abs(pw)*abs(ph))));
421 for(iter=particle_list.rbegin();iter!=particle_list.rend();++iter)
423 float radius(radius);
424 Color color(iter->color);
427 radius*=color.get_a();
431 x1=ceil_to_int((iter->point[0]-tl[0])/pw-(radius*0.5));
432 y1=ceil_to_int((iter->point[1]-tl[1])/ph-(radius*0.5));
433 x2=x1+round_to_int(radius);
434 y2=y1+round_to_int(radius);
436 if(x1>=surface->get_w() || y1>=surface->get_h())
442 if(x2>=surface->get_w())
444 if(y2>=surface->get_h())
452 int w(min(round_to_int(radius),x2-x1));
453 int h(min(round_to_int(radius),y2-y1));
458 Surface::alpha_pen surface_pen(dest_surface.get_pen(x1,y1),1.0f);
460 dest_surface.fill(color,surface_pen,w,h);
466 radius*=sqrt(step)*12.0f;
469 for(iter=particle_list.rbegin();iter!=particle_list.rend();++iter)
471 float radius(radius);
472 Color color(iter->color);
475 radius*=color.get_a();
479 x=floor_to_int((iter->point[0]-tl[0])/pw-0.5f);
480 y=floor_to_int((iter->point[1]-tl[1])/ph-0.5f);
482 if(x>=surface->get_w()-1 || y>=surface->get_h()-1 || x<0 || y<0)
487 a=((iter->point[0]-tl[0])/pw-0.5f-x)*radius;
488 b=((iter->point[1]-tl[1])/ph-0.5f-y)*radius;
492 Surface::alpha_pen surface_pen(dest_surface.get_pen(x,y),1.0f);
494 surface_pen.set_alpha(c*d);
495 surface_pen.put_value(color);
497 surface_pen.set_alpha(a*d);
498 surface_pen.put_value(color);
500 surface_pen.set_alpha(a*b);
501 surface_pen.put_value(color);
503 surface_pen.set_alpha(c*b);
504 surface_pen.put_value(color);
508 Surface::alpha_pen pen(surface->get_pen(0,0),get_amount(),get_blend_method());
509 dest_surface.blit_to(pen);
515 Plant::get_bounding_rect(Context context)const
517 if(needs_sync_==true)
523 if(Color::is_onto(get_blend_method()))
524 return context.get_full_bounding_rect() & bounding_rect;
526 //if(get_blend_method()==Color::BLEND_BEHIND)
527 // return context.get_full_bounding_rect() | bounding_rect;
528 return bounding_rect;