1 /* === S Y N F I G ========================================================= */
2 /*! \file metaballs.cpp
3 ** \brief Implementation of the "Metaballs" layer
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/string.h>
33 #include <synfig/time.h>
34 #include <synfig/context.h>
35 #include <synfig/paramdesc.h>
36 #include <synfig/renddesc.h>
37 #include <synfig/surface.h>
38 #include <synfig/value.h>
39 #include <synfig/valuenode.h>
42 #include "metaballs.h"
46 /* === U S I N G =========================================================== */
50 using namespace synfig;
52 /* === G L O B A L S ======================================================= */
54 SYNFIG_LAYER_INIT(Metaballs);
55 SYNFIG_LAYER_SET_NAME(Metaballs,"metaballs");
56 SYNFIG_LAYER_SET_LOCAL_NAME(Metaballs,N_("Metaballs"));
57 SYNFIG_LAYER_SET_CATEGORY(Metaballs,N_("Example"));
58 SYNFIG_LAYER_SET_VERSION(Metaballs,"0.1");
59 SYNFIG_LAYER_SET_CVS_ID(Metaballs,"$Id$");
61 /* === P R O C E D U R E S ================================================= */
63 /* === M E T H O D S ======================================================= */
65 /* === E N T R Y P O I N T ================================================= */
67 Metaballs::Metaballs():
68 Layer_Composite(1.0,Color::BLEND_STRAIGHT),
69 gradient(Color::black(), Color::white()),
74 centers.push_back(Point( 0, -1.5)); radii.push_back(2.5); weights.push_back(1);
75 centers.push_back(Point(-2, 1)); radii.push_back(2.5); weights.push_back(1);
76 centers.push_back(Point( 2, 1)); radii.push_back(2.5); weights.push_back(1);
80 Metaballs::set_param(const String & param, const ValueBase &value)
82 if( param=="centers" && value.same_type_as(centers))
88 if( param=="weights" && value.same_type_as(weights))
94 if( param=="radii" && value.same_type_as(radii))
105 return Layer_Composite::set_param(param,value);
109 Metaballs::get_param(const String ¶m)const
123 return Layer_Composite::get_param(param);
127 Metaballs::get_param_vocab()const
129 Layer::Vocab ret(Layer_Composite::get_param_vocab());
131 ret.push_back(ParamDesc("gradient")
132 .set_local_name(_("Gradient"))
135 ret.push_back(ParamDesc("centers")
136 .set_local_name(_("Points"))
139 ret.push_back(ParamDesc("radii")
140 .set_local_name(_("Radii"))
143 ret.push_back(ParamDesc("weights")
144 .set_local_name(_("Weights"))
147 ret.push_back(ParamDesc("threshold")
148 .set_local_name(_("Gradient Left"))
151 ret.push_back(ParamDesc("threshold2")
152 .set_local_name(_("Gradient Right"))
155 ret.push_back(ParamDesc("positive")
156 .set_local_name(_("Positive Only"))
162 synfig::Layer::Handle
163 Metaballs::hit_check(synfig::Context context, const synfig::Point &point)const
165 Real density(totaldensity(point));
167 if (density <= 0 || density > 1 || get_amount() == 0)
168 return context.hit_check(point);
170 synfig::Layer::Handle tmp;
172 if (get_blend_method()==Color::BLEND_BEHIND && (tmp=context.hit_check(point)))
175 if (Color::is_onto(get_blend_method()) && !(context.hit_check(point)))
178 return const_cast<Metaballs*>(this);
182 Metaballs::densityfunc(const synfig::Point &p, const synfig::Point &c, Real R)const
184 const Real dx = p[0] - c[0];
185 const Real dy = p[1] - c[1];
187 const Real n = (1 - (dx*dx + dy*dy)/(R*R));
188 if (positive && n < 0) return 0;
193 f'(d) = -6d * (1 - d^2)^2
195 could use this too...
197 f'(d) = -6d * (1 - d^2)
202 Metaballs::totaldensity(const Point &pos)const
206 //sum up weighted functions
207 for(unsigned int i=0;i<centers.size();i++)
208 density += weights[i] * densityfunc(pos,centers[i], radii[i]);
210 return (density - threshold) / (threshold2 - threshold);
214 Metaballs::get_color(Context context, const Point &pos)const
216 if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
217 return gradient(totaldensity(pos));
219 return Color::blend(gradient(totaldensity(pos)),context.get_color(pos),get_amount(),get_blend_method());
223 Metaballs::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
225 // Width and Height of a pixel
226 const Point br(renddesc.get_br()), tl(renddesc.get_tl());
227 const int w(renddesc.get_w()), h(renddesc.get_h());
228 const Real pw(renddesc.get_pw()), ph(renddesc.get_ph());
230 SuperCallback supercb(cb,0,9000,10000);
232 Point pos(tl[0],tl[1]);
234 if(!context.accelerated_render(surface,quality,renddesc,&supercb))
236 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
240 for(int y = 0; y < h; y++, pos[1] += ph)
243 for(int x = 0; x < w; x++, pos[0] += pw)
244 (*surface)[y][x] = Color::blend(gradient(totaldensity(pos)),(*surface)[y][x],get_amount(),get_blend_method());
247 // Mark our progress as finished
248 if(cb && !cb->amount_complete(10000,10000))