Merge branch 'genete_static_values'
[synfig.git] / synfig-core / src / modules / mod_example / metaballs.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file metaballs.cpp
3 **      \brief Implementation of the "Metaballs" layer
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **
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.
14 **
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.
19 **      \endlegal
20 */
21 /* ========================================================================= */
22
23 /* === H E A D E R S ======================================================= */
24
25 #ifdef USING_PCH
26 #       include "pch.h"
27 #else
28 #ifdef HAVE_CONFIG_H
29 #       include <config.h>
30 #endif
31
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>
40 #include <ETL/pen>
41
42 #include "metaballs.h"
43
44 #endif
45
46 /* === U S I N G =========================================================== */
47
48 using namespace etl;
49 using namespace std;
50 using namespace synfig;
51
52 /* === G L O B A L S ======================================================= */
53
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$");
60
61 /* === P R O C E D U R E S ================================================= */
62
63 /* === M E T H O D S ======================================================= */
64
65 /* === E N T R Y P O I N T ================================================= */
66
67 Metaballs::Metaballs():
68         Layer_Composite(1.0,Color::BLEND_COMPOSITE),
69         gradient(Color::black(), Color::white()),
70         threshold(0),
71         threshold2(1),
72         positive(false)
73 {
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);
77         Layer::Vocab voc(get_param_vocab());
78         Layer::fill_static(voc);
79 }
80
81 bool
82 Metaballs::set_param(const String & param, const ValueBase &value)
83 {
84         if(     param=="centers" && value.same_type_as(centers))
85         {
86                 centers = value;
87                 return true;
88         }
89
90         if(     param=="weights" && value.same_type_as(weights))
91         {
92                 weights = value;
93                 return true;
94         }
95
96         if(     param=="radii" && value.same_type_as(radii))
97         {
98                 radii = value;
99                 return true;
100         }
101
102         IMPORT(gradient);
103         IMPORT(threshold);
104         IMPORT(threshold2);
105         IMPORT(positive);
106
107         return Layer_Composite::set_param(param,value);
108 }
109
110 ValueBase
111 Metaballs::get_param(const String &param)const
112 {
113         EXPORT(gradient);
114
115         EXPORT(radii);
116         EXPORT(weights);
117         EXPORT(centers);
118         EXPORT(threshold);
119         EXPORT(threshold2);
120         EXPORT(positive);
121
122         EXPORT_NAME();
123         EXPORT_VERSION();
124
125         return Layer_Composite::get_param(param);
126 }
127
128 Layer::Vocab
129 Metaballs::get_param_vocab()const
130 {
131         Layer::Vocab ret(Layer_Composite::get_param_vocab());
132
133         ret.push_back(ParamDesc("gradient")
134                 .set_local_name(_("Gradient"))
135         );
136
137         ret.push_back(ParamDesc("centers")
138                 .set_local_name(_("Balls"))
139         );
140
141         ret.push_back(ParamDesc("radii")
142                 .set_local_name(_("Radii"))
143         );
144
145         ret.push_back(ParamDesc("weights")
146                 .set_local_name(_("Weights"))
147         );
148
149         ret.push_back(ParamDesc("threshold")
150                 .set_local_name(_("Gradient Left"))
151         );
152
153         ret.push_back(ParamDesc("threshold2")
154                 .set_local_name(_("Gradient Right"))
155         );
156
157         ret.push_back(ParamDesc("positive")
158                 .set_local_name(_("Positive Only"))
159         );
160
161         return ret;
162 }
163
164 synfig::Layer::Handle
165 Metaballs::hit_check(synfig::Context context, const synfig::Point &point)const
166 {
167         Real density(totaldensity(point));
168
169         if (density <= 0 || density > 1 || get_amount() == 0)
170                 return context.hit_check(point);
171
172         synfig::Layer::Handle tmp;
173
174         if (get_blend_method()==Color::BLEND_BEHIND && (tmp=context.hit_check(point)))
175                 return tmp;
176
177         if (Color::is_onto(get_blend_method()) && !(context.hit_check(point)))
178                 return 0;
179
180         return const_cast<Metaballs*>(this);
181 }
182
183 Real
184 Metaballs::densityfunc(const synfig::Point &p, const synfig::Point &c, Real R)const
185 {
186         const Real dx = p[0] - c[0];
187         const Real dy = p[1] - c[1];
188
189         const Real n = (1 - (dx*dx + dy*dy)/(R*R));
190         if (positive && n < 0) return 0;
191         return (n*n*n);
192
193         /*
194         f(d) = (1 - d^2)^3
195         f'(d) = -6d * (1 - d^2)^2
196
197         could use this too...
198         f(d) = (1 - d^2)^2
199         f'(d) = -6d * (1 - d^2)
200         */
201 }
202
203 Real
204 Metaballs::totaldensity(const Point &pos)const
205 {
206         Real density = 0;
207
208         //sum up weighted functions
209         for(unsigned int i=0;i<centers.size();i++)
210                 density += weights[i] * densityfunc(pos,centers[i], radii[i]);
211
212         return (density - threshold) / (threshold2 - threshold);
213 }
214
215 Color
216 Metaballs::get_color(Context context, const Point &pos)const
217 {
218         if(get_amount()==1.0 && get_blend_method()==Color::BLEND_STRAIGHT)
219                 return gradient(totaldensity(pos));
220         else
221                 return Color::blend(gradient(totaldensity(pos)),context.get_color(pos),get_amount(),get_blend_method());
222 }
223
224 bool
225 Metaballs::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
226 {
227         // Width and Height of a pixel
228         const Point br(renddesc.get_br()), tl(renddesc.get_tl());
229         const int        w(renddesc.get_w()),   h(renddesc.get_h());
230         const Real      pw(renddesc.get_pw()), ph(renddesc.get_ph());
231
232         SuperCallback supercb(cb,0,9000,10000);
233
234         Point pos(tl[0],tl[1]);
235
236         if(!context.accelerated_render(surface,quality,renddesc,&supercb))
237         {
238                 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
239                 return false;
240         }
241
242         for(int y = 0; y < h; y++, pos[1] += ph)
243         {
244                 pos[0] = tl[0];
245                 for(int x = 0; x < w; x++, pos[0] += pw)
246                         (*surface)[y][x] = Color::blend(gradient(totaldensity(pos)),(*surface)[y][x],get_amount(),get_blend_method());
247         }
248
249         // Mark our progress as finished
250         if(cb && !cb->amount_complete(10000,10000))
251                 return false;
252
253         return true;
254 }