-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathCommonCurve.cpp
178 lines (167 loc) · 6.83 KB
/
CommonCurve.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
#include "CommonCurve.h"
namespace nV{
namespace Graphics{
CommonCurve::CommonCurve() {
}
CommonCurve::CommonCurve(F1P *f1, double start, double end) {
LineStrip* lineStrip = new LineStrip;
lineStrips.add(lineStrip);
f = f1;
xmin = start;
xmax = end;
spx = xmax - xmin;
makePoints();
}
CommonCurve::CommonCurve(F1P *f1, double start, double end, F1P *cf1) {
LineStrip* lineStrip = new LineStrip;
lineStrips.add(lineStrip);
f = f1;
xmin = start;
xmax = end;
spx = xmax - xmin;
cf = cf1;
isColorfulCurve = true;
makePoints();
useColorFunction();
}
CommonCurve::~CommonCurve() {
}
void CommonCurve::makePoints() {
dataNum = getBaseNum();
double *x = new double[dataNum];
double *y = new double[dataNum];
for (int i = 0; i < dataNum; i++) {
//split the domain into dataNum-1 parts
x[i] = xmin + (xmax - xmin) * i / (dataNum - 1);
}
f->getArrayData(x, y, dataNum);
for (int i = 0; i < dataNum; i++) {
lineStrips[0]->vert.add(new Point2d(x[i], y[i]));
}
delete x;
delete y;
ymin = lineStrips[0]->vert[0]->y;
ymax = lineStrips[0]->vert[0]->y;
for (int i = 0; i < dataNum; i++) {
if (lineStrips[0]->vert[i]->y > ymax) ymax = lineStrips[0]->vert[i]->y;
if (lineStrips[0]->vert[i]->y < ymin) ymin = lineStrips[0]->vert[i]->y;
}
spx = xmax - xmin;
spy = ymax - ymin;
graphScale = spy / spx;
dx = spx / LARGE;
addVerts(lineStrips[0]);
}
void CommonCurve::addVerts(LineStrip* lineStrip) {
//add points where the angle between two lines is too large
ArrayList<double> kcaled; //save the k calculated
double derivative = f->getDerivative(lineStrip->vert[0]->x, dx);
if(!isNaN(derivative)) {
kcaled.add(derivative / graphScale); //get k0, kcaledÊÇÒ»½×µ¼Êý
} else {
kcaled.add(MAX_DERIVATIVE);
}
unsigned int i = 0;
while (i < lineStrip->vert.size() - 1) {
double dis = std::sqrt(((lineStrip->vert[i+1]->x - lineStrip->vert[i]->x) / spx) * ((lineStrip->vert[i+1]->x - lineStrip->vert[i]->x) / spx) + ((lineStrip->vert[i+1]->y - lineStrip->vert[i]->y) / spy) * ((lineStrip->vert[i+1]->y - lineStrip->vert[i]->y) / spy));
double disx=lineStrip->vert[i+1]->x - lineStrip->vert[i]->x;
if (dis < MIN_DELTA_DIS_COMMON_CURVE || std::abs(disx)<=MIN_X_DIS) {
i++;
derivative = f->getDerivative(lineStrip->vert[i]->x, dx);
if(!isNaN(derivative)) {
kcaled.add(derivative / graphScale); //get ki, kcaledÊÇÒ»½×µ¼Êý
} else {
kcaled.add(MAX_DERIVATIVE);
}
continue; //line is too short
}
double netk1 = kcaled[i];
derivative = f->getDerivative(lineStrip->vert[i + 1]->x, -dx);
double netk2;
if(!isNaN(derivative)) {
netk2 = f->getDerivative(lineStrip->vert[i+1]->x, -dx) / graphScale; //always divided by graph scale
} else {
netk2 = MAX_DERIVATIVE;
}
double dangle = std::acos((1 + netk1 * netk2) / std::sqrt(1 + netk1 * netk1) / std::sqrt(1 + netk2 * netk2));
if (dangle > ADD_POINT_ANGLE_COMMON_CURVE) { //need to add points
double x = (lineStrip->vert[i]->x + lineStrip->vert[i+1]->x) / 2;
double y = f->getSingleData(x);
if(!isNaN(y)) {
lineStrip->vert.add(i + 1, new Point2d(x, y));
if (y > ymax) ymax = y;
if (y < ymin) ymin = y;
} else {
i++;
}
} else { //try adding points
double x = (lineStrip->vert[i]->x + lineStrip->vert[i+1]->x) / 2;
double net3y=f->getSingleData(x);
if(isNaN(net3y)) {
i++;
continue;
}
double dis1 = std::sqrt(((x - lineStrip->vert[i]->x) / spx) * ((x - lineStrip->vert[i]->x) / spx) + ((net3y - lineStrip->vert[i]->y) / spy) * ((net3y - lineStrip->vert[i]->y) / spy));
double dis2 = std::sqrt(((lineStrip->vert[i+1]->x - x) / spx) * ((lineStrip->vert[i+1]->x - x) / spx) + ((lineStrip->vert[i+1]->y - net3y) / spy) * ((lineStrip->vert[i+1]->y - net3y) / spy));
double netk3 = f->getDerivative(x, dx) / graphScale;
if(isNaN(netk3)) {
netk3 = MAX_DERIVATIVE;
}
double disx1=std::abs(x - lineStrip->vert[i]->x);
double disx2=std::abs(x - lineStrip->vert[i+1]->x);
if (((netk3 < netk1 && netk3 < netk2) || (netk3 > netk1 && netk3 > netk2)) && (dis1>MIN_DELTA_DIS_COMMON_CURVE&&dis2>MIN_DELTA_DIS_COMMON_CURVE) && (disx1>MIN_X_DIS&&disx2>MIN_X_DIS)) {
double y = f->getSingleData(x);
if(!isNaN(y)) {
lineStrip->vert.add(i + 1, new Point2d(x, y));
if (y > ymax) ymax = y;
if (y < ymin) ymin = y;
} else {
i++;
}
} else {
i++;
derivative = f->getDerivative(lineStrip->vert[i]->x, dx);
if(!isNaN(derivative)) {
kcaled.add(derivative / graphScale);
} else {
kcaled.add(MAX_DERIVATIVE);
}
}
}
}
spx = xmax - xmin;
spy = ymax - ymin;
graphScale = spy / spx;
}
int CommonCurve::getBaseNum() {
//should not be to large
return 20;
}
void CommonCurve::useColorFunction() {
double cfmin, cfmax;
cfmin = cf->getSingleData(lineStrips[0]->vert[0]->x);
cfmax = cfmin;
for (unsigned int i = 0; i < lineStrips.size(); i++) {
LineStrip* lineStrip = lineStrips[i];
for (unsigned int j = 0; j < lineStrip->vert.size(); j++) {
double tmp = cf->getSingleData(lineStrip->vert[j]->x);
if (tmp > cfmax)
cfmax = tmp;
if (tmp < cfmin)
cfmin = tmp;
}
}
for (unsigned int i = 0; i < lineStrips.size(); i++) {
LineStrip* lineStrip = lineStrips[i];
for (unsigned int j = 0; j < lineStrip->vert.size(); j++) {
double tmp = cf->getSingleData(lineStrip->vert[j]->x);
if(isNaN(cfmax) || isNaN(cfmin) || isNaN(tmp) || (cfmax == cfmin)) {
lineStrip->color.add(j, getRainbowColor(1));
continue;
}
lineStrip->color.add(j, getRainbowColor((tmp - cfmin) / (cfmax - cfmin)));
}
}
}
}
}