1 /* === S Y N F I G ========================================================= */
2 /*! \file mandelbrot.cpp
3 ** \brief Template Header
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.
19 ** === N O T E S ===========================================================
21 ** ========================================================================= */
23 /* === H E A D E R S ======================================================= */
32 #include "mandelbrot.h"
34 #include <synfig/string.h>
35 #include <synfig/time.h>
36 #include <synfig/context.h>
37 #include <synfig/paramdesc.h>
38 #include <synfig/renddesc.h>
39 #include <synfig/surface.h>
40 #include <synfig/value.h>
41 #include <synfig/valuenode.h>
45 /* === M A C R O S ========================================================= */
47 #define LOG_OF_2 0.69314718055994528623
49 /* === G L O B A L S ======================================================= */
51 SYNFIG_LAYER_INIT(Mandelbrot);
52 SYNFIG_LAYER_SET_NAME(Mandelbrot,"mandelbrot");
53 SYNFIG_LAYER_SET_LOCAL_NAME(Mandelbrot,_("Mandelbrot Set"));
54 SYNFIG_LAYER_SET_CATEGORY(Mandelbrot,_("Fractals"));
55 SYNFIG_LAYER_SET_VERSION(Mandelbrot,"0.2");
56 SYNFIG_LAYER_SET_CVS_ID(Mandelbrot,"$Id: mandelbrot.cpp,v 1.1.1.1 2005/01/04 01:23:10 darco Exp $");
58 /* === P R O C E D U R E S ================================================= */
61 color_neg_flip(Color &color)
74 color.set_g(color.get_g()-color.get_r());
75 color.set_b(color.get_b()-color.get_r());
80 color.set_r(color.get_r()-color.get_g());
81 color.set_b(color.get_b()-color.get_g());
86 color.set_r(color.get_r()-color.get_b());
87 color.set_g(color.get_g()-color.get_b());
92 /* === M E T H O D S ======================================================= */
94 Mandelbrot::Mandelbrot():
95 gradient_offset_inside(0.0),
96 gradient_offset_outside(0.0),
97 gradient_loop_inside(true),
98 gradient_scale_outside(1.0),
99 gradient_inside(Color::alpha(),Color::black()),
100 gradient_outside(Color::alpha(),Color::black())
103 // color_shift=Angle::deg(0);
106 distort_outside=true;
110 invert_outside=false;
118 lp=log(log(bailout));
122 Mandelbrot::set_param(const String & param, const ValueBase &value)
125 // IMPORT(color_shift);
127 IMPORT(gradient_offset_inside);
128 IMPORT(gradient_offset_outside);
129 IMPORT(gradient_loop_inside);
130 IMPORT(gradient_scale_outside);
132 IMPORT(distort_inside);
133 IMPORT(distort_outside);
134 IMPORT(solid_inside);
135 IMPORT(solid_outside);
136 IMPORT(invert_inside);
137 IMPORT(invert_outside);
138 IMPORT(shade_inside);
139 IMPORT(shade_outside);
141 IMPORT(smooth_outside);
144 IMPORT(gradient_inside);
145 IMPORT(gradient_outside);
147 if(param=="iterations" && value.same_as(iterations))
149 iterations=value.get(iterations);
152 if(iterations>500000)
156 if(param=="bailout" && value.same_as(bailout))
158 bailout=value.get(bailout);
160 lp=log(log(bailout));
168 Mandelbrot::get_param(const String & param)const
172 // EXPORT(color_shift);
175 EXPORT(gradient_offset_inside);
176 EXPORT(gradient_offset_outside);
177 EXPORT(gradient_loop_inside);
178 EXPORT(gradient_scale_outside);
180 EXPORT(distort_inside);
181 EXPORT(distort_outside);
182 EXPORT(solid_inside);
183 EXPORT(solid_outside);
184 EXPORT(invert_inside);
185 EXPORT(invert_outside);
186 EXPORT(shade_inside);
187 EXPORT(shade_outside);
188 EXPORT(smooth_outside);
191 EXPORT(gradient_inside);
192 EXPORT(gradient_outside);
195 return sqrt(bailout);
204 Mandelbrot::get_param_vocab()const
209 ret.push_back(ParamDesc("iterations")
210 .set_local_name(_("Iterations"))
212 ret.push_back(ParamDesc("bailout")
213 .set_local_name(_("Bailout ValueBase"))
216 ret.push_back(ParamDesc("broken")
217 .set_local_name(_("Break Set"))
218 .set_description(_("Modify equation to achieve interesting results"))
222 ret.push_back(ParamDesc("distort_inside")
223 .set_local_name(_("Distort Inside"))
224 .set_group(_("Inside"))
226 ret.push_back(ParamDesc("shade_inside")
227 .set_local_name(_("Shade Inside"))
228 .set_group(_("Inside"))
230 ret.push_back(ParamDesc("solid_inside")
231 .set_local_name(_("Solid Inside"))
232 .set_group(_("Inside"))
234 ret.push_back(ParamDesc("invert_inside")
235 .set_local_name(_("Invert Inside"))
236 .set_group(_("Inside"))
238 ret.push_back(ParamDesc("gradient_inside")
239 .set_local_name(_("Gradient Inside"))
240 .set_group(_("Inside"))
242 ret.push_back(ParamDesc("gradient_offset_inside")
243 .set_local_name(_("Offset Inside"))
244 .set_group(_("Inside"))
246 ret.push_back(ParamDesc("gradient_loop_inside")
247 .set_local_name(_("Loop Inside"))
248 .set_group(_("Inside"))
251 ret.push_back(ParamDesc("distort_outside")
252 .set_local_name(_("Distort Outside"))
253 .set_group(_("Outside"))
255 ret.push_back(ParamDesc("shade_outside")
256 .set_local_name(_("Shade Outside"))
257 .set_group(_("Outside"))
259 ret.push_back(ParamDesc("solid_outside")
260 .set_local_name(_("Solid Outside"))
261 .set_group(_("Outside"))
263 ret.push_back(ParamDesc("invert_outside")
264 .set_local_name(_("Invert Outside"))
265 .set_group(_("Outside"))
267 ret.push_back(ParamDesc("gradient_outside")
268 .set_local_name(_("Gradient outside"))
269 .set_group(_("Outside"))
271 ret.push_back(ParamDesc("smooth_outside")
272 .set_local_name(_("Smooth Outside"))
273 .set_description(_("Smooth the coloration outside the set"))
274 .set_group(_("Outside"))
276 ret.push_back(ParamDesc("gradient_offset_outside")
277 .set_local_name(_("Offset Outside"))
278 .set_group(_("Outside"))
280 ret.push_back(ParamDesc("gradient_scale_outside")
281 .set_local_name(_("Scale Outside"))
282 .set_group(_("Outside"))
289 Mandelbrot::get_color(Context context, const Point &pos)const
307 for(int i=0;i<iterations;i++)
309 // Perform complex multiplication
312 if(broken)zr+=zi; // Use "broken" algorithm, if requested (looks weird)
313 zi=zr_hold*zi*2 + ci;
316 // Calculate Magnitude
323 // Darco's original mandelbrot smoothing algo
324 // depth=((Point::value_type)i+(2.0-sqrt(mag))/PI);
326 // Linas Vepstas algo (Better than darco's)
327 // See (http://linas.org/art-gallery/escape/smooth.html)
328 depth= (ColorReal)i + LOG_OF_2*lp - log(log(sqrt(mag))) / LOG_OF_2;
334 depth=static_cast<ColorReal>(i);
336 ColorReal amount(depth/static_cast<ColorReal>(iterations));
337 amount=amount*gradient_scale_outside+gradient_offset_outside;
338 amount-=floor(amount);
341 ret=gradient_outside(amount);
345 ret=context.get_color(Point(pos[0]+zr,pos[1]+zi));
347 ret=context.get_color(pos);
353 ret=Color::blend(gradient_outside(amount), ret, 1.0);
361 ColorReal amount(abs(mag+gradient_offset_inside));
362 if(gradient_loop_inside)
363 amount-=floor(amount);
366 ret=gradient_inside(amount);
370 ret=context.get_color(Point(pos[0]+zr,pos[1]+zi));
372 ret=context.get_color(pos);
378 ret=Color::blend(gradient_inside(amount), ret, 1.0);