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 
24 #include <ceres/ceres.h>
25 
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 
44  bool
45  Evaluate(double const* const* parameters,
46  double* residuals,
47  double** jacobians) const override
48  {
49  const double yaw = parameters[0][0];
50  const double yawNext = parameters[0][1];
51 
52  residuals[0] = weight * periodicDiff(yawPre, yaw);
53  residuals[1] = weight * periodicDiff(yaw, yawNext);
54 
55  const auto periodicDiffJ0 = periodicDiffJacobian(yawPre, yaw);
56  const auto periodicDiffJ1 = periodicDiffJacobian(yaw, yawNext);
57 
58  if (jacobians == nullptr)
59  {
60  return true;
61  }
62  double* jacobian0 = jacobians[0];
63  if (jacobian0 == nullptr)
64  {
65  return true;
66  }
67 
68  double* jacobian1 = jacobians[1];
69  if (jacobian1 == nullptr)
70  {
71  return true;
72  }
73 
74 
75  // d r_i / d p_0
76  jacobian0[0] = weight * periodicDiffJ0.at(1);
77  jacobian0[1] = weight * periodicDiffJ1.at(0);
78  // jacobian0[2] = weight * periodicDiffJ1.at(0);
79 
80  // d r_i / d p_1
81  jacobian1[0] = 0.;
82  jacobian1[1] = weight * periodicDiffJ1.at(1);
83 
84  return true;
85  }
86 
87  private:
88  const double yawPre;
89  const double weight;
90  };
91 
92  struct SmoothOrientationFixedNextCostFunctor : ceres::CostFunction
93  {
94 
95  SmoothOrientationFixedNextCostFunctor(const double yawNext, const double weight) :
96  yawNext(yawNext), weight(weight)
97  {
98  set_num_residuals(2);
99 
100  mutable_parameter_block_sizes()->push_back(1);
101  mutable_parameter_block_sizes()->push_back(1);
102  }
103 
104 
105  bool
106  Evaluate(double const* const* parameters,
107  double* residuals,
108  double** jacobians) const override
109  {
110  const double yawPre = parameters[0][0];
111  const double yaw = parameters[0][1];
112 
113  residuals[0] = weight * periodicDiff(yawPre, yaw);
114  residuals[1] = weight * periodicDiff(yaw, yawNext);
115 
116  const auto periodicDiffJ0 = periodicDiffJacobian(yawPre, yaw);
117  const auto periodicDiffJ1 = periodicDiffJacobian(yaw, yawNext);
118 
119  if (jacobians == nullptr)
120  {
121  return true;
122  }
123  double* jacobian0 = jacobians[0];
124  if (jacobian0 == nullptr)
125  {
126  return true;
127  }
128 
129  double* jacobian1 = jacobians[1];
130  if (jacobian1 == nullptr)
131  {
132  return true;
133  }
134 
135  // d r_i / d p_0
136  jacobian0[0] = weight * periodicDiffJ0.at(0);
137  jacobian0[1] = 0;
138 
139  // d r_i / d p_1
140  jacobian1[0] = weight * periodicDiffJ0.at(1);
141  jacobian1[1] = weight * periodicDiffJ1.at(0);
142 
143  return true;
144  }
145 
146  private:
147  const double yawNext;
148  const double weight;
149  };
150 
151  struct OrientationPriorCostFunctor : ceres::SizedCostFunction<1, 1>
152  {
153 
154  OrientationPriorCostFunctor(const double prior, const double weight) :
155  prior(prior), weight(weight)
156  {
157  }
158 
159  bool
160  Evaluate(double const* const* parameters,
161  double* residuals,
162  double** jacobians) const override
163  {
164  const double yaw = parameters[0][0];
165 
166  residuals[0] = weight * periodicDiff(prior, yaw);
167 
168  const auto periodicDiffJ = periodicDiffJacobian(prior, yaw);
169 
170  if (jacobians == nullptr)
171  {
172  return true;
173  }
174  double* jacobian = jacobians[0];
175  if (jacobian == nullptr)
176  {
177  return true;
178  }
179 
180 
181  jacobian[0] = weight * periodicDiffJ.at(1);
182  return true;
183  }
184 
185  private:
186  const double prior;
187  const double weight;
188  };
189 
190 
191  struct SmoothOrientationCostFunctor : ceres::CostFunction
192  {
193 
194  SmoothOrientationCostFunctor(const double weight) : weight(weight)
195  {
196  set_num_residuals(2);
197 
198  mutable_parameter_block_sizes()->push_back(1);
199  mutable_parameter_block_sizes()->push_back(1);
200  mutable_parameter_block_sizes()->push_back(1);
201  }
202 
203  bool
204  Evaluate(double const* const* parameters,
205  double* residuals,
206  double** jacobians) const override
207  {
208  const double yawPrev = parameters[0][0];
209  const double yaw = parameters[0][1];
210  const double yawNext = parameters[0][2];
211 
212  residuals[0] = weight * periodicDiff(yawPrev, yaw);
213  residuals[1] = weight * periodicDiff(yaw, yawNext);
214 
215  const auto periodicDiffJ0 = periodicDiffJacobian(yawPrev, yaw);
216  const auto periodicDiffJ1 = periodicDiffJacobian(yaw, yawNext);
217 
218  if (jacobians == nullptr)
219  {
220  return true;
221  }
222 
223  double* jacobian0 = jacobians[0];
224  if (jacobian0 == nullptr)
225  {
226  return true;
227  }
228 
229  double* jacobian1 = jacobians[1];
230  if (jacobian1 == nullptr)
231  {
232  return true;
233  }
234 
235  double* jacobian2 = jacobians[2];
236  if (jacobian2 == nullptr)
237  {
238  return true;
239  }
240 
241  // d r_i / d p_0
242  jacobian0[0] = weight * periodicDiffJ0.at(0);
243  jacobian0[1] = 0.;
244 
245  // d r_i / d p_2
246  jacobian1[0] = weight * periodicDiffJ0.at(1);
247  jacobian1[1] = weight * periodicDiffJ1.at(0);
248 
249  // d r_i / d p_1
250  jacobian2[0] = 0.;
251  jacobian2[1] = weight * periodicDiffJ1.at(1);
252 
253  return true;
254  }
255 
256  private:
257  const double weight;
258  };
259 
260 } // namespace armarx::navigation::global_planning::optimization
armarx::navigation::global_planning::optimization
This file is part of ArmarX.
Definition: aron_conversions.h:12
armarx::navigation::global_planning::optimization::OrientationPriorCostFunctor::Evaluate
bool Evaluate(double const *const *parameters, double *residuals, double **jacobians) const override
Definition: cost_functions.h:160
armarx::navigation::global_planning::optimization::SmoothOrientationFixedPreCostFunctor::SmoothOrientationFixedPreCostFunctor
SmoothOrientationFixedPreCostFunctor(const double yawPre, const double weight)
Definition: cost_functions.h:34
armarx::navigation::global_planning::optimization::SmoothOrientationFixedPreCostFunctor::Evaluate
bool Evaluate(double const *const *parameters, double *residuals, double **jacobians) const override
Definition: cost_functions.h:45
armarx::navigation::global_planning::optimization::periodicDiffJacobian
std::array< double, 2 > periodicDiffJacobian(const auto a, const auto b)
Definition: math.h:50
armarx::navigation::global_planning::optimization::periodicDiff
auto periodicDiff(const auto a, const auto b)
Definition: math.h:32
math.h
armarx::navigation::global_planning::optimization::SmoothOrientationCostFunctor
Definition: cost_functions.h:191
armarx::navigation::global_planning::optimization::SmoothOrientationFixedNextCostFunctor
Definition: cost_functions.h:92
armarx::navigation::global_planning::optimization::SmoothOrientationFixedPreCostFunctor
Definition: cost_functions.h:31
armarx::navigation::global_planning::optimization::SmoothOrientationFixedNextCostFunctor::SmoothOrientationFixedNextCostFunctor
SmoothOrientationFixedNextCostFunctor(const double yawNext, const double weight)
Definition: cost_functions.h:95
armarx::navigation::global_planning::optimization::OrientationPriorCostFunctor
Definition: cost_functions.h:151
armarx::navigation::global_planning::optimization::SmoothOrientationCostFunctor::Evaluate
bool Evaluate(double const *const *parameters, double *residuals, double **jacobians) const override
Definition: cost_functions.h:204
armarx::navigation::global_planning::optimization::SmoothOrientationCostFunctor::SmoothOrientationCostFunctor
SmoothOrientationCostFunctor(const double weight)
Definition: cost_functions.h:194
armarx::navigation::global_planning::optimization::SmoothOrientationFixedNextCostFunctor::Evaluate
bool Evaluate(double const *const *parameters, double *residuals, double **jacobians) const override
Definition: cost_functions.h:106
armarx::navigation::global_planning::optimization::OrientationPriorCostFunctor::OrientationPriorCostFunctor
OrientationPriorCostFunctor(const double prior, const double weight)
Definition: cost_functions.h:154