4 // kinematic constraints
5 bool BicycleCar::drivable(const BicycleCar &bc) const
7 return this->drivable(bc, bc.h(), bc.h());
9 bool BicycleCar::drivable(const BicycleCar &bc, double b, double e) const
11 // assert bc.h() == (b + e) / 2.0
12 double a_1 = atan2(bc.y() - this->y(), bc.x() - this->x()) - this->h();
17 double h_d = bc.h() - this->h();
23 if (h_d == 0 && (a_1 == 0 || a_2 == M_PI || a_2 == -M_PI)) {
25 } else if (0 < a_1 && a_1 <= M_PI/2) { // left front
26 BicycleCar z(*this); // zone border
27 z.rotate(this->ccl().x(), this->ccl().y(), h_d);
28 // assert z.h() == bc.h()
29 if (bc.y() == z.y() && bc.x() == z.x()) // bc on zone border
31 a_2 = atan2(bc.y() - z.y(), bc.x() - z.x());
36 if (z.h() >= a_2 && a_2 >= this->h())
38 } else if (M_PI/2 < a_1 && a_1 <= M_PI) { // left rear
39 BicycleCar z(*this); // zone border
40 z.rotate(this->ccl().x(), this->ccl().y(), h_d);
41 // assert z.h() == bc.h()
42 if (bc.y() == z.y() && bc.x() == z.x()) // bc on zone border
44 a_2 = atan2(bc.y() - z.y(), bc.x() - z.x());
50 if (this->h() >= a_2 && a_2 >= z.h())
52 } else if (0 > a_1 && a_1 >= -M_PI/2) { // right front
53 BicycleCar z(*this); // zone border
54 z.rotate(this->ccr().x(), this->ccr().y(), h_d);
55 // assert z.h() == bc.h()
56 if (bc.y() == z.y() && bc.x() == z.x()) // bc on zone border
58 a_2 = atan2(bc.y() - z.y(), bc.x() - z.x());
63 if (this->h() >= a_2 && a_2 >= z.h())
65 } else if (-M_PI/2 > a_1 && a_1 >= -M_PI) { // right rear
66 BicycleCar z(*this); // zone border
67 z.rotate(this->ccr().x(), this->ccr().y(), h_d);
68 // assert z.h() == bc.h()
69 if (bc.y() == z.y() && bc.x() == z.x()) // bc on zone border
71 a_2 = atan2(bc.y() - z.y(), bc.x() - z.x());
77 if (z.h() >= a_2 && a_2 >= this->h())
80 // Not happenning, as ``-pi <= a <= pi``.
85 double BicycleCar::iradi() const
87 return this->mtr() - this->w() / 2;
90 double BicycleCar::ofradi() const
92 return sqrt(pow(this->mtr() + this->w() / 2, 2) + pow(this->df(), 2));
95 double BicycleCar::orradi() const
97 return sqrt(pow(this->mtr() + this->w() / 2, 2) + pow(this->dr(), 2));
100 double BicycleCar::perfect_parking_slot_len() const
102 // see Simon R. Blackburn *The Geometry of Perfect Parking*
103 // see https://www.ma.rhul.ac.uk/SRBparking
104 double r = this->ctc() / 2;
105 double l = this->wb();
106 double k = this->df() - this->wb();
107 double w = this->w();
113 - pow(sqrt(r*r - l*l) - w, 2)
120 void BicycleCar::set_max_steer()
122 this->st(atan(this->wb() / this->mtr()));
126 double BicycleCar::lfx() const
128 double lfx = this->x();
129 lfx += (this->w() / 2) * cos(this->h() + M_PI / 2);
130 lfx += this->df() * cos(this->h());
131 lfx += this->sd() * cos(this->h());
135 double BicycleCar::lfy() const
137 double lfy = this->y();
138 lfy += (this->w() / 2) * sin(this->h() + M_PI / 2);
139 lfy += this->df() * sin(this->h());
140 lfy += this->sd() * sin(this->h());
144 double BicycleCar::lrx() const
146 double lrx = this->x();
147 lrx += (this->w() / 2) * cos(this->h() + M_PI / 2);
148 lrx += -this->dr() * cos(this->h());
149 lrx += -this->sd() * cos(this->h());
153 double BicycleCar::lry() const
155 double lry = this->y();
156 lry += (this->w() / 2) * sin(this->h() + M_PI / 2);
157 lry += -this->dr() * sin(this->h());
158 lry += -this->sd() * sin(this->h());
162 double BicycleCar::rrx() const
164 double rrx = this->x();
165 rrx += (this->w() / 2) * cos(this->h() - M_PI / 2);
166 rrx += -this->dr() * cos(this->h());
167 rrx += -this->sd() * cos(this->h());
171 double BicycleCar::rry() const
173 double rry = this->y();
174 rry += (this->w() / 2) * sin(this->h() - M_PI / 2);
175 rry += -this->dr() * sin(this->h());
176 rry += -this->sd() * sin(this->h());
180 double BicycleCar::rfx() const
182 double rfx = this->x();
183 rfx += (this->w() / 2) * cos(this->h() - M_PI / 2);
184 rfx += this->df() * cos(this->h());
185 rfx += this->sd() * cos(this->h());
189 double BicycleCar::rfy() const
191 double rfy = this->y();
192 rfy += (this->w() / 2) * sin(this->h() - M_PI / 2);
193 rfy += this->df() * sin(this->h());
194 rfy += this->sd() * sin(this->h());
198 double BicycleCar::ralx() const
200 double lrx = this->x();
201 lrx += (this->w() / 2) * cos(this->h() + M_PI / 2);
204 double BicycleCar::raly() const
206 double lry = this->y();
207 lry += (this->w() / 2) * sin(this->h() + M_PI / 2);
211 double BicycleCar::rarx() const
213 double rrx = this->x();
214 rrx += (this->w() / 2) * cos(this->h() - M_PI / 2);
218 double BicycleCar::rary() const
220 double rry = this->y();
221 rry += (this->w() / 2) * sin(this->h() - M_PI / 2);
225 BicycleCar BicycleCar::ccl() const
228 bc.x(this->x() + this->mtr() * cos(this->h() + M_PI / 2));
229 bc.y(this->y() + this->mtr() * sin(this->h() + M_PI / 2));
234 BicycleCar BicycleCar::ccr() const
237 bc.x(this->x() + this->mtr() * cos(this->h() - M_PI / 2));
238 bc.y(this->y() + this->mtr() * sin(this->h() - M_PI / 2));
244 void BicycleCar::next()
246 this->x(this->x() + this->sp() * cos(this->h()));
247 this->y(this->y() + this->sp() * sin(this->h()));
248 this->h(this->h() + this->sp() / this->wb() * tan(this->st()));
251 void BicycleCar::rotate(double cx, double cy, double angl)
253 double px = this->x();
254 double py = this->y();
257 double nx = px * cos(angl) - py * sin(angl);
258 double ny = px * sin(angl) + py * cos(angl);
259 this->h(this->h() + angl);
264 BicycleCar::BicycleCar()
266 // TODO according to mtr_ FIXME
275 std::tuple<bool, unsigned int, unsigned int>
277 std::vector<std::tuple<double, double>> &p1,
278 std::vector<std::tuple<double, double>> &p2
281 for (unsigned int i = 0; i < p1.size() - 1; i++) {
282 for (unsigned int j = 0; j < p2.size() - 1; j++) {
286 std::get<0>(p1[i + 1]),
287 std::get<1>(p1[i + 1]),
290 std::get<0>(p2[j + 1]),
291 std::get<1>(p2[j + 1])
294 return std::make_tuple(true, i, j);
297 return std::make_tuple(false, 0, 0);
301 inside(double x, double y, std::vector<std::tuple<double, double>> &poly)
306 for (i = 0; i < 4; i++) {
308 (std::get<1>(poly[i]) > y) != (std::get<1>(poly[j]) > y)
310 x < std::get<0>(poly[i])
311 + (std::get<0>(poly[j]) - std::get<0>(poly[i]))
312 * (y - std::get<1>(poly[i]))
313 / (std::get<1>(poly[j]) - std::get<1>(poly[i]))
322 std::tuple<bool, double, double>
324 double x1, double y1,
325 double x2, double y2,
326 double x3, double y3,
330 double deno = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
332 return std::make_tuple(false, 0, 0);
333 double t = (x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4);
335 double u = (x1 - x2) * (y1 - y3) - (y1 - y2) * (x1 - x3);
338 if (t < 0 || t > 1 || u < 0 || u > 1)
339 return std::make_tuple(false, 0, 0);
340 return std::make_tuple(true, x1 + t * (x2 - x1), y1 + t * (y2 - y1));
343 std::tuple<bool, double, double, double, double>
345 double cx, double cy, double r,
346 double x1, double y1,
357 double dr = sqrt(dx*dx + dy*dy);
358 double D = x1*y2 - x2*y1;
359 if (r*r * dr*dr - D*D < 0)
360 return std::make_tuple(false, 0, 0, 0, 0);
361 // intersection coordinates
362 double ix1 = (D*dy + sgn(dy)*dx*sqrt(r*r * dr*dr - D*D)) / (dr*dr);
364 double ix2 = (D*dy - sgn(dy)*dx*sqrt(r*r * dr*dr - D*D)) / (dr*dr);
366 double iy1 = (-D*dx + std::abs(dy)*sqrt(r*r * dr*dr - D*D)) / (dr*dr);
368 double iy2 = (-D*dx - std::abs(dy)*sqrt(r*r * dr*dr - D*D)) / (dr*dr);
370 return std::make_tuple(true, ix1, iy1, ix2, iy2);
374 angle_between_three_points(
375 double x1, double y1,
376 double x2, double y2,
379 double d1x = x2 - x1;
380 double d1y = y2 - y1;
381 double d2x = x3 - x2;
382 double d2y = y3 - y2;
384 double dot = d1x*d2x + d1y*d2y;
385 double d1 = sqrt(d1x*d1x + d1y*d1y);
386 double d2 = sqrt(d2x*d2x + d2y*d2y);
388 double delta = acos(dot / (d1 * d2));
389 return std::min(delta, M_PI - delta);
394 double x1, double y1,
395 double x2, double y2,
398 if (sgn((x3 - x1) * (y2 - y1) - (y3 - y1) * (x2 - x1)) < 0)