4 // kinematic constraints
5 bool BicycleCar::drivable(const BicycleCar &bc) const
7 double a_1 = atan2(bc.y() - this->y(), bc.x() - this->x()) - this->h();
12 double h_d = bc.h() - this->h();
18 if (h_d == 0 && (a_1 == 0 || a_2 == M_PI || a_2 == -M_PI)) {
20 } else if (0 < a_1 && a_1 <= M_PI/2) { // left front
21 BicycleCar z(*this); // zone border
22 z.rotate(this->ccl().x(), this->ccl().y(), h_d);
23 // assert z.h() == bc.h()
24 if (bc.y() == z.y() && bc.x() == z.x()) // bc on zone border
26 a_2 = atan2(bc.y() - z.y(), bc.x() - z.x());
31 if (z.h() >= a_2 && a_2 >= this->h())
33 } else if (M_PI/2 < a_1 && a_1 <= M_PI) { // left rear
34 BicycleCar z(*this); // zone border
35 z.rotate(this->ccl().x(), this->ccl().y(), h_d);
36 // assert z.h() == bc.h()
37 if (bc.y() == z.y() && bc.x() == z.x()) // bc on zone border
39 a_2 = atan2(bc.y() - z.y(), bc.x() - z.x());
45 if (this->h() >= a_2 && a_2 >= z.h())
47 } else if (0 > a_1 && a_1 >= -M_PI/2) { // right front
48 BicycleCar z(*this); // zone border
49 z.rotate(this->ccr().x(), this->ccr().y(), h_d);
50 // assert z.h() == bc.h()
51 if (bc.y() == z.y() && bc.x() == z.x()) // bc on zone border
53 a_2 = atan2(bc.y() - z.y(), bc.x() - z.x());
58 if (this->h() >= a_2 && a_2 >= z.h())
60 } else if (-M_PI/2 > a_1 && a_1 >= -M_PI) { // right rear
61 BicycleCar z(*this); // zone border
62 z.rotate(this->ccr().x(), this->ccr().y(), h_d);
63 // assert z.h() == bc.h()
64 if (bc.y() == z.y() && bc.x() == z.x()) // bc on zone border
66 a_2 = atan2(bc.y() - z.y(), bc.x() - z.x());
72 if (z.h() >= a_2 && a_2 >= this->h())
75 // Not happenning, as ``-pi <= a <= pi``.
80 double BicycleCar::iradi() const
82 return this->mtr() - this->w() / 2;
85 double BicycleCar::ofradi() const
87 return sqrt(pow(this->mtr() + this->w() / 2, 2) + pow(this->df(), 2));
90 double BicycleCar::orradi() const
92 return sqrt(pow(this->mtr() + this->w() / 2, 2) + pow(this->dr(), 2));
95 double BicycleCar::perfect_parking_slot_len() const
97 // see Simon R. Blackburn *The Geometry of Perfect Parking*
98 // see https://www.ma.rhul.ac.uk/SRBparking
99 double r = this->ctc() / 2;
100 double l = this->wb();
101 double k = this->df() - this->wb();
102 double w = this->w();
108 - pow(sqrt(r*r - l*l) - w, 2)
115 void BicycleCar::set_max_steer()
117 this->st(atan(this->wb() / this->mtr()));
121 double BicycleCar::lfx() const
123 double lfx = this->x();
124 lfx += (this->w() / 2) * cos(this->h() + M_PI / 2);
125 lfx += this->df() * cos(this->h());
126 lfx += this->sd() * cos(this->h());
130 double BicycleCar::lfy() const
132 double lfy = this->y();
133 lfy += (this->w() / 2) * sin(this->h() + M_PI / 2);
134 lfy += this->df() * sin(this->h());
135 lfy += this->sd() * sin(this->h());
139 double BicycleCar::lrx() const
141 double lrx = this->x();
142 lrx += (this->w() / 2) * cos(this->h() + M_PI / 2);
143 lrx += -this->dr() * cos(this->h());
144 lrx += -this->sd() * cos(this->h());
148 double BicycleCar::lry() const
150 double lry = this->y();
151 lry += (this->w() / 2) * sin(this->h() + M_PI / 2);
152 lry += -this->dr() * sin(this->h());
153 lry += -this->sd() * sin(this->h());
157 double BicycleCar::rrx() const
159 double rrx = this->x();
160 rrx += (this->w() / 2) * cos(this->h() - M_PI / 2);
161 rrx += -this->dr() * cos(this->h());
162 rrx += -this->sd() * cos(this->h());
166 double BicycleCar::rry() const
168 double rry = this->y();
169 rry += (this->w() / 2) * sin(this->h() - M_PI / 2);
170 rry += -this->dr() * sin(this->h());
171 rry += -this->sd() * sin(this->h());
175 double BicycleCar::rfx() const
177 double rfx = this->x();
178 rfx += (this->w() / 2) * cos(this->h() - M_PI / 2);
179 rfx += this->df() * cos(this->h());
180 rfx += this->sd() * cos(this->h());
184 double BicycleCar::rfy() const
186 double rfy = this->y();
187 rfy += (this->w() / 2) * sin(this->h() - M_PI / 2);
188 rfy += this->df() * sin(this->h());
189 rfy += this->sd() * sin(this->h());
193 double BicycleCar::ralx() const
195 double lrx = this->x();
196 lrx += (this->w() / 2) * cos(this->h() + M_PI / 2);
199 double BicycleCar::raly() const
201 double lry = this->y();
202 lry += (this->w() / 2) * sin(this->h() + M_PI / 2);
206 double BicycleCar::rarx() const
208 double rrx = this->x();
209 rrx += (this->w() / 2) * cos(this->h() - M_PI / 2);
213 double BicycleCar::rary() const
215 double rry = this->y();
216 rry += (this->w() / 2) * sin(this->h() - M_PI / 2);
220 BicycleCar BicycleCar::ccl() const
223 bc.x(this->x() + this->mtr() * cos(this->h() + M_PI / 2));
224 bc.y(this->y() + this->mtr() * sin(this->h() + M_PI / 2));
229 BicycleCar BicycleCar::ccr() const
232 bc.x(this->x() + this->mtr() * cos(this->h() - M_PI / 2));
233 bc.y(this->y() + this->mtr() * sin(this->h() - M_PI / 2));
239 void BicycleCar::next()
241 this->x(this->x() + this->sp() * cos(this->h()));
242 this->y(this->y() + this->sp() * sin(this->h()));
243 this->h(this->h() + this->sp() / this->wb() * tan(this->st()));
246 void BicycleCar::rotate(double cx, double cy, double angl)
248 double px = this->x();
249 double py = this->y();
252 double nx = px * cos(angl) - py * sin(angl);
253 double ny = px * sin(angl) + py * cos(angl);
254 this->h(this->h() + angl);
259 BicycleCar::BicycleCar()
261 // TODO according to mtr_ FIXME
270 std::tuple<bool, unsigned int, unsigned int>
272 std::vector<std::tuple<double, double>> &p1,
273 std::vector<std::tuple<double, double>> &p2
276 for (unsigned int i = 0; i < p1.size() - 1; i++) {
277 for (unsigned int j = 0; j < p2.size() - 1; j++) {
281 std::get<0>(p1[i + 1]),
282 std::get<1>(p1[i + 1]),
285 std::get<0>(p2[j + 1]),
286 std::get<1>(p2[j + 1])
289 return std::make_tuple(true, i, j);
292 return std::make_tuple(false, 0, 0);
296 inside(double x, double y, std::vector<std::tuple<double, double>> &poly)
301 for (i = 0; i < 4; i++) {
303 (std::get<1>(poly[i]) > y) != (std::get<1>(poly[j]) > y)
305 x < std::get<0>(poly[i])
306 + (std::get<0>(poly[j]) - std::get<0>(poly[i]))
307 * (y - std::get<1>(poly[i]))
308 / (std::get<1>(poly[j]) - std::get<1>(poly[i]))
317 std::tuple<bool, double, double>
319 double x1, double y1,
320 double x2, double y2,
321 double x3, double y3,
325 double deno = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
327 return std::make_tuple(false, 0, 0);
328 double t = (x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4);
330 double u = (x1 - x2) * (y1 - y3) - (y1 - y2) * (x1 - x3);
333 if (t < 0 || t > 1 || u < 0 || u > 1)
334 return std::make_tuple(false, 0, 0);
335 return std::make_tuple(true, x1 + t * (x2 - x1), y1 + t * (y2 - y1));
338 std::tuple<bool, double, double, double, double>
340 double cx, double cy, double r,
341 double x1, double y1,
350 double dr = sqrt(dx*dx + dy*dy);
351 double D = x1*y2 - x2*y1;
352 if (r*r * dr*dr - D*D < 0)
353 return std::make_tuple(false, 0, 0, 0, 0);
354 // intersection coordinates
355 double ix1 = (D*dy + sgn(dy)*dx*sqrt(r*r * dr*dr - D*D)) / (dr*dr);
356 double ix2 = (D*dy - sgn(dy)*dx*sqrt(r*r * dr*dr - D*D)) / (dr*dr);
357 double iy1 = (-D*dx + std::abs(dy)*sqrt(r*r * dr*dr - D*D)) / (dr*dr);
358 double iy2 = (-D*dx - std::abs(dy)*sqrt(r*r * dr*dr - D*D)) / (dr*dr);
359 return std::make_tuple(true, ix1, iy1, ix2, iy2);
363 angle_between_three_points(
364 double x1, double y1,
365 double x2, double y2,
368 double d1x = x2 - x1;
369 double d1y = y2 - y1;
370 double d2x = x3 - x2;
371 double d2y = y3 - y2;
373 double dot = d1x*d2x + d1y*d2y;
374 double d1 = sqrt(d1x*d1x + d1y*d1y);
375 double d2 = sqrt(d2x*d2x + d2y*d2y);
377 double delta = acos(dot / (d1 * d2));
378 return std::min(delta, M_PI - delta);
383 double x1, double y1,
384 double x2, double y2,
387 if (sgn((x3 - x1) * (y2 - y1) - (y3 - y1) * (x2 - x1)) < 0)