-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathPlot2D.h
More file actions
169 lines (135 loc) · 4.52 KB
/
Plot2D.h
File metadata and controls
169 lines (135 loc) · 4.52 KB
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
#ifndef Plot2D_h
#define Plot2D_h
#include <vector>
#include <string>
#include "fastmath.h"
#include "Vec2.h"
#include "Vec3.h"
#include "geom2D.h"
#include "VecN.h"
const int nXTicks_def = 11;
const int nYTicks_def = 11;
const int dXTicks_def = 1.0;
const int dYTicks_def = 1.0;
// =======================
// ==== DataLine2D
// =======================
class DataLine2D{ public:
// data
int n = 0;
bool bSharedX =false;
//Arr xs;
double * xs =0;
double * ys =0;
Func1d yfunc =0;
//update_data =false;
// draw properties
Rect2d bounds;
int glObj =0;
char lineStyle ='-';
//char pointStyle ='+';
char pointStyle =' ';
float pointSize =0.1;
bool bView = true;
uint32_t clr =0xFFFF00FF;
std::string label;
void update();
int render();
void draw();
void view();
inline void set ( int n_, double * xs_, double * ys_){ n=n_; xs=xs_; ys=ys_; };
//inline void allocate( int n_ ){ n=n_; xs=new double[n]; ys=new double[n]; bSharedX=false; };
inline void allocate( int n_ ){ n=n_; _allocIfNull(xs,n); _allocIfNull(ys,n); bSharedX=false; };
inline void linspan(double xmin, double xmax){ VecN::linspan(n,xmin,xmax,xs); };
inline void arange (double xmin, double dx ){ VecN::arange (n,xmin,dx,xs); };
inline DataLine2D()=default;
inline DataLine2D(int n_){ allocate(n_); }
inline DataLine2D(int n_,double*xs_){ n=n_; bSharedX=true; xs=xs_; ys=new double[n]; }
//inline DataLine2D(int n_,double xmin,double xmax){ allocate(n_); linspan(xmin,xmax); }
inline DataLine2D(int n_,double xmin,double dx, uint32_t clr_=0xFFFF00FF, std::string label_="", double* ys_=0 ){ ys=ys_; allocate(n_); arange(xmin,dx); clr=clr_; label=label_; }
inline DataLine2D(int n_,double*xs_, uint32_t clr_=0xFFFF00FF, std::string label_="", double* ys_=0 ){ ys=ys_; n=n_; bSharedX=true; xs=xs_; ys=new double[n]; clr=clr_; label=label_; }
inline void replace_xs( double* xs_ ){ if(xs)delete [] xs; xs=xs_; };
~DataLine2D();
};
template<typename Func>
void evalLine( DataLine2D& l, Func func ){
for(int i=0; i<l.n; i++){ l.ys[i]=func(l.xs[i]); }
};
// =======================
// ==== Plot2D
// =======================
class Plot2D{ public:
// data
std::vector<DataLine2D*> lines;
// properties
Rect2d bounds;
int nXTicks=0,nYTicks=0;
double * xTicks=NULL;
double * yTicks=NULL;
int glObj=0;
bool bRenderAxes=true;
Vec2d shift =Vec2dZero;
Vec2d scaling =Vec2dOnes;
Vec2d axPos;
Rect2d axBounds;
float tickSz = 0.1;
Vec2f legend_pos={0.,0.};
std::string xlabel="";
std::string ylabel="";
inline DataLine2D* add(DataLine2D* dline){
lines.push_back(dline);
return dline;
};
bool logX = false;
bool logY = false;
bool bGrid = true;
bool bAxes = true;
bool bTicks = true;
bool tickCaption = false;
uint32_t clrBg = 0x00f0f0f0;
uint32_t clrGrid = 0xFF858585;
uint32_t clrTicksX = 0xFF000000;
uint32_t clrTicksY = 0xFF000000;
char * tickFormat = "%2.2f\0";
int fontTex=0;
void update();
void drawTexts();
void drawAxes();
int render();
void view (bool bAxes=true);
void init ();
//void xsharingLines(int nl, int np);
//void xsharingLines(int nl, int np, double xmin, double dx);
//void init( double dx, double dy );
void xsharingLines(int nl, int np, double xmin=0.0, double dx=0.1, uint32_t* colors=0, int ncol=-1);
void autoAxes(double dx, double dy);
void clear(bool bDeep=true);
void erase(int i);
void savetxt(const char* fname);
void drawHline ( double y );
void drawVline ( double x );
//void drawCursor( Vec2d p, double sz );
//void addDataLineSequence( int nl, int np, uint32_t colors, int ncol=-1 ){}
Plot2D()=default;
~Plot2D(){};
};
// =======================
// ==== QuePlot2D
// =======================
class QuePlot2D{ public:
int n;
int nlines;
uint32_t * lColors;
double * ts;
double ** data;
int nsamp=0;
int ip=0;
void init( int n_, int nlines_ );
void next(double t);
void draw( bool xoff, bool yoff );
void drawTrj3D( Vec3i which );
void drawTrj3DPoints( Vec3i which, double pointSize );
inline int wrap_index(int i){ return i>=n? i%n : i; };
inline void set_back(int iline, double y ){ data[iline][ip] = y; };
};
#endif