Line data Source code
1 : #ifndef FFCF1757_A52C_4BEF_BFD6_2475D08B37C6
2 : #define FFCF1757_A52C_4BEF_BFD6_2475D08B37C6
3 :
4 : /// \file point2d.h
5 : /// \brief 2D point class with named coordinate access
6 : ///
7 : /// This file defines Point2d, a 2D point class that inherits from Vector<ValueType_, 2>
8 : /// and provides convenient named access to x and y coordinates.
9 : ///
10 : /// \note Point2d is a thin wrapper around Vector that adds semantic meaning
11 : /// and named accessors for 2D geometric operations.
12 :
13 : #include "base/first_include.h" // IWYU pragma: keep
14 : #include "math/linalg/vector.hpp" // IWYU pragma: keep
15 :
16 : namespace tracking
17 : {
18 : namespace math
19 : {
20 :
21 : // TODO(matthias): add downcast from Point3d
22 : // TODO(matthias): add interface contract
23 :
24 : /// \brief 2D point with named coordinate access
25 : ///
26 : /// Point2d represents a point in 2D space with x and y coordinates.
27 : /// It inherits from Vector<ValueType_, 2> and provides convenient named
28 : /// accessors for the coordinates, making geometric code more readable.
29 : ///
30 : /// All Vector operations are available, plus coordinate-specific accessors.
31 : ///
32 : /// \tparam ValueType_ The atomic data type of internal elements
33 : ///
34 : /// Example usage:
35 : /// \code{.cpp}
36 : /// Point2d<double> p = Point2d<double>::FromValues(1.0, 2.0);
37 : /// double x = p.x(); // 1.0
38 : /// double y = p.y(); // 2.0
39 : /// p.x() = 3.0; // modify x coordinate
40 : /// \endcode
41 : ///
42 : /// \see Point3d for 3D points
43 : /// \see Vector for underlying vector operations
44 : template <typename ValueType_>
45 44 : class Point2d: public Vector<ValueType_, 2>
46 : {
47 : public:
48 : /// \brief Type of the parent Vector class
49 : using BaseVector = Vector<ValueType_, 2>;
50 :
51 : // unhide ctor of base class to allow implicit call in derived default ctors
52 41 : using BaseVector::BaseVector;
53 :
54 : /// \brief Construct a new Point 2d<ValueType_> object
55 : /// \param[in] other A base class object
56 : explicit Point2d(const BaseVector& other)
57 : : BaseVector{other}
58 : {
59 : }
60 :
61 : /// \brief Move construct a new Point 2d<ValueType_> object
62 : /// \param[in] other A base class object
63 : explicit Point2d(BaseVector&& other) noexcept
64 : : BaseVector{std::move(other)}
65 : {
66 : }
67 :
68 : /// \brief Create a 2D point from coordinate values
69 : ///
70 : /// Static factory method to create a Point2d from individual x and y values.
71 : /// This is the preferred way to construct points with explicit coordinates.
72 : ///
73 : /// \param[in] x The x-coordinate value
74 : /// \param[in] y The y-coordinate value
75 : /// \return Point2d with the specified coordinates
76 : ///
77 : /// Example:
78 : /// \code{.cpp}
79 : /// auto point = Point2d<double>::FromValues(1.5, -2.3);
80 : /// \endcode
81 : static auto FromValues(const ValueType_ x, const ValueType_ y) -> Point2d;
82 :
83 : /// \brief Read access to x-coordinate
84 : ///
85 : /// Provides read-only access to the x-coordinate (first component).
86 : ///
87 : /// \return The x-coordinate value
88 : [[nodiscard]] auto x() const -> ValueType_;
89 :
90 : /// \brief Read access to y-coordinate
91 : ///
92 : /// Provides read-only access to the y-coordinate (second component).
93 : ///
94 : /// \return The y-coordinate value
95 : [[nodiscard]] auto y() const -> ValueType_;
96 :
97 : /// \brief Write access to x-coordinate
98 : ///
99 : /// Provides read-write access to the x-coordinate (first component).
100 : ///
101 : /// \return Reference to the x-coordinate for modification
102 : [[nodiscard]] auto x() -> ValueType_&;
103 :
104 : /// \brief Write access to y-coordinate
105 : ///
106 : /// Provides read-write access to the y-coordinate (second component).
107 : ///
108 : /// \return Reference to the y-coordinate for modification
109 : [[nodiscard]] auto y() -> ValueType_&;
110 :
111 : private:
112 : /// \brief hide inherited at_unsafe to prevent wrong access
113 : using BaseVector::at_unsafe;
114 : };
115 : ;
116 :
117 : template <typename ValueType_>
118 83 : inline auto Point2d<ValueType_>::FromValues(const ValueType_ x, const ValueType_ y) -> Point2d
119 : {
120 83 : Point2d tmp{};
121 166 : tmp.x() = x;
122 166 : tmp.y() = y;
123 83 : return tmp;
124 : }
125 :
126 : template <typename ValueType_>
127 42 : inline auto Point2d<ValueType_>::x() const -> ValueType_
128 : {
129 42 : return BaseVector::at_unsafe(0);
130 : }
131 :
132 : template <typename ValueType_>
133 42 : inline auto Point2d<ValueType_>::y() const -> ValueType_
134 : {
135 42 : return BaseVector::at_unsafe(1);
136 : }
137 :
138 : template <typename ValueType_>
139 125 : inline auto Point2d<ValueType_>::x() -> ValueType_&
140 : {
141 124 : return BaseVector::at_unsafe(0);
142 : }
143 :
144 : template <typename ValueType_>
145 85 : inline auto Point2d<ValueType_>::y() -> ValueType_&
146 : {
147 85 : return BaseVector::at_unsafe(1);
148 : }
149 :
150 : } // namespace math
151 : } // namespace tracking
152 :
153 : #endif // FFCF1757_A52C_4BEF_BFD6_2475D08B37C6
|