MIRA
PolygonTools.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) by
3  * MetraLabs GmbH (MLAB), GERMANY
4  * and
5  * Neuroinformatics and Cognitive Robotics Labs (NICR) at TU Ilmenau, GERMANY
6  * All rights reserved.
7  *
8  * Redistribution and modification of this code is strictly prohibited.
9  *
10  * IN NO EVENT SHALL "MLAB" OR "NICR" BE LIABLE TO ANY PARTY FOR DIRECT,
11  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
12  * THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF "MLAB" OR
13  * "NICR" HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14  *
15  * "MLAB" AND "NICR" SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
16  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
17  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
18  * ON AN "AS IS" BASIS, AND "MLAB" AND "NICR" HAVE NO OBLIGATION TO
19  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS OR MODIFICATIONS.
20  */
21 
30 #ifndef _MIRA_TOOLBOXES_MAPPING_INCLUDE_MAPPING_POYLGONTOOLS_H_
31 #define _MIRA_TOOLBOXES_MAPPING_INCLUDE_MAPPING_POYLGONTOOLS_H_
32 
33 #include <geometry/RasterPolygon.h>
34 #include <maps/GridMap.h>
35 #include <transform/Pose.h>
36 
37 namespace mira { namespace maps { namespace polygon {
38 
40 
47 template<class Celltype>
49 {
50 public:
51  TransformToMapFunctor(const float rotation, const Point2f& translation,
52  const maps::GridMap<Celltype>& map)
53  : mCos_phi(std::cos(rotation)), mSin_phi(std::sin(rotation)), mTranslation(translation), mMap(map)
54  {}
55 
57  : TransformToMapFunctor(pose.phi(), pose.t, map)
58  {}
59 
60  inline Point2f operator()(const Point2f& p) const
61  {
62  return mMap.world2mapf(shiftPoint(rotatePoint(p)));
63  }
64 
65 private:
66  inline Point2f rotatePoint(const Point2f& p) const
67  {
68  return Point2f(p.x() * mCos_phi - p.y() * mSin_phi, p.x() * mSin_phi + p.y() * mCos_phi);
69  }
70 
71  inline Point2f shiftPoint(Point2f&& p) const
72  {
73  return p + mTranslation;
74  }
75 
76 private:
77  float mCos_phi;
78  float mSin_phi;
79  Point2f mTranslation;
80  const maps::GridMap<Celltype>& mMap;
81 };
82 
91 template<class Celltype>
92 Celltype getMaximum(const maps::GridMap<Celltype>& map, const Polygon2f& polygon, const Pose2& polygonPose,
93  uint8 precision = 1)
94 {
95  auto maximum = std::numeric_limits<Celltype>::lowest();
96 
97  rasterPolygon(polygon, Rect2i(0, 0, map.size().width(), map.size().height()),
98  TransformToMapFunctor<Celltype>(polygonPose, map),
99  [&](int x, int y) {
100  maximum = std::max(map(x, y), maximum);
101  return false;
102  },
103  precision);
104  return maximum;
105 }
106 
115 template<class Celltype>
116 Celltype getMaximum(const maps::GridMap<Celltype>& map, const std::vector<Polygon2f>& polygons,
117  const Pose2& polygonPose, uint8 precision = 1)
118 {
119  auto maximum = std::numeric_limits<Celltype>::lowest();
120  for (const auto& polygon : polygons) {
121  auto currentMax = getMaximum(map, polygon, polygonPose, precision);
122  maximum = std::max(maximum, currentMax);
123  }
124  return maximum;
125 }
126 
135 template<class Celltype>
136 void fill(maps::GridMap<Celltype>& map, const Polygon2f& polygon, const Pose2& polygonPose,
137  const Celltype fillValue, uint8 precision = 1)
138 {
139  rasterPolygon(polygon, Rect2i(0, 0, map.size().width(), map.size().height()),
140  TransformToMapFunctor<Celltype>(polygonPose, map),
141  [&](int x, int y) {
142  map(x, y) = fillValue;
143  return false;
144  },
145  precision);
146 }
147 
156 template<class Celltype>
157 void fill(maps::GridMap<Celltype>& map, const std::vector<Polygon2f>& polygons, const Pose2& polygonPose,
158  const Celltype fillValue, uint8 precision = 1)
159 {
160  for (const auto& polygon : polygons) {
161  fill(map, polygon, polygonPose, fillValue, precision);
162  }
163 }
164 
165 // Not tested functions
166 //
167 // template<class Celltype, class TerminationCondition>
168 // Celltype getMinimum(const maps::GridMap<Celltype>& map, const Polygon2f& polygon, const Pose2& polygonPose,
169 // TerminationCondition&& terminate, uint8 precision = 1)
170 //{
171 //
172 // auto minimum = std::numeric_limits<Celltype>::max();
173 //
174 // rasterPolygon(polygon, Rect2i(0, 0, map.size().width(), map.size().height()),
175 // TransformToMapFunctor<Celltype>(polygonPose, map),
176 // [&](int x, int y) {
177 // minimum = std::min(minimum, map(x, y));
178 // return terminate(minimum);
179 // },
180 // precision);
181 // return minimum;
182 //}
183 //
184 //template<class Celltype>
185 //Celltype getMinimum(const maps::GridMap<Celltype>& map, const std::vector<Polygon2f>& polygons,
186 // const Pose2& polygonPose, Celltype lowerBound, uint8 precision = 1)
187 //{
188 // auto minimum = std::numeric_limits<Celltype>::max();
189 // for (const auto& polygon : polygons) {
190 // auto polygonMin =
191 // getMinimum(map, polygon, polygonPose,
192 // [=](const Celltype currentMin) { return currentMin <= lowerBound; }, precision);
193 //
194 // if (polygonMin <= lowerBound)
195 // return polygonMin;
196 //
197 // minimum = std::min(polygonMin, minimum);
198 // }
199 // return minimum;
200 //}
201 //
202 // GridMap<float> distanceTransformAndClearPolygon(const OccupancyGrid& map, const Polygon2f& polygon,
203 // const Pose2& polygonPose, Img32F1&& buffer,
204 // uint8 obstacleThreshold = 127)
205 //{
206 // auto res = GridMap<float>(std::move(buffer), map.getCellSize(), map.getOffset());
207 // // buffer is to large or to small? no problem
208 // res.clip(map.getMapRegion(), 0);
209 //
210 // OccupancyGrid clearedMap = map.clone();
211 // cv::threshold(map, clearedMap, obstacleThreshold, 255, cv::THRESH_BINARY_INV);
212 // // Clear the map on the actual position with the shape.
213 // fill(clearedMap, polygon, polygonPose, (uint8)255);
214 //
215 //#if CV_MAJOR_VERSION >= 3
216 // cv::distanceTransform(clearedMap, res, cv::DIST_L2, 5);
217 //#else
218 // cv::distanceTransform(clearedMap, res, CV_DIST_L2, 5);
219 //#endif
220 //
221 // return res;
222 //}
223 //
224 // GridMap<float> distanceTransformAndClearPolygon(const OccupancyGrid& map,
225 // const std::vector<Polygon2f>& polygons,
226 // const Pose2& polygonPose, Img32F1&& buffer,
227 // uint8 obstacleThreshold = 127)
228 //{
229 // auto res = GridMap<float>(std::move(buffer), map.getCellSize(), map.getOffset());
230 // // buffer is to large or to small? no problem
231 // res.clip(map.getMapRegion(), 0);
232 //
233 // OccupancyGrid clearedMap = map.clone();
234 // cv::threshold(map, clearedMap, obstacleThreshold, 255, cv::THRESH_BINARY_INV);
235 // // Clear the map on the actual position with the shape.
236 // fill(clearedMap, polygons, polygonPose, (uint8)255);
237 //
238 //#if CV_MAJOR_VERSION >= 3
239 // cv::distanceTransform(clearedMap, res, cv::DIST_L2, 5);
240 //#else
241 // cv::distanceTransform(clearedMap, res, CV_DIST_L2, 5);
242 //#endif
243 //
244 // return res;
245 //}
246 //
247 // GridMap<float> distanceTransformAndClearPolygon(const OccupancyGrid& map, const Polygon2f& polygon,
248 // const Pose2 polygonPose, uint8 obstacleThreshold = 127)
249 //{
250 // return distanceTransformAndClearPolygon(map, polygon, polygonPose, Img32F1{}, obstacleThreshold);
251 //}
252 //
253 // GridMap<float> distanceTransformAndClearPolygon(const OccupancyGrid& map,
254 // const std::vector<Polygon2f>& polygons,
255 // const Pose2 polygonPose, uint8 obstacleThreshold = 127)
256 //{
257 // return distanceTransformAndClearPolygon(map, polygons, polygonPose, Img32F1{}, obstacleThreshold);
258 //}
259 
261 
262 }}} // namespace mira::maps::polygon
263 
264 #endif
void rasterPolygon(const Polygon2f &polygon, const Rect2i &region, TransformationInRegion &&transformation, Visitor &&visitor)
Rect< int, 2 > Rect2i
PropertyHint maximum(const T &max)
Size2i size() const
void fill(maps::GridMap< Celltype > &map, const Polygon2f &polygon, const Pose2 &polygonPose, const Celltype fillValue, uint8 precision=1)
Function for filling cells that overlap with the polygon at the provided pose.
Definition: PolygonTools.h:136
Description.
STL namespace.
Celltype getMaximum(const maps::GridMap< Celltype > &map, const Polygon2f &polygon, const Pose2 &polygonPose, uint8 precision=1)
Function for finding the maximum value of cells that have an overlap with the rasterization of a poly...
Definition: PolygonTools.h:92
TransformToMapFunctor(const Pose2 &pose, const maps::GridMap< Celltype > &map)
Definition: PolygonTools.h:56
Point2f operator()(const Point2f &p) const
Definition: PolygonTools.h:60
uint8_t uint8
Functor to rotate, shift and transform a point to map coordinates This functor stores cos and sin of ...
Definition: PolygonTools.h:48
PropertyHint precision(int p)
Point< float, 2 > Point2f
TransformToMapFunctor(const float rotation, const Point2f &translation, const maps::GridMap< Celltype > &map)
Definition: PolygonTools.h:51
Point2f world2mapf(const Point2f &p) const
Convert a given point to map coordinates.
Definition: GridMap.h:232
boost::geometry::model::ring< Point2f > Polygon2f