MRPT  2.0.1
CPTG_RobotShape_Polygonal.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "nav-precomp.h" // Precomp header
11 
16 
17 using namespace mrpt::nav;
18 
22  const mrpt::math::CPolygon& robotShape)
23 {
24  ASSERT_ABOVEEQ_(robotShape.size(), 3u);
25  m_robotShape = robotShape;
26 
27  m_robotMaxRadius = .0; // Default minimum
28  for (const auto& v : m_robotShape)
30 
32 }
33 
35 {
36  m_robotShape.clear();
37  m_robotShape.AddVertex(-0.15, 0.15);
38  m_robotShape.AddVertex(0.2, 0.1);
39  m_robotShape.AddVertex(0.2, -0.1);
40  m_robotShape.AddVertex(-0.15, -0.15);
41 }
42 
44  const mrpt::config::CConfigFileBase& cfg, const std::string& sSection)
45 {
46  bool any_pt = false;
47  const double BADNUM = std::numeric_limits<double>::max();
48 
49  for (unsigned int nPt = 0;; ++nPt)
50  {
51  const std::string sPtx = mrpt::format("shape_x%u", nPt);
52  const std::string sPty = mrpt::format("shape_y%u", nPt);
53 
54  const double ptx = cfg.read_double(sSection, sPtx, BADNUM, false);
55  const double pty = cfg.read_double(sSection, sPty, BADNUM, false);
56  if (ptx == BADNUM && pty == BADNUM) break;
57  ASSERTMSG_(
58  (ptx != BADNUM && pty != BADNUM),
59  "Error: mismatch between number of pts in {x,y} defining robot "
60  "shape");
61 
62  if (!any_pt)
63  {
64  m_robotShape.clear();
65  any_pt = true;
66  }
67 
68  m_robotShape.AddVertex(ptx, pty);
69  }
70 
71  if (any_pt) internal_processNewRobotShape();
72 }
73 
75  mrpt::config::CConfigFileBase& cfg, const std::string& sSection) const
76 {
77  const int WN = 25, WV = 30;
78 
79  for (unsigned int i = 0; i < m_robotShape.size(); i++)
80  {
81  const std::string sPtx = mrpt::format("shape_x%u", i);
82  const std::string sPty = mrpt::format("shape_y%u", i);
83 
84  cfg.write(
85  sSection, sPtx, m_robotShape[i].x, WN, WV,
86  "Robot polygonal shape, `x` [m].");
87  cfg.write(
88  sSection, sPty, m_robotShape[i].y, WN, WV,
89  "Robot polygonal shape, `y` [m].");
90  }
91 }
92 
94  mrpt::opengl::CSetOfLines& gl_shape,
95  const mrpt::poses::CPose2D& origin) const
96 {
97  const int N = m_robotShape.size();
98  if (N >= 2)
99  {
100  // Transform coordinates:
101  mrpt::math::CVectorDouble shap_x(N), shap_y(N), shap_z(N);
102  for (int i = 0; i < N; i++)
103  {
104  origin.composePoint(
105  m_robotShape[i].x, m_robotShape[i].y, 0, shap_x[i], shap_y[i],
106  shap_z[i]);
107  }
108 
109  gl_shape.appendLine(
110  shap_x[0], shap_y[0], shap_z[0], shap_x[1], shap_y[1], shap_z[1]);
111  for (int i = 0; i <= shap_x.size(); i++)
112  {
113  const int idx = i % shap_x.size();
114  gl_shape.appendLineStrip(shap_x[idx], shap_y[idx], shap_z[idx]);
115  }
116  }
117 }
118 
121 {
122  uint8_t version;
123  in >> version;
124 
125  switch (version)
126  {
127  case 0:
128  in >> m_robotShape;
129  break;
130  default:
132  }
133 }
134 
137 {
138  uint8_t version = 0;
139  out << version;
140 
141  out << m_robotShape;
142 }
143 
145 {
146  return m_robotMaxRadius;
147 }
148 
150  const double x, const double y) const
151 {
153 }
154 
156  const double ox, const double oy) const
157 {
158  // Approximated computation, valid for relatively distant objects, which
159  // is where clearance is useful.
160 
161  if (isPointInsideRobotShape(ox, oy)) return .0;
162 
163  double d = mrpt::hypot_fast(ox, oy) - m_robotMaxRadius;
164 
165  // if d<=0, we know from the isPointInsideRobotShape() above that
166  // it's a false positive: enforce a minimum "fake" clearance:
168 
169  return d;
170 }
void appendLineStrip(float x, float y, float z)
Appends a line whose starting point is the end point of the last line (similar to OpenGL&#39;s GL_LINE_ST...
Definition: CSetOfLines.h:91
std::string std::string format(std::string_view fmt, ARGS &&... args)
Definition: format.h:26
void loadDefaultParams() override
Loads a set of default parameters; provided exclusively for the PTG-configurator tool.
A wrapper of a TPolygon2D class, implementing CSerializable.
Definition: CPolygon.h:19
bool contains(const TPoint2D &point) const
Check whether a point is inside (or within geometryEpsilon of a polygon edge).
Definition: TPolygon2D.cpp:69
void composePoint(double lx, double ly, double &gx, double &gy) const
An alternative, slightly more efficient way of doing with G and L being 2D points and P this 2D pose...
Definition: CPose2D.cpp:199
void internal_shape_saveToStream(mrpt::serialization::CArchive &out) const
void add_robotShape_to_setOfLines(mrpt::opengl::CSetOfLines &gl_shape, const mrpt::poses::CPose2D &origin=mrpt::poses::CPose2D()) const override
Auxiliary function for rendering.
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
Definition: exceptions.h:97
void loadShapeFromConfigFile(const mrpt::config::CConfigFileBase &source, const std::string &section)
This class allows loading and storing values and vectors of different types from a configuration text...
double evalClearanceToRobotShape(const double ox, const double oy) const override
Evals the clearance from an obstacle (ox,oy) in coordinates relative to the robot center...
void appendLine(const mrpt::math::TSegment3D &sgm)
Appends a line to the set.
Definition: CSetOfLines.h:71
virtual void internal_processNewRobotShape()=0
Will be called whenever the robot shape is set / updated.
void internal_shape_loadFromStream(mrpt::serialization::CArchive &in)
#define ASSERTMSG_(f, __ERROR_MSG)
Defines an assertion mechanism.
Definition: exceptions.h:108
double read_double(const std::string &section, const std::string &name, double defaultValue, bool failIfNotFound=false) const
T hypot_fast(const T x, const T y)
Faster version of std::hypot(), to use when overflow is not an issue and we prefer fast code...
#define ASSERT_ABOVEEQ_(__A, __B)
Definition: exceptions.h:167
void write(const std::string &section, const std::string &name, enum_t value, const int name_padding_width=-1, const int value_padding_width=-1, const std::string &comment=std::string())
void keep_max(T &var, const K test_val)
If the second argument is above the first one, set the first argument to this higher value...
void setRobotShape(const mrpt::math::CPolygon &robotShape)
Robot shape must be set before initialization, either from ctor params or via this method...
Virtual base class for "archives": classes abstracting I/O streams.
Definition: CArchive.h:54
A class used to store a 2D pose, including the 2D coordinate point and a heading (phi) angle...
Definition: CPose2D.h:39
mrpt::vision::TStereoCalibResults out
void saveToConfigFile(mrpt::config::CConfigFileBase &cfg, const std::string &sSection) const override
This method saves the options to a ".ini"-like file or memory-stored string list. ...
bool isPointInsideRobotShape(const double x, const double y) const override
Returns true if the point lies within the robot shape.
A set of independent lines (or segments), one line with its own start and end positions (X...
Definition: CSetOfLines.h:32
void AddVertex(double x, double y)
Add a new vertex to polygon.
Definition: CPolygon.h:28
double getMaxRobotRadius() const override
Returns an approximation of the robot radius.



Page generated by Doxygen 1.8.14 for MRPT 2.0.1 Git: 0fef1a6d7 Fri Apr 3 23:00:21 2020 +0200 at vie abr 3 23:20:28 CEST 2020