+#include <cassert>
#include <cmath>
-#include "pslot.h"
+#include "pslot.hh"
-template <typename T> int sgn(T val) {
- return (T(0) < val) - (val < T(0));
+namespace bcar {
+
+ParkingSlot::ParkingSlot(Point p, double h, double W, double L) :
+ border_({p,
+ Point(p.x() + W * cos(h - M_PI / 2.0),
+ p.y() + W * sin(h - M_PI / 2.0)),
+ Point(p.x() + W * cos(h - M_PI / 2.0) + L * cos(h),
+ p.y() + W * sin(h - M_PI / 2.0) + L * sin(h)),
+ Point(p.x() + L * cos(h), p.y() + L * sin(h))}),
+ entry_(border_[0], border_[3]),
+ rear_(border_[0], border_[1]),
+ curb_(border_[1], border_[2]),
+ front_(border_[2], border_[3])
+{
+}
+
+ParkingSlot::ParkingSlot(double lrx, double lry, double rrx, double rry,
+ double rfx, double rfy, double lfx, double lfy) :
+ border_({Point(lrx, lry), Point(rrx, rry),
+ Point(rfx, rfy), Point(lfx, lfy)}),
+ entry_(border_[0], border_[3]),
+ rear_(border_[0], border_[1]),
+ curb_(border_[1], border_[2]),
+ front_(border_[2], border_[3])
+{
+}
+
+double
+ParkingSlot::len() const
+{
+ return this->entry_.len();
+}
+
+double
+ParkingSlot::w() const
+{
+ return this->rear_.len();
+}
+
+double
+ParkingSlot::lfx() const
+{
+ return this->border_[3].x();
+}
+
+double
+ParkingSlot::lfy() const
+{
+ return this->border_[3].y();
+}
+
+double
+ParkingSlot::lrx() const
+{
+ return this->border_[0].x();
+}
+
+double
+ParkingSlot::lry() const
+{
+ return this->border_[0].y();
+}
+
+double
+ParkingSlot::rrx() const
+{
+ return this->border_[1].x();
+}
+
+double
+ParkingSlot::rry() const
+{
+ return this->border_[1].y();
}
-// slot info
-double ParkingSlot::heading()
+double
+ParkingSlot::rfx() const
{
- return atan2(this->y4() - this->y1(), this->x4() - this->x1());
+ return this->border_[2].x();
}
-bool ParkingSlot::parallel()
+double
+ParkingSlot::rfy() const
{
- double d1 = sqrt(
- pow(this->x2() - this->x1(), 2)
- + pow(this->y2() - this->y1(), 2)
- );
- double d2 = sqrt(
- pow(this->x3() - this->x2(), 2)
- + pow(this->y3() - this->y2(), 2)
- );
- if (d1 < d2)
- return true;
- else
- return false;
+ return this->border_[2].y();
}
-bool ParkingSlot::right()
+double
+ParkingSlot::h() const
{
- if (sgn(
- (this->x2() - this->x1()) * (this->y4() - this->y1())
- - (this->y2() - this->y1()) * (this->x4() - this->x1())
- ) < 0)
- return false;
- else
- return true;
+ return atan2(this->lfy() - this->lry(), this->lfx() - this->lrx());
}
-ParkingSlot::ParkingSlot()
+bool
+ParkingSlot::parallel() const
{
+ return this->entry_.len() > this->rear_.len();
}
+
+bool
+ParkingSlot::right() const
+{
+ return this->border_[1].on_right_side_of(this->entry_);
+}
+
+void
+ParkingSlot::swap_side()
+{
+ this->border_[1].rotate(this->border_[0], M_PI);
+ this->border_[2].rotate(this->border_[3], M_PI);
+ this->entry_ = Line(this->border_[0], this->border_[3]);
+ this->rear_ = Line(this->border_[0], this->border_[1]);
+ this->curb_ = Line(this->border_[1], this->border_[2]);
+ this->front_ = Line(this->border_[2], this->border_[3]);
+}
+
+bool
+ParkingSlot::parked(BicycleCar const& c) const
+{
+ auto b_len = sizeof(this->border_) / sizeof(this->border_[0]);
+ std::vector<Point> b(this->border_, this->border_ + b_len);
+ return c.lf().inside_of(b) && c.lr().inside_of(b)
+ && c.rr().inside_of(b) && c.rf().inside_of(b);
+}
+
+bool
+ParkingSlot::collide(BicycleCar const& c) const
+{
+ return c.left().intersects_with(this->rear_)
+ || c.left().intersects_with(this->curb_)
+ || c.left().intersects_with(this->front_)
+ || c.rear().intersects_with(this->rear_)
+ || c.rear().intersects_with(this->curb_)
+ || c.rear().intersects_with(this->front_)
+ || c.right().intersects_with(this->rear_)
+ || c.right().intersects_with(this->curb_)
+ || c.right().intersects_with(this->front_)
+ || c.front().intersects_with(this->rear_)
+ || c.front().intersects_with(this->curb_)
+ || c.front().intersects_with(this->front_);
+}
+
+std::vector<BicycleCar>
+ParkingSlot::drive_in_slot(BicycleCar c, unsigned int& max)
+{
+ assert(this->parallel());
+ assert(this->right());
+ assert(c.len() < this->len());
+ assert(c.w() < this->w());
+ std::vector<BicycleCar> path;
+ path.reserve(max + 2);
+ path.push_back(c);
+ unsigned int cusp = 0;
+ while (cusp < max + 1) {
+ if (this->parked(c)) {
+ if (cusp < max) {
+ max = cusp;
+ }
+ path.push_back(c);
+ return path;
+ }
+ if (c.h() < this->h()) {
+ return std::vector<BicycleCar>();
+ }
+ c.next();
+ if (this->collide(c)) {
+ c.sp(c.sp() * -1.0);
+ c.next();
+ path.push_back(c);
+ c.st(c.st() * -1.0);
+ cusp += 1;
+ }
+ }
+ return std::vector<BicycleCar>();
+}
+
+std::vector<Pose>
+ParkingSlot::steer_in_slot(BicycleCar c)
+{
+ std::vector<Pose> path;
+ while (!this->parked(c)) {
+ path.push_back(c);
+ c.next();
+ if (this->collide(c)) {
+ c.sp(c.sp() * -1.0);
+ c.next();
+ c.st(c.st() * -1.0);
+ }
+ }
+ path.push_back(c);
+ return path;
+}
+
+PoseRange
+ParkingSlot::fe(BicycleCar c, unsigned int& max)
+{
+ assert(this->parallel());
+ assert(this->right());
+ c.h(this->h());
+ double clen = -this->offset_ + this->len() - c.df();
+ double cw = c.w() / 2.0;
+ c.x(this->lrx() + clen * cos(c.h()) + cw * cos(c.h() + M_PI / 2.0));
+ c.y(this->lry() + clen * sin(c.h()) + cw * sin(c.h() + M_PI / 2.0));
+ c.set_max_steer();
+ c.sp(-0.001);
+ auto const rc = c.rf();
+ this->curb_.intersects_with(rc, c.len());
+ double max_to_slot;
+ auto const& rr = c.rr();
+ auto const& i1 = this->curb_.i1();
+ auto const& i2 = this->curb_.i2();
+ if (rr.edist(i1) < rr.edist(i2)) {
+ max_to_slot = rr.min_angle_between(rc, i1);
+ } else {
+ max_to_slot = rr.min_angle_between(rc, i2);
+ }
+ std::vector<BicycleCar> starts;
+ double a_to_slot = 0.0;
+ while (a_to_slot < max_to_slot) {
+ a_to_slot += 0.0001;
+ c.rotate(rc, 0.0001);
+ starts.push_back(c);
+ }
+ std::vector<std::vector<BicycleCar>> entries;
+ for (auto s: starts) {
+ auto r = this->drive_in_slot(s, max);
+ if (r.size() > 0) {
+ entries.push_back(r);
+ }
+ }
+ if (entries.size() == 0) {
+ return PoseRange();
+ }
+ if (entries.size() == 1) {
+ PoseRange pr;
+ pr.x(entries.front().front().x());
+ pr.y(entries.front().front().y());
+ pr.b(entries.front().front().h());
+ pr.e(entries.front().front().h());
+ return pr;
+ }
+ auto& c1 = entries.front().front();
+ auto& c2 = entries.back().front();
+ double b = std::min(c1.h(), c2.h());
+ double e = std::max(c1.h(), c2.h());
+ clen = c.len();
+ Point b1(c1.x() - clen * cos(c1.h()), c1.y() - clen * sin(c1.h()));
+ Point b2(c2.x() - clen * cos(c2.h()), c2.y() - clen * sin(c2.h()));
+ Point e1(c1.x() + clen * cos(c1.h()), c1.y() + clen * sin(c1.h()));
+ Point e2(c2.x() + clen * cos(c2.h()), c2.y() + clen * sin(c2.h()));
+ Line li1(b1, e1);
+ Line li2(b2, e2);
+ li1.intersects_with(li2);
+ PoseRange pr;
+ pr.x(li1.i1().x());
+ pr.y(li1.i1().y());
+ pr.b(b);
+ pr.e(e);
+ return pr;
+}
+
+PoseRange
+ParkingSlot::recompute_entry(PoseRange p)
+{
+ p.rotate(Point(0.0, 0.0), this->h());
+ p.x(p.x() + this->lrx());
+ p.y(p.y() + this->lry());
+ if (!this->right()) {
+ p.reflect(this->entry_);
+ }
+ return p;
+}
+
+std::ostream&
+operator<<(std::ostream& o, ParkingSlot const& s)
+{
+ o << "[";
+ o << s.border_[0] << ",";
+ o << s.border_[1] << ",";
+ o << s.border_[2] << ",";
+ o << s.border_[3];
+ o << "]";
+ return o;
+}
+
+} // namespace bcar