Block.h
Go to the documentation of this file.
1 /*+-------------------------------------------------------------------------+
2  | Multiblock 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 
10 #pragma once
11 
12 #include <mvsim/basic_types.h>
13 #include <mvsim/VisualObject.h>
14 #include <mvsim/Simulable.h>
15 #include <mvsim/ClassFactory.h>
16 
17 #include <Box2D/Dynamics/b2World.h>
18 #include <Box2D/Dynamics/b2Body.h>
22 
23 #include <mrpt/poses/CPose2D.h>
24 #include <mrpt/opengl/CSetOfObjects.h>
25 #include <mrpt/opengl/CSetOfLines.h>
26 #if MRPT_VERSION<0x199
27 #include <mrpt/utils/TColor.h>
28 using mrpt::utils::TColor;
29 #else
30 #include <mrpt/img/TColor.h>
31 using mrpt::img::TColor;
32 #endif
33 
34 namespace mvsim
35 {
38 class Block : public VisualObject, public Simulable
39 {
40  public:
43  static Block* factory(
44  World* parent, const rapidxml::xml_node<char>* xml_node);
46  static Block* factory(World* parent, const std::string& xml_text);
47 
50  static void register_block_class(const rapidxml::xml_node<char>* xml_node);
51 
52  // ------- Interface with "World" ------
53  virtual void simul_pre_timestep(const TSimulContext& context);
54  virtual void simul_post_timestep(const TSimulContext& context);
55  virtual void apply_force(
56  double fx, double fy, double local_ptx = 0.0, double local_pty = 0.0);
57 
59  void simul_post_timestep_common(const TSimulContext& context);
60 
63  virtual void create_multibody_system(b2World* world);
64 
67  virtual float getMaxBlockRadius() const { return m_max_radius; }
69  virtual double getMass() const { return m_mass; }
70  b2Body* getBox2DBlockBody() { return m_b2d_block_body; }
71  mrpt::math::TPoint2D getBlockCenterOfMass() const
72  {
73  return m_block_com;
74  }
75 
76  const mrpt::math::TPose3D& getPose() const
77  {
78  return m_q;
79  }
80  void setPose(const mrpt::math::TPose3D& p) const
82  {
83  const_cast<mrpt::math::TPose3D&>(m_q) = p;
84  }
85 
87  mrpt::poses::CPose2D getCPose2D() const;
88 
90  const vec3& getVelocity() const { return m_dq; }
93  vec3 getVelocityLocal() const;
94 
96  const std::string& getName() const { return m_name; }
99  const mrpt::math::TPolygon2D& getBlockShape() const { return m_block_poly; }
101  void setBlockIndex(size_t idx) { m_block_index = idx; }
103  size_t getBlockIndex() const { return m_block_index; }
106  virtual void gui_update(mrpt::opengl::COpenGLScene& scene);
107 
108  protected:
109  // Protected ctor for class factory
110  Block(World* parent);
111 
112  std::string
114  size_t m_block_index;
115 
122  b2Body* m_b2d_block_body;
123  std::vector<b2FrictionJoint*> m_friction_joints;
124 
125  mrpt::math::TPose3D
126  m_q;
127  vec3 m_dq;
128 
130  // Block info:
131  double m_mass;
132  mrpt::math::TPolygon2D m_block_poly;
133  double m_max_radius;
134  double m_block_z_min, m_block_z_max;
136  TColor m_block_color;
137  mrpt::math::TPoint2D m_block_com;
138 
139  double m_lateral_friction;
140  double m_ground_friction;
141  double m_restitution;
142 
143  void updateMaxRadiusFromPoly();
144 
145  // Box2D elements:
146  b2Fixture* m_fixture_block;
147 
148  private:
149  void internal_gui_update_forces(
150  mrpt::opengl::COpenGLScene&
151  scene);
152 
153  mrpt::opengl::CSetOfObjects::Ptr m_gl_block;
154  mrpt::opengl::CSetOfLines::Ptr m_gl_forces;
155  std::mutex m_force_segments_for_rendering_cs;
156  std::vector<mrpt::math::TSegment3D> m_force_segments_for_rendering;
157 
158 }; // end Block
159 }
vec3 getVelocityLocal() const
Definition: Block.cpp:275
mrpt::math::TPoint2D getBlockCenterOfMass() const
In local coordinates.
Definition: Block.h:71
virtual void apply_force(double fx, double fy, double local_ptx=0.0, double local_pty=0.0)
Definition: Block.cpp:432
std::string m_name
User-supplied name of the block (e.g. "r1", "veh1")
Definition: Block.h:113
size_t m_block_index
Definition: Block.h:114
const std::string & getName() const
Definition: Block.h:96
virtual float getMaxBlockRadius() const
Definition: Block.h:67
virtual double getMass() const
Definition: Block.h:69
virtual void simul_post_timestep(const TSimulContext &context)
Definition: Block.cpp:273
static void register_block_class(const rapidxml::xml_node< char > *xml_node)
Definition: Block.cpp:93
void simul_post_timestep_common(const TSimulContext &context)
Definition: Block.cpp:69
b2Body * getBox2DBlockBody()
Definition: Block.h:70
const mrpt::math::TPose3D & getPose() const
Definition: Block.h:76
const vec3 & getVelocity() const
Definition: Block.h:90
A rigid body. These are created via b2World::CreateBody.
Definition: b2Body.h:126
virtual void create_multibody_system(b2World *world)
Definition: Block.cpp:354
virtual void gui_update(mrpt::opengl::COpenGLScene &scene)
Definition: Block.cpp:292
static Block * factory(World *parent, const rapidxml::xml_node< char > *xml_node)
Definition: Block.cpp:114
const mrpt::math::TPolygon2D & getBlockShape() const
Definition: Block.h:99
void setPose(const mrpt::math::TPose3D &p) const
Definition: Block.h:81
size_t getBlockIndex() const
Definition: Block.h:103
void setBlockIndex(size_t idx)
Definition: Block.h:101
Block(World *parent)
Definition: Block.cpp:45
mrpt::poses::CPose2D getCPose2D() const
Definition: Block.cpp:286
virtual void simul_pre_timestep(const TSimulContext &context)
Definition: Block.cpp:270


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