From 21717d4b37c59c34990193a7acf1f50d7daeff6d Mon Sep 17 00:00:00 2001 From: Jiri Vlasak Date: Wed, 19 Jan 2022 22:27:13 +0100 Subject: [PATCH] Improve generate for gnuplot example code --- src/gen_for_gnuplot.cc | 101 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 89 insertions(+), 12 deletions(-) diff --git a/src/gen_for_gnuplot.cc b/src/gen_for_gnuplot.cc index 8175217..d719e06 100644 --- a/src/gen_for_gnuplot.cc +++ b/src/gen_for_gnuplot.cc @@ -4,27 +4,104 @@ * SPDX-License-Identifier: GPL-3.0-only */ +#include #include #include #include "pslot.hh" -int main() +#define SP 0.5 +#define STEPS 10 + +#define V v +#define INIT_V vector V; +#define P(W) V.push_back(M(#W)); V.back().o.W = true; + +class M { +public: + std::string fn; + std::ofstream f; + bcar::BicycleCar c; + bcar::BicycleCar::GenPlotOpts o; + + void gpl() + { + this->c.gen_gnuplot_to(this->f, this->o); + this->c.next(); + } + + ~M() + { + this->f.close(); + } + M(std::string fn) : fn(fn) + { + this->f.open(this->fn); + this->c.sp(SP); + this->c.set_max_steer(); + this->o.ALL = false; + } + M(M const& m) : fn(m.fn), c(m.c), o(m.o) + { + this->f.open(this->fn); + } + M& operator=(M m) + { + swap(*this, m); + return *this; + } + friend void swap(M& f, M& t) + { + using std::swap; + f.f.close(); + t.f.close(); + swap(f.fn, t.fn); + swap(f.c, t.c); + swap(f.o, t.o); + f.f.open(f.fn); + t.f.open(t.fn); + } + M(M&& m) noexcept + { + swap(*this, m); + } +}; + +void +gen_pl_script(std::vector& ms) +{ + using namespace std; + ofstream f; + f.open("plot.pl"); + f << "#!/usr/bin/gnuplot" << endl; + f << "set size ratio -1" << endl; + f << "plot"; + for (auto& m: ms) { + f << " '" << m.fn << "'" << " w l,"; + } + f << endl; + f << "pause -1" << endl; + f.close(); +} + +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(); + INIT_V + P(LF_POINT) + P(LFM_POINT) + P(RF_POINT) + P(RFM_POINT) + P(CRA_POINT) + + for (unsigned int i = 0; i < STEPS; i++) { + for (auto& m: V) { + m.gpl(); + } } + gen_pl_script(V); return 0; } -- 2.39.2