10 template <typename T> int sgn(T val) {
11 return (T(0) < val) - (val < T(0));
21 Point(double x, double y);
24 /*! Get horizontal coordinate. */
27 /*! Set horizontal coordinate. */
30 /*! Get vertical coordinate. */
33 /*! Set vertical coordinate. */
36 /*! \brief Return the smallest angle between three points.
38 \see https://math.stackexchange.com/questions/361412/finding-the-angle-between-three-points
40 double min_angle_between(Point const& p1, Point const& p2) const;
42 /*! \brief Return `true` if `this` point is inside of polygon `poly`.
44 * The polygon is given by the vector of `Point`s.
46 * \see https://en.wikipedia.org/wiki/Even%E2%80%93odd_rule
48 * \param poly Polygon to consider.
50 bool inside_of(std::vector<Point> const& poly) const;
52 /*! \brief Return `true` if on the right side of the plane.
54 * The plane is given by the line `li`, where `li->fp()` is the base
55 * point and the direction is given by `li->lp() - li->fp()`.
57 * \param li The plane to consider is given by `li`.
59 bool on_right_side_of(Line const& li) const;
61 friend std::ostream& operator<<(std::ostream& out, Point const& p);
71 Line(Point const& fp, Point const& lp);
73 /*! Get first point. */
76 /*! Get last point. */
79 /*! Get intersection point. */
82 /*! Get intersection point. */
85 /*! \brief Return if `this` line intersects with line `li`.
87 * If the method returns `true`, the intersection `Point` is available
90 * \see https://en.wikipedia.org/wiki/Line%E2%80%93line_intersection
92 * \param li The line to check the intersection with.
94 bool intersects_with(Line const& li);
96 /*! \brief Return intersections of `this` (infinite) line and circle.
98 * If the method returns `true`, the intersection `Point`s are available
99 * in `this->in1()` and `this->in2()`.
101 * \see https://mathworld.wolfram.com/Circle-LineIntersection.html
103 * \param c Circle center.
104 * \param r Circle radius.
106 bool intersects_with(Point const& c, double const r);
111 /*! Store coordinates `x`, `y`, and heading `h`. */
112 class Pose : public Point {
116 Pose(double x, double y, double h);
119 /*! Get heading in the interval [-pi, +pi] radians. */
122 /*! Set heading in radians. It's recomputed to [-pi, +pi]. */
125 /*! Set pose (`x`, `y`, and `h`.) */
126 void set_pose(Pose const& p);
128 /*! \brief Rotate self around the point.
130 \param c Rotation center `Point`.
131 \param angl Angle of rotation.
133 void rotate(Point const& c, double const angl);
135 friend std::ostream& operator<<(std::ostream& out, Pose const& p);
138 class PoseRange : public Pose {
143 /*! Get heading's begin in the interval [-pi, +pi] radians. */
146 /*! Set heading's begin in radians. It's recomputed to [-pi, +pi]. */
149 /*! Get heading's end in the interval [-pi, +pi] radians. */
152 /*! Set heading's end in radians. It's recomputed to [-pi, +pi]. */
155 void rotate(Point const& c, double const angl);
157 friend std::ostream& operator<<(std::ostream& out, PoseRange const& p);
160 /*! \brief Store car size.
162 * - Default is https://en.wikipedia.org/wiki/Fiat_Punto
166 double curb_to_curb = 10.820;
167 double width = 1.625;
168 double wheelbase = 2.450;
169 double distance_to_front = 3.105;
170 double length = 3.760;
172 /*! Get curb-to-curb distance. */
175 /*! Set curb-to-curb distance. */
176 void ctc(double ctc);
178 /*! Get wheelbase. */
181 /*! Set wheelbase. */
194 void len(double len);
196 /*! Get distance from rear axle to front. */
199 /*! Set distance from rear axle to front. */
202 /*! Get distance from rear axle to rear. */
205 /*! \brief Get minimum turning radius.
207 * Please, note that the method returns really _minimum turning radius_,
208 * which is the distance from the reare axle center to the center of
209 * left or right rotation given by the kinematics constrants, i.e.
210 * _wheelbase_ and _curb-to-curb_ distance.
212 * Sometimes _minimum turning radius_ is not radius, not minimum, or not
213 * turning. In this method, _minimum turning radius_ is minimum turning
218 /*! \brief Return inner radius.
220 * The inner radius is the distance from minimum turning radius circle
221 * center to the nearest point on the car. In this case, the nearest
222 * points on the car are rear axle endpoints.
224 double iradi() const;
226 /*! \brief Return outer front radius.
228 * The outer front radius is the distance from minimum turning radius
229 * circle center to the farthest point on the front (from the rear axle
230 * view) part of the car.
232 double ofradi() const;
234 /*! \brief Return outer rear radius.
236 * The outer rear radius is the distance from minimum turning radius
237 * circle center to the farthest point on the rear (from the rear axle
238 * view) part of the car.
240 double orradi() const;
242 /*! \brief Return length of perfect parking slot.
244 * The width of the slot is the same as the width of the car.
246 * \see Simon R. Blackburn *The Geometry of Perfect Parking*
247 * \see https://www.ma.rhul.ac.uk/SRBparking
249 double perfect_parking_slot_len() const;
252 /*! Store car motion. */
271 /*! \brief Geometrical computations of a bicycle car.
273 * - `x()` and `y()` methods returns coordinates of rear axle center.
275 class BicycleCar : public Pose, public CarSize, public CarMove {
278 /*! \brief Return `false` if `bc` is not achievable.
280 * When `false` is returned the `bc` may still be drivable, but not
281 * trivially, i.e. by "line segment - circle arc - line segment".
283 * \param p `PoseRange` (resp. `Pose`) to achieve.
285 bool drivable(PoseRange const& p) const;
286 bool drivable(Pose const& p) const;
288 /*! Set maximum steering angle. */
289 void set_max_steer();
291 /*! Get frame's left front x coordinate. */
294 /*! Get frame's left front y coordinate. */
297 /*! Get frame's left rear x coordinate. */
300 /*! Get frame's left rear y coordinate. */
303 /*! Get frame's right rear x coordinate. */
306 /*! Get frame's right rear y coordinate. */
309 /*! Get frame's right front x coordinate. */
312 /*! Get frame's right front y coordinate. */
315 /*! Get rear axle's left x coordinate. */
318 /*! Get rear axle's left y coordinate. */
321 /*! Get rear axle's right x coordinate. */
324 /*! Get rear axle's right y coordinate. */
327 /*! Min. turning radius circle center on left. */
330 /*! Min. turning radius circle center on rigth. */
333 /*! Next car position based on speed `sp` and steer `st`. */
338 #endif /* BCAR_BCAR_H */