]> rtime.felk.cvut.cz Git - hubacji1/bcar.git/blobdiff - src/pslot.cc
Add set to start method for parkig slot
[hubacji1/bcar.git] / src / pslot.cc
index 0efb39d1c7438eaf335e5e278cdd7cd3e555cee0..b1a17dad1d2b8b8b81bc555fd2e9217faeaf8577 100644 (file)
+/*
+ * SPDX-FileCopyrightText: 2021 Jiri Vlasak <jiri.vlasak.2@cvut.cz>
+ *
+ * SPDX-License-Identifier: GPL-3.0-only
+ */
+
+#include <cassert>
 #include <cmath>
-#include "pslot.h"
+#include "pslot.hh"
+
+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();
+}
 
-template <typename T> int sgn(T val) {
-        return (T(0) < val) - (val < T(0));
+double
+ParkingSlot::rrx() const
+{
+       return this->_border[1].x();
+}
+
+double
+ParkingSlot::rry() const
+{
+       return this->_border[1].y();
+}
+
+double
+ParkingSlot::rfx() const
+{
+       return this->_border[2].x();
+}
+
+double
+ParkingSlot::rfy() const
+{
+       return this->_border[2].y();
+}
+
+double
+ParkingSlot::h() const
+{
+       return atan2(this->lfy() - this->lry(), this->lfx() - this->lrx());
 }
 
-// slot info
-double ParkingSlot::heading() const
+Point
+ParkingSlot::lf() const
 {
-        return atan2(this->y4() - this->y1(), this->x4() - this->x1());
+       return Point(this->lfx(), this->lfy());
 }
 
-bool ParkingSlot::parallel() const
+Point
+ParkingSlot::lr() 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 Point(this->lrx(), this->lry());
 }
 
-bool ParkingSlot::right() const
+Point
+ParkingSlot::rr() 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 Point(this->rrx(), this->rry());
 }
 
-ParkingSlot::ParkingSlot()
+Point
+ParkingSlot::rf() const
 {
+       return Point(this->rfx(), this->rfy());
 }
+
+Line
+ParkingSlot::entry() const
+{
+       return this->_entry;
+}
+
+Line
+ParkingSlot::rear() const
+{
+       return this->_rear;
+}
+
+Line
+ParkingSlot::curb() const
+{
+       return this->_curb;
+}
+
+Line
+ParkingSlot::front() const
+{
+       return this->_front;
+}
+
+void
+ParkingSlot::set_parking_speed(double s)
+{
+       this->_parking_speed = s;
+}
+
+unsigned int
+ParkingSlot::get_max_cusp() const
+{
+       return this->_max_cusp;
+}
+
+void
+ParkingSlot::set_max_cusp(unsigned int m)
+{
+       this->_max_cusp = m;
+}
+
+void
+ParkingSlot::set_delta_angle_to_slot(double d)
+{
+       this->_delta_angle_to_slot = d;
+}
+
+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);
+}
+
+void
+ParkingSlot::set_to_start(BicycleCar& c)
+{
+       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();
+       assert(this->_parking_speed < 0.0);
+       c.sp(this->_parking_speed);
+}
+
+std::vector<BicycleCar>
+ParkingSlot::drive_in_slot(BicycleCar c)
+{
+       assert(this->parallel());
+       assert(this->right());
+       assert(c.len() < this->len());
+       assert(c.w() < this->w());
+       std::vector<BicycleCar> path;
+       path.reserve(this->_max_cusp + 2);
+       path.push_back(c);
+       unsigned int cusp = 0;
+       while (cusp < this->_max_cusp + 1) {
+               if (this->parked(c)) {
+                       if (cusp < this->_max_cusp) {
+                               this->_max_cusp = cusp;
+                       }
+                       path.push_back(c);
+                       return path;
+               }
+               double sx = c.x() + 10.0 * cos(this->h());
+               double sy = c.y() + 10.0 * sin(this->h());
+               double cx = c.x() + 10.0 * cos(c.h());
+               double cy = c.y() + 10.0 * sin(c.h());
+               if (Point(cx, cy).on_right_side_of(
+                               Line(Point(c.x(), c.y()), Point(sx, sy)))) {
+                       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<BicycleCar>
+ParkingSlot::drive_of_slot(BicycleCar c)
+{
+       assert(this->parallel());
+       assert(this->right());
+       assert(c.len() < this->len());
+       assert(c.w() < this->w());
+       assert(this->parked(c));
+       std::vector<BicycleCar> path;
+       path.reserve(this->_max_cusp + 2);
+       path.push_back(c);
+       unsigned int cusp = 0;
+       auto b_len = sizeof(this->_border) / sizeof(this->_border[0]);
+       std::vector<Point> b(this->_border, this->_border + b_len);
+       while (cusp < this->_max_cusp + 1) {
+               if (!c.lf().inside_of(b) && !c.rf().inside_of(b)) {
+                       if (cusp < this->_max_cusp) {
+                               this->_max_cusp = cusp;
+                       }
+                       path.push_back(c);
+                       return path;
+               }
+               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);
+               }
+       }
+       return path;
+}
+
+PoseRange
+ParkingSlot::fe(BicycleCar c)
+{
+       if (!this->parallel()) {
+               double gd = 0.0;
+               double dd = 0.0;
+               double radi = 0.0;
+               if (this->_parking_speed < 0) {
+                       gd = c.df();
+                       c.h(this->_rear.h() + M_PI);
+                       c.sp(1.0);
+                       radi = c.iradi();
+               } else {
+                       gd = c.dr();
+                       c.h(this->_rear.h());
+                       c.sp(-1.0);
+                       radi = c.ofradi();
+               }
+               c.x(this->_entry.m().x() + gd * cos(this->_rear.h()));
+               c.y(this->_entry.m().y() + gd * sin(this->_rear.h()));
+               Point cc(0.0, 0.0);
+               if (this->right()) {
+                       cc = c.ccl();
+               } else {
+                       cc = c.ccr();
+               }
+               this->_rear.intersects_with(cc, radi);
+               dd = std::min(this->_border[0].edist(this->_rear.i1()),
+                       this->_border[0].edist(this->_rear.i2()));
+               c.st(0.0);
+               c.sp(c.sp() * dd);
+               c.next();
+               c.sp(this->_parking_speed);
+               return PoseRange(c.x(), c.y(), c.h(), c.h());
+       }
+       bool swapped = false;
+       if (!this->right()) {
+               this->swap_side();
+               swapped = true;
+       }
+       this->set_to_start(c);
+       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 += this->_delta_angle_to_slot;
+               c.rotate(rc, this->_delta_angle_to_slot);
+               starts.push_back(c);
+       }
+       for (auto s: starts) {
+               auto r = this->drive_in_slot(s);
+               if (r.size() > 0) {
+                       this->_entries.push_back(r);
+               }
+       }
+       if (this->_entries.size() == 0) {
+               return PoseRange(Pose(0.0, 0.0, 0.0), Pose(0.0, 0.0, 0.0));
+       }
+       if (this->_entries.size() == 1) {
+               auto f = this->_entries.front().front();
+               return PoseRange(f, f);
+       }
+       auto& c1 = this->_entries.front().front();
+       auto& c2 = this->_entries.back().front();
+       PoseRange p(c1, c2);
+       if (swapped) {
+               this->swap_side();
+               p.reflect(this->_entry);
+       }
+       return p;
+}
+
+PoseRange
+ParkingSlot::recompute_entry(PoseRange p)
+{
+       p.rotate(Point(0.0, 0.0), this->h());
+       p.translate(this->_border[0]);
+       if (!this->right()) {
+               p.reflect(this->_entry);
+       }
+       return p;
+}
+
+void
+ParkingSlot::gen_gnuplot_to(std::ostream& out)
+{
+       this->rear().gen_gnuplot_to(out);
+       this->curb().gen_gnuplot_to(out);
+       this->front().gen_gnuplot_to(out);
+}
+
+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