]> rtime.felk.cvut.cz Git - hubacji1/bcar.git/blobdiff - src/bcar.cc
Add line middle point getter
[hubacji1/bcar.git] / src / bcar.cc
index 638db3e03bc0a021e0105d840ca3cb9e1a8d7fcc..b3748dbe9ca0c3cbae6dce9c82a201ab796daa8e 100644 (file)
@@ -1,13 +1,13 @@
 #include <cmath>
 #include "bcar.hh"
 
-using namespace bcar;
+namespace bcar {
 
-Point::Point(double x, double y) : x_(x), y_(y)
+Point::Point()
 {
 }
 
-Point::Point() : Point::Point(0.0, 0.0)
+Point::Point(double x, double y) : x_(x), y_(y)
 {
 }
 
@@ -79,46 +79,117 @@ Point::inside_of(std::vector<Point> const& poly) const
        return c;
 }
 
-Line::Line(Point const& fp, Point const& lp): first(fp), last(lp),
-               intersection1(Point(0.0, 0.0)), intersection2(Point(0.0, 0.0))
+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::translate(Point const& p)
+{
+       this->x_ += p.x();
+       this->y_ += p.y();
+}
+
+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::fp() const&
+Line::e() const&
 {
-       return this->first;
+       return this->e_;
 }
 
 Point
-Line::lp() const&
+Line::m() const
 {
-       return this->last;
+       return Point((this->b_.x() + this->e_.x()) / 2.0,
+               (this->b_.y() + this->e_.y()) / 2.0);
 }
 
 Point
-Line::in1() const&
+Line::i1() const&
 {
-       return this->intersection1;
+       return this->i1_;
 }
 
 Point
-Line::in2() const&
+Line::i2() const&
 {
-       return this->intersection2;
+       return this->i2_;
 }
 
 bool
 Line::intersects_with(Line const& li)
 {
-       auto x1 = this->fp().x();
-       auto y1 = this->fp().y();
-       auto x2 = this->lp().x();
-       auto y2 = this->lp().y();
-       auto x3 = li.fp().x();
-       auto y3 = li.fp().y();
-       auto x4 = li.lp().x();
-       auto y4 = li.lp().y();
+       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;
@@ -129,20 +200,20 @@ 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));
+       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->fp().x();
-       auto y1 = this->fp().y();
-       auto x2 = this->lp().x();
-       auto y2 = this->lp().y();
+       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;
@@ -150,7 +221,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;
@@ -168,51 +239,34 @@ Line::intersects_with(Point const& c, double const r)
        iy1 += cy;
        double iy2 = (-D*dx - std::abs(dy)*sqrt(r*r * dr*dr - D*D)) / (dr*dr);
        iy2 += cy;
-       this->intersection1.x(ix1);
-       this->intersection1.y(iy1);
-       this->intersection2.x(ix2);
-       this->intersection2.y(iy2);
+       this->i1_.x(ix1);
+       this->i1_.y(iy1);
+       this->i2_.x(ix2);
+       this->i2_.y(iy2);
        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_;
+       return this->b_.edist(this->e_);
 }
 
-void
-Pose::x(double x)
+double
+Line::h() const
 {
-       this->x_ = x;
+       return atan2(this->e_.y() - this->b_.y(), this->e_.x() - this->b_.x());
 }
 
-double
-Pose::y() const
+std::ostream&
+operator<<(std::ostream& out, Line const& li)
 {
-       return this->y_;
+       out << "[" << li.b_ << "," << li.e_ << "]";
+       return out;
 }
 
-void
-Pose::y(double y)
+Pose::Pose(double x, double y, double h) : Point(x, y), h_(h)
 {
-       this->y_ = y;
 }
 
 double
@@ -233,18 +287,33 @@ 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());
+}
+
+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&
@@ -254,41 +323,86 @@ operator<<(std::ostream& out, Pose const& p)
        return out;
 }
 
-double
-PoseRange::b() const
+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))
 {
-       return this->h();
 }
 
-void
-PoseRange::b(double b)
+Pose
+PoseRange::bp() const
+{
+       return this->bp_;
+}
+
+Pose
+PoseRange::ep() const
 {
-       this->h(b);
+       return this->ep_;
+}
+
+double
+PoseRange::b() const
+{
+       return std::min(this->bp_.h(), this->ep_.h());
 }
 
 double
 PoseRange::e() const
 {
-       return this->e_;
+       return std::max(this->bp_.h(), this->ep_.h());
 }
 
 void
-PoseRange::e(double e)
+PoseRange::translate(Point const& p)
 {
-       while (e < -M_PI) {
-               e += 2 * M_PI;
-       }
-       while (e > +M_PI) {
-               e -= 2 * M_PI;
-       }
-       this->e_ = e;
+       this->bp_.translate(p);
+       this->ep_.translate(p);
+       this->set_xyh();
 }
 
 void
 PoseRange::rotate(Point const& c, double const angl)
 {
-       Pose::rotate(c, angl);
-       this->e(this->e() + 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&
@@ -302,61 +416,61 @@ operator<<(std::ostream& out, PoseRange const& p)
 double
 CarSize::ctc() const
 {
-       return this->curb_to_curb;
+       return this->curb_to_curb_;
 }
 
 void
 CarSize::ctc(double ctc)
 {
-       this->curb_to_curb = ctc;
+       this->curb_to_curb_ = ctc;
 }
 
 double
 CarSize::wb() const
 {
-       return this->wheelbase;
+       return this->wheelbase_;
 }
 
 void
 CarSize::wb(double wb)
 {
-       this->wheelbase = wb;
+       this->wheelbase_ = wb;
 }
 
 double
 CarSize::w() const
 {
-       return this->width;
+       return this->width_;
 }
 
 void
 CarSize::w(double w)
 {
-       this->width = w;
+       this->width_ = w;
 }
 
 double
 CarSize::len() const
 {
-       return this->length;
+       return this->length_;
 }
 
 void
 CarSize::len(double len)
 {
-       this->length = len;
+       this->length_ = len;
 }
 
 double
 CarSize::df() const
 {
-       return this->distance_to_front;
+       return this->distance_to_front_;
 }
 
 void
 CarSize::df(double df)
 {
-       this->distance_to_front = df;
+       this->distance_to_front_ = df;
 }
 
 double
@@ -373,40 +487,73 @@ 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
 {
-       return this->speed;
+       return this->speed_;
 }
 
 void
 CarMove::sp(double sp)
 {
-       this->speed = sp;
+       this->speed_ = sp;
 }
 
 double
 CarMove::st() const
 {
-       return this->steer;
+       return this->steer_;
 }
 
 void
 CarMove::st(double st)
 {
-       this->steer = st;
+       this->steer_ = st;
 }
 
 bool
 BicycleCar::drivable(Pose const& p) const
 {
-    return this->drivable(p, p.h(), p.h());
+       return this->drivable(PoseRange(p, p));
 }
 
 bool
-BicycleCar::drivable(Pose const& p, double b, double e) const
+BicycleCar::drivable(PoseRange const& p) const
 {
-       // assert p.h() == (b + 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;
@@ -422,7 +569,7 @@ BicycleCar::drivable(Pose const& p, 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);
+               z.h(p.e());
                h_d = p.h() - this->h();
                z.rotate(this->ccl(), h_d);
                // assert z.h() == p.h()
@@ -437,7 +584,7 @@ BicycleCar::drivable(Pose const& p, 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);
+               z.h(p.e());
                h_d = p.h() - this->h();
                z.rotate(this->ccl(), h_d);
                // assert z.h() == p.h()
@@ -453,7 +600,7 @@ BicycleCar::drivable(Pose const& p, 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);
+               z.h(p.b());
                h_d = p.h() - this->h();
                z.rotate(this->ccr(), h_d);
                // assert z.h() == p.h()
@@ -468,7 +615,7 @@ BicycleCar::drivable(Pose const& p, 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);
+               z.h(p.b());
                h_d = p.h() - this->h();
                z.rotate(this->ccr(), h_d);
                // assert z.h() == p.h()
@@ -488,40 +635,6 @@ BicycleCar::drivable(Pose const& p, 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()
 {
@@ -600,6 +713,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
 {
@@ -656,3 +817,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