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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
|
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <getopt.h>
#include <string.h>
#include <time.h>
#include "grids.h"
#include "precision.h"
#include "fractals.h"
#define EXIT_BAD_ARGUMENT 2
#ifndef NUM_RUNS
#define NUM_RUNS 5
#endif
/*
* Prints out usage information for the program
*/
void print_usage(FILE* file, const char* program_name){
fprintf(file, "Usage: %s [-v] [-i iterations] [-x x_res] [-y y_res] [-z magnification] [-d degree] [-c constant] [-r radius] [-l lower_left] [-u upper_right] [-o output_grid] -f fractal\n", program_name);
}
/*
* Print out the full help message for the program
*/
void print_help(){
printf("Options:\n"
" -i, --iterations <value> the number of iterations (default: 25, max 255)\n"
" -x, --x-res <value> the horizontal resolution (default: terminal width)\n"
" -y, --y-res <value> the vertical resolution (default: terminal height)\n"
" -l, --lower-left <value> Set the lower left corner of the fractal area (default: -2.0+-2.0i)\n"
" -u, --upper-right <value> Set the upper right corner of the fractal area (default: 2.0+2.0i)\n"
" -z, --magnification <value> Set the magnification factor (default: 1)\n"
" -d, --degree <value> Set the degree for fractals that use it (default: 2)\n"
" -c, --constant <value> Set the constant for fractals that use it (default: 0+0i)\n"
" -r, --radius <value> Set the radius for fractals that use it (default: 2)\n"
" -o, --output <filename> the output filename (default: fractal.grid)\n"
" -f, --fractal <type> the fractal type (default: mandelbrot)\n"
" supported fractals: mandelbrot, tricorn, multibrot, multicorn, burning_ship, julia\n"
" -p, --performance print performance info\n"
" -v, --verbose verbose output\n"
" -h, --help prints this help message\n"
"\ndegree is mutually exclusive with constant and radius\n"
"\nExits with a status code of 1 if the program encounters an error, exits with 2 if an argument is incorrect\n");
}
/*
* Prints out additional information about the program, such as the precision it was compiled with
*/
void print_info(const char* program_name){
#ifdef EXTENDED_PRECISION
printf("Compiled with long double float precision\n");
#endif
#ifndef EXTENDED_PRECISION
printf("%s complied with double float precision\n", program_name);
#endif
}
/*
* Runs a fractal generator NUM_RUNS times and returns the average of those runs
*/
double time_fractal(fractal_generator generator, grid_t* grid, grid_gen_params* params){
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);
for(size_t i = 0; i < NUM_RUNS; i++){
generator(grid, params);
}
clock_gettime(CLOCK_MONOTONIC, &end);
return (end.tv_sec - start.tv_sec + (end.tv_nsec - start.tv_nsec) * 1.0e-9) / NUM_RUNS;
}
static inline void parse_complex(const char* string, complex_t* z){
if(sscanf(string, CFORMAT "+" CFORMAT "i", &z->re, &z->im) != 2){
fprintf(stderr, "Failed while parsing complex number: %s , is it formatted correctly?\n", string);
exit(EXIT_FAILURE);
};
}
/*
* Parses a fractal generator form a string, exitting if it is supplied a parameter which it doesn't support
*/
fractal_generator parse_fractal_generator(const char* argument, const bool param_is_degree, const bool param_is_cr){
if(strncmp(argument, "mandelbrot", strlen("mandelbrot")) == 0) {
return mandelbrot_grid;
}
else if(strncmp(argument, "tricorn", strlen("tricorn")) == 0) {
return tricorn_grid;
}
else if(strncmp(argument, "multibrot", strlen("multibrot")) == 0) {
if(param_is_cr){
fprintf(stderr, "multibrot requires a degree, not constant and radius, exitting\n");
exit(EXIT_BAD_ARGUMENT);
}
return multibrot_grid;
}
else if(strncmp(argument, "multicorn", strlen("multicorn")) == 0) {
if(param_is_cr){
fprintf(stderr, "multicorn requires a degree, not constant and radius, exitting\n");
exit(EXIT_BAD_ARGUMENT);
}
return multicorn_grid;
}
else if(strncmp(argument, "burning_ship", strlen("burning_ship")) == 0) {
return burning_ship_grid;
}
else if(strncmp(argument, "julia", strlen("julia")) == 0) {
if(param_is_degree){
fprintf(stderr, "julia requires a constant and a radius, not a degree, exitting\n");
exit(EXIT_BAD_ARGUMENT);
}
return julia_grid;
}
else {
fprintf(stderr, "Invalid fractal type: %s, see --help for a list of supported fractals\n", argument);
exit(EXIT_BAD_ARGUMENT);
}
}
int main(const int argc, char *argv[]) {
struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
//default values
byte iterations = 25;
size_t x_res = w.ws_col;
size_t y_res = w.ws_row;
complex_t lower_left = { .re = -2, .im = -2};
complex_t upper_right = { .re = 2, .im = 2};
CBASE magnification = 1;
bool verbose = false;
bool performance = false;
//degree is mutually exclusive with constant and radius
//this could be simplified if grid_gen_params was a struct instead of a union but ¯\_(ツ)_/¯
bool param_is_degree = false;
bool param_is_cr = false;
CBASE degree = 2;
complex_t constant = { .re = 0, .im = 0};
double radius = 2;
char* fractal_name = "mandelbrot";
fractal_generator generator = mandelbrot_grid;
char* output_filename = "fractal.grid";
grid_gen_params* params = malloc(sizeof(grid_gen_params));
if(!params){
fprintf(stderr, "Failed to allocate memory: %zu bytes\n", sizeof(grid_gen_params));
exit(EXIT_FAILURE);
}
static struct option long_options[] = {
{"iterations", required_argument, NULL, 'i'},
{"x-res", required_argument, NULL, 'x'},
{"y-res", required_argument, NULL, 'y'},
{"lower-left", required_argument, NULL, 'l'},
{"upper-right", required_argument, NULL, 'u'},
{"magnification", required_argument, NULL, 'z'},
{"degree", required_argument, NULL, 'd'},
{"constant", required_argument, NULL, 'c'},
{"radius", required_argument, NULL, 'r'},
{"output", required_argument, NULL, 'o'},
{"verbose", no_argument, NULL, 'v'},
{"performance", no_argument, NULL, 'p'},
{"help", no_argument, NULL, 'h'},
{"fractal", required_argument, NULL, 'f'},
{0, 0, 0, 0} // Termination element
};
unsigned long temp;
//parse command line arguments
int opt;
while((opt = getopt_long(argc, argv, "i:x:y:l:u:z:d:c:r:o:vphf:", long_options, NULL)) != -1){
switch(opt){
case 'i':
temp = strtoul(optarg, NULL, 10);
if(temp > 255){
fprintf(stderr, "Iterations above maximum, setting to 255\n");
iterations = 255;
}
else {
iterations = temp;
}
break;
case 'x':
x_res = strtoull(optarg, NULL, 10);
break;
case 'y':
y_res = strtoull(optarg, NULL, 10);
break;
case 'l':
parse_complex(optarg, &lower_left);
break;
case 'u':
parse_complex(optarg, &upper_right);
break;
case 'o':
output_filename = optarg;
break;
case 'd':
param_is_degree = true;
if(param_is_cr){
fprintf(stderr, "--degree and --constant --radius are mutually exclusive, exiting\n");
exit(EXIT_BAD_ARGUMENT);
}
if(sscanf(optarg, CFORMAT, °ree) != 1){
fprintf(stderr, "Failed to parse degree: %s, exitting\n", optarg);
exit(EXIT_BAD_ARGUMENT);
}
break;
case 'c':
if(param_is_degree){
fprintf(stderr, "--degree and --constant --radius are mutually exclusive, exiting\n");
exit(EXIT_BAD_ARGUMENT);
}
parse_complex(optarg, &constant);
param_is_cr = true;
break;
case 'r':
if(param_is_degree){
fprintf(stderr, "--degree and --constant --radius are mutually exclusive, exiting\n");
exit(EXIT_BAD_ARGUMENT);
}
if(sscanf(optarg, CFORMAT, &radius) != 1){
fprintf(stderr, "Failed to parse radius: %s, exitting\n", optarg);
exit(EXIT_BAD_ARGUMENT);
}
param_is_cr = true;
break;
case 'f':
fractal_name = optarg;
generator = parse_fractal_generator(optarg, param_is_degree, param_is_cr);
break;
case 'z':
if(sscanf(optarg, CFORMAT, &magnification) != 1){
fprintf(stderr, "Failed to parse magnification: %s, exitting\n", optarg);
exit(EXIT_BAD_ARGUMENT);
}
if(magnification <= 0){
fprintf(stderr, "Invalid magnification "CFORMAT", exitting\n", magnification);
exit(EXIT_BAD_ARGUMENT);
}
break;
case 'v':
verbose = true;
break;
case 'p':
performance = true;
break;
case 'h':
print_usage(stdout, argv[0]);
print_help();
return 0;
default:
print_usage(stderr, argv[0]);
return 2;
}
}
if(param_is_degree){
params->degree = degree;
}
else {
params->cr.constant = constant;
params->cr.radius = radius;
}
grid_t* grid = create_grid(x_res, y_res, iterations, lower_left, upper_right);
if(!grid) return 1;
if(magnification != 1){
zoom_grid(grid, magnification);
}
generator(grid, params);
if(performance){
double time = time_fractal(generator, grid, params);
printf("%s,%s,%lf,"CFORMAT","CFORMAT",%lf,%hhu,%zu,%zu,",
argv[0], fractal_name, degree, constant.re, constant.im, radius, iterations, x_res, y_res);
printf(CFORMAT","CFORMAT","CFORMAT","CFORMAT",%f\n",
lower_left.re, lower_left.im, upper_right.re, upper_right.im, time);
}
if(verbose){
print_info(argv[0]);
printf("Magnification:\t"CFORMAT"\n", magnification);
print_grid_info(grid);
}
if(!performance){
//uses "safer" versions of c string functions
//likely aren't necessary unless a user can pass non-null terminated strings as arguments, but that would likely break something up in getopt
if(output_filename[0] == '-' && strnlen(output_filename, 16) == 1){
if(write_grid(stdout, grid) == GRID_WRITE_ERROR){
fprintf(stderr, "Error occured while writting to file %s\n", output_filename);
}
}
else {
FILE* file = fopen(output_filename, "wb");
if(!file){
perror("Error occured while trying to write");
}
else if(write_grid(file, grid) == GRID_WRITE_ERROR){
fprintf(stderr, "Error occured while writting to file %s\n", output_filename);
}
fclose(file);
}
}
free(params);
free_grid(grid);
return 0;
}
|