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