braille_plot.h 8.65 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"


Damien Leroux's avatar
Damien Leroux committed
8
struct braille_grid {
9
10
11
12
13
    int m_width;
    int m_height;
    typedef Eigen::Matrix<unsigned long, Eigen::Dynamic, Eigen::Dynamic> plot_data_type;
    plot_data_type m_data;

Damien Leroux's avatar
Damien Leroux committed
14
15
16
    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))
17
18
    {}

Damien Leroux's avatar
Damien Leroux committed
19
20
    braille_grid&
        put_pixel(int ix, int iy)
21
22
23
24
25
26
        {
            unsigned long bit = xy2bit(ix, iy);
            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
27
                /*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;*/
28
29
30
31
            }
            return *this;
        }

Damien Leroux's avatar
Damien Leroux committed
32
33
    braille_grid&
        clear_pixel(int ix, int iy)
34
35
36
37
38
39
40
41
42
43
44
        {
            unsigned char bit = xy2bit(ix, iy);
            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;
            }
            return *this;
        }

    bool
Damien Leroux's avatar
Damien Leroux committed
45
        get_pixel(int ix, int iy) const
46
47
48
49
50
51
52
53
54
55
56
        {
            unsigned char bit = xy2bit(ix, iy);
            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;
        }

    friend
Damien Leroux's avatar
Damien Leroux committed
57
        std::ostream& operator << (std::ostream& os, const braille_grid& plot)
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
        {
            for (int r = 0; r < plot.m_data.rows(); ++r) {
                for (int c = 0; c < plot.m_data.cols(); ++c) {
                    os.write((char*) &plot.m_data(r, c), 3);
                }
                os << std::endl;
            }
            return os;
        }

private:
    int x2data(int x) const { return x >> 1; }
    int y2data(int y) const { return y >> 2; }

    unsigned long xy2bit(int x, int y) const
    {
        static unsigned int bitval[8] = {
            0x010000, 0x080000,
            0x020000, 0x100000,
            0x040000, 0x200000,
            0x000100, 0x000200
        };

        return bitval[ ((y & 0x3) << 1) | (x & 1) ];
    }
Damien Leroux's avatar
Damien Leroux committed
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
};


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&
        plot(FUN fun, double step)
        {
            for (double x = m_xmin; x < m_xmax; x += step) {
                put_pixel(x, fun(x));
            }
            return *this;
        }

    braille_plot&
        line(double x0, double y0, double x1, double y1, int dash_length, int space_length)
        {
            int gx0 = x2grid(x0);
            int gy0 = y2grid(y0);
            int gx1 = x2grid(x1);
            int gy1 = y2grid(y1);
            int gdx = abs(gx0 - gx1);
            int gdy = abs(gy0 - gy1);
            double dx, dy;
            int counter = 0;
            space_length += dash_length;
            double x = x0, y = y0;
            if (gdx < gdy) {
                dy = y0 > y1 ? -m_yres : m_yres;
                dx = ((x0 > x1 ? -m_xres : m_xres) * gdx) / gdy;
                /*std::cout << "dx=" << dx << " dy=" << dy << std::endl;*/
                if (dy > 0) {
                    for (; y <= y1; y += dy, x += dx) {
                        if (counter++ < dash_length) {
                            put_pixel(x, y);
                        }
                        counter %= space_length;
                    }
                } else {
                    for (; y >= y1; y += dy, x += dx) {
                        if (counter++ < dash_length) {
                            put_pixel(x, y);
                        }
                        counter %= space_length;
                    }
                }
            } else {
                dx = x0 > x1 ? -m_xres : m_xres;
                dy = ((y0 > y1 ? -m_yres : m_yres) * gdy) / gdx;
                /*std::cout << "dx=" << dx << " dy=" << dy << std::endl;*/
                if (dx > 0) {
                    for (; x <= x1; y += dy, x += dx) {
                        if (counter++ < dash_length) {
                            put_pixel(x, y);
                        }
                        counter %= space_length;
                    }
                } else {
                    for (; x >= x1; y += dy, x += dx) {
                        if (counter++ < dash_length) {
                            put_pixel(x, y);
                        }
                        counter %= space_length;
                    }
                }
            }
            return *this;
        }

    braille_plot&
        hline(double y, int dash_length, int space_length)
        {
            return line(m_xmin, y, m_xmax, y, dash_length, space_length);
        }

    braille_plot&
        vline(double x, int dash_length, int space_length)
        {
            return line(x, m_ymin, x, m_ymax, dash_length, space_length);
        }

    braille_plot&
        haxis(double y, double tick_origin, double tick_major, double tick_minor)
        {
            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); }
            return haxis(y, ticks_maj, ticks_min);
        }

    braille_plot&
        vaxis(double x, double tick_origin, double tick_major, double tick_minor)
        {
            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); }
            return vaxis(x, ticks_maj, ticks_min);
        }

    braille_plot&
        vaxis(double x, const std::vector<double>& ticks_major, const std::vector<double>& ticks_minor)
        {
            vline(x, 1, 0);
            for (double y: ticks_minor) {
                put_pixel(x - m_xres, y);
                put_pixel(x + m_xres, y);
            }
            for (double y: ticks_major) {
                put_pixel(x - m_xres - m_xres, y);
                put_pixel(x + m_xres, y);
                put_pixel(x - m_xres, y);
                put_pixel(x + m_xres + m_xres, y);
            }
            return *this;
        }

    braille_plot&
        haxis(double y, const std::vector<double>& ticks_major, const std::vector<double>& ticks_minor)
        {
            hline(y, 1, 0);
            for (double x: ticks_minor) {
                put_pixel(x, y - m_yres);
                put_pixel(x, y + m_yres);
            }
            for (double x: ticks_major) {
                put_pixel(x, y - m_yres - m_yres);
                put_pixel(x, y + m_yres);
                put_pixel(x, y - m_yres);
                put_pixel(x, y + m_yres + m_yres);
            }
            return *this;
        }

    braille_plot& put_pixel(double x, double y)
    {
        braille_grid::put_pixel(x2grid(x), y2grid(y));
        return *this;
    }
246

Damien Leroux's avatar
Damien Leroux committed
247
248
private:
    int x2grid(double x) const
249
250
    {
        double rel_x = (x - m_xmin) * m_xrange_inv;
Damien Leroux's avatar
Damien Leroux committed
251
        return (int) round((m_width - 1) * rel_x);
252
253
    }

Damien Leroux's avatar
Damien Leroux committed
254
    int y2grid(double y) const
255
256
    {
        double rel_y = 1 - (y - m_ymin) * m_yrange_inv;
Damien Leroux's avatar
Damien Leroux committed
257
        return (int) round((m_height - 1) * rel_y);
258
259
260
261
262
263
264
    }
};



#endif