-
Notifications
You must be signed in to change notification settings - Fork 8
/
PNPSimulation.cpp
200 lines (154 loc) · 6.33 KB
/
PNPSimulation.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
//
// Created by lancelot on 3/15/17.
//
#include <memory>
#include "PNPSimulation.h"
#include <ceres/ceres.h>
Eigen::Vector2d Camera::project(double x, double y, double z) {
Eigen::Vector2d uv;
uv(0) = fx_ * x / z + cx_;
uv(1) = fy_ * y / z + cy_;
return uv;
}
Eigen::Vector2d Camera::project(Eigen::Vector3d point) {
return project(point(0), point(1), point(2));
}
Eigen::Vector3d Camera::bacProject(Eigen::Vector2d uv, double d) {
Eigen::Vector3d point;
point(2) = d;
point(0) = (uv(0) - cx_) * d / fx_;
point(1) = (uv(1) - cy_) * d / fy_;
return point;
}
/* ############################################################################################
* ############################################################################################
*/
class reprojectErr : public ceres::SizedCostFunction<2, 6> {
public:
reprojectErr(Eigen::Vector3d& pt, Eigen::Vector2d &uv,
Eigen::Matrix<double, 2, 2> &information,
std::shared_ptr<Camera> cam);
virtual ~reprojectErr() {}
virtual bool Evaluate(double const* const* parameters,
double* residuals,
double** jacobians) const;
public:
Eigen::Vector3d pt_;
Eigen::Vector2d uv_;
std::shared_ptr<Camera> cam_;
Eigen::Matrix<double, 2, 2> sqrt_information_;
static int index;
};
int reprojectErr::index = 0;
bool reprojectErr::Evaluate(double const *const *parameters, double *residuals, double **jacobians) const {
Eigen::Map<const Eigen::Matrix<double, 6, 1>> lie(parameters[0]);
Sophus::SE3d T = Sophus::SE3d::exp(lie);
//std::cout << T.matrix3x4() << std::endl;
Eigen::Vector3d P = T * pt_;
Eigen::Vector2d uv = cam_->project(P);
Eigen::Vector2d err = uv - uv_;
err = sqrt_information_ * err;
residuals[0] = err(0);
residuals[1] = err(1);
Eigen::Matrix<double, 2, 6> Jac = Eigen::Matrix<double, 2, 6>::Zero();
Jac(0, 0) = cam_->fx_ / P(2); Jac(0, 2) = -P(0) / P(2) /P(2) * cam_->fx_; Jac(0, 3) = Jac(0, 2) * P(1);
Jac(0, 4) = cam_->fx_ - Jac(0, 2) * P(0); Jac(0, 5) = -Jac(0, 0) * P(1);
Jac(1, 1) = cam_->fy_ / P(2); Jac(1, 2) = -P(1) / P(2) /P(2) * cam_->fy_; Jac(1, 3) = -cam_->fy_ + Jac(1, 2) * P(1);
Jac(1, 4) = -Jac(1, 2) * P(0); Jac(1, 5) = Jac(1, 1) * P(0);
Jac = sqrt_information_ * Jac;
int k = 0;
for(int i = 0; i < 2; ++i) {
for(int j = 0; j < 6; ++j) {
if(k >= 12)
return false;
if(jacobians) {
if(jacobians[0])
jacobians[0][k] = Jac(i, j);
}
k++;
}
}
//printf("jacobian ok!\n");
return true;
}
reprojectErr::reprojectErr(Eigen::Vector3d& pt, Eigen::Vector2d &uv,
Eigen::Matrix<double, 2, 2>& information,
std::shared_ptr<Camera> cam) : pt_(pt), uv_(uv), cam_(cam) {
//printf("index = %d\n", index++);
Eigen::LLT<Eigen::Matrix<double, 2, 2>> llt(information);
sqrt_information_ = llt.matrixL();
}
class CERES_EXPORT SE3Parameterization : public ceres::LocalParameterization {
public:
SE3Parameterization() {}
virtual ~SE3Parameterization() {}
virtual bool Plus(const double* x,
const double* delta,
double* x_plus_delta) const;
virtual bool ComputeJacobian(const double* x,
double* jacobian) const;
virtual int GlobalSize() const { return 6; }
virtual int LocalSize() const { return 6; }
};
bool SE3Parameterization::ComputeJacobian(const double *x, double *jacobian) const {
ceres::MatrixRef(jacobian, 6, 6) = ceres::Matrix::Identity(6, 6);
return true;
}
bool SE3Parameterization::Plus(const double* x,
const double* delta,
double* x_plus_delta) const {
Eigen::Map<const Eigen::Matrix<double, 6, 1>> lie(x);
Eigen::Map<const Eigen::Matrix<double, 6, 1>> delta_lie(delta);
Sophus::SE3d T = Sophus::SE3d::exp(lie);
Sophus::SE3d delta_T = Sophus::SE3d::exp(delta_lie);
Eigen::Matrix<double, 6, 1> x_plus_delta_lie = (delta_T * T).log();
for(int i = 0; i < 6; ++i) x_plus_delta[i] = x_plus_delta_lie(i, 0);
return true;
}
/* ############################################################################################
* ############################################################################################
*/
PNPSimulation::PNPSimulation(Sophus::SE3d &se3, Eigen::Matrix<double, 2, 2>& Var) {
real_ = se3;
information_ = Var.inverse();
}
void PNPSimulation::start() {
std::vector<Eigen::Vector3d> ptReals;
sampleUniformMeans<double, 3>(-20.0, 20.0, ptReals);
std::shared_ptr<Camera> cam = std::make_shared<Camera>(1.0, 1.0, 1.0, 1.0);
double se3[6] = {0, 0, 0, 0, 0, 0};
std::vector<Eigen::Vector2d> turbulences;
{
Eigen::Matrix<double, 2, 1> turbulence_;
Eigen::Matrix<double, 2, 2> var = information_.inverse();
Eigen::Matrix<double, 2, 1> mean = Eigen::Matrix<double, 2, 1>::Zero();
for(size_t i = 0; i < ptReals.size(); ++i) {
turbulence_ = oneSampleGauss<double, 2>(mean, var);
turbulences.push_back(turbulence_);
}
}
std::vector<Eigen::Vector2d> pix;
for(size_t i = 0; i < turbulences.size(); ++i) {
pix.push_back(cam->project((real_ * ptReals[i])) + turbulences[i]);
}
// for(size_t i = 0; i < ptReal.size(); ++i) {
// std::cout << "3d:\n" << ptReal[i] << "\n2d:\n" << pix[i] << "\n###########################" << std::endl;
// }
ceres::Problem problem;
for(size_t i = 0; i < ptReals.size(); ++i) {
ceres::CostFunction * costFun = new reprojectErr(ptReals[i], pix[i], information_, cam);
problem.AddResidualBlock(costFun, new ceres::HuberLoss(0.5), se3);
//
}
problem.SetParameterization(se3, new SE3Parameterization());
ceres::Solver::Options options;
options.minimizer_type = ceres::TRUST_REGION;
options.linear_solver_type = ceres::DENSE_NORMAL_CHOLESKY;
options.trust_region_strategy_type = ceres::DOGLEG;
options.minimizer_progress_to_stdout = true;
options.dogleg_type = ceres::SUBSPACE_DOGLEG;
ceres::Solver::Summary summary;
ceres::Solve(options, &problem, &summary);
std::cout << summary.BriefReport() << "\n";
}
PNPSimulation::~PNPSimulation() {}