VerticalPlane.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 <mrpt/system/filesystem.h>
12 #include <mrpt/version.h>
13 #include <mvsim/World.h>
15 
16 #include <rapidxml.hpp>
17 
18 #include "JointXMLnode.h"
19 #include "xml_utils.h"
20 
21 using namespace rapidxml;
22 using namespace mvsim;
23 using namespace std;
24 
25 VerticalPlane::VerticalPlane(
26  World* parent, const rapidxml::xml_node<char>* root)
27  : WorldElementBase(parent)
28 {
29  // Create opengl object: in this class, we'll store most state data directly
30  // in the mrpt::opengl object.
32 }
33 
35 
37 {
38  ASSERT_(root);
39 
40  // Common setup for simulable objects:
41  // -----------------------------------------------------------
42  {
44  p.init_pose_mandatory = false;
45 
46  JointXMLnode<> jnode;
47  jnode.add(root);
48  parseSimulable(jnode, p);
49  }
50 
51  TParameterDefinitions params;
52  params["color"] = TParamEntry("%color", &color_);
53  params["enable_shadows"] = TParamEntry("%bool", &enableShadows_);
54 
55  params["x0"] = TParamEntry("%f", &x0_);
56  params["x1"] = TParamEntry("%f", &x1_);
57  params["y0"] = TParamEntry("%f", &y0_);
58  params["y1"] = TParamEntry("%f", &y1_);
59  params["z"] = TParamEntry("%f", &z_);
60  params["height"] = TParamEntry("%f", &height_);
61  params["cull_face"] = TParamEntry("%s", &cull_faces_);
62 
63  params["texture"] = TParamEntry("%s", &textureFileName_);
64  params["texture_size_x"] = TParamEntry("%lf", &textureSizeX_);
65  params["texture_size_y"] = TParamEntry("%lf", &textureSizeY_);
66 
68  *root, params, world_->user_defined_variables());
69 
70  // Create box2d fixtures, to enable collision detection:
71  // --------------------------------------------------------
72  b2World& world = *world_->getBox2DWorld();
73 
74  // Define the dynamic body. We set its position and call the body factory.
75  b2BodyDef bodyDef;
76  bodyDef.type = b2_staticBody;
77 
78  b2dBody_ = world.CreateBody(&bodyDef);
79 
80  // Define shape of block:
81  // ------------------------------
82  {
83  const float thickness = 0.02f;
84 
85  const mrpt::math::TPoint2Df p0 = {x0_, y0_};
86  const mrpt::math::TPoint2Df p1 = {x1_, y1_};
87  ASSERT_(p0 != p1);
88  const auto v01 = p1 - p0;
89  const mrpt::math::TPoint2Df normal = {-v01.y, v01.x};
90  const auto w = normal.unitarize() * thickness;
91 
92  // Convert shape into Box2D format:
93  const size_t nPts = 4;
94  std::vector<b2Vec2> pts;
95 
96  const auto p00 = p0 - w, p01 = p0 + w;
97  const auto p10 = p1 - w, p11 = p1 + w;
98 
99  pts.emplace_back(p00.x, p00.y);
100  pts.emplace_back(p01.x, p01.y);
101  pts.emplace_back(p11.x, p11.y);
102  pts.emplace_back(p10.x, p10.y);
103 
104  b2PolygonShape blockPoly;
105  blockPoly.Set(&pts[0], nPts);
106 
107  // Define the dynamic body fixture.
108  b2FixtureDef fixtureDef;
109  fixtureDef.shape = &blockPoly;
110  fixtureDef.restitution = restitution_;
111 
112  // Add the shape to the body.
113  fixture_block_ = b2dBody_->CreateFixture(&fixtureDef);
114  }
115 
116  // Init pos: (the vertices already have the global coordinates)
117  b2dBody_->SetTransform(b2Vec2(0, 0), 0);
118 }
119 
121  const mrpt::optional_ref<mrpt::opengl::COpenGLScene>& viz,
122  const mrpt::optional_ref<mrpt::opengl::COpenGLScene>& physical,
123  [[maybe_unused]] bool childrenOnly)
124 {
125  using namespace mrpt::math;
126  using namespace std::string_literals;
127 
128  if (!glGroup_) glGroup_ = mrpt::opengl::CSetOfObjects::Create();
129 
130  // 1st call? (w/o texture)
131  if (!gl_plane_ && textureFileName_.empty() && viz && physical)
132  {
133  const mrpt::math::TPoint3Df p0 = {x0_, y0_, z_};
134  const mrpt::math::TPoint3Df p1 = {x1_, y1_, z_};
135 
136  ASSERT_(p0 != p1);
137  const auto v01 = p1 - p0;
138 
139  const float L = v01.norm(), H = height_;
140 
141  const auto center =
142  (p0 + p1) * 0.5f + mrpt::math::TPoint3Df(0, 0, 0.5f * H);
143 
144  gl_plane_ = mrpt::opengl::CTexturedPlane::Create();
145  gl_plane_->setPlaneCorners(-0.5 * L, 0.5 * L, -0.5 * H, 0.5 * H);
146 
147  mrpt::math::TPose3D p;
148  p.x = center.x;
149  p.y = center.y;
150  p.z = center.z;
151  p.yaw = std::atan2(v01.y, v01.x);
152  p.roll = mrpt::DEG2RAD(90.0);
153 
154  gl_plane_->setPose(p);
155  gl_plane_->setName("VerticalPlane_"s + getName());
156 
157 #if MRPT_VERSION >= 0x270
158  gl_plane_->enableLighting(enableShadows_);
159 #endif
160 
161  gl_plane_->setColor_u8(color_);
162 
163 #if MRPT_VERSION >= 0x240
164  gl_plane_->cullFaces(
165  mrpt::typemeta::TEnumType<mrpt::opengl::TCullFace>::name2value(
166  cull_faces_));
167 #endif
168  glGroup_->insert(gl_plane_);
169  viz->get().insert(glGroup_);
170  physical->get().insert(glGroup_);
171  }
172  // 1st call? (with texture)
173  if (!gl_plane_text_ && !textureFileName_.empty() && viz && physical)
174  {
175  const std::string localFileName =
177  ASSERT_FILE_EXISTS_(localFileName);
178 
179  mrpt::img::CImage texture;
180  bool textureReadOk = texture.loadFromFile(localFileName);
181  ASSERT_(textureReadOk);
182 
183  const mrpt::math::TPoint3Df p0 = {x0_, y0_, z_};
184  const mrpt::math::TPoint3Df p1 = {x1_, y1_, z_};
185 
186  ASSERT_(p0 != p1);
187  const auto v01 = p1 - p0;
188 
189  const float L = v01.norm(), H = height_;
190 
191  // Compute (U,V) texture coordinates:
192  float u_min = 0;
193  float v_min = 0;
194  float u_max = L / textureSizeX_;
195  float v_max = H / textureSizeY_;
196 
197  gl_plane_text_ = mrpt::opengl::CSetOfTexturedTriangles::Create();
198  gl_plane_text_->setName("VerticalPlane_"s + getName());
199 
200  gl_plane_text_->enableLight(enableShadows_);
201 
202  {
203  mrpt::opengl::CSetOfTexturedTriangles::TTriangle t;
204  t.vertices[0].xyzrgba.pt = {x0_, y0_, z_};
205  t.vertices[1].xyzrgba.pt = {x1_, y1_, z_};
206  t.vertices[2].xyzrgba.pt = {x1_, y1_, z_ + H};
207 
208  t.vertices[0].uv = {u_min, v_min};
209  t.vertices[1].uv = {u_max, v_min};
210  t.vertices[2].uv = {u_max, v_max};
211 
212  t.computeNormals();
213  gl_plane_text_->insertTriangle(t);
214  }
215  {
216  mrpt::opengl::CSetOfTexturedTriangles::TTriangle t;
217  t.vertices[0].xyzrgba.pt = {x0_, y0_, z_};
218  t.vertices[1].xyzrgba.pt = {x1_, y1_, z_ + H};
219  t.vertices[2].xyzrgba.pt = {x0_, y0_, z_ + H};
220 
221  t.vertices[0].uv = {u_min, v_min};
222  t.vertices[1].uv = {u_max, v_max};
223  t.vertices[2].uv = {u_min, v_max};
224 
225  t.computeNormals();
226  gl_plane_text_->insertTriangle(t);
227  }
228 
229  gl_plane_text_->assignImage(texture);
230 
231 #if MRPT_VERSION >= 0x240
232  gl_plane_text_->cullFaces(
233  mrpt::typemeta::TEnumType<mrpt::opengl::TCullFace>::name2value(
234  cull_faces_));
235 #endif
236 
237  glGroup_->insert(gl_plane_text_);
238  viz->get().insert(glGroup_);
239  physical->get().insert(glGroup_);
240  }
241 
242  // Update them:
243  // If "viz" does not have a value, it's because we are already inside a
244  // setPose() change event, so my caller already holds the mutex and we
245  // don't need/can't acquire it again:
246  const auto objectPose = viz.has_value() ? getPose() : getPoseNoLock();
247  glGroup_->setPose(objectPose);
248 }
249 
251 {
253 }
254 
256 {
258 }
const b2Shape * shape
Definition: b2_fixture.h:76
virtual void loadConfigFrom(const rapidxml::xml_node< char > *root) override
This file contains rapidxml parser and DOM implementation.
b2Fixture * CreateFixture(const b2FixtureDef *def)
Definition: b2_body.cpp:165
std::map< std::string, TParamEntry > TParameterDefinitions
std::string cull_faces_
Definition: VerticalPlane.h:52
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
f
mrpt::math::TPose3D getPose() const
Definition: Simulable.cpp:438
void SetTransform(const b2Vec2 &position, float angle)
Definition: b2_body.cpp:415
mrpt::math::TPose3D getPoseNoLock() const
No thread-safe version. Used internally only.
Definition: Simulable.cpp:446
virtual void internalGuiUpdate(const mrpt::optional_ref< mrpt::opengl::COpenGLScene > &viz, const mrpt::optional_ref< mrpt::opengl::COpenGLScene > &physical, bool childrenOnly) override
XmlRpcServer s
std::unique_ptr< b2World > & getBox2DWorld()
Definition: World.h:297
geometry_msgs::TransformStamped t
A 2D column vector.
Definition: b2_math.h:41
mrpt::opengl::CTexturedPlane::Ptr gl_plane_
Definition: VerticalPlane.h:54
b2BodyType type
Definition: b2_body.h:74
void simul_pre_timestep(const TSimulContext &context) override
std::string textureFileName_
Definition: VerticalPlane.h:47
mrpt::img::TColor color_
Definition: VerticalPlane.h:43
const std::map< std::string, std::string > & user_defined_variables() const
Definition: World.h:391
virtual void simul_post_timestep(const TSimulContext &context)
Definition: Simulable.cpp:59
const std::string & getName() const
Definition: Simulable.h:92
void simul_post_timestep(const TSimulContext &context) override
b2Fixture * fixture_block_
Definition: VerticalPlane.h:60
virtual void simul_pre_timestep(const TSimulContext &context)
Definition: Simulable.cpp:30
mrpt::opengl::CSetOfTexturedTriangles::Ptr gl_plane_text_
Definition: VerticalPlane.h:55
void Set(const b2Vec2 *points, int32 count)
mrpt::opengl::CSetOfObjects::Ptr glGroup_
Definition: VerticalPlane.h:56
std::string xmlPathToActualPath(const std::string &modelURI) const
Definition: World.cpp:244
float restitution
The restitution (elasticity) usually in the range [0,1].
Definition: b2_fixture.h:85
double restitution_
Default: 0.01.
Definition: VerticalPlane.h:59
void add(const rapidxml::xml_node< Ch > *node)
Definition: JointXMLnode.h:28


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