residuals.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
17#pragma once
18
19#include <algorithm>
20#include <cmath>
21#include <vector>
22
24
27
28#include <ceres/ceres.h>
29
30#if CERES_VERSION_MAJOR >= 2 && CERES_VERSION_MINOR >= 1
31# include <ceres/manifold.h>
32 using SubsetConstraint = ceres::SubsetManifold;
33# define SET_CONSTRAINT(problem, param, constraint) \
34 (problem).SetManifold((param), (constraint))
35#else
36# include <ceres/local_parameterization.h>
37 using SubsetConstraint = ceres::SubsetParameterization;
38# define SET_CONSTRAINT(problem, param, constraint) \
39 (problem).SetParameterization((param), (constraint))
40#endif
41
43{
45 {
46 double x, y, theta, v;
47 };
48
49 inline double normalizeAngle(double a)
50 {
51 return std::atan2(std::sin(a), std::cos(a));
52 }
53
54 static void normalizeTrajectoryAngles(std::vector<CenterPoint>& traj)
55 {
56 for (auto& p : traj)
57 p.theta = normalizeAngle(p.theta);
58 }
59
60 // -----------------------------
61 // Position-only smoothness residual
62 // -----------------------------
64 {
65 explicit PositionSmoothResidual(double weight = 1.0) : w(weight) {}
66
67 template <typename T>
68 bool operator()(const T* const c_prev,
69 const T* const c_i,
70 const T* const c_next,
71 T* residual) const
72 {
73 residual[0] = T(w) * (c_prev[0] - T(2.0) * c_i[0] + c_next[0]); // x
74 residual[1] = T(w) * (c_prev[1] - T(2.0) * c_i[1] + c_next[1]); // y
75 return true;
76 }
77
78 double w;
79 };
80
81 // -----------------------------
82 // Obstacle residuals (2-D)
83 // -----------------------------
85 {
87 double weight,
88 double max_dist,
89 double clearance) :
90 wrapper_(wrapper),
91 w_(weight),
92 max_dist_(max_dist),
93 clearance_(clearance)
94 {
95 }
96
97 template <typename T>
98 bool operator()(const T* const node, T* residual) const
99 {
100 T d_raw;
101 wrapper_(&node[0], &node[1], &d_raw);
102
103 if (!ceres::isfinite(d_raw))
104 {
105 residual[0] = T(w_) * T(100.0);
106 return true;
107 }
108
109 T obs_dist = d_raw - T(clearance_);
110
111 if (obs_dist > T(max_dist_))
112 {
113 residual[0] = T(0.0);
114 }
115 else if (obs_dist < T(0.0))
116 {
117 T penetration = -obs_dist;
118 const T max_pen = T(3.0) * T(max_dist_);
119 if (penetration > max_pen)
120 penetration = max_pen;
121 T ratio = penetration / T(max_dist_);
122 residual[0] = T(w_) * (ceres::exp(T(2.0) * ratio) - T(1.0) + T(0.1) * ratio);
123 }
124 else
125 {
126 residual[0] = T(w_) * (T(max_dist_) - obs_dist) / T(max_dist_);
127 }
128 return true;
129 }
130
132 double w_;
133 double max_dist_;
135 };
136
138 {
140 double weight,
141 double max_dist,
142 double clearance) :
143 wrapper_(wrapper),
144 w_(weight),
145 max_dist_(max_dist),
146 clearance_(clearance)
147 {
148 }
149
150 template <typename T>
151 bool operator()(const T* const c_i, const T* const c_ip1, T* residual) const
152 {
153 T mx = (c_i[0] + c_ip1[0]) / T(2.0);
154 T my = (c_i[1] + c_ip1[1]) / T(2.0);
155
156 T d_raw;
157 wrapper_(&mx, &my, &d_raw);
158
159 if (!ceres::isfinite(d_raw))
160 {
161 residual[0] = T(w_) * T(100.0);
162 return true;
163 }
164
165 T obs_dist = d_raw - T(clearance_);
166
167 if (obs_dist > T(max_dist_))
168 {
169 residual[0] = T(0.0);
170 }
171 else if (obs_dist < T(0.0))
172 {
173 T penetration = -obs_dist;
174 const T max_pen = T(3.0) * T(max_dist_);
175 if (penetration > max_pen)
176 penetration = max_pen;
177 T ratio = penetration / T(max_dist_);
178 residual[0] = T(w_) * (ceres::exp(T(2.0) * ratio) - T(1.0) + T(0.1) * ratio);
179 }
180 else
181 {
182 residual[0] = T(w_) * (T(max_dist_) - obs_dist) / T(max_dist_);
183 }
184 return true;
185 }
186
188 double w_;
189 double max_dist_;
191 };
192
193 // Obstacle residual sampled at an arbitrary position along the segment.
194 // t=0 evaluates at c_i, t=1 at c_ip1; midpoints use t=0.5.
196 {
198 double weight,
199 double max_dist,
200 double clearance,
201 double t) :
202 wrapper_(wrapper),
203 w_(weight),
204 max_dist_(max_dist),
205 clearance_(clearance),
206 t_(t)
207 {
208 }
209
210 template <typename T>
211 bool operator()(const T* const c_i, const T* const c_ip1, T* residual) const
212 {
213 T sx = (T(1.0) - T(t_)) * c_i[0] + T(t_) * c_ip1[0];
214 T sy = (T(1.0) - T(t_)) * c_i[1] + T(t_) * c_ip1[1];
215
216 T d_raw;
217 wrapper_(&sx, &sy, &d_raw);
218
219 if (!ceres::isfinite(d_raw))
220 {
221 residual[0] = T(w_) * T(100.0);
222 return true;
223 }
224
225 T obs_dist = d_raw - T(clearance_);
226
227 if (obs_dist > T(max_dist_))
228 {
229 residual[0] = T(0.0);
230 }
231 else if (obs_dist < T(0.0))
232 {
233 T penetration = -obs_dist;
234 const T max_pen = T(3.0) * T(max_dist_);
235 if (penetration > max_pen)
236 penetration = max_pen;
237 T ratio = penetration / T(max_dist_);
238 residual[0] = T(w_) * (ceres::exp(T(2.0) * ratio) - T(1.0) + T(0.1) * ratio);
239 }
240 else
241 {
242 residual[0] = T(w_) * (T(max_dist_) - obs_dist) / T(max_dist_);
243 }
244 return true;
245 }
246
248 double w_;
249 double max_dist_;
251 double t_;
252 };
253
254 // -----------------------------
255 // Tracking residuals
256 // -----------------------------
257
258 /**
259 * Bounded tracking residual.
260 *
261 * - Deviation <= deadzone: 0 cost.
262 * - deadzone < deviation <= max_deviation: quadratic growth.
263 * - deviation > max_deviation: very steep quadratic growth (hard limit).
264 */
266 {
268 double yr,
269 double deadzone,
270 double max_deviation,
271 double w_in_bounds,
272 double w_hard) :
273 xr_(xr),
274 yr_(yr),
275 deadzone_(deadzone),
276 max_deviation_(max_deviation),
277 w_in_bounds_(w_in_bounds),
278 w_hard_(w_hard)
279 {
280 }
281
282 template <typename T>
283 bool operator()(const T* const c_i, T* residual) const
284 {
285 T dx = c_i[0] - T(xr_);
286 T dy = c_i[1] - T(yr_);
287 T d = ceres::sqrt(dx * dx + dy * dy + T(1e-6));
288
289 T excess = d - T(deadzone_);
290 if (excess <= T(0))
291 {
292 residual[0] = T(0);
293 }
294 else if (excess <= T(max_deviation_ - deadzone_))
295 {
296 residual[0] = T(w_in_bounds_) * excess;
297 }
298 else
299 {
300 T over = excess - T(max_deviation_ - deadzone_);
301 residual[0] = T(w_in_bounds_) * T(max_deviation_ - deadzone_) + T(w_hard_) * over;
302 }
303 return true;
304 }
305
306 double xr_, yr_;
307 double deadzone_;
310 double w_hard_;
311 };
312
313 // Soft tracking to the hill-climbed safe path (weak weight).
315 {
316 SoftTrackingResidual(double xr, double yr, double weight) :
317 xr_(xr), yr_(yr), w_(weight)
318 {
319 }
320
321 template <typename T>
322 bool operator()(const T* const c_i, T* residual) const
323 {
324 residual[0] = T(w_) * (c_i[0] - T(xr_));
325 residual[1] = T(w_) * (c_i[1] - T(yr_));
326 return true;
327 }
328
329 double xr_, yr_;
330 double w_;
331 };
332
333 // -----------------------------
334 // Spacing residual
335 // -----------------------------
337 {
338 SpacingResidual(double d_avg, double weight) : d_avg_(d_avg), w(weight) {}
339
340 template <typename T>
341 bool operator()(const T* const c_i, const T* const c_ip1, T* residual) const
342 {
343 T dx = c_ip1[0] - c_i[0];
344 T dy = c_ip1[1] - c_i[1];
345 T dist_sq = dx * dx + dy * dy;
346 T target_sq = T(d_avg_ * d_avg_);
347 residual[0] = T(w) * (dist_sq - target_sq);
348 return true;
349 }
350
351 double d_avg_;
352 double w;
353 };
354
355 // -----------------------------
356 // Helpers
357 // -----------------------------
358 static double segment_length(const CenterPoint& a, const CenterPoint& b)
359 {
360 return std::hypot(b.x - a.x, b.y - a.y);
361 }
362
363 static double computeAverageSpacing(const std::vector<CenterPoint>& traj)
364 {
365 if (traj.size() < 2)
366 return 0.0;
367 double sum = 0.0;
368 for (size_t i = 0; i + 1 < traj.size(); ++i)
369 sum += segment_length(traj[i], traj[i + 1]);
370 return sum / double(traj.size() - 1);
371 }
372
373} // namespace armarx::navigation::algorithms::spfa::smoothing
Differentiable wrapper around the standard 2-D distance-to-obstacle costmap.
This file offers overloads of toIce() and fromIce() functions for STL container types.
ceres::SubsetParameterization SubsetConstraint
This file is part of ArmarX.
Definition residuals.h:22
BoundedTrackingResidual(double xr, double yr, double deadzone, double max_deviation, double w_in_bounds, double w_hard)
Definition residuals.h:267
MidpointObstacleResidual2D(const Costmap2DWrapper &wrapper, double weight, double max_dist, double clearance)
Definition residuals.h:139
bool operator()(const T *const c_i, const T *const c_ip1, T *residual) const
Definition residuals.h:151
bool operator()(const T *const node, T *residual) const
Definition residuals.h:98
ObstacleResidual2D(const Costmap2DWrapper &wrapper, double weight, double max_dist, double clearance)
Definition residuals.h:86
bool operator()(const T *const c_prev, const T *const c_i, const T *const c_next, T *residual) const
Definition residuals.h:68
bool operator()(const T *const c_i, const T *const c_ip1, T *residual) const
Definition residuals.h:211
SegmentObstacleResidual2D(const Costmap2DWrapper &wrapper, double weight, double max_dist, double clearance, double t)
Definition residuals.h:197
bool operator()(const T *const c_i, T *residual) const
Definition residuals.h:322
bool operator()(const T *const c_i, const T *const c_ip1, T *residual) const
Definition residuals.h:341