+/*
+ * SPDX-FileCopyrightText: 2021 Jiri Vlasak <jiri.vlasak.2@cvut.cz>
+ *
+ * SPDX-License-Identifier: GPL-3.0-only
+ */
+
#include <cmath>
-#include "bcar.h"
+#include "bcar.hh"
+
+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::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));
+}
+
+void
+Point::gen_gnuplot_to(std::ostream& out)
+{
+ out << this->_x << " " << this->_y << std::endl;
+}
+
+bool
+Point::operator==(Point const& p)
+{
+ return this->x() == p.x() && this->y() == p.y();
+}
-// car frame
-double BicycleCar::lfx()
+std::ostream&
+operator<<(std::ostream& out, Point const& p)
{
- double lfx = this->x();
- lfx += (this->w() / 2) * cos(this->h() + M_PI / 2);
- lfx += this->df() * cos(this->h());
- lfx += this->sd() * cos(this->h());
- return lfx;
+ out << "[" << p.x() << "," << p.y() << "]";
+ return out;
}
-double BicycleCar::lfy()
+Line::Line(Point const& b, Point const& e): _b(b), _e(e)
{
- double lfy = this->y();
- lfy += (this->w() / 2) * sin(this->h() + M_PI / 2);
- lfy += this->df() * sin(this->h());
- lfy += this->sd() * sin(this->h());
- return lfy;
}
-double BicycleCar::lrx()
+Point
+Line::b() const&
{
- double lrx = this->x();
- lrx += (this->w() / 2) * cos(this->h() + M_PI / 2);
- lrx += -this->dr() * cos(this->h());
- lrx += -this->sd() * cos(this->h());
- return lrx;
+ return this->_b;
}
-double BicycleCar::lry()
+Point
+Line::e() const&
{
- double lry = this->y();
- lry += (this->w() / 2) * sin(this->h() + M_PI / 2);
- lry += -this->dr() * sin(this->h());
- lry += -this->sd() * sin(this->h());
- return lry;
+ return this->_e;
}
-double BicycleCar::rrx()
+Point
+Line::m() const
{
- double rrx = this->x();
- rrx += (this->w() / 2) * cos(this->h() - M_PI / 2);
- rrx += -this->dr() * cos(this->h());
- rrx += -this->sd() * cos(this->h());
- return rrx;
+ return Point((this->_b.x() + this->_e.x()) / 2.0,
+ (this->_b.y() + this->_e.y()) / 2.0);
}
-double BicycleCar::rry()
+Point
+Line::i1() const&
{
- double rry = this->y();
- rry += (this->w() / 2) * sin(this->h() - M_PI / 2);
- rry += -this->dr() * sin(this->h());
- rry += -this->sd() * sin(this->h());
- return rry;
+ return this->_i1;
}
-double BicycleCar::rfx()
+Point
+Line::i2() const&
{
- double rfx = this->x();
- rfx += (this->w() / 2) * cos(this->h() - M_PI / 2);
- rfx += this->df() * cos(this->h());
- rfx += this->sd() * cos(this->h());
- return rfx;
+ return this->_i2;
}
-double BicycleCar::rfy()
+bool
+Line::intersects_with(Line const& li)
{
- double rfy = this->y();
- rfy += (this->w() / 2) * sin(this->h() - M_PI / 2);
- rfy += this->df() * sin(this->h());
- rfy += this->sd() * sin(this->h());
- return rfy;
+ 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;
}
-double BicycleCar::ralx()
+bool
+Line::intersects_with(Point const& c, double const r)
{
- double lrx = this->x();
- lrx += (this->w() / 2) * cos(this->h() + M_PI / 2);
- return lrx;
+ 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 BicycleCar::raly()
+
+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());
+}
+
+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 << "]";
+ 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());
+ 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)
+{
+ 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::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->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->_curb_to_curb;
+}
+
+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
+{
+ return this->_width;
+}
+
+void
+CarSize::w(double 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;
+}
+
+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
{
- double lry = this->y();
- lry += (this->w() / 2) * sin(this->h() + M_PI / 2);
- return lry;
+ return this->len() - this->df();
}
-double BicycleCar::rarx()
+void
+CarSize::ft(double ft)
{
- double rrx = this->x();
- rrx += (this->w() / 2) * cos(this->h() - M_PI / 2);
- return rrx;
+ this->_front_track = ft;
}
-double BicycleCar::rary()
+double
+CarSize::ft() const
{
- double rry = this->y();
- rry += (this->w() / 2) * sin(this->h() - M_PI / 2);
- return rry;
+ return this->_front_track;
}
-BicycleCar *BicycleCar::ccl()
+double
+CarSize::mtr() const
{
- BicycleCar *bc = new BicycleCar();
- 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;
+ auto ctc2 = pow(this->ctc() / 2.0, 2.0);
+ auto wb2 = pow(this->wb(), 2.0);
+ return sqrt(ctc2 - wb2) - this->ft() / 2.0;
}
-BicycleCar *BicycleCar::ccr()
+double
+CarSize::iradi() const
{
- BicycleCar *bc = new BicycleCar();
- 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 this->mtr() - this->w() / 2;
}
-// moving
-void BicycleCar::next()
+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);
}
-BicycleCar::BicycleCar()
+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::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(); // 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;
+}
+
+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 = p.h() - this->h();
+ while (h_d < -M_PI)
+ h_d += 2 * M_PI;
+ while (h_d > +M_PI)
+ h_d -= 2 * M_PI;
+ double a_2 = 0;
+ if (h_d == 0 && (a_1 == 0 || a_2 == M_PI || a_2 == -M_PI)) {
+ return true;
+ } else if (0 < a_1 && a_1 <= M_PI/2) { // left front
+ BicycleCar z(*this); // 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(p.y() - z.y(), p.x() - z.x());
+ while (a_2 < -M_PI)
+ a_2 += 2 * M_PI;
+ while (a_2 > +M_PI)
+ a_2 -= 2 * M_PI;
+ if (z.h() >= a_2 && a_2 >= this->h())
+ return true;
+ } else if (M_PI/2 < a_1 && a_1 <= M_PI) { // left rear
+ BicycleCar z(*this); // 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(p.y() - z.y(), p.x() - z.x());
+ a_2 -= M_PI;
+ while (a_2 < -M_PI)
+ a_2 += 2 * M_PI;
+ while (a_2 > +M_PI)
+ a_2 -= 2 * M_PI;
+ if (this->h() >= a_2 && a_2 >= z.h())
+ return true;
+ } else if (0 > a_1 && a_1 >= -M_PI/2) { // right front
+ BicycleCar z(*this); // 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(p.y() - z.y(), p.x() - z.x());
+ while (a_2 < -M_PI)
+ a_2 += 2 * M_PI;
+ while (a_2 > +M_PI)
+ a_2 -= 2 * M_PI;
+ if (this->h() >= a_2 && a_2 >= z.h())
+ return true;
+ } else if (-M_PI/2 > a_1 && a_1 >= -M_PI) { // right rear
+ BicycleCar z(*this); // 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(p.y() - z.y(), p.x() - z.x());
+ a_2 -= M_PI;
+ while (a_2 < -M_PI)
+ a_2 += 2 * M_PI;
+ while (a_2 > +M_PI)
+ a_2 -= 2 * M_PI;
+ if (z.h() >= a_2 && a_2 >= this->h())
+ return true;
+ } else {
+ // Not happenning, as ``-pi <= a <= pi``.
+ }
+ return false;
+}
+
+void
+BicycleCar::set_max_steer()
+{
+ this->st(atan(this->wb() / this->mtr()));
+}
+
+double
+BicycleCar::lfx() const
+{
+ double lfx = this->x();
+ lfx += (this->w() / 2.0) * cos(this->h() + M_PI / 2.0);
+ lfx += this->df() * cos(this->h());
+ return lfx;
+}
+
+double
+BicycleCar::lfy() const
+{
+ double lfy = this->y();
+ lfy += (this->w() / 2.0) * sin(this->h() + M_PI / 2.0);
+ lfy += this->df() * sin(this->h());
+ return lfy;
+}
+
+double
+BicycleCar::lrx() const
+{
+ double lrx = this->x();
+ lrx += (this->w() / 2.0) * cos(this->h() + M_PI / 2.0);
+ lrx += -this->dr() * cos(this->h());
+ return lrx;
+}
+
+double
+BicycleCar::lry() const
+{
+ double lry = this->y();
+ lry += (this->w() / 2.0) * sin(this->h() + M_PI / 2.0);
+ lry += -this->dr() * sin(this->h());
+ return lry;
+}
+
+double
+BicycleCar::rrx() const
+{
+ double rrx = this->x();
+ rrx += (this->w() / 2.0) * cos(this->h() - M_PI / 2.0);
+ rrx += -this->dr() * cos(this->h());
+ return rrx;
+}
+
+double
+BicycleCar::rry() const
+{
+ double rry = this->y();
+ rry += (this->w() / 2.0) * sin(this->h() - M_PI / 2.0);
+ rry += -this->dr() * sin(this->h());
+ return rry;
+}
+
+double
+BicycleCar::rfx() const
+{
+ double rfx = this->x();
+ rfx += (this->w() / 2.0) * cos(this->h() - M_PI / 2.0);
+ rfx += this->df() * cos(this->h());
+ return rfx;
+}
+
+double
+BicycleCar::rfy() const
+{
+ double rfy = this->y();
+ rfy += (this->w() / 2.0) * sin(this->h() - M_PI / 2.0);
+ rfy += this->df() * sin(this->h());
+ 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::lrax() const
+{
+ double lrx = this->x();
+ lrx += (this->w() / 2.0) * cos(this->h() + M_PI / 2.0);
+ return lrx;
+}
+double
+BicycleCar::lray() const
+{
+ double lry = this->y();
+ lry += (this->w() / 2.0) * sin(this->h() + M_PI / 2.0);
+ return lry;
+}
+
+double
+BicycleCar::rrax() const
+{
+ double rrx = this->x();
+ rrx += (this->w() / 2.0) * cos(this->h() - M_PI / 2.0);
+ return rrx;
+}
+
+double
+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
+{
+ return Point(
+ this->x() + this->mtr() * cos(this->h() + M_PI / 2.0),
+ this->y() + this->mtr() * sin(this->h() + M_PI / 2.0)
+ );
+}
+
+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)
+ );
+}
+
+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()));
+}
+
+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