From 2b8c6d0b02a37b83fbddd450b300c018049b6541 Mon Sep 17 00:00:00 2001
From: heappy <christopher.ruwisch@gmail.com>
Date: Mon, 25 Nov 2024 09:13:10 +0100
Subject: [PATCH] stops creating plots with matplot++ if plotOn = false

---
 .../low/low_t_tail_empennage_design_plot.cpp  | 179 +++++++++++-------
 1 file changed, 110 insertions(+), 69 deletions(-)

diff --git a/empennage_design/src/taw/t_tail/low/low_t_tail_empennage_design_plot.cpp b/empennage_design/src/taw/t_tail/low/low_t_tail_empennage_design_plot.cpp
index 0c224b97..0eb2ac97 100644
--- a/empennage_design/src/taw/t_tail/low/low_t_tail_empennage_design_plot.cpp
+++ b/empennage_design/src/taw/t_tail/low/low_t_tail_empennage_design_plot.cpp
@@ -13,12 +13,12 @@
 #include <aircraftGeometry2/processing/transform.h>
 #include <matplot/matplot.h>
 
+#include <algorithm>
 #include <array>
 #include <cstdlib>
 #include <deque>
 #include <map>
 #include <utility>
-#include <algorithm>
 
 #include "low_t_tail_empennage_design.h"
 
@@ -27,7 +27,8 @@
  *
  * \param surface aerodynamic surface object
  * \param object spar or control device object
- * \return std::tuple<std::vector<double>,std::vector<double>,std::string> coordinates and object name
+ * \return std::tuple<std::vector<double>,std::vector<double>,std::string>
+ * coordinates and object name
  */
 std::tuple<std::vector<double>, std::vector<double>, std::string> gen_polygon(
     const geom2::MultisectionSurface<geom2::AirfoilSection>& surface,
@@ -42,75 +43,96 @@ std::tuple<std::vector<double>, std::vector<double>, std::string> gen_polygon(
  * \param a alpha value between 0 (invisible) - 1 (solid)
  * \return std::array<float,4UL> matplot++ conform color format
  */
-//std::array<float, 4UL> rgba(float r, float b, float g, float a) { return {1 - a, r / 255, b / 255, g / 255}; }
+// std::array<float, 4UL> rgba(float r, float b, float g, float a) { return {1 -
+// a, r / 255, b / 255, g / 255}; }
 //
 ///* */
-//std::array<float, 4UL> spar_color = rgba(99, 110, 114, 1.0);
+// std::array<float, 4UL> spar_color = rgba(99, 110, 114, 1.0);
 //
-//std::map<std::string, std::array<float, 4UL>> control_device_colormap = {{"elevator", rgba(108, 92, 231, 1.0)},
-//                                                                         {"rudder", rgba(232, 67, 147, 1.0)}};
+// std::map<std::string, std::array<float, 4UL>> control_device_colormap =
+// {{"elevator", rgba(108, 92, 231, 1.0)},
+//                                                                          {"rudder",
+//                                                                          rgba(232,
+//                                                                          67,
+//                                                                          147, 1.0)}};
 //
-//std::tuple<std::vector<double>, std::vector<double>, std::string> gen_polygon(
-//    const geom2::MultisectionSurface<geom2::AirfoilSection>& surface,
-//    const geom2::MultisectionSurface<geom2::PolygonSection>& object) {
-//  double half_span = surface.is_symmetric ? 0.5 * geom2::measure::span(surface) : geom2::measure::span(surface);
-//  /* Spar coordinates positive values normally*/
-//  double from = object.sections.at(0).origin.z() * half_span;
-//  double to = object.sections.at(1).origin.z() * half_span;
+// std::tuple<std::vector<double>, std::vector<double>, std::string>
+// gen_polygon(
+//     const geom2::MultisectionSurface<geom2::AirfoilSection>& surface,
+//     const geom2::MultisectionSurface<geom2::PolygonSection>& object) {
+//   double half_span = surface.is_symmetric ? 0.5 *
+//   geom2::measure::span(surface) : geom2::measure::span(surface);
+//   /* Spar coordinates positive values normally*/
+//   double from = object.sections.at(0).origin.z() * half_span;
+//   double to = object.sections.at(1).origin.z() * half_span;
 //
-//  /* Chordwise coordinates */
-//  double chord_from_fwd = object.sections.at(0).get_contour().vertex(0).x();
-//  double chord_from_aft = object.sections.at(0).get_contour().vertex(1).x();
-//  double chord_to_fwd = object.sections.at(1).get_contour().vertex(0).x();
-//  double chord_to_aft = object.sections.at(1).get_contour().vertex(1).x();
+//   /* Chordwise coordinates */
+//   double chord_from_fwd = object.sections.at(0).get_contour().vertex(0).x();
+//   double chord_from_aft = object.sections.at(0).get_contour().vertex(1).x();
+//   double chord_to_fwd = object.sections.at(1).get_contour().vertex(0).x();
+//   double chord_to_aft = object.sections.at(1).get_contour().vertex(1).x();
 //
-//  auto f_chord_fwd = [chord_from_fwd, chord_to_fwd, half_span](double span_position) {
-//    return std::lerp(chord_from_fwd, chord_to_fwd, fabs(span_position) / half_span);
-//  };
-//  auto f_chord_aft = [chord_from_aft, chord_to_aft, half_span](double span_position) {
-//    return std::lerp(chord_from_aft, chord_to_aft, fabs(span_position) / half_span);
-//  };
-//  /* Add object from */
-//  std::vector<double> spanwise_coordinates{-fabs(from)};
+//   auto f_chord_fwd = [chord_from_fwd, chord_to_fwd, half_span](double
+//   span_position) {
+//     return std::lerp(chord_from_fwd, chord_to_fwd, fabs(span_position) /
+//     half_span);
+//   };
+//   auto f_chord_aft = [chord_from_aft, chord_to_aft, half_span](double
+//   span_position) {
+//     return std::lerp(chord_from_aft, chord_to_aft, fabs(span_position) /
+//     half_span);
+//   };
+//   /* Add object from */
+//   std::vector<double> spanwise_coordinates{-fabs(from)};
 //
-//  /* Add intermediate sections */
-//  for (auto& section : surface.sections) {
-//    if (fabs(section.origin.z()) > fabs(from) && fabs(section.origin.z()) < fabs(to)) {
-//      spanwise_coordinates.push_back(section.origin.z());
-//    }
-//  }
-//  /* Add object to */
-//  spanwise_coordinates.push_back(-fabs(to));
+//   /* Add intermediate sections */
+//   for (auto& section : surface.sections) {
+//     if (fabs(section.origin.z()) > fabs(from) && fabs(section.origin.z()) <
+//     fabs(to)) {
+//       spanwise_coordinates.push_back(section.origin.z());
+//     }
+//   }
+//   /* Add object to */
+//   spanwise_coordinates.push_back(-fabs(to));
 //
-//  /**/
-//  std::vector<double> x;
-//  std::vector<double> y;
-//  /* From in to out*/
-//  for (auto it = spanwise_coordinates.begin(); it != spanwise_coordinates.end(); ++it) {
-//    y.push_back(fabs(*it));
-//    x.push_back(geom2::measure::offset_LE(surface, *it).x() + f_chord_fwd(*it) * geom2::measure::chord(surface, *it));
-//  }
-//  /* From out to in*/
-//  for (auto rit = spanwise_coordinates.rbegin(); rit != spanwise_coordinates.rend(); ++rit) {
-//    y.push_back(fabs(*rit));
-//    x.push_back(geom2::measure::offset_LE(surface, *rit).x() +
-//                f_chord_aft(*rit) * geom2::measure::chord(surface, *rit));
-//  }
+//   /**/
+//   std::vector<double> x;
+//   std::vector<double> y;
+//   /* From in to out*/
+//   for (auto it = spanwise_coordinates.begin(); it !=
+//   spanwise_coordinates.end(); ++it) {
+//     y.push_back(fabs(*it));
+//     x.push_back(geom2::measure::offset_LE(surface, *it).x() +
+//     f_chord_fwd(*it) * geom2::measure::chord(surface, *it));
+//   }
+//   /* From out to in*/
+//   for (auto rit = spanwise_coordinates.rbegin(); rit !=
+//   spanwise_coordinates.rend(); ++rit) {
+//     y.push_back(fabs(*rit));
+//     x.push_back(geom2::measure::offset_LE(surface, *rit).x() +
+//                 f_chord_aft(*rit) * geom2::measure::chord(surface, *rit));
+//   }
 //
-//  /* Close loop */
-//  y.push_back(y.front());
-//  x.push_back(x.front());
+//   /* Close loop */
+//   y.push_back(y.front());
+//   x.push_back(x.front());
 //
-//  return {x, y, object.name};
-//}
+//   return {x, y, object.name};
+// }
 namespace taw {
 namespace low {
 
 fs::path T_Tail::plot_stabilizer_planform(
     const geom2::MultisectionSurface<geom2::AirfoilSection>& surface,
     const std::vector<geom2::MultisectionSurface<geom2::PolygonSection>>& spars,
-    const std::vector<geom2::MultisectionSurface<geom2::PolygonSection>>& devices) {
-  auto coordinates_xy = geom2::transform::outline_2d(surface, geom2::Direction_3(0, 0, 1));
+    const std::vector<geom2::MultisectionSurface<geom2::PolygonSection>>&
+        devices) {
+  /* Plot active */
+  if (!rtIO->plotOn) {
+    return "";
+  }
+  auto coordinates_xy =
+      geom2::transform::outline_2d(surface, geom2::Direction_3(0, 0, 1));
   std::deque<double> spanwise;
   std::deque<double> chordwise;
   auto span = geom2::measure::span(surface);
@@ -130,9 +152,10 @@ fs::path T_Tail::plot_stabilizer_planform(
   }
 
   // /* Get mac coordinates */
-  // auto mac_position_y = geom2::measure::mean_aerodynamic_chord_position(data->wing);
-  // auto mac_position = geom2::measure::offset_LE(data->wing,mac_position_y);
-  // auto mac = geom2::measure::chord(data->wing,mac_position_y);
+  // auto mac_position_y =
+  // geom2::measure::mean_aerodynamic_chord_position(data->wing); auto
+  // mac_position = geom2::measure::offset_LE(data->wing,mac_position_y); auto
+  // mac = geom2::measure::chord(data->wing,mac_position_y);
 
   /* Generate plot */
   std::string name = surface.name;
@@ -199,7 +222,8 @@ fs::path T_Tail::plot_stabilizer_planform(
   ax->legend()->box(true);
   ax->legend()->font_size(8);
 
-  fs::path plot_path = rtIO->getPlotDir() + "/" + surface.name + "_planfrom.svg";
+  fs::path plot_path =
+      rtIO->getPlotDir() + "/" + surface.name + "_planfrom.svg";
   matplot::save(f, plot_path.string(), "svg");
   std::this_thread::sleep_for(std::chrono::milliseconds(500));
   system("pkill gnuplot");
@@ -208,27 +232,36 @@ fs::path T_Tail::plot_stabilizer_planform(
 
 fs::path T_Tail::plot_thickness_and_twist_distribution(
     const geom2::MultisectionSurface<geom2::AirfoilSection>& surface) {
+  /* Plot active */
+  if (!rtIO->plotOn) {
+    return "";
+  }
   std::vector<double> eta;
   std::vector<double> thickness_to_chord_ratio;
   std::vector<double> twist;
-  double half_span = surface.is_symmetric ? geom2::measure::span(surface) * 0.5 : geom2::measure::span(surface);
+  double half_span = surface.is_symmetric ? geom2::measure::span(surface) * 0.5
+                                          : geom2::measure::span(surface);
   for (auto section : surface.sections) {
     eta.push_back(-section.origin.z() / half_span);
-    thickness_to_chord_ratio.push_back(geom2::measure::thickness_max(section) / section.get_chord_length());
+    thickness_to_chord_ratio.push_back(geom2::measure::thickness_max(section) /
+                                       section.get_chord_length());
     twist.push_back(section.get_twist_angle());
   }
-  const auto [t_to_c_min,t_to_c_max] = std::ranges::minmax_element(thickness_to_chord_ratio);
-  const auto [twist_min,twist_max] = std::ranges::minmax_element(twist);
+  const auto [t_to_c_min, t_to_c_max] =
+      std::ranges::minmax_element(thickness_to_chord_ratio);
+  const auto [twist_min, twist_max] = std::ranges::minmax_element(twist);
 
   auto f = matplot::figure(true);
 
   auto ax1 = f->add_subplot(2, 1, 0);
-  ax1->plot(eta, thickness_to_chord_ratio, "g-")->color("black").line_width(1.5);
+  ax1->plot(eta, thickness_to_chord_ratio, "g-")
+      ->color("black")
+      .line_width(1.5);
   // ax1->xlabel("\u03B7");
   ax1->x_axis().visible(true);
   ax1->grid(true);
   ax1->ylabel("t / c [1]");
-  ax1->ylim({*t_to_c_min - 0.01,*t_to_c_max + 0.01});
+  ax1->ylim({*t_to_c_min - 0.01, *t_to_c_max + 0.01});
 
   auto ax2 = f->add_subplot(2, 1, 1);
   ax2->plot(eta, twist, "b-")->color("black").line_style("--").line_width(1.5);
@@ -237,7 +270,8 @@ fs::path T_Tail::plot_thickness_and_twist_distribution(
   ax2->grid(true);
   ax2->ylim({*twist_min - 0.01, *twist_max + 0.01});
 
-  fs::path plot_path = rtIO->getPlotDir() + "/" + surface.name + "_thickness_and_twist_distribution.svg";
+  fs::path plot_path = rtIO->getPlotDir() + "/" + surface.name +
+                       "_thickness_and_twist_distribution.svg";
   matplot::save(f, plot_path.string(), "svg");
   std::this_thread::sleep_for(std::chrono::milliseconds(500));
   system("pkill gnuplot");
@@ -249,7 +283,12 @@ fs::path T_Tail::plot_thickness_and_twist_distribution(
  *
  * \return fs::path
  */
-std::vector<fs::path> T_Tail::plot_airfoils(const geom2::MultisectionSurface<geom2::AirfoilSection>& surface) {
+std::vector<fs::path> T_Tail::plot_airfoils(
+    const geom2::MultisectionSurface<geom2::AirfoilSection>& surface) {
+  /* Plot active */
+  if (!rtIO->plotOn) {
+    return {""};
+  }
   std::map<std::string, std::vector<geom2::Point_2>> airfoils_data{};
   std::vector<fs::path> airfoil_plots_path;
 
@@ -284,9 +323,11 @@ std::vector<fs::path> T_Tail::plot_airfoils(const geom2::MultisectionSurface<geo
       ax->xlim({0, 1});
       ax->axis(matplot::equal);
 
-      fs::path plot_path = rtIO->getPlotDir() + "/" + surface.name + "_" + airfoil_data.first + "_airfoil.svg";
+      fs::path plot_path = rtIO->getPlotDir() + "/" + surface.name + "_" +
+                           airfoil_data.first + "_airfoil.svg";
       matplot::save(f, plot_path.string(), "svg");
-      airfoil_plots_path.push_back(fs::relative(plot_path, fs::path(rtIO->getHtmlDir())));
+      airfoil_plots_path.push_back(
+          fs::relative(plot_path, fs::path(rtIO->getHtmlDir())));
       std::this_thread::sleep_for(std::chrono::milliseconds(500));
       system("pkill gnuplot");
     }
-- 
GitLab