1 /*! ========================================================================
4 ** $Id: mandelbrot.cpp,v 1.1.1.1 2005/01/04 01:23:10 darco Exp $
6 ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
8 ** This software and associated documentation
9 ** are CONFIDENTIAL and PROPRIETARY property of
10 ** the above-mentioned copyright holder.
12 ** You may not copy, print, publish, or in any
13 ** other way distribute this software without
14 ** a prior written agreement with
15 ** the copyright holder.
17 ** === N O T E S ===========================================================
19 ** ========================================================================= */
21 /* === H E A D E R S ======================================================= */
30 #include "mandelbrot.h"
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>
43 /* === M A C R O S ========================================================= */
45 #define LOG_OF_2 0.69314718055994528623
47 /* === G L O B A L S ======================================================= */
49 SYNFIG_LAYER_INIT(Mandelbrot);
50 SYNFIG_LAYER_SET_NAME(Mandelbrot,"mandelbrot");
51 SYNFIG_LAYER_SET_LOCAL_NAME(Mandelbrot,_("Mandelbrot Set"));
52 SYNFIG_LAYER_SET_CATEGORY(Mandelbrot,_("Fractals"));
53 SYNFIG_LAYER_SET_VERSION(Mandelbrot,"0.2");
54 SYNFIG_LAYER_SET_CVS_ID(Mandelbrot,"$Id: mandelbrot.cpp,v 1.1.1.1 2005/01/04 01:23:10 darco Exp $");
56 /* === P R O C E D U R E S ================================================= */
59 color_neg_flip(Color &color)
72 color.set_g(color.get_g()-color.get_r());
73 color.set_b(color.get_b()-color.get_r());
78 color.set_r(color.get_r()-color.get_g());
79 color.set_b(color.get_b()-color.get_g());
84 color.set_r(color.get_r()-color.get_b());
85 color.set_g(color.get_g()-color.get_b());
90 /* === M E T H O D S ======================================================= */
92 Mandelbrot::Mandelbrot():
93 gradient_offset_inside(0.0),
94 gradient_offset_outside(0.0),
95 gradient_loop_inside(true),
96 gradient_scale_outside(1.0),
97 gradient_inside(Color::alpha(),Color::black()),
98 gradient_outside(Color::alpha(),Color::black())
101 // color_shift=Angle::deg(0);
104 distort_outside=true;
108 invert_outside=false;
116 lp=log(log(bailout));
120 Mandelbrot::set_param(const String & param, const ValueBase &value)
123 // IMPORT(color_shift);
125 IMPORT(gradient_offset_inside);
126 IMPORT(gradient_offset_outside);
127 IMPORT(gradient_loop_inside);
128 IMPORT(gradient_scale_outside);
130 IMPORT(distort_inside);
131 IMPORT(distort_outside);
132 IMPORT(solid_inside);
133 IMPORT(solid_outside);
134 IMPORT(invert_inside);
135 IMPORT(invert_outside);
136 IMPORT(shade_inside);
137 IMPORT(shade_outside);
139 IMPORT(smooth_outside);
142 IMPORT(gradient_inside);
143 IMPORT(gradient_outside);
145 if(param=="iterations" && value.same_as(iterations))
147 iterations=value.get(iterations);
150 if(iterations>500000)
154 if(param=="bailout" && value.same_as(bailout))
156 bailout=value.get(bailout);
158 lp=log(log(bailout));
166 Mandelbrot::get_param(const String & param)const
170 // EXPORT(color_shift);
173 EXPORT(gradient_offset_inside);
174 EXPORT(gradient_offset_outside);
175 EXPORT(gradient_loop_inside);
176 EXPORT(gradient_scale_outside);
178 EXPORT(distort_inside);
179 EXPORT(distort_outside);
180 EXPORT(solid_inside);
181 EXPORT(solid_outside);
182 EXPORT(invert_inside);
183 EXPORT(invert_outside);
184 EXPORT(shade_inside);
185 EXPORT(shade_outside);
186 EXPORT(smooth_outside);
189 EXPORT(gradient_inside);
190 EXPORT(gradient_outside);
193 return sqrt(bailout);
202 Mandelbrot::get_param_vocab()const
207 ret.push_back(ParamDesc("iterations")
208 .set_local_name(_("Iterations"))
210 ret.push_back(ParamDesc("bailout")
211 .set_local_name(_("Bailout ValueBase"))
214 ret.push_back(ParamDesc("broken")
215 .set_local_name(_("Break Set"))
216 .set_description(_("Modify equation to achieve interesting results"))
220 ret.push_back(ParamDesc("distort_inside")
221 .set_local_name(_("Distort Inside"))
222 .set_group(_("Inside"))
224 ret.push_back(ParamDesc("shade_inside")
225 .set_local_name(_("Shade Inside"))
226 .set_group(_("Inside"))
228 ret.push_back(ParamDesc("solid_inside")
229 .set_local_name(_("Solid Inside"))
230 .set_group(_("Inside"))
232 ret.push_back(ParamDesc("invert_inside")
233 .set_local_name(_("Invert Inside"))
234 .set_group(_("Inside"))
236 ret.push_back(ParamDesc("gradient_inside")
237 .set_local_name(_("Gradient Inside"))
238 .set_group(_("Inside"))
240 ret.push_back(ParamDesc("gradient_offset_inside")
241 .set_local_name(_("Offset Inside"))
242 .set_group(_("Inside"))
244 ret.push_back(ParamDesc("gradient_loop_inside")
245 .set_local_name(_("Loop Inside"))
246 .set_group(_("Inside"))
249 ret.push_back(ParamDesc("distort_outside")
250 .set_local_name(_("Distort Outside"))
251 .set_group(_("Outside"))
253 ret.push_back(ParamDesc("shade_outside")
254 .set_local_name(_("Shade Outside"))
255 .set_group(_("Outside"))
257 ret.push_back(ParamDesc("solid_outside")
258 .set_local_name(_("Solid Outside"))
259 .set_group(_("Outside"))
261 ret.push_back(ParamDesc("invert_outside")
262 .set_local_name(_("Invert Outside"))
263 .set_group(_("Outside"))
265 ret.push_back(ParamDesc("gradient_outside")
266 .set_local_name(_("Gradient outside"))
267 .set_group(_("Outside"))
269 ret.push_back(ParamDesc("smooth_outside")
270 .set_local_name(_("Smooth Outside"))
271 .set_description(_("Smooth the coloration outside the set"))
272 .set_group(_("Outside"))
274 ret.push_back(ParamDesc("gradient_offset_outside")
275 .set_local_name(_("Offset Outside"))
276 .set_group(_("Outside"))
278 ret.push_back(ParamDesc("gradient_scale_outside")
279 .set_local_name(_("Scale Outside"))
280 .set_group(_("Outside"))
287 Mandelbrot::get_color(Context context, const Point &pos)const
305 for(int i=0;i<iterations;i++)
307 // Perform complex multiplication
310 if(broken)zr+=zi; // Use "broken" algorithm, if requested (looks weird)
311 zi=zr_hold*zi*2 + ci;
314 // Calculate Magnitude
321 // Darco's original mandelbrot smoothing algo
322 // depth=((Point::value_type)i+(2.0-sqrt(mag))/PI);
324 // Linas Vepstas algo (Better than darco's)
325 // See (http://linas.org/art-gallery/escape/smooth.html)
326 depth= (ColorReal)i + LOG_OF_2*lp - log(log(sqrt(mag))) / LOG_OF_2;
332 depth=static_cast<ColorReal>(i);
334 ColorReal amount(depth/static_cast<ColorReal>(iterations));
335 amount=amount*gradient_scale_outside+gradient_offset_outside;
336 amount-=floor(amount);
339 ret=gradient_outside(amount);
343 ret=context.get_color(Point(pos[0]+zr,pos[1]+zi));
345 ret=context.get_color(pos);
351 ret=Color::blend(gradient_outside(amount), ret, 1.0);
359 ColorReal amount(abs(mag+gradient_offset_inside));
360 if(gradient_loop_inside)
361 amount-=floor(amount);
364 ret=gradient_inside(amount);
368 ret=context.get_color(Point(pos[0]+zr,pos[1]+zi));
370 ret=context.get_color(pos);
376 ret=Color::blend(gradient_inside(amount), ret, 1.0);