-
Notifications
You must be signed in to change notification settings - Fork 0
/
plotter.cpp
247 lines (201 loc) · 5.88 KB
/
plotter.cpp
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
#include "plotter.h"
#include "render.h"
using namespace byteimage;
Plotter::Plot::Plot() {
setPointSize(3);
setLineSize(0);
}
void Plotter::Plot::addPoint(double x, double y) {
points.push_back(Pt2f(x, y));
}
void Plotter::Plot::setColor(Byte r, Byte g, Byte b) {
color = Color(r, g, b);
}
void Plotter::Plot::setPointSize(int size) {
has_bar = 0;
point_size = size;
}
void Plotter::Plot::setLineSize(int size) {
has_bar = 0;
line_size = size;
}
void Plotter::Plot::setBar(bool enabled) {
has_bar = enabled;
}
Plotter::Plotter() {
titleRender = labelRender = numberRender = NULL;
label_margin = title_margin = number_margin = 0;
setScale(0, 0, 1, 1);
hideTicks();
enableGrid(0);
setNumberFormat("%.0lf", "%.0lf");
setBorderMargin(5);
setAxisThickness(2);
setAxisOffset(3);
}
Plotter::~Plotter() {
if (titleRender) delete titleRender;
if (labelRender) delete labelRender;
if (numberRender) delete numberRender;
}
void Plotter::addPlot(const Plot& plot) {
plots.push_back(plot);
}
void Plotter::setScale(double xmin, double ymin, double xmax, double ymax) {
mn = Pt2f(xmin, ymin);
mx = Pt2f(xmax, ymax);
}
void Plotter::hideTicks() {
draw_ticks = 0;
}
void Plotter::setTicks(double xticks, double yticks, double xoffset, double yoffset) {
draw_ticks = 1;
ticks = Pt2f(xticks, yticks);
tick_offset = Pt2f(xoffset, yoffset);
}
void Plotter::enableGrid(bool enabled) {
draw_grid = enabled;
}
static int getDefaultMargin(int size) {
return (int)(size / 72.0 * TextRenderer::dpi + 5);
}
void Plotter::setTitleFont(const std::string& name, int size, int margin) {
if (titleRender) delete titleRender;
titleRender = new TextRenderer(name.c_str(), size);
title_margin = margin? margin : getDefaultMargin(size);
}
void Plotter::setTitle(const std::string& title) {
this->title = title;
}
void Plotter::setLabelFont(const std::string& name, int size, int margin) {
if (labelRender) delete labelRender;
labelRender = new TextRenderer(name.c_str(), size);
label_margin = margin? margin : getDefaultMargin(size);
}
void Plotter::setXLabel(const std::string& label) {
xlabel = label;
}
void Plotter::setYLabel(const std::string& label) {
ylabel = label;
}
void Plotter::setNumberFont(const std::string& name, int size, int margin) {
if (numberRender) delete numberRender;
numberRender = new TextRenderer(name.c_str(), size);
number_margin = margin? margin : getDefaultMargin(size);
}
void Plotter::setNumberFormat(const std::string& xformat, const std::string& yformat) {
this->xformat = xformat;
this->yformat = yformat;
}
void Plotter::setBorderMargin(int px) {
border_margin = px;
}
void Plotter::setAxisThickness(int px) {
axis_thickness = px;
}
void Plotter::setAxisOffset(int px) {
axis_offset = px;
}
static Pt2f transformPoint(const Matrix& T, const Pt2f& pt) {
const double* d = T.getArray();
double x = d[0] * pt.x + d[1] * pt.y + d[2];
double y = d[3] * pt.x + d[4] * pt.y + d[5];
double w = d[6] * pt.x + d[7] * pt.y + d[8];
return Pt2f(x / w, y / w);
}
ByteImage Plotter::render(int nr, int nc) const {
ByteImage img(nr, nc, 3);
memset(img.pixels, 0xFF, img.size());
int x = 0, y = 0, w = nc, h = nr;
//Compute border margin
x += border_margin;
y += border_margin;
w -= 2 * border_margin;
h -= 2 * border_margin;
//Compute title area
if (titleRender && title_margin && !title.empty()) {
titleRender->drawCentered(img, title.c_str(), y + h - title_margin / 2, x + w / 2, 0);
h -= title_margin;
}
if (labelRender && label_margin) {
//Compute xlabel
if (!xlabel.empty()) {
//TODO: Don't leave space if the ylabel isn't drawn
ByteImage textBox(label_margin, w - label_margin);
memset(textBox.pixels, 0xFF, textBox.size());
labelRender->drawCentered(textBox, xlabel.c_str(),
textBox.nr / 2, textBox.nc / 2, 0);
img.blit(textBox, y + h - label_margin, x + label_margin);
h -= label_margin;
}
//Compute ylabel
if (!ylabel.empty()) {
ByteImage textBox(label_margin, h);
memset(textBox.pixels, 0xFF, textBox.size());
labelRender->drawCentered(textBox, ylabel.c_str(),
textBox.nr / 2, textBox.nc / 2, 0);
img.blit(textBox.rotatedCCW(), y, x);
x += label_margin;
w -= label_margin;
}
}
//Compute numbers area
if (numberRender && number_margin) {
//Compute xnumbers
if (!xformat.empty()) {
//TODO
h -= number_margin;
}
//Compute ynumbers
if (!yformat.empty()) {
//TODO
x += number_margin;
w -= number_margin;
}
}
//Graph area
ByteImage graph(h, w, 3);
memset(graph.pixels, 0xFF, graph.size());
//Draw axes
DrawRect(graph, axis_offset, 0,
axis_thickness, graph.nr - axis_offset, 0);
DrawRect(graph, axis_offset, graph.nr - axis_offset - axis_thickness,
graph.nc - axis_offset - 1, axis_thickness, 0);
//Compute value-to-graph matrix
Matrix T = Matrix::identity(3);
double a = axis_offset + axis_thickness * 0.5;
double f = (w - a) / (mx.x - mn.x);
T.at(0, 0) = f;
T.at(0, 2) = a - f * mn.x;
a = h - a;
f = -a / (mx.y - mn.y);
T.at(1, 1) = f;
T.at(1, 2) = a - f * mn.y;
//Draw ticks
std::vector<int> xticks, yticks;
std::vector<double> xtickvalues, ytickvalues;
//TODO
//Draw grid
//TODO
//Draw numbers
//TODO
//Draw plots
graph = graph.toColor();
for (int i = 0; i < plots.size(); i++) {
if (plots[i].line_size)
for (int j = 1; j < plots[i].points.size(); j++)
DrawLine(graph,
transformPoint(T, plots[i].points[j - 1]),
transformPoint(T, plots[i].points[j]),
plots[i].color, plots[i].line_size);
if (plots[i].point_size)
for (int j = 0; j < plots[i].points.size(); j++)
DrawPoint(graph,
transformPoint(T, plots[i].points[j]),
plots[i].color, plots[i].point_size);
}
//TODO
//Blit graph to rest of image
img.blit(graph, y, x);
return img;
}