10 template <typename T> int sgn(T val) {
11 return (T(0) < val) - (val < T(0));
22 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->b()` is the base
55 * point and the direction is given by `li->e() - li->b()`.
57 * \param li The plane to consider is given by `li`.
59 bool on_right_side_of(Line const& li) const;
61 /*! \brief Rotate self around the point.
63 \param c Rotation center `Point`.
64 \param angl Angle of rotation.
66 void rotate(Point const& c, double const angl);
68 /*! \brief Compute reflection of `this` around the `Line`.
70 * \param li The plane to reflect around is given by `li`.
72 void reflect(Line const& li);
74 /*! Return Euclidean distance to `p`. */
75 double edist(Point const& p) const;
77 friend std::ostream& operator<<(std::ostream& out, Point const& p);
87 Line(Point const& fp, Point const& lp);
89 /*! Get beginning point. */
95 /*! Get intersection point. */
98 /*! Get intersection point. */
101 /*! \brief Return if `this` line intersects with line `li`.
103 * If the method returns `true`, the intersection `Point` is available
106 * \see https://en.wikipedia.org/wiki/Line%E2%80%93line_intersection
108 * \param li The line to check the intersection with.
110 bool intersects_with(Line const& li);
112 /*! \brief Return intersections of `this` (infinite) line and circle.
114 * If the method returns `true`, the intersection `Point`s are available
115 * in `this->i1()` and `this->i2()`.
117 * \see https://mathworld.wolfram.com/Circle-LineIntersection.html
119 * \param c Circle center.
120 * \param r Circle radius.
122 bool intersects_with(Point const& c, double const r);
128 friend std::ostream& operator<<(std::ostream& out, Line const& li);
131 /*! Store coordinates `x`, `y`, and heading `h`. */
132 class Pose : public virtual Point {
137 Pose(double x, double y, double h);
139 /*! Get heading in the interval [-pi, +pi] radians. */
142 /*! Set heading in radians. It's recomputed to [-pi, +pi]. */
145 /*! Set pose (`x`, `y`, and `h`.) */
146 void set_pose(Pose const& p);
148 void rotate(Point const& c, double const angl);
150 void reflect(Line const& li);
152 friend std::ostream& operator<<(std::ostream& out, Pose const& p);
155 class PoseRange : public virtual Pose {
160 /*! Get heading's begin in the interval [-pi, +pi] radians. */
163 /*! Set heading's begin in radians. It's recomputed to [-pi, +pi]. */
166 /*! Get heading's end in the interval [-pi, +pi] radians. */
169 /*! Set heading's end in radians. It's recomputed to [-pi, +pi]. */
172 void rotate(Point const& c, double const angl);
174 void reflect(Line const& li);
176 friend std::ostream& operator<<(std::ostream& out, PoseRange const& p);
179 /*! \brief Store car size.
181 * - Default is https://en.wikipedia.org/wiki/Fiat_Punto
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;
191 /*! Get curb-to-curb distance. */
194 /*! Set curb-to-curb distance. */
195 void ctc(double ctc);
197 /*! Get wheelbase. */
200 /*! Set wheelbase. */
213 void len(double len);
215 /*! Get distance from rear axle to front. */
218 /*! Set distance from rear axle to front. */
221 /*! Get distance from rear axle to rear. */
224 /*! \brief Get minimum turning radius.
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.
231 * Sometimes _minimum turning radius_ is not radius, not minimum, or not
232 * turning. In this method, _minimum turning radius_ is minimum turning
237 /*! \brief Return inner radius.
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.
243 double iradi() const;
245 /*! \brief Return outer front radius.
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.
251 double ofradi() const;
253 /*! \brief Return outer rear radius.
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.
259 double orradi() const;
261 /*! \brief Return length of perfect parking slot.
263 * The width of the slot is the same as the width of the car.
265 * \see Simon R. Blackburn *The Geometry of Perfect Parking*
266 * \see https://www.ma.rhul.ac.uk/SRBparking
268 double perfect_parking_slot_len() const;
271 /*! Store car motion. */
290 /*! \brief Geometrical computations of a bicycle car.
292 * - `x()` and `y()` methods returns coordinates of rear axle center.
294 class BicycleCar : public virtual Pose, public virtual CarSize,
295 public virtual CarMove {
298 /*! \brief Return `true` if `this` can drive to `p` trivially.
300 * Trivially means that `this` can drive to `p` by line segment - circle
301 * arc - line segment.
303 * \param p `PoseRange` (resp. `Pose`) to achieve.
305 bool drivable(PoseRange const& p) const;
306 bool drivable(Pose const& p) const;
308 /*! Set maximum steering angle. */
309 void set_max_steer();
311 /*! Get frame's left front x coordinate. */
314 /*! Get frame's left front y coordinate. */
317 /*! Get frame's left rear x coordinate. */
320 /*! Get frame's left rear y coordinate. */
323 /*! Get frame's right rear x coordinate. */
326 /*! Get frame's right rear y coordinate. */
329 /*! Get frame's right front x coordinate. */
332 /*! Get frame's right front y coordinate. */
335 /*! Get frame's left front point. */
338 /*! Get frame's left rear point. */
341 /*! Get frame's right rear point. */
344 /*! Get frame's right front point. */
347 /*! Get frame's left side. */
350 /*! Get frame's rear side. */
353 /*! Get frame's right side. */
356 /*! Get frame's front side. */
359 /*! Get rear axle's left x coordinate. */
362 /*! Get rear axle's left y coordinate. */
365 /*! Get rear axle's right x coordinate. */
368 /*! Get rear axle's right y coordinate. */
371 /*! Min. turning radius circle center on left. */
374 /*! Min. turning radius circle center on rigth. */
377 /*! Next car position based on speed `sp` and steer `st`. */
382 #endif /* BCAR_BCAR_H */