]> rtime.felk.cvut.cz Git - hubacji1/bcar.git/blobdiff - src/bcar.cc
Add gen plot for lra and rra points
[hubacji1/bcar.git] / src / bcar.cc
index 673ec198853baafcfec05e29f46e2b25c9dfc007..4e635d6c395a9a8f72519f1c94ea6d2fb7b5252b 100644 (file)
@@ -1,3 +1,9 @@
+/*
+ * SPDX-FileCopyrightText: 2021 Jiri Vlasak <jiri.vlasak.2@cvut.cz>
+ *
+ * SPDX-License-Identifier: GPL-3.0-only
+ */
+
 #include <cmath>
 #include "bcar.hh"
 
@@ -7,32 +13,32 @@ Point::Point()
 {
 }
 
-Point::Point(double x, double y) : x_(x), y_(y)
+Point::Point(double x, double y) : _x(x), _y(y)
 {
 }
 
 double
 Point::x() const
 {
-       return this->x_;
+       return this->_x;
 }
 
 void
 Point::x(double x)
 {
-       this->x_ = x;
+       this->_x = x;
 }
 
 double
 Point::y() const
 {
-       return this->y_;
+       return this->_y;
 }
 
 void
 Point::y(double y)
 {
-       this->y_ = y;
+       this->_y = y;
 }
 
 double
@@ -86,8 +92,8 @@ Point::on_right_side_of(Line const& li) const
        auto y1 = li.b().y();
        auto x2 = li.e().x();
        auto y2 = li.e().y();
-       auto x3 = this->x_;
-       auto y3 = this->y_;
+       auto x3 = this->_x;
+       auto y3 = this->_y;
        if (sgn((x3 - x1) * (y2 - y1) - (y3 - y1) * (x2 - x1)) < 0.0) {
                return false;
        } else {
@@ -95,6 +101,13 @@ Point::on_right_side_of(Line const& li) const
        }
 }
 
+void
+Point::translate(Point const& p)
+{
+       this->_x += p.x();
+       this->_y += p.y();
+}
+
 void
 Point::rotate(Point const& c, double const angl)
 {
@@ -112,16 +125,22 @@ 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->_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));
+       return sqrt(pow(p.x() - this->_x, 2.0) + pow(p.y() - this->_y, 2.0));
+}
+
+void
+Point::gen_gnuplot_to(std::ostream& out)
+{
+       out << this->_x << " " << this->_y << std::endl;
 }
 
 bool
@@ -137,41 +156,48 @@ operator<<(std::ostream& out, Point const& p)
        return out;
 }
 
-Line::Line(Point const& b, Point const& e): b_(b), e_(e)
+Line::Line(Point const& b, Point const& e): _b(b), _e(e)
 {
 }
 
 Point
 Line::b() const&
 {
-       return this->b_;
+       return this->_b;
 }
 
 Point
 Line::e() const&
 {
-       return this->e_;
+       return this->_e;
+}
+
+Point
+Line::m() const
+{
+       return Point((this->_b.x() + this->_e.x()) / 2.0,
+               (this->_b.y() + this->_e.y()) / 2.0);
 }
 
 Point
 Line::i1() const&
 {
-       return this->i1_;
+       return this->_i1;
 }
 
 Point
 Line::i2() const&
 {
-       return this->i2_;
+       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 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();
@@ -188,18 +214,18 @@ Line::intersects_with(Line const& li)
        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));
+       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 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;
@@ -225,40 +251,48 @@ 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->i1_.x(ix1);
-       this->i1_.y(iy1);
-       this->i2_.x(ix2);
-       this->i2_.y(iy2);
+       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_);
+       return this->_b.edist(this->_e);
 }
 
 double
 Line::h() const
 {
-       return atan2(this->e_.y() - this->b_.y(), this->e_.x() - this->b_.x());
+       return atan2(this->_e.y() - this->_b.y(), this->_e.x() - this->_b.x());
+}
+
+void
+Line::gen_gnuplot_to(std::ostream& out)
+{
+       this->b().gen_gnuplot_to(out);
+       this->e().gen_gnuplot_to(out);
+       out << std::endl;
 }
 
 std::ostream&
 operator<<(std::ostream& out, Line const& li)
 {
-       out << "[" << li.b_ << "," << li.e_ << "]";
+       out << "[" << li._b << "," << li._e << "]";
        return out;
 }
 
-Pose::Pose(double x, double y, double h) : Point(x, y), h_(h)
+Pose::Pose(double x, double y, double h) : Point(x, y), _h(h)
 {
 }
 
 double
 Pose::h() const
 {
-       return this->h_;
+       return this->_h;
 }
 
 void
@@ -270,7 +304,7 @@ Pose::h(double h)
        while (h > +M_PI) {
                h -= 2 * M_PI;
        }
-       this->h_ = h;
+       this->_h = h;
 }
 
 void
@@ -313,26 +347,36 @@ 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());
+       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());
+       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());
+       double bh = this->b();
+       while (bh < 0.0) {
+               bh += 2.0 * M_PI;
+       }
+       this->_bp.h(bh);
+       double eh = this->e();
+       while (eh < 0.0) {
+               eh += 2.0 * M_PI;
+       }
+       this->_ep.h(eh);
        this->h((this->b() + this->e()) / 2.0);
 }
 
-PoseRange::PoseRange(Pose bp, Pose ep) : bp_(bp), ep_(ep)
+PoseRange::PoseRange(Pose bp, Pose ep) : _bp(bp), _ep(ep)
 {
-       if (this->bp_ == this->ep_) {
-               this->set_pose(this->ep_);
+       if (this->_bp == this->_ep) {
+               this->set_pose(this->_ep);
        } else {
                this->set_xyh();
        }
@@ -346,40 +390,48 @@ PoseRange::PoseRange(double x, double y, double b, double e)
 Pose
 PoseRange::bp() const
 {
-       return this->bp_;
+       return this->_bp;
 }
 
 Pose
 PoseRange::ep() const
 {
-       return this->ep_;
+       return this->_ep;
 }
 
 double
 PoseRange::b() const
 {
-       return std::min(this->bp_.h(), this->ep_.h());
+       return std::min(this->_bp.h(), this->_ep.h());
 }
 
 double
 PoseRange::e() const
 {
-       return std::max(this->bp_.h(), this->ep_.h());
+       return std::max(this->_bp.h(), this->_ep.h());
+}
+
+void
+PoseRange::translate(Point const& p)
+{
+       this->_bp.translate(p);
+       this->_ep.translate(p);
+       this->set_xyh();
 }
 
 void
 PoseRange::rotate(Point const& c, double const angl)
 {
-       this->bp_.rotate(c, angl);
-       this->ep_.rotate(c, 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->_bp.reflect(li);
+       this->_ep.reflect(li);
        this->set_xyh();
 }
 
@@ -394,61 +446,73 @@ 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::wwm() const
+{
+       return this->_width_with_mirrors;
+}
+
+void
+CarSize::wwm(double w)
+{
+       this->_width_with_mirrors = 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
@@ -457,12 +521,24 @@ CarSize::dr() const
        return this->len() - this->df();
 }
 
+void
+CarSize::ft(double ft)
+{
+       this->_front_track = ft;
+}
+
+double
+CarSize::ft() const
+{
+       return this->_front_track;
+}
+
 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;
+       return sqrt(ctc2 - wb2) - this->ft() / 2.0;
 }
 
 double
@@ -487,13 +563,29 @@ CarSize::orradi() const
        return sqrt(mtrw2 + dr2);
 }
 
+double
+CarSize::imradi() const
+{
+       auto mtrw2 = pow(this->mtr() - this->wwm() / 2.0, 2.0);
+       auto df2 = pow(this->wb(), 2.0);
+       return sqrt(mtrw2 + df2);
+}
+
+double
+CarSize::omradi() const
+{
+       auto mtrw2 = pow(this->mtr() + this->wwm() / 2.0, 2.0);
+       auto df2 = pow(this->wb(), 2.0);
+       return sqrt(mtrw2 + df2);
+}
+
 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 w = this->w(); // FIXME use wwm()?
        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;
@@ -502,25 +594,25 @@ CarSize::perfect_parking_slot_len() const
 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
@@ -740,14 +832,14 @@ BicycleCar::front() const
 }
 
 double
-BicycleCar::ralx() const
+BicycleCar::lrax() const
 {
        double lrx = this->x();
        lrx += (this->w() / 2.0) * cos(this->h() + M_PI / 2.0);
        return lrx;
 }
 double
-BicycleCar::raly() const
+BicycleCar::lray() const
 {
        double lry = this->y();
        lry += (this->w() / 2.0) * sin(this->h() + M_PI / 2.0);
@@ -755,7 +847,7 @@ BicycleCar::raly() const
 }
 
 double
-BicycleCar::rarx() const
+BicycleCar::rrax() const
 {
        double rrx = this->x();
        rrx += (this->w() / 2.0) * cos(this->h() - M_PI / 2.0);
@@ -763,13 +855,127 @@ BicycleCar::rarx() const
 }
 
 double
-BicycleCar::rary() const
+BicycleCar::rray() const
 {
        double rry = this->y();
        rry += (this->w() / 2.0) * sin(this->h() - M_PI / 2.0);
        return rry;
 }
 
+Point
+BicycleCar::lra() const
+{
+       return Point(this->lrax(), this->lray());
+}
+
+Point
+BicycleCar::rra() const
+{
+       return Point(this->rrax(), this->rray());
+}
+
+double
+BicycleCar::lfax() const
+{
+       return this->lrax() + this->wb() * cos(this->h());
+}
+
+double
+BicycleCar::lfay() const
+{
+       return this->lray() + this->wb() * sin(this->h());
+}
+
+double
+BicycleCar::rfax() const
+{
+       return this->rrax() + this->wb() * cos(this->h());
+}
+
+double
+BicycleCar::rfay() const
+{
+       return this->rray() + this->wb() * sin(this->h());
+}
+
+Point
+BicycleCar::lfa() const
+{
+       return Point(this->lfax(), this->lfay());
+}
+
+Point
+BicycleCar::rfa() const
+{
+       return Point(this->rfax(), this->rfay());
+}
+
+double
+BicycleCar::lfmx() const
+{
+       double x = this->x();
+       x += (this->wwm() / 2.0) * cos(this->h() + M_PI / 2.0);
+       x += this->wb() * cos(this->h());
+       return x;
+}
+
+double
+BicycleCar::lfmy() const
+{
+       double y = this->y();
+       y += (this->wwm() / 2.0) * sin(this->h() + M_PI / 2.0);
+       y += this->wb() * sin(this->h());
+       return y;
+}
+
+double
+BicycleCar::rfmx() const
+{
+       double x = this->x();
+       x += (this->wwm() / 2.0) * cos(this->h() - M_PI / 2.0);
+       x += this->wb() * cos(this->h());
+       return x;
+}
+
+double
+BicycleCar::rfmy() const
+{
+       double y = this->y();
+       y += (this->wwm() / 2.0) * sin(this->h() - M_PI / 2.0);
+       y += this->wb() * sin(this->h());
+       return y;
+}
+
+Point
+BicycleCar::lfm() const
+{
+       return Point(this->lfmx(), this->lfmy());
+}
+
+Point
+BicycleCar::rfm() const
+{
+       return Point(this->rfmx(), this->rfmy());
+}
+
+double
+BicycleCar::cfx() const
+{
+       return this->x() + this->df() * cos(this->h());
+}
+
+double
+BicycleCar::cfy() const
+{
+       return this->y() + this->df() * sin(this->h());
+}
+
+Point
+BicycleCar::cf() const
+{
+       return Point(this->cfx(), this->cfy());
+}
+
 Point
 BicycleCar::ccl() const
 {
@@ -796,4 +1002,111 @@ BicycleCar::next()
        this->h(this->h() + this->sp() / this->wb() * tan(this->st()));
 }
 
+void
+BicycleCar::gen_gnuplot_to(std::ostream& out, GenPlotOpts opts)
+{
+       if (opts.ALL) {
+               opts.CAR = true;
+               opts.MIRRORS = true;
+       }
+       if (opts.MIRRORS) {
+               opts.LEFT_MIRROR = true;
+               opts.RIGHT_MIRROR = true;
+       }
+       if (opts.CAR) {
+               opts.FRAME = true;
+               opts.CROSS = true;
+               opts.ARROW = true;
+       }
+       if (opts.FRAME) {
+               opts.LEFT = true;
+               opts.RIGHT = true;
+               opts.REAR = true;
+               opts.FRONT = true;
+       }
+       if (opts.LF_POINT) {
+               this->lf().gen_gnuplot_to(out);
+       }
+       if (opts.LR_POINT) {
+               this->lr().gen_gnuplot_to(out);
+       }
+       if (opts.RR_POINT) {
+               this->rr().gen_gnuplot_to(out);
+       }
+       if (opts.RF_POINT) {
+               this->rf().gen_gnuplot_to(out);
+       }
+       if (opts.LFM_POINT) {
+               this->lfm().gen_gnuplot_to(out);
+       }
+       if (opts.RFM_POINT) {
+               this->rfm().gen_gnuplot_to(out);
+       }
+       if (opts.CRA_POINT || opts.CAR_POINT) {
+               Point::gen_gnuplot_to(out);
+       }
+       if (opts.LRA_POINT) {
+               this->lra().gen_gnuplot_to(out);
+       }
+       if (opts.RRA_POINT) {
+               this->rra().gen_gnuplot_to(out);
+       }
+       if (opts.LEFT) {
+               this->lf().gen_gnuplot_to(out);
+               this->lr().gen_gnuplot_to(out);
+               out << std::endl;
+       }
+       if (opts.RIGHT) {
+               this->rf().gen_gnuplot_to(out);
+               this->rr().gen_gnuplot_to(out);
+               out << std::endl;
+       }
+       if (opts.REAR) {
+               this->lr().gen_gnuplot_to(out);
+               this->rr().gen_gnuplot_to(out);
+               out << std::endl;
+       }
+       if (opts.FRONT) {
+               this->lf().gen_gnuplot_to(out);
+               this->rf().gen_gnuplot_to(out);
+               out << std::endl;
+       }
+       if (opts.ARROW) {
+               this->cf().gen_gnuplot_to(out);
+               this->lfa().gen_gnuplot_to(out);
+               this->rfa().gen_gnuplot_to(out);
+               this->cf().gen_gnuplot_to(out);
+               out << std::endl;
+       }
+       if (opts.CROSS) {
+               double lx = this->x() + 0.2 * cos(this->h() + M_PI/2);
+               double rx = this->x() - 0.2 * cos(this->h() + M_PI/2);
+               double fx = this->x() + 0.2 * cos(this->h());
+               double bx = this->x() - 0.2 * cos(this->h()); // rear is back
+               double ly = this->y() + 0.2 * sin(this->h() + M_PI/2);
+               double ry = this->y() - 0.2 * sin(this->h() + M_PI/2);
+               double fy = this->y() + 0.2 * sin(this->h());
+               double by = this->y() - 0.2 * sin(this->h()); // rear is back
+               out << lx << " " << ly << std::endl;
+               out << rx << " " << ry << std::endl;
+               out << std::endl;
+               out << fx << " " << fy << std::endl;
+               out << bx << " " << by << std::endl;
+               out << std::endl;
+       }
+       if (opts.LEFT_MIRROR) {
+               this->lf().gen_gnuplot_to(out);
+               this->lfm().gen_gnuplot_to(out);
+               this->lr().gen_gnuplot_to(out);
+               out << std::endl;
+
+       }
+       if (opts.RIGHT_MIRROR) {
+               this->rf().gen_gnuplot_to(out);
+               this->rfm().gen_gnuplot_to(out);
+               this->rr().gen_gnuplot_to(out);
+               out << std::endl;
+       }
+}
+
 } // namespace bcar