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 (0 <= a_1 && a_1 <= M_PI/2) { // left front
19 BicycleCar z(*this); // zone border
20 z.rotate(this->ccl().x(), this->ccl().y(), h_d);
21 // assert z.h() == bc.h()
22 if (bc.y() == z.y() && bc.x() == z.x()) // bc on zone border
24 a_2 = atan2(bc.y() - z.y(), bc.x() - z.x());
29 if (z.h() >= a_2 && a_2 >= this->h())
31 } else if (M_PI/2 < a_1 && a_1 <= M_PI) { // left rear
32 BicycleCar z(*this); // zone border
33 z.rotate(this->ccl().x(), this->ccr().y(), h_d);
34 // assert z.h() == bc.h()
35 if (bc.y() == z.y() && bc.x() == z.x()) // bc on zone border
37 a_2 = atan2(bc.y() - z.y(), bc.x() - z.x());
43 if (this->h() >= a_2 && a_2 >= z.h())
45 } else if (0 > a_1 && a_1 >= -M_PI/2) { // right front
46 BicycleCar z(*this); // zone border
47 z.rotate(this->ccr().x(), this->ccl().y(), h_d);
48 // assert z.h() == bc.h()
49 if (bc.y() == z.y() && bc.x() == z.x()) // bc on zone border
51 a_2 = atan2(bc.y() - z.y(), bc.x() - z.x());
56 if (this->h() >= a_2 && a_2 >= z.h())
58 } else if (-M_PI/2 > a_1 && a_1 >= -M_PI) { // right rear
59 BicycleCar z(*this); // zone border
60 z.rotate(this->ccr().x(), this->ccl().y(), h_d);
61 // assert z.h() == bc.h()
62 if (bc.y() == z.y() && bc.x() == z.x()) // bc on zone border
64 a_2 = atan2(bc.y() - z.y(), bc.x() - z.x());
70 if (z.h() >= a_2 && a_2 >= this->h())
73 // Not happenning, as ``-pi <= a <= pi``.
78 double BicycleCar::iradi() const
80 return this->mtr() - this->w() / 2;
83 double BicycleCar::ofradi() const
85 return sqrt(pow(this->mtr() + this->w() / 2, 2) + pow(this->df(), 2));
88 double BicycleCar::orradi() const
90 return sqrt(pow(this->mtr() + this->w() / 2, 2) + pow(this->dr(), 2));
93 double BicycleCar::perfect_parking_slot_len() const
95 // see Simon R. Blackburn *The Geometry of Perfect Parking*
96 // see https://www.ma.rhul.ac.uk/SRBparking
97 double r = this->ctc() / 2;
98 double l = this->wb();
99 double k = this->df() - this->wb();
100 double w = this->w();
106 - pow(sqrt(r*r - l*l) - w, 2)
113 void BicycleCar::set_max_steer()
115 this->st(atan(this->wb() / this->mtr()));
119 double BicycleCar::lfx() const
121 double lfx = this->x();
122 lfx += (this->w() / 2) * cos(this->h() + M_PI / 2);
123 lfx += this->df() * cos(this->h());
124 lfx += this->sd() * cos(this->h());
128 double BicycleCar::lfy() const
130 double lfy = this->y();
131 lfy += (this->w() / 2) * sin(this->h() + M_PI / 2);
132 lfy += this->df() * sin(this->h());
133 lfy += this->sd() * sin(this->h());
137 double BicycleCar::lrx() const
139 double lrx = this->x();
140 lrx += (this->w() / 2) * cos(this->h() + M_PI / 2);
141 lrx += -this->dr() * cos(this->h());
142 lrx += -this->sd() * cos(this->h());
146 double BicycleCar::lry() const
148 double lry = this->y();
149 lry += (this->w() / 2) * sin(this->h() + M_PI / 2);
150 lry += -this->dr() * sin(this->h());
151 lry += -this->sd() * sin(this->h());
155 double BicycleCar::rrx() const
157 double rrx = this->x();
158 rrx += (this->w() / 2) * cos(this->h() - M_PI / 2);
159 rrx += -this->dr() * cos(this->h());
160 rrx += -this->sd() * cos(this->h());
164 double BicycleCar::rry() const
166 double rry = this->y();
167 rry += (this->w() / 2) * sin(this->h() - M_PI / 2);
168 rry += -this->dr() * sin(this->h());
169 rry += -this->sd() * sin(this->h());
173 double BicycleCar::rfx() const
175 double rfx = this->x();
176 rfx += (this->w() / 2) * cos(this->h() - M_PI / 2);
177 rfx += this->df() * cos(this->h());
178 rfx += this->sd() * cos(this->h());
182 double BicycleCar::rfy() const
184 double rfy = this->y();
185 rfy += (this->w() / 2) * sin(this->h() - M_PI / 2);
186 rfy += this->df() * sin(this->h());
187 rfy += this->sd() * sin(this->h());
191 double BicycleCar::ralx() const
193 double lrx = this->x();
194 lrx += (this->w() / 2) * cos(this->h() + M_PI / 2);
197 double BicycleCar::raly() const
199 double lry = this->y();
200 lry += (this->w() / 2) * sin(this->h() + M_PI / 2);
204 double BicycleCar::rarx() const
206 double rrx = this->x();
207 rrx += (this->w() / 2) * cos(this->h() - M_PI / 2);
211 double BicycleCar::rary() const
213 double rry = this->y();
214 rry += (this->w() / 2) * sin(this->h() - M_PI / 2);
218 BicycleCar BicycleCar::ccl() const
221 bc.x(this->x() + this->mtr() * cos(this->h() + M_PI / 2));
222 bc.y(this->y() + this->mtr() * sin(this->h() + M_PI / 2));
227 BicycleCar BicycleCar::ccr() const
230 bc.x(this->x() + this->mtr() * cos(this->h() - M_PI / 2));
231 bc.y(this->y() + this->mtr() * sin(this->h() - M_PI / 2));
237 void BicycleCar::next()
239 this->x(this->x() + this->sp() * cos(this->h()));
240 this->y(this->y() + this->sp() * sin(this->h()));
241 this->h(this->h() + this->sp() / this->wb() * tan(this->st()));
244 void BicycleCar::rotate(double cx, double cy, double angl)
246 double px = this->x();
247 double py = this->y();
250 double nx = px * cos(angl) - py * sin(angl);
251 double ny = px * sin(angl) + py * cos(angl);
252 this->h(this->h() + angl);
257 BicycleCar::BicycleCar()
259 // TODO according to mtr_ FIXME
268 std::tuple<bool, unsigned int, unsigned int> collide(
269 std::vector<std::tuple<double, double>> &p1,
270 std::vector<std::tuple<double, double>> &p2
273 for (unsigned int i = 0; i < p1.size() - 1; i++) {
274 for (unsigned int j = 0; j < p2.size() - 1; j++) {
278 std::get<0>(p1[i + 1]),
279 std::get<1>(p1[i + 1]),
282 std::get<0>(p2[j + 1]),
283 std::get<1>(p2[j + 1])
286 return std::make_tuple(true, i, j);
289 return std::make_tuple(false, 0, 0);
292 bool inside(double x, double y, std::vector<std::tuple<double, double>> &poly)
297 for (i = 0; i < 4; i++) {
299 (std::get<1>(poly[i]) > y) != (std::get<1>(poly[j]) > y)
301 x < std::get<0>(poly[i])
302 + (std::get<0>(poly[j]) - std::get<0>(poly[i]))
303 * (y - std::get<1>(poly[i]))
304 / (std::get<1>(poly[j]) - std::get<1>(poly[i]))
313 std::tuple<bool, double, double> intersect(
314 double x1, double y1,
315 double x2, double y2,
316 double x3, double y3,
320 double deno = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
322 return std::make_tuple(false, 0, 0);
323 double t = (x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4);
325 double u = (x1 - x2) * (y1 - y3) - (y1 - y2) * (x1 - x3);
328 if (t < 0 || t > 1 || u < 0 || u > 1)
329 return std::make_tuple(false, 0, 0);
330 return std::make_tuple(true, x1 + t * (x2 - x1), y1 + t * (y2 - y1));