22 * Copyright (c) 2009-2011, Fabian Greif
33 * Copyright (c) 2012, Martin Rosekeit
44 * Copyright (c) 2012, Niklas Hauser
5+ * Copyright (c) 2022, Thomas Sommer
56 *
67 * This file is part of the modm project.
78 *
1011 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
1112 */
1213// ----------------------------------------------------------------------------
13-
14- #ifndef MODM_LINE_SEGMENT_2D_HPP
15- #define MODM_LINE_SEGMENT_2D_HPP
14+ #pragma once
1615
1716#include " geometric_traits.hpp"
18-
19- #include " vector.hpp"
2017#include " point_set_2d.hpp"
18+ #include " vector.hpp"
2119
2220namespace modm
2321{
24- // forward declaration
25- template <typename T>
26- class Circle2D ;
22+ // forward declaration
23+ template <typename T>
24+ class Circle2D ;
25+
26+ template <typename T>
27+ class Polygon2D ;
28+
29+ /* *
30+ * \brief Line segment
31+ *
32+ * \author Fabian Greif
33+ * \ingroup modm_math_geometry
34+ */
35+ template <typename T = int16_t >
36+ class LineSegment2D
37+ {
38+ public:
39+ using WideType = GeometricTraits<T>::WideType;
40+ using FloatType = GeometricTraits<T>::FloatType;
41+
42+ public:
43+ modm::Vector<T, 2 > startPoint, endPoint;
44+
45+ constexpr LineSegment2D () = default;
46+
47+ constexpr LineSegment2D (const Vector<T, 2 >& start, const Vector<T, 2 >& end)
48+ : startPoint(start), endPoint(end)
49+ {}
50+
51+ constexpr bool operator ==(const LineSegment2D& other) const = default ;
52+
53+ constexpr LineSegment2D& operator +=(const Vector<T, 2 >& vector)
54+ {
55+ startPoint += vector;
56+ endPoint += vector;
57+ return *this ;
58+ }
59+
60+ constexpr LineSegment2D& operator -=(const Vector<T, 2 >& vector)
61+ {
62+ startPoint -= vector;
63+ endPoint -= vector;
64+ return *this ;
65+ }
66+
67+ constexpr Vector<T, 2 >
68+ getDirectionVector () const
69+ { return endPoint - startPoint; }
2770
28- template <typename T>
29- class Polygon2D ;
71+ constexpr T
72+ getLength () const
73+ {
74+ Vector<T, 2 > directionVector = this ->endPoint - this ->startPoint ;
75+ return directionVector.getLength ();
76+ }
77+
78+ // / Shortest distance to a point
79+ constexpr T
80+ getDistanceTo (const Vector<T, 2 >& point) const ;
81+
82+ // / Calculate the point on the line segment closes to the given point
83+ constexpr Vector<T, 2 >
84+ getClosestPointTo (const Vector<T, 2 >& point) const ;
3085
3186 /* *
32- * \brief Line segment
87+ * \brief Check if two line segments intersect
3388 *
34- * \author Fabian Greif
35- * \ingroup modm_math_geometry
89+ * Uses Vector2D::ccw() to check if any intersection exists.
3690 */
37- template <typename T = int16_t >
38- class LineSegment2D
91+ constexpr bool
92+ intersects (const LineSegment2D& other) const ;
93+
94+ constexpr bool
95+ intersects (const Polygon2D<T>& polygon) const
96+ { return polygon.intersects (*this ); }
97+
98+ // / Calculate the intersection point
99+ constexpr bool
100+ getIntersections (const LineSegment2D& other, PointSet2D<T>& intersectionPoints) const ;
101+
102+ /* *
103+ * \brief Calculate the intersection point(s)
104+ * \see http://local.wasp.uwa.edu.au/~pbourke/geometry/sphereline/
105+ */
106+ constexpr bool
107+ getIntersections (const Circle2D<T>& circle, PointSet2D<T>& intersectionPoints) const ;
108+
109+ constexpr bool
110+ getIntersections (const Polygon2D<T>& polygon, PointSet2D<T>& intersectionPoints) const
111+ { return polygon.getIntersections (*this , intersectionPoints); }
112+
113+ // deprecated setters and getters
114+ [[deprecated(" Assign public member directly!" )]]
115+ void setStartPoint (const Vector<T, 2 >& point)
116+ { this ->startPoint = point; }
117+
118+ [[deprecated(" Assign public member directly!" )]]
119+ const Vector<T, 2 >& getStartPoint () const
120+ { return this ->startPoint ; }
121+
122+ [[deprecated(" Assign public member directly!" )]]
123+ void setEndPoint (const Vector<T, 2 >& point)
124+ { this ->endPoint = point; }
125+
126+ [[deprecated(" Assign public member directly!" )]]
127+ const Vector<T, 2 >& getEndPoint () const
128+ { return this ->endPoint ; }
129+
130+ [[deprecated(" Assign public member directly!" )]]
131+ void set (const Vector<T, 2 >& start, const Vector<T, 2 >& end)
39132 {
40- public:
41- typedef typename GeometricTraits<T>::WideType WideType;
42- typedef typename GeometricTraits<T>::FloatType FloatType;
43-
44- public:
45- constexpr LineSegment2D () = default;
46-
47- constexpr LineSegment2D (const Vector<T, 2 >& start, const Vector<T, 2 >& end)
48- : startPoint(start), endPoint(end)
49- {}
50-
51-
52- // / Set the starting point of the line segment
53- inline void
54- setStartPoint (const Vector<T, 2 >& point);
55-
56- inline const Vector<T, 2 >&
57- getStartPoint () const ;
58-
59- // / Set the end point of the line segment
60- inline void
61- setEndPoint (const Vector<T, 2 >& point);
62-
63- inline const Vector<T, 2 >&
64- getEndPoint () const ;
65-
66- inline void
67- set (const Vector<T, 2 >& start, const Vector<T, 2 >& end);
68-
69- void
70- translate (const Vector<T, 2 >& vector);
71-
72- /* *
73- * \brief Length of the line segment
74- */
75- T
76- getLength () const ;
77-
78- Vector<T, 2 >
79- getDirectionVector () const ;
80-
81- // / Shortest distance to a point
82- const T
83- getDistanceTo (const Vector<T, 2 >& point) const ;
84-
85- // / Calculate the point on the line segment closes to the given point
86- const Vector<T, 2 >
87- getClosestPointTo (const Vector<T, 2 >& point) const ;
88-
89- /* *
90- * \brief Check if two line segments intersect
91- *
92- * Uses Vector2D::ccw() to check if any intersection exists.
93- */
94- bool
95- intersects (const LineSegment2D& other) const ;
96-
97- // / Check if a intersection exists
98- bool
99- intersects (const Polygon2D<T>& polygon) const ;
100-
101- /* *
102- * \brief Calculate the intersection point
103- */
104- bool
105- getIntersections (const LineSegment2D& other,
106- PointSet2D<T>& intersectionPoints) const ;
107-
108- /* *
109- * \brief Calculate the intersection point(s)
110- *
111- * \see http://local.wasp.uwa.edu.au/~pbourke/geometry/sphereline/
112- */
113- bool
114- getIntersections (const Circle2D<T>& circle,
115- PointSet2D<T>& intersectionPoints) const ;
116-
117- bool
118- getIntersections (const Polygon2D<T>& polygon,
119- PointSet2D<T>& intersectionPoints) const ;
120-
121- bool
122- operator == (const LineSegment2D &other) const ;
123-
124- bool
125- operator != (const LineSegment2D &other) const ;
126-
127- protected:
128- modm::Vector<T, 2 > startPoint;
129- modm::Vector<T, 2 > endPoint;
130- };
131- }
133+ this ->startPoint = start;
134+ this ->endPoint = end;
135+ }
132136
133- #include " circle_2d.hpp"
134- #include " polygon_2d.hpp"
137+ // deprecated translate
138+ [[deprecated(" Use LineSegment2D<T>::operator+= instead!" )]]
139+ void translate (const Vector<T, 2 >& vector)
140+ { operator +=(vector); }
135141
136- #include " line_segment_2d_impl.hpp"
142+ };
143+ } // namespace modm
137144
138- #endif // MODM_LINE_SEGMENT_2D_HPP
145+ #include " circle_2d.hpp"
146+ #include " line_segment_2d_impl.hpp"
147+ #include " polygon_2d.hpp"
0 commit comments