]> rtime.felk.cvut.cz Git - hubacji1/bcar.git/blob - incl/bcar.hh
d62727d383825ca03070ad6962b75fffb9b6ab4a
[hubacji1/bcar.git] / incl / bcar.hh
1 /*! \file */
2 #ifndef BCAR_BCAR_H
3 #define BCAR_BCAR_H
4
5 #include <ostream>
6 #include <vector>
7
8 namespace bcar {
9
10 template <typename T> int sgn(T val) {
11         return (T(0) < val) - (val < T(0));
12 }
13
14 class Line;
15
16 class Point {
17 private:
18         double x_ = 0.0;
19         double y_ = 0.0;
20 public:
21         Point();
22         Point(double x, double y);
23
24         /*! Get horizontal coordinate. */
25         double x() const;
26
27         /*! Set horizontal coordinate. */
28         void x(double x);
29
30         /*! Get vertical coordinate. */
31         double y() const;
32
33         /*! Set vertical coordinate. */
34         void y(double y);
35
36         /*! \brief Return the smallest angle between three points.
37
38         \see https://math.stackexchange.com/questions/361412/finding-the-angle-between-three-points
39         */
40         double min_angle_between(Point const& p1, Point const& p2) const;
41
42         /*! \brief Return `true` if `this` point is inside of polygon `poly`.
43          *
44          * The polygon is given by the vector of `Point`s.
45          *
46          * \see https://en.wikipedia.org/wiki/Even%E2%80%93odd_rule
47          *
48          * \param poly Polygon to consider.
49          */
50         bool inside_of(std::vector<Point> const& poly) const;
51
52         /*! \brief Return `true` if on the right side of the plane.
53          *
54          * The plane is given by the line `li`, where `li->b()` is the base
55          * point and the direction is given by `li->e() - li->b()`.
56          *
57          * \param li The plane to consider is given by `li`.
58          */
59         bool on_right_side_of(Line const& li) const;
60
61         /*! \brief Rotate self around the point.
62
63         \param c Rotation center `Point`.
64         \param angl Angle of rotation.
65         */
66         void rotate(Point const& c, double const angl);
67
68         /*! Return Euclidean distance to `p`. */
69         double edist(Point const& p) const;
70
71         friend std::ostream& operator<<(std::ostream& out, Point const& p);
72 };
73
74 class Line {
75 private:
76         Point b_;
77         Point e_;
78         Point i1_;
79         Point i2_;
80 public:
81         Line(Point const& fp, Point const& lp);
82
83         /*! Get beginning point. */
84         Point b() const&;
85
86         /*! Get end point. */
87         Point e() const&;
88
89         /*! Get intersection point. */
90         Point i1() const&;
91
92         /*! Get intersection point. */
93         Point i2() const&;
94
95         /*! \brief Return if `this` line intersects with line `li`.
96          *
97          * If the method returns `true`, the intersection `Point` is available
98          * in `this->i1()`.
99          *
100          * \see https://en.wikipedia.org/wiki/Line%E2%80%93line_intersection
101          *
102          * \param li The line to check the intersection with.
103          */
104         bool intersects_with(Line const& li);
105
106         /*! \brief Return intersections of `this` (infinite) line and circle.
107          *
108          * If the method returns `true`, the intersection `Point`s are available
109          * in `this->i1()` and `this->i2()`.
110          *
111          * \see https://mathworld.wolfram.com/Circle-LineIntersection.html
112          *
113          * \param c Circle center.
114          * \param r Circle radius.
115          */
116         bool intersects_with(Point const& c, double const r);
117
118         double len() const;
119
120         double h() const;
121
122         friend std::ostream& operator<<(std::ostream& out, Line const& li);
123 };
124
125 /*! Store coordinates `x`, `y`, and heading `h`. */
126 class Pose : public virtual Point {
127 private:
128         double h_ = 0.0;
129 public:
130         using Point::Point;
131         Pose(double x, double y, double h);
132
133         /*! Get heading in the interval [-pi, +pi] radians. */
134         double h() const;
135
136         /*! Set heading in radians. It's recomputed to [-pi, +pi]. */
137         void h(double h);
138
139         /*! Set pose (`x`, `y`, and `h`.) */
140         void set_pose(Pose const& p);
141
142         void rotate(Point const& c, double const angl);
143
144         friend std::ostream& operator<<(std::ostream& out, Pose const& p);
145 };
146
147 class PoseRange : public virtual Pose {
148 private:
149         double e_ = 0.0;
150         using Pose::h;
151 public:
152         /*! Get heading's begin in the interval [-pi, +pi] radians. */
153         double b() const;
154
155         /*! Set heading's begin in radians. It's recomputed to [-pi, +pi]. */
156         void b(double b);
157
158         /*! Get heading's end in the interval [-pi, +pi] radians. */
159         double e() const;
160
161         /*! Set heading's end in radians. It's recomputed to [-pi, +pi]. */
162         void e(double e);
163
164         void rotate(Point const& c, double const angl);
165
166         friend std::ostream& operator<<(std::ostream& out, PoseRange const& p);
167 };
168
169 /*! \brief Store car size.
170  *
171  * - Default is https://en.wikipedia.org/wiki/Fiat_Punto
172  */
173 class CarSize {
174 private:
175         double curb_to_curb_ = 10.820;
176         double width_ = 1.625;
177         double wheelbase_ = 2.450;
178         double distance_to_front_ = 3.105;
179         double length_ = 3.760;
180 public:
181         /*! Get curb-to-curb distance. */
182         double ctc() const;
183
184         /*! Set curb-to-curb distance. */
185         void ctc(double ctc);
186
187         /*! Get wheelbase. */
188         double wb() const;
189
190         /*! Set wheelbase. */
191         void wb(double wb);
192
193         /*! Get width. */
194         double w() const;
195
196         /*! Set width. */
197         void w(double w);
198
199         /*! Get length. */
200         double len() const;
201
202         /*! Set length. */
203         void len(double len);
204
205         /*! Get distance from rear axle to front. */
206         double df() const;
207
208         /*! Set distance from rear axle to front. */
209         void df(double df);
210
211         /*! Get distance from rear axle to rear. */
212         double dr() const;
213
214         /*! \brief Get minimum turning radius.
215          *
216          * Please, note that the method returns really _minimum turning radius_,
217          * which is the distance from the rear axle center to the center of
218          * left or right rotation given by the kinematics constrants, i.e.
219          * _wheelbase_ and _curb-to-curb_ distance.
220          *
221          * Sometimes _minimum turning radius_ is not radius, not minimum, or not
222          * turning. In this method, _minimum turning radius_ is minimum turning
223          * radius.
224          */
225         double mtr() const;
226
227         /*! \brief Return inner radius.
228          *
229          * The inner radius is the distance from minimum turning radius circle
230          * center to the nearest point on the car. In this case, the nearest
231          * points on the car are rear axle endpoints.
232          */
233         double iradi() const;
234
235         /*! \brief Return outer front radius.
236          *
237          * The outer front radius is the distance from minimum turning radius
238          * circle center to the farthest point on the front (from the rear axle
239          * view) part of the car.
240          */
241         double ofradi() const;
242
243         /*! \brief Return outer rear radius.
244          *
245          * The outer rear radius is the distance from minimum turning radius
246          * circle center to the farthest point on the rear (from the rear axle
247          * view) part of the car.
248          */
249         double orradi() const;
250
251         /*! \brief Return length of perfect parking slot.
252          *
253          * The width of the slot is the same as the width of the car.
254          *
255          * \see Simon R. Blackburn *The Geometry of Perfect Parking*
256          * \see https://www.ma.rhul.ac.uk/SRBparking
257          */
258         double perfect_parking_slot_len() const;
259 };
260
261 /*! Store car motion. */
262 class CarMove {
263 private:
264         double speed_ = 0.0;
265         double steer_ = 0.0;
266 public:
267         /*! Get speed. */
268         double sp() const;
269
270         /*! Set speed. */
271         void sp(double sp);
272
273         /*! Get steer. */
274         double st() const;
275
276         /*! Set steer. */
277         void st(double st);
278 };
279
280 /*! \brief Geometrical computations of a bicycle car.
281  *
282  * - `x()` and `y()` methods returns coordinates of rear axle center.
283  */
284 class BicycleCar : public virtual Pose, public virtual CarSize,
285                 public virtual CarMove {
286 private:
287 public:
288         /*! \brief Return `true` if `this` can drive to `p` trivially.
289          *
290          * Trivially means that `this` can drive to `p` by line segment - circle
291          * arc - line segment.
292          *
293          * \param p `PoseRange` (resp. `Pose`) to achieve.
294          */
295         bool drivable(PoseRange const& p) const;
296         bool drivable(Pose const& p) const;
297
298         /*! Set maximum steering angle. */
299         void set_max_steer();
300
301         /*! Get frame's left front x coordinate. */
302         double lfx() const;
303
304         /*! Get frame's left front y coordinate. */
305         double lfy() const;
306
307         /*! Get frame's left rear x coordinate. */
308         double lrx() const;
309
310         /*! Get frame's left rear y coordinate. */
311         double lry() const;
312
313         /*! Get frame's right rear x coordinate. */
314         double rrx() const;
315
316         /*! Get frame's right rear y coordinate. */
317         double rry() const;
318
319         /*! Get frame's right front x coordinate. */
320         double rfx() const;
321
322         /*! Get frame's right front y coordinate. */
323         double rfy() const;
324
325         /*! Get frame's left front point. */
326         Point lf() const;
327
328         /*! Get frame's left rear point. */
329         Point lr() const;
330
331         /*! Get frame's right rear point. */
332         Point rr() const;
333
334         /*! Get frame's right front point. */
335         Point rf() const;
336
337         /*! Get frame's left side. */
338         Line left() const;
339
340         /*! Get frame's rear side. */
341         Line rear() const;
342
343         /*! Get frame's right side. */
344         Line right() const;
345
346         /*! Get frame's front side. */
347         Line front() const;
348
349         /*! Get rear axle's left x coordinate. */
350         double ralx() const;
351
352         /*! Get rear axle's left y coordinate. */
353         double raly() const;
354
355         /*! Get rear axle's right x coordinate. */
356         double rarx() const;
357
358         /*! Get rear axle's right y coordinate. */
359         double rary() const;
360
361         /*! Min. turning radius circle center on left. */
362         Point ccl() const;
363
364         /*! Min. turning radius circle center on rigth. */
365         Point ccr() const;
366
367         /*! Next car position based on speed `sp` and steer `st`. */
368         void next();
369 };
370
371 } // namespace bcar
372 #endif /* BCAR_BCAR_H */