MVSim
Lightweight simulator for 2.5D vehicles and robots
Simulable.h
1 /*+-------------------------------------------------------------------------+
2  | MultiVehicle simulator (libmvsim) |
3  | |
4  | Copyright (C) 2014-2026 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 
12 #include <box2d/b2_body.h>
13 #include <mrpt/math/TPoint2D.h>
14 #include <mrpt/math/TPose3D.h>
15 #include <mrpt/poses/CPose2D.h>
16 #include <mrpt/poses/CPose3DInterpolator.h>
17 #include <mvsim/basic_types.h>
18 
19 #include <shared_mutex>
20 
21 namespace mvsim
22 {
23 class Client;
24 class World;
25 class VisualObject;
26 
29 {
30  ParseSimulableParams() = default;
31 
32  bool init_pose_mandatory = true;
33 };
34 
39 class Simulable
40 {
41  public:
42  using Ptr = std::shared_ptr<Simulable>;
43 
44  Simulable(World* parent) : simulable_parent_(parent) {}
45 
48  virtual void simul_pre_timestep(const TSimulContext& context);
49 
55  virtual void simul_post_timestep(const TSimulContext& context);
56 
60  virtual void apply_force(
61  const mrpt::math::TVector2D& force,
62  const mrpt::math::TPoint2D& applyPoint = mrpt::math::TPoint2D(0, 0));
63 
64  virtual VisualObject* meAsVisualObject() { return nullptr; }
65 
69  mrpt::math::TTwist2D getRefVelocityLocal() const;
70 
72  [[deprecated("Use getRefVelocityLocal() instead")]] mrpt::math::TTwist2D getVelocityLocal()
73  const
74  {
75  return getRefVelocityLocal();
76  }
77 
81  mrpt::math::TTwist2D getRefVelocityGlobal() const;
82 
85  mrpt::math::TTwist2D getComVelocityGlobal() const;
86 
89  mrpt::math::TPose3D getPose() const;
90 
95  virtual mrpt::math::TPose3D getRelativePose() const { return getPose(); }
96 
98  mrpt::math::TPose3D getPoseNoLock() const;
99 
104  mrpt::math::TVector3D getLinearAcceleration() const;
105 
108  void setPose(const mrpt::math::TPose3D& p, bool notifyChange = true) const;
109 
112  virtual void setRelativePose(const mrpt::math::TPose3D& p) { setPose(p); }
113 
119  void setRefVelocityLocal(const mrpt::math::TTwist2D& dq);
120 
122  mrpt::poses::CPose2D getCPose2D() const;
123 
125  mrpt::poses::CPose3D getCPose3D() const;
126 
128  const std::string& getName() const { return name_; }
129 
131  void setName(const std::string& s) { name_ = s; }
132 
134  bool isInCollision() const;
135 
139  bool hadCollision() const;
140 
143 
144  virtual void registerOnServer(mvsim::Client& c);
145 
146  const b2Body* b2d_body() const { return b2dBody_; }
147  b2Body* b2d_body() { return b2dBody_; }
148 
149  World* getSimulableWorldObject() { return simulable_parent_; }
150  const World* getSimulableWorldObject() const { return simulable_parent_; }
151 
152  virtual void freeOpenGLResources() {}
153 
158  virtual std::optional<float> getElevationAt(
159  [[maybe_unused]] const mrpt::math::TPoint2D& worldXY) const
160  {
161  return std::nullopt;
162  }
163 
164  protected:
166  std::string name_;
167 
173  b2Body* b2dBody_ = nullptr;
174 
175  bool parseSimulable(const JointXMLnode<>& node, const ParseSimulableParams& p = {});
176 
177  void internalHandlePublish(const TSimulContext& context);
178 
181  virtual void notifySimulableSetPose([[maybe_unused]] const mrpt::math::TPose3D& newPose)
182  {
183  // Default: do nothing
184  }
185 
186  private:
187  World* simulable_parent_ = nullptr;
188 
190  mutable std::shared_mutex q_mtx_;
191 
193  mrpt::math::TPose3D q_ = mrpt::math::TPose3D::Identity();
194 
196  mrpt::math::TTwist2D dq_{0, 0, 0};
197 
199  mrpt::math::TTwist2D dq_com_{0, 0, 0};
200 
202  mrpt::math::TVector3D ddq_lin_{0, 0, 0};
203 
204  mrpt::math::TPose3D former_q_;
205  mrpt::math::TTwist2D former_dq_{0, 0, 0};
206 
207  // ============ ANIMATION VARIABLES ============
209  mrpt::math::TPose3D initial_q_ = mrpt::math::TPose3D::Identity();
210 
211  std::optional<mrpt::poses::CPose3DInterpolator> anim_keyframes_path_;
212 
213  // ============ END OF ANIMATION VARIABLES ============
214 
216  bool isInCollision_ = false;
217 
222  bool hadCollisionFlag_ = false;
223 
225  std::string publishPoseTopic_;
226 
227  std::string publishRelativePoseTopic_;
228  std::vector<std::string> publishRelativePoseOfOtherObjects_;
229 
230  double publishPosePeriod_ = 100e-3;
231  double publishPoseLastTime_ = 0;
232 };
233 } // namespace mvsim
Definition: Client.h:49
Definition: JointXMLnode.h:20
Definition: Simulable.h:40
virtual void apply_force(const mrpt::math::TVector2D &force, const mrpt::math::TPoint2D &applyPoint=mrpt::math::TPoint2D(0, 0))
virtual mrpt::math::TPose3D getRelativePose() const
Definition: Simulable.h:95
mrpt::poses::CPose2D getCPose2D() const
Alternative to getPose()
void setName(const std::string &s)
Definition: Simulable.h:131
virtual void simul_pre_timestep(const TSimulContext &context)
mrpt::math::TTwist2D getRefVelocityLocal() const
virtual void setRelativePose(const mrpt::math::TPose3D &p)
Definition: Simulable.h:112
std::string name_
Definition: Simulable.h:166
mrpt::math::TPose3D getPose() const
virtual void simul_post_timestep(const TSimulContext &context)
mrpt::math::TTwist2D getRefVelocityGlobal() const
mrpt::math::TVector3D getLinearAcceleration() const
void setPose(const mrpt::math::TPose3D &p, bool notifyChange=true) const
const std::string & getName() const
Definition: Simulable.h:128
void setRefVelocityLocal(const mrpt::math::TTwist2D &dq)
mrpt::math::TTwist2D getVelocityLocal() const
Definition: Simulable.h:72
mrpt::poses::CPose3D getCPose3D() const
Alternative to getPose()
mrpt::math::TPose3D getPoseNoLock() const
No thread-safe version. Used internally only.
mrpt::math::TTwist2D getComVelocityGlobal() const
bool hadCollision() const
void resetCollisionFlag()
bool isInCollision() const
virtual std::optional< float > getElevationAt([[maybe_unused]] const mrpt::math::TPoint2D &worldXY) const
Definition: Simulable.h:158
Definition: VisualObject.h:36
Definition: World.h:132
Definition: Simulable.h:29
Definition: basic_types.h:58