segment_expander.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017, <copyright holder> <email>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of the <organization> nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY <copyright holder> <email> ''AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL <copyright holder> <email> BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  */
28 
29 #ifndef SEGMENT_EXPANDER_H
30 #define SEGMENT_EXPANDER_H
31 
32 #include <algorithm>
33 #include <memory>
34 #include <queue>
35 #include <vector>
38 #include <opencv/cv.hpp>
39 
40 namespace tuw_graph
41 {
42 
44  {
45  public:
47 
54  void Initialize(cv::Mat &_map, cv::Mat &_distField, cv::Mat &_voronoiPath);
60  std::vector<std::vector<Eigen::Vector2d>> calcEndpoints(float *_potential);
61 
71  std::vector< Segment > getGraph(const std::vector<std::vector<Eigen::Vector2d>> &_endPoints, float *_potential, const float _min_length, float _optimizePixelsCrossing, const float _optimizePixelsEndSegments);
72 
73  private:
74  std::unique_ptr<float[]> distance_field_;
75  std::unique_ptr<int8_t[]> voronoi_graph_;
76  std::unique_ptr<int8_t[]> global_map_;
77  bool findEdgeSegments_ = true;
78  int nx_, ny_, ns_;
79 
80  template <class T, class S, class C>
81  void clearpq(std::priority_queue<T, S, C>& q)
82  {
83  q = std::priority_queue<T, S, C>();
84  }
85  class Index
86  {
87  public:
88  Index(int index, float c, float d, float p)
89  {
90  i = index;
91  weight = c;
92  dist = d;
93  potential = p;
94  }
95  Index(int x_val, int y_val, int nx, float c, float d, float p)
96  {
97  i = x_val + y_val * nx;
98  weight = c;
99  dist = d;
100  potential = p;
101  }
102  Index offsetDist(int dist_x, int dist_y, int nx, int ny) const
103  {
104  int x_val = (i % nx) + dist_x;
105  int y_val = (i / nx) + dist_y;
106 
107  if(x_val < 0 || x_val > nx || y_val < 0 || y_val > ny)
108  return Index(-1, -1, -1, -1);
109 
110  return Index(x_val, y_val, nx, 0, 0, 0);
111  }
112  int getX(int nx) const
113  {
114  return (i % nx);
115  }
116  int getY(int nx) const
117  {
118  return (i / nx);
119  }
120  int i;
121  float weight;
122  float dist;
123  float cost;
124  float potential;
125  };
126  struct greater1
127  {
128  bool operator()(const Index& a, const Index& b) const
129  {
130  return a.weight > b.weight;
131  }
132  };
133  std::priority_queue<Index, std::vector<Index>, greater1> queue_;
134 
135  //Splits a given path into multiple segments with minimum length
136  const std::vector<tuw_graph::Segment> splitPath(const std::vector<Eigen::Vector2d> &_path, const float _minimum_length);
137 
138  //Returns the nr of neighbors (removing double neighbours...)
139  uint32_t nrOfNeighbours(uint32_t i) const;
140 
141  //searches all path endpoints at a crossing
142  std::vector<Eigen::Vector2d> expandCrossing(const Index &i, float* _potential);
143 
144  //expands from a given crossing endpoint until another crossing is found
145  Eigen::Vector2d expandSegment(Index start, float* _potential, const std::vector<std::vector<Eigen::Vector2d>> &_endpoints);
146 
147  //Tries to find a path
148  std::vector<Eigen::Vector2d> getPath(const Index &start, float* _potential, const std::vector<std::vector<Eigen::Vector2d>> &_endpoints);
149 
150  //Returns the minimum space a segment has
151  float getMinSegmentWidth(const std::vector<Eigen::Vector2d> &_path);
152 
153  //Safely removes segments from the segment list taking care of neighbors
154  void removeSegmentFromList(const uint32_t _id, std::vector<Segment> &_segments);
155 
156  //Adds an expansion candidate for the Dijkstra algorithm
157  void addExpansionCandidate(const Index &current, const Index &next, float* potential);
158 
159  //Checks if the given point is a valid endpoint
160  bool isEndpoint(Index &_current, const std::vector<std::vector<Eigen::Vector2d>> &_endpoints);
161 
162  //Removes a endponit
163  void removeEndpoint(const Index &_current, std::vector<std::vector<Eigen::Vector2d>> &_endpoints);
164  bool checkSegmentPoint(const Index &_point);
165 
166  //Optimizes the Graph by removing to short segments at the end of it and merging crossings in a specific radius
167  void optimizeSegments(std::vector<Segment> &_segments, float _maxPixelsCrossing, float _maxPixelsEndSeg);
168 
169  //Merges crossings together
170  void optimizeSegmentsAroundPoint(std::vector<Segment> &_segments, const Eigen::Vector2d &pt, float maxPixels, int startIndex);
171  };
172 
173 }
174 #endif // VORONOI_EXPANDER_H
void addExpansionCandidate(const Index &current, const Index &next, float *potential)
void optimizeSegments(std::vector< Segment > &_segments, float _maxPixelsCrossing, float _maxPixelsEndSeg)
std::unique_ptr< float[]> distance_field_
Index(int index, float c, float d, float p)
Index(int x_val, int y_val, int nx, float c, float d, float p)
std::priority_queue< Index, std::vector< Index >, greater1 > queue_
std::unique_ptr< int8_t[]> voronoi_graph_
std::vector< Eigen::Vector2d > getPath(const Index &start, float *_potential, const std::vector< std::vector< Eigen::Vector2d >> &_endpoints)
std::vector< Eigen::Vector2d > expandCrossing(const Index &i, float *_potential)
void Initialize(cv::Mat &_map, cv::Mat &_distField, cv::Mat &_voronoiPath)
initializes the expander by setting the voronoi path map and distancefield
Index offsetDist(int dist_x, int dist_y, int nx, int ny) const
std::vector< std::vector< Eigen::Vector2d > > calcEndpoints(float *_potential)
looks for crossings and saves all segment endpoints of it
Eigen::Vector2d expandSegment(Index start, float *_potential, const std::vector< std::vector< Eigen::Vector2d >> &_endpoints)
void optimizeSegmentsAroundPoint(std::vector< Segment > &_segments, const Eigen::Vector2d &pt, float maxPixels, int startIndex)
bool operator()(const Index &a, const Index &b) const
float getMinSegmentWidth(const std::vector< Eigen::Vector2d > &_path)
void removeSegmentFromList(const uint32_t _id, std::vector< Segment > &_segments)
void removeEndpoint(const Index &_current, std::vector< std::vector< Eigen::Vector2d >> &_endpoints)
bool checkSegmentPoint(const Index &_point)
std::vector< Segment > getGraph(const std::vector< std::vector< Eigen::Vector2d >> &_endPoints, float *_potential, const float _min_length, float _optimizePixelsCrossing, const float _optimizePixelsEndSegments)
returns a list of segments, which represent the found search graph
const std::vector< tuw_graph::Segment > splitPath(const std::vector< Eigen::Vector2d > &_path, const float _minimum_length)
void clearpq(std::priority_queue< T, S, C > &q)
uint32_t nrOfNeighbours(uint32_t i) const
bool isEndpoint(Index &_current, const std::vector< std::vector< Eigen::Vector2d >> &_endpoints)
std::unique_ptr< int8_t[]> global_map_
std::queue< Index > q
Definition: thinning.cpp:106


tuw_voronoi_graph
Author(s): Benjamin Binder
autogenerated on Mon Jun 10 2019 15:42:44