braille_plot.h 36.2 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
    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;
251
252
253
254
255
256
257
258
            return line(x0, y0, x1, y1, dash_length, space_length, r, g, b, counter);
        }


    braille_grid&
        line(int x0, int y0, int x1, int y1, int dash_length, int space_length, int r, int g, int b, int& counter)
        {
            /*int counter = 0;*/
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
            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;
                        }
                    }
                }
            }
319
320
            counter += space_length - 1;
            counter %= space_length;
321
322
323
324
325
326
327
328
329
330
331
332
            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);
        }

333
334
335
336
337
338
339
340
341
342
343
    braille_grid&
        filled_box(int x0, int y0, int x1, int y1, int r=255, int g=255, int b=255)
        {
            int y_begin = std::min(y0, y1);
            int y_end = std::max(y0, y1);
            for (int y = y_begin; y <= y_end; ++y) {
                line(x0, y, x1, y, 1, 0, r, g, b);
            }
            return *this;
        }

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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
    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;
    }

438
    friend
Damien Leroux's avatar
Damien Leroux committed
439
        std::ostream& operator << (std::ostream& os, const braille_grid& plot)
440
        {
441
442
443
444
445
            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) {
446
                for (int c = 0; c < plot.m_data.cols(); ++c) {
447
448
449
450
451
452
453
454
455
456
457
                    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);
                    }
458
                }
459
                os << _NORMAL << std::endl << bg_col;
460
            }
461
462
463
464
465
466
467
468
469
470
471
472
473
474
            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;
475
476
477
        }

private:
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
    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 */
    }

517
518
519
    int x2data(int x) const { return x >> 1; }
    int y2data(int y) const { return y >> 2; }

520
521
522
    int xy2bitidx(int x, int y) const { return ((y & 0x3) << 1) | (x & 1); }

    unsigned long idx2bit(int idx) const
523
524
525
526
527
528
529
530
    {
        static unsigned int bitval[8] = {
            0x010000, 0x080000,
            0x020000, 0x100000,
            0x040000, 0x200000,
            0x000100, 0x000200
        };

531
        return bitval[idx];
532
    }
Damien Leroux's avatar
Damien Leroux committed
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
};


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;*/
    }

558
559
560
561
562
563
564
565
566
567
568
569
570
571
    braille_plot&
        set_range(double min_x, double max_x, double min_y, double max_y)
        {
            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);
            return *this;
        }

Damien Leroux's avatar
Damien Leroux committed
572
573
    template <typename FUN>
    braille_plot&
574
        plot(FUN fun, double step, int R=255, int G=255, int B=255)
Damien Leroux's avatar
Damien Leroux committed
575
576
        {
            for (double x = m_xmin; x < m_xmax; x += step) {
577
                put_pixel(x, fun(x), R, G, B);
Damien Leroux's avatar
Damien Leroux committed
578
579
580
581
582
            }
            return *this;
        }

    braille_plot&
583
        plot(const std::vector<double>& X, const std::vector<double>& Y, int R=255, int G=255, int B=255, int dash_length=1, int space_length=0)
Damien Leroux's avatar
Damien Leroux committed
584
        {
585
            int counter = 0;
586
587
588
            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
589
                } else {
590
591
592
                    double x0 = X[0];
                    double y0 = Y[0];
                    for (size_t i = 1; i < X.size(); ++i) {
593
594
                        /*MSG_DEBUG("plot line " << x0 << ',' << y0 << '-' << X[i] << ',' << Y[i]);*/
                        line(x0, y0, X[i], Y[i], dash_length, space_length, R, G, B, counter);
595
596
                        x0 = X[i];
                        y0 = Y[i];
Damien Leroux's avatar
Damien Leroux committed
597
598
599
600
601
602
603
                    }
                }
            }
            return *this;
        }

    braille_plot&
604
        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
605
        {
606
607
608
609
            braille_grid::line(x2grid(x0), y2grid(y0), x2grid(x1), y2grid(y1), dash_length, space_length, R, G, B);
            return *this;
        }

610
611
612
613
614
615
616
    braille_plot&
        line(double x0, double y0, double x1, double y1, int dash_length, int space_length, int R, int G, int B, int& counter)
        {
            braille_grid::line(x2grid(x0), y2grid(y0), x2grid(x1), y2grid(y1), dash_length, space_length, R, G, B, counter);
            return *this;
        }

617
618
619
620
    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
621
622
623
        }

    braille_plot&
624
        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
625
        {
626
            return line(x, m_ymin, x, m_ymax, dash_length, space_length, R, G, B);
Damien Leroux's avatar
Damien Leroux committed
627
628
629
        }

    braille_plot&
630
631
632
633
634
635
636
637
        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);
        }

638
639
640
641
642
643
644
    braille_plot&
        filled_box(double x0, double y0, double x1, double y1, int R=255, int G=255, int B=255)
        {
            braille_grid::filled_box(x2grid(x0), y2grid(y0), x2grid(x1), y2grid(y1), R, G, B);
            return *this;
        }

645
646
    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
647
648
649
650
651
652
        {
            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); }
653
            return haxis(y, ticks_maj, ticks_min, R, G, B);
Damien Leroux's avatar
Damien Leroux committed
654
655
656
        }

    braille_plot&
657
        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
658
659
660
661
662
663
        {
            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); }
664
            return vaxis(x, ticks_maj, ticks_min, R, G, B);
Damien Leroux's avatar
Damien Leroux committed
665
666
667
        }

    braille_plot&
668
        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
669
        {
670
            vline(x, 1, 0, R, G, B);
Damien Leroux's avatar
Damien Leroux committed
671
            for (double y: ticks_minor) {
672
673
                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
674
675
            }
            for (double y: ticks_major) {
676
677
678
679
                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
680
681
682
683
684
            }
            return *this;
        }

    braille_plot&
685
        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
686
        {
687
            hline(y, 1, 0, R, G, B);
Damien Leroux's avatar
Damien Leroux committed
688
            for (double x: ticks_minor) {
689
690
                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
691
692
            }
            for (double x: ticks_major) {
693
694
695
696
                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
697
698
699
700
            }
            return *this;
        }

701
702
703
704
705
706
707
    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
708
    {
709
        braille_grid::add_label(x2grid(x), y2grid(y), label, R, G, B);
Damien Leroux's avatar
Damien Leroux committed
710
711
        return *this;
    }
712

Damien Leroux's avatar
Damien Leroux committed
713
714
private:
    int x2grid(double x) const
715
716
    {
        double rel_x = (x - m_xmin) * m_xrange_inv;
Damien Leroux's avatar
Damien Leroux committed
717
        return (int) round((m_width - 1) * rel_x);
718
719
    }

Damien Leroux's avatar
Damien Leroux committed
720
    int y2grid(double y) const
721
722
    {
        double rel_y = 1 - (y - m_ymin) * m_yrange_inv;
Damien Leroux's avatar
Damien Leroux committed
723
        return (int) round((m_height - 1) * rel_y);
724
725
726
727
    }
};


728
729
730
731
732
template <typename KeyType, typename KeyComp=std::less<KeyType>>
struct braille_histogram : public braille_plot {
    std::map<KeyType, int> m_data_points;
    int m_n_bins;
    double m_bin_width;
733

734
735
736
737
738
    braille_histogram(int char_width, int char_height, double bin_width=.5)
        : braille_plot(char_width, char_height, -1, 1, -1, 1)
        , m_data_points()
        , m_bin_width(bin_width)
    {}
739

740
741
742
743
744
745
    braille_histogram
        add_point(const KeyType& x)
        {
            ++m_data_points[x];
            return *this;
        }
746

747
748
    std::vector<int>
        make_bins(int& height, std::vector<KeyType>& bin_labels)
749
        {
750
751
752
753
754
755
756
757
758
            bin_labels.clear();
            /*MSG_DEBUG("data points " << m_data_points);*/
            height = 0;
            std::vector<int> ret;
            for (const auto& kv: m_data_points) {
                ret.push_back(kv.second);
                bin_labels.push_back(kv.first);
                if (height < ret.back()) {
                    height = ret.back();
759
760
                }
            }
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
            MSG_DEBUG("bins " << ret);
            MSG_DEBUG("labels " << bin_labels);
            return ret;
        }

    braille_histogram&
        render(int R=255, int G=255, int B=255)
        {
            int height = 0;
            std::vector<double> labels;
            std::vector<int> bins = make_bins(height, labels);
            double w = m_bin_width * .5;
            double x0 = -w;
            double x1 = bins.size() - w;
            double y0 = 0;
            double y1 = height;
            double pad_x = .1;
            double pad_y = height * .1;
            set_range(x0 - pad_x, x1 + pad_x, y0 - pad_y, y1 + pad_y);
            for (size_t b = 0; b < bins.size(); ++b) {
                filled_box(b - w, 0, b + w, bins[b], R, G, B);
782
            }
783
784
785
786
787
788
            haxis(-5 * m_yres, 0, bins.size(), 1);
            vaxis(-w - 5 * m_xres, 0, 10, 1);
            /*for (size_t x = 0; x < labels.size(); ++x) {*/
                /*std::string lab = MESSAGE("\n" << labels[x]);*/
                /*add_label(x, -3 * m_yres, lab);*/
            /*}*/
789
790
            return *this;
        }
791
};
792

793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889

#if 0
template <>
struct braille_histogram<double> : public braille_plot {
    std::vector<double> m_data_points;
    int m_n_bins;
    double m_min, m_max;
    double m_bin_width;
    bool m_linear;

    braille_histogram(int char_width, int char_height, int n_bins, double bin_width)
        : braille_plot(char_width, char_height, -1, 1, -1, 1)
        , m_data_points()
        , m_n_bins(n_bins)
        , m_min(std::numeric_limits<double>::infinity())
        , m_max(-std::numeric_limits<double>::infinity())
        , m_bin_width(bin_width)
        , m_linear(!!n_bins)
    {}

    braille_histogram
        add_point(double x)
        {
            m_data_points.push_back(x);
            if (x > m_max) { m_max = x; }
            if (x < m_min) { m_min = x; }
            return *this;
        }

    std::vector<int>
        make_bins(int& height, std::vector<double>& bin_labels)
        {
            bin_labels.clear();
            MSG_DEBUG("data points " << m_data_points);
            if (m_linear) {
                std::vector<int> bins(m_n_bins, 0);
                if (m_max == m_min) { m_max += 1; }
                for (double x: m_data_points) {
                    int bin = (int) (((x - m_min) / (m_max - m_min)) * m_n_bins);
                    MSG_DEBUG(x << " => " << bin);
                    if (bin == m_n_bins) { --bin; }
                    ++bins[bin];
                    if (bins[bin] > height) {
                        height = bins[bin];
                    }
                }
                if (bins.size() > 1) {
                    for (size_t b = 0; b < bins.size(); ++b) {
                        bin_labels.push_back(m_min + b * (m_max - m_min) / (m_n_bins - 1));
                    }
                } else {
                    bin_labels.push_back(m_min);
                }
                MSG_DEBUG("bins " << bins);
                MSG_DEBUG("labels " << bin_labels);
                return bins;
            } else {
                std::map<double, int> tmp_bins;
                for (double d: m_data_points) {
                    ++tmp_bins[d];
                }
                std::vector<int> bins(tmp_bins.size());
                size_t b = 0;
                for (const auto& kv: tmp_bins) {
                    bins[b] = kv.second;
                    ++b;
                    bin_labels.push_back(kv.first);
                }
                return bins;
            }
        }

    braille_histogram&
        render(int R=255, int G=255, int B=255)
        {
            int height = 0;
            std::vector<double> labels;
            std::vector<int> bins = make_bins(height, labels);
            double w = m_bin_width * .5;
            double x0 = -w;
            double x1 = bins.size() - w;
            double y0 = 0;
            double y1 = height;
            double pad_x = .1;
            double pad_y = height * .1;
            set_range(x0 - pad_x, x1 + pad_x, y0 - pad_y, y1 + pad_y);
            for (size_t b = 0; b < bins.size(); ++b) {
                filled_box(b - w, 0, b + w, bins[b], R, G, B);
            }
            haxis(-5 * m_yres, 0, bins.size(), 1);
            vaxis(-w - 5 * m_xres, 0, 10, 1);
            /*for (size_t x = 0; x < labels.size(); ++x) {*/
                /*std::string lab = MESSAGE("\n" << labels[x]);*/
                /*add_label(x, -3 * m_yres, lab);*/
            /*}*/
            return *this;
        }
890
};
891

892
893
#endif

894

895

896
897
#endif