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 bool operator==(Point const& p);
78 friend std::ostream& operator<<(std::ostream& out, Point const& p);
88 Line(Point const& fp, Point const& lp);
90 /*! Get beginning point. */
96 /*! Get intersection point. */
99 /*! Get intersection point. */
102 /*! \brief Return if `this` line intersects with line `li`.
104 * If the method returns `true`, the intersection `Point` is available
107 * \see https://en.wikipedia.org/wiki/Line%E2%80%93line_intersection
109 * \param li The line to check the intersection with.
111 bool intersects_with(Line const& li);
113 /*! \brief Return intersections of `this` (infinite) line and circle.
115 * If the method returns `true`, the intersection `Point`s are available
116 * in `this->i1()` and `this->i2()`.
118 * \see https://mathworld.wolfram.com/Circle-LineIntersection.html
120 * \param c Circle center.
121 * \param r Circle radius.
123 bool intersects_with(Point const& c, double const r);
129 friend std::ostream& operator<<(std::ostream& out, Line const& li);
132 /*! Store coordinates `x`, `y`, and heading `h`. */
133 class Pose : public virtual Point {
138 Pose(double x, double y, double h);
140 /*! Get heading in the interval [-pi, +pi] radians. */
143 /*! Set heading in radians. It's recomputed to [-pi, +pi]. */
146 /*! Set pose (`x`, `y`, and `h`.) */
147 void set_pose(Pose const& p);
149 void rotate(Point const& c, double const angl);
151 void reflect(Line const& li);
153 bool operator==(Pose const& p);
154 friend std::ostream& operator<<(std::ostream& out, Pose const& p);
157 class PoseRange : public virtual Pose {
163 PoseRange(Pose bp, Pose ep);
168 /*! Get heading's begin in the interval [-pi, +pi] radians. */
171 /*! Get heading's end in the interval [-pi, +pi] radians. */
174 void rotate(Point const& c, double const angl);
176 void reflect(Line const& li);
178 friend std::ostream& operator<<(std::ostream& out, PoseRange const& p);
181 /*! \brief Store car size.
183 * - Default is https://en.wikipedia.org/wiki/Fiat_Punto
187 double curb_to_curb_ = 10.820;
188 double width_ = 1.625;
189 double wheelbase_ = 2.450;
190 double distance_to_front_ = 3.105;
191 double length_ = 3.760;
193 /*! Get curb-to-curb distance. */
196 /*! Set curb-to-curb distance. */
197 void ctc(double ctc);
199 /*! Get wheelbase. */
202 /*! Set wheelbase. */
215 void len(double len);
217 /*! Get distance from rear axle to front. */
220 /*! Set distance from rear axle to front. */
223 /*! Get distance from rear axle to rear. */
226 /*! \brief Get minimum turning radius.
228 * Please, note that the method returns really _minimum turning radius_,
229 * which is the distance from the rear axle center to the center of
230 * left or right rotation given by the kinematics constrants, i.e.
231 * _wheelbase_ and _curb-to-curb_ distance.
233 * Sometimes _minimum turning radius_ is not radius, not minimum, or not
234 * turning. In this method, _minimum turning radius_ is minimum turning
239 /*! \brief Return inner radius.
241 * The inner radius is the distance from minimum turning radius circle
242 * center to the nearest point on the car. In this case, the nearest
243 * points on the car are rear axle endpoints.
245 double iradi() const;
247 /*! \brief Return outer front radius.
249 * The outer front radius is the distance from minimum turning radius
250 * circle center to the farthest point on the front (from the rear axle
251 * view) part of the car.
253 double ofradi() const;
255 /*! \brief Return outer rear radius.
257 * The outer rear radius is the distance from minimum turning radius
258 * circle center to the farthest point on the rear (from the rear axle
259 * view) part of the car.
261 double orradi() const;
263 /*! \brief Return length of perfect parking slot.
265 * The width of the slot is the same as the width of the car.
267 * \see Simon R. Blackburn *The Geometry of Perfect Parking*
268 * \see https://www.ma.rhul.ac.uk/SRBparking
270 double perfect_parking_slot_len() const;
273 /*! Store car motion. */
292 /*! \brief Geometrical computations of a bicycle car.
294 * - `x()` and `y()` methods returns coordinates of rear axle center.
296 class BicycleCar : public virtual Pose, public virtual CarSize,
297 public virtual CarMove {
300 /*! \brief Return `true` if `this` can drive to `p` trivially.
302 * Trivially means that `this` can drive to `p` by line segment - circle
303 * arc - line segment.
305 * \param p `PoseRange` (resp. `Pose`) to achieve.
307 bool drivable(PoseRange const& p) const;
308 bool drivable(Pose const& p) const;
310 /*! Set maximum steering angle. */
311 void set_max_steer();
313 /*! Get frame's left front x coordinate. */
316 /*! Get frame's left front y coordinate. */
319 /*! Get frame's left rear x coordinate. */
322 /*! Get frame's left rear y coordinate. */
325 /*! Get frame's right rear x coordinate. */
328 /*! Get frame's right rear y coordinate. */
331 /*! Get frame's right front x coordinate. */
334 /*! Get frame's right front y coordinate. */
337 /*! Get frame's left front point. */
340 /*! Get frame's left rear point. */
343 /*! Get frame's right rear point. */
346 /*! Get frame's right front point. */
349 /*! Get frame's left side. */
352 /*! Get frame's rear side. */
355 /*! Get frame's right side. */
358 /*! Get frame's front side. */
361 /*! Get rear axle's left x coordinate. */
364 /*! Get rear axle's left y coordinate. */
367 /*! Get rear axle's right x coordinate. */
370 /*! Get rear axle's right y coordinate. */
373 /*! Min. turning radius circle center on left. */
376 /*! Min. turning radius circle center on rigth. */
379 /*! Next car position based on speed `sp` and steer `st`. */
384 #endif /* BCAR_BCAR_H */