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 void BicycleCar::set_max_steer()
39 this->st(asin(this->wb() / (10.82 / 2)));
43 double BicycleCar::lfx() const
45 double lfx = this->x();
46 lfx += (this->w() / 2) * cos(this->h() + M_PI / 2);
47 lfx += this->df() * cos(this->h());
48 lfx += this->sd() * cos(this->h());
52 double BicycleCar::lfy() const
54 double lfy = this->y();
55 lfy += (this->w() / 2) * sin(this->h() + M_PI / 2);
56 lfy += this->df() * sin(this->h());
57 lfy += this->sd() * sin(this->h());
61 double BicycleCar::lrx() const
63 double lrx = this->x();
64 lrx += (this->w() / 2) * cos(this->h() + M_PI / 2);
65 lrx += -this->dr() * cos(this->h());
66 lrx += -this->sd() * cos(this->h());
70 double BicycleCar::lry() const
72 double lry = this->y();
73 lry += (this->w() / 2) * sin(this->h() + M_PI / 2);
74 lry += -this->dr() * sin(this->h());
75 lry += -this->sd() * sin(this->h());
79 double BicycleCar::rrx() const
81 double rrx = this->x();
82 rrx += (this->w() / 2) * cos(this->h() - M_PI / 2);
83 rrx += -this->dr() * cos(this->h());
84 rrx += -this->sd() * cos(this->h());
88 double BicycleCar::rry() const
90 double rry = this->y();
91 rry += (this->w() / 2) * sin(this->h() - M_PI / 2);
92 rry += -this->dr() * sin(this->h());
93 rry += -this->sd() * sin(this->h());
97 double BicycleCar::rfx() const
99 double rfx = this->x();
100 rfx += (this->w() / 2) * cos(this->h() - M_PI / 2);
101 rfx += this->df() * cos(this->h());
102 rfx += this->sd() * cos(this->h());
106 double BicycleCar::rfy() const
108 double rfy = this->y();
109 rfy += (this->w() / 2) * sin(this->h() - M_PI / 2);
110 rfy += this->df() * sin(this->h());
111 rfy += this->sd() * sin(this->h());
115 double BicycleCar::ralx() const
117 double lrx = this->x();
118 lrx += (this->w() / 2) * cos(this->h() + M_PI / 2);
121 double BicycleCar::raly() const
123 double lry = this->y();
124 lry += (this->w() / 2) * sin(this->h() + M_PI / 2);
128 double BicycleCar::rarx() const
130 double rrx = this->x();
131 rrx += (this->w() / 2) * cos(this->h() - M_PI / 2);
135 double BicycleCar::rary() const
137 double rry = this->y();
138 rry += (this->w() / 2) * sin(this->h() - M_PI / 2);
142 BicycleCar BicycleCar::ccl() const
145 bc.x(this->x() + this->mtr() * cos(this->h() + M_PI / 2));
146 bc.y(this->y() + this->mtr() * sin(this->h() + M_PI / 2));
151 BicycleCar BicycleCar::ccr() const
154 bc.x(this->x() + this->mtr() * cos(this->h() - M_PI / 2));
155 bc.y(this->y() + this->mtr() * sin(this->h() - M_PI / 2));
161 void BicycleCar::next()
164 //if (this->st() > this->wb() / this->mtr())
165 // this->st(this->wb() / this->mtr());
166 //if (this->st() < -this->wb() / this->mtr())
167 // this->st(-this->wb() / this->mtr());
168 this->h(this->h() + this->sp() / this->wb() * tan(this->st()));
169 this->x(this->x() + this->sp() * cos(this->h()));
170 this->y(this->y() + this->sp() * sin(this->h()));
173 void BicycleCar::rotate(double cx, double cy, double angl)
175 double px = this->x();
176 double py = this->y();
179 double nx = px * cos(angl) - py * sin(angl);
180 double ny = px * sin(angl) + py * cos(angl);
181 this->h(this->h() + angl);
186 BicycleCar::BicycleCar()
188 // TODO according to mtr_ FIXME
197 std::tuple<bool, unsigned int, unsigned int> collide(
198 std::vector<std::tuple<double, double>> &p1,
199 std::vector<std::tuple<double, double>> &p2
202 for (unsigned int i = 0; i < p1.size() - 1; i++) {
203 for (unsigned int j = 0; j < p2.size() - 1; j++) {
207 std::get<0>(p1[i + 1]),
208 std::get<1>(p1[i + 1]),
211 std::get<0>(p2[j + 1]),
212 std::get<1>(p2[j + 1])
215 return std::make_tuple(true, i, j);
218 return std::make_tuple(false, 0, 0);
221 bool inside(double x, double y, std::vector<std::tuple<double, double>> &poly)
226 for (i = 0; i < 4; i++) {
228 (std::get<1>(poly[i]) > y) != (std::get<1>(poly[j]) > y)
230 x < std::get<0>(poly[i])
231 + (std::get<0>(poly[j]) - std::get<0>(poly[i]))
232 * (y - std::get<1>(poly[i]))
233 / (std::get<1>(poly[j]) - std::get<1>(poly[i]))
242 std::tuple<bool, double, double> intersect(
243 double x1, double y1,
244 double x2, double y2,
245 double x3, double y3,
249 double deno = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
251 return std::make_tuple(false, 0, 0);
252 double t = (x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4);
254 double u = (x1 - x2) * (y1 - y3) - (y1 - y2) * (x1 - x3);
257 if (t < 0 || t > 1 || u < 0 || u > 1)
258 return std::make_tuple(false, 0, 0);
259 return std::make_tuple(true, x1 + t * (x2 - x1), y1 + t * (y2 - y1));