]> rtime.felk.cvut.cz Git - hubacji1/bcar.git/blob - src/bcar.cc
Inherit pose from point
[hubacji1/bcar.git] / src / bcar.cc
1 #include <cmath>
2 #include "bcar.hh"
3
4 using namespace bcar;
5
6 Point::Point(double x, double y) : x_(x), y_(y)
7 {
8 }
9
10 Point::Point() : Point::Point(0.0, 0.0)
11 {
12 }
13
14 double
15 Point::x() const
16 {
17         return this->x_;
18 }
19
20 void
21 Point::x(double x)
22 {
23         this->x_ = x;
24 }
25
26 double
27 Point::y() const
28 {
29         return this->y_;
30 }
31
32 void
33 Point::y(double y)
34 {
35         this->y_ = y;
36 }
37
38 double
39 Point::min_angle_between(Point const& p1, Point const& p2) const
40 {
41         double d1x = p1.x() - this->x();
42         double d1y = p1.y() - this->y();
43         double d2x = p2.x() - p1.x();
44         double d2y = p2.y() - p1.y();
45
46         double dot = d1x*d2x + d1y*d2y;
47         double d1 = sqrt(d1x*d1x + d1y*d1y);
48         double d2 = sqrt(d2x*d2x + d2y*d2y);
49
50         double delta = acos(dot / (d1 * d2));
51         return std::min(delta, M_PI - delta);
52 }
53
54 bool
55 Point::inside_of(std::vector<Point> const& poly) const
56 {
57         unsigned int num = poly.size();
58         unsigned int j = num - 1;
59         bool c = false;
60         for (unsigned int i = 0; i < num; i++) {
61                 if (this->x() == poly[i].x() && this->y() == poly[i].y()) {
62                         return true;
63                 }
64                 if ((poly[i].y() > this->y()) != (poly[j].y() > this->y())) {
65                         auto slope1 = this->x() - poly[i].x();
66                         slope1 *= poly[j].y() - poly[i].y();
67                         auto slope2 = poly[j].x() - poly[i].x();
68                         slope2 *= this->y() - poly[i].y();
69                         auto slope = slope1 - slope2;
70                         if (slope == 0.0) {
71                                 return true;
72                         }
73                         if ((slope < 0.0) != (poly[j].y() < poly[i].y())) {
74                                 c = !c;
75                         }
76                 }
77                 j = i;
78         }
79         return c;
80 }
81
82 Line::Line(Point const& fp, Point const& lp): first(fp), last(lp),
83                 intersection1(Point(0.0, 0.0)), intersection2(Point(0.0, 0.0))
84 {
85 }
86
87 Point
88 Line::fp() const&
89 {
90         return this->first;
91 }
92
93 Point
94 Line::lp() const&
95 {
96         return this->last;
97 }
98
99 Point
100 Line::in1() const&
101 {
102         return this->intersection1;
103 }
104
105 Point
106 Line::in2() const&
107 {
108         return this->intersection2;
109 }
110
111 bool
112 Line::intersects_with(Line const& li)
113 {
114         auto x1 = this->fp().x();
115         auto y1 = this->fp().y();
116         auto x2 = this->lp().x();
117         auto y2 = this->lp().y();
118         auto x3 = li.fp().x();
119         auto y3 = li.fp().y();
120         auto x4 = li.lp().x();
121         auto y4 = li.lp().y();
122         double deno = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
123         if (deno == 0.0) {
124                 return false;
125         }
126         double t = (x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4);
127         t /= deno;
128         double u = (x1 - x2) * (y1 - y3) - (y1 - y2) * (x1 - x3);
129         u *= -1.0;
130         u /= deno;
131         if (t < 0.0 || t > 1.0 || u < 0.0 || u > 1.0) {
132                 false;
133         }
134         this->intersection1.x(x1 + t * (x2 - x1));
135         this->intersection1.y(y1 + t * (y2 - y1));
136         return true;
137 }
138
139 bool
140 Line::intersects_with(Point const& c, double const r)
141 {
142         auto x1 = this->fp().x();
143         auto y1 = this->fp().y();
144         auto x2 = this->lp().x();
145         auto y2 = this->lp().y();
146         auto cx = c.x();
147         auto cy = c.y();
148         x2 -= cx;
149         x1 -= cx;
150         y2 -= cy;
151         y1 -= cy;
152         if (y1 == y2) {
153                 y1 += 0.00001;
154         }
155         double dx = x2 - x1;
156         double dy = y2 - y1;
157         double dr = sqrt(dx*dx + dy*dy);
158         double D = x1*y2 - x2*y1;
159         if (r*r * dr*dr - D*D < 0.0) {
160                 return false;
161         }
162         // intersection coordinates
163         double ix1 = (D*dy + sgn(dy)*dx*sqrt(r*r * dr*dr - D*D)) / (dr*dr);
164         ix1 += cx;
165         double ix2 = (D*dy - sgn(dy)*dx*sqrt(r*r * dr*dr - D*D)) / (dr*dr);
166         ix2 += cx;
167         double iy1 = (-D*dx + std::abs(dy)*sqrt(r*r * dr*dr - D*D)) / (dr*dr);
168         iy1 += cy;
169         double iy2 = (-D*dx - std::abs(dy)*sqrt(r*r * dr*dr - D*D)) / (dr*dr);
170         iy2 += cy;
171         this->intersection1.x(ix1);
172         this->intersection1.y(iy1);
173         this->intersection2.x(ix2);
174         this->intersection2.y(iy2);
175         return true;
176 }
177
178 bool
179 Line::is_on_right_side(Point const& p) const
180 {
181         auto x1 = this->fp().x();
182         auto y1 = this->fp().y();
183         auto x2 = this->lp().x();
184         auto y2 = this->lp().y();
185         auto x3 = p.x();
186         auto y3 = p.y();
187         if (sgn((x3 - x1) * (y2 - y1) - (y3 - y1) * (x2 - x1)) < 0.0) {
188                 return false;
189         } else {
190                 return true;
191         }
192 }
193
194 Pose::Pose() : Point()
195 {
196 }
197
198 Pose::Pose(double x, double y, double h) : Point(x, y), h_(h)
199 {
200 }
201
202 double
203 Pose::h() const
204 {
205         return this->h_;
206 }
207
208 void
209 Pose::h(double h)
210 {
211         while (h < -M_PI) {
212                 h += 2 * M_PI;
213         }
214         while (h > +M_PI) {
215                 h -= 2 * M_PI;
216         }
217         this->h_ = h;
218 }
219
220 void
221 Pose::set_pose(Pose const& p)
222 {
223         this->x(p.x());
224         this->y(p.y());
225         this->h(p.h());
226 }
227
228 void
229 Pose::rotate(Point const& c, double const angl)
230 {
231         double px = this->x();
232         double py = this->y();
233         px -= c.x();
234         py -= c.y();
235         double nx = px * cos(angl) - py * sin(angl);
236         double ny = px * sin(angl) + py * cos(angl);
237         this->h(this->h() + angl);
238         this->x(nx + c.x());
239         this->y(ny + c.y());
240 }
241
242 std::ostream&
243 operator<<(std::ostream& out, Pose const& p)
244 {
245         out << "[" << p.x() << "," << p.y() << "," << p.h() << "]";
246         return out;
247 }
248
249 double
250 PoseRange::b() const
251 {
252         return this->h();
253 }
254
255 void
256 PoseRange::b(double b)
257 {
258         this->h(b);
259 }
260
261 double
262 PoseRange::e() const
263 {
264         return this->e_;
265 }
266
267 void
268 PoseRange::e(double e)
269 {
270         while (e < -M_PI) {
271                 e += 2 * M_PI;
272         }
273         while (e > +M_PI) {
274                 e -= 2 * M_PI;
275         }
276         this->e_ = e;
277 }
278
279 void
280 PoseRange::rotate(Point const& c, double const angl)
281 {
282         Pose::rotate(c, angl);
283         this->e(this->e() + angl);
284 }
285
286 std::ostream&
287 operator<<(std::ostream& out, PoseRange const& p)
288 {
289         out << "[" << p.x() << "," << p.y() << "," << p.b() << "," << p.e();
290         out << "]";
291         return out;
292 }
293
294 double
295 CarSize::ctc() const
296 {
297         return this->curb_to_curb;
298 }
299
300 void
301 CarSize::ctc(double ctc)
302 {
303         this->curb_to_curb = ctc;
304 }
305
306 double
307 CarSize::wb() const
308 {
309         return this->wheelbase;
310 }
311
312 void
313 CarSize::wb(double wb)
314 {
315         this->wheelbase = wb;
316 }
317
318 double
319 CarSize::w() const
320 {
321         return this->width;
322 }
323
324 void
325 CarSize::w(double w)
326 {
327         this->width = w;
328 }
329
330 double
331 CarSize::len() const
332 {
333         return this->length;
334 }
335
336 void
337 CarSize::len(double len)
338 {
339         this->length = len;
340 }
341
342 double
343 CarSize::df() const
344 {
345         return this->distance_to_front;
346 }
347
348 void
349 CarSize::df(double df)
350 {
351         this->distance_to_front = df;
352 }
353
354 double
355 CarSize::dr() const
356 {
357         return this->len() - this->df();
358 }
359
360 double
361 CarSize::mtr() const
362 {
363         auto ctc2 = pow(this->ctc() / 2.0, 2.0);
364         auto wb2 = pow(this->wb(), 2.0);
365         return sqrt(ctc2 - wb2) - this->w() / 2.0;
366 }
367
368 double
369 CarSize::iradi() const
370 {
371         return this->mtr() - this->w() / 2;
372 }
373
374 double
375 CarSize::ofradi() const
376 {
377         auto mtrw2 = pow(this->mtr() + this->w() / 2.0, 2.0);
378         auto df2 = pow(this->df(), 2.0);
379         return sqrt(mtrw2 + df2);
380 }
381
382 double
383 CarSize::orradi() const
384 {
385         auto mtrw2 = pow(this->mtr() + this->w() / 2.0, 2.0);
386         auto dr2 = pow(this->dr(), 2.0);
387         return sqrt(mtrw2 + dr2);
388 }
389
390 double
391 CarSize::perfect_parking_slot_len() const
392 {
393         auto r = this->ctc() / 2.0;
394         auto l = this->wb();
395         auto k = this->df() - this->wb();
396         auto w = this->w();
397         auto r2l2 = r * r - l * l;
398         auto s = r2l2 + pow(l + k, 2.0) - pow(sqrt(r2l2) - w, 2.0);
399         return this->len() + sqrt(s) - l - k;
400 }
401
402 double
403 CarMove::sp() const
404 {
405         return this->speed;
406 }
407
408 void
409 CarMove::sp(double sp)
410 {
411         this->speed = sp;
412 }
413
414 double
415 CarMove::st() const
416 {
417         return this->steer;
418 }
419
420 void
421 CarMove::st(double st)
422 {
423         this->steer = st;
424 }
425
426 bool
427 BicycleCar::drivable(Pose const& p) const
428 {
429         PoseRange pr;
430         pr.x(p.x());
431         pr.y(p.y());
432         pr.b(p.h());
433         pr.e(p.h());
434         return this->drivable(pr);
435 }
436
437 bool
438 BicycleCar::drivable(PoseRange const& p) const
439 {
440         double h = (p.b() + p.e()) / 2.0;
441         double a_1 = atan2(p.y() - this->y(), p.x() - this->x()) - this->h();
442         while (a_1 < -M_PI)
443                 a_1 += 2 * M_PI;
444         while (a_1 > +M_PI)
445                 a_1 -= 2 * M_PI;
446         double h_d = h - this->h();
447         while (h_d < -M_PI)
448                 h_d += 2 * M_PI;
449         while (h_d > +M_PI)
450                 h_d -= 2 * M_PI;
451         double a_2 = 0;
452         if (h_d == 0 && (a_1 == 0 || a_2 == M_PI || a_2 == -M_PI)) {
453                 return true;
454         } else if (0 < a_1 && a_1 <= M_PI/2) { // left front
455                 BicycleCar z(*this); // zone border
456                 z.h(p.e());
457                 h_d = h - this->h();
458                 z.rotate(this->ccl(), h_d);
459                 // assert z.h() == h
460                 if (p.y() == z.y() && p.x() == z.x()) // p on zone border
461                         return true;
462                 a_2 = atan2(p.y() - z.y(), p.x() - z.x());
463                 while (a_2 < -M_PI)
464                         a_2 += 2 * M_PI;
465                 while (a_2 > +M_PI)
466                         a_2 -= 2 * M_PI;
467                 if (z.h() >= a_2 && a_2 >= this->h())
468                         return true;
469         } else if (M_PI/2 < a_1 && a_1 <= M_PI) { // left rear
470                 BicycleCar z(*this); // zone border
471                 z.h(p.e());
472                 h_d = h - this->h();
473                 z.rotate(this->ccl(), h_d);
474                 // assert z.h() == h
475                 if (p.y() == z.y() && p.x() == z.x()) // p on zone border
476                         return true;
477                 a_2 = atan2(p.y() - z.y(), p.x() - z.x());
478                 a_2 -= M_PI;
479                 while (a_2 < -M_PI)
480                         a_2 += 2 * M_PI;
481                 while (a_2 > +M_PI)
482                         a_2 -= 2 * M_PI;
483                 if (this->h() >= a_2 && a_2 >= z.h())
484                         return true;
485         } else if (0 > a_1 && a_1 >= -M_PI/2) { // right front
486                 BicycleCar z(*this); // zone border
487                 z.h(p.b());
488                 h_d = h - this->h();
489                 z.rotate(this->ccr(), h_d);
490                 // assert z.h() == h
491                 if (p.y() == z.y() && p.x() == z.x()) // p on zone border
492                         return true;
493                 a_2 = atan2(p.y() - z.y(), p.x() - z.x());
494                 while (a_2 < -M_PI)
495                         a_2 += 2 * M_PI;
496                 while (a_2 > +M_PI)
497                         a_2 -= 2 * M_PI;
498                 if (this->h() >= a_2 && a_2 >= z.h())
499                         return true;
500         } else if (-M_PI/2 > a_1 && a_1 >= -M_PI) { // right rear
501                 BicycleCar z(*this); // zone border
502                 z.h(p.b());
503                 h_d = h - this->h();
504                 z.rotate(this->ccr(), h_d);
505                 // assert z.h() == h
506                 if (p.y() == z.y() && p.x() == z.x()) // p on zone border
507                         return true;
508                 a_2 = atan2(p.y() - z.y(), p.x() - z.x());
509                 a_2 -= M_PI;
510                 while (a_2 < -M_PI)
511                         a_2 += 2 * M_PI;
512                 while (a_2 > +M_PI)
513                         a_2 -= 2 * M_PI;
514                 if (z.h() >= a_2 && a_2 >= this->h())
515                         return true;
516         } else {
517                 // Not happenning, as ``-pi <= a <= pi``.
518         }
519         return false;
520 }
521
522 void
523 BicycleCar::set_max_steer()
524 {
525         this->st(atan(this->wb() / this->mtr()));
526 }
527
528 double
529 BicycleCar::lfx() const
530 {
531         double lfx = this->x();
532         lfx += (this->w() / 2.0) * cos(this->h() + M_PI / 2.0);
533         lfx += this->df() * cos(this->h());
534         return lfx;
535 }
536
537 double
538 BicycleCar::lfy() const
539 {
540         double lfy = this->y();
541         lfy += (this->w() / 2.0) * sin(this->h() + M_PI / 2.0);
542         lfy += this->df() * sin(this->h());
543         return lfy;
544 }
545
546 double
547 BicycleCar::lrx() const
548 {
549         double lrx = this->x();
550         lrx += (this->w() / 2.0) * cos(this->h() + M_PI / 2.0);
551         lrx += -this->dr() * cos(this->h());
552         return lrx;
553 }
554
555 double
556 BicycleCar::lry() const
557 {
558         double lry = this->y();
559         lry += (this->w() / 2.0) * sin(this->h() + M_PI / 2.0);
560         lry += -this->dr() * sin(this->h());
561         return lry;
562 }
563
564 double
565 BicycleCar::rrx() const
566 {
567         double rrx = this->x();
568         rrx += (this->w() / 2.0) * cos(this->h() - M_PI / 2.0);
569         rrx += -this->dr() * cos(this->h());
570         return rrx;
571 }
572
573 double
574 BicycleCar::rry() const
575 {
576         double rry = this->y();
577         rry += (this->w() / 2.0) * sin(this->h() - M_PI / 2.0);
578         rry += -this->dr() * sin(this->h());
579         return rry;
580 }
581
582 double
583 BicycleCar::rfx() const
584 {
585         double rfx = this->x();
586         rfx += (this->w() / 2.0) * cos(this->h() - M_PI / 2.0);
587         rfx += this->df() * cos(this->h());
588         return rfx;
589 }
590
591 double
592 BicycleCar::rfy() const
593 {
594         double rfy = this->y();
595         rfy += (this->w() / 2.0) * sin(this->h() - M_PI / 2.0);
596         rfy += this->df() * sin(this->h());
597         return rfy;
598 }
599
600 double
601 BicycleCar::ralx() const
602 {
603         double lrx = this->x();
604         lrx += (this->w() / 2.0) * cos(this->h() + M_PI / 2.0);
605         return lrx;
606 }
607 double
608 BicycleCar::raly() const
609 {
610         double lry = this->y();
611         lry += (this->w() / 2.0) * sin(this->h() + M_PI / 2.0);
612         return lry;
613 }
614
615 double
616 BicycleCar::rarx() const
617 {
618         double rrx = this->x();
619         rrx += (this->w() / 2.0) * cos(this->h() - M_PI / 2.0);
620         return rrx;
621 }
622
623 double
624 BicycleCar::rary() const
625 {
626         double rry = this->y();
627         rry += (this->w() / 2.0) * sin(this->h() - M_PI / 2.0);
628         return rry;
629 }
630
631 Point
632 BicycleCar::ccl() const
633 {
634         return Point(
635                 this->x() + this->mtr() * cos(this->h() + M_PI / 2.0),
636                 this->y() + this->mtr() * sin(this->h() + M_PI / 2.0)
637         );
638 }
639
640 Point
641 BicycleCar::ccr() const
642 {
643         return Point(
644                 this->x() + this->mtr() * cos(this->h() - M_PI / 2.0),
645                 this->y() + this->mtr() * sin(this->h() - M_PI / 2.0)
646         );
647 }
648
649 void
650 BicycleCar::next()
651 {
652         this->x(this->x() + this->sp() * cos(this->h()));
653         this->y(this->y() + this->sp() * sin(this->h()));
654         this->h(this->h() + this->sp() / this->wb() * tan(this->st()));
655 }