]> rtime.felk.cvut.cz Git - hubacji1/bcar.git/blobdiff - src/pslot.cc
Fix collide
[hubacji1/bcar.git] / src / pslot.cc
index 5a4f1352d573feb288fb988c6d47f4cf89b710a9..ec6462776b71842d65313b7c107e2c250db140e6 100644 (file)
@@ -112,6 +112,10 @@ 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
@@ -127,17 +131,17 @@ 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_);
+               || 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>
@@ -151,15 +155,16 @@ ParkingSlot::drive_in_slot(BicycleCar c, unsigned int& max)
        path.reserve(max + 2);
        path.push_back(c);
        unsigned int cusp = 0;
-       while (cusp < max) {
-               if (c.h() < this->h()) {
-                       return std::vector<BicycleCar>();
-               }
+       while (cusp < max + 1) {
                if (this->parked(c)) {
                        if (cusp < max) {
                                max = cusp;
                        }
-                       break;
+                       path.push_back(c);
+                       return path;
+               }
+               if (c.h() < this->h()) {
+                       return std::vector<BicycleCar>();
                }
                c.next();
                if (this->collide(c)) {
@@ -170,6 +175,22 @@ ParkingSlot::drive_in_slot(BicycleCar c, unsigned int& max)
                        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;
 }
@@ -180,28 +201,28 @@ 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 = this->offset_ + c.w() / 2.0;
+       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.01);
-       auto const& b3 = this->border_[3];
-       this->curb_.intersects_with(b3, c.len());
+       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(b3, i1);
+               max_to_slot = rr.min_angle_between(rc, i1);
        } else {
-               max_to_slot = rr.min_angle_between(b3, i2);
+               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.001;
-               c.rotate(b3, 0.001);
+               a_to_slot += 0.0001;
+               c.rotate(rc, 0.0001);
                starts.push_back(c);
        }
        std::vector<std::vector<BicycleCar>> entries;
@@ -211,7 +232,17 @@ ParkingSlot::fe(BicycleCar c, unsigned int& max)
                        entries.push_back(r);
                }
        }
-       assert(entries.size() > 0);
+       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());
@@ -232,6 +263,18 @@ ParkingSlot::fe(BicycleCar c, unsigned int& max)
        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)
 {