]> rtime.felk.cvut.cz Git - hubacji1/bcar.git/blob - src/bcar.cc
Merge branch 'refactor'
[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 double
195 Pose::x() const
196 {
197         return this->x_;
198 }
199
200 void
201 Pose::x(double x)
202 {
203         this->x_ = x;
204 }
205
206 double
207 Pose::y() const
208 {
209         return this->y_;
210 }
211
212 void
213 Pose::y(double y)
214 {
215         this->y_ = y;
216 }
217
218 double
219 Pose::h() const
220 {
221         return this->h_;
222 }
223
224 void
225 Pose::h(double h)
226 {
227         while (h < -M_PI) {
228                 h += 2 * M_PI;
229         }
230         while (h > +M_PI) {
231                 h -= 2 * M_PI;
232         }
233         this->h_ = h;
234 }
235
236 void
237 Pose::rotate(Point const& c, double const angl)
238 {
239         double px = this->x();
240         double py = this->y();
241         px -= c.x();
242         py -= c.y();
243         double nx = px * cos(angl) - py * sin(angl);
244         double ny = px * sin(angl) + py * cos(angl);
245         this->h(this->h() + angl);
246         this->x(nx + c.x());
247         this->y(ny + c.y());
248 }
249
250 std::ostream&
251 operator<<(std::ostream& out, Pose const& p)
252 {
253         out << "[" << p.x() << "," << p.y() << "," << p.h() << "]";
254         return out;
255 }
256
257 double
258 CarSize::ctc() const
259 {
260         return this->curb_to_curb;
261 }
262
263 void
264 CarSize::ctc(double ctc)
265 {
266         this->curb_to_curb = ctc;
267 }
268
269 double
270 CarSize::wb() const
271 {
272         return this->wheelbase;
273 }
274
275 void
276 CarSize::wb(double wb)
277 {
278         this->wheelbase = wb;
279 }
280
281 double
282 CarSize::w() const
283 {
284         return this->width;
285 }
286
287 void
288 CarSize::w(double w)
289 {
290         this->width = w;
291 }
292
293 double
294 CarSize::len() const
295 {
296         return this->length;
297 }
298
299 void
300 CarSize::len(double len)
301 {
302         this->length = len;
303 }
304
305 double
306 CarSize::df() const
307 {
308         return this->distance_to_front;
309 }
310
311 void
312 CarSize::df(double df)
313 {
314         this->distance_to_front = df;
315 }
316
317 double
318 CarSize::dr() const
319 {
320         return this->len() - this->df();
321 }
322
323 double
324 CarSize::mtr() const
325 {
326         auto ctc2 = pow(this->ctc() / 2.0, 2.0);
327         auto wb2 = pow(this->wb(), 2.0);
328         return sqrt(ctc2 - wb2) - this->w() / 2.0;
329 }
330
331 double
332 CarMove::sp() const
333 {
334         return this->speed;
335 }
336
337 void
338 CarMove::sp(double sp)
339 {
340         this->speed = sp;
341 }
342
343 double
344 CarMove::st() const
345 {
346         return this->steer;
347 }
348
349 void
350 CarMove::st(double st)
351 {
352         this->steer = st;
353 }
354
355 bool
356 BicycleCar::drivable(BicycleCar const& bc) const
357 {
358     return this->drivable(bc, bc.h(), bc.h());
359 }
360
361 bool
362 BicycleCar::drivable(BicycleCar const& bc, double b, double e) const
363 {
364         // assert bc.h() == (b + e) / 2.0
365         double a_1 = atan2(bc.y() - this->y(), bc.x() - this->x()) - this->h();
366         while (a_1 < -M_PI)
367                 a_1 += 2 * M_PI;
368         while (a_1 > +M_PI)
369                 a_1 -= 2 * M_PI;
370         double h_d = bc.h() - this->h();
371         while (h_d < -M_PI)
372                 h_d += 2 * M_PI;
373         while (h_d > +M_PI)
374                 h_d -= 2 * M_PI;
375         double a_2 = 0;
376         if (h_d == 0 && (a_1 == 0 || a_2 == M_PI || a_2 == -M_PI)) {
377                 return true;
378         } else if (0 < a_1 && a_1 <= M_PI/2) { // left front
379                 BicycleCar z(*this); // zone border
380                 z.h(e);
381                 h_d = bc.h() - this->h();
382                 z.rotate(this->ccl(), h_d);
383                 // assert z.h() == bc.h()
384                 if (bc.y() == z.y() && bc.x() == z.x()) // bc on zone border
385                         return true;
386                 a_2 = atan2(bc.y() - z.y(), bc.x() - z.x());
387                 while (a_2 < -M_PI)
388                         a_2 += 2 * M_PI;
389                 while (a_2 > +M_PI)
390                         a_2 -= 2 * M_PI;
391                 if (z.h() >= a_2 && a_2 >= this->h())
392                         return true;
393         } else if (M_PI/2 < a_1 && a_1 <= M_PI) { // left rear
394                 BicycleCar z(*this); // zone border
395                 z.h(e);
396                 h_d = bc.h() - this->h();
397                 z.rotate(this->ccl(), h_d);
398                 // assert z.h() == bc.h()
399                 if (bc.y() == z.y() && bc.x() == z.x()) // bc on zone border
400                         return true;
401                 a_2 = atan2(bc.y() - z.y(), bc.x() - z.x());
402                 a_2 -= M_PI;
403                 while (a_2 < -M_PI)
404                         a_2 += 2 * M_PI;
405                 while (a_2 > +M_PI)
406                         a_2 -= 2 * M_PI;
407                 if (this->h() >= a_2 && a_2 >= z.h())
408                         return true;
409         } else if (0 > a_1 && a_1 >= -M_PI/2) { // right front
410                 BicycleCar z(*this); // zone border
411                 z.h(b);
412                 h_d = bc.h() - this->h();
413                 z.rotate(this->ccr(), h_d);
414                 // assert z.h() == bc.h()
415                 if (bc.y() == z.y() && bc.x() == z.x()) // bc on zone border
416                         return true;
417                 a_2 = atan2(bc.y() - z.y(), bc.x() - z.x());
418                 while (a_2 < -M_PI)
419                         a_2 += 2 * M_PI;
420                 while (a_2 > +M_PI)
421                         a_2 -= 2 * M_PI;
422                 if (this->h() >= a_2 && a_2 >= z.h())
423                         return true;
424         } else if (-M_PI/2 > a_1 && a_1 >= -M_PI) { // right rear
425                 BicycleCar z(*this); // zone border
426                 z.h(b);
427                 h_d = bc.h() - this->h();
428                 z.rotate(this->ccr(), h_d);
429                 // assert z.h() == bc.h()
430                 if (bc.y() == z.y() && bc.x() == z.x()) // bc on zone border
431                         return true;
432                 a_2 = atan2(bc.y() - z.y(), bc.x() - z.x());
433                 a_2 -= M_PI;
434                 while (a_2 < -M_PI)
435                         a_2 += 2 * M_PI;
436                 while (a_2 > +M_PI)
437                         a_2 -= 2 * M_PI;
438                 if (z.h() >= a_2 && a_2 >= this->h())
439                         return true;
440         } else {
441                 // Not happenning, as ``-pi <= a <= pi``.
442         }
443         return false;
444 }
445
446 double
447 BicycleCar::iradi() const
448 {
449         return this->mtr() - this->w() / 2;
450 }
451
452 double
453 BicycleCar::ofradi() const
454 {
455         auto mtrw2 = pow(this->mtr() + this->w() / 2.0, 2.0);
456         auto df2 = pow(this->df(), 2.0);
457         return sqrt(mtrw2 + df2);
458 }
459
460 double
461 BicycleCar::orradi() const
462 {
463         auto mtrw2 = pow(this->mtr() + this->w() / 2.0, 2.0);
464         auto dr2 = pow(this->dr(), 2.0);
465         return sqrt(mtrw2 + dr2);
466 }
467
468 double
469 BicycleCar::perfect_parking_slot_len() const
470 {
471         auto r = this->ctc() / 2.0;
472         auto l = this->wb();
473         auto k = this->df() - this->wb();
474         auto w = this->w();
475         auto r2l2 = r * r - l * l;
476         auto s = r2l2 + pow(l + k, 2.0) - pow(sqrt(r2l2) - w, 2.0);
477         return this->len() + sqrt(s) - l - k;
478 }
479
480 void
481 BicycleCar::set_max_steer()
482 {
483         this->st(atan(this->wb() / this->mtr()));
484 }
485
486 double
487 BicycleCar::lfx() const
488 {
489         double lfx = this->x();
490         lfx += (this->w() / 2.0) * cos(this->h() + M_PI / 2.0);
491         lfx += this->df() * cos(this->h());
492         return lfx;
493 }
494
495 double
496 BicycleCar::lfy() const
497 {
498         double lfy = this->y();
499         lfy += (this->w() / 2.0) * sin(this->h() + M_PI / 2.0);
500         lfy += this->df() * sin(this->h());
501         return lfy;
502 }
503
504 double
505 BicycleCar::lrx() const
506 {
507         double lrx = this->x();
508         lrx += (this->w() / 2.0) * cos(this->h() + M_PI / 2.0);
509         lrx += -this->dr() * cos(this->h());
510         return lrx;
511 }
512
513 double
514 BicycleCar::lry() const
515 {
516         double lry = this->y();
517         lry += (this->w() / 2.0) * sin(this->h() + M_PI / 2.0);
518         lry += -this->dr() * sin(this->h());
519         return lry;
520 }
521
522 double
523 BicycleCar::rrx() const
524 {
525         double rrx = this->x();
526         rrx += (this->w() / 2.0) * cos(this->h() - M_PI / 2.0);
527         rrx += -this->dr() * cos(this->h());
528         return rrx;
529 }
530
531 double
532 BicycleCar::rry() const
533 {
534         double rry = this->y();
535         rry += (this->w() / 2.0) * sin(this->h() - M_PI / 2.0);
536         rry += -this->dr() * sin(this->h());
537         return rry;
538 }
539
540 double
541 BicycleCar::rfx() const
542 {
543         double rfx = this->x();
544         rfx += (this->w() / 2.0) * cos(this->h() - M_PI / 2.0);
545         rfx += this->df() * cos(this->h());
546         return rfx;
547 }
548
549 double
550 BicycleCar::rfy() const
551 {
552         double rfy = this->y();
553         rfy += (this->w() / 2.0) * sin(this->h() - M_PI / 2.0);
554         rfy += this->df() * sin(this->h());
555         return rfy;
556 }
557
558 double
559 BicycleCar::ralx() const
560 {
561         double lrx = this->x();
562         lrx += (this->w() / 2.0) * cos(this->h() + M_PI / 2.0);
563         return lrx;
564 }
565 double
566 BicycleCar::raly() const
567 {
568         double lry = this->y();
569         lry += (this->w() / 2.0) * sin(this->h() + M_PI / 2.0);
570         return lry;
571 }
572
573 double
574 BicycleCar::rarx() const
575 {
576         double rrx = this->x();
577         rrx += (this->w() / 2.0) * cos(this->h() - M_PI / 2.0);
578         return rrx;
579 }
580
581 double
582 BicycleCar::rary() const
583 {
584         double rry = this->y();
585         rry += (this->w() / 2.0) * sin(this->h() - M_PI / 2.0);
586         return rry;
587 }
588
589 Point
590 BicycleCar::ccl() const
591 {
592         return Point(
593                 this->x() + this->mtr() * cos(this->h() + M_PI / 2.0),
594                 this->y() + this->mtr() * sin(this->h() + M_PI / 2.0)
595         );
596 }
597
598 Point
599 BicycleCar::ccr() const
600 {
601         return Point(
602                 this->x() + this->mtr() * cos(this->h() - M_PI / 2.0),
603                 this->y() + this->mtr() * sin(this->h() - M_PI / 2.0)
604         );
605 }
606
607 void
608 BicycleCar::next()
609 {
610         this->x(this->x() + this->sp() * cos(this->h()));
611         this->y(this->y() + this->sp() * sin(this->h()));
612         this->h(this->h() + this->sp() / this->wb() * tan(this->st()));
613 }