1 /**
2 @file	OpenGLGraph.h
3 @author Lime Microsystems
4 @brief	Header for OpenGLGraph.h
5 */
6 
7 #ifndef OPENGL_GRAPH
8 #define OPENGL_GRAPH
9 
10 #include "glew/GL/glew.h"
11 #if defined(__APPLE__)
12 #include <OpenGL/gl.h>
13 #else
14 #include <GL/gl.h>
15 #endif
16 #include <wx/wx.h>
17 #include <wx/glcanvas.h>
18 #include <wx/timer.h>
19 
20 #include <string>
21 #include <vector>
22 #include "string.h"
23 
24 enum eOGLGMouseButton
25 {
26 	OGLG_LEFT,
27 	OGLG_RIGHT,
28 	OGLG_MIDDLE
29 };
30 
31 enum eOGLGActionState
32 {
33     OGLG_IDLE,
34     OGLG_ZOOMIN,
35     OGLG_PAN,
36     OGLG_SCALE,
37     OGLG_ADD_MARKER,
38     OGLG_MOVE_MARKER,
39     OGLG_REMOVE_MARKER,
40     OGLG_SEARCH_PEAK
41 };
42 
43 enum
44 {
45     OGLG_FIT = 3000,
46     OGLG_LOCKASPECT,
47     OGLG_HELP_MOUSE,
48     OGLG_ADD_MARK,
49     OGLG_SHOW_MARKERS_MENU,
50     OGLG_RESET
51 };
52 
53 enum eDrawingMode
54 {
55 	GLG_POINTS = 0,
56 	GLG_LINE
57 };
58 
59 template<class T>
60 struct sRect
61 {
sRectsRect62 	sRect(T X1, T X2, T Y1, T Y2)
63 	{
64 		x1 = X1;
65 		x2 = X2;
66 		y1 = Y1;
67 		y2 = Y2;
68 	}
69 	T x1,x2,y1,y2;
setsRect70 	void set(T X1, T X2, T Y1, T Y2)
71 	{
72 		x1 = X1;
73 		x2 = X2;
74 		y1 = Y1;
75 		y2 = Y2;
76 	}
77 };
78 
79 struct GLG_color
80 {
GLG_colorGLG_color81 	GLG_color(unsigned int rgba)
82 	{
83 		red = (rgba >> 24)  / 255.0;
84 		green = ((rgba >> 16) & 0xFF) / 255.0;
85 		blue = ((rgba >> 8) & 0xFF) / 255.0;
86 		alpha = (rgba & 0xFF) / 255.0;
87 	}
GLG_colorGLG_color88 	GLG_color() : red(0.5), green(0.5), blue(0.5), alpha(1.0)
89 	{}
90 
getColor4bGLG_color91 	unsigned int getColor4b()
92 	{
93 	    unsigned int color = 0;
94 	    color |= (unsigned int)(255*red) << 24;
95 	    color |= (unsigned int)(255*green) << 16;
96 	    color |= (unsigned int)(255*blue) << 8;
97 	    color |= (unsigned int)(255*alpha);
98 	    return color;
99 	}
100 
101 	float red;
102 	float green;
103 	float blue;
104 	float alpha;
105 };
106 
107 class cDataSerie
108 {
109 public:
cDataSerie()110 	cDataSerie() : size(0), allocatedSize(0), vboIndex(0), visible(true), modified(true), values(NULL)
111 	{
112 		color = 0x000000FF;
113 		Initialize(10);
114 	};
115 
~cDataSerie()116 	~cDataSerie()
117 	{
118         if(values)
119             delete []values;
120 	};
121 
AssignValues(float * xserie,float * yserie,unsigned int count)122 	void AssignValues(float *xserie, float *yserie, unsigned int count)
123 	{
124 		if(2*count > allocatedSize && count > 0)
125 			Initialize(2*count);
126         if(xserie && yserie)
127         {
128             int index = 0;
129             for(unsigned i=0; i<count; ++i)
130             {
131                 values[index] = xserie[i];
132                 ++index;
133                 values[index] = yserie[i];
134                 ++index;
135             }
136         }
137         modified = true;
138         size = count;
139 	}
AssignValues(float * valuesXY,unsigned int length)140 	void AssignValues(float *valuesXY, unsigned int length)
141 	{
142 		if(length > allocatedSize && length > 0)
143 			Initialize(length);
144 		if(valuesXY)
145             memcpy(values, valuesXY, (length)*sizeof(float));
146         modified = true;
147         size = length/2;
148 	}
149 
Clear()150 	void Clear()
151 	{
152         if(values)
153             delete []values;
154 		values = NULL;
155 		size = 0;
156 		allocatedSize = 0;
157 		modified = true;
158 	}
Initialize(unsigned int count)159 	void Initialize(unsigned int count)
160 	{
161 		Clear();
162 		size = count;
163 		values = new float[2*count];
164 		memset(values, 0, sizeof(float)*2*count);
165 		allocatedSize = count;
166 	}
AddXY(float x,float y)167 	void AddXY(float x, float y)
168 	{
169 		if(size < allocatedSize)
170 		{
171 			values[2*size] = x;
172 			values[2*size+1] = y;
173 			++size;
174 		}
175 		else
176 		{
177 			float *newVal = new float [size*2];
178 			allocatedSize = size*2;
179 			memcpy(newVal, values, sizeof(float)*size*2);
180 			delete []values;
181 			values = newVal;
182 			values[2*size] = x;
183 			values[2*size+1] = y;
184 			++size;
185 		}
186 		modified = true;
187 	}
188 
189 	unsigned int size;
190 	unsigned int allocatedSize;
191 	unsigned int vboIndex;
192 	GLG_color color;
193 	bool visible;
194 	bool modified;
195 
196 	float *values;
197 };
198 
199 struct GLG_settings
200 {
201 	GLG_settings();
202 	std::string title;
203 	std::string titleXaxis;
204 	std::string titleYaxis;
205 
206 	std::string xUnits;
207 	std::string yUnits;
208 
209 	bool drawGridX;
210 	bool drawGridY;
211 	bool drawTitle;
212 	bool drawTitleX;
213 	bool drawTitleY;
214 
215 	int windowWidth;
216 	int windowHeight;
217 	int dataViewWidth;
218 	int dataViewHeight;
219 
220 	int marginTop;
221 	int marginBottom;
222 	int marginLeft;
223 	int marginRight;
224 	bool useVBO;
225 
226 	GLG_color backgroundColor;
227 	GLG_color titlesColor;
228 	GLG_color dataViewBackgroundColor;
229 	GLG_color dataViewPerimeterColor;
230 	GLG_color gridColor;
231 	eDrawingMode graphType;
232 
233 	sRect<double> visibleArea;
234 
235 	float gridXstart;
236 	float gridYstart;
237 	float gridXspacing;
238 	float gridYspacing;
239 	int gridXlines;
240 	int gridYlines;
241 	int gridXprec;
242 	int gridYprec;
243 	int gridValuesHeight;
244 
245 	float pointsSize;
246 	float fontSize;
247 
248 	bool staticGrid;
249 	bool lock_aspect;
250 	bool markersEnabled;
251 	float gridXoffset;
252 };
253 
254 struct OGLMarker
255 {
OGLMarkerOGLMarker256 	OGLMarker() : color(0x007F00FF)
257 	{
258 	    used = false;
259 		posX = 0;
260 		posY = 0;
261 		size = 10;
262 		iposX = 0;
263 		iposY = 0;
264 		dataValueIndex = 0;
265 		id = 0;
266 		show = true;
267     }
268 	float posX, posY; //data view position
269 	int iposX, iposY; //window position
270 	float size;
271 	GLG_color color;
272 	int dataValueIndex;
273 	int id;
274 	bool used;
275 	bool show;
276 };
277 
278 class GLFont;
279 class dlgMarkers;
280 
281 class OpenGLGraph : public wxGLCanvas
282 {
283 friend class dlgMarkers;
284 public:
285     static const int GLCanvasAttributes[8];
286 
287 	OpenGLGraph(wxWindow* parent,  wxWindowID id, const wxPoint& pos, const wxSize& size, long style, const wxString& name = wxEmptyString, const int* args = GLCanvasAttributes);
288 	virtual ~OpenGLGraph();
289 
290 	bool Initialize(int width, int height);
291 	void Resize(int w, int h);
292 
293 	void AddSerie(cDataSerie* serie);
294 	void RemoveSeries(unsigned int i);
295 
296 	void SetInitialDisplayArea(float minx, float maxx, float miny, float maxy);
297 	void SetDisplayArea(float minx, float maxx, float miny, float maxy);
298 	void ZoomY( float centerY, float spanY);
299 	void ZoomX( float centerX, float spanX);
300 	void Zoom( float centerX, float centerY, float spanX, float spanY);
301 	void ZoomRect( int x1, int x2, int y1, int y2);
302 	void Pan( float dx, float dy);
303 
304 	void SetDrawingMode( eDrawingMode mode );
305 	void Draw();
306 
307 	bool SaveConfig(char *file);
308 	bool LoadConfig(char *file);
309 
310 	std::vector<cDataSerie*> series;
311 
312 	GLG_settings settings;
313 	void SettingsChanged();
314 
315 	void OnMouseDown(int mouseButton, int X, int Y);
316 	void OnMouseUp(int mouseButton, int X, int Y);
317 	void OnMouseMove(int X, int Y);
318 	void ResetView();
319 	void Fit();
320 
321     void SetMarker(int id, float xValue, bool enabled, bool show);
322     void GetMarker(int id, float &xValue, float &yValue, bool &enabled, bool &show);
323 	int AddMarker(int posX);
324 	int AddMarkerAtValue(float xValue);
325 	void RemoveMarker();
326 	void RemoveMarker(int id);
327 	void MoveMarker(int markerID, int posX);
328 	void ChangeMarker(int markerID, float xValue);
329 	bool SearchPeak();
330 
331 	void SetInfoMessage(const char* msg, unsigned int index);
332 
333 	// events
334     void render(wxPaintEvent& evt);
335     void resized(wxSizeEvent& evt);
336 	void mouseMoved(wxMouseEvent& event);
337 	void mouseWheelMoved(wxMouseEvent& event);
338 	void mouseReleased(wxMouseEvent& event);
339 	void rightClick(wxMouseEvent& event);
340 	void leftClick(wxMouseEvent& event);
341 	void middleClick(wxMouseEvent& event);
342 	void mouseLeftWindow(wxMouseEvent& event);
343 	void keyPressed(wxKeyEvent& event);
344 	void keyReleased(wxKeyEvent& event);
345 	void onFit(wxCommandEvent& event);
346 	void onMouseHelp(wxCommandEvent& event);
347 	void onAddMarker(wxCommandEvent& event);
348 	void onRemoveMarker(wxCommandEvent& event);
349 	void onShowMarkersMenu(wxCommandEvent& event);
350 	void onLockAspect(wxCommandEvent& event);
351 	void onSearchPeak(wxCommandEvent& event);
352 	void OnTimer(wxTimerEvent& event);
353 	void onReset(wxCommandEvent& event);
354 	DECLARE_EVENT_TABLE()
355 
356 private:
357 	bool oglOk;
358 	bool glInitialized;
359     void UpdateInfoDisplay();
360     std::vector<std::string> info_msg;
361     std::vector<std::string> info_msg_toDisplay;
362     static const unsigned int mMarkerColors[];
363 
364     dlgMarkers* mMarkersDlg;
365     void ShowMenu(int x, int y);
366     wxMenu m_popmenu;
367     void setupViewport(int w, int h);
368     wxGLContext *m_DrawingContext;
369 	eOGLGActionState m_actionState;
370 
371     bool viewChanged;
372 	bool initialized;
373 	sRect<float> initialDisplayArea;
374 	sRect<int> m_MouseCoord;
375 	float m_lastSpanX;
376 	float m_lastSpanY;
377 
378 	bool isInsideDataView(int X, int Y);
379 	void dataViewPixelToValue(int x, int y, float &valX, float &valY);
380 
381 	void DrawStaticElements();
382 	void DrawMarkers();
383 	void CalculateGrid();
384 
385 	void switchToWindowView();
386 	void switchToDataView();
387 
388 	GLvoid glRenderText(float posx, float posy, float angle, float scale, unsigned int rgba, const char *fmt, ...);
389 	int TextWidthInPixels(const char *str);
390 	int NumberWidthInPixels(float num, unsigned int prec = 0);
391 	int LineHeight();
392 
393 	float calcAxisGrid(GLG_settings st, bool xAxis);
394 
395     GLFont *m_font;
396 
397 	std::vector<OGLMarker> markers;
398 	int m_selectedMarker;
399 	unsigned m_maxMarkers;
400 	int clickedOnMarker(int X, int Y);
401 
402 	bool m_currentlyDrawing;
403 	wxTimer* m_timer;
404 	wxGLContext *m_glContext;
405 
406 };
407 
408 #endif
409