-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfitting.cpp
More file actions
47 lines (44 loc) · 1.21 KB
/
Copy pathfitting.cpp
File metadata and controls
47 lines (44 loc) · 1.21 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
#include<bits/stdc++.h>
#include"eigen-3.3.8/Eigen/Dense"
using namespace std;
using namespace Eigen;
struct Point {
double x, y, z;
};
vector<double> fitQuadraticSurface(const vector<Point>& points) {
int n = points.size();
MatrixXd A(n, 6);
VectorXd b(n);
for (int i = 0; i < n; ++i) {
double x = points[i].x;
double y = points[i].y;
double z = points[i].z;
A(i, 0) = x * x;
A(i, 1) = y * y;
A(i, 2) = x * y;
A(i, 3) = x;
A(i, 4) = y;
A(i, 5) = 1;
b(i) = z;
}
VectorXd params = A.colPivHouseholderQr().solve(b);
vector<double> result(params.data(), params.data() + params.size());
return result;
}
int main() {
vector<Point> points;
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
Point p;
cin >> p.x >> p.y >> p.z;
points.push_back(p);
}
vector<double> params = fitQuadraticSurface(points);
cout << "Parameters obtained by fitting:" << endl;
char paramNames[6] = {'a', 'b', 'c', 'd', 'e', 'f'};
for (int i = 0; i < 6; ++i) {
cout << paramNames[i] << " = " << params[i] << endl;
}
return 0;
}