-
Notifications
You must be signed in to change notification settings - Fork 0
/
mandelbrot.c
265 lines (247 loc) · 8.53 KB
/
mandelbrot.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// http://mathr.co.uk/blog/2014-11-02_practical_interior_distance_rendering.html
#include <complex.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
const double pi = 3.141592653589793;
const double infinity = 1.0 / 0.0;
const double phi = 1.618033988749895; // (sqrt(5.0) + 1.0) / 2.0;
const double colour_modulus = 5.7581917135421046e-2; // (1.0 + 1.0 / (phi * phi)) / 24.0;
const double escape_radius_2 = 512.0 * 512.0;
const int BIAS_UNKNOWN = 0;
const int BIAS_INTERIOR = 1;
const int BIAS_EXTERIOR = 2;
const int ALGORITHM_PLAIN = 0;
const int ALGORITHM_UNBIASED = 1;
const int ALGORITHM_BIASED = 2;
const int ALGORITHM_ANALYSE = 4;
static inline double cabs2(complex double z) {
return creal(z) * creal(z) + cimag(z) * cimag(z);
}
static inline unsigned char *image_new(int width, int height) {
return malloc(width * height * 3);
}
static inline void image_delete(unsigned char *image) {
free(image);
}
static inline void image_save_ppm(unsigned char *image, int width, int height, const char *filename) {
FILE *f = fopen(filename, "wb");
if (f) {
fprintf(f, "P6\n%d %d\n255\n", width, height);
fwrite(image, width * height * 3, 1, f);
fclose(f);
} else {
fprintf(stderr, "ERROR saving `%s'\n", filename);
}
}
static inline void image_poke(unsigned char *image, int width, int i, int j, int r, int g, int b) {
int k = (width * j + i) * 3;
image[k++] = r;
image[k++] = g;
image[k ] = b;
}
static inline void colour_hsv_to_rgb(double h, double s, double v, double *r, double *g, double *b) {
double i, f, p, q, t;
if (s == 0) { *r = *g = *b = v; } else {
h = 6 * (h - floor(h));
int ii = i = floor(h);
f = h - i;
p = v * (1 - s);
q = v * (1 - (s * f));
t = v * (1 - (s * (1 - f)));
switch(ii) {
case 0: *r = v; *g = t; *b = p; break;
case 1: *r = q; *g = v; *b = p; break;
case 2: *r = p; *g = v; *b = t; break;
case 3: *r = p; *g = q; *b = v; break;
case 4: *r = t; *g = p; *b = v; break;
default:*r = v; *g = p; *b = q; break;
}
}
}
static inline void colour_to_bytes(double r, double g, double b, int *r_out, int *g_out, int *b_out) {
*r_out = fmin(fmax(255 * r, 0), 255);
*g_out = fmin(fmax(255 * g, 0), 255);
*b_out = fmin(fmax(255 * b, 0), 255);
}
static inline void colour_mandelbrot(unsigned char *image, int width, int i, int j, int period, double distance) {
double r, g, b;
colour_hsv_to_rgb(period * colour_modulus, 0.5, tanh(distance), &r, &g, &b);
int ir, ig, ib;
colour_to_bytes(r, g, b, &ir, &ig, &ib);
image_poke(image, width, i, j, ir, ig, ib);
}
static inline void colour_analysis(unsigned char *image, int width, int i, int j, int bias, int outcome) {
int ir = 0, ig = 0, ib = 0;
if (bias == outcome) {
ig = 255;
} else if (bias == BIAS_INTERIOR && outcome == BIAS_EXTERIOR) {
ib = 255;
} else if (bias == BIAS_EXTERIOR && outcome == BIAS_INTERIOR) {
ir = 255;
}
image_poke(image, width, i, j, ir, ig, ib);
}
static inline int attractor(complex double *z_out, complex double *dz_out, complex double z0, complex double c, int period) {
double epsilon_2 = 1e-20;
complex double zz = z0;
for (int j = 0; j < 64; ++j) {
complex double z = zz;
complex double dz = 1;
for (int i = 0; i < period; ++i) {
dz = 2.0 * z * dz;
z = z * z + c;
}
complex double zz1 = zz - (z - zz) / (dz - 1.0);
if (cabs2(zz1 - zz) < epsilon_2) {
*z_out = z;
*dz_out = dz;
return 1;
}
zz = zz1;
}
return 0;
}
static inline double interior_distance(complex double z0, complex double c, int period) {
complex double z = z0;
complex double dz = 1;
complex double dzdz = 0;
complex double dc = 0;
complex double dcdz = 0;
for (int p = 0; p < period; ++p) {
dcdz = 2 * (z * dcdz + dz * dc);
dc = 2 * z * dc + 1;
dzdz = 2 * (dz * dz + z * dzdz);
dz = 2 * z * dz;
z = z * z + c;
}
return (1 - cabs2(dz)) / cabs(dcdz + dzdz * dc / (1 - dz));
}
struct partial {
complex double z;
int p;
};
static inline void render(unsigned char *image, int algorithm, int maxiters, int width, int height, complex double center, double radius) {
double pixel_spacing = radius / (height / 2.0);
#pragma omp parallel for schedule(dynamic, 1)
for (int j = 0; j < height; ++j) {
struct partial *partials = 0;
int bias = BIAS_EXTERIOR, new_bias, npartials;
if (algorithm & ALGORITHM_BIASED) {
partials = malloc(maxiters * sizeof(struct partial));
}
for (int i = 0; i < width; ++i) {
new_bias = BIAS_UNKNOWN;
npartials = 0;
double x = i + 0.5 - width / 2.0;
double y = height / 2.0 - j - 0.5;
complex double c = center + pixel_spacing * (x + I * y);
complex double z = 0;
complex double dc = 0;
double minimum_z2 = infinity;
int period = 0;
for (int n = 1; n <= maxiters; ++n) {
dc = 2 * z * dc + 1;
z = z * z + c;
double z2 = cabs2(z);
if (z2 < minimum_z2) {
minimum_z2 = z2;
period = n;
if (algorithm & (ALGORITHM_UNBIASED | ALGORITHM_BIASED)) {
if (algorithm & ALGORITHM_UNBIASED || bias == BIAS_INTERIOR) {
complex double z0 = 0, dz0 = 0;
if (attractor(&z0, &dz0, z, c, period)) {
if (cabs2(dz0) <= 1.0) {
if (algorithm & ALGORITHM_ANALYSE) {
colour_analysis(image, width, i, j, bias, BIAS_INTERIOR);
} else {
double distance = interior_distance(z0, c, period) / pixel_spacing;
colour_mandelbrot(image, width, i, j, period, distance);
}
new_bias = BIAS_INTERIOR;
break;
}
}
} else if (algorithm & ALGORITHM_BIASED) {
partials[npartials].z = z;
partials[npartials].p = period;
npartials++;
}
}
}
if (z2 >= escape_radius_2) {
if (algorithm & ALGORITHM_ANALYSE) {
colour_analysis(image, width, i, j, bias, BIAS_EXTERIOR);
} else {
double distance = sqrt(z2) * log(z2) / (cabs(dc) * pixel_spacing);
colour_mandelbrot(image, width, i, j, period, distance);
}
new_bias = BIAS_EXTERIOR;
break;
}
}
if (algorithm & ALGORITHM_BIASED) {
if (bias == BIAS_EXTERIOR && new_bias == BIAS_UNKNOWN) {
for (int n = 0; n < npartials; ++n) {
complex double z = partials[n].z;
int period = partials[n].p;
complex double z0 = 0, dz0 = 0;
if (attractor(&z0, &dz0, z, c, period)) {
if (cabs2(dz0) <= 1.0) {
if (algorithm & ALGORITHM_ANALYSE) {
colour_analysis(image, width, i, j, bias, BIAS_INTERIOR);
} else {
double distance = interior_distance(z0, c, period) / pixel_spacing;
colour_mandelbrot(image, width, i, j, period, distance);
}
new_bias = BIAS_INTERIOR;
break;
}
}
}
}
}
if (new_bias == BIAS_UNKNOWN) {
if (algorithm & ALGORITHM_ANALYSE) {
colour_analysis(image, width, i, j, bias, BIAS_UNKNOWN);
} else {
if (algorithm & (ALGORITHM_UNBIASED | ALGORITHM_BIASED)) {
colour_mandelbrot(image, width, i, j, period, 0.0);
} else {
colour_mandelbrot(image, width, i, j, period, 10.0);
}
}
new_bias = BIAS_EXTERIOR;
}
bias = new_bias;
}
if (algorithm & ALGORITHM_BIASED) {
free(partials);
}
}
}
int main(int argc, char **argv) {
if (argc != 9) {
fprintf(stderr,
"usage: %s algorithm maxiters width height creal cimag radius filename\n"
"values for algorithm:\n"
" 0 plain (exterior only)\n"
" 1 unbiased (exterior and interior, unoptimized)\n"
" 2 biased (exterior and interior optimized by local connectedness)\n"
" 6 analysis map of biased rendering\n"
, argv[0]);
return 1;
}
int algorithm = atoi(argv[1]);
int maxiters = atoi(argv[2]);
int width = atoi(argv[3]);
int height = atoi(argv[4]);
complex double center = atof(argv[5]) + I * atof(argv[6]);
double radius = atof(argv[7]);
const char *filename = argv[8];
unsigned char *image = image_new(width, height);
render(image, algorithm, maxiters, width, height, center, radius);
image_save_ppm(image, width, height, filename);
image_delete(image);
return 0;
}