-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathCommonCurveWithPlotRange.cpp
222 lines (208 loc) · 6.75 KB
/
CommonCurveWithPlotRange.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
#include "CommonCurveWithPlotRange.h"
namespace nV {
namespace Graphics {
CommonCurveWithPlotRange::CommonCurveWithPlotRange(F1P *f1, double xmin, double xmax, double ymin, double ymax) {
f = f1;
this->xmin = xmin;
this->xmax = xmax;
this->ymin = ymin;
this->ymax = ymax;
spx = xmax - xmin;
spy = ymax - ymin;
makePoints();
}
CommonCurveWithPlotRange::CommonCurveWithPlotRange(F1P *f1, double xmin, double xmax, double ymin, double ymax, F1P *cf1) {
f = f1;
this->xmin = xmin;
this->xmax = xmax;
this->ymin = ymin;
this->ymax = ymax;
spx = xmax - xmin;
spy = ymax - ymin;
makePoints();
isColorfulCurve = true;
cf = cf1;
useColorFunction();
}
CommonCurveWithPlotRange::CommonCurveWithPlotRange(F1P *f1, double xmin, double xmax) {
f = f1;
this->xmin = xmin;
this->xmax = xmax;
spx = xmax - xmin;
makePlotRange();
makePoints();
}
CommonCurveWithPlotRange::CommonCurveWithPlotRange(F1P *f1, double xmin, double xmax, F1P *cf1) {
f = f1;
this->xmin = xmin;
this->xmax = xmax;
spx = xmax - xmin;
makePlotRange();
makePoints();
isColorfulCurve = true;
cf = cf1;
useColorFunction();
}
CommonCurveWithPlotRange::CommonCurveWithPlotRange(F1P *f1, double xmin, double xmax, double ymin, double ymax,
std::vector<singularitynode *> singularityset) {
f = f1;
this->xmin = xmin;
this->xmax = xmax;
this->ymin = ymin;
this->ymax = ymax;
spx = xmax - xmin;
spy = ymax - ymin;
this->singularity = true;
this->singularityset = singularityset;
makePoints();
}
CommonCurveWithPlotRange::CommonCurveWithPlotRange(F1P *f1, double xmin, double xmax, double ymin,
double ymax, std::vector<singularitynode *> singularityset, F1P *cf1) {
f = f1;
this->xmin = xmin;
this->xmax = xmax;
this->ymin = ymin;
this->ymax = ymax;
spx = xmax - xmin;
spy = ymax - ymin;
this->singularity = true;
this->singularityset = singularityset;
makePoints();
isColorfulCurve = true;
cf = cf1;
useColorFunction();
}
CommonCurveWithPlotRange::~CommonCurveWithPlotRange() {
}
void CommonCurveWithPlotRange::makePoints() {
dataNum = getBaseNum();
double *x = new double[dataNum];
double *y = new double[dataNum];
for (int z = 0; z < dataNum; z++) {
x[z] = xmin + (xmax - xmin) * z / (dataNum - 1);
}
f->getArrayData(x, y, dataNum);
LineStrip* lineStrip;
//add the first in-range point
int i = 0;
while (((y[i] < ymin) || (y[i] > ymax) || isNaN(y[i])) && i < dataNum)
i++;
if (i == dataNum) {
return;
}
lineStrip = new LineStrip;
lineStrips.add(lineStrip);
if (i == 0) {
lineStrip->vert.add(new Point2d(x[i], y[i]));
} else { //xmin point was excluded
double newx = getPointOnRangeFromLeft(x[i-1], x[i], 1);
double newy = f->getSingleData(newx);
lineStrip->vert.add(new Point2d(newx, newy));
lineStrip->vert.add(new Point2d(x[i], y[i]));
}
i++;
int state = 0;
while (i < dataNum) {
if ((y[i] > ymin) && (y[i] < ymax) && !isNaN(y[i])) {
if (state == 0) {
//line strip still going
lineStrip->vert.add(new Point2d(x[i], y[i]));
} else {
//make a new line strip
lineStrip = new LineStrip;
lineStrips.add(lineStrip);
double newx = getPointOnRangeFromLeft(x[i-1], x[i], 1);
double newy = f->getSingleData(newx);
lineStrip->vert.add(new Point2d(newx, newy));
lineStrip->vert.add(new Point2d(x[i], y[i]));
state = 0;
}
} else {
if (state == 0) {
//a line strip ends
double newx = getPointOnRangeFromRight(x[i-1], x[i], 1);
double newy = f->getSingleData(newx);
lineStrip->vert.add(new Point2d(newx, newy));
state = 1;
} else {
//do nothing
}
}
i++;
}
delete []x;
delete []y;
spx = xmax - xmin;
spy = ymax - ymin;
graphScale = spy / spx;
dx = spx / LARGE;
for (unsigned int i = 0; i < lineStrips.size(); i++)
addVerts(lineStrips[i]);
}
double CommonCurveWithPlotRange::getPointOnRangeFromLeft(double x1, double x2, int depth) {
if (depth > MAX_GET_POINT_ON_RANGE_DEPTH)
return x2;
double x = (x1 + x2) / 2;
double y = f->getSingleData(x);
if (y < ymin || y > ymax || isNaN(y))
return getPointOnRangeFromLeft(x, x2, depth + 1);
else
return getPointOnRangeFromLeft(x1, x, depth + 1);
}
double CommonCurveWithPlotRange::getPointOnRangeFromRight(double x1, double x2, int depth) {
if (depth > MAX_GET_POINT_ON_RANGE_DEPTH)
return x1;
double x = (x1 + x2) / 2;
double y = f->getSingleData(x);
if (y < ymin || y > ymax || isNaN(y))
return getPointOnRangeFromRight(x1, x, depth + 1);
else
return getPointOnRangeFromRight(x, x2, depth + 1);
}
int CommonCurveWithPlotRange::getBaseNum() {
//return 54; //54-1 is a prime number
//return 150;
if(xmax - xmin <= 10) {
return 55;
} else {
return (xmax - xmin) * 5;
}
}
void CommonCurveWithPlotRange::makePlotRange() {
int bigDataNum = 500;
double *x = new double[bigDataNum];
double *y = new double[bigDataNum];
for (int i = 0; i < bigDataNum; i++) {
x[i] = xmin + (xmax - xmin) * i / (bigDataNum - 1);
}
f->getArrayData(x, y, bigDataNum);
ArrayList<Point2d*> vert;
ArrayList<double> d;
for (int i = 0; i < bigDataNum; i++) {
vert.add(new Point2d(x[i], y[i]));
}
delete x;
delete y;
ymin = vert[0]->y;
ymax = vert[0]->y;
for (uint i = 0; i < vert.size(); i++) {
if (vert[i]->y > ymax) ymax = vert[i]->y;
if (vert[i]->y < ymin) ymin = vert[i]->y;
}
spx = xmax - xmin;
spy = ymax - ymin;
graphScale = spy / spx;
if(graphScale > MAX_GRAPHSCALE) {
double y_temp = (ymax + ymin) / 2.0;
spy = MAX_GRAPHSCALE * spx;
ymax = y_temp + std::abs(spy / 2);
ymin = y_temp - std::abs(spy / 2);
} else if(graphScale < MIN_GRAPHSCALE) {
double y_temp = (ymax + ymin) / 2.0;
spy = MIN_GRAPHSCALE * spx;
ymax = y_temp + std::abs(spy / 2);
ymin = y_temp - std::abs(spy / 2);
}
}
}
}