]> rtime.felk.cvut.cz Git - hubacji1/bcar.git/blobdiff - src/bcar.cc
Add car frame point and side getters
[hubacji1/bcar.git] / src / bcar.cc
index 4305b7cfeb4d467b32fc2558ca24013302b42dbe..32b2b8869ded7e6a1ec7ea3dcae717e1198ef43e 100644 (file)
@@ -1,7 +1,7 @@
 #include <cmath>
 #include "bcar.hh"
 
-using namespace bcar;
+namespace bcar {
 
 Point::Point(double x, double y) : x_(x), y_(y)
 {
@@ -79,6 +79,48 @@ Point::inside_of(std::vector<Point> const& poly) const
        return c;
 }
 
+bool
+Point::on_right_side_of(Line const& li) const
+{
+       auto x1 = li.fp().x();
+       auto y1 = li.fp().y();
+       auto x2 = li.lp().x();
+       auto y2 = li.lp().y();
+       auto x3 = this->x_;
+       auto y3 = this->y_;
+       if (sgn((x3 - x1) * (y2 - y1) - (y3 - y1) * (x2 - x1)) < 0.0) {
+               return false;
+       } else {
+               return true;
+       }
+}
+
+void
+Point::rotate(Point const& c, double const angl)
+{
+       double px = this->x();
+       double py = this->y();
+       px -= c.x();
+       py -= c.y();
+       double nx = px * cos(angl) - py * sin(angl);
+       double ny = px * sin(angl) + py * cos(angl);
+       this->x(nx + c.x());
+       this->y(ny + c.y());
+}
+
+double
+Point::edist(Point const& p) const
+{
+       return sqrt(pow(p.x() - this->x_, 2.0) + pow(p.y() - this->y_, 2.0));
+}
+
+std::ostream&
+operator<<(std::ostream& out, Point const& p)
+{
+       out << "[" << p.x() << "," << p.y() << "]";
+       return out;
+}
+
 Line::Line(Point const& fp, Point const& lp): first(fp), last(lp),
                intersection1(Point(0.0, 0.0)), intersection2(Point(0.0, 0.0))
 {
@@ -129,7 +171,7 @@ Line::intersects_with(Line const& li)
        u *= -1.0;
        u /= deno;
        if (t < 0.0 || t > 1.0 || u < 0.0 || u > 1.0) {
-               false;
+               return false;
        }
        this->intersection1.x(x1 + t * (x2 - x1));
        this->intersection1.y(y1 + t * (y2 - y1));
@@ -150,7 +192,7 @@ Line::intersects_with(Point const& c, double const r)
        y2 -= cy;
        y1 -= cy;
        if (y1 == y2) {
-           y1 += 0.00001;
+               y1 += 0.00001;
        }
        double dx = x2 - x1;
        double dy = y2 - y1;
@@ -175,44 +217,27 @@ Line::intersects_with(Point const& c, double const r)
        return true;
 }
 
-bool
-Line::is_on_right_side(Point const& p) const
-{
-       auto x1 = this->fp().x();
-       auto y1 = this->fp().y();
-       auto x2 = this->lp().x();
-       auto y2 = this->lp().y();
-       auto x3 = p.x();
-       auto y3 = p.y();
-       if (sgn((x3 - x1) * (y2 - y1) - (y3 - y1) * (x2 - x1)) < 0.0) {
-               return false;
-       } else {
-               return true;
-       }
-}
-
 double
-Pose::x() const
+Line::len() const
 {
-       return this->x_;
+       double dx = this->lp().x() - this->fp().x();
+       double dy = this->lp().y() - this->fp().y();
+       return sqrt(dx * dx + dy * dy);
 }
 
-void
-Pose::x(double x)
+std::ostream&
+operator<<(std::ostream& out, Line const& li)
 {
-       this->x_ = x;
+       out << "[" << li.first << "," << li.last << "]";
+       return out;
 }
 
-double
-Pose::y() const
+Pose::Pose() : Point()
 {
-       return this->y_;
 }
 
-void
-Pose::y(double y)
+Pose::Pose(double x, double y, double h) : Point(x, y), h_(h)
 {
-       this->y_ = y;
 }
 
 double
@@ -233,18 +258,19 @@ Pose::h(double h)
        this->h_ = h;
 }
 
+void
+Pose::set_pose(Pose const& p)
+{
+       this->x(p.x());
+       this->y(p.y());
+       this->h(p.h());
+}
+
 void
 Pose::rotate(Point const& c, double const angl)
 {
-       double px = this->x();
-       double py = this->y();
-       px -= c.x();
-       py -= c.y();
-       double nx = px * cos(angl) - py * sin(angl);
-       double ny = px * sin(angl) + py * cos(angl);
+       Point::rotate(c, angl);
        this->h(this->h() + angl);
-       this->x(nx + c.x());
-       this->y(ny + c.y());
 }
 
 std::ostream&
@@ -254,6 +280,51 @@ operator<<(std::ostream& out, Pose const& p)
        return out;
 }
 
+double
+PoseRange::b() const
+{
+       return this->h();
+}
+
+void
+PoseRange::b(double b)
+{
+       this->h(b);
+}
+
+double
+PoseRange::e() const
+{
+       return this->e_;
+}
+
+void
+PoseRange::e(double e)
+{
+       while (e < -M_PI) {
+               e += 2 * M_PI;
+       }
+       while (e > +M_PI) {
+               e -= 2 * M_PI;
+       }
+       this->e_ = e;
+}
+
+void
+PoseRange::rotate(Point const& c, double const angl)
+{
+       Pose::rotate(c, angl);
+       this->e(this->e() + angl);
+}
+
+std::ostream&
+operator<<(std::ostream& out, PoseRange const& p)
+{
+       out << "[" << p.x() << "," << p.y() << "," << p.b() << "," << p.e();
+       out << "]";
+       return out;
+}
+
 double
 CarSize::ctc() const
 {
@@ -328,6 +399,40 @@ CarSize::mtr() const
        return sqrt(ctc2 - wb2) - this->w() / 2.0;
 }
 
+double
+CarSize::iradi() const
+{
+       return this->mtr() - this->w() / 2;
+}
+
+double
+CarSize::ofradi() const
+{
+       auto mtrw2 = pow(this->mtr() + this->w() / 2.0, 2.0);
+       auto df2 = pow(this->df(), 2.0);
+       return sqrt(mtrw2 + df2);
+}
+
+double
+CarSize::orradi() const
+{
+       auto mtrw2 = pow(this->mtr() + this->w() / 2.0, 2.0);
+       auto dr2 = pow(this->dr(), 2.0);
+       return sqrt(mtrw2 + dr2);
+}
+
+double
+CarSize::perfect_parking_slot_len() const
+{
+       auto r = this->ctc() / 2.0;
+       auto l = this->wb();
+       auto k = this->df() - this->wb();
+       auto w = this->w();
+       auto r2l2 = r * r - l * l;
+       auto s = r2l2 + pow(l + k, 2.0) - pow(sqrt(r2l2) - w, 2.0);
+       return this->len() + sqrt(s) - l - k;
+}
+
 double
 CarMove::sp() const
 {
@@ -353,21 +458,26 @@ CarMove::st(double st)
 }
 
 bool
-BicycleCar::drivable(BicycleCar const& bc) const
+BicycleCar::drivable(Pose const& p) const
 {
-    return this->drivable(bc, bc.h(), bc.h());
+       PoseRange pr;
+       pr.x(p.x());
+       pr.y(p.y());
+       pr.b(p.h());
+       pr.e(p.h());
+       return this->drivable(pr);
 }
 
 bool
-BicycleCar::drivable(BicycleCar const& bc, double b, double e) const
+BicycleCar::drivable(PoseRange const& p) const
 {
-       // assert bc.h() == (b + e) / 2.0
-       double a_1 = atan2(bc.y() - this->y(), bc.x() - this->x()) - this->h();
+       double h = (p.b() + p.e()) / 2.0;
+       double a_1 = atan2(p.y() - this->y(), p.x() - this->x()) - this->h();
        while (a_1 < -M_PI)
                a_1 += 2 * M_PI;
        while (a_1 > +M_PI)
                a_1 -= 2 * M_PI;
-       double h_d = bc.h() - this->h();
+       double h_d = h - this->h();
        while (h_d < -M_PI)
                h_d += 2 * M_PI;
        while (h_d > +M_PI)
@@ -377,13 +487,13 @@ BicycleCar::drivable(BicycleCar const& bc, double b, double e) const
                return true;
        } else if (0 < a_1 && a_1 <= M_PI/2) { // left front
                BicycleCar z(*this); // zone border
-               z.h(e);
-               h_d = bc.h() - this->h();
+               z.h(p.e());
+               h_d = h - this->h();
                z.rotate(this->ccl(), h_d);
-               // assert z.h() == bc.h()
-               if (bc.y() == z.y() && bc.x() == z.x()) // bc on zone border
+               // assert z.h() == h
+               if (p.y() == z.y() && p.x() == z.x()) // p on zone border
                        return true;
-               a_2 = atan2(bc.y() - z.y(), bc.x() - z.x());
+               a_2 = atan2(p.y() - z.y(), p.x() - z.x());
                while (a_2 < -M_PI)
                        a_2 += 2 * M_PI;
                while (a_2 > +M_PI)
@@ -392,13 +502,13 @@ BicycleCar::drivable(BicycleCar const& bc, double b, double e) const
                        return true;
        } else if (M_PI/2 < a_1 && a_1 <= M_PI) { // left rear
                BicycleCar z(*this); // zone border
-               z.h(e);
-               h_d = bc.h() - this->h();
+               z.h(p.e());
+               h_d = h - this->h();
                z.rotate(this->ccl(), h_d);
-               // assert z.h() == bc.h()
-               if (bc.y() == z.y() && bc.x() == z.x()) // bc on zone border
+               // assert z.h() == h
+               if (p.y() == z.y() && p.x() == z.x()) // p on zone border
                        return true;
-               a_2 = atan2(bc.y() - z.y(), bc.x() - z.x());
+               a_2 = atan2(p.y() - z.y(), p.x() - z.x());
                a_2 -= M_PI;
                while (a_2 < -M_PI)
                        a_2 += 2 * M_PI;
@@ -408,13 +518,13 @@ BicycleCar::drivable(BicycleCar const& bc, double b, double e) const
                        return true;
        } else if (0 > a_1 && a_1 >= -M_PI/2) { // right front
                BicycleCar z(*this); // zone border
-               z.h(b);
-               h_d = bc.h() - this->h();
+               z.h(p.b());
+               h_d = h - this->h();
                z.rotate(this->ccr(), h_d);
-               // assert z.h() == bc.h()
-               if (bc.y() == z.y() && bc.x() == z.x()) // bc on zone border
+               // assert z.h() == h
+               if (p.y() == z.y() && p.x() == z.x()) // p on zone border
                        return true;
-               a_2 = atan2(bc.y() - z.y(), bc.x() - z.x());
+               a_2 = atan2(p.y() - z.y(), p.x() - z.x());
                while (a_2 < -M_PI)
                        a_2 += 2 * M_PI;
                while (a_2 > +M_PI)
@@ -423,13 +533,13 @@ BicycleCar::drivable(BicycleCar const& bc, double b, double e) const
                        return true;
        } else if (-M_PI/2 > a_1 && a_1 >= -M_PI) { // right rear
                BicycleCar z(*this); // zone border
-               z.h(b);
-               h_d = bc.h() - this->h();
+               z.h(p.b());
+               h_d = h - this->h();
                z.rotate(this->ccr(), h_d);
-               // assert z.h() == bc.h()
-               if (bc.y() == z.y() && bc.x() == z.x()) // bc on zone border
+               // assert z.h() == h
+               if (p.y() == z.y() && p.x() == z.x()) // p on zone border
                        return true;
-               a_2 = atan2(bc.y() - z.y(), bc.x() - z.x());
+               a_2 = atan2(p.y() - z.y(), p.x() - z.x());
                a_2 -= M_PI;
                while (a_2 < -M_PI)
                        a_2 += 2 * M_PI;
@@ -443,40 +553,6 @@ BicycleCar::drivable(BicycleCar const& bc, double b, double e) const
        return false;
 }
 
-double
-BicycleCar::iradi() const
-{
-       return this->mtr() - this->w() / 2;
-}
-
-double
-BicycleCar::ofradi() const
-{
-       auto mtrw2 = pow(this->mtr() + this->w() / 2.0, 2.0);
-       auto df2 = pow(this->df(), 2.0);
-       return sqrt(mtrw2 + df2);
-}
-
-double
-BicycleCar::orradi() const
-{
-       auto mtrw2 = pow(this->mtr() + this->w() / 2.0, 2.0);
-       auto dr2 = pow(this->dr(), 2.0);
-       return sqrt(mtrw2 + dr2);
-}
-
-double
-BicycleCar::perfect_parking_slot_len() const
-{
-       auto r = this->ctc() / 2.0;
-       auto l = this->wb();
-       auto k = this->df() - this->wb();
-       auto w = this->w();
-       auto r2l2 = r * r - l * l;
-       auto s = r2l2 + pow(l + k, 2.0) - pow(sqrt(r2l2) - w, 2.0);
-       return this->len() + sqrt(s) - l - k;
-}
-
 void
 BicycleCar::set_max_steer()
 {
@@ -555,6 +631,54 @@ BicycleCar::rfy() const
        return rfy;
 }
 
+Point
+BicycleCar::lf() const
+{
+       return Point(this->lfx(), this->lfy());
+}
+
+Point
+BicycleCar::lr() const
+{
+       return Point(this->lrx(), this->lry());
+}
+
+Point
+BicycleCar::rr() const
+{
+       return Point(this->rrx(), this->rry());
+}
+
+Point
+BicycleCar::rf() const
+{
+       return Point(this->rfx(), this->rfy());
+}
+
+Line
+BicycleCar::left() const
+{
+       return Line(this->lr(), this->lf());
+}
+
+Line
+BicycleCar::rear() const
+{
+       return Line(this->lr(), this->rr());
+}
+
+Line
+BicycleCar::right() const
+{
+       return Line(this->rr(), this->rf());
+}
+
+Line
+BicycleCar::front() const
+{
+       return Line(this->rf(), this->lf());
+}
+
 double
 BicycleCar::ralx() const
 {
@@ -611,3 +735,5 @@ BicycleCar::next()
        this->y(this->y() + this->sp() * sin(this->h()));
        this->h(this->h() + this->sp() / this->wb() * tan(this->st()));
 }
+
+} // namespace bcar