MIRA
CollisionTest.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 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  * Contact: info@mira-project.org
9  *
10  * Commercial Usage:
11  * Licensees holding valid commercial licenses may use this file in
12  * accordance with the commercial license agreement provided with the
13  * software or, alternatively, in accordance with the terms contained in
14  * a written agreement between you and MLAB or NICR.
15  *
16  * GNU General Public License Usage:
17  * Alternatively, this file may be used under the terms of the GNU
18  * General Public License version 3.0 as published by the Free Software
19  * Foundation and appearing in the file LICENSE.GPL3 included in the
20  * packaging of this file. Please review the following information to
21  * ensure the GNU General Public License version 3.0 requirements will be
22  * met: http://www.gnu.org/copyleft/gpl.html.
23  * Alternatively you may (at your option) use any later version of the GNU
24  * General Public License if such license has been publicly approved by
25  * MLAB and NICR (or its successors, if any).
26  *
27  * IN NO EVENT SHALL "MLAB" OR "NICR" BE LIABLE TO ANY PARTY FOR DIRECT,
28  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
29  * THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF "MLAB" OR
30  * "NICR" HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * "MLAB" AND "NICR" SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
33  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
34  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
35  * ON AN "AS IS" BASIS, AND "MLAB" AND "NICR" HAVE NO OBLIGATION TO
36  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS OR MODIFICATIONS.
37  */
38 
47 #ifndef _MIRA_COLLISIONTEST_H_
48 #define _MIRA_COLLISIONTEST_H_
49 
50 #include <boost/optional.hpp>
51 
53 #include <image/Img.h>
54 #include <math/Angle.h>
55 #include <utils/Path.h>
56 #include <geometry/Point.h>
57 
58 #include <model/Footprint.h>
59 
60 namespace mira { namespace model {
61 
63 
82 {
83 public:
84 
85  CollisionTest(float cellSize = 0.02f, uint32 segments=360)
86  {
88  mCellSize = cellSize;
89  mSegments = segments;
90  }
91 
93  mSegments(other.mSegments),
94  mShapeLUTBB(other.mShapeLUTBB),
98  {
99  mShapeLUT.reserve(other.mShapeLUT.size());
100  std::transform(other.mShapeLUT.begin(), other.mShapeLUT.end(),
101  std::back_inserter(mShapeLUT),
102  std::mem_fn(&Img8U1::clone));
103  }
104 
105  template<typename Reflector>
106  void reflect(Reflector& r)
107  {
108  r.member("CellSize", mCellSize,
109  "Resolution of the collision map [m]", 0.02f);
110  r.member("Segments", mSegments,
111  "Size of the collision LUT (360 deg is divided in #segments)",
112  360);
113  r.property("ObstacleThreshold", mObstacleThreshold,
114  "Occupancy grid values above this threshold are counted as obstacles",
115  140, PropertyHints::limits(0, 255) | PropertyHints::step(1));
116  }
117 
119  {
120  mCellSize = other.mCellSize;
121  mSegments = other.mSegments;
122  mShapeLUTBB = other.mShapeLUTBB;
123 
124  mShapeLUT.clear();
125  mShapeLUT.reserve(other.mShapeLUT.size());
126  std::transform(other.mShapeLUT.begin(), other.mShapeLUT.end(),
127  std::back_inserter(mShapeLUT),
128  std::mem_fn(&Img8U1::clone));
129 
132  mSegmentStep = other.mSegmentStep;
133 
134  return *this;
135  }
136 
146  bool testCollision(const Img8U1& map, const Point2i& p, float phi) const;
147 
160  float distanceToObstacle(const Img32F1& distanceMap, const Point2i& p, float phi,
161  Point2i* oClosestModelPoint = NULL) const;
162 
177  static boost::optional<Point2i> obstaclePosition(
178  const Img32F1& distanceMap, const Img<int32>& labelMap,
179  const Point2i& closestModelPoint,
180  std::map<int, Point2i>* ioCache = NULL);
181 
192  void distanceTransform(const Img8U1& map, Img32F1& oDistanceMap) const;
193 
203  void distanceTransform(const Img8U1& map, Img32F1& oDistanceMap,
204  Img<int32>& oLabelMap) const;
205 
219  void distanceTransformAndClearShape(const Img8U1& map, Img32F1& oDistanceMap,
220  const Point2i& p, float phi) const;
221 
233  void distanceTransformAndClearShape(const Img8U1& map, Img32F1& oDistanceMap,
234  Img<int32>& oLabelMap,
235  const Point2i& p, float phi) const;
236 
241  const Img8U1& getShape(float phi) const;
242 
246  const Rect2i& getShapeBB(float phi) const;
247 
254  void initialize(const Footprint& footprint);
255 
263  void initialize(const Footprint& footprint, float cellSize);
264 
277  void initialize(const Footprint& footprint, float cellSize, uint32 segments);
278 
287  void clearShape(Img8U1& map, const Point2i& p, float phi,
288  uint8 freeValue = 0) const;
289 
304  void decayShape(Img8U1& map, const Point2i& p, float phi,
305  float decayRate, float neutral = 127.f,
306  bool decayObstacles = true, bool decayFreespace = false,
307  bool invertShape = false) const;
308 
309 
310 protected:
311 
312  int calculateSegment(float phi) const;
313  std::pair<Point2i, Rect2i> calculateOffsetAndOverlap(const Size2i& map,
314  const Point2i& p,
315  float phi) const;
316 
317 protected:
318 
319  float mCellSize;
321 
322  std::vector<Img8U1> mShapeLUT;
323  std::vector<Rect2i> mShapeLUTBB;
324 
327 
330 
333 };
334 
336 
337 }}
338 
339 #endif
CollisionTest & operator=(const CollisionTest &other)
Definition: CollisionTest.h:118
float distanceToObstacle(const Img32F1 &distanceMap, const Point2i &p, float phi, Point2i *oClosestModelPoint=NULL) const
Calculates the distance of the enclosed footprint to the nearest obstacle in the given map...
uint32 mSegments
Size of the collision LUT (360° is divided in #segments)
Definition: CollisionTest.h:320
#define MIRA_INITIALIZE_THIS
uint32_t uint32
std::pair< Point2i, Rect2i > calculateOffsetAndOverlap(const Size2i &map, const Point2i &p, float phi) const
void distanceTransform(const Img8U1 &map, Img32F1 &oDistanceMap) const
Calculates a distance transformation on the given map.
static boost::optional< Point2i > obstaclePosition(const Img32F1 &distanceMap, const Img< int32 > &labelMap, const Point2i &closestModelPoint, std::map< int, Point2i > *ioCache=NULL)
Determine the position of the obstacle.
std::vector< Img8U1 > mShapeLUT
Look up table for faster collision checking.
Definition: CollisionTest.h:322
Class that provides several methods for running collision tests of a rigid models footprint with imag...
Definition: CollisionTest.h:81
CollisionTest(float cellSize=0.02f, uint32 segments=360)
Definition: CollisionTest.h:85
int calculateSegment(float phi) const
float mCellSize
Resolution of the collision map [m].
Definition: CollisionTest.h:319
PropertyHint limits(const T &min, const T &max)
void initialize(const Footprint &footprint)
Initialize collision testing with the given footprint.
CollisionTest(const CollisionTest &other)
Definition: CollisionTest.h:92
uint8_t uint8
Representation of a footprint of a rigid model (part).
int mObstacleThreshold
Occupancy grid values above this threshold are counted as obstacles.
Definition: CollisionTest.h:329
void clearShape(Img8U1 &map, const Point2i &p, float phi, uint8 freeValue=0) const
Clears the map region that is covered by the rotated footprint at position p in the given map...
void distanceTransformAndClearShape(const Img8U1 &map, Img32F1 &oDistanceMap, const Point2i &p, float phi) const
Calculates a distance transformation on the given map and clears the rotated footprint at position p ...
const Rect2i & getShapeBB(float phi) const
Return the bounding box for the shape for phi.
std::vector< Rect2i > mShapeLUTBB
Bounding box for each precalculated shape.
Definition: CollisionTest.h:323
float mSegmentStep
Denotes a step in rad between each LUT entry (2pi / mSegments)
Definition: CollisionTest.h:332
PropertyHint step(const T &step)
void reflect(Reflector &r)
Definition: CollisionTest.h:106
Img< T, TChannels > clone() const
const Img8U1 & getShape(float phi) const
Return the entry of the LUT that is closest to the representation of the enclosed footprint rotated a...
Represents the footprint of a rigid model that is the 2D projection of the 3D model onto the xy-plane...
Definition: Footprint.h:70
int mShapeDimension
Each LUT entry has this dimension (e.g. is a dim x dim image)
Definition: CollisionTest.h:326
void decayShape(Img8U1 &map, const Point2i &p, float phi, float decayRate, float neutral=127.f, bool decayObstacles=true, bool decayFreespace=false, bool invertShape=false) const
Decays the map region that is covered by the rotated footprint at position p in the given map...
bool testCollision(const Img8U1 &map, const Point2i &p, float phi) const
Tests collision of the enclosed footprint with a given map for rotation phi.