1 /*! ========================================================================
4 ** $Id: mandelbrot.cpp,v 1.1.1.1 2005/01/04 01:23:10 darco Exp $
6 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
8 ** This package is free software; you can redistribute it and/or
9 ** modify it under the terms of the GNU General Public License as
10 ** published by the Free Software Foundation; either version 2 of
11 ** the License, or (at your option) any later version.
13 ** This package is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 ** General Public License for more details.
18 ** === N O T E S ===========================================================
20 ** ========================================================================= */
22 /* === H E A D E R S ======================================================= */
31 #include "mandelbrot.h"
33 #include <synfig/string.h>
34 #include <synfig/time.h>
35 #include <synfig/context.h>
36 #include <synfig/paramdesc.h>
37 #include <synfig/renddesc.h>
38 #include <synfig/surface.h>
39 #include <synfig/value.h>
40 #include <synfig/valuenode.h>
44 /* === M A C R O S ========================================================= */
46 #define LOG_OF_2 0.69314718055994528623
48 /* === G L O B A L S ======================================================= */
50 SYNFIG_LAYER_INIT(Mandelbrot);
51 SYNFIG_LAYER_SET_NAME(Mandelbrot,"mandelbrot");
52 SYNFIG_LAYER_SET_LOCAL_NAME(Mandelbrot,_("Mandelbrot Set"));
53 SYNFIG_LAYER_SET_CATEGORY(Mandelbrot,_("Fractals"));
54 SYNFIG_LAYER_SET_VERSION(Mandelbrot,"0.2");
55 SYNFIG_LAYER_SET_CVS_ID(Mandelbrot,"$Id: mandelbrot.cpp,v 1.1.1.1 2005/01/04 01:23:10 darco Exp $");
57 /* === P R O C E D U R E S ================================================= */
60 color_neg_flip(Color &color)
73 color.set_g(color.get_g()-color.get_r());
74 color.set_b(color.get_b()-color.get_r());
79 color.set_r(color.get_r()-color.get_g());
80 color.set_b(color.get_b()-color.get_g());
85 color.set_r(color.get_r()-color.get_b());
86 color.set_g(color.get_g()-color.get_b());
91 /* === M E T H O D S ======================================================= */
93 Mandelbrot::Mandelbrot():
94 gradient_offset_inside(0.0),
95 gradient_offset_outside(0.0),
96 gradient_loop_inside(true),
97 gradient_scale_outside(1.0),
98 gradient_inside(Color::alpha(),Color::black()),
99 gradient_outside(Color::alpha(),Color::black())
102 // color_shift=Angle::deg(0);
105 distort_outside=true;
109 invert_outside=false;
117 lp=log(log(bailout));
121 Mandelbrot::set_param(const String & param, const ValueBase &value)
124 // IMPORT(color_shift);
126 IMPORT(gradient_offset_inside);
127 IMPORT(gradient_offset_outside);
128 IMPORT(gradient_loop_inside);
129 IMPORT(gradient_scale_outside);
131 IMPORT(distort_inside);
132 IMPORT(distort_outside);
133 IMPORT(solid_inside);
134 IMPORT(solid_outside);
135 IMPORT(invert_inside);
136 IMPORT(invert_outside);
137 IMPORT(shade_inside);
138 IMPORT(shade_outside);
140 IMPORT(smooth_outside);
143 IMPORT(gradient_inside);
144 IMPORT(gradient_outside);
146 if(param=="iterations" && value.same_as(iterations))
148 iterations=value.get(iterations);
151 if(iterations>500000)
155 if(param=="bailout" && value.same_as(bailout))
157 bailout=value.get(bailout);
159 lp=log(log(bailout));
167 Mandelbrot::get_param(const String & param)const
171 // EXPORT(color_shift);
174 EXPORT(gradient_offset_inside);
175 EXPORT(gradient_offset_outside);
176 EXPORT(gradient_loop_inside);
177 EXPORT(gradient_scale_outside);
179 EXPORT(distort_inside);
180 EXPORT(distort_outside);
181 EXPORT(solid_inside);
182 EXPORT(solid_outside);
183 EXPORT(invert_inside);
184 EXPORT(invert_outside);
185 EXPORT(shade_inside);
186 EXPORT(shade_outside);
187 EXPORT(smooth_outside);
190 EXPORT(gradient_inside);
191 EXPORT(gradient_outside);
194 return sqrt(bailout);
203 Mandelbrot::get_param_vocab()const
208 ret.push_back(ParamDesc("iterations")
209 .set_local_name(_("Iterations"))
211 ret.push_back(ParamDesc("bailout")
212 .set_local_name(_("Bailout ValueBase"))
215 ret.push_back(ParamDesc("broken")
216 .set_local_name(_("Break Set"))
217 .set_description(_("Modify equation to achieve interesting results"))
221 ret.push_back(ParamDesc("distort_inside")
222 .set_local_name(_("Distort Inside"))
223 .set_group(_("Inside"))
225 ret.push_back(ParamDesc("shade_inside")
226 .set_local_name(_("Shade Inside"))
227 .set_group(_("Inside"))
229 ret.push_back(ParamDesc("solid_inside")
230 .set_local_name(_("Solid Inside"))
231 .set_group(_("Inside"))
233 ret.push_back(ParamDesc("invert_inside")
234 .set_local_name(_("Invert Inside"))
235 .set_group(_("Inside"))
237 ret.push_back(ParamDesc("gradient_inside")
238 .set_local_name(_("Gradient Inside"))
239 .set_group(_("Inside"))
241 ret.push_back(ParamDesc("gradient_offset_inside")
242 .set_local_name(_("Offset Inside"))
243 .set_group(_("Inside"))
245 ret.push_back(ParamDesc("gradient_loop_inside")
246 .set_local_name(_("Loop Inside"))
247 .set_group(_("Inside"))
250 ret.push_back(ParamDesc("distort_outside")
251 .set_local_name(_("Distort Outside"))
252 .set_group(_("Outside"))
254 ret.push_back(ParamDesc("shade_outside")
255 .set_local_name(_("Shade Outside"))
256 .set_group(_("Outside"))
258 ret.push_back(ParamDesc("solid_outside")
259 .set_local_name(_("Solid Outside"))
260 .set_group(_("Outside"))
262 ret.push_back(ParamDesc("invert_outside")
263 .set_local_name(_("Invert Outside"))
264 .set_group(_("Outside"))
266 ret.push_back(ParamDesc("gradient_outside")
267 .set_local_name(_("Gradient outside"))
268 .set_group(_("Outside"))
270 ret.push_back(ParamDesc("smooth_outside")
271 .set_local_name(_("Smooth Outside"))
272 .set_description(_("Smooth the coloration outside the set"))
273 .set_group(_("Outside"))
275 ret.push_back(ParamDesc("gradient_offset_outside")
276 .set_local_name(_("Offset Outside"))
277 .set_group(_("Outside"))
279 ret.push_back(ParamDesc("gradient_scale_outside")
280 .set_local_name(_("Scale Outside"))
281 .set_group(_("Outside"))
288 Mandelbrot::get_color(Context context, const Point &pos)const
306 for(int i=0;i<iterations;i++)
308 // Perform complex multiplication
311 if(broken)zr+=zi; // Use "broken" algorithm, if requested (looks weird)
312 zi=zr_hold*zi*2 + ci;
315 // Calculate Magnitude
322 // Darco's original mandelbrot smoothing algo
323 // depth=((Point::value_type)i+(2.0-sqrt(mag))/PI);
325 // Linas Vepstas algo (Better than darco's)
326 // See (http://linas.org/art-gallery/escape/smooth.html)
327 depth= (ColorReal)i + LOG_OF_2*lp - log(log(sqrt(mag))) / LOG_OF_2;
333 depth=static_cast<ColorReal>(i);
335 ColorReal amount(depth/static_cast<ColorReal>(iterations));
336 amount=amount*gradient_scale_outside+gradient_offset_outside;
337 amount-=floor(amount);
340 ret=gradient_outside(amount);
344 ret=context.get_color(Point(pos[0]+zr,pos[1]+zi));
346 ret=context.get_color(pos);
352 ret=Color::blend(gradient_outside(amount), ret, 1.0);
360 ColorReal amount(abs(mag+gradient_offset_inside));
361 if(gradient_loop_inside)
362 amount-=floor(amount);
365 ret=gradient_inside(amount);
369 ret=context.get_color(Point(pos[0]+zr,pos[1]+zi));
371 ret=context.get_color(pos);
377 ret=Color::blend(gradient_inside(amount), ret, 1.0);