2 * SPDX-FileCopyrightText: 2008-2018 Andrew Walker
4 * SPDX-License-Identifier: MIT
8 #define _USE_MATH_DEFINES
13 #define EPSILON (10e-10)
22 /* The segment types for each of the Path types */
23 const SegmentType DIRDATA[][3] = {
24 { L_SEG, S_SEG, L_SEG },
25 { L_SEG, S_SEG, R_SEG },
26 { R_SEG, S_SEG, L_SEG },
27 { R_SEG, S_SEG, R_SEG },
28 { R_SEG, L_SEG, R_SEG },
29 { L_SEG, R_SEG, L_SEG }
43 } DubinsIntermediateResults;
46 int dubins_word(DubinsIntermediateResults* in, DubinsPathType pathType, double out[3]);
47 int dubins_intermediate_results(DubinsIntermediateResults* in, double q0[3], double q1[3], double rho);
50 * Floating point modulus suitable for rings
52 * fmod doesn't behave correctly for angular quantities, this function does
54 double fmodr( double x, double y)
56 return x - y*floor(x/y);
59 double mod2pi( double theta )
61 return fmodr( theta, 2 * M_PI );
64 int dubins_shortest_path(DubinsPath* path, double q0[3], double q1[3], double rho)
67 DubinsIntermediateResults in;
70 double best_cost = INFINITY;
72 errcode = dubins_intermediate_results(&in, q0, q1, rho);
73 if(errcode != EDUBOK) {
83 for( i = 0; i < 6; i++ ) {
84 DubinsPathType pathType = (DubinsPathType)i;
85 errcode = dubins_word(&in, pathType, params);
86 if(errcode == EDUBOK) {
87 cost = params[0] + params[1] + params[2];
88 if(cost < best_cost) {
91 path->param[0] = params[0];
92 path->param[1] = params[1];
93 path->param[2] = params[2];
94 path->type = pathType;
104 int dubins_path(DubinsPath* path, double q0[3], double q1[3], double rho, DubinsPathType pathType)
107 DubinsIntermediateResults in;
108 errcode = dubins_intermediate_results(&in, q0, q1, rho);
109 if(errcode == EDUBOK) {
111 errcode = dubins_word(&in, pathType, params);
112 if(errcode == EDUBOK) {
113 path->param[0] = params[0];
114 path->param[1] = params[1];
115 path->param[2] = params[2];
120 path->type = pathType;
126 double dubins_path_length( DubinsPath* path )
129 length += path->param[0];
130 length += path->param[1];
131 length += path->param[2];
132 length = length * path->rho;
136 double dubins_segment_length( DubinsPath* path, int i )
138 if( (i < 0) || (i > 2) )
142 return path->param[i] * path->rho;
145 double dubins_segment_length_normalized( DubinsPath* path, int i )
147 if( (i < 0) || (i > 2) )
151 return path->param[i];
154 DubinsPathType dubins_path_type( DubinsPath* path )
159 void dubins_segment( double t, double qi[3], double qt[3], SegmentType type)
161 double st = sin(qi[2]);
162 double ct = cos(qi[2]);
163 if( type == L_SEG ) {
164 qt[0] = +sin(qi[2]+t) - st;
165 qt[1] = -cos(qi[2]+t) + ct;
168 else if( type == R_SEG ) {
169 qt[0] = -sin(qi[2]-t) + st;
170 qt[1] = +cos(qi[2]-t) - ct;
173 else if( type == S_SEG ) {
183 int dubins_path_sample( DubinsPath* path, double t, double q[3] )
185 /* tprime is the normalised variant of the parameter t */
186 double tprime = t / path->rho;
187 double qi[3]; /* The translated initial configuration */
188 double q1[3]; /* end-of segment 1 */
189 double q2[3]; /* end-of segment 2 */
190 const SegmentType* types = DIRDATA[path->type];
193 if( t < 0 || t > dubins_path_length(path) ) {
197 /* initial configuration */
202 /* generate the target configuration */
205 dubins_segment( p1, qi, q1, types[0] );
206 dubins_segment( p2, q1, q2, types[1] );
208 dubins_segment( tprime, qi, q, types[0] );
210 else if( tprime < (p1+p2) ) {
211 dubins_segment( tprime-p1, q1, q, types[1] );
214 dubins_segment( tprime-p1-p2, q2, q, types[2] );
217 /* scale the target configuration, translate back to the original starting point */
218 q[0] = q[0] * path->rho + path->qi[0];
219 q[1] = q[1] * path->rho + path->qi[1];
225 int dubins_path_sample_many(DubinsPath* path, double stepSize,
226 DubinsPathSamplingCallback cb, void* user_data)
231 double length = dubins_path_length(path);
232 while( x < length ) {
233 dubins_path_sample( path, x, q );
234 retcode = cb(q, x, user_data);
243 int dubins_path_endpoint( DubinsPath* path, double q[3] )
245 return dubins_path_sample( path, dubins_path_length(path) - EPSILON, q );
248 int dubins_extract_subpath( DubinsPath* path, double t, DubinsPath* newpath )
250 /* calculate the true parameter */
251 double tprime = t / path->rho;
253 if((t < 0) || (t > dubins_path_length(path)))
258 /* copy most of the data */
259 newpath->qi[0] = path->qi[0];
260 newpath->qi[1] = path->qi[1];
261 newpath->qi[2] = path->qi[2];
262 newpath->rho = path->rho;
263 newpath->type = path->type;
265 /* fix the parameters */
266 newpath->param[0] = fmin( path->param[0], tprime );
267 newpath->param[1] = fmin( path->param[1], tprime - newpath->param[0]);
268 newpath->param[2] = fmin( path->param[2], tprime - newpath->param[0] - newpath->param[1]);
272 int dubins_intermediate_results(DubinsIntermediateResults* in, double q0[3], double q1[3], double rho)
274 double dx, dy, D, d, theta, alpha, beta;
281 D = sqrt( dx * dx + dy * dy );
285 /* test required to prevent domain errors if dx=0 and dy=0 */
287 theta = mod2pi(atan2( dy, dx ));
289 alpha = mod2pi(q0[2] - theta);
290 beta = mod2pi(q1[2] - theta);
299 in->c_ab = cos(alpha - beta);
305 int dubins_LSL(DubinsIntermediateResults* in, double out[3])
307 double tmp0, tmp1, p_sq;
309 tmp0 = in->d + in->sa - in->sb;
310 p_sq = 2 + in->d_sq - (2*in->c_ab) + (2 * in->d * (in->sa - in->sb));
313 tmp1 = atan2( (in->cb - in->ca), tmp0 );
314 out[0] = mod2pi(tmp1 - in->alpha);
316 out[2] = mod2pi(in->beta - tmp1);
323 int dubins_RSR(DubinsIntermediateResults* in, double out[3])
325 double tmp0 = in->d - in->sa + in->sb;
326 double p_sq = 2 + in->d_sq - (2 * in->c_ab) + (2 * in->d * (in->sb - in->sa));
328 double tmp1 = atan2( (in->ca - in->cb), tmp0 );
329 out[0] = mod2pi(in->alpha - tmp1);
331 out[2] = mod2pi(tmp1 -in->beta);
337 int dubins_LSR(DubinsIntermediateResults* in, double out[3])
339 double p_sq = -2 + (in->d_sq) + (2 * in->c_ab) + (2 * in->d * (in->sa + in->sb));
341 double p = sqrt(p_sq);
342 double tmp0 = atan2( (-in->ca - in->cb), (in->d + in->sa + in->sb) ) - atan2(-2.0, p);
343 out[0] = mod2pi(tmp0 - in->alpha);
345 out[2] = mod2pi(tmp0 - mod2pi(in->beta));
351 int dubins_RSL(DubinsIntermediateResults* in, double out[3])
353 double p_sq = -2 + in->d_sq + (2 * in->c_ab) - (2 * in->d * (in->sa + in->sb));
355 double p = sqrt(p_sq);
356 double tmp0 = atan2( (in->ca + in->cb), (in->d - in->sa - in->sb) ) - atan2(2.0, p);
357 out[0] = mod2pi(in->alpha - tmp0);
359 out[2] = mod2pi(in->beta - tmp0);
365 int dubins_RLR(DubinsIntermediateResults* in, double out[3])
367 double tmp0 = (6. - in->d_sq + 2*in->c_ab + 2*in->d*(in->sa - in->sb)) / 8.;
368 double phi = atan2( in->ca - in->cb, in->d - in->sa + in->sb );
369 if( fabs(tmp0) <= 1) {
370 double p = mod2pi((2*M_PI) - acos(tmp0) );
371 double t = mod2pi(in->alpha - phi + mod2pi(p/2.));
374 out[2] = mod2pi(in->alpha - in->beta - t + mod2pi(p));
380 int dubins_LRL(DubinsIntermediateResults* in, double out[3])
382 double tmp0 = (6. - in->d_sq + 2*in->c_ab + 2*in->d*(in->sb - in->sa)) / 8.;
383 double phi = atan2( in->ca - in->cb, in->d + in->sa - in->sb );
384 if( fabs(tmp0) <= 1) {
385 double p = mod2pi( 2*M_PI - acos( tmp0) );
386 double t = mod2pi(-in->alpha - phi + p/2.);
389 out[2] = mod2pi(mod2pi(in->beta) - in->alpha -t + mod2pi(p));
395 int dubins_word(DubinsIntermediateResults* in, DubinsPathType pathType, double out[3])
401 result = dubins_LSL(in, out);
404 result = dubins_RSL(in, out);
407 result = dubins_LSR(in, out);
410 result = dubins_RSR(in, out);
413 result = dubins_LRL(in, out);
416 result = dubins_RLR(in, out);