From 538f391354dee57ee6e0de6344692333536a765a Mon Sep 17 00:00:00 2001 From: Jiri Vlasak Date: Wed, 19 Jan 2022 18:53:26 +0100 Subject: [PATCH 1/1] Add generate for gnuplot method and example code --- CMakeLists.txt | 3 ++ incl/bcar.hh | 23 ++++++++++ src/bcar.cc | 99 ++++++++++++++++++++++++++++++++++++++++++ src/gen_for_gnuplot.cc | 30 +++++++++++++ 4 files changed, 155 insertions(+) create mode 100644 src/gen_for_gnuplot.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index dc978e6..634f088 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -35,6 +35,9 @@ target_link_libraries(compare_to_zips pslot) add_executable(compare_to_li src/compare_to_li.cc) target_link_libraries(compare_to_li pslot) +add_executable(gen_for_gnuplot src/gen_for_gnuplot.cc) +target_link_libraries(gen_for_gnuplot pslot) + if (SKIP_UT) return() endif() diff --git a/incl/bcar.hh b/incl/bcar.hh index cf2020f..71e4f50 100644 --- a/incl/bcar.hh +++ b/incl/bcar.hh @@ -86,6 +86,9 @@ public: /*! Return Euclidean distance to `p`. */ double edist(Point const& p) const; + /*! Generate output for plotting with gnuplot. */ + void gen_gnuplot_to(std::ostream& out); + bool operator==(Point const& p); friend std::ostream& operator<<(std::ostream& out, Point const& p); }; @@ -459,6 +462,26 @@ public: /*! Next car position based on speed `sp` and steer `st`. */ void next(); + + /*! Options for generating output for gnuplot. */ + class GenPlotOpts { + public: + static bool LEFT; + static bool RIGHT; + static bool REAR; + static bool FRONT; + static bool FRAME; + static bool ARROW; + static bool CROSS; + static bool CAR; + static bool LEFT_MIRROR; + static bool RIGHT_MIRROR; + static bool MIRRORS; + static bool ALL; + }; + + /*! Generate output for plotting with gnuplot. */ + void gen_gnuplot_to(std::ostream& out, GenPlotOpts const& opts); }; } // namespace bcar diff --git a/src/bcar.cc b/src/bcar.cc index 1e205a7..0b9062a 100644 --- a/src/bcar.cc +++ b/src/bcar.cc @@ -137,6 +137,12 @@ 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) { @@ -972,4 +978,97 @@ BicycleCar::next() this->h(this->h() + this->sp() / this->wb() * tan(this->st())); } +bool BicycleCar::GenPlotOpts::LEFT = false; +bool BicycleCar::GenPlotOpts::RIGHT = false; +bool BicycleCar::GenPlotOpts::REAR = false; +bool BicycleCar::GenPlotOpts::FRONT = false; +bool BicycleCar::GenPlotOpts::FRAME = false; // LEFT, RIGHT, REAR, FRONT +bool BicycleCar::GenPlotOpts::ARROW = false; +bool BicycleCar::GenPlotOpts::CROSS = false; +bool BicycleCar::GenPlotOpts::CAR = false; // CROSS, ARROW, FRAME +bool BicycleCar::GenPlotOpts::LEFT_MIRROR = false; +bool BicycleCar::GenPlotOpts::RIGHT_MIRROR = false; +bool BicycleCar::GenPlotOpts::MIRRORS = false; // RIGHT_MIRROR, LEFT_MIRROR +bool BicycleCar::GenPlotOpts::ALL = true; // MIRRORS, CAR + +void +BicycleCar::gen_gnuplot_to(std::ostream& out, GenPlotOpts const& 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.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 diff --git a/src/gen_for_gnuplot.cc b/src/gen_for_gnuplot.cc new file mode 100644 index 0000000..8175217 --- /dev/null +++ b/src/gen_for_gnuplot.cc @@ -0,0 +1,30 @@ +/* + * SPDX-FileCopyrightText: 2021 Jiri Vlasak + * + * SPDX-License-Identifier: GPL-3.0-only + */ + +#include +#include +#include "pslot.hh" + +int main() +{ + using namespace std; + cout << fixed; + cerr << fixed; + + bcar::BicycleCar c; + bcar::BicycleCar::GenPlotOpts o; + o.ALL = false; + o.FRONT = true; + o.REAR = true; + o.MIRRORS = true; + c.sp(0.5); + c.set_max_steer(); + for (unsigned int i = 0; i < 10; i++) { + c.gen_gnuplot_to(cout, o); + c.next(); + } + return 0; +} -- 2.39.2