2 * SPDX-FileCopyrightText: 2021 Jiri Vlasak <jiri.vlasak.2@cvut.cz>
4 * SPDX-License-Identifier: GPL-3.0-only
13 ParkingSlot::ParkingSlot(Point p, double h, double W, double L) :
15 Point(p.x() + W * cos(h - M_PI / 2.0),
16 p.y() + W * sin(h - M_PI / 2.0)),
17 Point(p.x() + W * cos(h - M_PI / 2.0) + L * cos(h),
18 p.y() + W * sin(h - M_PI / 2.0) + L * sin(h)),
19 Point(p.x() + L * cos(h), p.y() + L * sin(h))},
20 _entry(_border[0], _border[3]),
21 _rear(_border[0], _border[1]),
22 _curb(_border[1], _border[2]),
23 _front(_border[2], _border[3])
27 ParkingSlot::ParkingSlot(double lrx, double lry, double rrx, double rry,
28 double rfx, double rfy, double lfx, double lfy) :
29 _border{Point(lrx, lry), Point(rrx, rry),
30 Point(rfx, rfy), Point(lfx, lfy)},
31 _entry(_border[0], _border[3]),
32 _rear(_border[0], _border[1]),
33 _curb(_border[1], _border[2]),
34 _front(_border[2], _border[3])
39 ParkingSlot::len() const
41 return this->_entry.len();
45 ParkingSlot::w() const
47 return this->_rear.len();
51 ParkingSlot::lfx() const
53 return this->_border[3].x();
57 ParkingSlot::lfy() const
59 return this->_border[3].y();
63 ParkingSlot::lrx() const
65 return this->_border[0].x();
69 ParkingSlot::lry() const
71 return this->_border[0].y();
75 ParkingSlot::rrx() const
77 return this->_border[1].x();
81 ParkingSlot::rry() const
83 return this->_border[1].y();
87 ParkingSlot::rfx() const
89 return this->_border[2].x();
93 ParkingSlot::rfy() const
95 return this->_border[2].y();
99 ParkingSlot::h() const
101 return atan2(this->lfy() - this->lry(), this->lfx() - this->lrx());
105 ParkingSlot::lf() const
107 return Point(this->lfx(), this->lfy());
111 ParkingSlot::lr() const
113 return Point(this->lrx(), this->lry());
117 ParkingSlot::rr() const
119 return Point(this->rrx(), this->rry());
123 ParkingSlot::rf() const
125 return Point(this->rfx(), this->rfy());
129 ParkingSlot::entry() const
135 ParkingSlot::rear() const
141 ParkingSlot::curb() const
147 ParkingSlot::front() const
153 ParkingSlot::set_parking_speed(double s)
155 this->_parking_speed = s;
159 ParkingSlot::get_max_cusp() const
161 return this->_max_cusp;
165 ParkingSlot::set_max_cusp(unsigned int m)
171 ParkingSlot::set_delta_angle_to_slot(double d)
173 this->_delta_angle_to_slot = d;
177 ParkingSlot::parallel() const
179 return this->_entry.len() > this->_rear.len();
183 ParkingSlot::right() const
185 return this->_border[1].on_right_side_of(this->_entry);
189 ParkingSlot::swap_side()
191 this->_border[1].rotate(this->_border[0], M_PI);
192 this->_border[2].rotate(this->_border[3], M_PI);
193 this->_entry = Line(this->_border[0], this->_border[3]);
194 this->_rear = Line(this->_border[0], this->_border[1]);
195 this->_curb = Line(this->_border[1], this->_border[2]);
196 this->_front = Line(this->_border[2], this->_border[3]);
200 ParkingSlot::parked(BicycleCar const& c) const
202 auto b_len = sizeof(this->_border) / sizeof(this->_border[0]);
203 std::vector<Point> b(this->_border, this->_border + b_len);
204 return c.lf().inside_of(b) && c.lr().inside_of(b)
205 && c.rr().inside_of(b) && c.rf().inside_of(b);
209 ParkingSlot::collide(BicycleCar const& c) const
211 return c.left().intersects_with(this->_rear)
212 || c.left().intersects_with(this->_curb)
213 || c.left().intersects_with(this->_front)
214 || c.rear().intersects_with(this->_rear)
215 || c.rear().intersects_with(this->_curb)
216 || c.rear().intersects_with(this->_front)
217 || c.right().intersects_with(this->_rear)
218 || c.right().intersects_with(this->_curb)
219 || c.right().intersects_with(this->_front)
220 || c.front().intersects_with(this->_rear)
221 || c.front().intersects_with(this->_curb)
222 || c.front().intersects_with(this->_front);
225 std::vector<BicycleCar>
226 ParkingSlot::drive_in_slot(BicycleCar c)
228 assert(this->parallel());
229 assert(this->right());
230 assert(c.len() < this->len());
231 assert(c.w() < this->w());
232 std::vector<BicycleCar> path;
233 path.reserve(this->_max_cusp + 2);
235 unsigned int cusp = 0;
236 while (cusp < this->_max_cusp + 1) {
237 if (this->parked(c)) {
238 if (cusp < this->_max_cusp) {
239 this->_max_cusp = cusp;
244 double sx = c.x() + 10.0 * cos(this->h());
245 double sy = c.y() + 10.0 * sin(this->h());
246 double cx = c.x() + 10.0 * cos(c.h());
247 double cy = c.y() + 10.0 * sin(c.h());
248 if (Point(cx, cy).on_right_side_of(
249 Line(Point(c.x(), c.y()), Point(sx, sy)))) {
250 return std::vector<BicycleCar>();
253 if (this->collide(c)) {
261 return std::vector<BicycleCar>();
264 std::vector<BicycleCar>
265 ParkingSlot::drive_of_slot(BicycleCar c)
267 assert(this->parallel());
268 assert(this->right());
269 assert(c.len() < this->len());
270 assert(c.w() < this->w());
271 assert(this->parked(c));
272 std::vector<BicycleCar> path;
273 path.reserve(this->_max_cusp + 2);
275 unsigned int cusp = 0;
276 auto b_len = sizeof(this->_border) / sizeof(this->_border[0]);
277 std::vector<Point> b(this->_border, this->_border + b_len);
278 while (cusp < this->_max_cusp + 1) {
279 if (!c.lf().inside_of(b) && !c.rf().inside_of(b)) {
280 if (cusp < this->_max_cusp) {
281 this->_max_cusp = cusp;
287 if (this->collide(c)) {
295 return std::vector<BicycleCar>();
299 ParkingSlot::steer_in_slot(BicycleCar c)
301 std::vector<Pose> path;
302 while (!this->parked(c)) {
305 if (this->collide(c)) {
315 ParkingSlot::fe(BicycleCar c)
317 if (!this->parallel()) {
321 if (this->_parking_speed < 0) {
323 c.h(this->_rear.h() + M_PI);
328 c.h(this->_rear.h());
332 c.x(this->_entry.m().x() + gd * cos(this->_rear.h()));
333 c.y(this->_entry.m().y() + gd * sin(this->_rear.h()));
340 this->_rear.intersects_with(cc, radi);
341 dd = std::min(this->_border[0].edist(this->_rear.i1()),
342 this->_border[0].edist(this->_rear.i2()));
346 c.sp(this->_parking_speed);
347 return PoseRange(c.x(), c.y(), c.h(), c.h());
349 bool swapped = false;
350 if (!this->right()) {
355 double clen = -this->_offset + this->len() - c.df();
356 double cw = c.w() / 2.0;
357 c.x(this->lrx() + clen * cos(c.h()) + cw * cos(c.h() + M_PI / 2.0));
358 c.y(this->lry() + clen * sin(c.h()) + cw * sin(c.h() + M_PI / 2.0));
360 assert(this->_parking_speed < 0.0);
361 c.sp(this->_parking_speed);
362 auto const rc = c.rf();
363 this->_curb.intersects_with(rc, c.len());
365 auto const& rr = c.rr();
366 auto const& i1 = this->_curb.i1();
367 auto const& i2 = this->_curb.i2();
368 if (rr.edist(i1) < rr.edist(i2)) {
369 max_to_slot = rr.min_angle_between(rc, i1);
371 max_to_slot = rr.min_angle_between(rc, i2);
373 std::vector<BicycleCar> starts;
374 double a_to_slot = 0.0;
375 while (a_to_slot < max_to_slot) {
376 a_to_slot += this->_delta_angle_to_slot;
377 c.rotate(rc, this->_delta_angle_to_slot);
380 for (auto s: starts) {
381 auto r = this->drive_in_slot(s);
383 this->_entries.push_back(r);
386 if (this->_entries.size() == 0) {
387 return PoseRange(Pose(0.0, 0.0, 0.0), Pose(0.0, 0.0, 0.0));
389 if (this->_entries.size() == 1) {
390 auto f = this->_entries.front().front();
391 return PoseRange(f, f);
393 auto& c1 = this->_entries.front().front();
394 auto& c2 = this->_entries.back().front();
398 p.reflect(this->_entry);
404 ParkingSlot::recompute_entry(PoseRange p)
406 p.rotate(Point(0.0, 0.0), this->h());
407 p.translate(this->_border[0]);
408 if (!this->right()) {
409 p.reflect(this->_entry);
415 operator<<(std::ostream& o, ParkingSlot const& s)
418 o << s._border[0] << ",";
419 o << s._border[1] << ",";
420 o << s._border[2] << ",";