File: OpenGLGraph.h

package info (click to toggle)
limesuite 18.06.0+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 24,728 kB
  • sloc: cpp: 154,989; ansic: 6,823; python: 140; sh: 50; makefile: 32
file content (408 lines) | stat: -rw-r--r-- 8,443 bytes parent folder | download | duplicates (2)
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
/**
@file	OpenGLGraph.h
@author Lime Microsystems
@brief	Header for OpenGLGraph.h
*/

#ifndef OPENGL_GRAPH
#define OPENGL_GRAPH

#include <GL/glew.h>
#if defined(__APPLE__)
#include <OpenGL/gl.h>
#else
#include <GL/gl.h>
#endif
#include <wx/wx.h>
#include <wx/glcanvas.h>
#include <wx/timer.h>

#include <string>
#include <vector>
#include "string.h"

enum eOGLGMouseButton
{
	OGLG_LEFT,
	OGLG_RIGHT,
	OGLG_MIDDLE
};

enum eOGLGActionState
{
    OGLG_IDLE,
    OGLG_ZOOMIN,
    OGLG_PAN,
    OGLG_SCALE,
    OGLG_ADD_MARKER,
    OGLG_MOVE_MARKER,
    OGLG_REMOVE_MARKER,
    OGLG_SEARCH_PEAK
};

enum
{
    OGLG_FIT = 3000,
    OGLG_LOCKASPECT,
    OGLG_HELP_MOUSE,
    OGLG_ADD_MARK,
    OGLG_SHOW_MARKERS_MENU,
    OGLG_RESET
};

enum eDrawingMode
{
	GLG_POINTS = 0,
	GLG_LINE
};

template<class T>
struct sRect
{
	sRect(T X1, T X2, T Y1, T Y2)
	{
		x1 = X1;
		x2 = X2;
		y1 = Y1;
		y2 = Y2;
	}
	T x1,x2,y1,y2;
	void set(T X1, T X2, T Y1, T Y2)
	{
		x1 = X1;
		x2 = X2;
		y1 = Y1;
		y2 = Y2;
	}
};

struct GLG_color
{
	GLG_color(unsigned int rgba)
	{
		red = (rgba >> 24)  / 255.0;
		green = ((rgba >> 16) & 0xFF) / 255.0;
		blue = ((rgba >> 8) & 0xFF) / 255.0;
		alpha = (rgba & 0xFF) / 255.0;
	}
	GLG_color() : red(0.5), green(0.5), blue(0.5), alpha(1.0)
	{}

	unsigned int getColor4b()
	{
	    unsigned int color = 0;
	    color |= (unsigned int)(255*red) << 24;
	    color |= (unsigned int)(255*green) << 16;
	    color |= (unsigned int)(255*blue) << 8;
	    color |= (unsigned int)(255*alpha);
	    return color;
	}

	float red;
	float green;
	float blue;
	float alpha;
};

class cDataSerie
{
public:
	cDataSerie() : size(0), allocatedSize(0), vboIndex(0), visible(true), modified(true), values(NULL)
	{
		color = 0x000000FF;
		Initialize(10);
	};

	~cDataSerie()
	{
        if(values)
            delete []values;
	};

	void AssignValues(float *xserie, float *yserie, unsigned int count)
	{
		if(2*count > allocatedSize && count > 0)
			Initialize(2*count);
        if(xserie && yserie)
        {
            int index = 0;
            for(unsigned i=0; i<count; ++i)
            {
                values[index] = xserie[i];
                ++index;
                values[index] = yserie[i];
                ++index;
            }
        }
        modified = true;
        size = count;
	}
	void AssignValues(float *valuesXY, unsigned int length)
	{
		if(length > allocatedSize && length > 0)
			Initialize(length);
		if(valuesXY)
            memcpy(values, valuesXY, (length)*sizeof(float));
        modified = true;
        size = length/2;
	}

	void Clear()
	{
        if(values)
            delete []values;
		values = NULL;
		size = 0;
		allocatedSize = 0;
		modified = true;
	}
	void Initialize(unsigned int count)
	{
		Clear();
		size = count;
		values = new float[2*count];
		memset(values, 0, sizeof(float)*2*count);
		allocatedSize = count;
	}
	void AddXY(float x, float y)
	{
		if(size < allocatedSize)
		{
			values[2*size] = x;
			values[2*size+1] = y;
			++size;
		}
		else
		{
			float *newVal = new float [size*2];
			allocatedSize = size*2;
			memcpy(newVal, values, sizeof(float)*size*2);
			delete []values;
			values = newVal;
			values[2*size] = x;
			values[2*size+1] = y;
			++size;
		}
		modified = true;
	}

	unsigned int size;
	unsigned int allocatedSize;
	unsigned int vboIndex;
	GLG_color color;
	bool visible;
	bool modified;

	float *values;
};

struct GLG_settings
{
	GLG_settings();
	std::string title;
	std::string titleXaxis;
	std::string titleYaxis;

	std::string xUnits;
	std::string yUnits;

	bool drawGridX;
	bool drawGridY;
	bool drawTitle;
	bool drawTitleX;
	bool drawTitleY;

	int windowWidth;
	int windowHeight;
	int dataViewWidth;
	int dataViewHeight;

	int marginTop;
	int marginBottom;
	int marginLeft;
	int marginRight;
	bool useVBO;

	GLG_color backgroundColor;
	GLG_color titlesColor;
	GLG_color dataViewBackgroundColor;
	GLG_color dataViewPerimeterColor;
	GLG_color gridColor;
	eDrawingMode graphType;

	sRect<double> visibleArea;

	float gridXstart;
	float gridYstart;
	float gridXspacing;
	float gridYspacing;
	int gridXlines;
	int gridYlines;
	int gridXprec;
	int gridYprec;
	int gridValuesHeight;

	float pointsSize;
	float fontSize;

	bool staticGrid;
	bool lock_aspect;
	bool markersEnabled;
	float gridXoffset;
};

struct OGLMarker
{
	OGLMarker() : color(0x007F00FF)
	{
	    used = false;
		posX = 0;
		posY = 0;
		size = 10;
		iposX = 0;
		iposY = 0;
		dataValueIndex = 0;
		id = 0;
		show = true;
    }
	float posX, posY; //data view position
	int iposX, iposY; //window position
	float size;
	GLG_color color;
	int dataValueIndex;
	int id;
	bool used;
	bool show;
};

class GLFont;
class dlgMarkers;

class OpenGLGraph : public wxGLCanvas
{
friend class dlgMarkers;
public:
    static const int GLCanvasAttributes[8];

	OpenGLGraph(wxWindow* parent,  wxWindowID id, const wxPoint& pos, const wxSize& size, long style, const wxString& name = wxEmptyString, const int* args = GLCanvasAttributes);
	virtual ~OpenGLGraph();

	bool Initialize(int width, int height);
	void Resize(int w, int h);

	void AddSerie(cDataSerie* serie);
	void RemoveSeries(unsigned int i);

	void SetInitialDisplayArea(float minx, float maxx, float miny, float maxy);
	void SetDisplayArea(float minx, float maxx, float miny, float maxy);
	void ZoomY( float centerY, float spanY);
	void ZoomX( float centerX, float spanX);
	void Zoom( float centerX, float centerY, float spanX, float spanY);
	void ZoomRect( int x1, int x2, int y1, int y2);
	void Pan( float dx, float dy);

	void SetDrawingMode( eDrawingMode mode );
	void Draw();

	bool SaveConfig(char *file);
	bool LoadConfig(char *file);

	std::vector<cDataSerie*> series;

	GLG_settings settings;
	void SettingsChanged();

	void OnMouseDown(int mouseButton, int X, int Y);
	void OnMouseUp(int mouseButton, int X, int Y);
	void OnMouseMove(int X, int Y);
	void ResetView();
	void Fit();

    void SetMarker(int id, float xValue, bool enabled, bool show);
    void GetMarker(int id, float &xValue, float &yValue, bool &enabled, bool &show);
	int AddMarker(int posX);
	int AddMarkerAtValue(float xValue);
	void RemoveMarker();
	void RemoveMarker(int id);
	void MoveMarker(int markerID, int posX);
	void ChangeMarker(int markerID, float xValue);
	bool SearchPeak();

	void SetInfoMessage(const char* msg, unsigned int index);

	// events
    void render(wxPaintEvent& evt);
    void resized(wxSizeEvent& evt);
	void mouseMoved(wxMouseEvent& event);
	void mouseWheelMoved(wxMouseEvent& event);
	void mouseReleased(wxMouseEvent& event);
	void rightClick(wxMouseEvent& event);
	void leftClick(wxMouseEvent& event);
	void middleClick(wxMouseEvent& event);
	void mouseLeftWindow(wxMouseEvent& event);
	void keyPressed(wxKeyEvent& event);
	void keyReleased(wxKeyEvent& event);
	void onFit(wxCommandEvent& event);
	void onMouseHelp(wxCommandEvent& event);
	void onAddMarker(wxCommandEvent& event);
	void onRemoveMarker(wxCommandEvent& event);
	void onShowMarkersMenu(wxCommandEvent& event);
	void onLockAspect(wxCommandEvent& event);
	void onSearchPeak(wxCommandEvent& event);
	void OnTimer(wxTimerEvent& event);
	void onReset(wxCommandEvent& event);
	DECLARE_EVENT_TABLE()

private:
	bool oglOk;
	bool glInitialized;
    void UpdateInfoDisplay();
    std::vector<std::string> info_msg;
    std::vector<std::string> info_msg_toDisplay;
    static const unsigned int mMarkerColors[];

    dlgMarkers* mMarkersDlg;
    void ShowMenu(int x, int y);
    wxMenu m_popmenu;
    void setupViewport(int w, int h);
    wxGLContext *m_DrawingContext;
	eOGLGActionState m_actionState;

    bool viewChanged;
	bool initialized;
	sRect<float> initialDisplayArea;
	sRect<int> m_MouseCoord;
	float m_lastSpanX;
	float m_lastSpanY;

	bool isInsideDataView(int X, int Y);
	void dataViewPixelToValue(int x, int y, float &valX, float &valY);

	void DrawStaticElements();
	void DrawMarkers();
	void CalculateGrid();

	void switchToWindowView();
	void switchToDataView();

	GLvoid glRenderText(float posx, float posy, float angle, float scale, unsigned int rgba, const char *fmt, ...);
	int TextWidthInPixels(const char *str);
	int NumberWidthInPixels(float num, unsigned int prec = 0);
	int LineHeight();

	float calcAxisGrid(GLG_settings st, bool xAxis);

    GLFont *m_font;

	std::vector<OGLMarker> markers;
	int m_selectedMarker;
	unsigned m_maxMarkers;
	int clickedOnMarker(int X, int Y);

	bool m_currentlyDrawing;
	wxTimer* m_timer;
	wxGLContext *m_glContext;

};

#endif