blob: c175616562a837d6fa16964a6e1d30ee67445862 [file] [log] [blame]
Austin Schuhad596222018-01-31 23:34:03 -08001#ifndef Y2018_CONTORL_LOOPS_PYTHON_ARM_BOUNDS_H_
2#define Y2018_CONTORL_LOOPS_PYTHON_ARM_BOUNDS_H_
3
4#include <CGAL/Bbox_2.h>
5#include <CGAL/Boolean_set_operations_2.h>
6#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
7#include <CGAL/Polygon_2.h>
8#include <CGAL/Polygon_2_algorithms.h>
9#include <CGAL/Polygon_with_holes_2.h>
10#include <CGAL/squared_distance_2.h>
11
12#include <Eigen/Dense>
13
14// Prototype level code to find the nearest point and distance to a polygon.
15
16namespace y2018 {
17namespace control_loops {
18
19typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
20typedef K::Point_2 Point;
21typedef K::Segment_2 Segment;
22typedef CGAL::Bbox_2 Bbox;
23typedef CGAL::Polygon_2<K> SimplePolygon;
24typedef CGAL::Polygon_with_holes_2<K> Polygon;
25typedef K::Line_2 Line;
26typedef K::Vector_2 Vector;
27
Austin Schuhad596222018-01-31 23:34:03 -080028// Returns true if the point p3 is to the left of the vector from p1 to p2.
29inline bool is_left(Point p1, Point p2, Point p3) {
30 switch (CGAL::orientation(p1, p2, p3)) {
31 case CGAL::LEFT_TURN:
32 case CGAL::COLLINEAR:
33 return true;
34 case CGAL::RIGHT_TURN:
35 return false;
36 }
37}
38
39// Returns true if the segments intersect.
40inline bool intersects(Segment s1, Segment s2) {
41 return CGAL::do_intersect(s1, s2);
42}
43
44class BoundsCheck {
45 public:
46 BoundsCheck(const std::vector<Point> &points)
47 : points_(points), grid_(points_, 6) {}
48
49 double min_distance(Point point, ::Eigen::Matrix<double, 2, 1> *normal) const;
50
51 const std::vector<Point> &points() const { return points_; }
52
53 private:
54 static Bbox ToBbox(const std::vector<Point> &points) {
55 Bbox out;
56 out += Segment(points.back(), points.front()).bbox();
57 for (size_t i = 0; i < points.size() - 1; ++i) {
58 out += Segment(points[i], points[i + 1]).bbox();
59 }
60 return out;
61 }
62
63 static SimplePolygon ToPolygon(Bbox bbox) {
64 Point points[4]{{bbox.xmin(), bbox.ymin()},
65 {bbox.xmax(), bbox.ymin()},
66 {bbox.xmax(), bbox.ymax()},
67 {bbox.xmin(), bbox.ymax()}};
68 return SimplePolygon(&points[0], &points[4]);
69 }
70
71 static double min_dist(Point pt, const std::vector<Point> &points,
72 Segment *best_segment) {
73 *best_segment = Segment(points.back(), points.front());
74 double min_dist_sqr = CGAL::squared_distance(pt, *best_segment);
75 for (size_t i = 0; i < points.size() - 1; ++i) {
76 Segment s(points[i], points[i + 1]);
77 double segment_distance = CGAL::squared_distance(pt, s);
78 if (segment_distance < min_dist_sqr) {
79 min_dist_sqr = segment_distance;
80 *best_segment = s;
81 }
82 }
83 return sqrt(min_dist_sqr);
84 }
85
86 static std::vector<Segment> ToSegment(Bbox bbox) {
87 Point points[4]{{bbox.xmin(), bbox.ymin()},
88 {bbox.xmax(), bbox.ymin()},
89 {bbox.xmax(), bbox.ymax()},
90 {bbox.xmin(), bbox.ymax()}};
91
92 return std::vector<Segment>({{points[0], points[1]},
Philipp Schrader790cb542023-07-05 21:06:52 -070093 {points[1], points[2]},
94 {points[2], points[3]},
95 {points[3], points[0]}});
Austin Schuhad596222018-01-31 23:34:03 -080096 }
97
98 static bool check_inside(Point pt, const std::vector<Point> &points) {
99 switch (CGAL::bounded_side_2(&points[0], &points[points.size()], pt, K())) {
100 case CGAL::ON_BOUNDED_SIDE:
101 case CGAL::ON_BOUNDARY:
102 return true;
103 case CGAL::ON_UNBOUNDED_SIDE:
104 return false;
105 }
106 return false;
107 }
108
109 const std::vector<Point> points_;
110
111 class GridCell {
112 public:
113 GridCell(const std::vector<Point> &points, Bbox bbox) {
114 bool has_intersect = false;
115
116 Point center{(bbox.xmin() + bbox.xmax()) / 2,
117 (bbox.ymin() + bbox.ymax()) / 2};
118 // Purposefully overestimate.
119 double r = bbox.ymax() - bbox.ymin();
120
121 Segment best_segment;
122 double best = min_dist(center, points, &best_segment);
123 dist_upper_bound_ = best + 2 * r;
124 dist_lower_bound_ = std::max(best - 2 * r, 0.0);
125
126 double sq_upper_bound = dist_upper_bound_ * dist_upper_bound_;
127
128 auto try_add_segment = [&](Segment segment) {
129 for (const auto &bbox_segment : ToSegment(bbox)) {
130 if (CGAL::do_intersect(bbox_segment, segment)) {
131 has_intersect = true;
132 }
133 }
134
135 double dist_sqr = CGAL::squared_distance(center, segment);
136 if (dist_sqr < sq_upper_bound) {
137 segments_.push_back(segment);
138 }
139 };
140
141 try_add_segment(Segment(points.back(), points.front()));
142 for (size_t i = 0; i < points.size() - 1; ++i) {
143 try_add_segment(Segment(points[i], points[i + 1]));
144 }
145 if (has_intersect) {
146 is_borderline = true;
147 } else {
148 is_inside = check_inside(center, points);
149 }
150 }
151
152 bool IsInside(Point pt) const {
153 (void)pt;
154 return is_inside;
155 }
156
157 bool IsBorderline() const { return is_borderline; }
158
159 double DistanceSqr(Point pt, Segment *best_segment) const {
160 double min_dist_sqr = CGAL::squared_distance(pt, segments_[0]);
161 *best_segment = segments_[0];
162 for (size_t i = 1; i < segments_.size(); ++i) {
163 double new_distance = CGAL::squared_distance(pt, segments_[i]);
164 if (new_distance < min_dist_sqr) {
165 min_dist_sqr = new_distance;
166 *best_segment = segments_[i];
167 }
168 }
169 return min_dist_sqr;
170 }
171 double Distance(Point pt, Segment *best_segment) const {
172 return sqrt(DistanceSqr(pt, best_segment));
173 }
174
175 bool is_inside = false;
176 bool is_borderline = false;
177 double dist_upper_bound_;
178 double dist_lower_bound_;
179 std::vector<Segment> segments_;
180 std::vector<std::vector<Point>> polygons_;
181 };
182
183 class GridSystem {
184 public:
185 // Precision is really 2**-precision and must be positive.
186 GridSystem(const std::vector<Point> &points, int precision)
187 : points_(points), scale_factor_(1 << precision) {
188 auto bbox = ToBbox(points);
189 fprintf(stderr, "%g %g, %g %g\n", bbox.xmin(), bbox.ymin(), bbox.xmax(),
190 bbox.ymax());
191 x_min_ = static_cast<int>(std::floor(bbox.xmin() * scale_factor_)) - 1;
192 y_min_ = static_cast<int>(std::floor(bbox.ymin() * scale_factor_)) - 1;
193
194 stride_ = static_cast<int>(bbox.xmax() * scale_factor_) + 3 - x_min_;
195 height_ = static_cast<int>(bbox.ymax() * scale_factor_) + 3 - y_min_;
196
197 fprintf(stderr, "num_cells: %d\n", stride_ * height_);
198 cells_.reserve(stride_ * height_);
199 for (int y_cell = 0; y_cell < height_; ++y_cell) {
200 for (int x_cell = 0; x_cell < stride_; ++x_cell) {
201 cells_.push_back(
202 GridCell(points, Bbox(static_cast<double>(x_cell + x_min_) /
203 static_cast<double>(scale_factor_),
204 static_cast<double>(y_cell + y_min_) /
205 static_cast<double>(scale_factor_),
206 static_cast<double>(x_cell + x_min_ + 1) /
207 static_cast<double>(scale_factor_),
208 static_cast<double>(y_cell + y_min_ + 1) /
209 static_cast<double>(scale_factor_))));
210 }
211 }
212 }
213
214 const GridCell *GetCell(Point pt) const {
215 int x_cell =
216 static_cast<int>(std::floor(pt.x() * scale_factor_)) - x_min_;
217 int y_cell =
218 static_cast<int>(std::floor(pt.y() * scale_factor_)) - y_min_;
219 if (x_cell < 0 || x_cell >= stride_) return nullptr;
220 if (y_cell < 0 || y_cell >= height_) return nullptr;
221 return &cells_[stride_ * y_cell + x_cell];
222 }
223
224 const std::vector<Point> &points() const { return points_; }
225
226 private:
227 std::vector<Point> points_;
228 int scale_factor_;
229 int x_min_;
230 int y_min_;
231 int stride_;
232 int height_;
233 std::vector<GridCell> cells_;
234 };
235
236 GridSystem grid_;
237};
238
239BoundsCheck MakeClippedArmSpace();
240BoundsCheck MakeFullArmSpace();
241
242} // namespace control_loops
243} // namespace y2018
244
245#endif // Y2018_CONTORL_LOOPS_PYTHON_ARM_BOUNDS_H_