10 #include <mrpt/core/bits_math.h> 11 #include <mrpt/core/format.h> 12 #include <mrpt/core/lock_helper.h> 13 #include <mrpt/math/TPose2D.h> 30 using namespace mvsim;
53 "[Block::register_vehicle_class] XML node is nullptr");
54 if (0 != strcmp(xml_node->
name(),
"block:class"))
56 "[Block::register_block_class] XML element is '%s' " 57 "('block:class' expected)",
73 if (!root)
throw runtime_error(
"[Block::factory] XML node is nullptr");
74 if (0 != strcmp(root->
name(),
"block"))
76 "[Block::factory] XML root element is '%s' ('block' expected)",
90 const string sClassName = block_class->
value();
94 "[Block::factory] Block class '%s' undefined",
96 block_root_node.
add(class_root);
109 if (attrib_name && attrib_name->
value())
111 block->m_name = attrib_name->
value();
123 const xml_node<>* node = block_root_node.
first_node(
"init_pose");
126 "[Block::factory] Missing XML node <init_pose>");
129 if (3 != ::sscanf(node->value(),
"%lf %lf %lf", &q.
x, &q.
y, &q.
yaw))
131 "[Block::factory] Error parsing <init_pose>...</init_pose>");
138 const xml_node<>* node = block_root_node.
first_node(
"init_vel");
144 node->value(),
"%lf %lf %lf", &dq.
vx, &dq.
vy, &dq.
omega))
146 "[Block::factory] Error parsing <init_vel>...</init_vel>");
150 dq.
rotate(block->getPose().yaw);
157 block->parseVisual(block_root_node.
first_node(
"visual"));
158 block->parseSimulable(block_root_node.
first_node(
"publish"));
163 *root, block->m_params, {},
"[Block::factory]");
166 *class_root, block->m_params, {},
"[Block::factory]");
170 block_root_node.
first_node(
"shape_from_visual");
174 block->getVisualModelBoundingBox(bbmin, bbmax);
178 "Error: Tag <shape_from_visual/> found but bounding box of " 179 "visual object seems incorrect.");
182 block->m_block_poly.clear();
183 block->m_block_poly.emplace_back(bbmin.
x, bbmin.
y);
184 block->m_block_poly.emplace_back(bbmin.
x, bbmax.
y);
185 block->m_block_poly.emplace_back(bbmax.
x, bbmax.
y);
186 block->m_block_poly.emplace_back(bbmax.
x, bbmin.
y);
188 block->updateMaxRadiusFromPoly();
197 *xml_shape, block->m_block_poly,
"[Block::factory]");
198 block->updateMaxRadiusFromPoly();
205 if (block->m_b2d_body)
208 const auto q = block->getPose();
209 const auto dq = block->getTwist();
211 block->m_b2d_body->SetTransform(
b2Vec2(
q.x,
q.y),
q.yaw);
213 block->m_b2d_body->SetLinearVelocity(
b2Vec2(dq.vx, dq.vy));
214 block->m_b2d_body->SetAngularVelocity(dq.omega);
226 char* input_str =
const_cast<char*
>(xml_text.c_str());
230 xml.
parse<0>(input_str);
235 static_cast<long>(std::count(input_str, e.
where<
char>(),
'\n') + 1);
237 "[Block::factory] XML parse error (Line %u): %s",
238 static_cast<unsigned>(line), e.
what()));
271 m_gl_block = mrpt::opengl::CSetOfObjects::Create();
323 const float n = segment.norm();
344 std::vector<b2Vec2> pts(nPts);
345 for (
size_t i = 0; i < nPts; i++)
349 blockPoly.
Set(&pts[0], nPts);
354 fixtureDef.
shape = &blockPoly;
377 const size_t nContactPoints = 2;
378 const double weight_per_contact_point =
381 const double max_friction = mu * weight_per_contact_point;
391 fjd.
bodyB = m_b2d_body;
393 for (
size_t i = 0; i < nContactPoints; i++)
414 m_b2d_body->GetWorldPoint(
b2Vec2(applyPoint.
x, applyPoint.
y));
415 m_b2d_body->ApplyForce(
b2Vec2(force.x, force.y), wPt,
true );
This file contains rapidxml parser and DOM implementation.
GLint GLint GLint GLint GLint GLint y
virtual mrpt::poses::CPose3D internalGuiGetVisualPose() override
b2Vec2 localAnchorA
The local anchor point relative to bodyA's origin.
mrpt::math::TPoint2D m_block_com
In local coordinates.
void parse_xmlnode_children_as_param(const rapidxml::xml_node< char > &xml_node, const TParameterDefinitions ¶ms, const std::map< std::string, std::string > &variableNamesValues={}, const char *functionNameContext="")
void updateMaxRadiusFromPoly()
mrpt::opengl::CSetOfObjects::Ptr m_gl_block
#define THROW_EXCEPTION(msg)
b2Vec2 localAnchorB
The local anchor point relative to bodyB's origin.
virtual void apply_force(const mrpt::math::TVector2D &force, const mrpt::math::TPoint2D &applyPoint=mrpt::math::TPoint2D(0, 0)) override
float32 maxForce
The maximum friction force in N.
static Ptr factory(World *parent, const rapidxml::xml_node< char > *xml_node)
static void register_block_class(const rapidxml::xml_node< char > *xml_node)
mrpt::math::TPose3D getPose() const
static CSetOfLinesPtr Create(const std::vector< mrpt::math::TSegment3D > &sgms, const bool antiAliasing=true)
std::unique_ptr< b2World > & getBox2DWorld()
b2Vec2 center
The position of the shape's centroid relative to the shape's origin.
xml_node< Ch > * first_node(const Ch *name=0, std::size_t name_size=0, bool case_sensitive=true) const
void insert(const CRenderizablePtr &newObject, const std::string &viewportName=std::string("main"))
std::shared_ptr< Block > Ptr
Friction joint definition.
double get_gravity() const
double m_lateral_friction
Default: 0.5.
void ComputeMass(b2MassData *massData, float32 density) const
float32 mass
The mass of the shape, usually in kilograms.
static CPolyhedronPtr CreateCustomPrism(const std::vector< mrpt::math::TPoint2D > &baseVertices, double height)
GLint GLint GLint GLint GLint x
std::string BASE_IMPEXP format(const char *fmt,...) MRPT_printf_format_check(1
virtual void create_multibody_system(b2World &world)
mrpt::opengl::CSetOfLines::Ptr m_gl_forces
float32 maxTorque
The maximum friction torque in N-m.
TGUI_Options m_gui_options
virtual const char * what() const
xml_attribute< Ch > * first_attribute(const Ch *name=0, std::size_t name_size=0, bool case_sensitive=true) const
#define b2_maxPolygonVertices
double m_ground_friction
Default: 0.5.
void GetMassData(b2MassData *massData) const
void rotate(const double ang)
std::mutex m_force_segments_for_rendering_cs
float32 density
The density, usually in kg/m^2.
virtual void simul_post_timestep(const TSimulContext &context)
std::vector< mrpt::math::TSegment3D > m_force_segments_for_rendering
const rapidxml::xml_node< Ch > * first_node(const char *name)
virtual void simul_pre_timestep(const TSimulContext &context) override
mrpt::math::TPolygon2D m_block_poly
virtual void simul_pre_timestep(const TSimulContext &context)
double m_block_z_min
each change via updateMaxRadiusFromPoly()
virtual void internalGuiUpdate(mrpt::opengl::COpenGLScene &scene, bool childrenOnly) override
void internal_internalGuiUpdate_forces(mrpt::opengl::COpenGLScene &scene)
void Set(const b2Vec2 *points, int32 count)
b2Body * getBox2DGroundBody()
virtual void simul_post_timestep(const TSimulContext &context) override
mrpt::img::TColor m_block_color
float32 restitution
The restitution (elasticity) usually in the range [0,1].
const rapidxml::xml_node< char > * get(const std::string &xml_node_class) const
void parse_xmlnode_shape(const rapidxml::xml_node< char > &xml_node, mrpt::math::TPolygon2D &out_poly, const char *functionNameContext="")
This file contains rapidxml printer implementation.
GLdouble GLdouble GLdouble b
void add(const std::string &input_xml_node_class)
GLdouble GLdouble GLdouble GLdouble q
double m_restitution
Deault: 0.01.
b2Fixture * m_fixture_block
b2Body * bodyA
The first attached body.
This holds the mass data computed for a shape.
void add(const rapidxml::xml_node< Ch > *node)
static XmlClassesRegistry block_classes_registry("block:class")
float32 friction
The friction coefficient, usually in the range [0,1].
b2Body * CreateBody(const b2BodyDef *def)
std::vector< b2FrictionJoint * > m_friction_joints
b2Body * bodyB
The second attached body.