]> rtime.felk.cvut.cz Git - hubacji1/bcar.git/commitdiff
Add generate for gnuplot method and example code
authorJiri Vlasak <hubacji1@fel.cvut.cz>
Wed, 19 Jan 2022 17:53:26 +0000 (18:53 +0100)
committerJiri Vlasak <hubacji1@fel.cvut.cz>
Wed, 19 Jan 2022 17:53:27 +0000 (18:53 +0100)
CMakeLists.txt
incl/bcar.hh
src/bcar.cc
src/gen_for_gnuplot.cc [new file with mode: 0644]

index dc978e617f3d2e7eff999406f6aa8868c2b9d527..634f088ab5a518175510c207a66f2aa7f67c8d4a 100644 (file)
@@ -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()
index cf2020fbce5c1d5123a42e6245b2ca14b8024543..71e4f505c9a698352d330dc5e113740a4563b185 100644 (file)
@@ -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
index 1e205a7f1a1d51ffafa683c5c1a03343058fc49e..0b9062a0b793c6b0f9ac0874bfc069d3133efd1e 100644 (file)
@@ -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 (file)
index 0000000..8175217
--- /dev/null
@@ -0,0 +1,30 @@
+/*
+ * SPDX-FileCopyrightText: 2021 Jiri Vlasak <jiri.vlasak.2@cvut.cz>
+ *
+ * SPDX-License-Identifier: GPL-3.0-only
+ */
+
+#include <iostream>
+#include <vector>
+#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;
+}