1 /* === S Y N F I G ========================================================= */
2 /*! \file mandelbrot.cpp
3 ** \brief Implementation of the "Mandelbrot Set" layer
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 2007 Chris Moore
11 ** This package is free software; you can redistribute it and/or
12 ** modify it under the terms of the GNU General Public License as
13 ** published by the Free Software Foundation; either version 2 of
14 ** the License, or (at your option) any later version.
16 ** This package is distributed in the hope that it will be useful,
17 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ** General Public License for more details.
22 ** === N O T E S ===========================================================
24 ** ========================================================================= */
26 /* === H E A D E R S ======================================================= */
35 #include "mandelbrot.h"
37 #include <synfig/string.h>
38 #include <synfig/time.h>
39 #include <synfig/context.h>
40 #include <synfig/paramdesc.h>
41 #include <synfig/renddesc.h>
42 #include <synfig/surface.h>
43 #include <synfig/value.h>
44 #include <synfig/valuenode.h>
48 /* === M A C R O S ========================================================= */
50 #define LOG_OF_2 0.69314718055994528623
52 /* === G L O B A L S ======================================================= */
54 SYNFIG_LAYER_INIT(Mandelbrot);
55 SYNFIG_LAYER_SET_NAME(Mandelbrot,"mandelbrot");
56 SYNFIG_LAYER_SET_LOCAL_NAME(Mandelbrot,N_("Mandelbrot Set"));
57 SYNFIG_LAYER_SET_CATEGORY(Mandelbrot,N_("Fractals"));
58 SYNFIG_LAYER_SET_VERSION(Mandelbrot,"0.2");
59 SYNFIG_LAYER_SET_CVS_ID(Mandelbrot,"$Id$");
61 /* === P R O C E D U R E S ================================================= */
64 color_neg_flip(Color &color)
77 color.set_g(color.get_g()-color.get_r());
78 color.set_b(color.get_b()-color.get_r());
83 color.set_r(color.get_r()-color.get_g());
84 color.set_b(color.get_b()-color.get_g());
89 color.set_r(color.get_r()-color.get_b());
90 color.set_g(color.get_g()-color.get_b());
95 /* === M E T H O D S ======================================================= */
97 Mandelbrot::Mandelbrot():
98 gradient_offset_inside(0.0),
99 gradient_offset_outside(0.0),
100 gradient_loop_inside(true),
101 gradient_scale_outside(1.0),
102 gradient_inside(Color::alpha(),Color::black()),
103 gradient_outside(Color::alpha(),Color::black())
106 // color_shift=Angle::deg(0);
109 distort_outside=true;
113 invert_outside=false;
121 lp=log(log(bailout));
123 Layer::Vocab voc(get_param_vocab());
124 Layer::fill_static(voc);
128 Mandelbrot::set_param(const String & param, const ValueBase &value)
131 // IMPORT(color_shift);
133 IMPORT(gradient_offset_inside);
134 IMPORT(gradient_offset_outside);
135 IMPORT(gradient_loop_inside);
136 IMPORT(gradient_scale_outside);
138 IMPORT(distort_inside);
139 IMPORT(distort_outside);
140 IMPORT(solid_inside);
141 IMPORT(solid_outside);
142 IMPORT(invert_inside);
143 IMPORT(invert_outside);
144 IMPORT(shade_inside);
145 IMPORT(shade_outside);
147 IMPORT(smooth_outside);
150 IMPORT(gradient_inside);
151 IMPORT(gradient_outside);
153 // TODO: Use IMPORT_PLUS
154 if(param=="iterations" && value.same_type_as(iterations))
156 iterations=value.get(iterations);
159 if(iterations>500000)
161 set_param_static(param, value.get_static());
164 if(param=="bailout" && value.same_type_as(bailout))
166 bailout=value.get(bailout);
168 lp=log(log(bailout));
169 set_param_static(param, value.get_static());
177 Mandelbrot::get_param(const String & param)const
181 // EXPORT(color_shift);
184 EXPORT(gradient_offset_inside);
185 EXPORT(gradient_offset_outside);
186 EXPORT(gradient_loop_inside);
187 EXPORT(gradient_scale_outside);
189 EXPORT(distort_inside);
190 EXPORT(distort_outside);
191 EXPORT(solid_inside);
192 EXPORT(solid_outside);
193 EXPORT(invert_inside);
194 EXPORT(invert_outside);
195 EXPORT(shade_inside);
196 EXPORT(shade_outside);
197 EXPORT(smooth_outside);
200 EXPORT(gradient_inside);
201 EXPORT(gradient_outside);
205 ValueBase ret(sqrt(bailout));
206 ret.set_static(get_param_static(param));
217 Mandelbrot::get_param_vocab()const
222 ret.push_back(ParamDesc("iterations")
223 .set_local_name(_("Iterations"))
225 ret.push_back(ParamDesc("bailout")
226 .set_local_name(_("Bailout ValueBase"))
229 ret.push_back(ParamDesc("broken")
230 .set_local_name(_("Break Set"))
231 .set_description(_("Modify equation to achieve interesting results"))
235 ret.push_back(ParamDesc("distort_inside")
236 .set_local_name(_("Distort Inside"))
237 .set_group(_("Inside"))
239 ret.push_back(ParamDesc("shade_inside")
240 .set_local_name(_("Shade Inside"))
241 .set_group(_("Inside"))
243 ret.push_back(ParamDesc("solid_inside")
244 .set_local_name(_("Solid Inside"))
245 .set_group(_("Inside"))
247 ret.push_back(ParamDesc("invert_inside")
248 .set_local_name(_("Invert Inside"))
249 .set_group(_("Inside"))
251 ret.push_back(ParamDesc("gradient_inside")
252 .set_local_name(_("Gradient Inside"))
253 .set_group(_("Inside"))
255 ret.push_back(ParamDesc("gradient_offset_inside")
256 .set_local_name(_("Offset Inside"))
257 .set_group(_("Inside"))
259 ret.push_back(ParamDesc("gradient_loop_inside")
260 .set_local_name(_("Loop Inside"))
261 .set_group(_("Inside"))
264 ret.push_back(ParamDesc("distort_outside")
265 .set_local_name(_("Distort Outside"))
266 .set_group(_("Outside"))
268 ret.push_back(ParamDesc("shade_outside")
269 .set_local_name(_("Shade Outside"))
270 .set_group(_("Outside"))
272 ret.push_back(ParamDesc("solid_outside")
273 .set_local_name(_("Solid Outside"))
274 .set_group(_("Outside"))
276 ret.push_back(ParamDesc("invert_outside")
277 .set_local_name(_("Invert Outside"))
278 .set_group(_("Outside"))
280 ret.push_back(ParamDesc("gradient_outside")
281 .set_local_name(_("Gradient outside"))
282 .set_group(_("Outside"))
284 ret.push_back(ParamDesc("smooth_outside")
285 .set_local_name(_("Smooth Outside"))
286 .set_description(_("Smooth the coloration outside the set"))
287 .set_group(_("Outside"))
289 ret.push_back(ParamDesc("gradient_offset_outside")
290 .set_local_name(_("Offset Outside"))
291 .set_group(_("Outside"))
293 ret.push_back(ParamDesc("gradient_scale_outside")
294 .set_local_name(_("Scale Outside"))
295 .set_group(_("Outside"))
302 Mandelbrot::get_color(Context context, const Point &pos)const
320 for(int i=0;i<iterations;i++)
322 // Perform complex multiplication
325 if(broken)zr+=zi; // Use "broken" algorithm, if requested (looks weird)
326 zi=zr_hold*zi*2 + ci;
329 // Calculate Magnitude
336 // Darco's original mandelbrot smoothing algo
337 // depth=((Point::value_type)i+(2.0-sqrt(mag))/PI);
339 // Linas Vepstas algo (Better than darco's)
340 // See (http://linas.org/art-gallery/escape/smooth.html)
341 depth= (ColorReal)i + LOG_OF_2*lp - log(log(sqrt(mag))) / LOG_OF_2;
347 depth=static_cast<ColorReal>(i);
349 ColorReal amount(depth/static_cast<ColorReal>(iterations));
350 amount=amount*gradient_scale_outside+gradient_offset_outside;
351 amount-=floor(amount);
354 ret=gradient_outside(amount);
358 ret=context.get_color(Point(pos[0]+zr,pos[1]+zi));
360 ret=context.get_color(pos);
366 ret=Color::blend(gradient_outside(amount), ret, 1.0);
374 ColorReal amount(abs(mag+gradient_offset_inside));
375 if(gradient_loop_inside)
376 amount-=floor(amount);
379 ret=gradient_inside(amount);
383 ret=context.get_color(Point(pos[0]+zr,pos[1]+zi));
385 ret=context.get_color(pos);
391 ret=Color::blend(gradient_inside(amount), ret, 1.0);