]> rtime.felk.cvut.cz Git - hubacji1/bcar.git/blob - incl/bcar.hh
Refactor and rename due to consistency
[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         friend std::ostream& operator<<(std::ostream& out, Line const& li);
121 };
122
123 /*! Store coordinates `x`, `y`, and heading `h`. */
124 class Pose : public virtual Point {
125 private:
126         double h_ = 0.0;
127 public:
128         using Point::Point;
129         Pose(double x, double y, double h);
130
131         /*! Get heading in the interval [-pi, +pi] radians. */
132         double h() const;
133
134         /*! Set heading in radians. It's recomputed to [-pi, +pi]. */
135         void h(double h);
136
137         /*! Set pose (`x`, `y`, and `h`.) */
138         void set_pose(Pose const& p);
139
140         void rotate(Point const& c, double const angl);
141
142         friend std::ostream& operator<<(std::ostream& out, Pose const& p);
143 };
144
145 class PoseRange : public virtual Pose {
146 private:
147         double e_ = 0.0;
148         using Pose::h;
149 public:
150         /*! Get heading's begin in the interval [-pi, +pi] radians. */
151         double b() const;
152
153         /*! Set heading's begin in radians. It's recomputed to [-pi, +pi]. */
154         void b(double b);
155
156         /*! Get heading's end in the interval [-pi, +pi] radians. */
157         double e() const;
158
159         /*! Set heading's end in radians. It's recomputed to [-pi, +pi]. */
160         void e(double e);
161
162         void rotate(Point const& c, double const angl);
163
164         friend std::ostream& operator<<(std::ostream& out, PoseRange const& p);
165 };
166
167 /*! \brief Store car size.
168  *
169  * - Default is https://en.wikipedia.org/wiki/Fiat_Punto
170  */
171 class CarSize {
172 private:
173         double curb_to_curb_ = 10.820;
174         double width_ = 1.625;
175         double wheelbase_ = 2.450;
176         double distance_to_front_ = 3.105;
177         double length_ = 3.760;
178 public:
179         /*! Get curb-to-curb distance. */
180         double ctc() const;
181
182         /*! Set curb-to-curb distance. */
183         void ctc(double ctc);
184
185         /*! Get wheelbase. */
186         double wb() const;
187
188         /*! Set wheelbase. */
189         void wb(double wb);
190
191         /*! Get width. */
192         double w() const;
193
194         /*! Set width. */
195         void w(double w);
196
197         /*! Get length. */
198         double len() const;
199
200         /*! Set length. */
201         void len(double len);
202
203         /*! Get distance from rear axle to front. */
204         double df() const;
205
206         /*! Set distance from rear axle to front. */
207         void df(double df);
208
209         /*! Get distance from rear axle to rear. */
210         double dr() const;
211
212         /*! \brief Get minimum turning radius.
213          *
214          * Please, note that the method returns really _minimum turning radius_,
215          * which is the distance from the rear axle center to the center of
216          * left or right rotation given by the kinematics constrants, i.e.
217          * _wheelbase_ and _curb-to-curb_ distance.
218          *
219          * Sometimes _minimum turning radius_ is not radius, not minimum, or not
220          * turning. In this method, _minimum turning radius_ is minimum turning
221          * radius.
222          */
223         double mtr() const;
224
225         /*! \brief Return inner radius.
226          *
227          * The inner radius is the distance from minimum turning radius circle
228          * center to the nearest point on the car. In this case, the nearest
229          * points on the car are rear axle endpoints.
230          */
231         double iradi() const;
232
233         /*! \brief Return outer front radius.
234          *
235          * The outer front radius is the distance from minimum turning radius
236          * circle center to the farthest point on the front (from the rear axle
237          * view) part of the car.
238          */
239         double ofradi() const;
240
241         /*! \brief Return outer rear radius.
242          *
243          * The outer rear radius is the distance from minimum turning radius
244          * circle center to the farthest point on the rear (from the rear axle
245          * view) part of the car.
246          */
247         double orradi() const;
248
249         /*! \brief Return length of perfect parking slot.
250          *
251          * The width of the slot is the same as the width of the car.
252          *
253          * \see Simon R. Blackburn *The Geometry of Perfect Parking*
254          * \see https://www.ma.rhul.ac.uk/SRBparking
255          */
256         double perfect_parking_slot_len() const;
257 };
258
259 /*! Store car motion. */
260 class CarMove {
261 private:
262         double speed_ = 0.0;
263         double steer_ = 0.0;
264 public:
265         /*! Get speed. */
266         double sp() const;
267
268         /*! Set speed. */
269         void sp(double sp);
270
271         /*! Get steer. */
272         double st() const;
273
274         /*! Set steer. */
275         void st(double st);
276 };
277
278 /*! \brief Geometrical computations of a bicycle car.
279  *
280  * - `x()` and `y()` methods returns coordinates of rear axle center.
281  */
282 class BicycleCar : public virtual Pose, public virtual CarSize,
283                 public virtual CarMove {
284 private:
285 public:
286         /*! \brief Return `false` if `bc` is not achievable.
287          *
288          * When `false` is returned the `bc` may still be drivable, but not
289          * trivially, i.e. by "line segment - circle arc - line segment".
290          *
291          * \param p `PoseRange` (resp. `Pose`) to achieve.
292          */
293         bool drivable(PoseRange const& p) const;
294         bool drivable(Pose const& p) const;
295
296         /*! Set maximum steering angle. */
297         void set_max_steer();
298
299         /*! Get frame's left front x coordinate. */
300         double lfx() const;
301
302         /*! Get frame's left front y coordinate. */
303         double lfy() const;
304
305         /*! Get frame's left rear x coordinate. */
306         double lrx() const;
307
308         /*! Get frame's left rear y coordinate. */
309         double lry() const;
310
311         /*! Get frame's right rear x coordinate. */
312         double rrx() const;
313
314         /*! Get frame's right rear y coordinate. */
315         double rry() const;
316
317         /*! Get frame's right front x coordinate. */
318         double rfx() const;
319
320         /*! Get frame's right front y coordinate. */
321         double rfy() const;
322
323         /*! Get frame's left front point. */
324         Point lf() const;
325
326         /*! Get frame's left rear point. */
327         Point lr() const;
328
329         /*! Get frame's right rear point. */
330         Point rr() const;
331
332         /*! Get frame's right front point. */
333         Point rf() const;
334
335         /*! Get frame's left side. */
336         Line left() const;
337
338         /*! Get frame's rear side. */
339         Line rear() const;
340
341         /*! Get frame's right side. */
342         Line right() const;
343
344         /*! Get frame's front side. */
345         Line front() const;
346
347         /*! Get rear axle's left x coordinate. */
348         double ralx() const;
349
350         /*! Get rear axle's left y coordinate. */
351         double raly() const;
352
353         /*! Get rear axle's right x coordinate. */
354         double rarx() const;
355
356         /*! Get rear axle's right y coordinate. */
357         double rary() const;
358
359         /*! Min. turning radius circle center on left. */
360         Point ccl() const;
361
362         /*! Min. turning radius circle center on rigth. */
363         Point ccr() const;
364
365         /*! Next car position based on speed `sp` and steer `st`. */
366         void next();
367 };
368
369 } // namespace bcar
370 #endif /* BCAR_BCAR_H */