GroundGrid.cpp
Go to the documentation of this file.
1 /*+-------------------------------------------------------------------------+
2  | MultiVehicle simulator (libmvsim) |
3  | |
4  | Copyright (C) 2014-2023 Jose Luis Blanco Claraco |
5  | Copyright (C) 2017 Borys Tymchenko (Odessa Polytechnic University) |
6  | Distributed under 3-clause BSD License |
7  | See COPYING |
8  +-------------------------------------------------------------------------+ */
9 
10 #include <mrpt/opengl/COpenGLScene.h>
11 #include <mvsim/World.h>
13 
14 #include <rapidxml.hpp>
15 
16 #include "xml_utils.h"
17 
18 using namespace rapidxml;
19 using namespace mvsim;
20 using namespace std;
21 
22 GroundGrid::GroundGrid(World* parent, const rapidxml::xml_node<char>* root)
23  : WorldElementBase(parent),
24  is_floating_(true),
25  x_min_(-25.0),
26  x_max_(25.0),
27  y_min_(-25.0),
28  y_max_(25.0),
29  interval_(5.0),
30  color_(0xe0, 0xe0, 0xe0, 0xff),
31  line_width_(1.0)
32 {
33  // Create opengl object: in this class, we'll store most state data directly
34  // in the mrpt::opengl object.
35  loadConfigFrom(root);
36 }
37 
38 GroundGrid::~GroundGrid() = default;
39 
41 {
42  if (!root) return; // Assume defaults
43 
44  TParameterDefinitions params;
45  params["floating"] = TParamEntry("%bool", &is_floating_);
46  params["floating_focus"] =
48  params["color"] = TParamEntry("%color", &color_);
49  params["line_width"] = TParamEntry("%lf", &line_width_);
50 
51  params["x_min"] = TParamEntry("%lf", &x_min_);
52  params["x_max"] = TParamEntry("%lf", &x_max_);
53  params["y_min"] = TParamEntry("%lf", &y_min_);
54  params["y_max"] = TParamEntry("%lf", &y_max_);
55  params["interval"] = TParamEntry("%lf", &interval_);
56 
58  *root, params, world_->user_defined_variables());
59 
60  // If a vehicle name is given, setting "is_floating=true" by the user is
61  // optional:
62  if (!float_center_at_vehicle_name_.empty()) is_floating_ = true;
63 }
64 
66  const mrpt::optional_ref<mrpt::opengl::COpenGLScene>& viz,
67  [[maybe_unused]] const mrpt::optional_ref<mrpt::opengl::COpenGLScene>&
68  physical,
69  [[maybe_unused]] bool childrenOnly)
70 {
71  using namespace mrpt::math;
72 
73  // 1st call OR gridmap changed?
74  if (!gl_groundgrid_ && viz)
75  {
76  gl_groundgrid_ = mrpt::opengl::CGridPlaneXY::Create();
77  gl_groundgrid_->setPlaneLimits(x_min_, x_max_, y_min_, y_max_);
78  gl_groundgrid_->setGridFrequency(interval_);
79  gl_groundgrid_->setColor_u8(color_);
80  gl_groundgrid_->setLineWidth(line_width_);
81 
82  viz->get().insert(gl_groundgrid_);
83  }
84 
85  // Update:
86  mrpt::math::TPoint3D center_offset(.0, .0, .0);
87  if (is_floating_)
88  {
89  // Centered at a vehicle:
91  // Look for the vehicle by its name:
92  World::VehicleList::const_iterator it_veh =
94  // not found -> error:
95  if (!float_center_at_vehicle_name_.empty() && it_veh == vehs.end())
96  throw std::runtime_error(mrpt::format(
97  "[GroundGrid] *ERROR* Cannot find vehicle named '%s' to "
98  "focus on.",
100 
101  // If the user didn't specify any name, assume it's the first vehicle,
102  // or ignore it if none exists:
103  if (it_veh == vehs.end()) it_veh = vehs.begin();
104 
105  if (it_veh != vehs.end())
106  {
107  const mrpt::math::TPose3D& pose = it_veh->second->getPose();
108  center_offset.x = pose.x;
109  center_offset.y = pose.y;
110  }
111  }
112 
113  // "Discretize" offset for a better visual impact:
114  ASSERT_(interval_ > .0);
115  center_offset.x = interval_ *
116  ::floor(std::abs(center_offset.x) / interval_) *
117  (center_offset.x < 0 ? -1. : 1.);
118  center_offset.y = interval_ *
119  ::floor(std::abs(center_offset.y) / interval_) *
120  (center_offset.y < 0 ? -1. : 1.);
121  gl_groundgrid_->setLocation(center_offset);
122 }
This file contains rapidxml parser and DOM implementation.
virtual ~GroundGrid()
std::map< std::string, TParamEntry > TParameterDefinitions
void parse_xmlnode_children_as_param(const rapidxml::xml_node< char > &xml_node, const TParameterDefinitions &params, const std::map< std::string, std::string > &variableNamesValues={}, const char *functionNameContext="", mrpt::system::COutputLogger *logger=nullptr)
Definition: xml_utils.cpp:224
std::string float_center_at_vehicle_name_
Definition: GroundGrid.h:34
virtual void internalGuiUpdate(const mrpt::optional_ref< mrpt::opengl::COpenGLScene > &viz, const mrpt::optional_ref< mrpt::opengl::COpenGLScene > &physical, bool childrenOnly) override
Definition: GroundGrid.cpp:65
std::multimap< std::string, VehicleBase::Ptr > VehicleList
Definition: World.h:281
const VehicleList & getListOfVehicles() const
Definition: World.h:303
virtual void loadConfigFrom(const rapidxml::xml_node< char > *root) override
Definition: GroundGrid.cpp:40
const std::map< std::string, std::string > & user_defined_variables() const
Definition: World.h:391
mrpt::img::TColor color_
Definition: GroundGrid.h:36
mrpt::opengl::CGridPlaneXY::Ptr gl_groundgrid_
Definition: GroundGrid.h:39
double line_width_
Definition: GroundGrid.h:37


mvsim
Author(s):
autogenerated on Tue Jul 4 2023 03:08:20