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));
38 double BicycleCar::lfx() const
40 double lfx = this->x();
41 lfx += (this->w() / 2) * cos(this->h() + M_PI / 2);
42 lfx += this->df() * cos(this->h());
43 lfx += this->sd() * cos(this->h());
47 double BicycleCar::lfy() const
49 double lfy = this->y();
50 lfy += (this->w() / 2) * sin(this->h() + M_PI / 2);
51 lfy += this->df() * sin(this->h());
52 lfy += this->sd() * sin(this->h());
56 double BicycleCar::lrx() const
58 double lrx = this->x();
59 lrx += (this->w() / 2) * cos(this->h() + M_PI / 2);
60 lrx += -this->dr() * cos(this->h());
61 lrx += -this->sd() * cos(this->h());
65 double BicycleCar::lry() const
67 double lry = this->y();
68 lry += (this->w() / 2) * sin(this->h() + M_PI / 2);
69 lry += -this->dr() * sin(this->h());
70 lry += -this->sd() * sin(this->h());
74 double BicycleCar::rrx() const
76 double rrx = this->x();
77 rrx += (this->w() / 2) * cos(this->h() - M_PI / 2);
78 rrx += -this->dr() * cos(this->h());
79 rrx += -this->sd() * cos(this->h());
83 double BicycleCar::rry() const
85 double rry = this->y();
86 rry += (this->w() / 2) * sin(this->h() - M_PI / 2);
87 rry += -this->dr() * sin(this->h());
88 rry += -this->sd() * sin(this->h());
92 double BicycleCar::rfx() const
94 double rfx = this->x();
95 rfx += (this->w() / 2) * cos(this->h() - M_PI / 2);
96 rfx += this->df() * cos(this->h());
97 rfx += this->sd() * cos(this->h());
101 double BicycleCar::rfy() const
103 double rfy = this->y();
104 rfy += (this->w() / 2) * sin(this->h() - M_PI / 2);
105 rfy += this->df() * sin(this->h());
106 rfy += this->sd() * sin(this->h());
110 double BicycleCar::ralx() const
112 double lrx = this->x();
113 lrx += (this->w() / 2) * cos(this->h() + M_PI / 2);
116 double BicycleCar::raly() const
118 double lry = this->y();
119 lry += (this->w() / 2) * sin(this->h() + M_PI / 2);
123 double BicycleCar::rarx() const
125 double rrx = this->x();
126 rrx += (this->w() / 2) * cos(this->h() - M_PI / 2);
130 double BicycleCar::rary() const
132 double rry = this->y();
133 rry += (this->w() / 2) * sin(this->h() - M_PI / 2);
137 BicycleCar BicycleCar::ccl() const
140 bc.x(this->x() + this->mtr() * cos(this->h() + M_PI / 2));
141 bc.y(this->y() + this->mtr() * sin(this->h() + M_PI / 2));
146 BicycleCar BicycleCar::ccr() const
149 bc.x(this->x() + this->mtr() * cos(this->h() - M_PI / 2));
150 bc.y(this->y() + this->mtr() * sin(this->h() - M_PI / 2));
156 void BicycleCar::next()
158 if (this->st() > this->wb() / this->mtr())
159 this->st(this->wb() / this->mtr());
160 if (this->st() < -this->wb() / this->mtr())
161 this->st(-this->wb() / this->mtr());
162 this->h(this->h() + this->sp() / this->wb() * tan(this->st()));
163 this->x(this->x() + this->sp() * cos(this->h()));
164 this->y(this->y() + this->sp() * sin(this->h()));
167 void BicycleCar::rotate(double cx, double cy, double angl)
169 double px = this->x();
170 double py = this->y();
173 double nx = px * cos(angl) - py * sin(angl);
174 double ny = px * sin(angl) + py * cos(angl);
175 this->h(this->h() + angl);
180 BicycleCar::BicycleCar()
182 // TODO according to mtr_ FIXME
191 std::tuple<bool, unsigned int, unsigned int> collide(
192 std::vector<std::tuple<double, double>> &p1,
193 std::vector<std::tuple<double, double>> &p2
196 for (unsigned int i = 0; i < p1.size() - 1; i++) {
197 for (unsigned int j = 0; j < p2.size() - 1; j++) {
201 std::get<0>(p1[i + 1]),
202 std::get<1>(p1[i + 1]),
205 std::get<0>(p2[j + 1]),
206 std::get<1>(p2[j + 1])
209 return std::make_tuple(true, i, j);
212 return std::make_tuple(false, 0, 0);
215 bool inside(double x, double y, std::vector<std::tuple<double, double>> &poly)
220 for (i = 0; i < 4; i++) {
222 (std::get<1>(poly[i]) > y) != (std::get<1>(poly[j]) > y)
224 x < std::get<0>(poly[i])
225 + (std::get<0>(poly[j]) - std::get<0>(poly[i]))
226 * (y - std::get<1>(poly[i]))
227 / (std::get<1>(poly[j]) - std::get<1>(poly[i]))
236 std::tuple<bool, double, double> intersect(
237 double x1, double y1,
238 double x2, double y2,
239 double x3, double y3,
243 double deno = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
245 return std::make_tuple(false, 0, 0);
246 double t = (x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4);
248 double u = (x1 - x2) * (y1 - y3) - (y1 - y2) * (x1 - x3);
251 if (t < 0 || t > 1 || u < 0 || u > 1)
252 return std::make_tuple(false, 0, 0);
253 return std::make_tuple(true, x1 + t * (x2 - x1), y1 + t * (y2 - y1));