World_gui.cpp
Go to the documentation of this file.
1 /*+-------------------------------------------------------------------------+
2  | MultiVehicle simulator (libmvsim) |
3  | |
4  | Copyright (C) 2014 Jose Luis Blanco Claraco (University of Almeria) |
5  | Copyright (C) 2017 Borys Tymchenko (Odessa Polytechnic University) |
6  | Distributed under GNU General Public License version 3 |
7  | See <http://www.gnu.org/licenses/> |
8  +-------------------------------------------------------------------------+ */
9 #include <mvsim/World.h>
10 
11 
12 #include <mrpt/version.h>
13 #if MRPT_VERSION<0x199
14 #include <mrpt/utils/utils_defs.h> // mrpt::format()
15 #else
16 #include <mrpt/core/format.h>
17 #endif
18 
19 #include <mrpt/opengl/COpenGLScene.h>
20 #include <mrpt/opengl/CGridPlaneXY.h>
21 
22 #include <rapidxml.hpp>
23 #include "xml_utils.h"
24 
25 using namespace mvsim;
26 using namespace std;
27 
28 // Default ctor: inits empty world.
30  : win_w(800),
31  win_h(600),
32  ortho(false),
33  show_forces(false),
34  force_scale(0.01),
35  camera_distance(80),
36  fov_deg(60)
37 {
38 }
39 
41 {
42  std::map<std::string, TParamEntry> gui_params;
43  gui_params["win_w"] = TParamEntry("%u", &win_w);
44  gui_params["win_h"] = TParamEntry("%u", &win_h);
45  gui_params["ortho"] = TParamEntry("%bool", &ortho);
46  gui_params["show_forces"] = TParamEntry("%bool", &show_forces);
47  gui_params["force_scale"] = TParamEntry("%lf", &force_scale);
48  gui_params["cam_distance"] = TParamEntry("%lf", &camera_distance);
49  gui_params["fov_deg"] = TParamEntry("%lf", &fov_deg);
50  gui_params["follow_vehicle"] = TParamEntry("%s", &follow_vehicle);
51 
52  parse_xmlnode_children_as_param(node, gui_params, "[World::TGUI_Options]");
53 }
54 
56 // Text labels unique IDs:
57 size_t ID_GLTEXT_CLOCK = 0;
58 
61 bool World::is_GUI_open() const { return !!m_gui_win; }
63 void World::close_GUI() { m_gui_win.reset(); }
69 {
70  // First call?
71  // -----------------------
72  if (!m_gui_win)
73  {
74  m_timlogger.enter("update_GUI_init");
75 
76  m_gui_win = mrpt::gui::CDisplayWindow3D::Create(
78  m_gui_win->setCameraZoom(m_gui_options.camera_distance);
79  m_gui_win->setCameraProjective(!m_gui_options.ortho);
81 
82  // Only if the world is empty: at least introduce a ground grid:
83  if (m_world_elements.empty())
84  {
85  WorldElementBase* we =
86  WorldElementBase::factory(this, NULL, "groundgrid");
87  this->m_world_elements.push_back(we);
88  }
89 
90  m_timlogger.leave("update_GUI_init");
91  }
92 
93  m_timlogger.enter("update_GUI"); // Don't count initialization, since that
94  // is a total outlier and lacks interest!
95 
96  m_timlogger.enter("update_GUI.1.get-lock");
97  mrpt::opengl::COpenGLScene::Ptr gl_scene =
98  m_gui_win->get3DSceneAndLock(); // ** LOCK **
99  m_timlogger.leave("update_GUI.1.get-lock");
100 
101  // 1st time only:
102  // Build different "stacks" or "z-order levels" for
103  // rendering with transparencies to work nicely
104  // --------------------------------------------------------
105  if (!gl_scene->getByName("level_0"))
106  {
107  for (unsigned int i = 0; i < 5; i++)
108  {
109  auto gl_obj = mrpt::opengl::CSetOfObjects::Create();
110  gl_obj->setName(mrpt::format("level_%u", i));
111  gl_scene->insert(gl_obj);
112  }
113  }
114 
115  // Update view of map elements
116  // -----------------------------
117  m_timlogger.enter("update_GUI.2.map-elements");
118 
119  for (std::list<WorldElementBase*>::iterator it = m_world_elements.begin();
120  it != m_world_elements.end(); ++it)
121  (*it)->gui_update(*gl_scene);
122 
123  m_timlogger.leave("update_GUI.2.map-elements");
124 
125  // Update view of vehicles
126  // -----------------------------
127  m_timlogger.enter("update_GUI.3.vehicles");
128 
129  for (TListVehicles::iterator it = m_vehicles.begin();
130  it != m_vehicles.end(); ++it)
131  it->second->gui_update(*gl_scene);
132 
133  m_timlogger.leave("update_GUI.3.vehicles");
134 
135  // Update view of blocks
136  // -----------------------------
137  m_timlogger.enter("update_GUI.4.blocks");
138 
139  for (TListBlocks::iterator it = m_blocks.begin(); it != m_blocks.end();
140  ++it)
141  it->second->gui_update(*gl_scene);
142 
143  m_timlogger.leave("update_GUI.4.blocks");
144 
145  // Other messages
146  // -----------------------------
147  m_timlogger.enter("update_GUI.5.text-msgs");
148  {
149  const int txt_h = 12, space_h = 2; // font height
150  int txt_y = 4;
151 
152  // 1st line: time
153  m_gui_win->addTextMessage(
154  2, 2,
155  mrpt::format(
156  "Time: %s",
157  mrpt::system::formatTimeInterval(this->m_simul_time).c_str()),
158  TColorf(1, 1, 1, 0.5), "serif", txt_h,
159  mrpt::opengl::NICE, ID_GLTEXT_CLOCK);
160  txt_y += txt_h + space_h;
161 
162  // User supplied-lines:
163  if (guiparams)
164  {
165  const size_t nLines = std::count(
166  guiparams->msg_lines.begin(), guiparams->msg_lines.end(), '\n');
167  txt_y += nLines * (txt_h + space_h);
168  m_gui_win->addTextMessage(
169  2, txt_y, guiparams->msg_lines,
170  TColorf(1, 1, 1, 0.5), "serif", txt_h,
171  mrpt::opengl::NICE, ID_GLTEXT_CLOCK + 1);
172  }
173  }
174 
175  m_timlogger.leave("update_GUI.5.text-msgs");
176 
177  // Camera follow modes:
178  // -----------------------
179  if (!m_gui_options.follow_vehicle.empty())
180  {
181  TListVehicles::const_iterator it =
183  if (it == m_vehicles.end())
184  {
185  static bool warn1st = true;
186  if (warn1st)
187  {
188  std::cerr << mrpt::format(
189  "GUI: Camera set to follow vehicle named '%s' which can't "
190  "be found!\n",
191  m_gui_options.follow_vehicle.c_str());
192  warn1st = true;
193  }
194  }
195  else
196  {
197  const mrpt::poses::CPose2D pose = it->second->getCPose2D();
198  m_gui_win->setCameraPointingToPoint(pose.x(), pose.y(), 0.0f);
199  }
200  }
201 
202  // Force refresh view
203  // -----------------------
204  m_gui_win->unlockAccess3DScene(); // ** UNLOCK **
205  m_gui_win->repaint();
206 
207  m_timlogger.leave("update_GUI");
208 
209  // Key-strokes:
210  // -----------------------
211  if (guiparams && m_gui_win->keyHit())
212  guiparams->keyevent.keycode =
213  m_gui_win->getPushedKey(&guiparams->keyevent.key_modifier);
214 }
double force_scale
In meters/Newton.
Definition: World.h:255
This file contains rapidxml parser and DOM implementation.
mrpt::gui::CDisplayWindow3D::Ptr m_gui_win
Definition: World.h:284
size_t ID_GLTEXT_CLOCK
Definition: World_gui.cpp:57
static WorldElementBase * factory(World *parent, const rapidxml::xml_node< char > *xml_node, const char *class_name=NULL)
TListWorldElements m_world_elements
Definition: World.h:277
void update_GUI(TUpdateGUIParams *params=NULL)
Definition: World_gui.cpp:68
double m_simul_time
evaluate weights for friction, etc.
Definition: World.h:240
TListVehicles m_vehicles
Definition: World.h:276
std::string follow_vehicle
Name of the vehicle to follow (empty=none)
Definition: World.h:259
TGUIKeyEvent keyevent
Keystrokes in the window are returned here.
Definition: World.h:142
TListBlocks m_blocks
Definition: World.h:278
CTimeLogger m_timlogger
Definition: World.h:286
void parse_xmlnode_children_as_param(const rapidxml::xml_node< char > &xml_node, const std::map< std::string, TParamEntry > &params, const char *function_name_context="")
Definition: xml_utils.cpp:196
bool is_GUI_open() const
Forces closing the GUI window, if any.
Definition: World_gui.cpp:61
TGUI_Options m_gui_options
Definition: World.h:265
unsigned int win_w
Definition: World.h:252
std::string msg_lines
Messages to show.
Definition: World.h:143
mrpt::gui::mrptKeyModifier key_modifier
Definition: World.h:135
void parse_from(const rapidxml::xml_node< char > &node)
Definition: World_gui.cpp:40
unsigned int win_h
Definition: World.h:252
int keycode
0=no Key. Otherwise, ASCII code.
Definition: World.h:134
void close_GUI()
a previous call to update_GUI()
Definition: World_gui.cpp:63


mvsim
Author(s):
autogenerated on Thu Jun 6 2019 19:36:40