braille_plot.h 30.4 KB
Newer Older
1
2
3
4
5
6
7
#ifndef _SPELL_BRAILLE_PLOT_H_
#define _SPELL_BRAILLE_PLOT_H_

#include <iostream>
#include "eigen.h"


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
struct palette {
    typedef int palette_type[256][3];

    static const palette_type& _256()
    {
        static constexpr palette_type _ = {
            /* reformatted from http://www.calmar.ws/vim/256-xterm-24bit-rgb-color-chart.html */
            {0x00, 0x00, 0x00}, {0x80, 0x00, 0x00}, {0x00, 0x80, 0x00}, {0x80, 0x80, 0x00}, {0x00, 0x00, 0x80}, {0x80, 0x00, 0x80}, {0x00, 0x80, 0x80}, {0xc0, 0xc0, 0xc0},
            {0x80, 0x80, 0x80}, {0xff, 0x00, 0x00}, {0x00, 0xff, 0x00}, {0xff, 0xff, 0x00}, {0x00, 0x00, 0xff}, {0xff, 0x00, 0xff}, {0x00, 0xff, 0xff}, {0xff, 0xff, 0xff},
            {0x00, 0x00, 0x00}, {0x00, 0x00, 0x5f}, {0x00, 0x00, 0x87}, {0x00, 0x00, 0xaf}, {0x00, 0x00, 0xd7}, {0x00, 0x00, 0xff},
            {0x00, 0x5f, 0x00}, {0x00, 0x5f, 0x5f}, {0x00, 0x5f, 0x87}, {0x00, 0x5f, 0xaf}, {0x00, 0x5f, 0xd7}, {0x00, 0x5f, 0xff},
            {0x00, 0x87, 0x00}, {0x00, 0x87, 0x5f}, {0x00, 0x87, 0x87}, {0x00, 0x87, 0xaf}, {0x00, 0x87, 0xd7}, {0x00, 0x87, 0xff},
            {0x00, 0xaf, 0x00}, {0x00, 0xaf, 0x5f}, {0x00, 0xaf, 0x87}, {0x00, 0xaf, 0xaf}, {0x00, 0xaf, 0xd7}, {0x00, 0xaf, 0xff},
            {0x00, 0xd7, 0x00}, {0x00, 0xd7, 0x5f}, {0x00, 0xd7, 0x87}, {0x00, 0xd7, 0xaf}, {0x00, 0xd7, 0xd7}, {0x00, 0xd7, 0xff},
            {0x00, 0xff, 0x00}, {0x00, 0xff, 0x5f}, {0x00, 0xff, 0x87}, {0x00, 0xff, 0xaf}, {0x00, 0xff, 0xd7}, {0x00, 0xff, 0xff},
            {0x5f, 0x00, 0x00}, {0x5f, 0x00, 0x5f}, {0x5f, 0x00, 0x87}, {0x5f, 0x00, 0xaf}, {0x5f, 0x00, 0xd7}, {0x5f, 0x00, 0xff},
            {0x5f, 0x5f, 0x00}, {0x5f, 0x5f, 0x5f}, {0x5f, 0x5f, 0x87}, {0x5f, 0x5f, 0xaf}, {0x5f, 0x5f, 0xd7}, {0x5f, 0x5f, 0xff},
            {0x5f, 0x87, 0x00}, {0x5f, 0x87, 0x5f}, {0x5f, 0x87, 0x87}, {0x5f, 0x87, 0xaf}, {0x5f, 0x87, 0xd7}, {0x5f, 0x87, 0xff},
            {0x5f, 0xaf, 0x00}, {0x5f, 0xaf, 0x5f}, {0x5f, 0xaf, 0x87}, {0x5f, 0xaf, 0xaf}, {0x5f, 0xaf, 0xd7}, {0x5f, 0xaf, 0xff},
            {0x5f, 0xd7, 0x00}, {0x5f, 0xd7, 0x5f}, {0x5f, 0xd7, 0x87}, {0x5f, 0xd7, 0xaf}, {0x5f, 0xd7, 0xd7}, {0x5f, 0xd7, 0xff},
            {0x5f, 0xff, 0x00}, {0x5f, 0xff, 0x5f}, {0x5f, 0xff, 0x87}, {0x5f, 0xff, 0xaf}, {0x5f, 0xff, 0xd7}, {0x5f, 0xff, 0xff},
            {0x87, 0x00, 0x00}, {0x87, 0x00, 0x5f}, {0x87, 0x00, 0x87}, {0x87, 0x00, 0xaf}, {0x87, 0x00, 0xd7}, {0x87, 0x00, 0xff},
            {0x87, 0x5f, 0x00}, {0x87, 0x5f, 0x5f}, {0x87, 0x5f, 0x87}, {0x87, 0x5f, 0xaf}, {0x87, 0x5f, 0xd7}, {0x87, 0x5f, 0xff},
            {0x87, 0x87, 0x00}, {0x87, 0x87, 0x5f}, {0x87, 0x87, 0x87}, {0x87, 0x87, 0xaf}, {0x87, 0x87, 0xd7}, {0x87, 0x87, 0xff},
            {0x87, 0xaf, 0x00}, {0x87, 0xaf, 0x5f}, {0x87, 0xaf, 0x87}, {0x87, 0xaf, 0xaf}, {0x87, 0xaf, 0xd7}, {0x87, 0xaf, 0xff},
            {0x87, 0xd7, 0x00}, {0x87, 0xd7, 0x5f}, {0x87, 0xd7, 0x87}, {0x87, 0xd7, 0xaf}, {0x87, 0xd7, 0xd7}, {0x87, 0xd7, 0xff},
            {0x87, 0xff, 0x00}, {0x87, 0xff, 0x5f}, {0x87, 0xff, 0x87}, {0x87, 0xff, 0xaf}, {0x87, 0xff, 0xd7}, {0x87, 0xff, 0xff},
            {0xaf, 0x00, 0x00}, {0xaf, 0x00, 0x5f}, {0xaf, 0x00, 0x87}, {0xaf, 0x00, 0xaf}, {0xaf, 0x00, 0xd7}, {0xaf, 0x00, 0xff},
            {0xaf, 0x5f, 0x00}, {0xaf, 0x5f, 0x5f}, {0xaf, 0x5f, 0x87}, {0xaf, 0x5f, 0xaf}, {0xaf, 0x5f, 0xd7}, {0xaf, 0x5f, 0xff},
            {0xaf, 0x87, 0x00}, {0xaf, 0x87, 0x5f}, {0xaf, 0x87, 0x87}, {0xaf, 0x87, 0xaf}, {0xaf, 0x87, 0xd7}, {0xaf, 0x87, 0xff},
            {0xaf, 0xaf, 0x00}, {0xaf, 0xaf, 0x5f}, {0xaf, 0xaf, 0x87}, {0xaf, 0xaf, 0xaf}, {0xaf, 0xaf, 0xd7}, {0xaf, 0xaf, 0xff},
            {0xaf, 0xd7, 0x00}, {0xaf, 0xd7, 0x5f}, {0xaf, 0xd7, 0x87}, {0xaf, 0xd7, 0xaf}, {0xaf, 0xd7, 0xd7}, {0xaf, 0xd7, 0xff},
            {0xaf, 0xff, 0x00}, {0xaf, 0xff, 0x5f}, {0xaf, 0xff, 0x87}, {0xaf, 0xff, 0xaf}, {0xaf, 0xff, 0xd7}, {0xaf, 0xff, 0xff},
            {0xd7, 0x00, 0x00}, {0xd7, 0x00, 0x5f}, {0xd7, 0x00, 0x87}, {0xd7, 0x00, 0xaf}, {0xd7, 0x00, 0xd7}, {0xd7, 0x00, 0xff},
            {0xd7, 0x5f, 0x00}, {0xd7, 0x5f, 0x5f}, {0xd7, 0x5f, 0x87}, {0xd7, 0x5f, 0xaf}, {0xd7, 0x5f, 0xd7}, {0xd7, 0x5f, 0xff},
            {0xd7, 0x87, 0x00}, {0xd7, 0x87, 0x5f}, {0xd7, 0x87, 0x87}, {0xd7, 0x87, 0xaf}, {0xd7, 0x87, 0xd7}, {0xd7, 0x87, 0xff},
            {0xd7, 0xaf, 0x00}, {0xd7, 0xaf, 0x5f}, {0xd7, 0xaf, 0x87}, {0xd7, 0xaf, 0xaf}, {0xd7, 0xaf, 0xd7}, {0xd7, 0xaf, 0xff},
            {0xd7, 0xd7, 0x00}, {0xd7, 0xd7, 0x5f}, {0xd7, 0xd7, 0x87}, {0xd7, 0xd7, 0xaf}, {0xd7, 0xd7, 0xd7}, {0xd7, 0xd7, 0xff},
            {0xd7, 0xff, 0x00}, {0xd7, 0xff, 0x5f}, {0xd7, 0xff, 0x87}, {0xd7, 0xff, 0xaf}, {0xd7, 0xff, 0xd7}, {0xd7, 0xff, 0xff},
            {0xff, 0x00, 0x00}, {0xff, 0x00, 0x5f}, {0xff, 0x00, 0x87}, {0xff, 0x00, 0xaf}, {0xff, 0x00, 0xd7}, {0xff, 0x00, 0xff},
            {0xff, 0x5f, 0x00}, {0xff, 0x5f, 0x5f}, {0xff, 0x5f, 0x87}, {0xff, 0x5f, 0xaf}, {0xff, 0x5f, 0xd7}, {0xff, 0x5f, 0xff},
            {0xff, 0x87, 0x00}, {0xff, 0x87, 0x5f}, {0xff, 0x87, 0x87}, {0xff, 0x87, 0xaf}, {0xff, 0x87, 0xd7}, {0xff, 0x87, 0xff},
            {0xff, 0xaf, 0x00}, {0xff, 0xaf, 0x5f}, {0xff, 0xaf, 0x87}, {0xff, 0xaf, 0xaf}, {0xff, 0xaf, 0xd7}, {0xff, 0xaf, 0xff},
            {0xff, 0xd7, 0x00}, {0xff, 0xd7, 0x5f}, {0xff, 0xd7, 0x87}, {0xff, 0xd7, 0xaf}, {0xff, 0xd7, 0xd7}, {0xff, 0xd7, 0xff},
            {0xff, 0xff, 0x00}, {0xff, 0xff, 0x5f}, {0xff, 0xff, 0x87}, {0xff, 0xff, 0xaf}, {0xff, 0xff, 0xd7}, {0xff, 0xff, 0xff},
            {0x08, 0x08, 0x08}, {0x12, 0x12, 0x12}, {0x1c, 0x1c, 0x1c}, {0x26, 0x26, 0x26}, {0x30, 0x30, 0x30}, {0x3a, 0x3a, 0x3a},
            {0x44, 0x44, 0x44}, {0x4e, 0x4e, 0x4e}, {0x58, 0x58, 0x58}, {0x60, 0x60, 0x60}, {0x66, 0x66, 0x66}, {0x76, 0x76, 0x76},
            {0x80, 0x80, 0x80}, {0x8a, 0x8a, 0x8a}, {0x94, 0x94, 0x94}, {0x9e, 0x9e, 0x9e}, {0xa8, 0xa8, 0xa8}, {0xb2, 0xb2, 0xb2},
            {0xbc, 0xbc, 0xbc}, {0xc6, 0xc6, 0xc6}, {0xd0, 0xd0, 0xd0}, {0xda, 0xda, 0xda}, {0xe4, 0xe4, 0xe4}, {0xee, 0xee, 0xee}
        };
        return _;
    }

    static
        int closest_index(const palette_type& pal, size_t pal_size, int r, int g, int b)
        {
            int best_idx = -1;
            int best_accum = 0x7FFFFFFF;
            for (size_t i = 0; i < pal_size; ++i) {
                int r_delta = abs(r - pal[i][0]) * 3 * 256 / 10;
                int g_delta = abs(g - pal[i][1]) * 6 * 256 / 10;
                int b_delta = abs(b - pal[i][2]) * 1 * 256 / 10;
                int accum = r_delta + g_delta + b_delta;
                if (accum < best_accum) {
                    best_idx = i;
                    best_accum = accum;
                }
            }
            /*MSG_DEBUG("best matching color for (" << r << ", " << g << ", " << b << ") at #" << best_idx);*/
            return best_idx;
        }

    static
        int closest_index_256(int r, int g, int b) { return closest_index(_256(), 256, r, g, b); }

    struct color256 {
        int idx;
        bool bg;
        color256(int r, int g, int b, bool back=false) : idx(closest_index_256(r, g, b)), bg(back) {}
        color256(int i) : idx(i) {}
        friend std::ostream& operator << (std::ostream& os, const color256& col)
        {
            return os << "\x1b[" << (3 + col.bg) << "8;5;" << std::dec << col.idx << 'm';
        }
    };
};




Damien Leroux's avatar
Damien Leroux committed
98
struct braille_grid {
99
100
101
102
    int m_width;
    int m_height;
    typedef Eigen::Matrix<unsigned long, Eigen::Dynamic, Eigen::Dynamic> plot_data_type;
    plot_data_type m_data;
103
104
105
    plot_data_type m_pixel_count;
    std::vector<std::vector<std::vector<std::vector<double>>>> m_colors;
    int bgR, bgG, bgB;
106

Damien Leroux's avatar
Damien Leroux committed
107
108
109
    braille_grid(int dot_width, int dot_height)
        : m_width(dot_width), m_height(dot_height)
        , m_data(plot_data_type::Constant((dot_height + 3) >> 2, (dot_width + 1) >> 1, 0x0080A0E2))
110
111
112
113
114
115
116
117
        , m_pixel_count(plot_data_type::Zero(dot_height, dot_width))
        , bgR(0), bgG(0), bgB(0)
    {
        std::vector<std::vector<std::vector<double>>> col_row(m_data.cols(),
                                                              {{-1, -1, -1}, {-1, -1, -1}, {-1, -1, -1}, {-1, -1, -1},
                                                               {-1, -1, -1}, {-1, -1, -1}, {-1, -1, -1}, {-1, -1, -1}});
        m_colors.resize(m_data.rows(), col_row);
    }
118

Damien Leroux's avatar
Damien Leroux committed
119
    braille_grid&
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
        set_background(int r, int g, int b) { bgR = r; bgG = g; bgB = b; return *this; }

    const std::vector<int>
        get_background() { return {bgR, bgG, bgB}; }

    template <typename Scalar>
    braille_grid(const Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic>& data, int r=255, int g=255, int b=255)
        : m_width(data.cols()), m_height(data.rows())
        , m_data(plot_data_type::Constant((m_height + 3) >> 2, (m_width + 1) >> 1, 0x0080A0E2))
        , m_pixel_count(plot_data_type::Zero(data.rows(), data.cols()))
        , bgR(40), bgG(40), bgB(40)
    {
        std::vector<std::vector<std::vector<double>>> col_row(m_data.cols(),
                                                              {{-1, -1, -1}, {-1, -1, -1}, {-1, -1, -1}, {-1, -1, -1},
                                                               {-1, -1, -1}, {-1, -1, -1}, {-1, -1, -1}, {-1, -1, -1}});
        m_colors.resize(m_data.rows(), col_row);
        for (int j = 0; j < data.cols(); ++j) {
            for (int i = 0; i < data.rows(); ++i) {
                if (data(i, j)) {
                    put_pixel(j, i, r, g, b);
                }
            }
        }
    }

    braille_grid&
        put_pixel(int ix, int iy, int R=255, int G=255, int B=255)
147
        {
148
149
            int idx = xy2bitidx(ix, iy);
            unsigned long bit = idx2bit(idx);
150
151
152
153
            int r = y2data(iy);
            int c = x2data(ix);
            if (r >= 0 && r < m_data.rows() && c >= 0 && c < m_data.cols()) {
                m_data(r, c) |= bit;
Damien Leroux's avatar
Damien Leroux committed
154
                /*std::cout << "put_pixel(" << std::dec << ix << ", " << iy << ") bit=" << std::hex << bit << std::dec << " r=" << r << " c=" << c << " data=" << std::hex << m_data(r, c) << std::endl;*/
155
156
157
158
159
160
161
162
163
164
                if (m_pixel_count(iy, ix) == 0) {
                    m_colors[r][c][idx] = {(double) R, (double) G, (double) B};
                    m_pixel_count(iy, ix) = 1;
                } else {
                    auto rgb = m_colors[r][c][idx];
                    auto count = m_pixel_count(iy, ix);
                    double inv = 1. / (count + 1);
                    m_colors[r][c][idx] = {inv * (rgb[0] * count + R), inv * (rgb[1] * count + G), inv * (rgb[2] * count + B)};
                    m_pixel_count(iy, ix) = count + 1;
                }
165
166
167
168
            }
            return *this;
        }

Damien Leroux's avatar
Damien Leroux committed
169
170
    braille_grid&
        clear_pixel(int ix, int iy)
171
        {
172
173
            int idx = xy2bitidx(ix, iy);
            unsigned long bit = idx2bit(idx);
174
175
176
177
            int r = y2data(iy);
            int c = x2data(ix);
            if (r >= 0 && r < m_data.rows() && c >= 0 && c < m_data.cols()) {
                m_data(r, c) &= ~bit;
178
                m_colors[r][c][idx] = {-1, -1, -1};
179
180
181
182
183
            }
            return *this;
        }

    bool
Damien Leroux's avatar
Damien Leroux committed
184
        get_pixel(int ix, int iy) const
185
        {
186
187
            int idx = xy2bitidx(ix, iy);
            unsigned long bit = idx2bit(idx);
188
189
190
191
192
193
194
195
            int r = y2data(iy);
            int c = x2data(ix);
            if (r >= 0 && r < m_data.rows() && c >= 0 && c < m_data.cols()) {
                return !!(m_data(r, c) & bit);
            }
            return false;
        }

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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
    std::vector<int>
        get_pixel_color(int ix, int iy) const
        {
            static std::vector<int> no_color = {-1, -1, -1};
            int idx = xy2bitidx(ix, iy);
            int r = y2data(iy);
            int c = x2data(ix);
            if (r >= 0 && r < m_data.rows() && c >= 0 && c < m_data.cols()) {
                const auto& v = m_colors[r][c][idx];
                return {(int) v[0], (int) v[1], (int) v[2]};
            }
            return no_color;
        }

    braille_grid&
        add_label(int ix, int iy, const std::string& s, int R=255, int G=255, int B=255)
        {
            int r = y2data(iy + 3);
            int c = x2data(ix + 1);
            auto i = s.begin();
            auto j = s.end();
            int c_orig = c;
            while (i != j) {
                unsigned long c1 = (unsigned char) *i++;
                unsigned long c2 = 0;
                unsigned long c3 = 0;
                unsigned long c4 = 0;
                switch (c1) {
                    case ' ':  ++c; continue;
                    case '\n': ++r; c = c_orig; continue;
                    case '\v': --r; c = c_orig; continue;
                    case '\b': --c; continue;
                    default:
                               if (c1 >= 0xC0) {
                                   c2 = (unsigned char) *i++;
                               }
                               if (c1 >= 0xE0) {
                                   c3 = (unsigned char) *i++;
                               }
                               if (c1 >= 0xF0) {
                                   c4 = (unsigned char) *i++;
                               }
                               for (int i = 0; i < 8; ++i) {
                                   m_colors[r][c][i] = {(double) R, (double) G, (double) B};
                               }
                               m_data(r, c++) = c1 | (c2 << 8) | (c3 << 16) | (c4 << 24);
                };
            }
            return *this;
        }

    braille_grid&
        line(int x0, int y0, int x1, int y1, int dash_length, int space_length, int r=255, int g=255, int b=255)
        {
            int counter = 0;
            space_length += dash_length;
            if (abs(x1 - x0) >= abs(y1 - y0)) {
                if (x1 < x0) {
                    std::swap(x0, x1);
                    std::swap(y0, y1);
                }
                int dist_x = x1 - x0;
                int dist_y = abs(y1 - y0);
                if (dist_y == 0) {
                    for (; x0 <= x1; ++x0) {
                        if (counter++ < dash_length) {
                            put_pixel(x0, y0, r, g, b);
                        }
                        counter %= space_length;
                    }
                } else {
                    int dy = (y1 > y0 ? 1 : -1);
                    int accum = dist_x >> 1;
                    for (; x0 <= x1; ++x0) {
                        if (counter++ < dash_length) {
                            put_pixel(x0, y0, r, g, b);
                        }
                        counter %= space_length;
                        accum += dist_y;
                        if (accum > dist_x) {
                            accum -= dist_x;
                            y0 += dy;
                        }
                    }
                }
            } else {
                if (y1 < y0) {
                    std::swap(x0, x1);
                    std::swap(y0, y1);
                }
                int dist_x = abs(x1 - x0);
                int dist_y = y1 - y0;
                if (dist_x == 0) {
                    for (; y0 <= y1; ++y0) {
                        if (counter++ < dash_length) {
                            put_pixel(x0, y0, r, g, b);
                        }
                        counter %= space_length;
                    }
                } else {
                    int dx = (x1 > x0 ? 1 : -1);
                    int accum = dist_y >> 1;
                    for (; y0 <= y1; ++y0) {
                        if (counter++ < dash_length) {
                            put_pixel(x0, y0, r, g, b);
                        }
                        counter %= space_length;
                        accum += dist_x;
                        if (accum > dist_y) {
                            accum -= dist_y;
                            x0 += dx;
                        }
                    }
                }
            }
            return *this;
        }

    braille_grid&
        box(int x0, int y0, int x1, int y1, int dash_length, int space_length, int r=255, int g=255, int b=255)
        {
            return line(x0, y0, x0, y1, dash_length, space_length, r, g, b)
                  .line(x0, y1, x1, y1, dash_length, space_length, r, g, b)
                  .line(x1, y1, x1, y0, dash_length, space_length, r, g, b)
                  .line(x1, y0, x0, y0, dash_length, space_length, r, g, b);
        }

    braille_grid& ellipse(int center_x, int center_y, int radius_x, int radius_y, int dash_length, int space_length, int r=255, int g=255, int b=255)
    {
        typedef std::pair<int, int> point;
        struct point_compare {
            int cx;
            int cy;
            bool operator () (const point& p1, const point& p2) const
            {
                int q1 = (p1.first < cx
                          ? p1.second < cy ? 3 : 0
                          : p1.second < cy ? 2 : 1);
                int q2 = (p2.first < cx
                          ? p2.second < cy ? 3 : 0
                          : p2.second < cy ? 2 : 1);
                if (q1 == q2) {
                    switch (q1) {
                        case 0:
                            return p1.first < p2.first || (p1.first == p2.first && p1.second < p2.second);
                        case 1:
                            return p1.first < p2.first || (p1.first == p2.first && p1.second > p2.second);
                        case 2:
                            return p1.first > p2.first || (p1.first == p2.first && p1.second > p2.second);
                        case 3:
                            return p1.first > p2.first || (p1.first == p2.first && p1.second < p2.second);
                        default:
                            return false;
                    };
                } else {
                    return q1 < q2;
                }
            }
        };
        std::set<point, point_compare> points(point_compare{center_x, center_y});
        /* from https://dai.fmph.uniba.sk/upload/0/01/Ellipse.pdf */
        int x, y, deltax, deltay, ellipse_error, twoAsqr, twoBsqr, stoppingX, stoppingY;
        twoAsqr = 2 * radius_x * radius_x;
        twoBsqr = 2 * radius_y * radius_y;
        x = radius_x;
        y = 0;
        deltax = radius_y * radius_y * (1 - 2 * radius_x);
        deltay = radius_x * radius_x;
        ellipse_error = 0;
        stoppingX = twoBsqr * radius_x;
        stoppingY = 0;
        while (stoppingX >= stoppingY) {
            points.insert({center_x + x, center_y - y});
            points.insert({center_x + x, center_y + y});
            points.insert({center_x - x, center_y - y});
            points.insert({center_x - x, center_y + y});
            ++y;
            stoppingY += twoAsqr;
            ellipse_error += deltay;
            deltay += twoAsqr;
            if ((ellipse_error + ellipse_error + deltax) > 0) {
                --x;
                stoppingX -= twoBsqr;
                ellipse_error += deltax;
                deltax += twoBsqr;
            }
        }
        x = 0;
        y = radius_y;
        ellipse_error = 0;
        deltax = radius_y * radius_y;
        deltay = radius_x * radius_x * (1 - 2 * radius_y);
        stoppingX = 0;
        stoppingY = twoAsqr * radius_y;
        while (stoppingX <= stoppingY) {
            points.insert({center_x + x, center_y - y});
            points.insert({center_x + x, center_y + y});
            points.insert({center_x - x, center_y - y});
            points.insert({center_x - x, center_y + y});
            ++x;
            stoppingX += twoBsqr;
            ellipse_error += deltax;
            deltax += twoBsqr;
            if ((ellipse_error + ellipse_error + deltay) > 0) {
                --y;
                stoppingY -= twoAsqr;
                ellipse_error += deltay;
                deltay += twoAsqr;
            }
        }
        int counter = 0;
        space_length += dash_length;
        for (const point& p: points) {
            if (counter ++ < dash_length) {
                put_pixel(p.first, p.second, r, g, b);
            }
            counter %= space_length;
        }
        return *this;
    }

417
    friend
Damien Leroux's avatar
Damien Leroux committed
418
        std::ostream& operator << (std::ostream& os, const braille_grid& plot)
419
        {
420
421
422
423
424
            int rows = plot.m_data.rows() - 1;
            int r;
            auto bg_col = palette::color256(plot.bgR, plot.bgG, plot.bgB, true);
            os << bg_col;
            for (r = 0; r < rows; ++r) {
425
                for (int c = 0; c < plot.m_data.cols(); ++c) {
426
427
428
429
430
431
432
433
434
435
436
                    unsigned long tmp = plot.m_data(r, c);
                    os << plot.compute_color(r, c);
                    if (tmp & 0xFF000000) {
                        os.write((char*) &tmp, 4);
                    } else if (tmp & 0x00FF0000) {
                        os.write((char*) &tmp, 3);
                    } else if (tmp & 0x0000FF00) {
                        os.write((char*) &tmp, 2);
                    } else {
                        os.write((char*) &tmp, 1);
                    }
437
                }
438
                os << _NORMAL << std::endl << bg_col;
439
            }
440
441
442
443
444
445
446
447
448
449
450
451
452
453
            for (int c = 0; c < plot.m_data.cols(); ++c) {
                unsigned long tmp = plot.m_data(r, c);
                os << plot.compute_color(r, c);
                if (tmp & 0xFF000000) {
                    os.write((char*) &tmp, 4);
                } else if (tmp & 0x00FF0000) {
                    os.write((char*) &tmp, 3);
                } else if (tmp & 0x0000FF00) {
                    os.write((char*) &tmp, 2);
                } else {
                    os.write((char*) &tmp, 1);
                }
            }
            return os << _NORMAL;
454
455
456
        }

private:
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
    palette::color256 compute_color(int r, int c) const
    {
        int n = 0;
        double R = 0, G = 0, B = 0;
        const auto& block = m_colors[r][c];
#ifdef COMPUTE_COL_BY_MEAN_SQR
        for (int i = 0; i < 8; ++i) {
            if (block[i][0] != -1) {
                R += block[i][0] * block[i][0];
                G += block[i][1] * block[i][1];
                B += block[i][2] * block[i][2];
                ++n;
            }
        }
        if (n) {
            R /= n;
            G /= n;
            B /= n;
            return {(int) sqrt(R), (int) sqrt(G), (int) sqrt(B)};
        }
#else
        for (int i = 0; i < 8; ++i) {
            if (block[i][0] != -1) {
                R += block[i][0];
                G += block[i][1];
                B += block[i][2];
                ++n;
            }
        }
        if (n) {
            R /= n;
            G /= n;
            B /= n;
            return {(int) R, (int) G, (int) B};
        }
#endif
        return {15}; /* default: white */
    }

496
497
498
    int x2data(int x) const { return x >> 1; }
    int y2data(int y) const { return y >> 2; }

499
500
501
    int xy2bitidx(int x, int y) const { return ((y & 0x3) << 1) | (x & 1); }

    unsigned long idx2bit(int idx) const
502
503
504
505
506
507
508
509
    {
        static unsigned int bitval[8] = {
            0x010000, 0x080000,
            0x020000, 0x100000,
            0x040000, 0x200000,
            0x000100, 0x000200
        };

510
        return bitval[idx];
511
    }
Damien Leroux's avatar
Damien Leroux committed
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
};


struct braille_plot : public braille_grid {
    double m_xmin;
    double m_xmax;
    double m_ymin;
    double m_ymax;
    double m_xrange_inv;
    double m_yrange_inv;
    double m_xres;
    double m_yres;

    braille_plot(int w, int h, double min_x, double max_x, double min_y, double max_y)
        : braille_grid(w << 1, h << 2),
          /*m_width((w << 1)), m_height((h << 2)), m_data(plot_data_type::Constant(h + 1, w + 1, 0x0080A0E2)),*/
          m_xmin(min_x), m_xmax(max_x), m_ymin(min_y), m_ymax(max_y),
          m_xrange_inv(1. / (m_xmax - m_xmin)),
          m_yrange_inv(1. / (m_ymax - m_ymin)),
          m_xres((m_xmax - m_xmin) / (m_width - 1)),
          m_yres((m_ymax - m_ymin) / (m_height - 1))
    {
        /*std::cout << "m_xres=" << m_xres << " m_yres=" << m_yres << std::endl;*/
    }

    template <typename FUN>
    braille_plot&
539
        plot(FUN fun, double step, int R=255, int G=255, int B=255)
Damien Leroux's avatar
Damien Leroux committed
540
541
        {
            for (double x = m_xmin; x < m_xmax; x += step) {
542
                put_pixel(x, fun(x), R, G, B);
Damien Leroux's avatar
Damien Leroux committed
543
544
545
546
547
            }
            return *this;
        }

    braille_plot&
548
        plot(const std::vector<double>& X, const std::vector<double>& Y, int R=255, int G=255, int B=255)
Damien Leroux's avatar
Damien Leroux committed
549
        {
550
551
552
            if (X.size() == Y.size()) {
                if (X.size() == 1) {
                    put_pixel(X[0], Y[0], R, G, B);
Damien Leroux's avatar
Damien Leroux committed
553
                } else {
554
555
556
557
558
559
                    double x0 = X[0];
                    double y0 = Y[0];
                    for (size_t i = 1; i < X.size(); ++i) {
                        line(x0, y0, X[i], Y[i], 1, 0, R, G, B);
                        x0 = X[i];
                        y0 = Y[i];
Damien Leroux's avatar
Damien Leroux committed
560
561
562
563
564
565
566
                    }
                }
            }
            return *this;
        }

    braille_plot&
567
        line(double x0, double y0, double x1, double y1, int dash_length, int space_length, int R=255, int G=255, int B=255)
Damien Leroux's avatar
Damien Leroux committed
568
        {
569
570
571
572
573
574
575
576
            braille_grid::line(x2grid(x0), y2grid(y0), x2grid(x1), y2grid(y1), dash_length, space_length, R, G, B);
            return *this;
        }

    braille_plot&
        hline(double y, int dash_length, int space_length, int R=255, int G=255, int B=255)
        {
            return line(m_xmin, y, m_xmax, y, dash_length, space_length, R, G, B);
Damien Leroux's avatar
Damien Leroux committed
577
578
579
        }

    braille_plot&
580
        vline(double x, int dash_length, int space_length, int R=255, int G=255, int B=255)
Damien Leroux's avatar
Damien Leroux committed
581
        {
582
            return line(x, m_ymin, x, m_ymax, dash_length, space_length, R, G, B);
Damien Leroux's avatar
Damien Leroux committed
583
584
585
        }

    braille_plot&
586
587
588
589
590
591
592
593
594
595
        box(double x0, double y0, double x1, double y1, int dash_length, int space_length, int R=255, int G=255, int B=255)
        {
            return line(x0, y0, x0, y1, dash_length, space_length, R, G, B)
                  .line(x0, y1, x1, y1, dash_length, space_length, R, G, B)
                  .line(x1, y1, x1, y0, dash_length, space_length, R, G, B)
                  .line(x1, y0, x0, y0, dash_length, space_length, R, G, B);
        }

    braille_plot&
        haxis(double y, double tick_origin, double tick_major, double tick_minor, int R=255, int G=255, int B=255)
Damien Leroux's avatar
Damien Leroux committed
596
597
598
599
600
601
        {
            std::vector<double> ticks_maj, ticks_min;
            for (double x = tick_origin; x > m_xmin; x -= tick_major) { ticks_maj.push_back(x); }
            for (double x = tick_origin; x < m_xmax; x += tick_major) { ticks_maj.push_back(x); }
            for (double x = tick_origin; x > m_xmin; x -= tick_minor) { ticks_min.push_back(x); }
            for (double x = tick_origin; x < m_xmax; x += tick_minor) { ticks_min.push_back(x); }
602
            return haxis(y, ticks_maj, ticks_min, R, G, B);
Damien Leroux's avatar
Damien Leroux committed
603
604
605
        }

    braille_plot&
606
        vaxis(double x, double tick_origin, double tick_major, double tick_minor, int R=255, int G=255, int B=255)
Damien Leroux's avatar
Damien Leroux committed
607
608
609
610
611
612
        {
            std::vector<double> ticks_maj, ticks_min;
            for (double y = tick_origin; y > m_ymin; y -= tick_major) { ticks_maj.push_back(y); }
            for (double y = tick_origin; y < m_ymax; y += tick_major) { ticks_maj.push_back(y); }
            for (double y = tick_origin; y > m_ymin; y -= tick_minor) { ticks_min.push_back(y); }
            for (double y = tick_origin; y < m_ymax; y += tick_minor) { ticks_min.push_back(y); }
613
            return vaxis(x, ticks_maj, ticks_min, R, G, B);
Damien Leroux's avatar
Damien Leroux committed
614
615
616
        }

    braille_plot&
617
        vaxis(double x, const std::vector<double>& ticks_major, const std::vector<double>& ticks_minor, int R=255, int G=255, int B=255)
Damien Leroux's avatar
Damien Leroux committed
618
        {
619
            vline(x, 1, 0, R, G, B);
Damien Leroux's avatar
Damien Leroux committed
620
            for (double y: ticks_minor) {
621
622
                put_pixel(x - m_xres, y, R, G, B);
                put_pixel(x + m_xres, y, R, G, B);
Damien Leroux's avatar
Damien Leroux committed
623
624
            }
            for (double y: ticks_major) {
625
626
627
628
                put_pixel(x - m_xres - m_xres, y, R, G, B);
                put_pixel(x + m_xres, y, R, G, B);
                put_pixel(x - m_xres, y, R, G, B);
                put_pixel(x + m_xres + m_xres, y, R, G, B);
Damien Leroux's avatar
Damien Leroux committed
629
630
631
632
633
            }
            return *this;
        }

    braille_plot&
634
        haxis(double y, const std::vector<double>& ticks_major, const std::vector<double>& ticks_minor, int R=255, int G=255, int B=255)
Damien Leroux's avatar
Damien Leroux committed
635
        {
636
            hline(y, 1, 0, R, G, B);
Damien Leroux's avatar
Damien Leroux committed
637
            for (double x: ticks_minor) {
638
639
                put_pixel(x, y - m_yres, R, G, B);
                put_pixel(x, y + m_yres, R, G, B);
Damien Leroux's avatar
Damien Leroux committed
640
641
            }
            for (double x: ticks_major) {
642
643
644
645
                put_pixel(x, y - m_yres - m_yres, R, G, B);
                put_pixel(x, y + m_yres, R, G, B);
                put_pixel(x, y - m_yres, R, G, B);
                put_pixel(x, y + m_yres + m_yres, R, G, B);
Damien Leroux's avatar
Damien Leroux committed
646
647
648
649
            }
            return *this;
        }

650
651
652
653
654
655
656
    braille_plot& put_pixel(double x, double y, int R=255, int G=255, int B=255)
    {
        braille_grid::put_pixel(x2grid(x), y2grid(y), R, G, B);
        return *this;
    }

    braille_plot& add_label(double x, double y, const std::string& label, int R=255, int G=255, int B=255)
Damien Leroux's avatar
Damien Leroux committed
657
    {
658
        braille_grid::add_label(x2grid(x), y2grid(y), label, R, G, B);
Damien Leroux's avatar
Damien Leroux committed
659
660
        return *this;
    }
661

Damien Leroux's avatar
Damien Leroux committed
662
663
private:
    int x2grid(double x) const
664
665
    {
        double rel_x = (x - m_xmin) * m_xrange_inv;
Damien Leroux's avatar
Damien Leroux committed
666
        return (int) round((m_width - 1) * rel_x);
667
668
    }

Damien Leroux's avatar
Damien Leroux committed
669
    int y2grid(double y) const
670
671
    {
        double rel_y = 1 - (y - m_ymin) * m_yrange_inv;
Damien Leroux's avatar
Damien Leroux committed
672
        return (int) round((m_height - 1) * rel_y);
673
674
675
676
    }
};


677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
#if 0

struct braille_pedigree {
    std::vector<std::string> vertices;
    std::vector<size_t> ranks;
    std::vector<size_t> rank_count;
    std::vector<size_t> columns;
    std::vector<std::pair<size_t, size_t>> edges;

    std::map<std::string, size_t> vertex_lookup;
    std::multimap<size_t, size_t> edge_lookup;

    struct link {
        size_t p1, p2;
        std::vector<size_t> children;
    };

    braille_dag
        add_vertex(const std::string& label, const std::string& p1, const std::string& p2)
        {
            size_t n = vertices.size();
            vertices.push_back(label);
            ranks.push_back(0);
            if (p1 != "") {
                edges.emplace_back(vertex_lookup[p1], n);
                edge_lookup.emplace(n, vertex_lookup[p1]);
                ranks.back() = 1 + ranks[vertex_lookup[p1]];
                if (p2 != "") {
                    edges.emplace_back(vertex_lookup[p2], n);
                    ranks.back() = std::min(ranks.back(), 1 + ranks[vertex_lookup[p2]]);
                    edge_lookup.emplace(n, vertex_lookup[p2]);
                }
            }
            vertex_lookup[label] = n;
            if (ranks[n] > rank_count.size()) {
                rank_count.push_back(1);
            } else {
                ++rank_count[ranks[n]];
            }
            columns.push_back(rank_count[ranks[n]]);
            return *this;
        }

    unsigned int edge_attraction(size_t v1, size_t v2)
    {
        int y = ranks[v2] - ranks[v1];
        int x = columns[v2] - columns[v1];
        return x * x + y * y;
    }
};
#endif

729
730
731

#endif