File: fractal.cpp

package info (click to toggle)
tbb 4.0%2Br233-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 14,396 kB
  • sloc: cpp: 78,709; ansic: 6,200; asm: 950; makefile: 875; sh: 107; pascal: 68
file content (272 lines) | stat: -rw-r--r-- 8,349 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
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
/*
    Copyright 2005-2011 Intel Corporation.  All Rights Reserved.

    This file is part of Threading Building Blocks.

    Threading Building Blocks is free software; you can redistribute it
    and/or modify it under the terms of the GNU General Public License
    version 2 as published by the Free Software Foundation.

    Threading Building Blocks is distributed in the hope that it will be
    useful, but WITHOUT ANY WARRANTY; without even the implied warranty
    of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Threading Building Blocks; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

    As a special exception, you may use this file as part of a free software
    library without restriction.  Specifically, if other files instantiate
    templates or use macros or inline functions from this file, or you compile
    this file and link it with other files to produce an executable, this
    file does not by itself cause the resulting executable to be covered by
    the GNU General Public License.  This exception does not however
    invalidate any other reasons why the executable file might be covered by
    the GNU General Public License.
*/

#include "fractal.h"

#include "tbb/compat/thread"
#include "tbb/parallel_for.h"
#include "tbb/blocked_range2d.h"
#include "tbb/task_scheduler_init.h"
#include "tbb/tick_count.h"

#include <math.h>
#include <stdio.h>

video *v;
extern bool silent;
extern int grain_size;

color_t fractal::calc_one_pixel(int x0, int y0) {
    int iter;
    double fx0, fy0, xtemp, x, y, mu;

    color_t color;

    fx0 = (double)x0 - (double) size_x / 2.0;
    fy0 = (double)y0 - (double) size_y / 2.0;
    fx0 = fx0 / magn + cx;
    fy0 = fy0 / magn + cy;

    iter = 0; x = 0; y = 0;

    while (((x*x + y*y) <= 4) && (iter < max_iterations)) { 
        xtemp = x*x - y*y + fx0;
        y = 2*x*y + fy0;
        x = xtemp;
        iter++;
    }

    if (iter == max_iterations) {
        // point corresponds to the mandelbrot set
        color = v->get_color(255, 255, 255);
        return color;
    }

    // compute again but with exponent calculation at each iteration
    // it's all for coloring point outside the mandelbrot set
    iter = 0; x = 0; y = 0;
    mu = 0;
    while (((x*x + y*y) <= 4) && (iter < max_iterations)) { 
        xtemp = x*x - y*y + fx0;
        y = 2*x*y + fy0;
        x = xtemp;
        mu += exp(-sqrt(x*x+y*y));
        iter++;
    }

    int b = (int)(256*mu);
    int g = (b/8);
    int r = (g/16);

    b = b>255 ? 255 : b;
    g = g>255 ? 255 : g;
    r = r>255 ? 255 : r;

    color = v->get_color(r, g, b);
    return color;
}

void fractal::clear() {
    drawing_area area( off_x, off_y, size_x, size_y, dm) ;

    // fill the rendering area with black color
    for (int y=0; y<size_y; ++y) {
        area.set_pos( 0, y );
        for (int x=0; x<size_x; ++x) {
            area.put_pixel( v->get_color(0, 0, 0) );
        }
    }
}

void fractal::draw_border( bool is_active ) {
    color_t color = is_active ? v->get_color(0, 255, 0) // green color
                                : v->get_color(96, 128, 96); // green-gray color

    // top border
    drawing_area area0( off_x-1, off_y-1, size_x+2, 1, dm );
    for (int i=-1; i<size_x+1; ++i)
        area0.put_pixel(color);
    // bottom border
    drawing_area area1( off_x-1, off_y+size_y, size_x+2, 1, dm );
    for (int i=-1; i<size_x+1; ++i)
        area1.put_pixel(color);
    // left border
    drawing_area area2( off_x-1, off_y, 1, size_y+2, dm );
    for (int i=0; i<size_y; ++i)
        area2.set_pixel(0, i, color);
    // right border
    drawing_area area3( size_x+off_x, off_y, 1, size_y+2, dm );
    for (int i=0; i<size_y; ++i)
        area3.set_pixel(0, i, color);
}

void fractal::render_rect( int x0, int y0, int x1, int y1 ) {
    // render the specified rectangle area
    drawing_area area(off_x+x0, off_y+y0, x1-x0, y1-y0, dm);
    for ( int y=y0; y<y1; ++y ) {
        area.set_pos( 0, y-y0 );
        for ( int x=x0; x<x1; ++x ) {
            area.put_pixel( calc_one_pixel( x, y ) );
        }
    }
}

class fractal_body {
    fractal &f;
public:
    void operator()( tbb::blocked_range2d<int> &r ) const {
        if ( v->next_frame() )
            f.render_rect( r.cols().begin(), r.rows().begin(), r.cols().end(), r.rows().end() );
    }

    fractal_body( fractal &f ) : f(f) {
    }
};

void fractal::render( tbb::task_group_context &context ) {
    // run parallel_for that process the fractal area
    tbb::parallel_for( tbb::blocked_range2d<int>(0, size_y, grain_size, 0, size_x, grain_size ), 
            fractal_body(*this), tbb::simple_partitioner(), context);
}

void fractal::run( tbb::task_group_context &context ) {
    clear();
    render( context );
}

bool fractal::check_point( int x, int y ) {
    return x >= off_x && x <= off_x+size_x && 
            y >= off_y && y <= off_y+size_y;
}

void fractal_group::calc_fractal( int num ) {
    // calculate the fractal
    fractal &f = num ? f1 : f0;

    tbb::tick_count t0 = tbb::tick_count::now();
    while ( v->next_frame() && num_frames[num] != 0 ) {
        f.run( context[num] );
        if ( num_frames[num]>0 ) num_frames[num] -= 1;
    }
    tbb::tick_count t1 = tbb::tick_count::now();

    if ( !silent ) {
        printf("  %s fractal finished. Time: %g\n", num ? "Second" : "First", (t1-t0).seconds());
    }
}

void fg_thread_func(fractal_group *fg) {
    // initialize the task scheduler for the second thread
    tbb::task_scheduler_init init( fg->get_num_threads() );
    // calculate the second fractal
    fg->calc_fractal( 1 );
}

void fractal_group::set_priorities() {
    // set the high priority for the active area and the normal priority for another area
    context[active].set_priority( tbb::priority_high );
    context[active^1].set_priority( tbb::priority_normal );
}

void fractal_group::switch_priorities( int new_active ) {
    if( new_active!=-1 ) active = new_active;
    else                 active = 1-active; // assumes 'active' is only 0 or 1
    set_priorities();
    draw_borders();
}

void fractal_group::set_num_frames_at_least(int n) {
    if ( num_frames[0]<n ) num_frames[0] = n;
    if ( num_frames[1]<n ) num_frames[1] = n;
}

void fractal_group::run( bool create_second_fractal ) {
    // initialize task scheduler
    tbb::task_scheduler_init init( num_threads );

    // create contexts to manage fractal priorities
    context = new tbb::task_group_context[2];

    set_priorities();
    draw_borders();

    // the second fractal is calculating on separated thread
    std::thread *fg_thread = 0;
    if ( create_second_fractal ) fg_thread = new std::thread( fg_thread_func, this );

    // calculate the first fractal
    calc_fractal( 0 );

    if ( fg_thread ) {
        // wait for second fractal
        fg_thread->join();
        delete fg_thread;
    }

    delete[] context;
}

void fractal_group::draw_borders() {
    f0.draw_border( active==0 );
    f1.draw_border( active==1 );
}

fractal_group::fractal_group( const drawing_memory &_dm, int _num_threads, int _max_iterations, int _num_frames ) : f0(_dm), f1(_dm), num_threads(_num_threads) {
    // set rendering areas
    f0.size_x = f1.size_x = _dm.sizex/2-4;
    f0.size_y = f1.size_y = _dm.sizey-4;
    f0.off_x = f0.off_y = f1.off_y = 2;
    f1.off_x = f0.size_x+4+2;

    // set fractals parameters
    f0.cx = -0.6f; f0.cy = 0.0f; f0.magn = 200.0f;
    f1.cx = -0.6f; f1.cy = 0.0f; f1.magn = 200.0f;
    f0.max_iterations = f1.max_iterations = _max_iterations;

    // initially the first fractal is active
    active = 0;

    num_frames[0] = num_frames[1] = _num_frames;
}

void fractal_group::mouse_click(int x, int y) {
    // assumption that the point is not inside any fractal area
    int new_active = -1;

    if ( f0.check_point( x, y ) ) {
        // the point is inside the first fractal area
        new_active = 0;
    } else if ( f1.check_point( x, y ) ) {
        // the point is inside the second fractal area
        new_active = 1;
    }

    if ( new_active != -1 && new_active != active ) {
        switch_priorities( new_active );
    }
}