1 /* === S Y N F I G ========================================================= */
2 /*! \file mandelbrot.cpp
3 ** \brief Template Header
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 ** === N O T E S ===========================================================
23 ** ========================================================================= */
25 /* === H E A D E R S ======================================================= */
34 #include "mandelbrot.h"
36 #include <synfig/string.h>
37 #include <synfig/time.h>
38 #include <synfig/context.h>
39 #include <synfig/paramdesc.h>
40 #include <synfig/renddesc.h>
41 #include <synfig/surface.h>
42 #include <synfig/value.h>
43 #include <synfig/valuenode.h>
47 /* === M A C R O S ========================================================= */
49 #define LOG_OF_2 0.69314718055994528623
51 /* === G L O B A L S ======================================================= */
53 SYNFIG_LAYER_INIT(Mandelbrot);
54 SYNFIG_LAYER_SET_NAME(Mandelbrot,"mandelbrot");
55 SYNFIG_LAYER_SET_LOCAL_NAME(Mandelbrot,_("Mandelbrot Set"));
56 SYNFIG_LAYER_SET_CATEGORY(Mandelbrot,_("Fractals"));
57 SYNFIG_LAYER_SET_VERSION(Mandelbrot,"0.2");
58 SYNFIG_LAYER_SET_CVS_ID(Mandelbrot,"$Id$");
60 /* === P R O C E D U R E S ================================================= */
63 color_neg_flip(Color &color)
76 color.set_g(color.get_g()-color.get_r());
77 color.set_b(color.get_b()-color.get_r());
82 color.set_r(color.get_r()-color.get_g());
83 color.set_b(color.get_b()-color.get_g());
88 color.set_r(color.get_r()-color.get_b());
89 color.set_g(color.get_g()-color.get_b());
94 /* === M E T H O D S ======================================================= */
96 Mandelbrot::Mandelbrot():
97 gradient_offset_inside(0.0),
98 gradient_offset_outside(0.0),
99 gradient_loop_inside(true),
100 gradient_scale_outside(1.0),
101 gradient_inside(Color::alpha(),Color::black()),
102 gradient_outside(Color::alpha(),Color::black())
105 // color_shift=Angle::deg(0);
108 distort_outside=true;
112 invert_outside=false;
120 lp=log(log(bailout));
124 Mandelbrot::set_param(const String & param, const ValueBase &value)
127 // IMPORT(color_shift);
129 IMPORT(gradient_offset_inside);
130 IMPORT(gradient_offset_outside);
131 IMPORT(gradient_loop_inside);
132 IMPORT(gradient_scale_outside);
134 IMPORT(distort_inside);
135 IMPORT(distort_outside);
136 IMPORT(solid_inside);
137 IMPORT(solid_outside);
138 IMPORT(invert_inside);
139 IMPORT(invert_outside);
140 IMPORT(shade_inside);
141 IMPORT(shade_outside);
143 IMPORT(smooth_outside);
146 IMPORT(gradient_inside);
147 IMPORT(gradient_outside);
149 if(param=="iterations" && value.same_type_as(iterations))
151 iterations=value.get(iterations);
154 if(iterations>500000)
158 if(param=="bailout" && value.same_type_as(bailout))
160 bailout=value.get(bailout);
162 lp=log(log(bailout));
170 Mandelbrot::get_param(const String & param)const
174 // EXPORT(color_shift);
177 EXPORT(gradient_offset_inside);
178 EXPORT(gradient_offset_outside);
179 EXPORT(gradient_loop_inside);
180 EXPORT(gradient_scale_outside);
182 EXPORT(distort_inside);
183 EXPORT(distort_outside);
184 EXPORT(solid_inside);
185 EXPORT(solid_outside);
186 EXPORT(invert_inside);
187 EXPORT(invert_outside);
188 EXPORT(shade_inside);
189 EXPORT(shade_outside);
190 EXPORT(smooth_outside);
193 EXPORT(gradient_inside);
194 EXPORT(gradient_outside);
197 return sqrt(bailout);
206 Mandelbrot::get_param_vocab()const
211 ret.push_back(ParamDesc("iterations")
212 .set_local_name(_("Iterations"))
214 ret.push_back(ParamDesc("bailout")
215 .set_local_name(_("Bailout ValueBase"))
218 ret.push_back(ParamDesc("broken")
219 .set_local_name(_("Break Set"))
220 .set_description(_("Modify equation to achieve interesting results"))
224 ret.push_back(ParamDesc("distort_inside")
225 .set_local_name(_("Distort Inside"))
226 .set_group(_("Inside"))
228 ret.push_back(ParamDesc("shade_inside")
229 .set_local_name(_("Shade Inside"))
230 .set_group(_("Inside"))
232 ret.push_back(ParamDesc("solid_inside")
233 .set_local_name(_("Solid Inside"))
234 .set_group(_("Inside"))
236 ret.push_back(ParamDesc("invert_inside")
237 .set_local_name(_("Invert Inside"))
238 .set_group(_("Inside"))
240 ret.push_back(ParamDesc("gradient_inside")
241 .set_local_name(_("Gradient Inside"))
242 .set_group(_("Inside"))
244 ret.push_back(ParamDesc("gradient_offset_inside")
245 .set_local_name(_("Offset Inside"))
246 .set_group(_("Inside"))
248 ret.push_back(ParamDesc("gradient_loop_inside")
249 .set_local_name(_("Loop Inside"))
250 .set_group(_("Inside"))
253 ret.push_back(ParamDesc("distort_outside")
254 .set_local_name(_("Distort Outside"))
255 .set_group(_("Outside"))
257 ret.push_back(ParamDesc("shade_outside")
258 .set_local_name(_("Shade Outside"))
259 .set_group(_("Outside"))
261 ret.push_back(ParamDesc("solid_outside")
262 .set_local_name(_("Solid Outside"))
263 .set_group(_("Outside"))
265 ret.push_back(ParamDesc("invert_outside")
266 .set_local_name(_("Invert Outside"))
267 .set_group(_("Outside"))
269 ret.push_back(ParamDesc("gradient_outside")
270 .set_local_name(_("Gradient outside"))
271 .set_group(_("Outside"))
273 ret.push_back(ParamDesc("smooth_outside")
274 .set_local_name(_("Smooth Outside"))
275 .set_description(_("Smooth the coloration outside the set"))
276 .set_group(_("Outside"))
278 ret.push_back(ParamDesc("gradient_offset_outside")
279 .set_local_name(_("Offset Outside"))
280 .set_group(_("Outside"))
282 ret.push_back(ParamDesc("gradient_scale_outside")
283 .set_local_name(_("Scale Outside"))
284 .set_group(_("Outside"))
291 Mandelbrot::get_color(Context context, const Point &pos)const
309 for(int i=0;i<iterations;i++)
311 // Perform complex multiplication
314 if(broken)zr+=zi; // Use "broken" algorithm, if requested (looks weird)
315 zi=zr_hold*zi*2 + ci;
318 // Calculate Magnitude
325 // Darco's original mandelbrot smoothing algo
326 // depth=((Point::value_type)i+(2.0-sqrt(mag))/PI);
328 // Linas Vepstas algo (Better than darco's)
329 // See (http://linas.org/art-gallery/escape/smooth.html)
330 depth= (ColorReal)i + LOG_OF_2*lp - log(log(sqrt(mag))) / LOG_OF_2;
336 depth=static_cast<ColorReal>(i);
338 ColorReal amount(depth/static_cast<ColorReal>(iterations));
339 amount=amount*gradient_scale_outside+gradient_offset_outside;
340 amount-=floor(amount);
343 ret=gradient_outside(amount);
347 ret=context.get_color(Point(pos[0]+zr,pos[1]+zi));
349 ret=context.get_color(pos);
355 ret=Color::blend(gradient_outside(amount), ret, 1.0);
363 ColorReal amount(abs(mag+gradient_offset_inside));
364 if(gradient_loop_inside)
365 amount-=floor(amount);
368 ret=gradient_inside(amount);
372 ret=context.get_color(Point(pos[0]+zr,pos[1]+zi));
374 ret=context.get_color(pos);
380 ret=Color::blend(gradient_inside(amount), ret, 1.0);