File: Quadtree.cpp

package info (click to toggle)
vite 1.4-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 19,112 kB
  • sloc: cpp: 30,167; makefile: 467; sh: 233; python: 140; ansic: 67
file content (348 lines) | stat: -rw-r--r-- 12,268 bytes parent folder | download | duplicates (3)
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/**
 *
 * @file plugins/MatrixVisualizer/Common/Quadtree.cpp
 *
 * @copyright 2008-2024 Bordeaux INP, CNRS (LaBRI UMR 5800), Inria,
 *                      Univ. Bordeaux. All rights reserved.
 *
 * @author Camille Ordronneau
 *
 * @date 2024-07-17
 */
#include <assert.h>
#include <cmath>

#include "Quadtree.hpp"

/**
 * This constant is the precision where the algorithm stops slicing the matrix
 */
#define PRECISION 1.e-15

Quadtree::Quadtree(symbol_matrix_t *matrix) :
    Zoom(matrix) {
    // Fill correct colors
    move(0.f, 1.f, 0.f, 1.f);
}

Quadtree::~Quadtree() { }

void Quadtree::move(double x_start, double x_end, double y_start, double y_end) {
    // Check positions
    assert(x_end >= x_start);
    assert(y_end >= y_start);
    assert(y_end >= x_start);

    // Check for out of bounds
    assert(x_start >= 0.);
    assert(y_start >= 0.);
    assert(x_end <= 1.);
    assert(y_end <= 1.);

    sliced_matrix_t m;

    // Convert to column/row indexes
    m.start_col = x_start * m_matrix->m_colsnbr;
    m.end_col = x_end * m_matrix->m_colsnbr - 1;
    m.start_row = y_start * m_matrix->m_rowsnbr;
    m.end_row = y_end * m_matrix->m_rowsnbr - 1;
    m.start_cblk = 0;
    m.end_cblk = m_matrix->m_cblknbr - 1;
    m.m_matrix = m_matrix;

    this->start_col = m.start_col;
    this->start_row = m.start_row;
    this->end_col = m.end_col;
    this->end_row = m.end_row;

    int nb_cols = m.end_col - m.start_col + 1;
    int nb_rows = m.end_row - m.start_row + 1;

    float y_coeff = (float)DEFAULT_LEVEL / ((float)nb_rows);
    float x_coeff = (float)DEFAULT_LEVEL / ((float)nb_cols);

    // fill the color matrix with white color
    for (int i = 0; i < DEFAULT_LEVEL; ++i) {
        for (int j = 0; j < DEFAULT_LEVEL; ++j) {
            m_colors[i][j] = 1.f;
        }
    }

    // if the size of the matrix is less than DEFAULT_LEVEL, the zooming method is used
    if (nb_cols < DEFAULT_LEVEL || nb_rows < DEFAULT_LEVEL) {
        symbol_cblk_t *cblk;
        symbol_blok_t *blok;
        int start_cblk = 0;
        int end_cblk = 0;
        cblk = m_matrix->m_cblktab;
        for (int i = 0; i < m_matrix->m_cblknbr; ++i, cblk++) {
            if ((cblk->m_fcolnum <= start_col) && (start_col <= cblk->m_lcolnum)) {
                start_cblk = i;
                start_col = cblk->m_fcolnum;
            }
            if ((cblk->m_fcolnum <= start_row) && (start_row <= cblk->m_lcolnum)) {
                start_row = cblk->m_fcolnum;
            }
            if ((cblk->m_fcolnum <= end_col) && (end_col <= cblk->m_lcolnum)) {
                end_cblk = i + 1;
                end_col = cblk->m_lcolnum;
            }
            if ((cblk->m_fcolnum <= end_row) && (end_row <= cblk->m_lcolnum)) {
                end_row = cblk->m_lcolnum;
            }
        }

        nb_cols = end_col - start_col + 1;
        nb_rows = end_row - start_row + 1;

        x_coeff = (float)DEFAULT_LEVEL / ((float)nb_cols);
        y_coeff = (float)DEFAULT_LEVEL / ((float)nb_rows);

        cblk = m_matrix->m_cblktab + start_cblk;
        for (int i = start_cblk; i < end_cblk; ++i, cblk++) {
            int fbloknum = cblk[0].m_bloknum;
            int lbloknum = cblk[1].m_bloknum;

            // Get first block size in col from x to x_end
            int xbegin = (cblk->m_fcolnum - start_col) * x_coeff;
            int xend = (cblk->m_lcolnum + 1 - start_col) * x_coeff;

            float cblk_color = cblk->m_color;

            blok = m_matrix->m_bloktab + fbloknum;
            for (int j = fbloknum; j < lbloknum; ++j, blok++) {
                if ((blok->m_lrownum < start_row) || (blok->m_frownum > end_row)) {
                    continue;
                }

                // Get first block size in row from y to y_end
                int ybegin = (blok->m_frownum - start_row) * y_coeff;
                int yend = (blok->m_lrownum + 1 - start_row) * y_coeff;

                float color = blok->m_color == -1. ? cblk_color : blok->m_color;

                for (int m = xbegin; m < xend; m++) {
                    for (int n = ybegin; n < yend; n++) {
                        m_colors[m][n] = color;
                    }
                }
            }
        }
    }
    // else the quadtree method is used
    else {
        createTiles(m, x_coeff, y_coeff);
    }
}

void Quadtree::createTiles(sliced_matrix_t &m, float x_coeff, float y_coeff) {
    symbol_cblk_t *cblk;
    int start_cblk = m.start_cblk;
    int end_cblk = m.end_cblk;

    // Slice the matrix into 4 sliced matrixes
    for (int i = m.start_row; i < m.end_row; i += (m.end_row - m.start_row + 1) / 2) {
        for (int j = m.start_col; j < m.end_col; j += (m.end_col - m.start_col + 1) / 2) {
            sliced_matrix_t new_matrix;
            new_matrix.m_matrix = m.m_matrix;
            // Fist row and column of the new matrix
            new_matrix.start_row = i;
            new_matrix.start_col = j;

            // Calcute the last row and column of the new matrix
            if (i + ((m.end_row - m.start_row + 1) / 2) == m.end_row) {
                new_matrix.end_row = m.end_row;
            }
            else {
                new_matrix.end_row = i + ((m.end_row - m.start_row + 1) / 2) - 1;
            }

            if (j + ((m.end_col - m.start_col + 1) / 2) == m.end_col) {
                new_matrix.end_col = m.end_col;
            }
            else {
                new_matrix.end_col = j + ((m.end_col - m.start_col + 1) / 2) - 1;
            }

            cblk = new_matrix.m_matrix->m_cblktab;
            // first and last column block of the new matrix
            for (int k = 0; k < new_matrix.m_matrix->m_cblknbr; ++k, cblk++) {
                if ((cblk->m_fcolnum <= new_matrix.start_col) && (new_matrix.start_col <= cblk->m_lcolnum)) {
                    start_cblk = k;
                }
                if ((cblk->m_fcolnum <= new_matrix.end_col) && (new_matrix.end_col <= cblk->m_lcolnum)) {
                    end_cblk = k;
                }
            }
            new_matrix.start_cblk = start_cblk;
            new_matrix.end_cblk = end_cblk;

            // Calculate the average color
            new_matrix.color_avg = average(new_matrix);
            // Calculate the color error
            float color_error = error(new_matrix);
            if (color_error <= PRECISION || ((new_matrix.end_row - new_matrix.start_row) / 2) * y_coeff <= 1 || ((new_matrix.end_col - new_matrix.start_col) / 2) * x_coeff <= 1) {
                int x, y, x_end, y_end;

                // Get the start and the end of the zone to be fill in the color matrix
                x = (new_matrix.start_col - this->start_col) * x_coeff;
                x_end = (new_matrix.end_col - this->start_col) * x_coeff;

                y_end = (new_matrix.end_row - this->start_row) * y_coeff;
                y = (new_matrix.start_row - this->start_row) * y_coeff;

                // Fill the color matrix
                for (int m = x; m <= x_end; m++) {
                    for (int n = y; n <= y_end; n++) {
                        m_colors[m][n] = new_matrix.color_avg;
                    }
                }
            }
            else {
                // recursive call which means reslice the matrix again
                createTiles(new_matrix, x_coeff, y_coeff);
            }
        }
    }
}

float Quadtree::average(sliced_matrix_t &m) {
    symbol_cblk_t *cblk;
    symbol_blok_t *blok;
    int start_cblk = m.start_cblk;
    int end_cblk = m.end_cblk;
    cblk = m.m_matrix->m_cblktab + start_cblk;
    float average = 0.;
    for (int i = start_cblk; i <= end_cblk; ++i, cblk++) {

        int x = 0, x_end = 0;

        // Get first block size in col from x to x_end
        if (i == start_cblk && i != end_cblk) {
            x = m.start_col;
            x_end = cblk->m_lcolnum;
        }
        else if (i == end_cblk && i != start_cblk) {
            x = cblk->m_fcolnum;
            x_end = m.end_col;
        }
        else if (i == end_cblk && i == start_cblk) {
            x = m.start_col;
            x_end = m.end_col;
        }
        else {
            x = cblk->m_fcolnum;
            x_end = cblk->m_lcolnum;
        }

        float cblk_color = cblk->m_color;
        int fbloknum = cblk[0].m_bloknum;
        int lbloknum = cblk[1].m_bloknum;

        blok = m.m_matrix->m_bloktab + fbloknum;
        float somme = 0.;
        int y = 0, y_end = 0;
        for (int j = fbloknum; j < lbloknum; ++j, blok++) {
            if ((blok->m_lrownum < m.start_row) || (blok->m_frownum > m.end_row)) {
                continue;
            }

            // Get first block size in row from y to y_end
            if ((blok->m_lrownum >= m.start_row) && (blok->m_lrownum < m.end_row)) {
                y = m.start_row;
                y_end = blok->m_lrownum;
            }
            else if ((blok->m_frownum <= m.end_row) && (blok->m_frownum > m.start_row)) {
                y = blok->m_frownum;
                y_end = m.end_row;
            }
            else if ((blok->m_frownum < m.start_row) && (blok->m_lrownum > m.end_row)) {
                y = m.start_row;
                y_end = m.end_row;
            }
            else {
                y = blok->m_frownum;
                y_end = blok->m_lrownum;
            }

            float color = blok->m_color == -1. ? cblk_color : blok->m_color;

            somme += (y_end - y + 1) * (x_end - x + 1);
            average += (float)((y_end - y + 1) * (x_end - x + 1) * color) / ((m.end_col - m.start_col) * (m.end_row - m.start_row));
        }
        average += (float)(((x_end - x + 1) * (m.end_row - m.start_row) - somme) * 1.f) / ((m.end_col - m.start_col) * (m.end_row - m.start_row));
    }
    if (average >= 1.f) {
        return 1.f;
    }
    return average;
}

float Quadtree::error(sliced_matrix_t &m) {
    symbol_cblk_t *cblk;
    symbol_blok_t *blok;
    int start_cblk = m.start_cblk;
    int end_cblk = m.end_cblk;
    cblk = m.m_matrix->m_cblktab + start_cblk;
    float err = 0.;
    for (int i = start_cblk; i <= end_cblk; ++i, cblk++) {

        int x = 0, x_end = 0;

        // Get first block size in col from x to x_end
        if (i == start_cblk && i != end_cblk) {
            x = m.start_col;
            x_end = cblk->m_lcolnum;
        }
        else if (i == end_cblk && i != start_cblk) {
            x = cblk->m_fcolnum;
            x_end = m.end_col;
        }
        else if (i == end_cblk && i == start_cblk) {
            x = m.start_col;
            x_end = m.end_col;
        }
        else {
            x = cblk->m_fcolnum;
            x_end = cblk->m_lcolnum;
        }

        float cblk_color = cblk->m_color;
        int fbloknum = cblk[0].m_bloknum;
        int lbloknum = cblk[1].m_bloknum;

        blok = m.m_matrix->m_bloktab + fbloknum;
        float somme = 0.;
        int y = 0, y_end = 0;
        for (int j = fbloknum; j < lbloknum; ++j, blok++) {
            if ((blok->m_lrownum < m.start_row) || (blok->m_frownum > m.end_row)) {
                continue;
            }

            // Get first block size in row from y to y_end
            if ((blok->m_lrownum >= m.start_row) && (blok->m_lrownum < m.end_row)) {
                y = m.start_row;
                y_end = blok->m_lrownum;
            }
            else if ((blok->m_frownum <= m.end_row) && (blok->m_frownum > m.start_row)) {
                y = blok->m_frownum;
                y_end = m.end_row;
            }
            else if ((blok->m_frownum < m.start_row) && (blok->m_lrownum > m.end_row)) {
                y = m.start_row;
                y_end = m.end_row;
            }
            else {
                y = blok->m_frownum;
                y_end = blok->m_lrownum;
            }

            float color = blok->m_color == -1. ? cblk_color : blok->m_color;

            somme += (y_end - y + 1) * (x_end - x + 1);
            err += (float)((y_end - y + 1) * (x_end - x + 1) * (color - m.color_avg)) / (float)((m.end_col - m.start_col) * (m.end_row - m.start_row));
        }
        err += ((float)(((x_end - x + 1) * (m.end_row - m.start_row) - somme) * (1.f - m.color_avg)) / (float)((m.end_col - m.start_col) * (m.end_row - m.start_row)));
    }
    return fabs(err);
}