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 /*! Return Euclidean distance to `p`. */
69 double edist(Point const& p) const;
71 friend std::ostream& operator<<(std::ostream& out, Point const& p);
81 Line(Point const& fp, Point const& lp);
83 /*! Get beginning point. */
89 /*! Get intersection point. */
92 /*! Get intersection point. */
95 /*! \brief Return if `this` line intersects with line `li`.
97 * If the method returns `true`, the intersection `Point` is available
100 * \see https://en.wikipedia.org/wiki/Line%E2%80%93line_intersection
102 * \param li The line to check the intersection with.
104 bool intersects_with(Line const& li);
106 /*! \brief Return intersections of `this` (infinite) line and circle.
108 * If the method returns `true`, the intersection `Point`s are available
109 * in `this->i1()` and `this->i2()`.
111 * \see https://mathworld.wolfram.com/Circle-LineIntersection.html
113 * \param c Circle center.
114 * \param r Circle radius.
116 bool intersects_with(Point const& c, double const r);
122 friend std::ostream& operator<<(std::ostream& out, Line const& li);
125 /*! Store coordinates `x`, `y`, and heading `h`. */
126 class Pose : public virtual Point {
131 Pose(double x, double y, double h);
133 /*! Get heading in the interval [-pi, +pi] radians. */
136 /*! Set heading in radians. It's recomputed to [-pi, +pi]. */
139 /*! Set pose (`x`, `y`, and `h`.) */
140 void set_pose(Pose const& p);
142 void rotate(Point const& c, double const angl);
144 friend std::ostream& operator<<(std::ostream& out, Pose const& p);
147 class PoseRange : public virtual Pose {
152 /*! Get heading's begin in the interval [-pi, +pi] radians. */
155 /*! Set heading's begin in radians. It's recomputed to [-pi, +pi]. */
158 /*! Get heading's end in the interval [-pi, +pi] radians. */
161 /*! Set heading's end in radians. It's recomputed to [-pi, +pi]. */
164 void rotate(Point const& c, double const angl);
166 friend std::ostream& operator<<(std::ostream& out, PoseRange const& p);
169 /*! \brief Store car size.
171 * - Default is https://en.wikipedia.org/wiki/Fiat_Punto
175 double curb_to_curb_ = 10.820;
176 double width_ = 1.625;
177 double wheelbase_ = 2.450;
178 double distance_to_front_ = 3.105;
179 double length_ = 3.760;
181 /*! Get curb-to-curb distance. */
184 /*! Set curb-to-curb distance. */
185 void ctc(double ctc);
187 /*! Get wheelbase. */
190 /*! Set wheelbase. */
203 void len(double len);
205 /*! Get distance from rear axle to front. */
208 /*! Set distance from rear axle to front. */
211 /*! Get distance from rear axle to rear. */
214 /*! \brief Get minimum turning radius.
216 * Please, note that the method returns really _minimum turning radius_,
217 * which is the distance from the rear axle center to the center of
218 * left or right rotation given by the kinematics constrants, i.e.
219 * _wheelbase_ and _curb-to-curb_ distance.
221 * Sometimes _minimum turning radius_ is not radius, not minimum, or not
222 * turning. In this method, _minimum turning radius_ is minimum turning
227 /*! \brief Return inner radius.
229 * The inner radius is the distance from minimum turning radius circle
230 * center to the nearest point on the car. In this case, the nearest
231 * points on the car are rear axle endpoints.
233 double iradi() const;
235 /*! \brief Return outer front radius.
237 * The outer front radius is the distance from minimum turning radius
238 * circle center to the farthest point on the front (from the rear axle
239 * view) part of the car.
241 double ofradi() const;
243 /*! \brief Return outer rear radius.
245 * The outer rear radius is the distance from minimum turning radius
246 * circle center to the farthest point on the rear (from the rear axle
247 * view) part of the car.
249 double orradi() const;
251 /*! \brief Return length of perfect parking slot.
253 * The width of the slot is the same as the width of the car.
255 * \see Simon R. Blackburn *The Geometry of Perfect Parking*
256 * \see https://www.ma.rhul.ac.uk/SRBparking
258 double perfect_parking_slot_len() const;
261 /*! Store car motion. */
280 /*! \brief Geometrical computations of a bicycle car.
282 * - `x()` and `y()` methods returns coordinates of rear axle center.
284 class BicycleCar : public virtual Pose, public virtual CarSize,
285 public virtual CarMove {
288 /*! \brief Return `true` if `this` can drive to `p` trivially.
290 * Trivially means that `this` can drive to `p` by line segment - circle
291 * arc - line segment.
293 * \param p `PoseRange` (resp. `Pose`) to achieve.
295 bool drivable(PoseRange const& p) const;
296 bool drivable(Pose const& p) const;
298 /*! Set maximum steering angle. */
299 void set_max_steer();
301 /*! Get frame's left front x coordinate. */
304 /*! Get frame's left front y coordinate. */
307 /*! Get frame's left rear x coordinate. */
310 /*! Get frame's left rear y coordinate. */
313 /*! Get frame's right rear x coordinate. */
316 /*! Get frame's right rear y coordinate. */
319 /*! Get frame's right front x coordinate. */
322 /*! Get frame's right front y coordinate. */
325 /*! Get frame's left front point. */
328 /*! Get frame's left rear point. */
331 /*! Get frame's right rear point. */
334 /*! Get frame's right front point. */
337 /*! Get frame's left side. */
340 /*! Get frame's rear side. */
343 /*! Get frame's right side. */
346 /*! Get frame's front side. */
349 /*! Get rear axle's left x coordinate. */
352 /*! Get rear axle's left y coordinate. */
355 /*! Get rear axle's right x coordinate. */
358 /*! Get rear axle's right y coordinate. */
361 /*! Min. turning radius circle center on left. */
364 /*! Min. turning radius circle center on rigth. */
367 /*! Next car position based on speed `sp` and steer `st`. */
372 #endif /* BCAR_BCAR_H */