2 * SPDX-FileCopyrightText: 2021 Jiri Vlasak <jiri.vlasak.2@cvut.cz>
4 * SPDX-License-Identifier: GPL-3.0-only
16 template <typename T> int sgn(T val) {
17 return (T(0) < val) - (val < T(0));
28 Point(double x, double y);
30 /*! Get horizontal coordinate. */
33 /*! Set horizontal coordinate. */
36 /*! Get vertical coordinate. */
39 /*! Set vertical coordinate. */
42 /*! \brief Return the smallest angle between three points.
44 \see https://math.stackexchange.com/questions/361412/finding-the-angle-between-three-points
46 double min_angle_between(Point const& p1, Point const& p2) const;
48 /*! \brief Return `true` if `this` point is inside of polygon `poly`.
50 * The polygon is given by the vector of `Point`s.
52 * \see https://en.wikipedia.org/wiki/Even%E2%80%93odd_rule
54 * \param poly Polygon to consider.
56 bool inside_of(std::vector<Point> const& poly) const;
58 /*! \brief Return `true` if on the right side of the plane.
60 * The plane is given by the line `li`, where `li->b()` is the base
61 * point and the direction is given by `li->e() - li->b()`.
63 * \param li The plane to consider is given by `li`.
65 bool on_right_side_of(Line const& li) const;
67 /*! \brief Translate self.
69 * \param p `Point` offset to translate by.
71 void translate(Point const& p);
73 /*! \brief Rotate self around the point.
75 \param c Rotation center `Point`.
76 \param angl Angle of rotation.
78 void rotate(Point const& c, double const angl);
80 /*! \brief Compute reflection of `this` around the `Line`.
82 * \param li The plane to reflect around is given by `li`.
84 void reflect(Line const& li);
86 /*! Return Euclidean distance to `p`. */
87 double edist(Point const& p) const;
89 /*! Generate output for plotting with gnuplot. */
90 void gen_gnuplot_to(std::ostream& out);
92 bool operator==(Point const& p);
93 friend std::ostream& operator<<(std::ostream& out, Point const& p);
103 Line(Point const& fp, Point const& lp);
105 /*! Get beginning point. */
108 /*! Get end point. */
111 /*! Get middle point. */
114 /*! Get intersection point. */
117 /*! Get intersection point. */
120 /*! \brief Return if `this` line intersects with line `li`.
122 * If the method returns `true`, the intersection `Point` is available
125 * \see https://en.wikipedia.org/wiki/Line%E2%80%93line_intersection
127 * \param li The line to check the intersection with.
129 bool intersects_with(Line const& li);
131 /*! \brief Return intersections of `this` (infinite) line and circle.
133 * If the method returns `true`, the intersection `Point`s are available
134 * in `this->i1()` and `this->i2()`.
136 * \see https://mathworld.wolfram.com/Circle-LineIntersection.html
138 * \param c Circle center.
139 * \param r Circle radius.
141 bool intersects_with(Point const& c, double const r);
147 friend std::ostream& operator<<(std::ostream& out, Line const& li);
150 /*! Store coordinates `x`, `y`, and heading `h`. */
151 class Pose : public virtual Point {
156 Pose(double x, double y, double h);
158 /*! Get heading in the interval [-pi, +pi] radians. */
161 /*! Set heading in radians. It's recomputed to [-pi, +pi]. */
164 /*! Set pose (`x`, `y`, and `h`.) */
165 void set_pose(Pose const& p);
167 void rotate(Point const& c, double const angl);
169 void reflect(Line const& li);
171 bool operator==(Pose const& p);
172 friend std::ostream& operator<<(std::ostream& out, Pose const& p);
175 class PoseRange : public virtual Pose {
181 PoseRange(Pose bp, Pose ep);
182 PoseRange(double x, double y, double b, double e);
187 /*! Get heading's begin in the interval [-pi, +pi] radians. */
190 /*! Get heading's end in the interval [-pi, +pi] radians. */
193 void translate(Point const& p);
194 void rotate(Point const& c, double const angl);
195 void reflect(Line const& li);
197 friend std::ostream& operator<<(std::ostream& out, PoseRange const& p);
200 /*! \brief Store car size.
202 * - The default is Renault ZOE (www.car.info)
206 double _curb_to_curb = 10.802166641822163;
207 double _width_with_mirrors = 1.945;
208 double _width = 1.771;
209 double _wheelbase = 2.588;
210 double _distance_to_front = 3.427;
211 double _length = 4.084;
212 double _front_track = 1.511;
214 /*! Get curb-to-curb distance. */
217 /*! Set curb-to-curb distance. */
218 void ctc(double ctc);
220 /*! Get wheelbase. */
223 /*! Set wheelbase. */
232 /*! Get width with mirrors. */
235 /*! Set width with mirrors. */
242 void len(double len);
244 /*! Get distance from rear axle to front. */
247 /*! Set distance from rear axle to front. */
250 /*! Get distance from rear axle to rear. */
253 /*! Set front track. */
256 /*! Get front track. */
259 /*! \brief Get minimum turning radius.
261 * Please, note that the method returns really _minimum turning radius_,
262 * which is the distance from the rear axle center to the center of
263 * left or right rotation given by the kinematics constrants, i.e.
264 * _wheelbase and _curb-to-curb_ distance.
266 * Sometimes _minimum turning _radius is not radius, not minimum, or not
267 * turning. In this method, _minimum turning _radius is minimum turning
272 /*! \brief Return inner radius.
274 * The inner radius is the distance from minimum turning radius circle
275 * center to the nearest point on the car. In this case, the nearest
276 * points on the car are rear axle endpoints.
278 double iradi() const;
280 /*! \brief Return outer front radius.
282 * The outer front radius is the distance from minimum turning radius
283 * circle center to the farthest point on the front (from the rear axle
284 * view) part of the car.
286 double ofradi() const;
288 /*! \brief Return outer rear radius.
290 * The outer rear radius is the distance from minimum turning radius
291 * circle center to the farthest point on the rear (from the rear axle
292 * view) part of the car.
294 double orradi() const;
296 /*! \brief Return length of perfect parking slot.
298 * The width of the slot is the same as the width of the car.
300 * \see Simon R. Blackburn *The Geometry of Perfect Parking*
301 * \see https://www.ma.rhul.ac.uk/SRBparking
303 double perfect_parking_slot_len() const;
306 /*! Store car motion. */
325 /*! \brief Geometrical computations of a bicycle car.
327 * - `x()` and `y()` methods returns coordinates of rear axle center.
329 class BicycleCar : public virtual Pose, public virtual CarSize,
330 public virtual CarMove {
333 /*! \brief Return `true` if `this` can drive to `p` trivially.
335 * Trivially means that `this` can drive to `p` by line segment - circle
336 * arc - line segment.
338 * \param p `PoseRange` (resp. `Pose`) to achieve.
340 bool drivable(PoseRange const& p) const;
341 bool drivable(Pose const& p) const;
343 /*! Set maximum steering angle. */
344 void set_max_steer();
346 /*! Get frame's left front x coordinate. */
349 /*! Get frame's left front y coordinate. */
352 /*! Get frame's left rear x coordinate. */
355 /*! Get frame's left rear y coordinate. */
358 /*! Get frame's right rear x coordinate. */
361 /*! Get frame's right rear y coordinate. */
364 /*! Get frame's right front x coordinate. */
367 /*! Get frame's right front y coordinate. */
370 /*! Get frame's left front point. */
373 /*! Get frame's left rear point. */
376 /*! Get frame's right rear point. */
379 /*! Get frame's right front point. */
382 /*! Get frame's left side. */
385 /*! Get frame's rear side. */
388 /*! Get frame's right side. */
391 /*! Get frame's front side. */
394 /*! Get frame's left rear axle x coordinate. */
397 /*! Get frame's left rear axle y coordinate. */
400 /*! Get frame's right rear axle x coordinate. */
403 /*! Get frame's right rear axle y coordinate. */
406 /*! Get frame's left rear axle point. */
409 /*! Get frame's right rear axle point. */
412 /*! Get frame's left front axle x coordinate. */
415 /*! Get frame's left front axle y coordinate. */
418 /*! Get frame's right front axle x coordinate. */
421 /*! Get frame's right front axle y coordinate. */
424 /*! Get iframe's left front axle point. */
427 /*! Get frame's right front axle point. */
430 /*! Get frame's left front mirror x coordinate. */
433 /*! Get frame's left front mirror y coordinate. */
436 /*! Get frame's right front mirror x coordinate. */
439 /*! Get frame's right front mirror y coordinate. */
442 /*! Get iframe's left front mirror point. */
445 /*! Get frame's right front mirror point. */
448 /*! Get frame's center front x coordinate. */
451 /*! Get frame's center front y coordinate. */
454 /*! Get frame's center front point. */
457 /*! Min. turning radius circle center on left. */
460 /*! Min. turning radius circle center on rigth. */
463 /*! Next car position based on speed `sp` and steer `st`. */
466 /*! Options for generating output for gnuplot. */
477 static bool LEFT_MIRROR;
478 static bool RIGHT_MIRROR;
483 /*! Generate output for plotting with gnuplot. */
484 void gen_gnuplot_to(std::ostream& out, GenPlotOpts const& opts);
488 #endif /* BCAR_BCAR_H */