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 Translate self.
63 * \param p `Point` offset to translate by.
65 void translate(Point const& p);
67 /*! \brief Rotate self around the point.
69 \param c Rotation center `Point`.
70 \param angl Angle of rotation.
72 void rotate(Point const& c, double const angl);
74 /*! \brief Compute reflection of `this` around the `Line`.
76 * \param li The plane to reflect around is given by `li`.
78 void reflect(Line const& li);
80 /*! Return Euclidean distance to `p`. */
81 double edist(Point const& p) const;
83 bool operator==(Point const& p);
84 friend std::ostream& operator<<(std::ostream& out, Point const& p);
94 Line(Point const& fp, Point const& lp);
96 /*! Get beginning point. */
102 /*! Get intersection point. */
105 /*! Get intersection point. */
108 /*! \brief Return if `this` line intersects with line `li`.
110 * If the method returns `true`, the intersection `Point` is available
113 * \see https://en.wikipedia.org/wiki/Line%E2%80%93line_intersection
115 * \param li The line to check the intersection with.
117 bool intersects_with(Line const& li);
119 /*! \brief Return intersections of `this` (infinite) line and circle.
121 * If the method returns `true`, the intersection `Point`s are available
122 * in `this->i1()` and `this->i2()`.
124 * \see https://mathworld.wolfram.com/Circle-LineIntersection.html
126 * \param c Circle center.
127 * \param r Circle radius.
129 bool intersects_with(Point const& c, double const r);
135 friend std::ostream& operator<<(std::ostream& out, Line const& li);
138 /*! Store coordinates `x`, `y`, and heading `h`. */
139 class Pose : public virtual Point {
144 Pose(double x, double y, double h);
146 /*! Get heading in the interval [-pi, +pi] radians. */
149 /*! Set heading in radians. It's recomputed to [-pi, +pi]. */
152 /*! Set pose (`x`, `y`, and `h`.) */
153 void set_pose(Pose const& p);
155 void rotate(Point const& c, double const angl);
157 void reflect(Line const& li);
159 bool operator==(Pose const& p);
160 friend std::ostream& operator<<(std::ostream& out, Pose const& p);
163 class PoseRange : public virtual Pose {
169 PoseRange(Pose bp, Pose ep);
170 PoseRange(double x, double y, double b, double e);
175 /*! Get heading's begin in the interval [-pi, +pi] radians. */
178 /*! Get heading's end in the interval [-pi, +pi] radians. */
181 void translate(Point const& p);
182 void rotate(Point const& c, double const angl);
183 void reflect(Line const& li);
185 friend std::ostream& operator<<(std::ostream& out, PoseRange const& p);
188 /*! \brief Store car size.
190 * - Default is https://en.wikipedia.org/wiki/Fiat_Punto
194 double curb_to_curb_ = 10.820;
195 double width_ = 1.625;
196 double wheelbase_ = 2.450;
197 double distance_to_front_ = 3.105;
198 double length_ = 3.760;
200 /*! Get curb-to-curb distance. */
203 /*! Set curb-to-curb distance. */
204 void ctc(double ctc);
206 /*! Get wheelbase. */
209 /*! Set wheelbase. */
222 void len(double len);
224 /*! Get distance from rear axle to front. */
227 /*! Set distance from rear axle to front. */
230 /*! Get distance from rear axle to rear. */
233 /*! \brief Get minimum turning radius.
235 * Please, note that the method returns really _minimum turning radius_,
236 * which is the distance from the rear axle center to the center of
237 * left or right rotation given by the kinematics constrants, i.e.
238 * _wheelbase_ and _curb-to-curb_ distance.
240 * Sometimes _minimum turning radius_ is not radius, not minimum, or not
241 * turning. In this method, _minimum turning radius_ is minimum turning
246 /*! \brief Return inner radius.
248 * The inner radius is the distance from minimum turning radius circle
249 * center to the nearest point on the car. In this case, the nearest
250 * points on the car are rear axle endpoints.
252 double iradi() const;
254 /*! \brief Return outer front radius.
256 * The outer front radius is the distance from minimum turning radius
257 * circle center to the farthest point on the front (from the rear axle
258 * view) part of the car.
260 double ofradi() const;
262 /*! \brief Return outer rear radius.
264 * The outer rear radius is the distance from minimum turning radius
265 * circle center to the farthest point on the rear (from the rear axle
266 * view) part of the car.
268 double orradi() const;
270 /*! \brief Return length of perfect parking slot.
272 * The width of the slot is the same as the width of the car.
274 * \see Simon R. Blackburn *The Geometry of Perfect Parking*
275 * \see https://www.ma.rhul.ac.uk/SRBparking
277 double perfect_parking_slot_len() const;
280 /*! Store car motion. */
299 /*! \brief Geometrical computations of a bicycle car.
301 * - `x()` and `y()` methods returns coordinates of rear axle center.
303 class BicycleCar : public virtual Pose, public virtual CarSize,
304 public virtual CarMove {
307 /*! \brief Return `true` if `this` can drive to `p` trivially.
309 * Trivially means that `this` can drive to `p` by line segment - circle
310 * arc - line segment.
312 * \param p `PoseRange` (resp. `Pose`) to achieve.
314 bool drivable(PoseRange const& p) const;
315 bool drivable(Pose const& p) const;
317 /*! Set maximum steering angle. */
318 void set_max_steer();
320 /*! Get frame's left front x coordinate. */
323 /*! Get frame's left front y coordinate. */
326 /*! Get frame's left rear x coordinate. */
329 /*! Get frame's left rear y coordinate. */
332 /*! Get frame's right rear x coordinate. */
335 /*! Get frame's right rear y coordinate. */
338 /*! Get frame's right front x coordinate. */
341 /*! Get frame's right front y coordinate. */
344 /*! Get frame's left front point. */
347 /*! Get frame's left rear point. */
350 /*! Get frame's right rear point. */
353 /*! Get frame's right front point. */
356 /*! Get frame's left side. */
359 /*! Get frame's rear side. */
362 /*! Get frame's right side. */
365 /*! Get frame's front side. */
368 /*! Get rear axle's left x coordinate. */
371 /*! Get rear axle's left y coordinate. */
374 /*! Get rear axle's right x coordinate. */
377 /*! Get rear axle's right y coordinate. */
380 /*! Min. turning radius circle center on left. */
383 /*! Min. turning radius circle center on rigth. */
386 /*! Next car position based on speed `sp` and steer `st`. */
391 #endif /* BCAR_BCAR_H */