7 ParkingSlot::ParkingSlot(Point p, double h, double W, double L) :
9 Point(p.x() + W * cos(h - M_PI / 2.0),
10 p.y() + W * sin(h - M_PI / 2.0)),
11 Point(p.x() + W * cos(h - M_PI / 2.0) + L * cos(h),
12 p.y() + W * sin(h - M_PI / 2.0) + L * sin(h)),
13 Point(p.x() + L * cos(h), p.y() + L * sin(h))}),
14 entry_(border_[0], border_[3]),
15 rear_(border_[0], border_[1]),
16 curb_(border_[1], border_[2]),
17 front_(border_[2], border_[3])
21 ParkingSlot::ParkingSlot(double lrx, double lry, double rrx, double rry,
22 double rfx, double rfy, double lfx, double lfy) :
23 border_({Point(lrx, lry), Point(rrx, rry),
24 Point(rfx, rfy), Point(lfx, lfy)}),
25 entry_(border_[0], border_[3]),
26 rear_(border_[0], border_[1]),
27 curb_(border_[1], border_[2]),
28 front_(border_[2], border_[3])
33 ParkingSlot::len() const
35 return this->entry_.len();
39 ParkingSlot::w() const
41 return this->rear_.len();
45 ParkingSlot::lfx() const
47 return this->border_[3].x();
51 ParkingSlot::lfy() const
53 return this->border_[3].y();
57 ParkingSlot::lrx() const
59 return this->border_[0].x();
63 ParkingSlot::lry() const
65 return this->border_[0].y();
69 ParkingSlot::rrx() const
71 return this->border_[1].x();
75 ParkingSlot::rry() const
77 return this->border_[1].y();
81 ParkingSlot::rfx() const
83 return this->border_[2].x();
87 ParkingSlot::rfy() const
89 return this->border_[2].y();
93 ParkingSlot::h() const
95 return atan2(this->lfy() - this->lry(), this->lfx() - this->lrx());
99 ParkingSlot::parallel() const
101 return this->entry_.len() > this->rear_.len();
105 ParkingSlot::right() const
107 return this->border_[1].on_right_side_of(this->entry_);
111 ParkingSlot::swap_side()
113 this->border_[1].rotate(this->border_[0], M_PI);
114 this->border_[2].rotate(this->border_[3], M_PI);
118 ParkingSlot::parked(BicycleCar const& c) const
120 auto b_len = sizeof(this->border_) / sizeof(this->border_[0]);
121 std::vector<Point> b(this->border_, this->border_ + b_len);
122 return c.lf().inside_of(b) && c.lr().inside_of(b)
123 && c.rr().inside_of(b) && c.rf().inside_of(b);
127 ParkingSlot::collide(BicycleCar const& c) const
129 return c.left().intersects_with(this->rear_)
130 && c.left().intersects_with(this->curb_)
131 && c.left().intersects_with(this->front_)
132 && c.rear().intersects_with(this->rear_)
133 && c.rear().intersects_with(this->curb_)
134 && c.rear().intersects_with(this->front_)
135 && c.right().intersects_with(this->rear_)
136 && c.right().intersects_with(this->curb_)
137 && c.right().intersects_with(this->front_)
138 && c.front().intersects_with(this->rear_)
139 && c.front().intersects_with(this->curb_)
140 && c.front().intersects_with(this->front_);
143 std::vector<BicycleCar>
144 ParkingSlot::drive_in_slot(BicycleCar c, unsigned int& max)
146 assert(this->parallel());
147 assert(this->right());
148 assert(c.len() < this->len());
149 assert(c.w() < this->w());
150 std::vector<BicycleCar> path;
151 path.reserve(max + 2);
153 unsigned int cusp = 0;
154 while (cusp < max + 1) {
155 if (this->parked(c)) {
162 if (c.h() < this->h()) {
163 return std::vector<BicycleCar>();
166 if (this->collide(c)) {
174 return std::vector<BicycleCar>();
178 ParkingSlot::steer_in_slot(BicycleCar c)
180 std::vector<Pose> path;
181 while (!this->parked(c)) {
184 if (this->collide(c)) {
195 ParkingSlot::fe(BicycleCar c, unsigned int& max)
197 assert(this->parallel());
198 assert(this->right());
200 double clen = -this->offset_ + this->len() - c.df();
201 double cw = c.w() / 2.0;
202 c.x(this->lrx() + clen * cos(c.h()) + cw * cos(c.h() + M_PI / 2.0));
203 c.y(this->lry() + clen * sin(c.h()) + cw * sin(c.h() + M_PI / 2.0));
206 auto const rc = c.rf();
207 this->curb_.intersects_with(rc, c.len());
209 auto const& rr = c.rr();
210 auto const& i1 = this->curb_.i1();
211 auto const& i2 = this->curb_.i2();
212 if (rr.edist(i1) < rr.edist(i2)) {
213 max_to_slot = rr.min_angle_between(rc, i1);
215 max_to_slot = rr.min_angle_between(rc, i2);
217 std::vector<BicycleCar> starts;
218 double a_to_slot = 0.0;
219 while (a_to_slot < max_to_slot) {
221 c.rotate(rc, 0.0001);
224 std::vector<std::vector<BicycleCar>> entries;
225 for (auto s: starts) {
226 auto r = this->drive_in_slot(s, max);
228 entries.push_back(r);
231 if (entries.size() == 0) {
234 if (entries.size() == 1) {
236 pr.x(entries.front().front().x());
237 pr.y(entries.front().front().y());
238 pr.b(entries.front().front().h());
239 pr.e(entries.front().front().h());
242 auto& c1 = entries.front().front();
243 auto& c2 = entries.back().front();
244 double b = std::min(c1.h(), c2.h());
245 double e = std::max(c1.h(), c2.h());
247 Point b1(c1.x() - clen * cos(c1.h()), c1.y() - clen * sin(c1.h()));
248 Point b2(c2.x() - clen * cos(c2.h()), c2.y() - clen * sin(c2.h()));
249 Point e1(c1.x() + clen * cos(c1.h()), c1.y() + clen * sin(c1.h()));
250 Point e2(c2.x() + clen * cos(c2.h()), c2.y() + clen * sin(c2.h()));
253 li1.intersects_with(li2);
263 operator<<(std::ostream& o, ParkingSlot const& s)
266 o << s.border_[0] << ",";
267 o << s.border_[1] << ",";
268 o << s.border_[2] << ",";