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);
164 PoseRange(double x, double y, double b, double e);
169 /*! Get heading's begin in the interval [-pi, +pi] radians. */
172 /*! Get heading's end in the interval [-pi, +pi] radians. */
175 void rotate(Point const& c, double const angl);
177 void reflect(Line const& li);
179 friend std::ostream& operator<<(std::ostream& out, PoseRange const& p);
182 /*! \brief Store car size.
184 * - Default is https://en.wikipedia.org/wiki/Fiat_Punto
188 double curb_to_curb_ = 10.820;
189 double width_ = 1.625;
190 double wheelbase_ = 2.450;
191 double distance_to_front_ = 3.105;
192 double length_ = 3.760;
194 /*! Get curb-to-curb distance. */
197 /*! Set curb-to-curb distance. */
198 void ctc(double ctc);
200 /*! Get wheelbase. */
203 /*! Set wheelbase. */
216 void len(double len);
218 /*! Get distance from rear axle to front. */
221 /*! Set distance from rear axle to front. */
224 /*! Get distance from rear axle to rear. */
227 /*! \brief Get minimum turning radius.
229 * Please, note that the method returns really _minimum turning radius_,
230 * which is the distance from the rear axle center to the center of
231 * left or right rotation given by the kinematics constrants, i.e.
232 * _wheelbase_ and _curb-to-curb_ distance.
234 * Sometimes _minimum turning radius_ is not radius, not minimum, or not
235 * turning. In this method, _minimum turning radius_ is minimum turning
240 /*! \brief Return inner radius.
242 * The inner radius is the distance from minimum turning radius circle
243 * center to the nearest point on the car. In this case, the nearest
244 * points on the car are rear axle endpoints.
246 double iradi() const;
248 /*! \brief Return outer front radius.
250 * The outer front radius is the distance from minimum turning radius
251 * circle center to the farthest point on the front (from the rear axle
252 * view) part of the car.
254 double ofradi() const;
256 /*! \brief Return outer rear radius.
258 * The outer rear radius is the distance from minimum turning radius
259 * circle center to the farthest point on the rear (from the rear axle
260 * view) part of the car.
262 double orradi() const;
264 /*! \brief Return length of perfect parking slot.
266 * The width of the slot is the same as the width of the car.
268 * \see Simon R. Blackburn *The Geometry of Perfect Parking*
269 * \see https://www.ma.rhul.ac.uk/SRBparking
271 double perfect_parking_slot_len() const;
274 /*! Store car motion. */
293 /*! \brief Geometrical computations of a bicycle car.
295 * - `x()` and `y()` methods returns coordinates of rear axle center.
297 class BicycleCar : public virtual Pose, public virtual CarSize,
298 public virtual CarMove {
301 /*! \brief Return `true` if `this` can drive to `p` trivially.
303 * Trivially means that `this` can drive to `p` by line segment - circle
304 * arc - line segment.
306 * \param p `PoseRange` (resp. `Pose`) to achieve.
308 bool drivable(PoseRange const& p) const;
309 bool drivable(Pose const& p) const;
311 /*! Set maximum steering angle. */
312 void set_max_steer();
314 /*! Get frame's left front x coordinate. */
317 /*! Get frame's left front y coordinate. */
320 /*! Get frame's left rear x coordinate. */
323 /*! Get frame's left rear y coordinate. */
326 /*! Get frame's right rear x coordinate. */
329 /*! Get frame's right rear y coordinate. */
332 /*! Get frame's right front x coordinate. */
335 /*! Get frame's right front y coordinate. */
338 /*! Get frame's left front point. */
341 /*! Get frame's left rear point. */
344 /*! Get frame's right rear point. */
347 /*! Get frame's right front point. */
350 /*! Get frame's left side. */
353 /*! Get frame's rear side. */
356 /*! Get frame's right side. */
359 /*! Get frame's front side. */
362 /*! Get rear axle's left x coordinate. */
365 /*! Get rear axle's left y coordinate. */
368 /*! Get rear axle's right x coordinate. */
371 /*! Get rear axle's right y coordinate. */
374 /*! Min. turning radius circle center on left. */
377 /*! Min. turning radius circle center on rigth. */
380 /*! Next car position based on speed `sp` and steer `st`. */
385 #endif /* BCAR_BCAR_H */