forked from erikkaashoek/NanoVNA-App
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Graphs.h
393 lines (320 loc) · 14.1 KB
/
Graphs.h
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
// (c) OneOfEleven 2020
//
// This code can be used on terms of WTFPL Version 2 (http://www.wtfpl.net)
#ifndef GraphsH
#define GraphsH
//#define USE_OPENGL
#define VC_EXTRALEAN
#define WIN32_EXTRA_LEAN
#define WIN32_LEAN_AND_MEAN
#include <Vcl.Graphics.hpp>
#ifdef USE_OPENGL
#define GL_GLEXT_PROTOTYPES
#define WGL_WGLEXT_PROTOTYPES
//#pragma link "glut.lib"
#include <windows.h>
// #include <gl/glew.h>
#include "gl/gl.h"
#include "gl/glext.h"
#include "gl/glu.h"
#include "gl/glaux.h"
#include "gl/wglext.h"
#endif
// GDI+ system
//#include <Unknwn.h>
//#include <windows.h>
#include <algorithm>
//using std::min;
//using std::max;
#pragma option push
#pragma warn -8022
#include <gdiplus.h>
// #pragma link "gdiplus.lib"
// #pragma comment (lib, "gdiplus.lib")
// using namespace Gdiplus;
#pragma option pop
#include <vector>
#include "types.h"
#include "settings.h"
#include "common.h"
#include "median_filter.h"
#include "smoothing_filter.h"
#include "biquad_filter.h"
#include "tfft.h"
// ****************************************************
#ifdef USE_OPENGL
#pragma pack(push, 1)
struct GLrgb
{
GLubyte r;
GLubyte g;
GLubyte b;
GLrgb()
{
r = 0;
g = 0;
b = 0;
}
GLrgb(GLubyte red, GLubyte grn, GLubyte blu)
{
r = red;
g = grn;
b = blu;
}
};
struct GLrgba
{
GLubyte r;
GLubyte g;
GLubyte b;
GLubyte a;
GLrgba()
{
r = 0;
g = 0;
b = 0;
a = 255;
}
GLrgba(GLubyte red, GLubyte grn, GLubyte blu)
{
r = red;
g = grn;
b = blu;
a = 255;
}
GLrgba(GLubyte red, GLubyte grn, GLubyte blu, GLubyte alpha)
{
r = red;
g = grn;
b = blu;
a = alpha;
}
};
#pragma pack(pop)
#pragma pack(push, 4) // make sure this number is the same size as a GLfloat!!!!!!!!!!!!!
struct GLvertex
{
GLfloat X;
GLfloat Y;
};
#pragma pack(pop)
struct glFont
{
Graphics::TBitmap *bitmap;
TFont *font;
int height;
GLuint base;
GLuint base_start;
GLYPHMETRICSFLOAT gmf[256];
};
#endif
// ****************************************************
class CGraphs
{
private:
Gdiplus::GdiplusStartupInput m_gdi_plus_startup_input;
ULONG_PTR m_gdi_plus_token;
Gdiplus::Graphics *m_gdi_plus;
TPaintBox *m_pb;
TForm *m_form;
TPanel *m_panel;
std::vector <Gdiplus::PointF> m_gdi_points;
#ifdef USE_OPENGL
struct
{
TWinControl *win_control;
HDC hdc;
HGLRC hrc;
int pixel_format;
PIXELFORMATDESCRIPTOR pfd;
std::vector <glFont> font;
bool rebuild_fonts;
} m_gl;
#endif
CMedianFilter median_filter;
CSmoothingFilter smoothing_filter;
CBiQuadFilter biquad_filter;
struct
{
int graph_type;
TColor vswr2_colour;
TColor grid_colour;
TColor back_colour;
bool both_scales;
Graphics::TBitmap *bitmap;
#ifdef USE_OPENGL
GLuint bitmap_texture;
// GLuint bitmap_pbo;
#endif
} m_grid[MAX_GRAPHS];
#ifdef USE_OPENGL
void __fastcall glRebuildFonts();
int __fastcall glBuildFont(HDC hdc, String name, int size, bool bold);
int __fastcall glTextWidth(int font_index, String s);
int __fastcall glTextHeight(int font_index, String s);
void glPrint(int font_index, GLint x, GLint y, const char *fmt, ...);
void __fastcall glTextOut(int font_index, GLint x, GLint y, String s, bool transparent = false);
void __fastcall glCircle(const float x, const float y, const float radius, const float deg_start, const float deg_end, const int segments, const bool fill);
GLuint __fastcall createTexture(Graphics::TBitmap *bitmap, bool pixel_perfect);
void __fastcall renderTexture(const TRect rect, const GLuint texture_id);
GLrgba __fastcall colourToGLcolour(TColor colour);
#endif
uint32_t __fastcall colourToRGB(TColor colour, uint8_t &red, uint8_t &grn, uint8_t &blu);
void __fastcall computeVisibleIndexRange(const int graph, const int graph_type);
void __fastcall traceLPF(const int graph, const int graph_type, const float level, const int interations = 3);
void __fastcall traceSmooth(const int graph, const int graph_type);
void __fastcall computeTraceMinMax(const int graph, const int graph_type);
void __fastcall computeCalTraceMinMax(const int graph, const int graph_type);
void __fastcall computeGraphMinMax(const int graph, const int graph_type, double &max_levels, double &min_levels);
void __fastcall drawMouseMarker(const int x, const int y, const int size);
void __fastcall drawMarker(const int graph, const int x, const int y, const bool upsidedown_marker, const bool highlighted, const bool selected, String s1, String s2 = "", String s3 = "", String s4 = "", String s5 = "", String s6 = "");
void __fastcall drawMarkersOnGraph(const int graph, const int mem, const int channel, String units_str, String id);
void __fastcall drawMarkersOnGraph(const int graph, const int mem, const int channel1, const int channel2, String units_str, String id1, String id2);
void __fastcall drawMarkers(const int graph, const int mem, const int channel, const float level_min, const float level_max, AnsiString units_str, const float gamma, const bool draw_v_line);
void __fastcall drawMouse(const int graph, const int graph_type, const double level_min, const double level_max, const double gamma);
void __fastcall drawMaxMarkers(const int graph, const int graph_type, const double level_min, const double level_max, const double gamma, String units, String id1, String id2);
void __fastcall drawMinMarkers(const int graph, const int graph_type, const double level_min, const double level_max, const double gamma, String units, String id1, String id2);
void __fastcall drawMarkersSmithPolar(const int graph, const int mem, const int channel, std::vector <t_pointf> &line_points, bool admittance, bool polar);
void __fastcall drawMarkersOnSmithPolarGraph(const int graph, const int mem, const int channel, bool admittance, bool polar);
void __fastcall drawMouseSmithPolar(const int graph, const int graph_type, const int mem, const int channel, std::vector <t_pointf> &line_points, bool admittance, bool polar);
double __fastcall computeGraticuleScale(const double min_spacing, const double scale, double &step, int &mult_index);
void __fastcall drawFreqLines(const int graph, const int graph_type);
void __fastcall drawTimeDistLines(const int graph, const int mem);
void __fastcall drawNoneLinMagLines(const int graph, const bool left_side, const double level_min, const double level_max, double gamma, const double initial_step_size, String fmt, String units, const bool is_vswr = false);
void __fastcall drawMagLines(const int graph, const bool left_size, const double level_min, const double level_max, const double initial_step_size, String fmt, String units);
void __fastcall drawSmithChart(const int graph, const int graph_type, const bool admittance, const float max_levels);
void __fastcall drawPolarChart(const int graph, const int graph_type, const float max_level);
void __fastcall drawPhaseVectorChart(const int graph, const int graph_type);
void __fastcall drawLCMatching(const int graph, const int graph_type, const int mem);
void __fastcall drawPoints(const int graph, const int graph_type, std::vector <t_pointf> &points_f, bool draw_spline, int line_width, bool show_points, TColor line_colour);
void __fastcall drawGraphLines(const int graph, const int graph_type);
void __fastcall drawDetails(const int graph, const int graph_type, String title);
void __fastcall drawDetails(const int graph, const int graph_type, const uint8_t chan_mask, const int mem, const int index, String title, String units, String s[], String s_value[], String min_max_fmt);
void __fastcall drawLogMagCalibrations(const int graph, const int graph_type, const uint8_t trace_mask, const bool show_marker_text);
void __fastcall drawLogMagS11S21(const int graph, const int graph_type, const uint8_t chan_mask, const bool show_marker_text);
void __fastcall drawLinMagS11S21(const int graph, const int graph_type, const uint8_t chan_mask, const bool show_marker_text);
void __fastcall drawPhaseS11S21(const int graph, const int graph_type, const uint8_t chan_mask, const bool unwrap, const bool show_marker_text);
void __fastcall drawVSWRReturnLossS11(const int graph, const int graph_type, const bool show_marker_text);
void __fastcall drawImpedanceS11(const int graph, const int graph_type, const bool show_marker_text);
void __fastcall drawQualityFactorS11(const int graph, const int graph_type, const bool show_marker_text);
void __fastcall drawQCS11(const int graph, const int graph_type, const bool show_marker_text);
void __fastcall drawQLS11(const int graph, const int graph_type, const bool show_marker_text);
void __fastcall drawGroupDelayS11S21(const int graph, const int graph_type, const uint8_t chan_mask, const bool show_marker_text);
void __fastcall drawSeriesRJX(const int graph, const int graph_type, const int mask, const bool show_marker_text);
void __fastcall drawParallelRJX(const int graph, const int graph_type, const int mask, const bool show_marker_text);
void __fastcall drawRealImag(const int graph, const int graph_type, const int channel, const bool show_marker_text);
void __fastcall drawCoaxLossS11(const int graph, const int graph_type, const bool show_marker_text);
void __fastcall drawSmithS11S21(const int graph, const int graph_type, const bool admittance, const int s_channel, const bool show_marker_text);
void __fastcall drawPolarS11S21(const int graph, const int graph_type, const int s_channel, const bool show_marker_text);
void __fastcall drawPhaseVectorS11S21(const int graph, const int graph_type, const int s_channel, const bool show_marker_text);
void __fastcall drawTDRLinear(const int graph, const int graph_type, const bool show_marker_text);
void __fastcall drawTDRLog(const int graph, const int graph_type, const bool show_marker_text);
void __fastcall drawTDRImpedance(const int graph, const int graph_type, const bool show_marker_text);
void __fastcall updateFFT(const int graph, const int graph_type, const int mem);
void __fastcall fetchSamples(int history_position);
void __fastcall setGraphPos(const int graph, const int x, const int y, const int w, const int h, const int double_v_scale);
void __fastcall arrangeGraph(const int graph_type1, const int graph_type2, const int graph_type3, const int graph_type4, const int num_graphs);
void __fastcall drawBorder();
void __fastcall drawGraph(const int graph, const int graph_type, const bool show_marker_text);
public:
__fastcall CGraphs();
__fastcall ~CGraphs();
TFFT m_fft[MAX_GRAPHS][MAX_MEMORIES];
Graphics::TBitmap *m_graph_bm;
float m_lpf_coeff;
struct
{
int x; // x
int y; // y
int w; // width
int h; // height
int gx; // graph area x
int gy; // graph area y
int gw; // graph area width
int gh; // graph area height
int cx; // graph area center x
int cy; // graph area center y
int cr; // graph area radius
} m_graph_pos[MAX_GRAPHS];
struct
{
int x;
int y;
int graph;
int marker_mem;
int marker_index;
int point_mem;
int point_index;
int time_mem;
int time_index;
double Hz;
double secs;
complexf cpx;
float mag;
int graph_type_select;
int graph_scale_pos;
int graph_v_scale;
int graph_h_scale;
int graph_scale_pos_drag;
int graph_v_scale_drag;
int graph_h_scale_drag;
int graph_drag;
int down_graph;
double down_Hz;
double down_max;
double down_min;
double down_val;
int down_x;
int down_y;
int down_marker;
} m_mouse;
//std::vector <float> m_levels[MAX_MEMORIES][MAX_CHANNELS];
//std::vector <t_pointf> m_line_points[MAX_GRAPHS][MAX_MEMORIES][MAX_CHANNELS];
std::vector <float> m_levels[MAX_MEMORIES][MAX_CAL_TRACES];
std::vector <t_pointf> m_line_points[MAX_GRAPHS][MAX_MEMORIES][MAX_CAL_TRACES];
struct
{
int channel;
int index;
double value;
} m_max[MAX_GRAPHS];
struct
{
int channel;
int index;
double value;
} m_min[MAX_GRAPHS];
int m_min_visible_index[MAX_GRAPHS][MAX_MEMORIES];
int m_max_visible_index[MAX_GRAPHS][MAX_MEMORIES];
int __fastcall numberOfGraphs();
bool __fastcall isFrequencyGraph(const int graph_type);
bool __fastcall isSmithGraph(const int graph_type);
bool __fastcall isAdmittanceGraph(const int graph_type);
bool __fastcall isPolarGraph(const int graph_type);
bool __fastcall isTDRGraph(const int graph_type);
bool __fastcall isTimeGraph(const int graph_type);
float __fastcall freqToX(const int graph, const int graph_type, const int64_t freq);
int64_t __fastcall xyToFreq(const int graph, const int graph_type, int x, int y);
bool __fastcall nearestPoint(const int graph, const int graph_type, const int x, const int y, const int max_pixel_dist, int &graph_num, int &mem, int &channel, int &index, int64_t &Hz, double &secs);
void __fastcall resetAutoMinMaxCounters();
#ifndef USE_OPENGL
void __fastcall onPaint(TObject *Sender,
const bool show_marker_text,
const int graph_type1,
const int graph_type2,
const int graph_type3,
const int graph_type4,
const int num_graphs,
const int history_position);
#else
void __fastcall glInit(TWinControl *win_control);
void __fastcall glResize(const int width, const int height);
void __fastcall glRenderScene(TObject *Sender, const bool show_marker_text, const int graph_type1, const int graph_type2, const int graph_type3, const int graph_type4, const int num_graphs, const int history_position);
Graphics::TBitmap * __fastcall glCopyScreen();
void __fastcall glQueuRebuildFonts()
{
m_gl.rebuild_fonts = true;
}
#endif
};
#endif