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));
125 Mandelbrot::set_param(const String & param, const ValueBase &value)
128 // IMPORT(color_shift);
130 IMPORT(gradient_offset_inside);
131 IMPORT(gradient_offset_outside);
132 IMPORT(gradient_loop_inside);
133 IMPORT(gradient_scale_outside);
135 IMPORT(distort_inside);
136 IMPORT(distort_outside);
137 IMPORT(solid_inside);
138 IMPORT(solid_outside);
139 IMPORT(invert_inside);
140 IMPORT(invert_outside);
141 IMPORT(shade_inside);
142 IMPORT(shade_outside);
144 IMPORT(smooth_outside);
147 IMPORT(gradient_inside);
148 IMPORT(gradient_outside);
150 if(param=="iterations" && value.same_type_as(iterations))
152 iterations=value.get(iterations);
155 if(iterations>500000)
159 if(param=="bailout" && value.same_type_as(bailout))
161 bailout=value.get(bailout);
163 lp=log(log(bailout));
171 Mandelbrot::get_param(const String & param)const
175 // EXPORT(color_shift);
178 EXPORT(gradient_offset_inside);
179 EXPORT(gradient_offset_outside);
180 EXPORT(gradient_loop_inside);
181 EXPORT(gradient_scale_outside);
183 EXPORT(distort_inside);
184 EXPORT(distort_outside);
185 EXPORT(solid_inside);
186 EXPORT(solid_outside);
187 EXPORT(invert_inside);
188 EXPORT(invert_outside);
189 EXPORT(shade_inside);
190 EXPORT(shade_outside);
191 EXPORT(smooth_outside);
194 EXPORT(gradient_inside);
195 EXPORT(gradient_outside);
198 return sqrt(bailout);
207 Mandelbrot::get_param_vocab()const
212 ret.push_back(ParamDesc("iterations")
213 .set_local_name(_("Iterations"))
215 ret.push_back(ParamDesc("bailout")
216 .set_local_name(_("Bailout ValueBase"))
219 ret.push_back(ParamDesc("broken")
220 .set_local_name(_("Break Set"))
221 .set_description(_("Modify equation to achieve interesting results"))
225 ret.push_back(ParamDesc("distort_inside")
226 .set_local_name(_("Distort Inside"))
227 .set_group(_("Inside"))
229 ret.push_back(ParamDesc("shade_inside")
230 .set_local_name(_("Shade Inside"))
231 .set_group(_("Inside"))
233 ret.push_back(ParamDesc("solid_inside")
234 .set_local_name(_("Solid Inside"))
235 .set_group(_("Inside"))
237 ret.push_back(ParamDesc("invert_inside")
238 .set_local_name(_("Invert Inside"))
239 .set_group(_("Inside"))
241 ret.push_back(ParamDesc("gradient_inside")
242 .set_local_name(_("Gradient Inside"))
243 .set_group(_("Inside"))
245 ret.push_back(ParamDesc("gradient_offset_inside")
246 .set_local_name(_("Offset Inside"))
247 .set_group(_("Inside"))
249 ret.push_back(ParamDesc("gradient_loop_inside")
250 .set_local_name(_("Loop Inside"))
251 .set_group(_("Inside"))
254 ret.push_back(ParamDesc("distort_outside")
255 .set_local_name(_("Distort Outside"))
256 .set_group(_("Outside"))
258 ret.push_back(ParamDesc("shade_outside")
259 .set_local_name(_("Shade Outside"))
260 .set_group(_("Outside"))
262 ret.push_back(ParamDesc("solid_outside")
263 .set_local_name(_("Solid Outside"))
264 .set_group(_("Outside"))
266 ret.push_back(ParamDesc("invert_outside")
267 .set_local_name(_("Invert Outside"))
268 .set_group(_("Outside"))
270 ret.push_back(ParamDesc("gradient_outside")
271 .set_local_name(_("Gradient outside"))
272 .set_group(_("Outside"))
274 ret.push_back(ParamDesc("smooth_outside")
275 .set_local_name(_("Smooth Outside"))
276 .set_description(_("Smooth the coloration outside the set"))
277 .set_group(_("Outside"))
279 ret.push_back(ParamDesc("gradient_offset_outside")
280 .set_local_name(_("Offset Outside"))
281 .set_group(_("Outside"))
283 ret.push_back(ParamDesc("gradient_scale_outside")
284 .set_local_name(_("Scale Outside"))
285 .set_group(_("Outside"))
292 Mandelbrot::get_color(Context context, const Point &pos)const
310 for(int i=0;i<iterations;i++)
312 // Perform complex multiplication
315 if(broken)zr+=zi; // Use "broken" algorithm, if requested (looks weird)
316 zi=zr_hold*zi*2 + ci;
319 // Calculate Magnitude
326 // Darco's original mandelbrot smoothing algo
327 // depth=((Point::value_type)i+(2.0-sqrt(mag))/PI);
329 // Linas Vepstas algo (Better than darco's)
330 // See (http://linas.org/art-gallery/escape/smooth.html)
331 depth= (ColorReal)i + LOG_OF_2*lp - log(log(sqrt(mag))) / LOG_OF_2;
337 depth=static_cast<ColorReal>(i);
339 ColorReal amount(depth/static_cast<ColorReal>(iterations));
340 amount=amount*gradient_scale_outside+gradient_offset_outside;
341 amount-=floor(amount);
344 ret=gradient_outside(amount);
348 ret=context.get_color(Point(pos[0]+zr,pos[1]+zi));
350 ret=context.get_color(pos);
356 ret=Color::blend(gradient_outside(amount), ret, 1.0);
364 ColorReal amount(abs(mag+gradient_offset_inside));
365 if(gradient_loop_inside)
366 amount-=floor(amount);
369 ret=gradient_inside(amount);
373 ret=context.get_color(Point(pos[0]+zr,pos[1]+zi));
375 ret=context.get_color(pos);
381 ret=Color::blend(gradient_inside(amount), ret, 1.0);