-
Notifications
You must be signed in to change notification settings - Fork 1
/
helloworld.cc
162 lines (130 loc) · 3.7 KB
/
helloworld.cc
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
// A simple example of using the Ceres minimizer.
//
#include <stdio.h>
#include <sstream>
#include "ceres/ceres.h"
#include "glog/logging.h"
#ifdef EMSCRIPTEN
#include "emscripten/emscripten.h"
#include "emscripten/bind.h"
using namespace emscripten;
#endif
using ceres::AutoDiffCostFunction;
using ceres::CostFunction;
using ceres::Problem;
using ceres::Solver;
using ceres::Solve;
using namespace std;
struct Point2D {
double x;
double y;
Point2D() {}
Point2D(double x_, double y_) : x(x_), y(y_) {}
};
struct CostFunctor {
double* goal_pose;
CostFunctor(double* g) : goal_pose(g) {}
template <typename T> bool operator()(const T* const current_pose, T* residual) const {
// Objective function: residual = 50^2 - ((x - x_)^2 + (y - y_)^2)
residual[0] = T(10000.0) - (T(goal_pose[0]) - current_pose[0]) * (T(goal_pose[0]) - current_pose[0]) - (T(goal_pose[1]) - current_pose[1]) * (T(goal_pose[1]) - current_pose[1]);
return true;
}
};
class AvoiderSolver {
private:
double current_pose_[2];
double goal_pose_[2];
Problem problem_;
public:
AvoiderSolver() {
current_pose_[0] = 0;
current_pose_[1] = 0;
goal_pose_[0] = 0;
goal_pose_[1] = 0;
buildProblem();
}
Point2D getCurrentPose() const {
Point2D p;
p.x = current_pose_[0];
p.y = current_pose_[1];
return p;
}
void setCurrentPose(Point2D p) {
current_pose_[0] = p.x;
current_pose_[1] = p.y;
}
Point2D getGoalPose() const {
Point2D p;
p.x = goal_pose_[0];
p.y = goal_pose_[1];
return p;
}
void setGoalPose(Point2D p) {
goal_pose_[0] = p.x;
goal_pose_[1] = p.y;
}
void buildProblem() {
CostFunction* cost_function =
new AutoDiffCostFunction<CostFunctor, 1, 2>(new CostFunctor(goal_pose_));
problem_.AddResidualBlock(cost_function, NULL, current_pose_);
}
void stepSolve(int step_limit) {
// Run the solver!
Solver::Options options;
options.max_num_iterations = step_limit;
options.logging_type = ceres::SILENT;
options.minimizer_type = ceres::LINE_SEARCH;
Solver::Summary summary;
Solve(options, &problem_, &summary);
}
void timeSolve(double time_limit) {
// Run the solver!
Solver::Options options;
options.max_solver_time_in_seconds = time_limit;
options.logging_type = ceres::SILENT;
options.minimizer_type = ceres::LINE_SEARCH;
Solver::Summary summary;
Solve(options, &problem_, &summary);
}
};
/*
class RenderingCallback : public IterationCallback {
public:
explicit RenderingCallback(double* x)
: x_(x) {}
~RenderingCallback() {}
CallbackReturnType operator()(const IterationSummary& summary) {
ostringstream strs;
strs << "Module.print('x = " << *x_ << "')";
emscripten_run_script(strs.str().c_str());
cout << "Iteration " << summary.iteration << endl;
return SOLVER_CONTINUE;
}
private:
const double* x_;
};
*/
#ifdef EMSCRIPTEN
EMSCRIPTEN_BINDINGS() {
value_array<Point2D>("Point2D")
.element(&Point2D::x)
.element(&Point2D::y)
;
class_<AvoiderSolver>("AvoiderSolver")
.constructor<>()
.function("stepSolve", &AvoiderSolver::stepSolve)
.function("timeSolve", &AvoiderSolver::timeSolve)
.property("goal_pose", &AvoiderSolver::getGoalPose, &AvoiderSolver::setGoalPose)
.property("current_pose", &AvoiderSolver::getCurrentPose, &AvoiderSolver::setCurrentPose)
;
}
#else
int main(int argc, char** argv) {
AvoiderSolver solver;
solver.setGoalPose(Point2D(30,100));
solver.stepSolve(10);
Point2D final_pose = solver.getCurrentPose();
cout << "final_pose.x = " << final_pose.x << ", " << "final_pose.y = " << final_pose.y << "\n";
return 0;
}
#endif