VisualObject.cpp
Go to the documentation of this file.
1 /*+-------------------------------------------------------------------------+
2  | MultiVehicle simulator (libmvsim) |
3  | |
4  | Copyright (C) 2014-2020 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/core/get_env.h>
12 #include <mrpt/opengl/CBox.h>
15 #include <mrpt/system/filesystem.h>
16 #include <mrpt/version.h>
17 #include <mvsim/VisualObject.h>
18 #include <mvsim/World.h>
19 
20 #include <atomic>
21 #include <rapidxml.hpp>
22 
23 #include "xml_utils.h"
24 
25 using namespace mvsim;
26 
27 static std::atomic_int32_t g_uniqueCustomVisualId = 0;
28 
30 {
31  using namespace std::string_literals;
32 
33  const auto objectPose = internalGuiGetVisualPose();
34 
35  if (m_glCustomVisual)
36  {
37  // Assign a unique ID on first call:
38  if (m_glCustomVisualId < 0)
39  {
40  // Assign a unique name, so we can localize the object in the scene
41  // if needed.
43  const auto name = "_autoViz"s + std::to_string(m_glCustomVisualId);
44  m_glCustomVisual->setName(name);
45  // Add to the 3D scene:
46  scene.insert(m_glCustomVisual);
47  }
48 
49  // Update pose:
50  m_glCustomVisual->setPose(objectPose);
51  }
52 
53  if (m_glBoundingBox)
54  {
55  if (m_glBoundingBox->empty())
56  {
57  auto glBox = mrpt::opengl::CBox::Create();
58  glBox->setWireframe(true);
59  glBox->setBoxCorners(viz_bbmin_, viz_bbmax_);
60  m_glBoundingBox->insert(glBox);
61  m_glBoundingBox->setVisibility(false);
62  scene.insert(m_glBoundingBox);
63  }
64  m_glBoundingBox->setPose(objectPose);
65  }
66 
67  const bool childrenOnly = !!m_glCustomVisual;
68 
69  internalGuiUpdate(scene, childrenOnly);
70 }
71 
72 std::map<std::string, mrpt::opengl::CAssimpModel::Ptr> gModelsCache;
73 
75 {
77 
78  m_glBoundingBox = mrpt::opengl::CSetOfObjects::Create();
79 
80  if (visual_node == nullptr) return false;
81 
82  std::string modelURI;
83  double modelScale = 1.0;
84  mrpt::math::TPose3D modelPose;
85  bool initialShowBoundingBox = false;
86 
88  params["model_uri"] = TParamEntry("%s", &modelURI);
89  params["model_scale"] = TParamEntry("%lf", &modelScale);
90  params["model_offset_x"] = TParamEntry("%lf", &modelPose.x);
91  params["model_offset_y"] = TParamEntry("%lf", &modelPose.y);
92  params["model_offset_z"] = TParamEntry("%lf", &modelPose.z);
93  params["model_yaw"] = TParamEntry("%lf_deg", &modelPose.yaw);
94  params["model_pitch"] = TParamEntry("%lf_deg", &modelPose.pitch);
95  params["model_roll"] = TParamEntry("%lf_deg", &modelPose.roll);
96  params["show_bounding_box"] = TParamEntry("%bool", &initialShowBoundingBox);
97 
98  // Parse XML params:
99  parse_xmlnode_children_as_param(*visual_node, params);
100 
101  if (modelURI.empty()) return false;
102 
103  const std::string localFileName = m_world->xmlPathToActualPath(modelURI);
104  ASSERT_FILE_EXISTS_(localFileName);
105 
106  auto glGroup = mrpt::opengl::CSetOfObjects::Create();
107 
108  auto glModel = [&]() {
109  if (auto it = gModelsCache.find(localFileName);
110  it != gModelsCache.end())
111  return it->second;
112  else
113  {
114  auto m = gModelsCache[localFileName] =
115  mrpt::opengl::CAssimpModel::Create();
116 
117  // En/Dis-able the extra verbosity while loading the 3D model:
118  int loadFlags =
119  mrpt::opengl::CAssimpModel::LoadFlags::RealTimeMaxQuality |
120  mrpt::opengl::CAssimpModel::LoadFlags::FlipUVs;
121 
122  if (mrpt::get_env<bool>("MVSIM_LOAD_MODELS_VERBOSE", false))
123  loadFlags |= mrpt::opengl::CAssimpModel::LoadFlags::Verbose;
124 
125  m->loadScene(localFileName, loadFlags);
126  return m;
127  }
128  }();
129 
130  mrpt::math::TPoint3D bbmin, bbmax;
131 #if MRPT_VERSION >= 0x218
132  const auto bb = glModel->getBoundingBox();
133  bbmin = bb.min;
134  bbmax = bb.max;
135 #else
136  glModel->getBoundingBox(bbmin, bbmax);
137 #endif
138  glGroup->insert(glModel);
139 
140  glGroup->setScale(modelScale);
141  glGroup->setPose(modelPose);
142  glGroup->setName("group");
143 
144  m_glCustomVisual = mrpt::opengl::CSetOfObjects::Create();
145  m_glCustomVisual->insert(glGroup);
146  m_glBoundingBox->setVisibility(initialShowBoundingBox);
147 
148  // Auto bounds from visual model bounding-box:
149 
150  // Apply transformation to bounding box too:
151  viz_bbmin_ = modelPose.composePoint(bbmin * modelScale);
152  viz_bbmax_ = modelPose.composePoint(bbmax * modelScale);
153 
154  return true;
156 }
157 
159 {
160  if (!m_glBoundingBox) return;
161  m_glBoundingBox->setVisibility(show);
162 }
This file contains rapidxml parser and DOM implementation.
std::shared_ptr< mrpt::opengl::CSetOfObjects > m_glBoundingBox
Definition: VisualObject.h:55
std::map< std::string, TParamEntry > TParameterDefinitions
std::map< std::string, mrpt::opengl::CAssimpModel::Ptr > gModelsCache
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="")
Definition: xml_utils.cpp:179
std::string xmlPathToActualPath(const std::string &modelURI) const
Definition: World.cpp:140
virtual void guiUpdate(mrpt::opengl::COpenGLScene &scene)
std::shared_ptr< mrpt::opengl::CSetOfObjects > m_glCustomVisual
Definition: VisualObject.h:54
int32_t m_glCustomVisualId
Definition: VisualObject.h:56
virtual mrpt::poses::CPose3D internalGuiGetVisualPose()
Definition: VisualObject.h:60
void insert(const CRenderizablePtr &newObject, const std::string &viewportName=std::string("main"))
#define MRPT_TRY_END
GLdouble s
GLuint const GLchar * name
mrpt::math::TPoint3D viz_bbmin_
Definition: VisualObject.h:63
static std::atomic_int32_t g_uniqueCustomVisualId
void showBoundingBox(bool show)
#define MRPT_TRY_START
GLfloat * params
#define ASSERT_FILE_EXISTS_(FIL)
const GLdouble * m
bool parseVisual(const rapidxml::xml_node< char > *visual_node)
mrpt::math::TPoint3D viz_bbmax_
Definition: VisualObject.h:63
static CBoxPtr Create(const mrpt::math::TPoint3D &corner1, const mrpt::math::TPoint3D &corner2, bool is_wireframe=false, float lineWidth=1.0)
virtual void internalGuiUpdate(mrpt::opengl::COpenGLScene &scene, bool childrenOnly=false)=0


mvsim
Author(s):
autogenerated on Fri May 7 2021 03:05:51