cost_functions.h
Go to the documentation of this file.
1/**
2 * This file is part of ArmarX.
3 *
4 * ArmarX is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * ArmarX is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * @author Fabian Reister ( fabian dot reister at kit dot edu )
17 * @date 2021
18 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
19 * GNU General Public License
20 */
21
22#pragma once
23
25
26#include <ceres/ceres.h>
27
29{
30
31 struct SmoothOrientationFixedPreCostFunctor : ceres::CostFunction
32 {
33
34 SmoothOrientationFixedPreCostFunctor(const double yawPre, const double weight) :
35 yawPre(yawPre), weight(weight)
36 {
37 set_num_residuals(2);
38
39 mutable_parameter_block_sizes()->push_back(1);
40 mutable_parameter_block_sizes()->push_back(1);
41 }
42
43 bool
44 Evaluate(double const* const* parameters,
45 double* residuals,
46 double** jacobians) const override
47 {
48 const double yaw = parameters[0][0];
49 const double yawNext = parameters[0][1];
50
51 residuals[0] = weight * periodicDiff(yawPre, yaw);
52 residuals[1] = weight * periodicDiff(yaw, yawNext);
53
54 const auto periodicDiffJ0 = periodicDiffJacobian(yawPre, yaw);
55 const auto periodicDiffJ1 = periodicDiffJacobian(yaw, yawNext);
56
57 if (jacobians == nullptr)
58 {
59 return true;
60 }
61 double* jacobian0 = jacobians[0];
62 if (jacobian0 == nullptr)
63 {
64 return true;
65 }
66
67 double* jacobian1 = jacobians[1];
68 if (jacobian1 == nullptr)
69 {
70 return true;
71 }
72
73
74 // d r_i / d p_0
75 jacobian0[0] = weight * periodicDiffJ0.at(1);
76 jacobian0[1] = weight * periodicDiffJ1.at(0);
77 // jacobian0[2] = weight * periodicDiffJ1.at(0);
78
79 // d r_i / d p_1
80 jacobian1[0] = 0.;
81 jacobian1[1] = weight * periodicDiffJ1.at(1);
82
83 return true;
84 }
85
86 private:
87 const double yawPre;
88 const double weight;
89 };
90
91 struct SmoothOrientationFixedNextCostFunctor : ceres::CostFunction
92 {
93
94 SmoothOrientationFixedNextCostFunctor(const double yawNext, const double weight) :
95 yawNext(yawNext), weight(weight)
96 {
97 set_num_residuals(2);
98
99 mutable_parameter_block_sizes()->push_back(1);
100 mutable_parameter_block_sizes()->push_back(1);
101 }
102
103 bool
104 Evaluate(double const* const* parameters,
105 double* residuals,
106 double** jacobians) const override
107 {
108 const double yawPre = parameters[0][0];
109 const double yaw = parameters[0][1];
110
111 residuals[0] = weight * periodicDiff(yawPre, yaw);
112 residuals[1] = weight * periodicDiff(yaw, yawNext);
113
114 const auto periodicDiffJ0 = periodicDiffJacobian(yawPre, yaw);
115 const auto periodicDiffJ1 = periodicDiffJacobian(yaw, yawNext);
116
117 if (jacobians == nullptr)
118 {
119 return true;
120 }
121 double* jacobian0 = jacobians[0];
122 if (jacobian0 == nullptr)
123 {
124 return true;
125 }
126
127 double* jacobian1 = jacobians[1];
128 if (jacobian1 == nullptr)
129 {
130 return true;
131 }
132
133 // d r_i / d p_0
134 jacobian0[0] = weight * periodicDiffJ0.at(0);
135 jacobian0[1] = 0;
136
137 // d r_i / d p_1
138 jacobian1[0] = weight * periodicDiffJ0.at(1);
139 jacobian1[1] = weight * periodicDiffJ1.at(0);
140
141 return true;
142 }
143
144 private:
145 const double yawNext;
146 const double weight;
147 };
148
149 struct OrientationPriorCostFunctor : ceres::SizedCostFunction<1, 1>
150 {
151
152 OrientationPriorCostFunctor(const double prior, const double weight) :
153 prior(prior), weight(weight)
154 {
155 }
156
157 bool
158 Evaluate(double const* const* parameters,
159 double* residuals,
160 double** jacobians) const override
161 {
162 const double yaw = parameters[0][0];
163
164 residuals[0] = weight * periodicDiff(prior, yaw);
165
166 const auto periodicDiffJ = periodicDiffJacobian(prior, yaw);
167
168 if (jacobians == nullptr)
169 {
170 return true;
171 }
172 double* jacobian = jacobians[0];
173 if (jacobian == nullptr)
174 {
175 return true;
176 }
177
178
179 jacobian[0] = weight * periodicDiffJ.at(1);
180 return true;
181 }
182
183 private:
184 const double prior;
185 const double weight;
186 };
187
188 struct SmoothOrientationCostFunctor : ceres::CostFunction
189 {
190
191 SmoothOrientationCostFunctor(const double weight) : weight(weight)
192 {
193 set_num_residuals(2);
194
195 mutable_parameter_block_sizes()->push_back(1);
196 mutable_parameter_block_sizes()->push_back(1);
197 mutable_parameter_block_sizes()->push_back(1);
198 }
199
200 bool
201 Evaluate(double const* const* parameters,
202 double* residuals,
203 double** jacobians) const override
204 {
205 const double yawPrev = parameters[0][0];
206 const double yaw = parameters[0][1];
207 const double yawNext = parameters[0][2];
208
209 residuals[0] = weight * periodicDiff(yawPrev, yaw);
210 residuals[1] = weight * periodicDiff(yaw, yawNext);
211
212 const auto periodicDiffJ0 = periodicDiffJacobian(yawPrev, yaw);
213 const auto periodicDiffJ1 = periodicDiffJacobian(yaw, yawNext);
214
215 if (jacobians == nullptr)
216 {
217 return true;
218 }
219
220 double* jacobian0 = jacobians[0];
221 if (jacobian0 == nullptr)
222 {
223 return true;
224 }
225
226 double* jacobian1 = jacobians[1];
227 if (jacobian1 == nullptr)
228 {
229 return true;
230 }
231
232 double* jacobian2 = jacobians[2];
233 if (jacobian2 == nullptr)
234 {
235 return true;
236 }
237
238 // d r_i / d p_0
239 jacobian0[0] = weight * periodicDiffJ0.at(0);
240 jacobian0[1] = 0.;
241
242 // d r_i / d p_2
243 jacobian1[0] = weight * periodicDiffJ0.at(1);
244 jacobian1[1] = weight * periodicDiffJ1.at(0);
245
246 // d r_i / d p_1
247 jacobian2[0] = 0.;
248 jacobian2[1] = weight * periodicDiffJ1.at(1);
249
250 return true;
251 }
252
253 private:
254 const double weight;
255 };
256
257} // namespace armarx::navigation::global_planning::optimization
std::array< double, 2 > periodicDiffJacobian(const auto a, const auto b)
Definition math.h:50
auto periodicDiff(const auto a, const auto b)
Definition math.h:32
bool Evaluate(double const *const *parameters, double *residuals, double **jacobians) const override
bool Evaluate(double const *const *parameters, double *residuals, double **jacobians) const override
bool Evaluate(double const *const *parameters, double *residuals, double **jacobians) const override
bool Evaluate(double const *const *parameters, double *residuals, double **jacobians) const override