Block.h
Go to the documentation of this file.
1 /*+-------------------------------------------------------------------------+
2  | Multiblock 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 #pragma once
11 
15 #include <Box2D/Dynamics/b2World.h>
16 #include <mrpt/img/TColor.h>
17 #include <mrpt/math/TPolygon2D.h>
20 #include <mrpt/poses/CPose2D.h>
21 #include <mvsim/ClassFactory.h>
22 #include <mvsim/Simulable.h>
24 #include <mvsim/VisualObject.h>
25 #include <mvsim/basic_types.h>
26 
27 #include <mutex>
28 
29 namespace mvsim
30 {
33 class Block : public VisualObject, public Simulable
34 {
35  public:
36  using Ptr = std::shared_ptr<Block>;
37 
40  static Ptr factory(World* parent, const rapidxml::xml_node<char>* xml_node);
42  static Ptr factory(World* parent, const std::string& xml_text);
43 
46  static void register_block_class(const rapidxml::xml_node<char>* xml_node);
47 
48  // ------- Interface with "World" ------
49  virtual void simul_pre_timestep(const TSimulContext& context) override;
50  virtual void simul_post_timestep(const TSimulContext& context) override;
51  virtual void apply_force(
52  const mrpt::math::TVector2D& force,
53  const mrpt::math::TPoint2D& applyPoint =
54  mrpt::math::TPoint2D(0, 0)) override;
55 
58  virtual void create_multibody_system(b2World& world);
59 
62  virtual float getMaxBlockRadius() const { return m_max_radius; }
64  virtual double getMass() const { return m_mass; }
65  b2Body* getBox2DBlockBody() { return m_b2d_body; }
67  {
68  return m_block_com;
69  }
70 
73  const mrpt::math::TPolygon2D& blockShape() const { return m_block_poly; }
74 
76  {
77  m_block_poly = p;
79  m_gl_block.reset(); // regenerate 3D view
80  }
81 
83  void setBlockIndex(size_t idx) { m_block_index = idx; }
85  size_t getBlockIndex() const { return m_block_index; }
86 
87  Block(World* parent);
88 
89  double ground_friction() const { return m_ground_friction; }
90  void ground_friction(double newValue) { m_ground_friction = newValue; }
91 
92  double mass() const { return m_mass; }
93  void mass(double newValue) { m_mass = newValue; }
94 
95  bool isStatic() const;
96  void setIsStatic(bool b);
97 
98  void poses_mutex_lock() override { m_gui_mtx.lock(); }
99  void poses_mutex_unlock() override { m_gui_mtx.unlock(); }
100 
101  const mrpt::img::TColor block_color() const { return m_block_color; }
102  void block_color(const mrpt::img::TColor& c)
103  {
104  m_block_color = c;
105  m_gl_block.reset(); // regenerate 3D view
106  }
107 
108  double block_z_min() const { return m_block_z_min; }
109  double block_z_max() const { return m_block_z_max; }
110  void block_z_min(double v)
111  {
112  m_block_z_min = v;
113  m_gl_block.reset(); // regenerate 3D view
114  }
115  void block_z_max(double v)
116  {
117  m_block_z_max = v;
118  m_gl_block.reset(); // regenerate 3D view
119  }
120 
121  protected:
122  virtual void internalGuiUpdate(
123  mrpt::opengl::COpenGLScene& scene, bool childrenOnly) override;
125 
128  size_t m_block_index = 0;
129 
130  std::vector<b2FrictionJoint*> m_friction_joints;
131 
132  // Block info:
133  double m_mass = 30.0;
134  bool m_isStatic = false;
136  double m_max_radius;
137  double m_block_z_min = 0.0, m_block_z_max = 1.0;
139  mrpt::img::TColor m_block_color{0x00, 0x00, 0xff};
141 
142  double m_lateral_friction = 0.5;
143  double m_ground_friction = 0.5;
144  double m_restitution = 0.01;
145 
147  {"mass", {"%lf", &m_mass}},
148  {"zmin", {"%lf", &m_block_z_min}},
149  {"zmax", {"%lf", &m_block_z_max}},
150  {"ground_friction", {"%lf", &m_ground_friction}},
151  {"lateral_friction", {"%lf", &m_lateral_friction}},
152  {"restitution", {"%lf", &m_restitution}},
153  {"color", {"%color", &m_block_color}}};
154 
156 
157  // Box2D elements:
159 
160  private:
162 
163  mrpt::opengl::CSetOfObjects::Ptr m_gl_block;
164  mrpt::opengl::CSetOfLines::Ptr m_gl_forces;
166  std::vector<mrpt::math::TSegment3D> m_force_segments_for_rendering;
167 
168  std::mutex m_gui_mtx;
169 
170 }; // end Block
171 } // namespace mvsim
void blockShape(const mrpt::math::TPolygon2D &p)
Definition: Block.h:75
const GLdouble * v
double m_block_z_max
Definition: Block.h:138
virtual mrpt::poses::CPose3D internalGuiGetVisualPose() override
Definition: Block.cpp:255
void poses_mutex_unlock() override
Definition: Block.h:99
mrpt::math::TPoint2D getBlockCenterOfMass() const
In local coordinates.
Definition: Block.h:66
size_t m_block_index
Definition: Block.h:128
std::map< std::string, TParamEntry > TParameterDefinitions
mrpt::math::TPoint2D m_block_com
In local coordinates.
Definition: Block.h:140
double m_max_radius
Definition: Block.h:136
void updateMaxRadiusFromPoly()
Definition: Block.cpp:316
const GLfloat * c
mrpt::opengl::CSetOfObjects::Ptr m_gl_block
Definition: Block.h:163
virtual float getMaxBlockRadius() const
Definition: Block.h:62
virtual double getMass() const
Definition: Block.h:64
double block_z_min() const
Definition: Block.h:108
void block_z_min(double v)
Definition: Block.h:110
virtual void apply_force(const mrpt::math::TVector2D &force, const mrpt::math::TPoint2D &applyPoint=mrpt::math::TPoint2D(0, 0)) override
Definition: Block.cpp:408
static Ptr factory(World *parent, const rapidxml::xml_node< char > *xml_node)
Definition: Block.cpp:68
static void register_block_class(const rapidxml::xml_node< char > *xml_node)
Definition: Block.cpp:48
scene
void setIsStatic(bool b)
Definition: Block.cpp:424
const mrpt::math::TPolygon2D & blockShape() const
Definition: Block.h:73
void mass(double newValue)
Definition: Block.h:93
b2Body * getBox2DBlockBody()
Definition: Block.h:65
std::shared_ptr< Block > Ptr
Definition: Block.h:36
double m_lateral_friction
Default: 0.5.
Definition: Block.h:142
A rigid body. These are created via b2World::CreateBody.
Definition: b2Body.h:126
bool m_isStatic
Definition: Block.h:134
double m_mass
Definition: Block.h:133
virtual void create_multibody_system(b2World &world)
Definition: Block.cpp:329
mrpt::opengl::CSetOfLines::Ptr m_gl_forces
Definition: Block.h:164
GLfloat GLfloat p
double ground_friction() const
Definition: Block.h:89
double block_z_max() const
Definition: Block.h:109
double m_ground_friction
Default: 0.5.
Definition: Block.h:143
std::mutex m_force_segments_for_rendering_cs
Definition: Block.h:165
void block_color(const mrpt::img::TColor &c)
Definition: Block.h:102
std::vector< mrpt::math::TSegment3D > m_force_segments_for_rendering
Definition: Block.h:166
virtual void simul_pre_timestep(const TSimulContext &context) override
Definition: Block.cpp:243
mrpt::math::TPolygon2D m_block_poly
Definition: Block.h:135
void block_z_max(double v)
Definition: Block.h:115
double m_block_z_min
each change via updateMaxRadiusFromPoly()
Definition: Block.h:138
virtual void internalGuiUpdate(mrpt::opengl::COpenGLScene &scene, bool childrenOnly) override
Definition: Block.cpp:260
size_t getBlockIndex() const
Definition: Block.h:85
void internal_internalGuiUpdate_forces(mrpt::opengl::COpenGLScene &scene)
Definition: Block.cpp:301
void setBlockIndex(size_t idx)
Definition: Block.h:83
bool isStatic() const
Definition: Block.cpp:418
double mass() const
Definition: Block.h:92
const TParameterDefinitions m_params
Definition: Block.h:146
virtual void simul_post_timestep(const TSimulContext &context) override
Definition: Block.cpp:250
mrpt::img::TColor m_block_color
Definition: Block.h:139
void poses_mutex_lock() override
Definition: Block.h:98
GLdouble GLdouble GLdouble b
double m_restitution
Deault: 0.01.
Definition: Block.h:144
Block(World *parent)
Definition: Block.cpp:36
b2Fixture * m_fixture_block
Definition: Block.h:158
std::mutex m_gui_mtx
Definition: Block.h:168
const mrpt::img::TColor block_color() const
Definition: Block.h:101
std::vector< b2FrictionJoint * > m_friction_joints
Definition: Block.h:130
void ground_friction(double newValue)
Definition: Block.h:90


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