]> rtime.felk.cvut.cz Git - hubacji1/iamcar.git/blob - perception/obstacle.cc
Update changelog
[hubacji1/iamcar.git] / perception / obstacle.cc
1 /*
2 This file is part of I am car.
3
4 I am car is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 I am car is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with I am car. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include <cmath>
19 #include "obstacle.h"
20
21 float CircleObstacle::r()
22 {
23         return this->h();
24 }
25
26 bool CircleObstacle::collide(RRTNode *n)
27 {
28         float xx = n->x() - this->x();
29         xx *= xx;
30         float yy = n->y() - this->y();
31         yy *= yy;
32         float rr = this->r() * this->r();
33         if (xx + yy <= rr) {
34                 return true;
35         }
36         return false;
37 }
38
39 bool CircleObstacle::collide(RRTEdge *e)
40 {
41         // see http://doswa.com/2009/07/13/circle-segment-intersectioncollision.html
42         // Find the closest point on seq.
43         float seg_v[] = {
44                 e->goal()->x() - e->init()->x(),
45                 e->goal()->y() - e->init()->y()
46         };
47         float pt_v[] = {
48                 this->x() - e->init()->x(),
49                 this->y() - e->init()->y()
50         };
51         float seg_vl = sqrt(pow(seg_v[0], 2) + pow(seg_v[1], 2));
52         // seg_vl must be > 0 otherwise it is invalid segment length.
53         if (seg_vl <= 0)
54                 return false;
55         float seg_v_unit[] = {seg_v[0] / seg_vl, seg_v[1] / seg_vl};
56         float proj = pt_v[0]*seg_v_unit[0] + pt_v[1]*seg_v_unit[1];
57         float closest[] = {0, 0};
58         if (proj <= 0) {
59                 closest[0] = e->init()->x();
60                 closest[1] = e->init()->y();
61         } else if (proj >= seg_vl) {
62                 closest[0] = e->goal()->x();
63                 closest[1] = e->goal()->y();
64         } else {
65                 float proj_v[] = {seg_v_unit[0] * proj, seg_v_unit[1] * proj};
66                 closest[0] = proj_v[0] + e->init()->x();
67                 closest[1] = proj_v[1] + e->init()->y();
68         }
69         // Find the segment circle.
70         float dist_v[] = {this->x() - closest[0], this->y() - closest[1]};
71         float dist = sqrt(pow(dist_v[0], 2) + pow(dist_v[1], 2));
72         if (dist <= this->r())
73                 return true;
74         return false;
75         // Offset computation.
76         // float offset[] = {
77         //        dist_v[0] / dist * (BCAR_TURNING_RADIUS - dist),
78         //        dist_v[1] / dist * (BCAR_TURNING_RADIUS - dist)
79         // };
80 }
81
82 bool CircleObstacle::collide(std::vector<RRTEdge *> &edges)
83 {
84         for (auto e: edges)
85                 if (this->collide(e))
86                         return true;
87         return false;
88 }
89
90 float CircleObstacle::dist_to(RRTNode *n)
91 {
92         float xx = n->x() - this->x();
93         xx *= xx;
94         float yy = n->y() - this->y();
95         yy *= yy;
96         return (float) (sqrt(xx + yy) - this->r());
97 }
98
99 void PolygonObstacle::add_bnode(RRTNode *n)
100 {
101         this->bnodes_.push_back(n);
102 }
103
104 bool PolygonObstacle::collide(RRTNode *n)
105 {
106         // From substack/point-in-polygon, see
107         // https://github.com/substack/point-in-polygon/blob/master/index.js
108         bool inside = false;
109         unsigned int i;
110         int j;
111         for (i = 0, j = this->bnodes_.size() - 1;
112                         i < this->bnodes_.size();
113                         j = i++) {
114                 float xi = this->bnodes_[i]->x();
115                 float yi = this->bnodes_[i]->y();
116                 float xj = this->bnodes_[j]->x();
117                 float yj = this->bnodes_[j]->y();
118                 bool intersect = ((yi > n->y()) != (yj > n->y())) &&
119                         (n->x() < (xj - xi) * (n->y() - yi) / (yj - yi) + xi);
120                 if (intersect)
121                         inside = !inside;
122         }
123         return inside;
124 }
125
126 bool PolygonObstacle::collide(RRTEdge *e)
127 {
128         for (auto &f: this->frame()) {
129                 if (SegmentObstacle(f->init(), f->goal()).collide(e))
130                         return true;
131         }
132         return false;
133 }
134
135 bool PolygonObstacle::collide(std::vector<RRTEdge *> &edges)
136 {
137         for (auto &e: edges) {
138                 if (this->collide(e))
139                         return true;
140         }
141         return false;
142 }
143
144 bool PolygonObstacle::collide(std::vector<RRTEdge *> edges)
145 {
146         for (auto &e: edges) {
147                 if (this->collide(e))
148                         return true;
149         }
150         return false;
151 }
152
153 float PolygonObstacle::dist_to(RRTNode *n)
154 {
155         return 0;
156 }
157
158 std::vector<RRTEdge *> PolygonObstacle::frame()
159 {
160         std::vector<RRTEdge *> frame;
161         unsigned int i;
162         int j;
163         for (i = 1, j = 0; i < this->bnodes_.size(); j = i++) {
164                 frame.push_back(new RRTEdge(
165                         new RRTNode(
166                                 this->bnodes_[j]->x(),
167                                 this->bnodes_[j]->y(),
168                                 this->bnodes_[j]->h()
169                         ),
170                         new RRTNode(
171                                 this->bnodes_[i]->x(),
172                                 this->bnodes_[i]->y(),
173                                 this->bnodes_[i]->h()
174                         )
175                 ));
176         }
177         return frame;
178 }
179
180 std::vector<RRTNode *> &PolygonObstacle::bnodes()
181 {
182         return this->bnodes_;
183 }
184
185 bool SegmentObstacle::collide(RRTNode *n)
186 {
187         return false;
188 }
189
190 bool SegmentObstacle::collide(RRTEdge *e)
191 {
192         if (this->intersect(e, true))
193                 return true;
194         return false;
195 }
196
197 bool SegmentObstacle::collide(std::vector<RRTEdge *> &edges)
198 {
199         for (auto &e: edges) {
200                 if (this->collide(e)) {
201                         return true;
202                 }
203         }
204         return false;
205 }
206
207 float SegmentObstacle::dist_to(RRTNode *n)
208 {
209         // see https://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line
210         float x1 = this->init()->x();
211         float y1 = this->init()->y();
212         float x2 = this->goal()->x();
213         float y2 = this->goal()->y();
214         float x0 = n->x();
215         float y0 = n->y();
216         float nume = (y2 - y1) * x0 - (x2 - x1) * y0 + x2 * y1 - y2 * x1;
217         nume = std::abs(nume);
218         float deno = sqrt(pow(y2 - y1, 2) + pow(x2 - x1, 2));
219         return nume / deno;
220 }