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