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