]> rtime.felk.cvut.cz Git - hubacji1/bcar.git/blob - src/bcar.cc
Add operator== for pose
[hubacji1/bcar.git] / src / bcar.cc
1 #include <cmath>
2 #include "bcar.hh"
3
4 namespace bcar {
5
6 Point::Point()
7 {
8 }
9
10 Point::Point(double x, double y) : x_(x), y_(y)
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 bool
83 Point::on_right_side_of(Line const& li) const
84 {
85         auto x1 = li.b().x();
86         auto y1 = li.b().y();
87         auto x2 = li.e().x();
88         auto y2 = li.e().y();
89         auto x3 = this->x_;
90         auto y3 = this->y_;
91         if (sgn((x3 - x1) * (y2 - y1) - (y3 - y1) * (x2 - x1)) < 0.0) {
92                 return false;
93         } else {
94                 return true;
95         }
96 }
97
98 void
99 Point::rotate(Point const& c, double const angl)
100 {
101         double px = this->x();
102         double py = this->y();
103         px -= c.x();
104         py -= c.y();
105         double nx = px * cos(angl) - py * sin(angl);
106         double ny = px * sin(angl) + py * cos(angl);
107         this->x(nx + c.x());
108         this->y(ny + c.y());
109 }
110
111 void
112 Point::reflect(Line const& li)
113 {
114         this->rotate(li.b(), -li.h());
115         this->y_ -= li.b().y();
116         this->y_ *= -1.0;
117         this->y_ += li.b().y();
118         this->rotate(li.b(), li.h());
119 }
120
121 double
122 Point::edist(Point const& p) const
123 {
124         return sqrt(pow(p.x() - this->x_, 2.0) + pow(p.y() - this->y_, 2.0));
125 }
126
127 bool
128 Point::operator==(Point const& p)
129 {
130         return this->x() == p.x() && this->y() == p.y();
131 }
132
133 std::ostream&
134 operator<<(std::ostream& out, Point const& p)
135 {
136         out << "[" << p.x() << "," << p.y() << "]";
137         return out;
138 }
139
140 Line::Line(Point const& b, Point const& e): b_(b), e_(e)
141 {
142 }
143
144 Point
145 Line::b() const&
146 {
147         return this->b_;
148 }
149
150 Point
151 Line::e() const&
152 {
153         return this->e_;
154 }
155
156 Point
157 Line::i1() const&
158 {
159         return this->i1_;
160 }
161
162 Point
163 Line::i2() const&
164 {
165         return this->i2_;
166 }
167
168 bool
169 Line::intersects_with(Line const& li)
170 {
171         auto x1 = this->b_.x();
172         auto y1 = this->b_.y();
173         auto x2 = this->e_.x();
174         auto y2 = this->e_.y();
175         auto x3 = li.b().x();
176         auto y3 = li.b().y();
177         auto x4 = li.e().x();
178         auto y4 = li.e().y();
179         double deno = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
180         if (deno == 0.0) {
181                 return false;
182         }
183         double t = (x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4);
184         t /= deno;
185         double u = (x1 - x2) * (y1 - y3) - (y1 - y2) * (x1 - x3);
186         u *= -1.0;
187         u /= deno;
188         if (t < 0.0 || t > 1.0 || u < 0.0 || u > 1.0) {
189                 return false;
190         }
191         this->i1_.x(x1 + t * (x2 - x1));
192         this->i1_.y(y1 + t * (y2 - y1));
193         return true;
194 }
195
196 bool
197 Line::intersects_with(Point const& c, double const r)
198 {
199         auto x1 = this->b_.x();
200         auto y1 = this->b_.y();
201         auto x2 = this->e_.x();
202         auto y2 = this->e_.y();
203         auto cx = c.x();
204         auto cy = c.y();
205         x2 -= cx;
206         x1 -= cx;
207         y2 -= cy;
208         y1 -= cy;
209         if (y1 == y2) {
210                 y1 += 0.00001;
211         }
212         double dx = x2 - x1;
213         double dy = y2 - y1;
214         double dr = sqrt(dx*dx + dy*dy);
215         double D = x1*y2 - x2*y1;
216         if (r*r * dr*dr - D*D < 0.0) {
217                 return false;
218         }
219         // intersection coordinates
220         double ix1 = (D*dy + sgn(dy)*dx*sqrt(r*r * dr*dr - D*D)) / (dr*dr);
221         ix1 += cx;
222         double ix2 = (D*dy - sgn(dy)*dx*sqrt(r*r * dr*dr - D*D)) / (dr*dr);
223         ix2 += cx;
224         double iy1 = (-D*dx + std::abs(dy)*sqrt(r*r * dr*dr - D*D)) / (dr*dr);
225         iy1 += cy;
226         double iy2 = (-D*dx - std::abs(dy)*sqrt(r*r * dr*dr - D*D)) / (dr*dr);
227         iy2 += cy;
228         this->i1_.x(ix1);
229         this->i1_.y(iy1);
230         this->i2_.x(ix2);
231         this->i2_.y(iy2);
232         return true;
233 }
234
235 double
236 Line::len() const
237 {
238         return this->b_.edist(this->e_);
239 }
240
241 double
242 Line::h() const
243 {
244         return atan2(this->e_.y() - this->b_.y(), this->e_.x() - this->b_.x());
245 }
246
247 std::ostream&
248 operator<<(std::ostream& out, Line const& li)
249 {
250         out << "[" << li.b_ << "," << li.e_ << "]";
251         return out;
252 }
253
254 Pose::Pose(double x, double y, double h) : Point(x, y), h_(h)
255 {
256 }
257
258 double
259 Pose::h() const
260 {
261         return this->h_;
262 }
263
264 void
265 Pose::h(double h)
266 {
267         while (h < -M_PI) {
268                 h += 2 * M_PI;
269         }
270         while (h > +M_PI) {
271                 h -= 2 * M_PI;
272         }
273         this->h_ = h;
274 }
275
276 void
277 Pose::set_pose(Pose const& p)
278 {
279         this->x(p.x());
280         this->y(p.y());
281         this->h(p.h());
282 }
283
284 void
285 Pose::rotate(Point const& c, double const angl)
286 {
287         Point::rotate(c, angl);
288         this->h(this->h() + angl);
289 }
290
291 void
292 Pose::reflect(Line const& li)
293 {
294         Point::reflect(li);
295         double dh = li.h() - this->h();
296         this->h(this->h() + 2.0 * dh);
297 }
298
299 bool
300 Pose::operator==(Pose const& p)
301 {
302         return this->x() == p.x() && this->y() == p.y() && this->h() == p.h();
303 }
304
305 std::ostream&
306 operator<<(std::ostream& out, Pose const& p)
307 {
308         out << "[" << p.x() << "," << p.y() << "," << p.h() << "]";
309         return out;
310 }
311
312 double
313 PoseRange::b() const
314 {
315         return this->h();
316 }
317
318 void
319 PoseRange::b(double b)
320 {
321         this->h(b);
322 }
323
324 double
325 PoseRange::e() const
326 {
327         return this->e_;
328 }
329
330 void
331 PoseRange::e(double e)
332 {
333         while (e < -M_PI) {
334                 e += 2 * M_PI;
335         }
336         while (e > +M_PI) {
337                 e -= 2 * M_PI;
338         }
339         this->e_ = e;
340 }
341
342 void
343 PoseRange::rotate(Point const& c, double const angl)
344 {
345         Pose::rotate(c, angl);
346         this->e(this->e() + angl);
347 }
348
349 void
350 PoseRange::reflect(Line const& li)
351 {
352         Pose::reflect(li);
353         double dh = li.h() - this->e();
354         this->e(this->e() + 2.0 * dh);
355 }
356
357 std::ostream&
358 operator<<(std::ostream& out, PoseRange const& p)
359 {
360         out << "[" << p.x() << "," << p.y() << "," << p.b() << "," << p.e();
361         out << "]";
362         return out;
363 }
364
365 double
366 CarSize::ctc() const
367 {
368         return this->curb_to_curb_;
369 }
370
371 void
372 CarSize::ctc(double ctc)
373 {
374         this->curb_to_curb_ = ctc;
375 }
376
377 double
378 CarSize::wb() const
379 {
380         return this->wheelbase_;
381 }
382
383 void
384 CarSize::wb(double wb)
385 {
386         this->wheelbase_ = wb;
387 }
388
389 double
390 CarSize::w() const
391 {
392         return this->width_;
393 }
394
395 void
396 CarSize::w(double w)
397 {
398         this->width_ = w;
399 }
400
401 double
402 CarSize::len() const
403 {
404         return this->length_;
405 }
406
407 void
408 CarSize::len(double len)
409 {
410         this->length_ = len;
411 }
412
413 double
414 CarSize::df() const
415 {
416         return this->distance_to_front_;
417 }
418
419 void
420 CarSize::df(double df)
421 {
422         this->distance_to_front_ = df;
423 }
424
425 double
426 CarSize::dr() const
427 {
428         return this->len() - this->df();
429 }
430
431 double
432 CarSize::mtr() const
433 {
434         auto ctc2 = pow(this->ctc() / 2.0, 2.0);
435         auto wb2 = pow(this->wb(), 2.0);
436         return sqrt(ctc2 - wb2) - this->w() / 2.0;
437 }
438
439 double
440 CarSize::iradi() const
441 {
442         return this->mtr() - this->w() / 2;
443 }
444
445 double
446 CarSize::ofradi() const
447 {
448         auto mtrw2 = pow(this->mtr() + this->w() / 2.0, 2.0);
449         auto df2 = pow(this->df(), 2.0);
450         return sqrt(mtrw2 + df2);
451 }
452
453 double
454 CarSize::orradi() const
455 {
456         auto mtrw2 = pow(this->mtr() + this->w() / 2.0, 2.0);
457         auto dr2 = pow(this->dr(), 2.0);
458         return sqrt(mtrw2 + dr2);
459 }
460
461 double
462 CarSize::perfect_parking_slot_len() const
463 {
464         auto r = this->ctc() / 2.0;
465         auto l = this->wb();
466         auto k = this->df() - this->wb();
467         auto w = this->w();
468         auto r2l2 = r * r - l * l;
469         auto s = r2l2 + pow(l + k, 2.0) - pow(sqrt(r2l2) - w, 2.0);
470         return this->len() + sqrt(s) - l - k;
471 }
472
473 double
474 CarMove::sp() const
475 {
476         return this->speed_;
477 }
478
479 void
480 CarMove::sp(double sp)
481 {
482         this->speed_ = sp;
483 }
484
485 double
486 CarMove::st() const
487 {
488         return this->steer_;
489 }
490
491 void
492 CarMove::st(double st)
493 {
494         this->steer_ = st;
495 }
496
497 bool
498 BicycleCar::drivable(Pose const& p) const
499 {
500         PoseRange pr;
501         pr.x(p.x());
502         pr.y(p.y());
503         pr.b(p.h());
504         pr.e(p.h());
505         return this->drivable(pr);
506 }
507
508 bool
509 BicycleCar::drivable(PoseRange const& p) const
510 {
511         double h = (p.b() + p.e()) / 2.0;
512         double a_1 = atan2(p.y() - this->y(), p.x() - this->x()) - this->h();
513         while (a_1 < -M_PI)
514                 a_1 += 2 * M_PI;
515         while (a_1 > +M_PI)
516                 a_1 -= 2 * M_PI;
517         double h_d = h - this->h();
518         while (h_d < -M_PI)
519                 h_d += 2 * M_PI;
520         while (h_d > +M_PI)
521                 h_d -= 2 * M_PI;
522         double a_2 = 0;
523         if (h_d == 0 && (a_1 == 0 || a_2 == M_PI || a_2 == -M_PI)) {
524                 return true;
525         } else if (0 < a_1 && a_1 <= M_PI/2) { // left front
526                 BicycleCar z(*this); // zone border
527                 z.h(p.e());
528                 h_d = h - this->h();
529                 z.rotate(this->ccl(), h_d);
530                 // assert z.h() == h
531                 if (p.y() == z.y() && p.x() == z.x()) // p on zone border
532                         return true;
533                 a_2 = atan2(p.y() - z.y(), p.x() - z.x());
534                 while (a_2 < -M_PI)
535                         a_2 += 2 * M_PI;
536                 while (a_2 > +M_PI)
537                         a_2 -= 2 * M_PI;
538                 if (z.h() >= a_2 && a_2 >= this->h())
539                         return true;
540         } else if (M_PI/2 < a_1 && a_1 <= M_PI) { // left rear
541                 BicycleCar z(*this); // zone border
542                 z.h(p.e());
543                 h_d = h - this->h();
544                 z.rotate(this->ccl(), h_d);
545                 // assert z.h() == h
546                 if (p.y() == z.y() && p.x() == z.x()) // p on zone border
547                         return true;
548                 a_2 = atan2(p.y() - z.y(), p.x() - z.x());
549                 a_2 -= M_PI;
550                 while (a_2 < -M_PI)
551                         a_2 += 2 * M_PI;
552                 while (a_2 > +M_PI)
553                         a_2 -= 2 * M_PI;
554                 if (this->h() >= a_2 && a_2 >= z.h())
555                         return true;
556         } else if (0 > a_1 && a_1 >= -M_PI/2) { // right front
557                 BicycleCar z(*this); // zone border
558                 z.h(p.b());
559                 h_d = h - this->h();
560                 z.rotate(this->ccr(), h_d);
561                 // assert z.h() == h
562                 if (p.y() == z.y() && p.x() == z.x()) // p on zone border
563                         return true;
564                 a_2 = atan2(p.y() - z.y(), p.x() - z.x());
565                 while (a_2 < -M_PI)
566                         a_2 += 2 * M_PI;
567                 while (a_2 > +M_PI)
568                         a_2 -= 2 * M_PI;
569                 if (this->h() >= a_2 && a_2 >= z.h())
570                         return true;
571         } else if (-M_PI/2 > a_1 && a_1 >= -M_PI) { // right rear
572                 BicycleCar z(*this); // zone border
573                 z.h(p.b());
574                 h_d = h - this->h();
575                 z.rotate(this->ccr(), h_d);
576                 // assert z.h() == h
577                 if (p.y() == z.y() && p.x() == z.x()) // p on zone border
578                         return true;
579                 a_2 = atan2(p.y() - z.y(), p.x() - z.x());
580                 a_2 -= M_PI;
581                 while (a_2 < -M_PI)
582                         a_2 += 2 * M_PI;
583                 while (a_2 > +M_PI)
584                         a_2 -= 2 * M_PI;
585                 if (z.h() >= a_2 && a_2 >= this->h())
586                         return true;
587         } else {
588                 // Not happenning, as ``-pi <= a <= pi``.
589         }
590         return false;
591 }
592
593 void
594 BicycleCar::set_max_steer()
595 {
596         this->st(atan(this->wb() / this->mtr()));
597 }
598
599 double
600 BicycleCar::lfx() const
601 {
602         double lfx = this->x();
603         lfx += (this->w() / 2.0) * cos(this->h() + M_PI / 2.0);
604         lfx += this->df() * cos(this->h());
605         return lfx;
606 }
607
608 double
609 BicycleCar::lfy() const
610 {
611         double lfy = this->y();
612         lfy += (this->w() / 2.0) * sin(this->h() + M_PI / 2.0);
613         lfy += this->df() * sin(this->h());
614         return lfy;
615 }
616
617 double
618 BicycleCar::lrx() const
619 {
620         double lrx = this->x();
621         lrx += (this->w() / 2.0) * cos(this->h() + M_PI / 2.0);
622         lrx += -this->dr() * cos(this->h());
623         return lrx;
624 }
625
626 double
627 BicycleCar::lry() const
628 {
629         double lry = this->y();
630         lry += (this->w() / 2.0) * sin(this->h() + M_PI / 2.0);
631         lry += -this->dr() * sin(this->h());
632         return lry;
633 }
634
635 double
636 BicycleCar::rrx() const
637 {
638         double rrx = this->x();
639         rrx += (this->w() / 2.0) * cos(this->h() - M_PI / 2.0);
640         rrx += -this->dr() * cos(this->h());
641         return rrx;
642 }
643
644 double
645 BicycleCar::rry() const
646 {
647         double rry = this->y();
648         rry += (this->w() / 2.0) * sin(this->h() - M_PI / 2.0);
649         rry += -this->dr() * sin(this->h());
650         return rry;
651 }
652
653 double
654 BicycleCar::rfx() const
655 {
656         double rfx = this->x();
657         rfx += (this->w() / 2.0) * cos(this->h() - M_PI / 2.0);
658         rfx += this->df() * cos(this->h());
659         return rfx;
660 }
661
662 double
663 BicycleCar::rfy() const
664 {
665         double rfy = this->y();
666         rfy += (this->w() / 2.0) * sin(this->h() - M_PI / 2.0);
667         rfy += this->df() * sin(this->h());
668         return rfy;
669 }
670
671 Point
672 BicycleCar::lf() const
673 {
674         return Point(this->lfx(), this->lfy());
675 }
676
677 Point
678 BicycleCar::lr() const
679 {
680         return Point(this->lrx(), this->lry());
681 }
682
683 Point
684 BicycleCar::rr() const
685 {
686         return Point(this->rrx(), this->rry());
687 }
688
689 Point
690 BicycleCar::rf() const
691 {
692         return Point(this->rfx(), this->rfy());
693 }
694
695 Line
696 BicycleCar::left() const
697 {
698         return Line(this->lr(), this->lf());
699 }
700
701 Line
702 BicycleCar::rear() const
703 {
704         return Line(this->lr(), this->rr());
705 }
706
707 Line
708 BicycleCar::right() const
709 {
710         return Line(this->rr(), this->rf());
711 }
712
713 Line
714 BicycleCar::front() const
715 {
716         return Line(this->rf(), this->lf());
717 }
718
719 double
720 BicycleCar::ralx() const
721 {
722         double lrx = this->x();
723         lrx += (this->w() / 2.0) * cos(this->h() + M_PI / 2.0);
724         return lrx;
725 }
726 double
727 BicycleCar::raly() const
728 {
729         double lry = this->y();
730         lry += (this->w() / 2.0) * sin(this->h() + M_PI / 2.0);
731         return lry;
732 }
733
734 double
735 BicycleCar::rarx() const
736 {
737         double rrx = this->x();
738         rrx += (this->w() / 2.0) * cos(this->h() - M_PI / 2.0);
739         return rrx;
740 }
741
742 double
743 BicycleCar::rary() const
744 {
745         double rry = this->y();
746         rry += (this->w() / 2.0) * sin(this->h() - M_PI / 2.0);
747         return rry;
748 }
749
750 Point
751 BicycleCar::ccl() const
752 {
753         return Point(
754                 this->x() + this->mtr() * cos(this->h() + M_PI / 2.0),
755                 this->y() + this->mtr() * sin(this->h() + M_PI / 2.0)
756         );
757 }
758
759 Point
760 BicycleCar::ccr() const
761 {
762         return Point(
763                 this->x() + this->mtr() * cos(this->h() - M_PI / 2.0),
764                 this->y() + this->mtr() * sin(this->h() - M_PI / 2.0)
765         );
766 }
767
768 void
769 BicycleCar::next()
770 {
771         this->x(this->x() + this->sp() * cos(this->h()));
772         this->y(this->y() + this->sp() * sin(this->h()));
773         this->h(this->h() + this->sp() / this->wb() * tan(this->st()));
774 }
775
776 } // namespace bcar