MVSim
Lightweight simulator for 2.5D vehicles and robots
Shape2p5.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 <mrpt/containers/CDynamicGrid.h>
13 #include <mrpt/math/TPoint3D.h>
14 #include <mrpt/math/TPolygon2D.h>
15 #include <mrpt/opengl/TTriangle.h>
16 
17 #include <cstdint>
18 #include <optional>
19 
20 namespace mvsim
21 {
30 class Shape2p5
31 {
32  public:
33  Shape2p5() = default;
34 
35  void buildInit(
36  const mrpt::math::TPoint2Df& bbMin, const mrpt::math::TPoint2Df& bbMax, int numCells = 100);
37  void buildAddPoint(const mrpt::math::TPoint3Df& pt);
38  void buildAddTriangle(const mrpt::opengl::TTriangle& t);
39 
40  const mrpt::math::TPolygon2D& getContour() const;
41 
42  double volume() const;
43 
44  void mergeWith(const Shape2p5& s);
45 
46  void setShapeManual(const mrpt::math::TPolygon2D& contour, const float zMin, const float zMax);
47 
48  float zMin() const { return zMin_; }
49  float zMax() const { return zMax_; }
50 
51  void clipZMin(float v);
52  void clipZMax(float v);
53 
54  std::string asString() const;
55 
56  private:
57  mutable std::optional<mrpt::math::TPolygon2D> contour_;
58  mutable float zMin_ = 0, zMax_ = 0;
59 
60  class SimpleOccGrid : public mrpt::containers::CDynamicGrid<uint8_t>
61  {
62  public:
63  template <typename... Args>
64  SimpleOccGrid(Args&&... args)
65  : mrpt::containers::CDynamicGrid<uint8_t>(std::forward<Args>(args)...)
66  {
67  }
68 
69  // Used to debug (save grid to txt file)
70  float cell2float(const uint8_t& v) const override { return v; }
71  };
72 
73  mutable std::optional<SimpleOccGrid> grid_;
74 
76  void computeShape() const;
77 
78  void internalGridFilterSpurious() const;
79  void internalGridFloodFill() const;
80  mrpt::math::TPolygon2D internalGridContour() const;
81  mrpt::math::TPolygon2D internalPrunePolygon(const mrpt::math::TPolygon2D& poly) const;
82 
83  struct RemovalCandidate
84  {
85  double loss = 0;
86  mrpt::math::TPolygon2D next;
87  };
88 
89  std::optional<RemovalCandidate> lossOfRemovingVertex(
90  size_t i, const mrpt::math::TPolygon2D& p, bool allowApproxEdges) const;
91 
92  void debugSaveGridTo3DSceneFile(
93  const mrpt::math::TPolygon2D& rawGridContour, const std::string& debugStr = {}) const;
94 };
95 
96 } // namespace mvsim
Definition: Shape2p5.h:31