]> rtime.felk.cvut.cz Git - hubacji1/iamcar.git/blob - perception/obstacle.cc
Add obstacles, collision check
[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         std::vector<RRTEdge *> edges;
42         edges.push_back(e);
43         return this->collide(edges);
44 }
45
46 bool CircleObstacle::collide(std::vector<RRTEdge *> &edges)
47 {
48         std::vector<RRTEdge *> bedges;
49         float radi = this->r() / cos(M_PI / 4); // TODO 4 is square
50         float angl = 2 * M_PI / 4;
51         float x1;
52         float y1;
53         float x2;
54         float y2;
55         int i;
56         for (i = 0; i < 4; i++) {
57                 x1 = radi * cos(i * angl);
58                 y1 = radi * sin(i * angl);
59                 x2 = radi * cos((i + 1) * angl);
60                 y2 = radi * sin((i + 1) * angl);
61                 x1 += this->x();
62                 y1 += this->y();
63                 x2 += this->x();
64                 y2 += this->y();
65                 bedges.push_back(new RRTEdge(
66                                         new RRTNode(x1, y1, 0),
67                                         new RRTNode(x2, y2, 0)));
68         }
69         for (auto &be: bedges) {
70                 for (auto &e: edges) {
71                         if (SegmentObstacle(
72                                                 be->init(),
73                                                 be->goal())
74                                         .collide(e)) {
75                                 return true;
76                         }
77                 }
78         }
79         return false;
80 }
81
82 bool SegmentObstacle::collide(RRTNode *n)
83 {
84         return false;
85 }
86
87 bool SegmentObstacle::collide(RRTEdge *e)
88 {
89         // see https://en.wikipedia.org/wiki/Line%E2%80%93line_intersection
90         float x1 = this->init()->x();
91         float y1 = this->init()->y();
92         float x2 = this->goal()->x();
93         float y2 = this->goal()->y();
94         float x3 = e->init()->x();
95         float y3 = e->init()->y();
96         float x4 = e->goal()->x();
97         float y4 = e->goal()->y();
98         float deno = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
99         if (deno == 0) {
100                 return false; // parallel
101         }
102         //return true; // colliding lines, not line segments
103         //float px = (x1 * y2 - y1 * x2) * (x3 - x4) -
104         //        (x1 - x2) * (x3 * y4 - y3 * x4);
105         //px /= deno;
106         //float py = (x1 * y2 - y1 * x2) * (y3 - y4) -
107         //        (y1 - y2) * (x3 * y4 - y3 * x4);
108         //py /= deno;
109         float s = (x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4);
110         s /= deno;
111         if (s < 0 || s > 1) {
112                 return false;
113         }
114         float t = (x1 - x2) * (y1 - y3) - (y1 - y2) * (x1 - x3);
115         t *= -1;
116         t /= deno;
117         if (t < 0 || t > 1) {
118                 return false;
119         }
120         return true;
121 }
122
123 bool SegmentObstacle::collide(std::vector<RRTEdge *> &edges)
124 {
125         for (auto &e: edges) {
126                 if (this->collide(e)) {
127                         return true;
128                 }
129         }
130         return false;
131 }