4 // kinematic constraints
5 bool BicycleCar::drivable(const BicycleCar &bc) const
7 double a_1 = atan2(bc.y() - this->y(), bc.x() - this->x()) - this->h();
12 double h_d = bc.h() - this->h();
18 if (0 <= a_1 && a_1 <= M_PI/2) { // left front
19 BicycleCar z(*this); // zone border
20 z.rotate(this->ccl().x(), this->ccl().y(), h_d);
21 // assert z.h() == bc.h()
22 if (bc.y() == z.y() && bc.x() == z.x()) // bc on zone border
24 a_2 = atan2(bc.y() - z.y(), bc.x() - z.x());
29 if (z.h() >= a_2 && a_2 >= this->h())
31 } else if (M_PI/2 < a_1 && a_1 <= M_PI) { // left rear
32 } else if (0 > a_1 && a_1 >= -M_PI/2) { // right front
33 } else if (-M_PI/2 > a_1 && a_1 >= -M_PI) { // right rear
35 // Not happenning, as ``-pi <= a <= pi``.
40 double BicycleCar::iradi() const
42 return this->mtr() - this->w() / 2;
45 double BicycleCar::ofradi() const
47 return sqrt(pow(this->mtr() + this->w() / 2, 2) + pow(this->df(), 2));
50 double BicycleCar::orradi() const
52 return sqrt(pow(this->mtr() + this->w() / 2, 2) + pow(this->dr(), 2));
55 double BicycleCar::perfect_parking_slot_len() const
57 // see Simon R. Blackburn *The Geometry of Perfect Parking*
58 // see https://www.ma.rhul.ac.uk/SRBparking
59 double r = this->ctc() / 2;
60 double l = this->wb();
61 double k = this->df() - this->wb();
68 - pow(sqrt(r*r - l*l) - w, 2)
75 void BicycleCar::set_max_steer()
77 this->st(atan(this->wb() / this->mtr()));
81 double BicycleCar::lfx() const
83 double lfx = this->x();
84 lfx += (this->w() / 2) * cos(this->h() + M_PI / 2);
85 lfx += this->df() * cos(this->h());
86 lfx += this->sd() * cos(this->h());
90 double BicycleCar::lfy() const
92 double lfy = this->y();
93 lfy += (this->w() / 2) * sin(this->h() + M_PI / 2);
94 lfy += this->df() * sin(this->h());
95 lfy += this->sd() * sin(this->h());
99 double BicycleCar::lrx() const
101 double lrx = this->x();
102 lrx += (this->w() / 2) * cos(this->h() + M_PI / 2);
103 lrx += -this->dr() * cos(this->h());
104 lrx += -this->sd() * cos(this->h());
108 double BicycleCar::lry() const
110 double lry = this->y();
111 lry += (this->w() / 2) * sin(this->h() + M_PI / 2);
112 lry += -this->dr() * sin(this->h());
113 lry += -this->sd() * sin(this->h());
117 double BicycleCar::rrx() const
119 double rrx = this->x();
120 rrx += (this->w() / 2) * cos(this->h() - M_PI / 2);
121 rrx += -this->dr() * cos(this->h());
122 rrx += -this->sd() * cos(this->h());
126 double BicycleCar::rry() const
128 double rry = this->y();
129 rry += (this->w() / 2) * sin(this->h() - M_PI / 2);
130 rry += -this->dr() * sin(this->h());
131 rry += -this->sd() * sin(this->h());
135 double BicycleCar::rfx() const
137 double rfx = this->x();
138 rfx += (this->w() / 2) * cos(this->h() - M_PI / 2);
139 rfx += this->df() * cos(this->h());
140 rfx += this->sd() * cos(this->h());
144 double BicycleCar::rfy() const
146 double rfy = this->y();
147 rfy += (this->w() / 2) * sin(this->h() - M_PI / 2);
148 rfy += this->df() * sin(this->h());
149 rfy += this->sd() * sin(this->h());
153 double BicycleCar::ralx() const
155 double lrx = this->x();
156 lrx += (this->w() / 2) * cos(this->h() + M_PI / 2);
159 double BicycleCar::raly() const
161 double lry = this->y();
162 lry += (this->w() / 2) * sin(this->h() + M_PI / 2);
166 double BicycleCar::rarx() const
168 double rrx = this->x();
169 rrx += (this->w() / 2) * cos(this->h() - M_PI / 2);
173 double BicycleCar::rary() const
175 double rry = this->y();
176 rry += (this->w() / 2) * sin(this->h() - M_PI / 2);
180 BicycleCar BicycleCar::ccl() const
183 bc.x(this->x() + this->mtr() * cos(this->h() + M_PI / 2));
184 bc.y(this->y() + this->mtr() * sin(this->h() + M_PI / 2));
189 BicycleCar BicycleCar::ccr() const
192 bc.x(this->x() + this->mtr() * cos(this->h() - M_PI / 2));
193 bc.y(this->y() + this->mtr() * sin(this->h() - M_PI / 2));
199 void BicycleCar::next()
201 this->x(this->x() + this->sp() * cos(this->h()));
202 this->y(this->y() + this->sp() * sin(this->h()));
203 this->h(this->h() + this->sp() / this->wb() * tan(this->st()));
206 void BicycleCar::rotate(double cx, double cy, double angl)
208 double px = this->x();
209 double py = this->y();
212 double nx = px * cos(angl) - py * sin(angl);
213 double ny = px * sin(angl) + py * cos(angl);
214 this->h(this->h() + angl);
219 BicycleCar::BicycleCar()
221 // TODO according to mtr_ FIXME
230 std::tuple<bool, unsigned int, unsigned int> collide(
231 std::vector<std::tuple<double, double>> &p1,
232 std::vector<std::tuple<double, double>> &p2
235 for (unsigned int i = 0; i < p1.size() - 1; i++) {
236 for (unsigned int j = 0; j < p2.size() - 1; j++) {
240 std::get<0>(p1[i + 1]),
241 std::get<1>(p1[i + 1]),
244 std::get<0>(p2[j + 1]),
245 std::get<1>(p2[j + 1])
248 return std::make_tuple(true, i, j);
251 return std::make_tuple(false, 0, 0);
254 bool inside(double x, double y, std::vector<std::tuple<double, double>> &poly)
259 for (i = 0; i < 4; i++) {
261 (std::get<1>(poly[i]) > y) != (std::get<1>(poly[j]) > y)
263 x < std::get<0>(poly[i])
264 + (std::get<0>(poly[j]) - std::get<0>(poly[i]))
265 * (y - std::get<1>(poly[i]))
266 / (std::get<1>(poly[j]) - std::get<1>(poly[i]))
275 std::tuple<bool, double, double> intersect(
276 double x1, double y1,
277 double x2, double y2,
278 double x3, double y3,
282 double deno = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
284 return std::make_tuple(false, 0, 0);
285 double t = (x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4);
287 double u = (x1 - x2) * (y1 - y3) - (y1 - y2) * (x1 - x3);
290 if (t < 0 || t > 1 || u < 0 || u > 1)
291 return std::make_tuple(false, 0, 0);
292 return std::make_tuple(true, x1 + t * (x2 - x1), y1 + t * (y2 - y1));