GroundGrid.cpp
Go to the documentation of this file.
1 /*+-------------------------------------------------------------------------+
2  | MultiVehicle simulator (libmvsim) |
3  | |
4  | Copyright (C) 2014-2024 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.
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"] = TParamEntry("%s", &float_center_at_vehicle_name_);
47  params["color"] = TParamEntry("%color", &color_);
48  params["line_width"] = TParamEntry("%lf", &line_width_);
49 
50  params["x_min"] = TParamEntry("%lf", &x_min_);
51  params["x_max"] = TParamEntry("%lf", &x_max_);
52  params["y_min"] = TParamEntry("%lf", &y_min_);
53  params["y_max"] = TParamEntry("%lf", &y_max_);
54  params["interval"] = TParamEntry("%lf", &interval_);
55 
57 
58  // If a vehicle name is given, setting "is_floating=true" by the user is
59  // optional:
60  if (!float_center_at_vehicle_name_.empty()) is_floating_ = true;
61 }
62 
64  const mrpt::optional_ref<mrpt::opengl::COpenGLScene>& viz,
65  [[maybe_unused]] const mrpt::optional_ref<mrpt::opengl::COpenGLScene>& physical,
66  [[maybe_unused]] bool childrenOnly)
67 {
68  using namespace mrpt::math;
69 
70  // 1st call OR gridmap changed?
71  if (!gl_groundgrid_ && viz)
72  {
73  gl_groundgrid_ = mrpt::opengl::CGridPlaneXY::Create();
74  gl_groundgrid_->setPlaneLimits(x_min_, x_max_, y_min_, y_max_);
75  gl_groundgrid_->setGridFrequency(interval_);
76  gl_groundgrid_->setColor_u8(color_);
77  gl_groundgrid_->setLineWidth(line_width_);
78 
79  viz->get().insert(gl_groundgrid_);
80  }
81 
82  // Update:
83  mrpt::math::TPoint3D center_offset(.0, .0, .0);
84  if (is_floating_)
85  {
86  // Centered at a vehicle:
88  // Look for the vehicle by its name:
89  World::VehicleList::const_iterator it_veh = vehs.find(float_center_at_vehicle_name_);
90  // not found -> error:
91  if (!float_center_at_vehicle_name_.empty() && it_veh == vehs.end())
92  throw std::runtime_error(mrpt::format(
93  "[GroundGrid] *ERROR* Cannot find vehicle named '%s' to "
94  "focus on.",
96 
97  // If the user didn't specify any name, assume it's the first vehicle,
98  // or ignore it if none exists:
99  if (it_veh == vehs.end()) it_veh = vehs.begin();
100 
101  if (it_veh != vehs.end())
102  {
103  const mrpt::math::TPose3D& pose = it_veh->second->getPose();
104  center_offset.x = pose.x;
105  center_offset.y = pose.y;
106  }
107  }
108 
109  // "Discretize" offset for a better visual impact:
110  ASSERT_(interval_ > .0);
111  center_offset.x = interval_ * ::floor(std::abs(center_offset.x) / interval_) *
112  (center_offset.x < 0 ? -1. : 1.);
113  center_offset.y = interval_ * ::floor(std::abs(center_offset.y) / interval_) *
114  (center_offset.y < 0 ? -1. : 1.);
115  gl_groundgrid_->setLocation(center_offset);
116 }
mvsim
Definition: Client.h:21
mvsim::VisualObject::world_
World * world_
Definition: VisualObject.h:73
mvsim::World::VehicleList
std::multimap< std::string, VehicleBase::Ptr > VehicleList
Definition: World.h:291
mvsim::GroundGrid::~GroundGrid
virtual ~GroundGrid()
mvsim::GroundGrid::line_width_
double line_width_
Definition: GroundGrid.h:39
mvsim::TParamEntry
Definition: TParameterDefinitions.h:38
mvsim::parse_xmlnode_children_as_param
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:215
World.h
mvsim::GroundGrid::gl_groundgrid_
mrpt::opengl::CGridPlaneXY::Ptr gl_groundgrid_
Definition: GroundGrid.h:41
mvsim::GroundGrid::is_floating_
bool is_floating_
Definition: GroundGrid.h:35
xml_utils.h
mvsim::GroundGrid::float_center_at_vehicle_name_
std::string float_center_at_vehicle_name_
Definition: GroundGrid.h:36
mvsim::World::getListOfVehicles
const VehicleList & getListOfVehicles() const
Definition: World.h:310
mvsim::GroundGrid::interval_
double interval_
Definition: GroundGrid.h:37
rapidxml
Definition: rapidxml.hpp:57
mvsim::GroundGrid::y_max_
double y_max_
Definition: GroundGrid.h:37
mvsim::TParameterDefinitions
std::map< std::string, TParamEntry > TParameterDefinitions
Definition: TParameterDefinitions.h:64
mvsim::GroundGrid::x_max_
double x_max_
Definition: GroundGrid.h:37
GroundGrid.h
mvsim::GroundGrid::x_min_
double x_min_
Definition: GroundGrid.h:37
mvsim::World
Definition: World.h:82
rapidxml::xml_node< char >
mvsim::GroundGrid::color_
mrpt::img::TColor color_
Definition: GroundGrid.h:38
mvsim::World::user_defined_variables
const std::map< std::string, std::string > & user_defined_variables() const
Definition: World.h:390
std
mrpt::math
Definition: xml_utils.h:22
mvsim::GroundGrid::internalGuiUpdate
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:63
root
root
mvsim::GroundGrid::y_min_
double y_min_
Definition: GroundGrid.h:37
rapidxml.hpp
mvsim::WorldElementBase
Definition: WorldElementBase.h:27
mvsim::GroundGrid::loadConfigFrom
virtual void loadConfigFrom(const rapidxml::xml_node< char > *root) override
Definition: GroundGrid.cpp:40


mvsim
Author(s):
autogenerated on Wed May 28 2025 02:13:07