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>
60 
61 namespace mira { namespace model {
62 
64 
83 {
84 public:
85 
86  CollisionTest(float cellSize = 0.02f, uint32 segments=360)
87  {
89  mCellSize = cellSize;
90  mSegments = segments;
91  }
92 
93  CollisionTest(const CollisionTest& other) : mCellSize(other.mCellSize),
94  mSegments(other.mSegments),
95  mShapeLUTBB(other.mShapeLUTBB),
96  mShapeDimension(other.mShapeDimension),
97  mObstacleThreshold(other.mObstacleThreshold),
98  mSegmentStep(other.mSegmentStep)
99  {
100  mShapeLUT.reserve(other.mShapeLUT.size());
101  std::transform(other.mShapeLUT.begin(), other.mShapeLUT.end(),
102  std::back_inserter(mShapeLUT),
103  std::mem_fn(&Img8U1::clone));
104  }
105 
106  template<typename Reflector>
107  void reflect(Reflector& r)
108  {
109  r.member("CellSize", mCellSize,
110  "Resolution of the collision map [m]", 0.02f);
111  r.member("Segments", mSegments,
112  "Size of the collision LUT (360 deg is divided in #segments)",
113  360);
114  r.property("ObstacleThreshold", mObstacleThreshold,
115  "Occupancy grid values above this threshold are counted as obstacles",
116  140, PropertyHints::limits(0, 255) | PropertyHints::step(1));
117  }
118 
120  {
121  mCellSize = other.mCellSize;
122  mSegments = other.mSegments;
123  mShapeLUTBB = other.mShapeLUTBB;
124 
125  mShapeLUT.clear();
126  mShapeLUT.reserve(other.mShapeLUT.size());
127  std::transform(other.mShapeLUT.begin(), other.mShapeLUT.end(),
128  std::back_inserter(mShapeLUT),
129  std::mem_fn(&Img8U1::clone));
130 
131  mShapeDimension = other.mShapeDimension;
132  mObstacleThreshold = other.mObstacleThreshold;
133  mSegmentStep = other.mSegmentStep;
134 
135  return *this;
136  }
137 
147  bool testCollision(const Img8U1& map, const Point2i& p, float phi) const;
148 
161  float distanceToObstacle(const Img32F1& distanceMap, const Point2i& p, float phi,
162  Point2i* oClosestModelPoint = NULL) const;
163 
178  static boost::optional<Point2i> obstaclePosition(
179  const Img32F1& distanceMap, const Img<int32>& labelMap,
180  const Point2i& closestModelPoint,
181  std::map<int, Point2i>* ioCache = NULL);
182 
193  void distanceTransform(const Img8U1& map, Img32F1& oDistanceMap) const;
194 
204  void distanceTransform(const Img8U1& map, Img32F1& oDistanceMap,
205  Img<int32>& oLabelMap) const;
206 
220  void distanceTransformAndClearShape(const Img8U1& map, Img32F1& oDistanceMap,
221  const Point2i& p, float phi) const;
222 
234  void distanceTransformAndClearShape(const Img8U1& map, Img32F1& oDistanceMap,
235  Img<int32>& oLabelMap,
236  const Point2i& p, float phi) const;
237 
242  const Img8U1& getShape(float phi) const;
243 
247  const Rect2i& getShapeBB(float phi) const;
248 
255  void initialize(const Footprint& footprint);
256 
264  void initialize(const Footprint& footprint, float cellSize);
265 
278  void initialize(const Footprint& footprint, float cellSize, uint32 segments);
279 
288  void clearShape(Img8U1& map, const Point2i& p, float phi,
289  uint8 freeValue = 0) const;
290 
305  void decayShape(Img8U1& map, const Point2i& p, float phi,
306  float decayRate, float neutral = 127.f,
307  bool decayObstacles = true, bool decayFreespace = false,
308  bool invertShape = false) const;
309 
310 
311 protected:
312 
313  int calculateSegment(float phi) const;
314  std::pair<Point2i, Rect2i> calculateOffsetAndOverlap(const Size2i& map,
315  const Point2i& p,
316  float phi) const;
317 
318 protected:
319 
320  float mCellSize;
321  uint32 mSegments;
322 
323  std::vector<Img8U1> mShapeLUT;
324  std::vector<Rect2i> mShapeLUTBB;
325 
328 
331 
334 };
335 
337 
338 }}
339 
340 #endif
CollisionTest & operator=(const CollisionTest &other)
Definition: CollisionTest.h:119
uint32 mSegments
Size of the collision LUT (360° is divided in #segments)
Definition: CollisionTest.h:321
#define MIRA_INITIALIZE_THIS
std::vector< Img8U1 > mShapeLUT
Look up table for faster collision checking.
Definition: CollisionTest.h:323
Class that provides several methods for running collision tests of a rigid models footprint with imag...
Definition: CollisionTest.h:82
CollisionTest(float cellSize=0.02f, uint32 segments=360)
Definition: CollisionTest.h:86
float mCellSize
Resolution of the collision map [m].
Definition: CollisionTest.h:320
PropertyHint limits(const T &min, const T &max)
CollisionTest(const CollisionTest &other)
Definition: CollisionTest.h:93
RigidModel export macro declaration.
Representation of a footprint of a rigid model (part).
int mObstacleThreshold
Occupancy grid values above this threshold are counted as obstacles.
Definition: CollisionTest.h:330
std::vector< Rect2i > mShapeLUTBB
Bounding box for each precalculated shape.
Definition: CollisionTest.h:324
#define MIRA_RIGID_MODEL_EXPORT
Definition: RigidModelExports.h:61
float mSegmentStep
Denotes a step in rad between each LUT entry (2pi / mSegments)
Definition: CollisionTest.h:333
PropertyHint step(const T &step)
void reflect(Reflector &r)
Definition: CollisionTest.h:107
Img< T, TChannels > clone() const
Represents the footprint of a rigid model that is the 2D projection of the 3D model onto the xy-plane...
Definition: Footprint.h:65
int mShapeDimension
Each LUT entry has this dimension (e.g. is a dim x dim image)
Definition: CollisionTest.h:327