]> rtime.felk.cvut.cz Git - hubacji1/bcar.git/blobdiff - src/bcar.cc
Add pose range constructor
[hubacji1/bcar.git] / src / bcar.cc
index 248ea49b3f31449ddd75dfa4750050c2632681a5..673ec198853baafcfec05e29f46e2b25c9dfc007 100644 (file)
-#include "bcar.h"
-#include "pslot.h"
+#include <cmath>
+#include "bcar.hh"
 
-// kinematic constraints
-bool BicycleCar::drivable(const BicycleCar &bc) const
+namespace bcar {
+
+Point::Point()
+{
+}
+
+Point::Point(double x, double y) : x_(x), y_(y)
+{
+}
+
+double
+Point::x() const
+{
+       return this->x_;
+}
+
+void
+Point::x(double x)
+{
+       this->x_ = x;
+}
+
+double
+Point::y() const
+{
+       return this->y_;
+}
+
+void
+Point::y(double y)
+{
+       this->y_ = y;
+}
+
+double
+Point::min_angle_between(Point const& p1, Point const& p2) const
+{
+       double d1x = p1.x() - this->x();
+       double d1y = p1.y() - this->y();
+       double d2x = p2.x() - p1.x();
+       double d2y = p2.y() - p1.y();
+
+       double dot = d1x*d2x + d1y*d2y;
+       double d1 = sqrt(d1x*d1x + d1y*d1y);
+       double d2 = sqrt(d2x*d2x + d2y*d2y);
+
+       double delta = acos(dot / (d1 * d2));
+       return std::min(delta, M_PI - delta);
+}
+
+bool
+Point::inside_of(std::vector<Point> const& poly) const
+{
+       unsigned int num = poly.size();
+       unsigned int j = num - 1;
+       bool c = false;
+       for (unsigned int i = 0; i < num; i++) {
+               if (this->x() == poly[i].x() && this->y() == poly[i].y()) {
+                       return true;
+               }
+               if ((poly[i].y() > this->y()) != (poly[j].y() > this->y())) {
+                       auto slope1 = this->x() - poly[i].x();
+                       slope1 *= poly[j].y() - poly[i].y();
+                       auto slope2 = poly[j].x() - poly[i].x();
+                       slope2 *= this->y() - poly[i].y();
+                       auto slope = slope1 - slope2;
+                       if (slope == 0.0) {
+                               return true;
+                       }
+                       if ((slope < 0.0) != (poly[j].y() < poly[i].y())) {
+                               c = !c;
+                       }
+               }
+               j = i;
+       }
+       return c;
+}
+
+bool
+Point::on_right_side_of(Line const& li) const
+{
+       auto x1 = li.b().x();
+       auto y1 = li.b().y();
+       auto x2 = li.e().x();
+       auto y2 = li.e().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());
+}
+
+void
+Point::reflect(Line const& li)
+{
+       this->rotate(li.b(), -li.h());
+       this->y_ -= li.b().y();
+       this->y_ *= -1.0;
+       this->y_ += li.b().y();
+       this->rotate(li.b(), li.h());
+}
+
+double
+Point::edist(Point const& p) const
+{
+       return sqrt(pow(p.x() - this->x_, 2.0) + pow(p.y() - this->y_, 2.0));
+}
+
+bool
+Point::operator==(Point const& p)
+{
+       return this->x() == p.x() && this->y() == p.y();
+}
+
+std::ostream&
+operator<<(std::ostream& out, Point const& p)
+{
+       out << "[" << p.x() << "," << p.y() << "]";
+       return out;
+}
+
+Line::Line(Point const& b, Point const& e): b_(b), e_(e)
+{
+}
+
+Point
+Line::b() const&
+{
+       return this->b_;
+}
+
+Point
+Line::e() const&
+{
+       return this->e_;
+}
+
+Point
+Line::i1() const&
+{
+       return this->i1_;
+}
+
+Point
+Line::i2() const&
+{
+       return this->i2_;
+}
+
+bool
+Line::intersects_with(Line const& li)
+{
+       auto x1 = this->b_.x();
+       auto y1 = this->b_.y();
+       auto x2 = this->e_.x();
+       auto y2 = this->e_.y();
+       auto x3 = li.b().x();
+       auto y3 = li.b().y();
+       auto x4 = li.e().x();
+       auto y4 = li.e().y();
+       double deno = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
+       if (deno == 0.0) {
+               return false;
+       }
+       double t = (x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4);
+       t /= deno;
+       double u = (x1 - x2) * (y1 - y3) - (y1 - y2) * (x1 - x3);
+       u *= -1.0;
+       u /= deno;
+       if (t < 0.0 || t > 1.0 || u < 0.0 || u > 1.0) {
+               return false;
+       }
+       this->i1_.x(x1 + t * (x2 - x1));
+       this->i1_.y(y1 + t * (y2 - y1));
+       return true;
+}
+
+bool
+Line::intersects_with(Point const& c, double const r)
+{
+       auto x1 = this->b_.x();
+       auto y1 = this->b_.y();
+       auto x2 = this->e_.x();
+       auto y2 = this->e_.y();
+       auto cx = c.x();
+       auto cy = c.y();
+       x2 -= cx;
+       x1 -= cx;
+       y2 -= cy;
+       y1 -= cy;
+       if (y1 == y2) {
+               y1 += 0.00001;
+       }
+       double dx = x2 - x1;
+       double dy = y2 - y1;
+       double dr = sqrt(dx*dx + dy*dy);
+       double D = x1*y2 - x2*y1;
+       if (r*r * dr*dr - D*D < 0.0) {
+               return false;
+       }
+       // intersection coordinates
+       double ix1 = (D*dy + sgn(dy)*dx*sqrt(r*r * dr*dr - D*D)) / (dr*dr);
+       ix1 += cx;
+       double ix2 = (D*dy - sgn(dy)*dx*sqrt(r*r * dr*dr - D*D)) / (dr*dr);
+       ix2 += cx;
+       double iy1 = (-D*dx + std::abs(dy)*sqrt(r*r * dr*dr - D*D)) / (dr*dr);
+       iy1 += cy;
+       double iy2 = (-D*dx - std::abs(dy)*sqrt(r*r * dr*dr - D*D)) / (dr*dr);
+       iy2 += cy;
+       this->i1_.x(ix1);
+       this->i1_.y(iy1);
+       this->i2_.x(ix2);
+       this->i2_.y(iy2);
+       return true;
+}
+
+double
+Line::len() const
+{
+       return this->b_.edist(this->e_);
+}
+
+double
+Line::h() const
+{
+       return atan2(this->e_.y() - this->b_.y(), this->e_.x() - this->b_.x());
+}
+
+std::ostream&
+operator<<(std::ostream& out, Line const& li)
+{
+       out << "[" << li.b_ << "," << li.e_ << "]";
+       return out;
+}
+
+Pose::Pose(double x, double y, double h) : Point(x, y), h_(h)
+{
+}
+
+double
+Pose::h() const
+{
+       return this->h_;
+}
+
+void
+Pose::h(double h)
+{
+       while (h < -M_PI) {
+               h += 2 * M_PI;
+       }
+       while (h > +M_PI) {
+               h -= 2 * M_PI;
+       }
+       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)
+{
+       Point::rotate(c, angl);
+       this->h(this->h() + angl);
+}
+
+void
+Pose::reflect(Line const& li)
+{
+       Point::reflect(li);
+       double dh = li.h() - this->h();
+       this->h(this->h() + 2.0 * dh);
+}
+
+bool
+Pose::operator==(Pose const& p)
+{
+       return this->x() == p.x() && this->y() == p.y() && this->h() == p.h();
+}
+
+std::ostream&
+operator<<(std::ostream& out, Pose const& p)
+{
+       out << "[" << p.x() << "," << p.y() << "," << p.h() << "]";
+       return out;
+}
+
+void
+PoseRange::set_xyh()
+{
+       double clen = 10.0;
+       double bpbx = this->bp_.x() - clen * cos(this->bp_.h());
+       double bpby = this->bp_.y() - clen * sin(this->bp_.h());
+       double bpfx = this->bp_.x() + clen * cos(this->bp_.h());
+       double bpfy = this->bp_.y() + clen * sin(this->bp_.h());
+       Line li1(Point(bpbx, bpby), Point(bpfx, bpfy));
+       double epbx = this->ep_.x() - clen * cos(this->ep_.h());
+       double epby = this->ep_.y() - clen * sin(this->ep_.h());
+       double epfx = this->ep_.x() + clen * cos(this->ep_.h());
+       double epfy = this->ep_.y() + clen * sin(this->ep_.h());
+       Line li2(Point(epbx, epby), Point(epfx, epfy));
+       li1.intersects_with(li2);
+       this->x(li1.i1().x());
+       this->y(li1.i1().y());
+       this->h((this->b() + this->e()) / 2.0);
+}
+
+PoseRange::PoseRange(Pose bp, Pose ep) : bp_(bp), ep_(ep)
+{
+       if (this->bp_ == this->ep_) {
+               this->set_pose(this->ep_);
+       } else {
+               this->set_xyh();
+       }
+}
+
+PoseRange::PoseRange(double x, double y, double b, double e)
+               : PoseRange(Pose(x, y, b), Pose(x, y, e))
+{
+}
+
+Pose
+PoseRange::bp() const
+{
+       return this->bp_;
+}
+
+Pose
+PoseRange::ep() const
+{
+       return this->ep_;
+}
+
+double
+PoseRange::b() const
+{
+       return std::min(this->bp_.h(), this->ep_.h());
+}
+
+double
+PoseRange::e() const
+{
+       return std::max(this->bp_.h(), this->ep_.h());
+}
+
+void
+PoseRange::rotate(Point const& c, double const angl)
+{
+       this->bp_.rotate(c, angl);
+       this->ep_.rotate(c, angl);
+       this->set_xyh();
+}
+
+void
+PoseRange::reflect(Line const& li)
+{
+       this->bp_.reflect(li);
+       this->ep_.reflect(li);
+       this->set_xyh();
+}
+
+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
 {
-    return this->drivable(bc, bc.h(), bc.h());
+       return this->curb_to_curb_;
 }
-bool BicycleCar::drivable(const BicycleCar &bc, double b, double e) const
+
+void
+CarSize::ctc(double ctc)
+{
+       this->curb_to_curb_ = ctc;
+}
+
+double
+CarSize::wb() const
+{
+       return this->wheelbase_;
+}
+
+void
+CarSize::wb(double wb)
+{
+       this->wheelbase_ = wb;
+}
+
+double
+CarSize::w() const
 {
-       // assert bc.h() == (b + e) / 2.0
-       double a_1 = atan2(bc.y() - this->y(), bc.x() - this->x()) - this->h();
+       return this->width_;
+}
+
+void
+CarSize::w(double w)
+{
+       this->width_ = w;
+}
+
+double
+CarSize::len() const
+{
+       return this->length_;
+}
+
+void
+CarSize::len(double len)
+{
+       this->length_ = len;
+}
+
+double
+CarSize::df() const
+{
+       return this->distance_to_front_;
+}
+
+void
+CarSize::df(double df)
+{
+       this->distance_to_front_ = df;
+}
+
+double
+CarSize::dr() const
+{
+       return this->len() - this->df();
+}
+
+double
+CarSize::mtr() const
+{
+       auto ctc2 = pow(this->ctc() / 2.0, 2.0);
+       auto wb2 = pow(this->wb(), 2.0);
+       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
+{
+       return this->speed_;
+}
+
+void
+CarMove::sp(double sp)
+{
+       this->speed_ = sp;
+}
+
+double
+CarMove::st() const
+{
+       return this->steer_;
+}
+
+void
+CarMove::st(double st)
+{
+       this->steer_ = st;
+}
+
+bool
+BicycleCar::drivable(Pose const& p) const
+{
+       return this->drivable(PoseRange(p, p));
+}
+
+bool
+BicycleCar::drivable(PoseRange const& p) const
+{
+       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 = p.h() - this->h();
        while (h_d < -M_PI)
                h_d += 2 * M_PI;
        while (h_d > +M_PI)
@@ -24,13 +547,13 @@ bool BicycleCar::drivable(const BicycleCar &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.rotate(this->ccl().x(), this->ccl().y(), h_d);
-               // assert z.h() == bc.h()
-               if (bc.y() == z.y() && bc.x() == z.x()) // bc on zone border
+               z.h(p.e());
+               h_d = p.h() - this->h();
+               z.rotate(this->ccl(), h_d);
+               // assert z.h() == p.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)
@@ -39,13 +562,13 @@ bool BicycleCar::drivable(const BicycleCar &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.rotate(this->ccl().x(), this->ccl().y(), h_d);
-               // assert z.h() == bc.h()
-               if (bc.y() == z.y() && bc.x() == z.x()) // bc on zone border
+               z.h(p.e());
+               h_d = p.h() - this->h();
+               z.rotate(this->ccl(), h_d);
+               // assert z.h() == p.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;
@@ -55,13 +578,13 @@ bool BicycleCar::drivable(const BicycleCar &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.rotate(this->ccr().x(), this->ccr().y(), h_d);
-               // assert z.h() == bc.h()
-               if (bc.y() == z.y() && bc.x() == z.x()) // bc on zone border
+               z.h(p.b());
+               h_d = p.h() - this->h();
+               z.rotate(this->ccr(), h_d);
+               // assert z.h() == p.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)
@@ -70,13 +593,13 @@ bool BicycleCar::drivable(const BicycleCar &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.rotate(this->ccr().x(), this->ccr().y(), h_d);
-               // assert z.h() == bc.h()
-               if (bc.y() == z.y() && bc.x() == z.x()) // bc on zone border
+               z.h(p.b());
+               h_d = p.h() - this->h();
+               z.rotate(this->ccr(), h_d);
+               // assert z.h() == p.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;
@@ -90,321 +613,187 @@ bool BicycleCar::drivable(const BicycleCar &bc, double b, double e) const
        return false;
 }
 
-double BicycleCar::iradi() const
-{
-       return this->mtr() - this->w() / 2;
-}
-
-double BicycleCar::ofradi() const
-{
-       return sqrt(pow(this->mtr() + this->w() / 2, 2) + pow(this->df(), 2));
-}
-
-double BicycleCar::orradi() const
-{
-       return sqrt(pow(this->mtr() + this->w() / 2, 2) + pow(this->dr(), 2));
-}
-
-double BicycleCar::perfect_parking_slot_len() const
-{
-       // see Simon R. Blackburn *The Geometry of Perfect Parking*
-       // see https://www.ma.rhul.ac.uk/SRBparking
-       double r = this->ctc() / 2;
-       double l = this->wb();
-       double k = this->df() - this->wb();
-       double w = this->w();
-       return
-               this->l()
-               + sqrt(
-                       (r*r - l*l)
-                       + pow(l + k, 2)
-                       - pow(sqrt(r*r - l*l) - w, 2)
-               )
-               - l
-               - k
-       ;
-}
-
-void BicycleCar::set_max_steer()
+void
+BicycleCar::set_max_steer()
 {
        this->st(atan(this->wb() / this->mtr()));
 }
 
-// car frame
-double BicycleCar::lfx() const
+double
+BicycleCar::lfx() const
 {
        double lfx = this->x();
-       lfx += (this->w() / 2) * cos(this->h() + M_PI / 2);
+       lfx += (this->w() / 2.0) * cos(this->h() + M_PI / 2.0);
        lfx += this->df() * cos(this->h());
-       lfx += this->sd() * cos(this->h());
        return lfx;
 }
 
-double BicycleCar::lfy() const
+double
+BicycleCar::lfy() const
 {
        double lfy = this->y();
-       lfy += (this->w() / 2) * sin(this->h() + M_PI / 2);
+       lfy += (this->w() / 2.0) * sin(this->h() + M_PI / 2.0);
        lfy += this->df() * sin(this->h());
-       lfy += this->sd() * sin(this->h());
        return lfy;
 }
 
-double BicycleCar::lrx() const
+double
+BicycleCar::lrx() const
 {
        double lrx = this->x();
-       lrx += (this->w() / 2) * cos(this->h() + M_PI / 2);
+       lrx += (this->w() / 2.0) * cos(this->h() + M_PI / 2.0);
        lrx += -this->dr() * cos(this->h());
-       lrx += -this->sd() * cos(this->h());
        return lrx;
 }
 
-double BicycleCar::lry() const
+double
+BicycleCar::lry() const
 {
        double lry = this->y();
-       lry += (this->w() / 2) * sin(this->h() + M_PI / 2);
+       lry += (this->w() / 2.0) * sin(this->h() + M_PI / 2.0);
        lry += -this->dr() * sin(this->h());
-       lry += -this->sd() * sin(this->h());
        return lry;
 }
 
-double BicycleCar::rrx() const
+double
+BicycleCar::rrx() const
 {
        double rrx = this->x();
-       rrx += (this->w() / 2) * cos(this->h() - M_PI / 2);
+       rrx += (this->w() / 2.0) * cos(this->h() - M_PI / 2.0);
        rrx += -this->dr() * cos(this->h());
-       rrx += -this->sd() * cos(this->h());
        return rrx;
 }
 
-double BicycleCar::rry() const
+double
+BicycleCar::rry() const
 {
        double rry = this->y();
-       rry += (this->w() / 2) * sin(this->h() - M_PI / 2);
+       rry += (this->w() / 2.0) * sin(this->h() - M_PI / 2.0);
        rry += -this->dr() * sin(this->h());
-       rry += -this->sd() * sin(this->h());
        return rry;
 }
 
-double BicycleCar::rfx() const
+double
+BicycleCar::rfx() const
 {
        double rfx = this->x();
-       rfx += (this->w() / 2) * cos(this->h() - M_PI / 2);
+       rfx += (this->w() / 2.0) * cos(this->h() - M_PI / 2.0);
        rfx += this->df() * cos(this->h());
-       rfx += this->sd() * cos(this->h());
        return rfx;
 }
 
-double BicycleCar::rfy() const
+double
+BicycleCar::rfy() const
 {
        double rfy = this->y();
-       rfy += (this->w() / 2) * sin(this->h() - M_PI / 2);
+       rfy += (this->w() / 2.0) * sin(this->h() - M_PI / 2.0);
        rfy += this->df() * sin(this->h());
-       rfy += this->sd() * sin(this->h());
        return rfy;
 }
 
-double BicycleCar::ralx() const
+Point
+BicycleCar::lf() const
 {
-       double lrx = this->x();
-       lrx += (this->w() / 2) * cos(this->h() + M_PI / 2);
-       return lrx;
+       return Point(this->lfx(), this->lfy());
 }
-double BicycleCar::raly() const
+
+Point
+BicycleCar::lr() const
 {
-       double lry = this->y();
-       lry += (this->w() / 2) * sin(this->h() + M_PI / 2);
-       return lry;
+       return Point(this->lrx(), this->lry());
 }
 
-double BicycleCar::rarx() const
+Point
+BicycleCar::rr() const
 {
-       double rrx = this->x();
-       rrx += (this->w() / 2) * cos(this->h() - M_PI / 2);
-       return rrx;
+       return Point(this->rrx(), this->rry());
 }
 
-double BicycleCar::rary() const
+Point
+BicycleCar::rf() const
 {
-       double rry = this->y();
-       rry += (this->w() / 2) * sin(this->h() - M_PI / 2);
-       return rry;
+       return Point(this->rfx(), this->rfy());
 }
 
-BicycleCar BicycleCar::ccl() const
+Line
+BicycleCar::left() const
 {
-       BicycleCar bc;
-       bc.x(this->x() + this->mtr() * cos(this->h() + M_PI / 2));
-       bc.y(this->y() + this->mtr() * sin(this->h() + M_PI / 2));
-       bc.h(this->h());
-       return bc;
+       return Line(this->lr(), this->lf());
 }
 
-BicycleCar BicycleCar::ccr() const
+Line
+BicycleCar::rear() const
 {
-       BicycleCar bc;
-       bc.x(this->x() + this->mtr() * cos(this->h() - M_PI / 2));
-       bc.y(this->y() + this->mtr() * sin(this->h() - M_PI / 2));
-       bc.h(this->h());
-       return bc;
+       return Line(this->lr(), this->rr());
 }
 
-// moving
-void BicycleCar::next()
+Line
+BicycleCar::right() const
 {
-       this->x(this->x() + this->sp() * cos(this->h()));
-       this->y(this->y() + this->sp() * sin(this->h()));
-       this->h(this->h() + this->sp() / this->wb() * tan(this->st()));
+       return Line(this->rr(), this->rf());
 }
 
-void BicycleCar::rotate(double cx, double cy, double angl)
+Line
+BicycleCar::front() const
 {
-       double px = this->x();
-       double py = this->y();
-       px -= cx;
-       py -= cy;
-       double nx = px * cos(angl) - py * sin(angl);
-       double ny = px * sin(angl) + py * cos(angl);
-       this->h(this->h() + angl);
-       this->x(nx + cx);
-       this->y(ny + cy);
-}
-
-BicycleCar::BicycleCar()
-{
-       // TODO according to mtr_ FIXME
-       this->mtr_ = sqrt(
-                       pow(10.82 / 2, 2)
-                       - pow(this->wb(), 2)
-               )
-               - this->w() / 2
-       ;
-}
-
-std::tuple<bool, unsigned int, unsigned int>
-collide(
-       std::vector<std::tuple<double, double>> &p1,
-       std::vector<std::tuple<double, double>> &p2
-)
-{
-       for (unsigned int i = 0; i < p1.size() - 1; i++) {
-               for (unsigned int j = 0; j < p2.size() - 1; j++) {
-                       auto x = intersect(
-                               std::get<0>(p1[i]),
-                               std::get<1>(p1[i]),
-                               std::get<0>(p1[i + 1]),
-                               std::get<1>(p1[i + 1]),
-                               std::get<0>(p2[j]),
-                               std::get<1>(p2[j]),
-                               std::get<0>(p2[j + 1]),
-                               std::get<1>(p2[j + 1])
-                       );
-                       if (std::get<0>(x))
-                               return std::make_tuple(true, i, j);
-               }
-       }
-       return std::make_tuple(false, 0, 0);
+       return Line(this->rf(), this->lf());
 }
 
-bool
-inside(double x, double y, std::vector<std::tuple<double, double>> &poly)
-{
-       unsigned int i = 0;
-       unsigned int j = 3;
-       bool inside = false;
-       for (i = 0; i < 4; i++) {
-               if (
-                       (std::get<1>(poly[i]) > y) != (std::get<1>(poly[j]) > y)
-                       && (
-                               x < std::get<0>(poly[i])
-                               + (std::get<0>(poly[j]) - std::get<0>(poly[i]))
-                               * (y - std::get<1>(poly[i]))
-                               / (std::get<1>(poly[j]) - std::get<1>(poly[i]))
-                       )
-               )
-                       inside = !inside;
-               j = i;
-       }
-       return inside;
+double
+BicycleCar::ralx() const
+{
+       double lrx = this->x();
+       lrx += (this->w() / 2.0) * cos(this->h() + M_PI / 2.0);
+       return lrx;
 }
-
-std::tuple<bool, double, double>
-intersect(
-       double x1, double y1,
-       double x2, double y2,
-       double x3, double y3,
-       double x4, double y4
-)
+double
+BicycleCar::raly() const
 {
-       double deno = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
-       if (deno == 0)
-               return std::make_tuple(false, 0, 0);
-       double t = (x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4);
-       t /= deno;
-       double u = (x1 - x2) * (y1 - y3) - (y1 - y2) * (x1 - x3);
-       u *= -1;
-       u /= deno;
-       if (t < 0 || t > 1 || u < 0 || u > 1)
-               return std::make_tuple(false, 0, 0);
-       return std::make_tuple(true, x1 + t * (x2 - x1), y1 + t * (y2 - y1));
+       double lry = this->y();
+       lry += (this->w() / 2.0) * sin(this->h() + M_PI / 2.0);
+       return lry;
 }
 
-std::tuple<bool, double, double, double, double>
-intersect(
-       double cx, double cy, double r,
-       double x1, double y1,
-       double x2, double y2
-) {
-       x2 -= cx;
-       x1 -= cx;
-       y2 -= cy;
-       y1 -= cy;
-       if (y1 == y2)
-           y1 += 0.00001;
-       double dx = x2 - x1;
-       double dy = y2 - y1;
-       double dr = sqrt(dx*dx + dy*dy);
-       double D = x1*y2 - x2*y1;
-       if (r*r * dr*dr - D*D < 0)
-       return std::make_tuple(false, 0, 0, 0, 0);
-       // intersection coordinates
-       double ix1 = (D*dy + sgn(dy)*dx*sqrt(r*r * dr*dr - D*D)) / (dr*dr);
-       ix1 += cx;
-       double ix2 = (D*dy - sgn(dy)*dx*sqrt(r*r * dr*dr - D*D)) / (dr*dr);
-       ix2 += cx;
-       double iy1 = (-D*dx + std::abs(dy)*sqrt(r*r * dr*dr - D*D)) / (dr*dr);
-       iy1 += cy;
-       double iy2 = (-D*dx - std::abs(dy)*sqrt(r*r * dr*dr - D*D)) / (dr*dr);
-       iy2 += cy;
-       return std::make_tuple(true, ix1, iy1, ix2, iy2);
+double
+BicycleCar::rarx() const
+{
+       double rrx = this->x();
+       rrx += (this->w() / 2.0) * cos(this->h() - M_PI / 2.0);
+       return rrx;
 }
 
 double
-angle_between_three_points(
-       double x1, double y1,
-       double x2, double y2,
-       double x3, double y3
-) {
-       double d1x = x2 - x1;
-       double d1y = y2 - y1;
-       double d2x = x3 - x2;
-       double d2y = y3 - y2;
+BicycleCar::rary() const
+{
+       double rry = this->y();
+       rry += (this->w() / 2.0) * sin(this->h() - M_PI / 2.0);
+       return rry;
+}
 
-       double dot = d1x*d2x + d1y*d2y;
-       double d1 = sqrt(d1x*d1x + d1y*d1y);
-       double d2 = sqrt(d2x*d2x + d2y*d2y);
+Point
+BicycleCar::ccl() const
+{
+       return Point(
+               this->x() + this->mtr() * cos(this->h() + M_PI / 2.0),
+               this->y() + this->mtr() * sin(this->h() + M_PI / 2.0)
+       );
+}
 
-       double delta = acos(dot / (d1 * d2));
-       return std::min(delta, M_PI - delta);
+Point
+BicycleCar::ccr() const
+{
+       return Point(
+               this->x() + this->mtr() * cos(this->h() - M_PI / 2.0),
+               this->y() + this->mtr() * sin(this->h() - M_PI / 2.0)
+       );
 }
 
-bool
-right_side_of_line(
-       double x1, double y1,
-       double x2, double y2,
-       double x3, double y3
-) {
-       if (sgn((x3 - x1) * (y2 - y1) - (y3 - y1) * (x2 - x1)) < 0)
-               return false;
-       else
-               return true;
+void
+BicycleCar::next()
+{
+       this->x(this->x() + this->sp() * cos(this->h()));
+       this->y(this->y() + this->sp() * sin(this->h()));
+       this->h(this->h() + this->sp() / this->wb() * tan(this->st()));
 }
+
+} // namespace bcar