IDS peak AFL v1.8.0
Loading...
Searching...
No Matches
peak_common_point.hpp
Go to the documentation of this file.
1
10
11#pragma once
12
17
18#include <array>
19#include <sstream>
20
21namespace peak
22{
23namespace common
24{
25
26namespace detail
27{
40template <typename T, size_t dimension>
42{
43public:
44 static_assert(dimension >= 2, "A Point must consist of at least two coordinates (x,y)!");
45 static_assert(std::is_arithmetic<T>::value, "A point must be created from an arithmetic type!");
46
54 constexpr PointXd() = default;
55
63 constexpr explicit PointXd(std::array<T, dimension> coordinates)
64 : m_coordinates{ coordinates }
65 {}
66
74 PEAK_COMMON_NO_DISCARD constexpr T GetX() const
75 {
76 return m_coordinates.at(0);
77 }
78
86 PEAK_COMMON_NO_DISCARD constexpr T GetY() const
87 {
88 return m_coordinates.at(1);
89 }
90
102 template <typename H,
103 size_t d
105 ,
106 typename std::enable_if_t<std::is_integral<H>::value, int> = 0
108 >
109 constexpr bool operator==(const PointXd<H, d>& rhs) const
110 {
111 return std::equal(this->m_coordinates.begin(), this->m_coordinates.end(), rhs.GetCoordinates().begin());
112 }
113
125 template<typename H, size_t d>
126 constexpr bool operator!=(const PointXd<H, d>& rhs) const
127 {
128 return !(*this == rhs);
129 }
130
143 template <typename H,
144 size_t d
146 ,
147 typename std::enable_if_t<std::is_floating_point<H>::value, int> = 0
149 >
150 constexpr bool operator==(const PointXd<H, d>& rhs) const
151 {
152 using common_type = std::common_type_t<T, H>;
153
154 // Unit in the last place
155 constexpr uint32_t ulp = 4;
156
157 return std::equal(this->m_coordinates.begin(), this->m_coordinates.end(), rhs.GetCoordinates().begin(),
158 [ulp](const auto& left, const auto& right) {
159 return detail::AreAlmostEqual<common_type>(static_cast<common_type>(left), static_cast<common_type>(right), ulp);
160 });
161 }
162
170 PEAK_COMMON_NO_DISCARD constexpr std::array<T, dimension> GetCoordinates() const
171 {
172 return m_coordinates;
173 }
174
175private:
176 std::array<T, dimension> m_coordinates{};
177};
178
190template <typename T>
191class PointT : public PointXd<T, 2>
192{
193
194private:
195 using PointType = PointT<T>;
196
197public:
198 static_assert(std::is_arithmetic<T>::value, "Template argument of Point must be arithmetic.");
199
207 constexpr PointT() = default;
208
216 constexpr PointT(T x, T y)
217 : PointXd<T, 2>({ x, y })
218 {}
219
231 friend std::ostream& operator<<(std::ostream& os, PointT point)
232 {
233 return os << "{ x: " << point.GetX() << ", y: " << point.GetY() << " }";
234 }
235
246 {
247 std::ostringstream ss;
248 ss << *this;
249 return ss.str();
250 }
251
252private:
253 friend detail::BackendAccessor<PointT<T>>;
254
255 template <typename U = T, typename std::enable_if<has_c_type_of<PointT<U>>::value, bool>::type = false>
256 constexpr explicit PointT(peak::common::detail::c_type_of_t<PointT<U>> point)
257 : PointT(static_cast<T>(point.x), static_cast<T>(point.y))
258 {}
259
260 template <typename U = T, typename std::enable_if<has_c_type_of<PointT<U>>::value, bool>::type = false>
261 constexpr explicit operator peak::common::detail::c_type_of_t<PointT<U>>() const
262 {
263 return { this->GetX(), this->GetY() };
264 }
265
266 friend detail::BackendAccessor<PointType>;
267};
268
269} // namespace detail
270
284
298
312
313} // namespace common
314} // namespace peak
2D point template type.
Definition peak_common_point.hpp:192
constexpr PointT()=default
Default constructor for PointT (2D point).
constexpr PointT(T x, T y)
Point constructor from x and y coordinates.
Definition peak_common_point.hpp:216
friend std::ostream & operator<<(std::ostream &os, PointT point)
Stream output operator for PointT.
Definition peak_common_point.hpp:231
PEAK_COMMON_NO_DISCARD std::string ToString() const
Returns a string representation of the point.
Definition peak_common_point.hpp:245
constexpr PointXd(std::array< T, dimension > coordinates)
Constructs a PointXd from an array of coordinates.
Definition peak_common_point.hpp:63
PEAK_COMMON_NO_DISCARD constexpr std::array< T, dimension > GetCoordinates() const
Returns the coordinates of the point as an array.
Definition peak_common_point.hpp:170
constexpr bool operator!=(const PointXd< H, d > &rhs) const
Inequality operator for comparing coordinates.
Definition peak_common_point.hpp:126
constexpr bool operator==(const PointXd< H, d > &rhs) const
Equality operator for comparing integral coordinates.
Definition peak_common_point.hpp:109
PEAK_COMMON_NO_DISCARD constexpr T GetY() const
Returns the y-coordinate (second component).
Definition peak_common_point.hpp:86
constexpr PointXd()=default
Default constructor for PointXd.
PEAK_COMMON_NO_DISCARD constexpr T GetX() const
Returns the x-coordinate (first component).
Definition peak_common_point.hpp:74
#define PEAK_COMMON_NO_DISCARD
Compatability macro for the nodiscard attribute.
Definition peak_common_defines.hpp:91
detail::PointT< float > PointF
2D point with single-precision floating-point coordinates.
Definition peak_common_point.hpp:297
detail::PointT< int32_t > Point
2D point with 32-bit signed integer coordinates.
Definition peak_common_point.hpp:283
detail::PointT< uint32_t > PointU
2D point with 32-bit unsigned integer coordinates.
Definition peak_common_point.hpp:311
Namespace for internal implementation details and low-level types.
Definition peak_common_math.hpp:24
Root namespace for common types and interfaces shared across multiple libraries.
Definition peak_common_math.hpp:22