Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / 0.61.09 / src / modules / 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_("Default"));
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_STRAIGHT),
69         color(Color::black())
70 {
71 }
72
73 bool
74 Metaballs::set_param(const String & param, const ValueBase &value)
75 {
76         if(     param=="centers" && value.same_type_as(centers))
77         {
78                 centers = value;
79                 return true;
80         }
81
82         if(     param=="weights" && value.same_type_as(weights))
83         {
84                 weights = value;
85                 return true;
86         }
87
88         if(     param=="radii" && value.same_type_as(radii))
89         {
90                 radii = value;
91                 return true;
92         }
93
94         IMPORT(color);
95         IMPORT(threshold);
96
97         return Layer_Composite::set_param(param,value);
98 }
99
100 ValueBase
101 Metaballs::get_param(const String &param)const
102 {
103         EXPORT(color);
104
105         EXPORT(radii);
106         EXPORT(weights);
107         EXPORT(centers);
108         EXPORT(threshold);
109
110         EXPORT_NAME();
111         EXPORT_VERSION();
112
113         return Layer_Composite::get_param(param);
114 }
115
116 Layer::Vocab
117 Metaballs::get_param_vocab()const
118 {
119         Layer::Vocab ret(Layer_Composite::get_param_vocab());
120
121         ret.push_back(ParamDesc("color")
122                 .set_local_name(_("Color"))
123         );
124
125         ret.push_back(ParamDesc("centers")
126                 .set_local_name(_("Points"))
127         );
128
129         ret.push_back(ParamDesc("radii")
130                 .set_local_name(_("Radii"))
131         );
132
133         ret.push_back(ParamDesc("weights")
134                 .set_local_name(_("Weights"))
135         );
136
137         ret.push_back(ParamDesc("threshold")
138                 .set_local_name(_("Threshold"))
139         );
140
141         return ret;
142 }
143
144 static inline Real densityfunc(const synfig::Point &p, const synfig::Point &c, Real R)
145 {
146         const Real dx = p[0] - c[0];
147         const Real dy = p[1] - c[1];
148
149         const Real n = (1 - (dx*dx + dy*dy)/(R*R));
150         return (n*n*n);
151
152         /*
153         f(d) = (1 - d^2)^3
154         f'(d) = -6d * (1 - d^2)^2
155
156         could use this too...
157         f(d) = (1 - d^2)^2
158         f'(d) = -6d * (1 - d^2)
159         */
160 }
161
162 Real
163 Metaballs::totaldensity(const Point &pos) const
164 {
165         Real density = 0;
166
167         //sum up weighted functions
168         for(unsigned int i=0;i<centers.size();i++)
169         {
170                 density += weights[i] * densityfunc(pos,centers[i], radii[i]);
171         }
172
173         return density;
174 }
175
176 Color
177 Metaballs::get_color(Context context, const Point &pos)const
178 {
179         Real dens = totaldensity(pos);
180
181         if(dens >= threshold)
182                 return color;
183         else
184                 return context.get_color(pos);
185 }
186
187 bool
188 Metaballs::accelerated_render(Context context,Surface *surface,int quality, const RendDesc &renddesc, ProgressCallback *cb)const
189 {
190         // Width and Height of a pixel
191         const Point br(renddesc.get_br()),
192                                 tl(renddesc.get_tl());
193
194         const int       w = renddesc.get_w(),
195                                 h = renddesc.get_h();
196
197         Real    pw = renddesc.get_pw();
198         Real    ph = renddesc.get_ph();
199
200         SuperCallback supercb(cb,0,9000,10000);
201
202         if(!context.accelerated_render(surface,quality,renddesc,&supercb))
203         {
204                 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
205                 return false;
206         }
207
208         Point pos(tl[0],tl[1]);
209
210         Real    dens;
211
212         if(!context.accelerated_render(surface,quality,renddesc,&supercb))
213         {
214                 if(cb)cb->error(strprintf(__FILE__"%d: Accelerated Renderer Failure",__LINE__));
215                 return false;
216         }
217
218         for(int y = 0; y < h; y++, pos[1] += ph)
219         {
220                 pos[0] = tl[0];
221                 for(int x = 0; x < w; x++, pos[0] += pw)
222                 {
223                         dens = totaldensity(pos);
224
225                         if(dens >= threshold)
226                         {
227                                 (*surface)[y][x] = Color::blend(color,(*surface)[y][x],get_amount(),get_blend_method());
228                         }
229                 }
230         }
231
232         // Mark our progress as finished
233         if(cb && !cb->amount_complete(10000,10000))
234                 return false;
235
236         return true;
237 }