forked from mhernando/Apolo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mathplot.cpp
3024 lines (2655 loc) · 92.6 KB
/
mathplot.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
/////////////////////////////////////////////////////////////////////////////
// Name: mathplot.cpp
// Purpose: Framework for plotting in wxWindows
// Original Author: David Schalig
// Maintainer: Davide Rondini
// Contributors: Jose Luis Blanco, Val Greene
// Created: 21/07/2003
// Last edit: 09/09/2007
// Copyright: (c) David Schalig, Davide Rondini
// Licence: wxWindows licence
/////////////////////////////////////////////////////////////////////////////
#ifdef __GNUG__
// #pragma implementation "plot.h"
#pragma implementation "mathplot.h"
#endif
// For compilers that support precompilation, includes "wx.h".
#include <wx/window.h>
//#include <wx/wxprec.h>
// Comment out for release operation:
// (Added by J.L.Blanco, Aug 2007)
// #define MATHPLOT_DO_LOGGING
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#ifndef WX_PRECOMP
#include "wx/object.h"
#include "wx/font.h"
#include "wx/colour.h"
#include "wx/settings.h"
#include "wx/sizer.h"
#include "wx/log.h"
#include "wx/intl.h"
#include "wx/dcclient.h"
#include "wx/cursor.h"
#endif
#include "mathplot.h"
#include <wx/bmpbuttn.h>
#include <wx/module.h>
#include <wx/msgdlg.h>
#include <wx/image.h>
#include <wx/tipwin.h>
#include <cmath>
#include <cstdio> // used only for debug
#include <ctime> // used for representation of x axes involving date
// #include "pixel.xpm"
// Memory leak debugging
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// Legend margins
#define mpLEGEND_MARGIN 5
#define mpLEGEND_LINEWIDTH 10
// Minimum axis label separation
#define mpMIN_X_AXIS_LABEL_SEPARATION 64
#define mpMIN_Y_AXIS_LABEL_SEPARATION 32
// Number of pixels to scroll when scrolling by a line
#define mpSCROLL_NUM_PIXELS_PER_LINE 10
// See doxygen comments.
double mpWindow::zoomIncrementalFactor = 1.5;
//-----------------------------------------------------------------------------
// mpLayer
//-----------------------------------------------------------------------------
IMPLEMENT_ABSTRACT_CLASS(mpLayer, wxObject)
mpLayer::mpLayer() : m_type(mpLAYER_UNDEF)
{
SetPen((wxPen&) *wxBLACK_PEN);
SetFont((wxFont&) *wxNORMAL_FONT);
m_continuous = FALSE; // Default
m_showName = TRUE; // Default
m_drawOutsideMargins = TRUE;
m_visible = true;
}
wxBitmap mpLayer::GetColourSquare(int side)
{
wxBitmap square(side, side, -1);
wxColour filler = m_pen.GetColour();
wxBrush brush(filler, wxSOLID);
wxMemoryDC dc;
dc.SelectObject(square);
dc.SetBackground(brush);
dc.Clear();
dc.SelectObject(wxNullBitmap);
return square;
}
//-----------------------------------------------------------------------------
// mpInfoLayer
//-----------------------------------------------------------------------------
IMPLEMENT_DYNAMIC_CLASS(mpInfoLayer, mpLayer)
mpInfoLayer::mpInfoLayer()
{
m_dim = wxRect(0,0,1,1);
m_brush = *wxTRANSPARENT_BRUSH;
m_reference.x = 0; m_reference.y = 0;
m_winX = 1; //parent->GetScrX();
m_winY = 1; //parent->GetScrY();
m_type = mpLAYER_INFO;
}
mpInfoLayer::mpInfoLayer(wxRect rect, const wxBrush* brush) : m_dim(rect)
{
m_brush = *brush;
m_reference.x = rect.x;
m_reference.y = rect.y;
m_winX = 1; //parent->GetScrX();
m_winY = 1; //parent->GetScrY();
m_type = mpLAYER_INFO;
}
mpInfoLayer::~mpInfoLayer()
{
}
void mpInfoLayer::UpdateInfo(mpWindow& w, wxEvent& event)
{
}
bool mpInfoLayer::Inside(wxPoint& point)
{
return m_dim.Contains(point);
}
void mpInfoLayer::Move(wxPoint delta)
{
m_dim.SetX(m_reference.x + delta.x);
m_dim.SetY(m_reference.y + delta.y);
}
void mpInfoLayer::UpdateReference()
{
m_reference.x = m_dim.x;
m_reference.y = m_dim.y;
}
void mpInfoLayer::Plot(wxDC & dc, mpWindow & w)
{
if (m_visible) {
// Adjust relative position inside the window
int scrx = w.GetScrX();
int scry = w.GetScrY();
// Avoid dividing by 0
if(scrx == 0) scrx=1;
if(scry == 0) scry=1;
if ((m_winX != scrx) || (m_winY != scry)) {
#ifdef MATHPLOT_DO_LOGGING
// wxLogMessage(_("mpInfoLayer::Plot() screen size has changed from %d x %d to %d x %d"), m_winX, m_winY, scrx, scry);
#endif
if (m_winX != 1) m_dim.x = (int) floor((double)(m_dim.x*scrx/m_winX));
if (m_winY != 1) {
m_dim.y = (int) floor((double)(m_dim.y*scry/m_winY));
UpdateReference();
}
// Finally update window size
m_winX = scrx;
m_winY = scry;
}
dc.SetPen(m_pen);
// wxImage image0(wxT("pixel.png"), wxBITMAP_TYPE_PNG);
// wxBitmap image1(image0);
// wxBrush semiWhite(image1);
dc.SetBrush(m_brush);
dc.DrawRectangle(m_dim.x, m_dim.y, m_dim.width, m_dim.height);
}
}
wxPoint mpInfoLayer::GetPosition()
{
return m_dim.GetPosition();
}
wxSize mpInfoLayer::GetSize()
{
return m_dim.GetSize();
}
mpInfoCoords::mpInfoCoords() : mpInfoLayer()
{
}
mpInfoCoords::mpInfoCoords(wxRect rect, const wxBrush* brush) : mpInfoLayer(rect, brush)
{
}
mpInfoCoords::~mpInfoCoords()
{
}
void mpInfoCoords::UpdateInfo(mpWindow& w, wxEvent& event)
{
if (event.GetEventType() == wxEVT_MOTION) {
int mouseX = ((wxMouseEvent&)event).GetX();
int mouseY = ((wxMouseEvent&)event).GetY();
/* It seems that Windows port of wxWidgets don't support multi-line test to be drawn in a wxDC.
wxGTK instead works perfectly with it.
Info on wxForum: http://wxforum.shadonet.com/viewtopic.php?t=3451&highlight=drawtext+eol */
#ifdef _WINDOWS
m_content.Printf(wxT("x = %f y = %f"), w.p2x(mouseX), w.p2y(mouseY));
#else
m_content.Printf(wxT("x = %f\ny = %f"), w.p2x(mouseX), w.p2y(mouseY));
#endif
}
}
void mpInfoCoords::Plot(wxDC & dc, mpWindow & w)
{
if (m_visible) {
// Adjust relative position inside the window
int scrx = w.GetScrX();
int scry = w.GetScrY();
if ((m_winX != scrx) || (m_winY != scry)) {
#ifdef MATHPLOT_DO_LOGGING
// wxLogMessage(_("mpInfoLayer::Plot() screen size has changed from %d x %d to %d x %d"), m_winX, m_winY, scrx, scry);
#endif
if (m_winX != 1) m_dim.x = (int) floor((double)(m_dim.x*scrx/m_winX));
if (m_winY != 1) {
m_dim.y = (int) floor((double)(m_dim.y*scry/m_winY));
UpdateReference();
}
// Finally update window size
m_winX = scrx;
m_winY = scry;
}
dc.SetPen(m_pen);
// wxImage image0(wxT("pixel.png"), wxBITMAP_TYPE_PNG);
// wxBitmap image1(image0);
// wxBrush semiWhite(image1);
dc.SetBrush(m_brush);
dc.SetFont(m_font);
int textX, textY;
dc.GetTextExtent(m_content, &textX, &textY);
if (m_dim.width < textX + 10) m_dim.width = textX + 10;
if (m_dim.height < textY + 10) m_dim.height = textY + 10;
dc.DrawRectangle(m_dim.x, m_dim.y, m_dim.width, m_dim.height);
dc.DrawText(m_content, m_dim.x + 5, m_dim.y + 5);
}
}
mpInfoLegend::mpInfoLegend() : mpInfoLayer()
{
}
mpInfoLegend::mpInfoLegend(wxRect rect, const wxBrush* brush) : mpInfoLayer(rect, brush)
{
}
mpInfoLegend::~mpInfoLegend()
{
}
void mpInfoLegend::UpdateInfo(mpWindow& w, wxEvent& event)
{
}
void mpInfoLegend::Plot(wxDC & dc, mpWindow & w)
{
if (m_visible) {
// Adjust relative position inside the window
int scrx = w.GetScrX();
int scry = w.GetScrY();
if ((m_winX != scrx) || (m_winY != scry)) {
#ifdef MATHPLOT_DO_LOGGING
// wxLogMessage(_("mpInfoLayer::Plot() screen size has changed from %d x %d to %d x %d"), m_winX, m_winY, scrx, scry);
#endif
if (m_winX != 1) m_dim.x = (int) floor((double)(m_dim.x*scrx/m_winX));
if (m_winY != 1) {
m_dim.y = (int) floor((double)(m_dim.y*scry/m_winY));
UpdateReference();
}
// Finally update window size
m_winX = scrx;
m_winY = scry;
}
// wxImage image0(wxT("pixel.png"), wxBITMAP_TYPE_PNG);
// wxBitmap image1(image0);
// wxBrush semiWhite(image1);
dc.SetBrush(m_brush);
dc.SetFont(m_font);
const int baseWidth = (mpLEGEND_MARGIN*2 + mpLEGEND_LINEWIDTH);
int textX = baseWidth, textY = mpLEGEND_MARGIN;
int plotCount = 0;
int posY = 0;
int tmpX = 0, tmpY = 0;
mpLayer* ly = NULL;
wxPen lpen;
wxString label;
for (unsigned int p = 0; p < w.CountAllLayers(); p++) {
ly = w.GetLayer(p);
if ((ly->GetLayerType() == mpLAYER_PLOT) && (ly->IsVisible())) {
label = ly->GetName();
dc.GetTextExtent(label, &tmpX, &tmpY);
textX = (textX > (tmpX + baseWidth)) ? textX : (tmpX + baseWidth + mpLEGEND_MARGIN);
textY += (tmpY);
#ifdef MATHPLOT_DO_LOGGING
// wxLogMessage(_("mpInfoLegend::Plot() Adding layer %d: %s"), p, label.c_str());
#endif
}
}
dc.SetPen(m_pen);
dc.SetBrush(m_brush);
m_dim.width = textX;
if (textY != mpLEGEND_MARGIN) { // Don't draw any thing if there are no visible layers
textY += mpLEGEND_MARGIN;
m_dim.height = textY;
dc.DrawRectangle(m_dim.x, m_dim.y, m_dim.width, m_dim.height);
for (unsigned int p2 = 0; p2 < w.CountAllLayers(); p2++) {
ly = w.GetLayer(p2);
if ((ly->GetLayerType() == mpLAYER_PLOT) && (ly->IsVisible())) {
label = ly->GetName();
lpen = ly->GetPen();
dc.GetTextExtent(label, &tmpX, &tmpY);
dc.SetPen(lpen);
//textX = (textX > (tmpX + baseWidth)) ? textX : (tmpX + baseWidth);
//textY += (tmpY + mpLEGEND_MARGIN);
posY = m_dim.y + mpLEGEND_MARGIN + plotCount*tmpY + (tmpY>>1);
dc.DrawLine(m_dim.x + mpLEGEND_MARGIN, // X start coord
posY, // Y start coord
m_dim.x + mpLEGEND_LINEWIDTH + mpLEGEND_MARGIN, // X end coord
posY);
//dc.DrawRectangle(m_dim.x + 5, m_dim.y + 5 + plotCount*tmpY, 5, 5);
dc.DrawText(label, m_dim.x + baseWidth, m_dim.y + mpLEGEND_MARGIN + plotCount*tmpY);
plotCount++;
}
}
}
}
}
//-----------------------------------------------------------------------------
// mpLayer implementations - functions
//-----------------------------------------------------------------------------
IMPLEMENT_ABSTRACT_CLASS(mpFX, mpLayer)
mpFX::mpFX(wxString name, int flags)
{
SetName(name);
m_flags = flags;
m_type = mpLAYER_PLOT;
}
void mpFX::Plot(wxDC & dc, mpWindow & w)
{
if (m_visible) {
dc.SetPen( m_pen);
wxCoord startPx = m_drawOutsideMargins ? 0 : w.GetMarginLeft();
wxCoord endPx = m_drawOutsideMargins ? w.GetScrX() : w.GetScrX() - w.GetMarginRight();
wxCoord minYpx = m_drawOutsideMargins ? 0 : w.GetMarginTop();
wxCoord maxYpx = m_drawOutsideMargins ? w.GetScrY() : w.GetScrY() - w.GetMarginBottom();
wxCoord iy = 0;
if (m_pen.GetWidth() <= 1)
{
for (wxCoord i = startPx; i < endPx; ++i)
{
iy = w.y2p( GetY(w.p2x(i)));
// Draw the point only if you can draw outside margins or if the point is inside margins
if (m_drawOutsideMargins || ((iy >= minYpx) && (iy <= maxYpx)))
dc.DrawPoint(i, iy );// (wxCoord) ((w.GetPosY() - GetY( (double)i / w.GetScaleX() + w.GetPosX()) ) * w.GetScaleY()));
}
}
else
{
for (wxCoord i = startPx; i < endPx; ++i)
{
iy = w.y2p( GetY(w.p2x(i)));
// Draw the point only if you can draw outside margins or if the point is inside margins
if (m_drawOutsideMargins || ((iy >= minYpx) && (iy <= maxYpx)))
dc.DrawLine( i, iy, i, iy);
// wxCoord c = w.y2p( GetY(w.p2x(i)) ); //(wxCoord) ((w.GetPosY() - GetY( (double)i / w.GetScaleX() + w.GetPosX()) ) * w.GetScaleY());
}
}
if (!m_name.IsEmpty() && m_showName)
{
dc.SetFont(m_font);
wxCoord tx, ty;
dc.GetTextExtent(m_name, &tx, &ty);
/*if ((m_flags & mpALIGNMASK) == mpALIGN_RIGHT)
tx = (w.GetScrX()>>1) - tx - 8;
else if ((m_flags & mpALIGNMASK) == mpALIGN_CENTER)
tx = -tx/2;
else
tx = -(w.GetScrX()>>1) + 8;
*/
if ((m_flags & mpALIGNMASK) == mpALIGN_RIGHT)
tx = (w.GetScrX() - tx) - w.GetMarginRight() - 8;
else if ((m_flags & mpALIGNMASK) == mpALIGN_CENTER)
tx = ((w.GetScrX() - w.GetMarginRight() - w.GetMarginLeft() - tx) / 2) + w.GetMarginLeft();
else
tx = w.GetMarginLeft() + 8;
dc.DrawText( m_name, tx, w.y2p(GetY(w.p2x(tx))) ); // (wxCoord) ((w.GetPosY() - GetY( (double)tx / w.GetScaleX() + w.GetPosX())) * w.GetScaleY()) );
}
}
}
IMPLEMENT_ABSTRACT_CLASS(mpFY, mpLayer)
mpFY::mpFY(wxString name, int flags)
{
SetName(name);
m_flags = flags;
m_type = mpLAYER_PLOT;
}
void mpFY::Plot(wxDC & dc, mpWindow & w)
{
if (m_visible) {
dc.SetPen( m_pen);
wxCoord i, ix;
wxCoord startPx = m_drawOutsideMargins ? 0 : w.GetMarginLeft();
wxCoord endPx = m_drawOutsideMargins ? w.GetScrX() : w.GetScrX() - w.GetMarginRight();
wxCoord minYpx = m_drawOutsideMargins ? 0 : w.GetMarginTop();
wxCoord maxYpx = m_drawOutsideMargins ? w.GetScrY() : w.GetScrY() - w.GetMarginBottom();
if (m_pen.GetWidth() <= 1)
{
for (i = minYpx; i < maxYpx; ++i)
{
ix = w.x2p(GetX(w.p2y(i)));
if (m_drawOutsideMargins || ((ix >= startPx) && (ix <= endPx)))
dc.DrawPoint(ix, i);
}
}
else
{
for (i=0;i< w.GetScrY(); ++i)
{
ix = w.x2p(GetX(w.p2y(i)));
if (m_drawOutsideMargins || ((ix >= startPx) && (ix <= endPx)))
dc.DrawLine(ix, i, ix, i);
// wxCoord c = w.x2p(GetX(w.p2y(i))); //(wxCoord) ((GetX( (double)i / w.GetScaleY() + w.GetPosY()) - w.GetPosX()) * w.GetScaleX());
// dc.DrawLine(c, i, c, i);
}
}
if (!m_name.IsEmpty() && m_showName)
{
dc.SetFont(m_font);
wxCoord tx, ty;
dc.GetTextExtent(m_name, &tx, &ty);
if ((m_flags & mpALIGNMASK) == mpALIGN_TOP)
ty = w.GetMarginTop() + 8;
else if ((m_flags & mpALIGNMASK) == mpALIGN_CENTER)
ty = ((w.GetScrY() - w.GetMarginTop() - w.GetMarginBottom() - ty) / 2) + w.GetMarginTop();
else
ty = w.GetScrY() - 8 - ty - w.GetMarginBottom();
dc.DrawText( m_name, w.x2p(GetX(w.p2y(ty))), ty ); // (wxCoord) ((GetX( (double)i / w.GetScaleY() + w.GetPosY()) - w.GetPosX()) * w.GetScaleX()), -ty);
}
}
}
IMPLEMENT_ABSTRACT_CLASS(mpFXY, mpLayer)
mpFXY::mpFXY(wxString name, int flags)
{
SetName(name);
m_flags = flags;
m_type = mpLAYER_PLOT;
}
void mpFXY::UpdateViewBoundary(wxCoord xnew, wxCoord ynew)
{
// Keep track of how many points have been drawn and the bouding box
maxDrawX = (xnew > maxDrawX) ? xnew : maxDrawX;
minDrawX = (xnew < minDrawX) ? xnew : minDrawX;
maxDrawY = (maxDrawY > ynew) ? maxDrawY : ynew;
minDrawY = (minDrawY < ynew) ? minDrawY : ynew;
//drawnPoints++;
}
void mpFXY::Plot(wxDC & dc, mpWindow & w)
{
if (m_visible) {
dc.SetPen( m_pen);
double x, y;
// Do this to reset the counters to evaluate bounding box for label positioning
Rewind(); GetNextXY(x, y);
maxDrawX = x; minDrawX = x; maxDrawY = y; minDrawY = y;
//drawnPoints = 0;
Rewind();
wxCoord startPx = m_drawOutsideMargins ? 0 : w.GetMarginLeft();
wxCoord endPx = m_drawOutsideMargins ? w.GetScrX() : w.GetScrX() - w.GetMarginRight();
wxCoord minYpx = m_drawOutsideMargins ? 0 : w.GetMarginTop();
wxCoord maxYpx = m_drawOutsideMargins ? w.GetScrY() : w.GetScrY() - w.GetMarginBottom();
wxCoord ix = 0, iy = 0;
if (!m_continuous)
{
// for some reason DrawPoint does not use the current pen,
// so we use DrawLine for fat pens
if (m_pen.GetWidth() <= 1)
{
while (GetNextXY(x, y))
{
ix = w.x2p(x);
iy = w.y2p(y);
if (m_drawOutsideMargins || ((ix >= startPx) && (ix <= endPx) && (iy >= minYpx) && (iy <= maxYpx))) {
dc.DrawPoint(ix, iy);
UpdateViewBoundary(ix, iy);
};
}
}
else
{
while (GetNextXY(x, y))
{
ix = w.x2p(x);
iy = w.y2p(y);
if (m_drawOutsideMargins || ((ix >= startPx) && (ix <= endPx) && (iy >= minYpx) && (iy <= maxYpx))) {
dc.DrawLine(ix, iy, ix, iy);
UpdateViewBoundary(ix, iy);
}
// dc.DrawLine(cx, cy, cx, cy);
}
}
}
else
{
// Old code
wxCoord x0=0,c0=0;
bool first = TRUE;
while (GetNextXY(x, y))
{
wxCoord x1 = w.x2p(x); // (wxCoord) ((x - w.GetPosX()) * w.GetScaleX());
wxCoord c1 = w.y2p(y); // (wxCoord) ((w.GetPosY() - y) * w.GetScaleY());
if (first)
{
first=FALSE;
x0=x1;c0=c1;
}
bool outUp, outDown;
if((x1 >= startPx)&&(x0 <= endPx)) {
outDown = (c0 > maxYpx) && (c1 > maxYpx);
outUp = (c0 < minYpx) && (c1 < minYpx);
if (!outUp && !outDown) {
if (c1 != c0) {
if (c0 < minYpx) {
x0 = (int)(((float)(minYpx - c0))/((float)(c1 - c0))*(x1-x0)) + x0;
c0 = minYpx;
}
if (c0 > maxYpx) {
x0 = (int)(((float)(maxYpx - c0))/((float)(c1 - c0))*(x1-x0)) + x0;
//wxLogDebug(wxT("old x0 = %d, new x0 = %d"), x0, newX0);
//x0 = newX0;
c0 = maxYpx;
}
if (c1 < minYpx) {
x1 = (int)(((float)(minYpx - c0))/((float)(c1 - c0))*(x1-x0)) + x0;
c1 = minYpx;
}
if (c1 > maxYpx) {
x1 = (int)(((float)(maxYpx - c0))/((float)(c1 - c0))*(x1-x0)) + x0;
//wxLogDebug(wxT("old x0 = %d, old x1 = %d, new x1 = %d, c0 = %d, c1 = %d, maxYpx = %d"), x0, x1, newX1, c0, c1, maxYpx);
//x1 = newX1;
c1 = maxYpx;
}
}
if (x1 != x0) {
if (x0 < startPx) {
c0 = (int)(((float)(startPx - x0))/((float)(x1 -x0))*(c1 -c0)) + c0;
x0 = startPx;
}
if (x1 > endPx) {
c1 = (int)(((float)(endPx - x0))/((float)(x1 -x0))*(c1 -c0)) + c0;
x1 = endPx;
}
}
dc.DrawLine(x0, c0, x1, c1);
UpdateViewBoundary(x1, c1);
}
}
x0=x1; c0=c1;
}
}
if (!m_name.IsEmpty() && m_showName)
{
dc.SetFont(m_font);
wxCoord tx, ty;
dc.GetTextExtent(m_name, &tx, &ty);
// xxx implement else ... if (!HasBBox())
{
// const int sx = w.GetScrX();
// const int sy = w.GetScrY();
if ((m_flags & mpALIGNMASK) == mpALIGN_NW)
{
tx = minDrawX + 8;
ty = maxDrawY + 8;
}
else if ((m_flags & mpALIGNMASK) == mpALIGN_NE)
{
tx = maxDrawX - tx - 8;
ty = maxDrawY + 8;
}
else if ((m_flags & mpALIGNMASK) == mpALIGN_SE)
{
tx = maxDrawX - tx - 8;
ty = minDrawY - ty - 8;
}
else
{ // mpALIGN_SW
tx = minDrawX + 8;
ty = minDrawY - ty - 8;
}
}
dc.DrawText( m_name, tx, ty);
}
}
}
//-----------------------------------------------------------------------------
// mpProfile implementation
//-----------------------------------------------------------------------------
IMPLEMENT_ABSTRACT_CLASS(mpProfile, mpLayer)
mpProfile::mpProfile(wxString name, int flags)
{
SetName(name);
m_flags = flags;
m_type = mpLAYER_PLOT;
}
void mpProfile::Plot(wxDC & dc, mpWindow & w)
{
if (m_visible) {
dc.SetPen( m_pen);
wxCoord startPx = m_drawOutsideMargins ? 0 : w.GetMarginLeft();
wxCoord endPx = m_drawOutsideMargins ? w.GetScrX() : w.GetScrX() - w.GetMarginRight();
wxCoord minYpx = m_drawOutsideMargins ? 0 : w.GetMarginTop();
wxCoord maxYpx = m_drawOutsideMargins ? w.GetScrY() : w.GetScrY() - w.GetMarginBottom();
// Plot profile linking subsequent point of the profile, instead of mpFY, which plots simple points.
for (wxCoord i = startPx; i < endPx; ++i) {
wxCoord c0 = w.y2p( GetY(w.p2x(i)) ); // (wxCoord) ((w.GetYpos() - GetY( (double)i / w.GetXscl() + w.GetXpos()) ) * w.GetYscl());
wxCoord c1 = w.y2p( GetY(w.p2x(i+1)) );//(wxCoord) ((w.GetYpos() - GetY( (double)(i+1) / w.GetXscl() + (w.GetXpos() ) ) ) * w.GetYscl());
// c0 = (c0 <= maxYpx) ? ((c0 >= minYpx) ? c0 : minYpx) : maxYpx;
// c1 = (c1 <= maxYpx) ? ((c1 >= minYpx) ? c1 : minYpx) : maxYpx;
if (!m_drawOutsideMargins) {
c0 = (c0 <= maxYpx) ? ((c0 >= minYpx) ? c0 : minYpx) : maxYpx;
c1 = (c1 <= maxYpx) ? ((c1 >= minYpx) ? c1 : minYpx) : maxYpx;
}
dc.DrawLine(i, c0, i+1, c1);
};
if (!m_name.IsEmpty()) {
dc.SetFont(m_font);
wxCoord tx, ty;
dc.GetTextExtent(m_name, &tx, &ty);
if ((m_flags & mpALIGNMASK) == mpALIGN_RIGHT)
tx = (w.GetScrX() - tx) - w.GetMarginRight() - 8;
else if ((m_flags & mpALIGNMASK) == mpALIGN_CENTER)
tx = ((w.GetScrX() - w.GetMarginRight() - w.GetMarginLeft() - tx) / 2) + w.GetMarginLeft();
else
tx = w.GetMarginLeft() + 8;
dc.DrawText( m_name, tx, w.y2p( GetY( w.p2x(tx) ) ) );//(wxCoord) ((w.GetPosY() - GetY( (double)tx / w.GetScaleX() + w.GetPosX())) * w.GetScaleY()) );
}
}
}
//-----------------------------------------------------------------------------
// mpLayer implementations - furniture (scales, ...)
//-----------------------------------------------------------------------------
#define mpLN10 2.3025850929940456840179914546844
IMPLEMENT_DYNAMIC_CLASS(mpScaleX, mpLayer)
mpScaleX::mpScaleX(wxString name, int flags, bool ticks, unsigned int type)
{
SetName(name);
SetFont( (wxFont&) *wxSMALL_FONT);
SetPen( (wxPen&) *wxGREY_PEN);
m_flags = flags;
m_ticks = ticks;
m_labelType = type;
m_type = mpLAYER_AXIS;
m_labelFormat = wxT("");
}
void mpScaleX::Plot(wxDC & dc, mpWindow & w)
{
if (m_visible) {
dc.SetPen( m_pen);
dc.SetFont( m_font);
int orgy=0;
const int extend = w.GetScrX(); // /2;
if (m_flags == mpALIGN_CENTER)
orgy = w.y2p(0); //(int)(w.GetPosY() * w.GetScaleY());
if (m_flags == mpALIGN_TOP) {
if (m_drawOutsideMargins)
orgy = X_BORDER_SEPARATION;
else
orgy = w.GetMarginTop();
}
if (m_flags == mpALIGN_BOTTOM) {
if (m_drawOutsideMargins)
orgy = X_BORDER_SEPARATION;
else
orgy = w.GetScrY() - w.GetMarginBottom();
}
if (m_flags == mpALIGN_BORDER_BOTTOM )
orgy = w.GetScrY() - 1;//dc.LogicalToDeviceY(0) - 1;
if (m_flags == mpALIGN_BORDER_TOP )
orgy = 1;//-dc.LogicalToDeviceY(0);
dc.DrawLine( 0, orgy, w.GetScrX(), orgy);
// To cut the axis line when draw outside margin is false, use this code
/*if (m_drawOutsideMargins == true)
dc.DrawLine( 0, orgy, w.GetScrX(), orgy);
else
dc.DrawLine( w.GetMarginLeft(), orgy, w.GetScrX() - w.GetMarginRight(), orgy); */
const double dig = floor( log( 128.0 / w.GetScaleX() ) / mpLN10 );
const double step = exp( mpLN10 * dig);
const double end = w.GetPosX() + (double)extend / w.GetScaleX();
wxCoord tx, ty;
wxString s;
wxString fmt;
int tmp = (int)dig;
if (m_labelType == mpX_NORMAL) {
if (!m_labelFormat.IsEmpty()) {
fmt = m_labelFormat;
} else {
if (tmp>=1) {
fmt = wxT("%.f");
} else {
tmp=8-tmp;
fmt.Printf(wxT("%%.%df"), tmp >= -1 ? 2 : -tmp);
}
}
} else {
// Date and/or time axis representation
if (m_labelType == mpX_DATETIME) {
fmt = (wxT("%04.0f-%02.0f-%02.0fT%02.0f:%02.0f:%02.0f"));
} else if (m_labelType == mpX_DATE) {
fmt = (wxT("%04.0f-%02.0f-%02.0f"));
} else if ((m_labelType == mpX_TIME) && (end/60 < 2)) {
fmt = (wxT("%02.0f:%02.3f"));
} else {
fmt = (wxT("%02.0f:%02.0f:%02.0f"));
}
}
//double n = floor( (w.GetPosX() - (double)extend / w.GetScaleX()) / step ) * step ;
double n0 = floor( (w.GetPosX() /* - (double)(extend - w.GetMarginLeft() - w.GetMarginRight())/ w.GetScaleX() */) / step ) * step ;
double n = 0;
#ifdef MATHPLOT_DO_LOGGING
wxLogMessage(wxT("mpScaleX::Plot: dig: %f , step: %f, end: %f, n: %f"), dig, step, end, n0);
#endif
wxCoord startPx = m_drawOutsideMargins ? 0 : w.GetMarginLeft();
wxCoord endPx = m_drawOutsideMargins ? w.GetScrX() : w.GetScrX() - w.GetMarginRight();
wxCoord minYpx = m_drawOutsideMargins ? 0 : w.GetMarginTop();
wxCoord maxYpx = m_drawOutsideMargins ? w.GetScrY() : w.GetScrY() - w.GetMarginBottom();
tmp=-65535;
int labelH = 0; // Control labels heigth to decide where to put axis name (below labels or on top of axis)
int maxExtent = 0;
for (n = n0; n < end; n += step) {
const int p = (int)((n - w.GetPosX()) * w.GetScaleX());
#ifdef MATHPLOT_DO_LOGGING
wxLogMessage(wxT("mpScaleX::Plot: n: %f -> p = %d"), n, p);
#endif
if ((p >= startPx) && (p <= endPx)) {
if (m_ticks) { // draw axis ticks
if (m_flags == mpALIGN_BORDER_BOTTOM)
dc.DrawLine( p, orgy, p, orgy-4);
else
dc.DrawLine( p, orgy, p, orgy+4);
} else { // draw grid dotted lines
m_pen.SetStyle(wxDOT);
dc.SetPen(m_pen);
if ((m_flags == mpALIGN_BOTTOM) && !m_drawOutsideMargins) {
dc.DrawLine( p, orgy+4, p, minYpx );
} else {
if ((m_flags == mpALIGN_TOP) && !m_drawOutsideMargins) {
dc.DrawLine( p, orgy-4, p, maxYpx );
} else {
dc.DrawLine( p, 0/*-w.GetScrY()*/, p, w.GetScrY() );
}
}
m_pen.SetStyle(wxSOLID);
dc.SetPen(m_pen);
}
// Write ticks labels in s string
if (m_labelType == mpX_NORMAL)
s.Printf(fmt, n);
else if (m_labelType == mpX_DATETIME) {
time_t when = (time_t)n;
struct tm tm = *localtime(&when);
s.Printf(fmt, (double)tm.tm_year+1900, (double)tm.tm_mon+1, (double)tm.tm_mday, (double)tm.tm_hour, (double)tm.tm_min, (double)tm.tm_sec);
} else if (m_labelType == mpX_DATE) {
time_t when = (time_t)n;
struct tm tm = *localtime(&when);
s.Printf(fmt, (double)tm.tm_year+1900, (double)tm.tm_mon+1, (double)tm.tm_mday);
} else if ((m_labelType == mpX_TIME) || (m_labelType == mpX_HOURS)) {
double modulus = fabs(n);
double sign = n/modulus;
double hh = floor(modulus/3600);
double mm = floor((modulus - hh*3600)/60);
double ss = modulus - hh*3600 - mm*60;
#ifdef MATHPLOT_DO_LOGGING
wxLogMessage(wxT("%02.0f Hours, %02.0f minutes, %02.0f seconds"), sign*hh, mm, ss);
#endif // MATHPLOT_DO_LOGGING
if (fmt.Len() == 20) // Format with hours has 11 chars
s.Printf(fmt, sign*hh, mm, floor(ss));
else
s.Printf(fmt, sign*mm, ss);
}
dc.GetTextExtent(s, &tx, &ty);
labelH = (labelH <= ty) ? ty : labelH;
/* if ((p-tx/2-tmp) > 64) { // Problem about non-regular axis labels
if ((m_flags == mpALIGN_BORDER_BOTTOM) || (m_flags == mpALIGN_TOP)) {
dc.DrawText( s, p-tx/2, orgy-4-ty);
} else {
dc.DrawText( s, p-tx/2, orgy+4);
}
tmp=p+tx/2;
}
*/
maxExtent = (tx > maxExtent) ? tx : maxExtent; // Keep in mind max label width
}
}
// Actually draw labels, taking care of not overlapping them, and distributing them regularly
double labelStep = ceil((maxExtent + mpMIN_X_AXIS_LABEL_SEPARATION)/(w.GetScaleX()*step))*step;
for (n = n0; n < end; n += labelStep) {
const int p = (int)((n - w.GetPosX()) * w.GetScaleX());
#ifdef MATHPLOT_DO_LOGGING
wxLogMessage(wxT("mpScaleX::Plot: n_label = %f -> p_label = %d"), n, p);
#endif
if ((p >= startPx) && (p <= endPx)) {
// Write ticks labels in s string
if (m_labelType == mpX_NORMAL)
s.Printf(fmt, n);
else if (m_labelType == mpX_DATETIME) {
time_t when = (time_t)n;
struct tm tm = *localtime(&when);
s.Printf(fmt, (double)tm.tm_year+1900, (double)tm.tm_mon+1, (double)tm.tm_mday, (double)tm.tm_hour, (double)tm.tm_min, (double)tm.tm_sec);
} else if (m_labelType == mpX_DATE) {
time_t when = (time_t)n;
struct tm tm = *localtime(&when);
s.Printf(fmt, (double)tm.tm_year+1900, (double)tm.tm_mon+1, (double)tm.tm_mday);
} else if ((m_labelType == mpX_TIME) || (m_labelType == mpX_HOURS)) {
double modulus = fabs(n);
double sign = n/modulus;
double hh = floor(modulus/3600);
double mm = floor((modulus - hh*3600)/60);
double ss = modulus - hh*3600 - mm*60;
#ifdef MATHPLOT_DO_LOGGING
wxLogMessage(wxT("%02.0f Hours, %02.0f minutes, %02.0f seconds"), sign*hh, mm, ss);
#endif // MATHPLOT_DO_LOGGING
if (fmt.Len() == 20) // Format with hours has 11 chars
s.Printf(fmt, sign*hh, mm, floor(ss));
else
s.Printf(fmt, sign*mm, ss);
}
dc.GetTextExtent(s, &tx, &ty);
if ((m_flags == mpALIGN_BORDER_BOTTOM) || (m_flags == mpALIGN_TOP)) {
dc.DrawText( s, p-tx/2, orgy-4-ty);
} else {
dc.DrawText( s, p-tx/2, orgy+4);
}
}
}
// Draw axis name
dc.GetTextExtent(m_name, &tx, &ty);
switch (m_flags) {
case mpALIGN_BORDER_BOTTOM:
dc.DrawText( m_name, extend - tx - 4, orgy - 8 - ty - labelH);
break;
case mpALIGN_BOTTOM: {
if ((!m_drawOutsideMargins) && (w.GetMarginBottom() > (ty + labelH + 8))) {
dc.DrawText( m_name, (endPx - startPx - tx)>>1, orgy + 6 + labelH);
} else {
dc.DrawText( m_name, extend - tx - 4, orgy - 4 - ty);
}
} break;
case mpALIGN_CENTER:
dc.DrawText( m_name, extend - tx - 4, orgy - 4 - ty);
break;
case mpALIGN_TOP: {
if ((!m_drawOutsideMargins) && (w.GetMarginTop() > (ty + labelH + 8))) {
dc.DrawText( m_name, (endPx - startPx - tx)>>1, orgy - 6 - ty - labelH);
} else {
dc.DrawText( m_name, extend - tx - 4, orgy + 4);
}
} break;
case mpALIGN_BORDER_TOP:
dc.DrawText( m_name, extend - tx - 4, orgy + 6 + labelH);
break;
default:
break;
}
}
/* if (m_flags != mpALIGN_TOP) {
if ((m_flags == mpALIGN_BORDER_BOTTOM) || (m_flags == mpALIGN_TOP)) {
dc.DrawText( m_name, extend - tx - 4, orgy - 4 - (ty*2));
} else {
dc.DrawText( m_name, extend - tx - 4, orgy - 4 - ty); //orgy + 4 + ty);
}
}; */
}
IMPLEMENT_DYNAMIC_CLASS(mpScaleY, mpLayer)
mpScaleY::mpScaleY(wxString name, int flags, bool ticks)
{
SetName(name);
SetFont( (wxFont&) *wxSMALL_FONT);
SetPen( (wxPen&) *wxGREY_PEN);
m_flags = flags;
m_ticks = ticks;
m_type = mpLAYER_AXIS;
m_labelFormat = wxT("");
}
void mpScaleY::Plot(wxDC & dc, mpWindow & w)
{
if (m_visible) {
dc.SetPen( m_pen);
dc.SetFont( m_font);
int orgx=0;
const int extend = w.GetScrY(); // /2;
if (m_flags == mpALIGN_CENTER)
orgx = w.x2p(0); //(int)(w.GetPosX() * w.GetScaleX());
if (m_flags == mpALIGN_LEFT) {
if (m_drawOutsideMargins)
orgx = Y_BORDER_SEPARATION;
else
orgx = w.GetMarginLeft();
}
if (m_flags == mpALIGN_RIGHT) {
if (m_drawOutsideMargins)
orgx = w.GetScrX() - Y_BORDER_SEPARATION;
else
orgx = w.GetScrX() - w.GetMarginRight();
}
if (m_flags == mpALIGN_BORDER_RIGHT )
orgx = w.GetScrX() - 1; //dc.LogicalToDeviceX(0) - 1;
if (m_flags == mpALIGN_BORDER_LEFT )
orgx = 1; //-dc.LogicalToDeviceX(0);
// Draw line