Main MRPT website > C++ reference for MRPT 1.9.9
CPTG_RobotShape_Polygonal.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "nav-precomp.h" // Precomp header
11 
14 #include <mrpt/utils/CStream.h>
15 
16 using namespace mrpt::nav;
17 
19  : m_robotShape(), m_robotMaxRadius(.01)
20 {
21 }
24  const mrpt::math::CPolygon& robotShape)
25 {
26  ASSERT_ABOVEEQ_(robotShape.size(), 3u);
27  m_robotShape = robotShape;
28 
29  m_robotMaxRadius = .0; // Default minimum
30  for (const auto& v : m_robotShape)
32 
34 }
35 
37 {
38  m_robotShape.clear();
39  m_robotShape.AddVertex(-0.15, 0.15);
40  m_robotShape.AddVertex(0.2, 0.1);
41  m_robotShape.AddVertex(0.2, -0.1);
42  m_robotShape.AddVertex(-0.15, -0.15);
43 }
44 
46  const mrpt::utils::CConfigFileBase& cfg, const std::string& sSection)
47 {
48  bool any_pt = false;
49  const double BADNUM = std::numeric_limits<double>::max();
50 
51  for (unsigned int nPt = 0;; ++nPt)
52  {
53  const std::string sPtx = mrpt::format("shape_x%u", nPt);
54  const std::string sPty = mrpt::format("shape_y%u", nPt);
55 
56  const double ptx = cfg.read_double(sSection, sPtx, BADNUM, false);
57  const double pty = cfg.read_double(sSection, sPty, BADNUM, false);
58  if (ptx == BADNUM && pty == BADNUM) break;
59  ASSERTMSG_(
60  (ptx != BADNUM && pty != BADNUM),
61  "Error: mismatch between number of pts in {x,y} defining robot "
62  "shape");
63 
64  if (!any_pt)
65  {
66  m_robotShape.clear();
67  any_pt = true;
68  }
69 
70  m_robotShape.AddVertex(ptx, pty);
71  }
72 
73  if (any_pt) internal_processNewRobotShape();
74 }
75 
77  mrpt::utils::CConfigFileBase& cfg, const std::string& sSection) const
78 {
79  const int WN = 25, WV = 30;
80 
81  for (unsigned int i = 0; i < m_robotShape.size(); i++)
82  {
83  const std::string sPtx = mrpt::format("shape_x%u", i);
84  const std::string sPty = mrpt::format("shape_y%u", i);
85 
86  cfg.write(
87  sSection, sPtx, m_robotShape[i].x, WN, WV,
88  "Robot polygonal shape, `x` [m].");
89  cfg.write(
90  sSection, sPty, m_robotShape[i].y, WN, WV,
91  "Robot polygonal shape, `y` [m].");
92  }
93 }
94 
96  mrpt::opengl::CSetOfLines& gl_shape,
97  const mrpt::poses::CPose2D& origin) const
98 {
99  const int N = m_robotShape.size();
100  if (N >= 2)
101  {
102  // Transform coordinates:
103  mrpt::math::CVectorDouble shap_x(N), shap_y(N), shap_z(N);
104  for (int i = 0; i < N; i++)
105  {
106  origin.composePoint(
107  m_robotShape[i].x, m_robotShape[i].y, 0, shap_x[i], shap_y[i],
108  shap_z[i]);
109  }
110 
111  gl_shape.appendLine(
112  shap_x[0], shap_y[0], shap_z[0], shap_x[1], shap_y[1], shap_z[1]);
113  for (int i = 0; i <= shap_x.size(); i++)
114  {
115  const int idx = i % shap_x.size();
116  gl_shape.appendLineStrip(shap_x[idx], shap_y[idx], shap_z[idx]);
117  }
118  }
119 }
120 
123 {
124  uint8_t version;
125  in >> version;
126 
127  switch (version)
128  {
129  case 0:
130  in >> m_robotShape;
131  break;
132  default:
134  }
135 }
136 
138  mrpt::utils::CStream& out) const
139 {
140  uint8_t version = 0;
141  out << version;
142 
143  out << m_robotShape;
144 }
145 
147 {
148  return m_robotMaxRadius;
149 }
150 
152  const double x, const double y) const
153 {
155 }
156 
158  const double ox, const double oy) const
159 {
160  // Approximated computation, valid for relatively distant objects, which
161  // is where clearance is useful.
162 
163  if (isPointInsideRobotShape(ox, oy)) return .0;
164 
165  double d = mrpt::math::hypot_fast(ox, oy) - m_robotMaxRadius;
166 
167  // if d<=0, we know from the isPointInsideRobotShape() above that
168  // it's a false positive: enforce a minimum "fake" clearance:
170 
171  return d;
172 }
void internal_shape_saveToStream(mrpt::utils::CStream &out) const
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
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:93
void saveToConfigFile(mrpt::utils::CConfigFileBase &cfg, const std::string &sSection) const override
This method saves the options to a ".ini"-like file or memory-stored string list. ...
void loadDefaultParams() override
Loads a set of default parameters; provided exclusively for the PTG-configurator tool.
Column vector, like Eigen::MatrixX*, but automatically initialized to zeros since construction...
Definition: eigen_frwds.h:42
A wrapper of a TPolygon2D class, implementing CSerializable.
Definition: CPolygon.h:22
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:188
void add_robotShape_to_setOfLines(mrpt::opengl::CSetOfLines &gl_shape, const mrpt::poses::CPose2D &origin=mrpt::poses::CPose2D()) const override
Auxiliary function for rendering.
void loadShapeFromConfigFile(const mrpt::utils::CConfigFileBase &source, const std::string &section)
This class allows loading and storing values and vectors of different types from a configuration text...
unsigned char uint8_t
Definition: rptypes.h:41
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:41
virtual 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:72
virtual void internal_processNewRobotShape()=0
Will be called whenever the robot shape is set / updated.
void internal_shape_loadFromStream(mrpt::utils::CStream &in)
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
GLsizei const GLchar ** string
Definition: glext.h:4101
bool contains(const TPoint2D &point) const
Check whether a point is inside (or within geometryEpsilon of a polygon edge).
void setRobotShape(const mrpt::math::CPolygon &robotShape)
Robot shape must be set before initialization, either from ctor params or via this method...
const GLdouble * v
Definition: glext.h:3678
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())
#define ASSERT_ABOVEEQ_(__A, __B)
A class used to store a 2D pose, including the 2D coordinate point and a heading (phi) angle...
Definition: CPose2D.h:40
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...
GLuint in
Definition: glext.h:7274
GLenum GLint GLint y
Definition: glext.h:3538
bool isPointInsideRobotShape(const double x, const double y) const override
Returns true if the point lies within the robot shape.
GLenum GLint x
Definition: glext.h:3538
A set of independent lines (or segments), one line with its own start and end positions (X...
Definition: CSetOfLines.h:35
Lightweight 2D point.
void AddVertex(double x, double y)
Add a new vertex to polygon.
Definition: CPolygon.h:31
#define ASSERTMSG_(f, __ERROR_MSG)
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...
double getMaxRobotRadius() const override
Returns an approximation of the robot radius.



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019