-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemulDSO.cpp
1818 lines (1616 loc) · 62.6 KB
/
emulDSO.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
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <tchar.h>
#define USE_EMUL_DSO
#include "emulDSO.h"
#include <stdio.h>
#include <stdlib.h>
#include <float.h>
#include <math.h>
#include <utility>
#include <string>
#include <vector>
#include <map>
#include <windows.h>
#include <process.h>
#include <GdiPlus.h>
using namespace std;
using namespace Gdiplus;
#pragma comment(lib, "Gdiplus.lib")
#ifndef ULONG_PTR
#define ULONG_PRT unsigned long*
#endif
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//#include "dataManager.hpp"
struct data_entry
{
union {
float time;
float x;
};
union {
float value;
float y;
};
};
struct data_info
{
int id; //data id
int gid; //group id
TCHAR name[64];
TCHAR style[256];
std::vector<data_entry> data;
float range_min;
float range_max;
void data_id_range(float x0, float x1, int &id0, int &id1)
{
for(id0=0;id0<data.size();id0++) if(data[id0].time >= x0) break;
if(id0 > 0) id0 --;
for(id1=data.size()-1;id1>=id0;id1--) if(data[id1].time <= x1) break;
if(id1 < data.size()-1) id1 ++;
}
};
struct group_info
{
bool bIsDigital;
int iSpectraHeight;
float range_min;
float range_max;
std::vector<int> ids; //data id of this group
};
static void get_group_name(const TCHAR * name, TCHAR * gname)
{
const TCHAR * pdot = _tcsrchr(name, _TEXT('.'));
int cnt = _tcslen(name);
if (pdot)
cnt = pdot - name;
_tcsncpy(gname, name, cnt);
gname[cnt] = '\0';
}
//it is very danger to use pointer with STL, so we use vector & id reference as main storage mechanism
// *group list is only created when new data name are met
// *data range may keep changing when new data arrived
// these are all maintained by data_manager now;
struct DataManager
{
std::vector<data_info> data; //for most basic storage & iteration (both refernced by id).
std::map<std::string, int> name2id; //based on data, provide dataname key based reference
std::vector<group_info> group;
std::map<std::string, int> gname2ids; //based on data, provide groupname key based reference
float record_time;
float x_min;
float x_max;
DataManager(){x_min = FLT_MAX; x_max = -FLT_MAX;record_time = 0;}
void ticktock(float time_step_sec) { record_time += time_step_sec; }
void clear()
{
group.clear();
data.clear();
name2id.clear();
gname2ids.clear();
record_time = 0;
x_min = FLT_MAX;
x_max = -FLT_MAX;
}
void record(const TCHAR * data_name, const TCHAR * style, float x, float value)
{
if(x_min > x) x_min = x;
if(x_max < x) x_max = x;
data_entry p;
p.time = x;
p.value = value;
data_info *pdi = NULL;
group_info *pg = NULL;
if (name2id.find(data_name) == name2id.end())
{
data_info dnew;
data.push_back(dnew);
pdi = &(data.back());
pdi->id = data.size() - 1;
_tcscpy(pdi->name, data_name);
_tcscpy(pdi->style, style);
pdi->range_min = value;
pdi->range_max = value;
pdi->data.reserve(100*60);// this can speed up little
//mapping name
name2id[data_name] = pdi->id;
//new data name met(rarely), time to maintain groups
//and add new data in a groups
TCHAR group_name[128];
get_group_name(data_name, group_name);
if (gname2ids.find(group_name) == gname2ids.end())
{
group_info gnew;
group.push_back(gnew);
pg = &(group.back());
pdi->gid = group.size() - 1;
gname2ids[group_name] = pdi->gid;
pg->bIsDigital = true;
pg->range_max = value;
pg->range_min = value;
}
else
pdi->gid = gname2ids[group_name];
pg = &(group[pdi->gid]);
pg->ids.push_back(pdi->id); //add this new data into group
if (pg->bIsDigital && (_tcsstr(pdi->style, _TEXT("d")) == NULL)) pg->bIsDigital = false;
const TCHAR * pcfg = _tcsstr(pdi->style, _TEXT("s"));
pg->iSpectraHeight = 0;
if (pcfg != NULL) {
if (pcfg[1] > _TEXT('0') && pcfg[1] <= _TEXT('9'))
pg->iSpectraHeight = pcfg[1] - _TEXT('0');
else
pg->iSpectraHeight = 1;
}
}
else
{
pdi = &(data[name2id[data_name]]);
pg = &(group[pdi->gid]);
}
//maintain data ranges
if (pdi->range_min > value) pdi->range_min = value;
if (pdi->range_max < value) pdi->range_max = value;
if (pg->range_min > value) pg->range_min = value;
if (pg->range_max < value) pg->range_max = value;
pdi->data.push_back(p);
}
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
struct DSOCoordinate
{
Rect rc;
float x0, x1, y0, y1;
float scale_x, scale_y;
RectF clip_rc;
};
struct DSOClass
{
const TCHAR * title;
int width;
int height;
int plot_height;
int plot_totalHeight;
int scroll_y;
int vmargin;
int hmargin;
HANDLE main_thread;
HWND hwnd;
ULONG_PTR gdiplusToken;
Bitmap * pmemBitmap;
CachedBitmap * pcachedBitmap;
HANDLE hDirty;
bool bOpened;
Font * pfontAnnot;
Font * pfontAnnotVal;
Font * pfontDigital;
Font * pfontTicksY;
Font * pfontTicksX;
CRITICAL_SECTION critical_sec_data;
///////////////////////////////////////////////////////////////////////////////
//data recording interface
DataManager data_manager;
void reset_inner_timer(float t){ data_manager.record_time = t; }
void record(const TCHAR * data_name, const TCHAR * style, float x, float value){
::EnterCriticalSection(&critical_sec_data);
if(x == FLT_MAX || x== -FLT_MAX) x = data_manager.record_time;
data_manager.record(data_name, style, x, value);
time_x0 = data_manager.x_min;
time_x1 = data_manager.x_max;
::LeaveCriticalSection(&critical_sec_data);
::SetEvent(hDirty);
//::InvalidateRect(hwnd, NULL, FALSE);
}
DWORD last_invalidate_systime;
void ticktock(float time_step_sec)
{
::EnterCriticalSection(&critical_sec_data);
data_manager.ticktock(time_step_sec);
time_x0 = data_manager.x_min;
time_x1 = data_manager.x_max;
::LeaveCriticalSection(&critical_sec_data);
::SetEvent(hDirty);
DWORD systime = GetTickCount();
DWORD delta = systime - last_invalidate_systime;
if (delta > 40)
{
last_invalidate_systime = systime;
::InvalidateRect(hwnd, NULL, FALSE);
}
}
///////////////////////////////////////////////////////////////////////////////
DSOClass(const TCHAR * ptitle, int plot_width, int plot_height);
~DSOClass();
void reset(void);
void close(bool bWait);
void update(Graphics &graphics);
bool b_in_display;
void display(HDC hdc, PAINTSTRUCT *pps);
float scale_time;
float time_x0;
float time_x1;
float log_x1;
float time_cursor;
float time_down;
FUNC_KEYPROC func_keyproc;
DSOCoordinate cc;
void set_coord(Graphics &graphics, Rect &rc, float x0, float x1,float y0, float y1);
void draw_digital(Graphics &graphics, data_info & di, int id);
void draw_curve(Graphics &graphics, data_info & di, int id);
void draw_spectra(Graphics &graphics, data_info & di, int id, int cnt, int lineHeight);
void magnify(float time_center, int delta);
void magnify_min(void);
void settime(float x_set);
float x2time(int xPos);
void setcursor(float x_set);
static unsigned __stdcall Main(void* param);
static LRESULT CALLBACK WndProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam );
};
DSOClass::DSOClass(const TCHAR * ptitle, int plot_width, int config_height)
{
InitializeCriticalSection(&critical_sec_data);
title = ptitle;
height = width = plot_width;
plot_height = config_height;
func_keyproc = NULL;
last_invalidate_systime = GetTickCount();
hDirty = CreateEvent(NULL, FALSE, FALSE, _TEXT("Dirty"));
// Initialize GDI+.
GdiplusStartupInput gdiplusStartupInput;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
pfontAnnot = new Font(L"Arial", 14, FontStyleItalic, UnitPixel);
pfontAnnotVal = new Font(L"Arial", 14, FontStyleRegular, UnitPixel);
pfontDigital = new Font(L"Arial", 12, FontStyleRegular, UnitPixel);
pfontTicksY = new Font(L"Consolas", 14, FontStyleRegular, UnitPixel);
pfontTicksX = new Font(L"Consolas", 12, FontStyleRegular, UnitPixel);
main_thread = (HANDLE)_beginthreadex(NULL, 0, Main, this, 0, NULL);
pmemBitmap = NULL;
pcachedBitmap = NULL;
reset();
bOpened = true;
}
DSOClass::~DSOClass()
{
close(true);
}
void DSOClass::reset(void)
{
::EnterCriticalSection(&critical_sec_data);
plot_totalHeight = 0;
scroll_y = 0;
vmargin = 10;
hmargin = 80;
time_x0 = time_x1 = time_cursor = 0;
b_in_display = false;
log_x1 = -9876;
if (pmemBitmap) delete pmemBitmap;
if (pcachedBitmap) delete pcachedBitmap;
pmemBitmap = NULL;
pcachedBitmap = NULL;
scale_time = 1.0;
data_manager.clear();
::LeaveCriticalSection(&critical_sec_data);
}
void DSOClass::close(bool bWait)
{
if (bWait) ::WaitForSingleObject(main_thread, INFINITE);
DWORD thid = ::GetThreadId(main_thread);
PostThreadMessage(thid, WM_QUIT,0,0);
::WaitForSingleObject(main_thread, INFINITE);
delete pfontAnnot;
delete pfontAnnotVal;
delete pfontDigital;
delete pfontTicksY;
delete pfontTicksX;
Gdiplus::GdiplusShutdown(gdiplusToken);
DeleteCriticalSection(&critical_sec_data);
CloseHandle(hDirty);
title = NULL;
bOpened = false;
}
/*
the transform in GDI+ will deform shapes, so is not suitable for our purpose
so we have to do our own transform for x-scale and y-scale.
*/
#define FATAL_ERROR(str) do{_tprintf(_TEXT("Line %d: %s\r\n"),__LINE__, str);_exit(0);}while(0)
static int generate_ticks(float x0, float x1, int expect_tick_cnt, vector<float> &ticks)
{
ticks.clear();
if(expect_tick_cnt == 0) return 0;
//ticks should be as close as possible to the interval of 10^N
float expect_resolution = (x1 - x0) / expect_tick_cnt;
float fN = log10f(expect_resolution);
int N = fN < 0 ? (int)(fN - 0.99f) : (int)(fN); //rounding to the lower bound
float tick_step = pow(10.0f, N);
if(tick_step == 0) return 0;
if (tick_step >= expect_resolution) tick_step *= 1;
else if (2*tick_step >= expect_resolution) tick_step *= 2;
else if (5 * tick_step >= expect_resolution) tick_step *= 5;
else if (10 * tick_step >= expect_resolution) {
N ++;
tick_step *= 10;
}
else
FATAL_ERROR("");
float x = (int)(x0 / tick_step) * tick_step;
for (; x < x1; x += tick_step)
if (x > x0) ticks.push_back(x);
return N;
}
void DSOClass::set_coord(Graphics &graphics, Rect &rc,
float x0, float x1,
float y0, float y1)
{
cc.rc = rc;
cc.y0 = y0;
cc.y1 = y1;
cc.x0 = x0;
cc.x1 = x1;
cc.scale_y = -(float)rc.Height/(y1-y0);
cc.scale_x = (float)rc.Width/(x1-x0);
cc.clip_rc = RectF(cc.x0*cc.scale_x, cc.y1*cc.scale_y, (cc.x1-cc.x0)*cc.scale_x, -(cc.y1-cc.y0)*cc.scale_y);
graphics.ResetTransform();
graphics.ResetClip();
// (y1 - y)/(y1-y0) = (py - rc.Y)/rc.Height
// (y - y1)*scale_y = (py - rc.Y);
// so when y = 0, py = rc.Y - y1*scale_y
//
graphics.TranslateTransform(cc.rc.X - cc.x0*cc.scale_x, cc.rc.Y - cc.y1*cc.scale_y);
graphics.ScaleTransform(1, 1);
Pen pen(Color::Black, 1.0);
REAL dashValues[4] = {1, 2, 1, 2};
//pen.SetDashPattern(dashValues, 4);
//Draw the ticks(with number):
StringFormat stringformat;
stringformat.SetAlignment(StringAlignmentFar);
stringformat.SetLineAlignment(StringAlignmentCenter);
graphics.SetTextRenderingHint(TextRenderingHintAntiAlias);
SolidBrush brush(Color(255, 100, 100, 100));
RectF txtBox;
graphics.MeasureString(L"99.90", 4, pfontTicksY, RectF(), &stringformat, &txtBox);
txtBox.X = cc.x0*cc.scale_x - hmargin;
txtBox.Width =(REAL) hmargin;
//How many ticks can we display without overlapping
vector<float> ticks;
int N = generate_ticks(cc.y0, cc.y1, cc.rc.Height/txtBox.Height, ticks);
WCHAR strinfo[32];
for (int i = 0; i < ticks.size(); i++)
{
txtBox.Y = ticks[i] * cc.scale_y - txtBox.Height / 2;
if (N >= 0) swprintf(strinfo, L"%.0f", ticks[i]);
else if (N >= -1) swprintf(strinfo, L"%.1f", ticks[i]);
else if (N >= -2) swprintf(strinfo, L"%.2f", ticks[i]);
else if (N >= -3) swprintf(strinfo, L"%.3f", ticks[i]);
else if (N >= -4) swprintf(strinfo, L"%.4f", ticks[i]);
else swprintf(strinfo, L"%.05g", ticks[i]);
graphics.DrawString(strinfo, wcslen(strinfo), pfontTicksY, txtBox, &stringformat, &brush);
graphics.DrawLine(&pen, PointF(cc.x0*cc.scale_x, ticks[i] * cc.scale_y), PointF(cc.x0*cc.scale_x + 5, ticks[i] * cc.scale_y));
}
stringformat.SetAlignment(StringAlignmentCenter);
graphics.MeasureString(L" 99.90 ", 7, pfontTicksX, RectF(), &stringformat, &txtBox);
N = generate_ticks(cc.x0, cc.x1, (cc.rc.Width / txtBox.Width), ticks);
txtBox.Y = cc.y0*cc.scale_y;
for (int i = 0; i < ticks.size(); i++)
{
txtBox.X = ticks[i] * cc.scale_x - txtBox.Width / 2;
if (N >= 0) swprintf(strinfo, L"%.0f", ticks[i]);
else if (N >= -1) swprintf(strinfo, L"%.1f", ticks[i]);
else if (N >= -2) swprintf(strinfo, L"%.2f", ticks[i]);
else swprintf(strinfo, L"%.05g", ticks[i]);
graphics.DrawString(strinfo, wcslen(strinfo), pfontTicksX, txtBox, &stringformat, &brush);
graphics.DrawLine(&pen, PointF(ticks[i] * cc.scale_x, cc.y0*cc.scale_y), PointF(ticks[i] * cc.scale_x, cc.y0*cc.scale_y - 5));
}
//txtBox.X = cc.x1 * cc.scale_x - txtBox.Width / 2;
//swprintf(strinfo, L"%.2f", x1);
//graphics.DrawString(strinfo, wcslen(strinfo), pfontTicksX, txtBox, &stringformat, &brush);
// Draw the outline of the region.
graphics.DrawRectangle(&pen, cc.clip_rc);
}
/*
digital is a powerful display style, besides the normal discrete digital number display mode
"d"
it can also use continuous color to represent one line of spectrogram
"ds"
*/
static const ARGB argb_table[] = {
Color::Red, Color::Green, Color::Blue, Color::Brown,
Color::DarkGreen, Color::DarkBlue, Color::DeepSkyBlue, Color::DeepPink,
Color::BlueViolet, Color::DarkCyan, Color::DarkGoldenrod, Color::Purple,
Color::BurlyWood, Color::Cornsilk, Color::Orange, Color::Pink, Color::AliceBlue, Color::LawnGreen, Color::LightPink, Color::LightGreen, Color::LightBlue,
};
static const ARGB argb_table_digit[] = {
Color::Red, Color::Lime, Color::Blue, Color::Brown,
Color::DarkGreen, Color::DarkBlue, Color::DeepSkyBlue, Color::DeepPink,
Color::BlueViolet, Color::DarkCyan, Color::DarkGoldenrod, Color::Purple,
Color::BurlyWood, Color::Cornsilk, Color::Orange, Color::Pink, Color::AliceBlue, Color::LawnGreen, Color::LightPink, Color::LightGreen, Color::LightBlue,
};
void DSOClass::draw_digital(Graphics &graphics, data_info & di, int id)
{
WCHAR strinfo[128];
StringFormat stringformat;
stringformat.SetAlignment(StringAlignmentCenter);
stringformat.SetLineAlignment(StringAlignmentCenter);
Color c(argb_table_digit[0]);
SolidBrush BgBrush(c);
SolidBrush FgBrush(c);
int i0,i1;
di.data_id_range(time_x0, time_x1, i0,i1);
graphics.SetClip(cc.clip_rc);
float fCursorValue = di.data[i0].value;
for (unsigned int i = i0; i <= i1 + 1; i++)
{
if ((i == di.data.size()) ||
(i > 0 && di.data[i].value != di.data[i - 1].value) ||
(di.data[i].time > cc.x1))
{
int data = (int)(di.data[i0].value);
//draw data from i0 to(i-1)
int cid = data % (sizeof(argb_table_digit) / sizeof(argb_table_digit[0]));
BgBrush.SetColor(Color(argb_table_digit[cid]));
FgBrush.SetColor(Color((~argb_table_digit[cid]) | Color::AlphaMask));
float w;
if (i < di.data.size()) w = (di.data[i].time - di.data[i0].time) * cc.scale_x;
else w = (data_manager.x_max - di.data[i0].time) * cc.scale_x;
RectF rc(di.data[i0].time * cc.scale_x, (cc.y0 + id + 1) * cc.scale_y, w, -1.0f * cc.scale_y);
graphics.FillRectangle(&BgBrush, rc);
swprintf(strinfo, L"%d", data);
graphics.DrawString(strinfo, wcslen(strinfo), pfontDigital, rc, &stringformat, &FgBrush);
i0 = i;
}
if (i<di.data.size() && di.data[i].time <= time_cursor) fCursorValue = di.data[i].value;
}
graphics.ResetClip();
SolidBrush AnnotBgBrush(Color(100, 255,255,255));
RectF txtBox;
SolidBrush ValueBgBrush(Color::Gray);
SolidBrush ValueFgBrush(Color::White);
//draw data cursor
float time_cursor_lc = time_cursor;
bool bDrawCursor = (time_cursor_lc > cc.x0 && time_cursor_lc < cc.x1);
if (bDrawCursor)
{
Pen penCursor(Color::Gray, 1);
graphics.DrawLine(&penCursor, PointF(time_cursor_lc*cc.scale_x, cc.y0*cc.scale_y), PointF(time_cursor_lc*cc.scale_x, cc.y1*cc.scale_y));
swprintf(strinfo, L" %.2f", time_cursor_lc);
graphics.MeasureString(strinfo, wcslen(strinfo), pfontAnnotVal, RectF(), &stringformat, &txtBox);
txtBox.X = time_cursor_lc * cc.scale_x - txtBox.Width/2;
txtBox.Y = cc.y0 * cc.scale_y;
graphics.FillRectangle(&ValueBgBrush, txtBox);
graphics.DrawString(strinfo, wcslen(strinfo), pfontAnnotVal, txtBox, &stringformat, &ValueFgBrush);
}
#ifdef _UNICODE
_tcscpy(strinfo, di.name);
#else
mbstowcs(strinfo, di.name, 128);
#endif
graphics.MeasureString(strinfo, wcslen(strinfo), pfontAnnot, RectF(), &stringformat, &txtBox);
txtBox.X = cc.x1 * cc.scale_x - txtBox.Width;
txtBox.Y = (cc.y0 + id + 1) * cc.scale_y;
graphics.FillRectangle(&AnnotBgBrush, txtBox);
graphics.DrawString(strinfo, wcslen(strinfo), pfontAnnot, txtBox, &stringformat, &FgBrush);
if (bDrawCursor)
{
swprintf(strinfo, L" %d", (int)(fCursorValue));
graphics.MeasureString(strinfo, wcslen(strinfo), pfontAnnotVal, RectF(), &stringformat, &txtBox);
txtBox.X = cc.x1 * cc.scale_x;
txtBox.Y = (cc.y0 + id + 1) * cc.scale_y;
graphics.DrawString(strinfo, wcslen(strinfo), pfontAnnotVal, txtBox, &stringformat, &ValueBgBrush);
}
}
//the Matlab colormap "parula"
const unsigned char colormap_parula[256][3] = {
{ 53, 42, 135 }, { 53, 44, 138 }, { 54, 45, 141 }, { 54, 47, 144 }, { 54, 48, 147 }, { 54, 50, 150 }, { 54, 51, 153 }, { 54, 53, 156 },
{ 54, 54, 160 }, { 54, 56, 163 }, { 54, 57, 166 }, { 54, 59, 169 }, { 53, 61, 172 }, { 53, 62, 175 }, { 52, 64, 179 }, { 51, 65, 182 },
{ 50, 67, 185 }, { 49, 69, 188 }, { 48, 70, 191 }, { 46, 72, 195 }, { 44, 74, 198 }, { 42, 76, 201 }, { 39, 78, 204 }, { 36, 80, 208 },
{ 33, 82, 211 }, { 29, 84, 214 }, { 25, 87, 217 }, { 21, 89, 219 }, { 16, 91, 221 }, { 12, 93, 222 }, { 8, 95, 224 }, { 5, 97, 224 },
{ 3, 98, 225 }, { 2, 100, 225 }, { 2, 101, 225 }, { 1, 102, 225 }, { 1, 104, 225 }, { 2, 105, 225 }, { 2, 106, 225 }, { 3, 107, 225 },
{ 4, 108, 224 }, { 5, 109, 224 }, { 6, 110, 223 }, { 7, 111, 223 }, { 8, 112, 223 }, { 9, 113, 222 }, { 10, 114, 222 }, { 11, 115, 221 },
{ 12, 116, 221 }, { 13, 117, 220 }, { 14, 118, 220 }, { 15, 119, 219 }, { 16, 120, 218 }, { 16, 121, 218 }, { 17, 122, 217 }, { 18, 123, 217 },
{ 18, 124, 216 }, { 19, 125, 216 }, { 19, 126, 215 }, { 19, 127, 215 }, { 20, 128, 214 }, { 20, 129, 214 }, { 20, 130, 213 }, { 20, 131, 213 },
{ 20, 132, 212 }, { 20, 133, 212 }, { 20, 134, 211 }, { 20, 135, 211 }, { 19, 136, 211 }, { 19, 137, 211 }, { 18, 138, 210 }, { 18, 140, 210 },
{ 17, 141, 210 }, { 16, 142, 210 }, { 15, 143, 210 }, { 14, 145, 210 }, { 13, 146, 210 }, { 12, 147, 210 }, { 11, 149, 210 }, { 10, 150, 210 },
{ 9, 151, 209 }, { 9, 152, 209 }, { 8, 153, 209 }, { 8, 154, 208 }, { 7, 155, 208 }, { 7, 156, 207 }, { 7, 157, 207 }, { 6, 158, 206 }, { 6, 159, 206 },
{ 6, 160, 205 }, { 6, 161, 204 }, { 6, 162, 203 }, { 6, 163, 203 }, { 6, 164, 202 }, { 6, 164, 201 }, { 6, 165, 200 }, { 6, 166, 199 }, { 6, 167, 198 },
{ 6, 167, 197 }, { 6, 168, 196 }, { 6, 169, 195 }, { 7, 169, 194 }, { 7, 170, 193 }, { 8, 171, 192 }, { 9, 171, 191 }, { 10, 172, 190 }, { 11, 172, 189 },
{ 12, 173, 188 }, { 13, 174, 186 }, { 15, 174, 185 }, { 16, 175, 184 }, { 18, 175, 183 }, { 19, 176, 182 }, { 21, 176, 180 }, { 23, 177, 179 },
{ 24, 178, 178 }, { 26, 178, 177 }, { 28, 179, 175 }, { 30, 179, 174 }, { 32, 180, 173 }, { 34, 180, 171 }, { 36, 181, 170 }, { 38, 181, 169 }, { 40, 182, 167 },
{ 43, 182, 166 }, { 45, 183, 165 }, { 47, 183, 163 }, { 49, 184, 162 }, { 52, 184, 160 }, { 54, 185, 159 }, { 57, 185, 157 }, { 59, 186, 156 },
{ 62, 186, 154 }, { 64, 186, 153 }, { 67, 187, 151 }, { 70, 187, 150 }, { 73, 188, 148 }, { 75, 188, 147 }, { 78, 188, 145 }, { 81, 189, 144 }, { 84, 189, 142 },
{ 87, 189, 141 }, { 90, 189, 139 }, { 93, 190, 138 }, { 96, 190, 136 }, { 99, 190, 135 }, { 102, 190, 133 }, { 105, 190, 132 }, { 108, 191, 131 },
{ 111, 191, 129 }, { 113, 191, 128 }, { 116, 191, 127 }, { 119, 191, 126 }, { 122, 191, 124 }, { 125, 191, 123 }, { 128, 191, 122 }, { 130, 191, 121 },
{ 133, 191, 120 }, { 136, 191, 119 }, { 138, 191, 118 }, { 141, 191, 117 }, { 143, 191, 116 }, { 146, 191, 114 }, { 148, 191, 114 }, { 151, 191, 113 }, { 153, 191, 112 },
{ 156, 191, 111 }, { 158, 190, 110 }, { 160, 190, 109 }, { 163, 190, 108 }, { 165, 190, 107 }, { 167, 190, 106 }, { 170, 190, 105 }, { 172, 190, 104 }, { 174, 190, 103 },
{ 176, 189, 102 }, { 179, 189, 101 }, { 181, 189, 101 }, { 183, 189, 100 }, { 185, 189, 99 }, { 187, 189, 98 }, { 189, 188, 97 }, { 192, 188, 96 }, { 194, 188, 95 },
{ 196, 188, 95 }, { 198, 188, 94 }, { 200, 188, 93 }, { 202, 187, 92 }, { 204, 187, 91 }, { 206, 187, 90 }, { 208, 187, 89 }, { 210, 187, 89 }, { 212, 187, 88 },
{ 214, 186, 87 }, { 216, 186, 86 }, { 218, 186, 85 }, { 220, 186, 84 }, { 222, 186, 83 }, { 224, 186, 82 }, { 226, 185, 81 }, { 228, 185, 80 }, { 230, 185, 79 },
{ 232, 185, 78 }, { 234, 185, 77 }, { 236, 185, 76 }, { 238, 185, 75 }, { 240, 185, 74 }, { 242, 185, 73 }, { 244, 185, 72 }, { 246, 186, 70 }, { 248, 186, 69 },
{ 249, 187, 67 }, { 251, 188, 66 }, { 252, 188, 64 }, { 253, 189, 62 }, { 254, 191, 61 }, { 254, 192, 59 }, { 255, 193, 57 }, { 255, 194, 56 }, { 255, 196, 55 },
{ 255, 197, 53 }, { 254, 198, 52 }, { 254, 200, 51 }, { 254, 201, 50 }, { 253, 202, 49 }, { 253, 204, 48 }, { 252, 205, 46 }, { 252, 206, 45 },
{ 251, 207, 44 }, { 251, 209, 43 }, { 250, 210, 42 }, { 249, 211, 41 }, { 249, 213, 40 }, { 248, 214, 39 }, { 248, 215, 38 }, { 247, 217, 37 }, { 247, 218, 36 },
{ 246, 219, 35 }, { 246, 221, 34 }, { 245, 222, 33 }, { 245, 224, 32 }, { 245, 225, 31 }, { 245, 227, 30 }, { 244, 228, 29 }, { 244, 230, 28 },
{ 244, 232, 26 }, { 245, 233, 25 }, { 245, 235, 24 }, { 245, 237, 23 }, { 245, 239, 22 }, { 246, 241, 20 }, { 246, 243, 19 }, { 247, 245, 18 },
{ 248, 247, 17 }, { 248, 249, 15 }, { 249, 251, 14 },
};
void DSOClass::draw_spectra(Graphics &graphics, data_info & di, int id, int cnt, int lineHeight)
{
const TCHAR * pcfg;
WCHAR strinfo[128];
StringFormat stringformat;
stringformat.SetAlignment(StringAlignmentCenter);
stringformat.SetLineAlignment(StringAlignmentCenter);
Color c(argb_table_digit[0]);
SolidBrush BgBrush(c);
SolidBrush FgBrush(c);
int i0, i1;
float time_pixel = -1;
float cur_time_pix = 0;
float data_acc = 0;
int data_cnt = 0;
di.data_id_range(time_x0, time_x1, i0, i1);
graphics.SetClip(cc.clip_rc);
float fCursorValue = di.data[i0].value;
time_pixel = di.data[i0].time * cc.scale_x;
for (unsigned int i = i0; i <= i1; i++)
{
cur_time_pix = di.data[i].time * cc.scale_x;
data_acc += di.data[i].value;
data_cnt ++;
if ((i == di.data.size()) ||
(i > 0 && di.data[i].value != di.data[i - 1].value) ||
(di.data[i].time > cc.x1))
{
//also check if we advanced a time-pixel step, if not, accumulate the data for display
if ((int)time_pixel != (int)cur_time_pix)
{
data_acc -= di.data[i].value;
data_cnt--;
if (data_cnt > 0)
{
//draw previous time-pixel
int data = (int)(data_acc / data_cnt);
if (data > 255) data = 255;
if (data < 0) data = 0;
BgBrush.SetColor(Color(colormap_parula[data][0], colormap_parula[data][1], colormap_parula[data][2]));
float w = cur_time_pix - time_pixel;
RectF rc(time_pixel, (cc.y0 + id + lineHeight) * cc.scale_y, w + 0.5f, -lineHeight * cc.scale_y + 0.5f);
graphics.FillRectangle(&BgBrush, rc);
}
time_pixel = cur_time_pix;
data_acc = di.data[i].value;
data_cnt = 1;
}
}
if (i<di.data.size() && di.data[i].time <= time_cursor) fCursorValue = di.data[i].value;
}
//draw last time-pixel
if (data_cnt > 0){
int data = (int)(data_acc / data_cnt);
if (data > 255) data = 255;
if (data < 0) data = 0;
BgBrush.SetColor(Color(colormap_parula[data][0], colormap_parula[data][1], colormap_parula[data][2]));
float w = cur_time_pix - time_pixel;
RectF rc(time_pixel, (cc.y0 + id + 1) * cc.scale_y, w, -lineHeight * cc.scale_y);
graphics.FillRectangle(&BgBrush, rc);
}
graphics.ResetClip();
SolidBrush AnnotBgBrush(Color(100, 255, 255, 255));
RectF txtBox;
SolidBrush ValueBgBrush(Color::Gray);
SolidBrush ValueFgBrush(Color::White);
//draw data cursor
float time_cursor_lc = time_cursor;
bool bDrawCursor = (time_cursor_lc > cc.x0 && time_cursor_lc < cc.x1);
if (bDrawCursor)
{
Pen penCursor(Color::Gray, 1);
graphics.DrawLine(&penCursor, PointF(time_cursor_lc*cc.scale_x, cc.y0*cc.scale_y), PointF(time_cursor_lc*cc.scale_x, cc.y1*cc.scale_y));
swprintf(strinfo, L" %.2f", time_cursor_lc);
graphics.MeasureString(strinfo, wcslen(strinfo), pfontAnnotVal, RectF(), &stringformat, &txtBox);
txtBox.X = time_cursor_lc * cc.scale_x - txtBox.Width / 2;
txtBox.Y = cc.y0 * cc.scale_y;
graphics.FillRectangle(&ValueBgBrush, txtBox);
graphics.DrawString(strinfo, wcslen(strinfo), pfontAnnotVal, txtBox, &stringformat, &ValueFgBrush);
}
if (id == cnt-1)
{
#ifdef _UNICODE
_tcscpy(strinfo, di.name);
#else
mbstowcs(strinfo, di.name, 128);
#endif
graphics.MeasureString(strinfo, wcslen(strinfo), pfontAnnot, RectF(), &stringformat, &txtBox);
txtBox.X = cc.x1 * cc.scale_x - txtBox.Width;
txtBox.Y = (cc.y0 + id + 1) * cc.scale_y;
graphics.FillRectangle(&AnnotBgBrush, txtBox);
graphics.DrawString(strinfo, wcslen(strinfo), pfontAnnot, txtBox, &stringformat, &FgBrush);
}
if (0 && bDrawCursor)
{
swprintf(strinfo, L" %d", (int)(fCursorValue));
graphics.MeasureString(strinfo, wcslen(strinfo), pfontAnnotVal, RectF(), &stringformat, &txtBox);
txtBox.X = cc.x1 * cc.scale_x;
txtBox.Y = (cc.y0 + id + 1) * cc.scale_y;
graphics.DrawString(strinfo, wcslen(strinfo), pfontAnnotVal, txtBox, &stringformat, &ValueBgBrush);
}
}
void DSOClass::draw_curve(Graphics &graphics, data_info & di, int id)
{
REAL dashValues[4] = { 1, 1, 1, 1 };
const TCHAR * pcfg;
REAL tension = 0.5;
graphics.SetClip(cc.clip_rc);
Pen pen(Color::Green, 1);
pcfg = _tcsstr(di.style, _TEXT("c")); if (pcfg) pen.SetColor(Color(argb_table[pcfg[1] - '0']));
pcfg = _tcsstr(di.style, _TEXT("w")); if (pcfg) pen.SetWidth(pcfg[1] - '0');
pcfg = _tcsstr(di.style, _TEXT(".")); if (pcfg) pen.SetDashPattern(dashValues, 2);
pcfg = _tcsstr(di.style, _TEXT("t")); if (pcfg) tension = (pcfg[1] - '0') * 0.1f;
Color color(0x80, 0, 0);
pen.GetColor(&color);
float time_cursor_lc = -999990;
int i0,i1,cnt;
di.data_id_range(time_x0, time_x1, i0,i1);
cnt = i1-i0+1;
//we are doing a smart downsample for points in same integer time pixel location
//basic idea is to using maxmium of 4 points to represent a single integer time pixel location
//the first,the last, the min and the max, added in order of true time of each point
int pix_curX = -9999;
PointF sameX_minY;
PointF sameX_maxY;
int sameX_ptcnt = 0;
int true_cnt = 0;
PointF * curvePoints = new PointF[cnt];
float fCursorValue = 0;
for (unsigned int i = 0; i < cnt; i++)
{
PointF curPoint;
curPoint.X = di.data[i+i0].time * cc.scale_x;
curPoint.Y = di.data[i+i0].value * cc.scale_y;
int int_X = curPoint.X;
//check if we are within the same "time pixel",
//if so we can simplify the curve at this time pixel
//as passing 2 points: min & max in the order of their occurences
if(pix_curX == int_X)
{
if(sameX_minY.Y > curPoint.Y) sameX_minY = curPoint;
if(sameX_maxY.Y < curPoint.Y) sameX_maxY = curPoint;
sameX_ptcnt++;
}
else
{
//for previous integer time pixel location
if(sameX_ptcnt >= 2)
{
if(sameX_minY.X < sameX_maxY.X){
curvePoints[true_cnt++] = sameX_minY;
curvePoints[true_cnt++] = sameX_maxY;
}
else{
curvePoints[true_cnt++] = sameX_maxY;
curvePoints[true_cnt++] = sameX_minY;
}
}
else
if (sameX_ptcnt == 1)
curvePoints[true_cnt++] = sameX_minY;
//for the next time pixel
sameX_maxY = sameX_minY = curPoint;
sameX_ptcnt = 1;
pix_curX = int_X;
}
if (di.data[i+i0].time <= time_cursor){
time_cursor_lc = di.data[i+i0].time;
fCursorValue = di.data[i+i0].value;
}
}
//the last time pixel
if (sameX_ptcnt >= 2)
{
if (sameX_minY.X < sameX_maxY.X){
curvePoints[true_cnt++] = sameX_minY;
curvePoints[true_cnt++] = sameX_maxY;
}
else{
curvePoints[true_cnt++] = sameX_maxY;
curvePoints[true_cnt++] = sameX_minY;
}
}
else
if (sameX_ptcnt == 1)
curvePoints[true_cnt++] = sameX_minY;
//draw point marker only if the curve is not too much squeezed
graphics.DrawCurve(&pen, curvePoints, true_cnt, tension);
if (_tcsstr(di.style, _TEXT("p")) && true_cnt == cnt )
{
SolidBrush markerBrush(color);
int pw = pen.GetWidth();
for (int i = 0; i < true_cnt; i++)
graphics.FillRectangle(&markerBrush, Rect(curvePoints[i].X - pw, curvePoints[i].Y - pw, pw * 2 + 1, pw * 2 + 1));
}
delete[]curvePoints;
graphics.ResetClip();
WCHAR strinfo[128];
RectF txtBox;
StringFormat stringformat;
stringformat.SetAlignment(StringAlignmentCenter);
stringformat.SetLineAlignment(StringAlignmentCenter);
SolidBrush AnnotBrush(color);
SolidBrush ValueBgBrush(Color::Gray);
SolidBrush ValueFgBrush(Color::White);
SolidBrush AnnotBgBrush(Color(200, 255,255,255));
//draw data cursor
bool bDrawCursor = (time_cursor_lc > cc.x0 && time_cursor_lc < cc.x1);
if (bDrawCursor && id == 0)
{
Pen penCursor(Color::Gray, 1);
pen.SetDashPattern(dashValues, 2);
graphics.DrawLine(&penCursor, PointF(time_cursor_lc*cc.scale_x, cc.y0*cc.scale_y), PointF(time_cursor_lc*cc.scale_x, cc.y1*cc.scale_y));
swprintf(strinfo, L" %.2f", time_cursor_lc);
graphics.MeasureString(strinfo, wcslen(strinfo), pfontAnnotVal, RectF(), &stringformat, &txtBox);
txtBox.X = time_cursor_lc * cc.scale_x - txtBox.Width/2;
txtBox.Y = cc.y0 * cc.scale_y;
graphics.FillRectangle(&ValueBgBrush, txtBox);
graphics.DrawString(strinfo, wcslen(strinfo), pfontAnnotVal, txtBox, &stringformat, &ValueFgBrush);
}
#ifdef _UNICODE
_tcscpy(strinfo, di.name);
#else
mbstowcs(strinfo, di.name, 128);
#endif
graphics.MeasureString(strinfo, wcslen(strinfo), pfontAnnot, RectF(), &stringformat, &txtBox);
txtBox.X = cc.x1 * cc.scale_x - txtBox.Width;
txtBox.Y = cc.y1 * cc.scale_y + txtBox.Height * id;
graphics.FillRectangle(&AnnotBgBrush, txtBox);
graphics.DrawString(strinfo, wcslen(strinfo), pfontAnnot, txtBox, &stringformat, &AnnotBrush);
if (bDrawCursor)
{
stringformat.SetAlignment(StringAlignmentNear);
swprintf(strinfo, L" %.2f", fCursorValue);
graphics.MeasureString(strinfo, wcslen(strinfo), pfontAnnotVal, RectF(), &stringformat, &txtBox);
txtBox.X = cc.x1 * cc.scale_x;
txtBox.Y = cc.y1 * cc.scale_y + txtBox.Height * id;
//graphics.FillRectangle(&ValueBgBrush, txtBox);
graphics.DrawString(strinfo, wcslen(strinfo), pfontAnnotVal, txtBox, &stringformat, &ValueBgBrush);
}
}
void DSOClass::update(Graphics &graphics)
{
//graphics.SetSmoothingMode(SmoothingModeHighQuality);
graphics.SetSmoothingMode(SmoothingModeAntiAlias);
SolidBrush BgBrush(Color::White);
graphics.FillRectangle(&BgBrush, 0, 0, width, height);
::RECT client_rc;
::GetClientRect(this->hwnd, &client_rc);
#define DIGITAL_SIGNAL_HEIGHT 28
#define TOP_BOTTOM_MAGIN 10
int x0 = client_rc.left + hmargin;
int subrc_width = client_rc.right - client_rc.left - 2 * hmargin;
int y = TOP_BOTTOM_MAGIN;
EnterCriticalSection(&critical_sec_data);
log_x1 = time_x1;
for (int i = 0; i < data_manager.group.size(); i++)
{
group_info &g = data_manager.group[i];
float range = g.range_max - g.range_min;
float margin = (range == 0 ? 1.0f : range) * 0.05f;
int cur_height = plot_height;
if (g.bIsDigital) cur_height = DIGITAL_SIGNAL_HEIGHT * g.ids.size();
if (g.iSpectraHeight > 0) cur_height = g.iSpectraHeight * g.ids.size();
//setup coordinate and draw
if (g.bIsDigital || g.iSpectraHeight > 0)
{
set_coord(graphics, Rect(x0, y + vmargin, subrc_width, cur_height), time_x0, time_x1, 0, g.ids.size());//each signal take range of 1
}
else
{
//re-calculate data range within current time window
float rng_min = FLT_MAX;
float rng_max = -FLT_MAX;
for (unsigned int j = 0; j < g.ids.size(); j++)
{
data_info &di = data_manager.data[g.ids[j]];
int i0, i1, cnt;
di.data_id_range(time_x0, time_x1, i0, i1);
cnt = i1 - i0 + 1;
if (cnt > 0)
for (unsigned int k = i0; k < i1; k++)
{
if (rng_min > di.data[k].value) rng_min = di.data[k].value;
if (rng_max < di.data[k].value) rng_max = di.data[k].value;
}
}
float rng_size = (rng_max - rng_min);
float rng_margin = (rng_size < 0.0000001f ? 1.0f : rng_size * 0.05f);
set_coord(graphics, Rect(x0, y + vmargin, subrc_width, plot_height), time_x0, time_x1, rng_min - rng_margin, rng_max + rng_margin);
//set_coord(graphics, Rect(x0, y + vmargin, subrc_width, plot_height), time_x0, time_x1, g.range_min - margin, g.range_max + margin);
}
for (unsigned int j = 0; j < g.ids.size(); j++){
data_info &di = data_manager.data[g.ids[j]];
if (g.bIsDigital) draw_digital(graphics, di, j);
else if (g.iSpectraHeight > 0) draw_spectra(graphics, di, j, g.ids.size(), g.iSpectraHeight);
else draw_curve(graphics, di, j);
}
y += cur_height + 2 * vmargin;
}
LeaveCriticalSection(&critical_sec_data);
SCROLLINFO sinfo;
sinfo.cbSize = sizeof(SCROLLINFO);
sinfo.nPage = (client_rc.bottom - client_rc.top);
sinfo.nMin = 0;
sinfo.nMax = plot_totalHeight;
sinfo.fMask = SIF_RANGE | SIF_PAGE;
SetScrollInfo(hwnd, SB_VERT, &sinfo, TRUE);
}
//use cached Bitmap to increase performance
void DSOClass::display(HDC hdc, PAINTSTRUCT *pps)
{
Graphics graphics(hdc);
bool bcreat = false;
if (WaitForSingleObject(hDirty, 0) == WAIT_OBJECT_0)
{
if (b_in_display)
{
printf("display() is not supposed to re-entriable!\r\n");
}
b_in_display = true;
if (pmemBitmap) delete pmemBitmap;
if (pcachedBitmap) delete pcachedBitmap;
RECT rc;
::GetClientRect(hwnd, &rc);
width = rc.right - rc.left;
height = rc.bottom - rc.top;
//estimate canvas height
EnterCriticalSection(&critical_sec_data);
int y = TOP_BOTTOM_MAGIN;
for (int i = 0; i < data_manager.group.size(); i++)
{
group_info &g = data_manager.group[i];
if (g.bIsDigital) y += DIGITAL_SIGNAL_HEIGHT * g.ids.size() + 2 * vmargin;