grid_map.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Software License Agreement (BSD License) *
3  * Copyright (C) 2016 by Horatiu George Todoran <todorangrg@gmail.com> *
4  * *
5  * Redistribution and use in source and binary forms, with or without *
6  * modification, are permitted provided that the following conditions *
7  * are met: *
8  * *
9  * 1. Redistributions of source code must retain the above copyright *
10  * notice, this list of conditions and the following disclaimer. *
11  * 2. Redistributions in binary form must reproduce the above copyright *
12  * notice, this list of conditions and the following disclaimer in *
13  * the documentation and/or other materials provided with the *
14  * distribution. *
15  * 3. Neither the name of the copyright holder nor the names of its *
16  * contributors may be used to endorse or promote products derived *
17  * from this software without specific prior written permission. *
18  * *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT *
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS *
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE *
23  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, *
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, *
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; *
26  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER *
27  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT *
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY *
29  * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE *
30  * POSSIBILITY OF SUCH DAMAGE. *
31  ***************************************************************************/
32 
33 
34 #ifndef TUW_GRID_MAP_H
35 #define TUW_GRID_MAP_H
36 
37 #include <memory>
39 
40 namespace tuw
41 {
42 
43 template <class T>
44 class GridMap : public WorldScopedMaps
45 {
46 public:
47 
48  static const int8_t SPACE_NA = -1;
49  static const int8_t SPACE_FREE = 0;
50  static const int8_t SPACE_OCCUPIED = 100;
51  //special class member functions
52  GridMap ( )
53  : threshold_occupyied_(SPACE_OCCUPIED)
54  , threshold_unknown_(SPACE_OCCUPIED / 2)
55  , threshold_free_(SPACE_OCCUPIED / 2)
56  {
57  bool read_only_ = true;
58  }
59  virtual ~GridMap() = default;
60  GridMap ( const GridMap& ) = default;
61  GridMap& operator= ( const GridMap& ) = default;
62  GridMap ( GridMap&& ) = default;
63  GridMap& operator= ( GridMap&& ) = default;
64 
65  template <typename MapMetaData, class ARRAY>
66  void init ( const MapMetaData &metadata, ARRAY *data )
67  {
68  WorldScopedMaps::init ( metadata );
69  data_ = cv::Mat_<T> ( height(), width(), data );
70  read_only_ = false;
71  }
72  template <typename MapMetaData, class ARRAY>
73  void init ( MapMetaData &metadata, ARRAY &data )
74  {
75  WorldScopedMaps::init ( metadata );
76  data_ = cv::Mat_<T> ( height(), width(), ( T * ) &data[0] );
77  read_only_ = false;
78  }
79  template <typename MapMetaData>
80  void init ( const MapMetaData &metadata, const T &data, bool copy = false)
81  {
82  WorldScopedMaps::init ( metadata );
83  if(copy){
84  data_ = cv::Mat_<T> ( height(), width());
85  std::copy(data.begin(), data.end(), data_.begin());
86  }else {
87  data_ = cv::Mat_<T> ( height(), width(), ( T * ) &data[0] );
88  }
89  read_only_ = true;
90  }
91 
101  void circle (const Point2D &p, double radius, int8_t value, int thickness=1, int lineType = CV_AA ) {
102  cv::circle ( data_, w2m ( p ).cv(), scale_w2m(radius), cv::Scalar(value), thickness, lineType );
103  }
104  void erode (double distance, const GridMap &src = GridMap()) {
105  int erosion_size = scale_w2m(distance);
106  cv::Mat element = cv::getStructuringElement( cv::MORPH_ELLIPSE,
107  cv::Size( 2*erosion_size + 1, 2*erosion_size+1 ),
108  cv::Point( erosion_size, erosion_size ) );
109 
110  cv::Mat I(rows(), cols(), CV_8U, data_.ptr(0));
111  cv::dilate( I, I, element);
112  }
113  T& operator () ( double x, double y )
114  {
115  return data_ ( w2m ( x, y ).cv() );
116  }
117  const T& operator () ( double x, double y ) const
118  {
119  return data_ ( w2m ( x, y ).cv() );
120  }
121  T& operator () ( const Point2D& _world_coordinates )
122  {
123  return data_ ( w2m ( _world_coordinates ).cv() );
124  }
125  const T& operator () ( const Point2D& _world_coordinates ) const
126  {
127  return data_ ( w2m ( _world_coordinates ).cv() );
128  }
129  T& get ( const Point2D& _world_coordinates )
130  {
131  return data_ ( w2m ( _world_coordinates ).cv() );
132  }
133  const T& get ( const Point2D& _world_coordinates ) const
134  {
135  return data_ ( w2m ( _world_coordinates ).cv() );
136  }
137  bool isOccupyied ( const Point2D& _world_coordinates ) const
138  {
139  Point2D p = w2m ( _world_coordinates );
140  T v = data_ ( p.cv() );
141  return v >= threshold_occupyied_;
142  }
143  bool isFree ( const Point2D& _world_coordinates ) const
144  {
145  Point2D p = w2m ( _world_coordinates );
146  T v = data_ ( p.cv() );
147  return (v > SPACE_NA) && (v < threshold_free_);
148  }
149  const cv::Mat_<T> &mat() const
150  {
151  return data_;
152  }
153  void setThresholdOccupied ( const T& threshold )
154  {
155  threshold_occupyied_ = threshold;
156  }
158  {
159  return threshold_occupyied_;
160  }
161  void setThresholdFree ( const T& threshold )
162  {
163  threshold_free_ = threshold;
164  }
165  const T &getThresholdFree()
166  {
167  return threshold_free_;
168  }
169  void setThresholdUnknown ( const T& threshold )
170  {
171  threshold_unknown_ = threshold;
172  }
174  {
175  return threshold_unknown_;
176  }
177  int rows() const {
178  return data_.rows;
179  }
180  int cols() const {
181  return data_.cols;
182  }
183  T& grid ( int row, int col )
184  {
185  return data_ ( row , col );
186  }
187  const T& grid ( int row, int col ) const
188  {
189  return data_ ( row , col );
190  }
191 private:
193  cv::Mat_<T> data_;
197 };
198 }
199 #endif // TUW_GRID_MAP_H
static const int8_t SPACE_FREE
Definition: grid_map.h:49
bool isOccupyied(const Point2D &_world_coordinates) const
Definition: grid_map.h:137
const T & getThresholdUnknown()
Definition: grid_map.h:173
int cols() const
Definition: grid_map.h:180
static const int8_t SPACE_NA
Definition: grid_map.h:48
void setThresholdOccupied(const T &threshold)
Definition: grid_map.h:153
void init(const MapMetaData &metadata, const T &data, bool copy=false)
Definition: grid_map.h:80
const T & getThresholdFree()
Definition: grid_map.h:165
cv::Mat_< T > data_
Definition: grid_map.h:193
void init()
initializes the transformation matrices
GridMap & operator=(const GridMap &)=default
Point2D w2m(const Point2D &src) const
Definition: point2d.h:208
double scale_w2m(double v) const
T threshold_occupyied_
Definition: grid_map.h:194
virtual ~GridMap()=default
bool isFree(const Point2D &_world_coordinates) const
Definition: grid_map.h:143
Definition: command.h:8
T threshold_free_
Definition: grid_map.h:196
const cv::Mat_< T > & mat() const
Definition: grid_map.h:149
const T & grid(int row, int col) const
Definition: grid_map.h:187
T threshold_unknown_
Definition: grid_map.h:195
T & grid(int row, int col)
Definition: grid_map.h:183
void erode(double distance, const GridMap &src=GridMap())
Definition: grid_map.h:104
void init(const MapMetaData &metadata, ARRAY *data)
Definition: grid_map.h:66
bool read_only_
Definition: grid_map.h:192
const T & getThresholdOccupied()
Definition: grid_map.h:157
void setThresholdFree(const T &threshold)
Definition: grid_map.h:161
static const int8_t SPACE_OCCUPIED
Definition: grid_map.h:50
T & operator()(double x, double y)
Definition: grid_map.h:113
void circle(const Point2D &p, double radius, int8_t value, int thickness=1, int lineType=CV_AA)
Definition: grid_map.h:101
void setThresholdUnknown(const T &threshold)
Definition: grid_map.h:169
void init(MapMetaData &metadata, ARRAY &data)
Definition: grid_map.h:73
const cv::Point_< double > & cv() const
Definition: point2d.cpp:135
int rows() const
Definition: grid_map.h:177


tuw_geometry
Author(s): Markus Bader
autogenerated on Mon Jun 10 2019 15:33:08