File: fractal.cpp

package info (click to toggle)
arrayfire 3.3.2%2Bdfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 109,016 kB
  • sloc: cpp: 127,909; lisp: 6,878; python: 3,923; ansic: 1,051; sh: 347; makefile: 338; xml: 175
file content (106 lines) | stat: -rw-r--r-- 2,906 bytes parent folder | download
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
/*******************************************************
 * Copyright (c) 2014, ArrayFire
 * All rights reserved.
 *
 * This file is distributed under 3-clause BSD license.
 * The complete license agreement can be obtained at:
 * http://arrayfire.com/licenses/BSD-3-Clause
 ********************************************************/

#include <stdio.h>
#include <iostream>
#include <arrayfire.h>
#include <cmath>
#include <cstdlib>

#define WIDTH 400 // Width of image
#define HEIGHT 400 // Width of image

using namespace af;
using std::abs;

array complex_grid(int width, int height, float zoom, float center[2])
{

    // Generate sequences of length width, height
    array X = (iota(dim4(1, height), dim4(width , 1)) -  (float)height / 2.0) / zoom + center[0];
    array Y = (iota(dim4(width , 1), dim4(1, height)) -  (float)width  / 2.0) / zoom + center[1];

    // Return the locations as a complex grid
    return complex(X, Y);
}

array mandelbrot(const array &in, int iter, float maxval)
{
    array C = in;
    array Z = C;
    array mag = constant(0, C.dims());

    for (int ii = 1; ii < iter; ii++) {

        // Do the calculation
        Z = Z * Z + C;

        // Get indices where abs(Z) crosses maxval
        array cond = (abs(Z) > maxval).as(f32);
        mag = af::max(mag, cond * ii);

        // If abs(Z) cross maxval, turn off those locations
        C = C * (1 - cond);
        Z = Z * (1 - cond);

        // Ensuring the JIT does not become too large
        C.eval();
        Z.eval();
    }

    // Normalize
    return mag / maxval;
}

array normalize(array a)
{
    float mx = af::max<float>(a);
    float mn = af::min<float>(a);
    return (a-mn)/(mx-mn);
}

int main(int argc, char **argv)
{
    int device = argc > 1 ? atoi(argv[1]) : 0;
    int iter = argc > 2 ? atoi(argv[2]) : 100;
    bool console = argc > 2 ? argv[2][0] == '-' : false;
    try {
        af::setDevice(device);
        info();
        printf("** ArrayFire Fractals Demo **\n");
        af::Window wnd(WIDTH, HEIGHT, "Fractal Demo");
        wnd.setColorMap(AF_COLORMAP_SPECTRUM);

        float center[] = {-0.75, 0.1};
        // Keep zomming out for each frame
        for (int i = 10; i < 400; i++) {
            int zoom = i * i;
            if(!(i % 10)) printf("iteration: %d zoom: %d\n", i, zoom); fflush(stdout);

            // Generate the grid at the current zoom factor
            array c = complex_grid(WIDTH, HEIGHT, zoom, center);

            iter =sqrt(abs(2*sqrt(abs(1-sqrt(5*zoom)))))*100;
            // Generate the mandelbrot image
            array mag = mandelbrot(c, iter, 1000);

            if(!console) {
                if (wnd.close()) break;
                array mag_norm = normalize(mag);
                wnd.image(mag_norm);
            }
        }

    } catch (af::exception &e) {
        fprintf(stderr, "%s\n", e.what());
        throw;
    }

    return 0;
}