-
Notifications
You must be signed in to change notification settings - Fork 1
/
circle_fitting.cpp
264 lines (220 loc) · 5.31 KB
/
circle_fitting.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
#include <iostream>
#include <cmath>
#include <vector>
#include <cstdlib>
#include <stdio.h>
#include <fstream>
using namespace std;
// 2D Matrix class
class Mat2D{
public:
double a, b, c, d;
Mat2D(){
a = b = c = d = 0.0;
}
Mat2D(double m, double n, double o, double p){
a = m;
b = n;
c = o;
d = p;
}
void setMat(double m, double n, double o, double p){
a = m;
b = n;
c = o;
d = p;
}
double determinant(){
return ((this->a*this->d)-(this->b*this->c));
}
void printMat(){
cout << endl;
cout << a << " " << b << endl;
cout << c << " " << d << endl;
cout << endl;
}
Mat2D& operator =(const Mat2D& M){
this->a = M.a;
this->b = M.b;
this->c = M.c;
this->d = M.d;
return *this;
}
};
// 2P point class
class Point2D: public Mat2D{
public:
double x;
double y;
Point2D(){
x = 0.0;
y = 0.0;
}
Point2D(float a, float b){
x = a;
y = b;
}
void setxy(float a, float b){
x = a;
y = b;
}
void pointPrint(){
cout << "(" << x << ", " << y << ")" << endl;
}
friend Point2D operator *(Mat2D& e, Point2D& b){
Point2D newP;
newP.x =(e.a * b.x) + (e.b * b.y);
newP.y =(e.c * b.x) + (e.d * b.y);
return newP;
}
};
// matrix inverse
Mat2D inverse(Mat2D& inv){
double dt = inv.determinant();
Mat2D newMat;
newMat.a = inv.d/dt;
newMat.b = -(inv.b/dt);
newMat.c = -(inv.c/dt);
newMat.d = inv.a/dt;
return newMat;
}
// Sum of x and y coordinate returned as point
Point2D sumXY(vector<Point2D>& val){
double sumX = 0.0;
double sumY = 0.0;
for(unsigned int i=0; i<val.size(); i++){
sumX += val[i].x;
sumY += val[i].y;
}
Point2D rval;
rval.setxy(sumX, sumY);
return rval;
}
// Sum of squares of the points
Point2D sumSQ(vector<Point2D>& val){
double sumX = 0.0;
double sumY = 0.0;
for(unsigned int i=0; i<val.size(); i++){
sumX += pow(val[i].x, 2);
sumY += pow(val[i].y, 2);
}
Point2D rval;
rval.setxy(sumX, sumY);
return rval;
}
// Sum of cubes of the x and y
Point2D sumCU(vector<Point2D>& val){
double sumX = 0.0;
double sumY = 0.0;
for(unsigned int i=0; i<val.size(); i++){
sumX += pow(val[i].x, 3);
sumY += pow(val[i].y, 3);
}
Point2D rval;
rval.setxy(sumX, sumY);
return rval;
}
// Sum of x*y of the points returned as double
double sumUV(vector<Point2D>& val){
double sum = 0.0;0;
for(unsigned int i=0; i<val.size(); i++){
sum += (val[i].x*val[i].y);
}
return sum;
}
// Sum of x*y^2 of the points returned as double
double sumUVV(vector<Point2D>& val){
double sum = 0.0;0;
for(unsigned int i=0; i<val.size(); i++){
sum += (val[i].x*(pow(val[i].y, 2)));
}
return sum;
}
// Sum of x^2*y of the points returned as double
double sumVUU(vector<Point2D>& val){
double sum = 0.0;0;
for(unsigned int i=0; i<val.size(); i++){
sum += ((pow(val[i].x, 2))*val[i].y);
}
return sum;
}
// Add points
Point2D addPoints(Point2D a, Point2D b){
Point2D res;
res.x = a.x + b.x;
res.y = a.y + b.y;
return res;
}
// average of the points
Point2D average(vector<Point2D>& vecPoints){
double sumX = 0.0;
double sumY = 0.0;
unsigned int N = vecPoints.size();
for(unsigned int i=0; i<N; i++){
sumX += vecPoints[i].x;
sumY += vecPoints[i].y;
}
double avg_x = sumX/(double)N;
double avg_y = sumY/(double)N;
Point2D avgPT;
avgPT.setxy(avg_x, avg_y);
return avgPT;
}
// calculating u and v points
void uvCalculator(vector<Point2D>& vecPoints, Point2D avg, vector<Point2D>& uvpoints){
for(unsigned int i = 0; i < vecPoints.size(); i++){
Point2D temp;
double a = vecPoints[i].x - avg.x;
double b = vecPoints[i].y - avg.y;
temp.setxy(a, b);
uvpoints.push_back(temp);
}
}
int main(int argc, const char * argv[]) {
vector<Point2D> vecPoints;
ifstream myfile("temp.txt");
double x=0.0, y =0.0;
if(!myfile.is_open()){
cout << "Unable to open file";
return 0;
}
while (myfile >> x >> y){
Point2D point;
point.setxy((double)x,(double)y);
vecPoints.push_back(point);
}
myfile.close();
Point2D avgPT;
avgPT = average(vecPoints); // average x and y
vector<Point2D> uvpoints;
uvCalculator(vecPoints, avgPT, uvpoints); // calculating u v points
Point2D S_2uv = sumSQ(uvpoints); // consists of two points u^2 and v ^2
Point2D S_3uv = sumCU(uvpoints); // consists of two points u^3 and v ^3
double S_uv = sumUV(uvpoints); // consists of sum of u.v
double S_UVV = sumUVV(uvpoints); // consists of sum of u.v^2
double S_VUU = sumVUU(uvpoints); // consists of sum of v.u^2
double ap = 0.5*(S_3uv.x + S_UVV);
double bp = 0.5*(S_3uv.y + S_VUU);
Point2D Pab;
Pab.setxy(ap, bp);
unsigned int N = uvpoints.size();
N = (double)N;
Mat2D iMat;
Mat2D nMat(S_2uv.x, S_uv, S_uv, S_2uv.y); // Initial matrix
iMat = inverse(nMat); // inverted matrix
//nMat.printMat();
//iMat.printMat();
Point2D center; // center of the circle
Point2D val_uvC;
val_uvC = iMat*Pab; // uc and vc
center = addPoints(val_uvC, avgPT); // Actual center as we add the average
double valSN = ((S_2uv.x + S_2uv.y)/N);
double R = sqrt(pow(val_uvC.x, 2) + pow(val_uvC.y, 2) + valSN);
cout << "The Number of Points are: " << N << endl;
cout << endl;
cout << "Circle is centered at: " << endl;
center.pointPrint();
cout << "\nRadius : " << endl;
cout << R << endl;
return 0;
}