forked from swissmicros/SDKdemo
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathplot.cc
467 lines (418 loc) · 13.2 KB
/
plot.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
// ****************************************************************************
// plot.cc DB48X project
// ****************************************************************************
//
// File Description:
//
// Function and curve plotting
//
//
//
//
//
//
//
//
// ****************************************************************************
// (C) 2023 Christophe de Dinechin <[email protected]>
// This software is licensed under the terms outlined in LICENSE.txt
// ****************************************************************************
// This file is part of DB48X.
//
// DB48X is free software: you can redistribute it and/or modify
// it under the terms outlined in the LICENSE.txt file
//
// DB48X is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// ****************************************************************************
#include "plot.h"
#include "arithmetic.h"
#include "compare.h"
#include "equations.h"
#include "expression.h"
#include "functions.h"
#include "graphics.h"
#include "program.h"
#include "stats.h"
#include "sysmenu.h"
#include "target.h"
#include "variables.h"
void draw_axes(const PlotParametersAccess &ppar)
// ----------------------------------------------------------------------------
// Draw axes
// ----------------------------------------------------------------------------
{
coord w = Screen.area().width();
coord h = Screen.area().height();
coord x = ppar.pixel_adjust(+ppar.xorigin, ppar.xmin, ppar.xmax, w);
coord y = ppar.pixel_adjust(+ppar.yorigin, ppar.ymax, ppar.ymin, h);
// Draw axes proper
pattern pat = Settings.Foreground();
Screen.fill(0, y, w, y, pat);
Screen.fill(x, 0, x, h, pat);
// Draw tick marks
coord tx = ppar.size_adjust(+ppar.xticks, ppar.xmin, ppar.xmax, w);
coord ty = ppar.size_adjust(+ppar.yticks, ppar.ymin, ppar.ymax, h);
if (tx > 0)
{
for (coord i = tx; x + i <= w; i += tx)
Screen.fill(x + i, y - 2, x + i, y + 2, pat);
for (coord i = tx; x - i >= 0; i += tx)
Screen.fill(x - i, y - 2, x - i, y + 2, pat);
}
if (ty > 0)
{
for (coord i = ty; y + i <= h; i += ty)
Screen.fill(x - 2, y + i, x + 2, y + i, pat);
for (coord i = ty; y - i >= 0; i += ty)
Screen.fill(x - 2, y - i, x + 2, y - i, pat);
}
// Draw arrows at end of axes
for (uint i = 0; i < 4; i++)
{
Screen.fill(w - 3*(i+1), y - i, w - 3*i, y + i, pat);
Screen.fill(x - i, 3*i, x + i, 3*(i+1), pat);
}
ui.draw_dirty(0, 0, w, h);
}
uint draw_data(array::iterator &it, array::iterator &end,
algebraic_g &x, algebraic_g &y, size_t xcol, size_t ycol)
// ----------------------------------------------------------------------------
// Fetch data from a stats array
// ----------------------------------------------------------------------------
{
if (it == end)
return 0;
object_p data = *it++;
if (data->is_real())
{
y = algebraic_p(data);
return 1;
}
else if (data->type() == object::ID_array)
{
array_p row = array_p(data);
algebraic_g xx, yy;
size_t col = 1;
for (object_p cdata : *row)
{
if (!cdata->is_real())
return false;
if (col == xcol)
xx = algebraic_p(cdata);
if (col == ycol)
yy = algebraic_p(cdata);
if (xx && yy)
{
x = xx;
y = yy;
return 2;
}
col++;
}
}
return 0;
}
object::result draw_plot(object::id kind,
const PlotParametersAccess &ppar,
object_g to_plot = nullptr)
// ----------------------------------------------------------------------------
// Draw an equation that takes input from the stack
// ----------------------------------------------------------------------------
{
object::result result = object::ERROR;
coord lx = -1;
coord ly = -1;
uint then = sys_current_ms();
algebraic_g min, max, step;
object::id dname;
// Select plotting parameters
switch(kind)
{
default:
case object::ID_Function:
min = ppar.xmin;
max = ppar.xmax;
dname = object::ID_Equation;
break;
case object::ID_Polar:
case object::ID_Parametric:
min = ppar.imin;
max = ppar.imax;
step = ppar.resolution;
if (step->is_zero())
step = (max - min) / integer::make(ScreenWidth());
dname = object::ID_Equation;
break;
case object::ID_Scatter:
case object::ID_Bar:
min = ppar.xmin;
max = ppar.xmax;
dname = object::ID_StatsData;
break;
}
step = ppar.resolution;
if (step->is_zero())
step = (max - min) / integer::make(ScreenWidth());
if (!to_plot)
{
to_plot = directory::recall_all(command::static_object(dname), false);
if (!to_plot)
{
if (dname == object::ID_Equation)
rt.no_equation_error();
else
rt.no_data_error();
return object::ERROR;
}
}
program_g eq;
array_g data;
array::iterator it, end;
size_t xcol = 0, ycol = 0;
size bar_width = 0, bar_skip = 0;
size bar_x = 0;
coord yzero = 0;
if (dname == object::ID_Equation)
{
if (to_plot->type() == object::ID_equation)
{
to_plot = equation_p(+to_plot)->value();
if (!to_plot)
return object::ERROR;
}
if (!to_plot->is_program())
{
rt.invalid_equation_error();
return object::ERROR;
}
eq = program_p(+to_plot);
}
else if (dname == object::ID_StatsData)
{
if (to_plot->type() != object::ID_array)
{
rt.invalid_plot_data_error();
return object::ERROR;
}
data = array_p(+to_plot);
size_t items = data->items();
data = array_p(+to_plot);
step = (max - min) / integer::make(items);
bar_skip = items && items < ScreenWidth() ? ScreenWidth() / items : 1;
bar_width = bar_skip > 2 ? bar_skip - 2: bar_skip;
it = data->begin();
end = data->end();
StatsParameters::Access stats;
xcol = stats.xcol;
ycol = stats.ycol;
yzero = ppar.pixel_y(integer::make(0));
}
algebraic_g x = min;
algebraic_g y;
save<symbol_g *> iref(expression::independent,
(symbol_g *) &ppar.independent);
settings::PrepareForFunctionEvaluation willEvaluateFunction;
if (ui.draw_graphics())
if (Settings.DrawPlotAxes())
draw_axes(ppar);
bool split_points = Settings.NoCurveFilling();
size lw = Settings.LineWidth();
pattern fg = Settings.Foreground();
pattern errbg = Settings.PlotErrorBackground();
while (!program::interrupted())
{
coord rx = 0;
coord ry = 0;
uint dcount = 1;
if (dname == object::ID_Equation)
{
y = algebraic::evaluate_function(eq, x);
}
else
{
dcount = draw_data(it, end, x, y, xcol, ycol);
if (!dcount)
break;
}
if (y)
{
switch(kind)
{
default:
case object::ID_Function:
rx = ppar.pixel_x(x);
ry = ppar.pixel_y(y);
break;
case object::ID_Polar:
{
algebraic_g i = rectangular::make(integer::make(0),
integer::make(1));
y = y * exp::run(i * x);
}
// Fall-through
case object::ID_Parametric:
if (y->is_real())
y = rectangular::make(y, integer::make(0));
if (y)
{
if (algebraic_g cx = y->algebraic_child(0))
rx = ppar.pixel_x(cx);
if (algebraic_g cy = y->algebraic_child(1))
ry = ppar.pixel_y(cy);
}
break;
case object::ID_Scatter:
case object::ID_Bar:
rx = ppar.pixel_x(x);
ry = ppar.pixel_y(y);
break;
}
}
if (y)
{
if (kind != object::ID_Bar)
{
if (lx < 0 || split_points)
{
lx = rx;
ly = ry;
}
Screen.line(lx,ly,rx,ry, lw, fg);
}
else
{
lx = bar_x;
ly = dcount == 1 ? yzero : rx;
rx = lx + bar_width - 1;
if (ry < ly)
std::swap(ly, ry);
Screen.fill(lx, ly, rx, ry, fg);
bar_x += bar_skip;
}
ui.draw_dirty(lx, ly, rx, ry);
lx = rx;
ly = ry;
}
else
{
if (kind == object::ID_Function)
{
rx = ppar.pixel_x(x);
ry = ppar.pixel_y(ppar.yorigin);
if (ry < 0)
ry = 0;
else if (ry >= LCD_H)
ry = LCD_H-1;
rect r(rx, ry - LCD_H/32, rx, ry + LCD_H/32);
Screen.fill(r, errbg);
ui.draw_dirty(r);
}
if (!rt.error())
rt.invalid_function_error();
Screen.text(0, 0, rt.error(), ErrorFont,
pattern::white, pattern::black);
ui.draw_dirty(0, 0, LCD_W, ErrorFont->height());
lx = ly = -1;
rt.clear_error();
}
if (kind != object::ID_Scatter)
{
x = x + step;
if (kind != object::ID_Bar)
{
algebraic_g cmp = x > max;
if (!cmp)
goto err;
if (cmp->as_truth(false))
break;
}
}
uint now = sys_current_ms();
if (now - then >= Settings.PlotRefreshRate())
{
refresh_dirty();
then = sys_current_ms();
}
}
result = object::OK;
err:
refresh_dirty();
return result;
}
static object::result draw_plot(object::id type)
// ----------------------------------------------------------------------------
// Draw the various kinds of plot
// ----------------------------------------------------------------------------
{
if (object_g eq = rt.pop())
{
PlotParametersAccess ppar;
return draw_plot(type, ppar, eq);
}
return object::ERROR;
}
COMMAND_BODY(Function)
// ----------------------------------------------------------------------------
// Draw plot from function on the stack taking stack arguments
// ----------------------------------------------------------------------------
{
return draw_plot(ID_Function);
}
COMMAND_BODY(Parametric)
// ----------------------------------------------------------------------------
// Draw plot from function on the stack taking stack arguments
// ----------------------------------------------------------------------------
{
return draw_plot(ID_Parametric);
}
COMMAND_BODY(Polar)
// ----------------------------------------------------------------------------
// Draw polar plot from function on the stack
// ----------------------------------------------------------------------------
{
return draw_plot(ID_Polar);
}
COMMAND_BODY(Scatter)
// ----------------------------------------------------------------------------
// Draw scatter plot from data on the stack
// ----------------------------------------------------------------------------
{
return draw_plot(ID_Scatter);
}
COMMAND_BODY(Bar)
// ----------------------------------------------------------------------------
// Draw bar plot from data on the stack
// ----------------------------------------------------------------------------
{
return draw_plot(ID_Bar);
}
COMMAND_BODY(Draw)
// ----------------------------------------------------------------------------
// Draw plot in EQ according to PPAR
// ----------------------------------------------------------------------------
{
PlotParametersAccess ppar;
switch(ppar.type)
{
default:
case ID_Function:
case ID_Parametric:
case ID_Polar:
return draw_plot(ppar.type, ppar);
}
rt.invalid_plot_type_error();
return ERROR;
}
COMMAND_BODY(Drax)
// ----------------------------------------------------------------------------
// Draw plot axes
// ----------------------------------------------------------------------------
{
ui.draw_graphics();
PlotParametersAccess ppar;
draw_axes(ppar);
refresh_dirty();
return OK;
}