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 if (0 <= a_1 && a_1 <= M_PI/2) { // left front
13 } else if (M_PI/2 < a_1 && a_1 <= M_PI) { // left rear
14 } else if (0 > a_1 && a_1 >= -M_PI/2) { // right front
15 } else if (-M_PI/2 > a_1 && a_1 >= -M_PI) { // right rear
17 // Not happenning, as ``-pi <= a <= pi``.
22 double BicycleCar::iradi() const
24 return this->mtr() - this->w() / 2;
27 double BicycleCar::ofradi() const
29 return sqrt(pow(this->mtr() + this->w() / 2, 2) + pow(this->df(), 2));
32 double BicycleCar::orradi() const
34 return sqrt(pow(this->mtr() + this->w() / 2, 2) + pow(this->dr(), 2));
37 double BicycleCar::perfect_parking_slot_len() const
39 // see Simon R. Blackburn *The Geometry of Perfect Parking*
40 // see https://www.ma.rhul.ac.uk/SRBparking
41 double r = this->ctc() / 2;
42 double l = this->wb();
43 double k = this->df() - this->wb();
50 - pow(sqrt(r*r - l*l) - w, 2)
57 void BicycleCar::set_max_steer()
59 this->st(atan(this->wb() / this->mtr()));
63 double BicycleCar::lfx() const
65 double lfx = this->x();
66 lfx += (this->w() / 2) * cos(this->h() + M_PI / 2);
67 lfx += this->df() * cos(this->h());
68 lfx += this->sd() * cos(this->h());
72 double BicycleCar::lfy() const
74 double lfy = this->y();
75 lfy += (this->w() / 2) * sin(this->h() + M_PI / 2);
76 lfy += this->df() * sin(this->h());
77 lfy += this->sd() * sin(this->h());
81 double BicycleCar::lrx() const
83 double lrx = this->x();
84 lrx += (this->w() / 2) * cos(this->h() + M_PI / 2);
85 lrx += -this->dr() * cos(this->h());
86 lrx += -this->sd() * cos(this->h());
90 double BicycleCar::lry() const
92 double lry = this->y();
93 lry += (this->w() / 2) * sin(this->h() + M_PI / 2);
94 lry += -this->dr() * sin(this->h());
95 lry += -this->sd() * sin(this->h());
99 double BicycleCar::rrx() const
101 double rrx = this->x();
102 rrx += (this->w() / 2) * cos(this->h() - M_PI / 2);
103 rrx += -this->dr() * cos(this->h());
104 rrx += -this->sd() * cos(this->h());
108 double BicycleCar::rry() const
110 double rry = this->y();
111 rry += (this->w() / 2) * sin(this->h() - M_PI / 2);
112 rry += -this->dr() * sin(this->h());
113 rry += -this->sd() * sin(this->h());
117 double BicycleCar::rfx() const
119 double rfx = this->x();
120 rfx += (this->w() / 2) * cos(this->h() - M_PI / 2);
121 rfx += this->df() * cos(this->h());
122 rfx += this->sd() * cos(this->h());
126 double BicycleCar::rfy() const
128 double rfy = this->y();
129 rfy += (this->w() / 2) * sin(this->h() - M_PI / 2);
130 rfy += this->df() * sin(this->h());
131 rfy += this->sd() * sin(this->h());
135 double BicycleCar::ralx() const
137 double lrx = this->x();
138 lrx += (this->w() / 2) * cos(this->h() + M_PI / 2);
141 double BicycleCar::raly() const
143 double lry = this->y();
144 lry += (this->w() / 2) * sin(this->h() + M_PI / 2);
148 double BicycleCar::rarx() const
150 double rrx = this->x();
151 rrx += (this->w() / 2) * cos(this->h() - M_PI / 2);
155 double BicycleCar::rary() const
157 double rry = this->y();
158 rry += (this->w() / 2) * sin(this->h() - M_PI / 2);
162 BicycleCar BicycleCar::ccl() const
165 bc.x(this->x() + this->mtr() * cos(this->h() + M_PI / 2));
166 bc.y(this->y() + this->mtr() * sin(this->h() + M_PI / 2));
171 BicycleCar BicycleCar::ccr() const
174 bc.x(this->x() + this->mtr() * cos(this->h() - M_PI / 2));
175 bc.y(this->y() + this->mtr() * sin(this->h() - M_PI / 2));
181 void BicycleCar::next()
183 this->x(this->x() + this->sp() * cos(this->h()));
184 this->y(this->y() + this->sp() * sin(this->h()));
185 this->h(this->h() + this->sp() / this->wb() * tan(this->st()));
188 void BicycleCar::rotate(double cx, double cy, double angl)
190 double px = this->x();
191 double py = this->y();
194 double nx = px * cos(angl) - py * sin(angl);
195 double ny = px * sin(angl) + py * cos(angl);
196 this->h(this->h() + angl);
201 BicycleCar::BicycleCar()
203 // TODO according to mtr_ FIXME
212 std::tuple<bool, unsigned int, unsigned int> collide(
213 std::vector<std::tuple<double, double>> &p1,
214 std::vector<std::tuple<double, double>> &p2
217 for (unsigned int i = 0; i < p1.size() - 1; i++) {
218 for (unsigned int j = 0; j < p2.size() - 1; j++) {
222 std::get<0>(p1[i + 1]),
223 std::get<1>(p1[i + 1]),
226 std::get<0>(p2[j + 1]),
227 std::get<1>(p2[j + 1])
230 return std::make_tuple(true, i, j);
233 return std::make_tuple(false, 0, 0);
236 bool inside(double x, double y, std::vector<std::tuple<double, double>> &poly)
241 for (i = 0; i < 4; i++) {
243 (std::get<1>(poly[i]) > y) != (std::get<1>(poly[j]) > y)
245 x < std::get<0>(poly[i])
246 + (std::get<0>(poly[j]) - std::get<0>(poly[i]))
247 * (y - std::get<1>(poly[i]))
248 / (std::get<1>(poly[j]) - std::get<1>(poly[i]))
257 std::tuple<bool, double, double> intersect(
258 double x1, double y1,
259 double x2, double y2,
260 double x3, double y3,
264 double deno = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
266 return std::make_tuple(false, 0, 0);
267 double t = (x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4);
269 double u = (x1 - x2) * (y1 - y3) - (y1 - y2) * (x1 - x3);
272 if (t < 0 || t > 1 || u < 0 || u > 1)
273 return std::make_tuple(false, 0, 0);
274 return std::make_tuple(true, x1 + t * (x2 - x1), y1 + t * (y2 - y1));