4 // kinematic constraints
5 bool BicycleCar::drivable(const BicycleCar &bc) const
8 pow(this->ccl().x() - bc.x(), 2)
9 + pow(this->ccl().y() - bc.y(), 2)
10 <= pow(this->mtr(), 2)
14 pow(this->ccr().x() - bc.x(), 2)
15 + pow(this->ccr().y() - bc.y(), 2)
16 <= pow(this->mtr(), 2)
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()
184 //if (this->st() > this->wb() / this->mtr())
185 // this->st(this->wb() / this->mtr());
186 //if (this->st() < -this->wb() / this->mtr())
187 // this->st(-this->wb() / this->mtr());
188 this->h(this->h() + this->sp() / this->wb() * tan(this->st()));
189 this->x(this->x() + this->sp() * cos(this->h()));
190 this->y(this->y() + this->sp() * sin(this->h()));
193 void BicycleCar::rotate(double cx, double cy, double angl)
195 double px = this->x();
196 double py = this->y();
199 double nx = px * cos(angl) - py * sin(angl);
200 double ny = px * sin(angl) + py * cos(angl);
201 this->h(this->h() + angl);
206 BicycleCar::BicycleCar()
208 // TODO according to mtr_ FIXME
217 std::tuple<bool, unsigned int, unsigned int> collide(
218 std::vector<std::tuple<double, double>> &p1,
219 std::vector<std::tuple<double, double>> &p2
222 for (unsigned int i = 0; i < p1.size() - 1; i++) {
223 for (unsigned int j = 0; j < p2.size() - 1; j++) {
227 std::get<0>(p1[i + 1]),
228 std::get<1>(p1[i + 1]),
231 std::get<0>(p2[j + 1]),
232 std::get<1>(p2[j + 1])
235 return std::make_tuple(true, i, j);
238 return std::make_tuple(false, 0, 0);
241 bool inside(double x, double y, std::vector<std::tuple<double, double>> &poly)
246 for (i = 0; i < 4; i++) {
248 (std::get<1>(poly[i]) > y) != (std::get<1>(poly[j]) > y)
250 x < std::get<0>(poly[i])
251 + (std::get<0>(poly[j]) - std::get<0>(poly[i]))
252 * (y - std::get<1>(poly[i]))
253 / (std::get<1>(poly[j]) - std::get<1>(poly[i]))
262 std::tuple<bool, double, double> intersect(
263 double x1, double y1,
264 double x2, double y2,
265 double x3, double y3,
269 double deno = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
271 return std::make_tuple(false, 0, 0);
272 double t = (x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4);
274 double u = (x1 - x2) * (y1 - y3) - (y1 - y2) * (x1 - x3);
277 if (t < 0 || t > 1 || u < 0 || u > 1)
278 return std::make_tuple(false, 0, 0);
279 return std::make_tuple(true, x1 + t * (x2 - x1), y1 + t * (y2 - y1));