linesegment2d_detector.cpp
Go to the documentation of this file.
2 
3 using namespace tuw;
4 
6 }
7 void LineSegment2DDetector::LineSegment::updatePoints(const std::vector<Point2D> &points) {
8  if((idx0_ < idx1_) && (idx0_ >= 0) && (idx1_ < points.size())) {
9  points_.clear();
10  points_.reserve(idx1_-idx0_+1);
11  for(unsigned int i = idx0_; i <= idx1_; i++) {
12  points_.push_back(points[i]);
13  }
14  }
15 }
17  if( (idx < idx0_) || (idx > idx1_)) {
18  return false;
19  } else {
20  return true;
21  }
22 }
24  return idx1_ - idx0_ + 2;
25 }
26 void LineSegment2DDetector::LineSegment::set(unsigned int idx0, unsigned int idx1, const std::vector<Point2D> &points) {
27  idx0_ = idx0, idx1_ = idx1;
28  LineSegment2D::set(points[idx0], points[idx1]);
29 }
30 
31 
32 void LineSegment2DDetector::start (const std::vector<Point2D> &points) {
33 
34  connected_measurments_.clear();
35  segments_.clear();
36 
37  if(points.size() > 0) {
38 
39  std::pair< unsigned int, unsigned int> idx;
40  idx.first = 0;
41 
42  while(idx.first < points.size()) {
43  idx.second = idx.first + 1;
44  float threshold = 4 * points[idx.second].distanceTo(points[idx.second+1]);
45  while(idx.second < points.size()) {
47  float d = points[idx.second].distanceTo(points[idx.second+1]);
48  if(d > threshold) {
49  break;
50  }
51  threshold = 4 * d;
52  }
53  idx.second++;
54  }
55  if((idx.second - idx.first) > 2) {
56  connected_measurments_.push_back(idx);
57  }
58  idx.first = idx.second+1;
59  }
60 
61  for(unsigned int i = 0; i < connected_measurments_.size(); i++) {
62  unsigned int idx0 = connected_measurments_[i].first;
63  unsigned int idx1 = connected_measurments_[i].second;
64  if(idx1 > idx0) {
65  LineSegment line;
66  line.set(idx0, idx1, points);
67  split(line, points);
68  }
69  }
70  }
71 }
72 std::vector<LineSegment2D> &LineSegment2DDetector::start (const std::vector<Point2D> &points, std::vector<LineSegment2D> &detected_segments){
73  start(points);
74  detected_segments.reserve(detected_segments.size() + segments_.size());
75  for(const LineSegment &l: segments_){
76  detected_segments.push_back(l);
77  }
78  return detected_segments;
79 }
80 void LineSegment2DDetector::split(LineSegment &line, const std::vector<Point2D> &points) {
81  unsigned int idxMax=line.idx0_;
82  float d;
83  float dMax = 0;
84  for(unsigned int i = line.idx0_; i < line.idx1_; i++) {
85  d = fabs(line.distanceTo(points[i]));
86  if(d > dMax) {
87  dMax = d, idxMax = i;
88  }
89  }
90  if(dMax > config_.threshold_split) {
91  LineSegment l0,l1;
92  if(line.idx0_ + config_.min_points_per_line < idxMax) {
93  l0.set(line.idx0_, idxMax, points);
94  split(l0, points);
95  }
96  if(idxMax + config_.min_points_per_line < line.idx1_) {
97  l1.set(idxMax, line.idx1_, points);
98  split(l1, points);
99  }
100  } else {
101  if(line.length() < config_.min_length) {
102  return;
103  }
104  if(((float) line.nrSupportPoint()) / line.length() < config_.min_points_per_unit) {
105  return;
106  }
107  line.id_ = segments_.size();
108  line.updatePoints(points);
109  segments_.push_back(line);
110  }
111 }
112 
113 const std::vector<LineSegment2DDetector::LineSegment> & LineSegment2DDetector::result(){
114  return segments_;
115 }
d
void set(unsigned int idx0, unsigned int idx1, const std::vector< Point2D > &points)
void start(const std::vector< Point2D > &points)
const double length() const
std::vector< std::pair< unsigned int, unsigned int > > connected_measurments_
Definition: command.h:8
const std::vector< LineSegment > & result()
LineSegment2DDetectorParameter config_
LineSegment2D & set(const double &x0, const double &y0, const double &x1, const double &y1)
void split(LineSegment &line, const std::vector< Point2D > &points)
double distanceTo(const Point2D &p, double &dx, double &dy) const
std::vector< LineSegment > segments_
void updatePoints(const std::vector< Point2D > &points)


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