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::set_max_cusp(unsigned int m)
165 ParkingSlot::set_delta_angle_to_slot(double d)
167 this->delta_angle_to_slot_ = d;
171 ParkingSlot::parallel() const
173 return this->entry_.len() > this->rear_.len();
177 ParkingSlot::right() const
179 return this->border_[1].on_right_side_of(this->entry_);
183 ParkingSlot::swap_side()
185 this->border_[1].rotate(this->border_[0], M_PI);
186 this->border_[2].rotate(this->border_[3], M_PI);
187 this->entry_ = Line(this->border_[0], this->border_[3]);
188 this->rear_ = Line(this->border_[0], this->border_[1]);
189 this->curb_ = Line(this->border_[1], this->border_[2]);
190 this->front_ = Line(this->border_[2], this->border_[3]);
194 ParkingSlot::parked(BicycleCar const& c) const
196 auto b_len = sizeof(this->border_) / sizeof(this->border_[0]);
197 std::vector<Point> b(this->border_, this->border_ + b_len);
198 return c.lf().inside_of(b) && c.lr().inside_of(b)
199 && c.rr().inside_of(b) && c.rf().inside_of(b);
203 ParkingSlot::collide(BicycleCar const& c) const
205 return c.left().intersects_with(this->rear_)
206 || c.left().intersects_with(this->curb_)
207 || c.left().intersects_with(this->front_)
208 || c.rear().intersects_with(this->rear_)
209 || c.rear().intersects_with(this->curb_)
210 || c.rear().intersects_with(this->front_)
211 || c.right().intersects_with(this->rear_)
212 || c.right().intersects_with(this->curb_)
213 || c.right().intersects_with(this->front_)
214 || c.front().intersects_with(this->rear_)
215 || c.front().intersects_with(this->curb_)
216 || c.front().intersects_with(this->front_);
219 std::vector<BicycleCar>
220 ParkingSlot::drive_in_slot(BicycleCar c)
222 assert(this->parallel());
223 assert(this->right());
224 assert(c.len() < this->len());
225 assert(c.w() < this->w());
226 std::vector<BicycleCar> path;
227 path.reserve(this->max_cusp_ + 2);
229 unsigned int cusp = 0;
230 while (cusp < this->max_cusp_ + 1) {
231 if (this->parked(c)) {
232 if (cusp < this->max_cusp_) {
233 this->max_cusp_ = cusp;
238 double sx = c.x() + 10.0 * cos(this->h());
239 double sy = c.y() + 10.0 * sin(this->h());
240 double cx = c.x() + 10.0 * cos(c.h());
241 double cy = c.y() + 10.0 * sin(c.h());
242 if (Point(cx, cy).on_right_side_of(
243 Line(Point(c.x(), c.y()), Point(sx, sy)))) {
244 return std::vector<BicycleCar>();
247 if (this->collide(c)) {
255 return std::vector<BicycleCar>();
258 std::vector<BicycleCar>
259 ParkingSlot::drive_of_slot(BicycleCar c)
261 assert(this->parallel());
262 assert(this->right());
263 assert(c.len() < this->len());
264 assert(c.w() < this->w());
265 assert(this->parked(c));
266 std::vector<BicycleCar> path;
267 path.reserve(this->max_cusp_ + 2);
269 unsigned int cusp = 0;
270 auto b_len = sizeof(this->border_) / sizeof(this->border_[0]);
271 std::vector<Point> b(this->border_, this->border_ + b_len);
272 while (cusp < this->max_cusp_ + 1) {
273 if (!c.lf().inside_of(b) && !c.rf().inside_of(b)) {
274 if (cusp < this->max_cusp_) {
275 this->max_cusp_ = cusp;
281 if (this->collide(c)) {
289 return std::vector<BicycleCar>();
293 ParkingSlot::steer_in_slot(BicycleCar c)
295 std::vector<Pose> path;
296 while (!this->parked(c)) {
299 if (this->collide(c)) {
309 ParkingSlot::fe(BicycleCar c)
311 if (!this->parallel()) {
315 if (this->parking_speed_ < 0) {
317 c.h(this->rear_.h() + M_PI);
322 c.h(this->rear_.h());
326 c.x(this->entry_.m().x() + gd * cos(this->rear_.h()));
327 c.y(this->entry_.m().y() + gd * sin(this->rear_.h()));
334 this->rear_.intersects_with(cc, radi);
335 dd = std::min(this->border_[0].edist(this->rear_.i1()),
336 this->border_[0].edist(this->rear_.i2()));
340 c.sp(this->parking_speed_);
341 return PoseRange(c.x(), c.y(), c.h(), c.h());
343 bool swapped = false;
344 if (!this->right()) {
349 double clen = -this->offset_ + this->len() - c.df();
350 double cw = c.w() / 2.0;
351 c.x(this->lrx() + clen * cos(c.h()) + cw * cos(c.h() + M_PI / 2.0));
352 c.y(this->lry() + clen * sin(c.h()) + cw * sin(c.h() + M_PI / 2.0));
354 assert(this->parking_speed_ < 0.0);
355 c.sp(this->parking_speed_);
356 auto const rc = c.rf();
357 this->curb_.intersects_with(rc, c.len());
359 auto const& rr = c.rr();
360 auto const& i1 = this->curb_.i1();
361 auto const& i2 = this->curb_.i2();
362 if (rr.edist(i1) < rr.edist(i2)) {
363 max_to_slot = rr.min_angle_between(rc, i1);
365 max_to_slot = rr.min_angle_between(rc, i2);
367 std::vector<BicycleCar> starts;
368 double a_to_slot = 0.0;
369 while (a_to_slot < max_to_slot) {
370 a_to_slot += this->delta_angle_to_slot_;
371 c.rotate(rc, this->delta_angle_to_slot_);
374 std::vector<std::vector<BicycleCar>> entries;
375 for (auto s: starts) {
376 auto r = this->drive_in_slot(s);
378 entries.push_back(r);
381 if (entries.size() == 0) {
382 return PoseRange(Pose(0.0, 0.0, 0.0), Pose(0.0, 0.0, 0.0));
384 if (entries.size() == 1) {
385 auto f = entries.front().front();
386 return PoseRange(f, f);
388 auto& c1 = entries.front().front();
389 auto& c2 = entries.back().front();
393 p.reflect(this->entry_);
399 ParkingSlot::recompute_entry(PoseRange p)
401 p.rotate(Point(0.0, 0.0), this->h());
402 p.translate(this->border_[0]);
403 if (!this->right()) {
404 p.reflect(this->entry_);
410 operator<<(std::ostream& o, ParkingSlot const& s)
413 o << s.border_[0] << ",";
414 o << s.border_[1] << ",";
415 o << s.border_[2] << ",";