IDS peak IPL v1.18.0
Loading...
Searching...
No Matches
peak_common_point.hpp
Go to the documentation of this file.
1
5
6#pragma once
7
12
13#include <array>
14#include <sstream>
15
16namespace peak
17{
18namespace common
19{
20
21namespace detail
22{
35template <typename T, size_t dimension>
37{
38public:
39 static_assert(dimension >= 2, "A Point must consist of at least two coordinates (x,y)!");
40 static_assert(std::is_arithmetic<T>::value, "A point must be created from an arithmetic type!");
41
49 constexpr PointXd() = default;
50
58 constexpr explicit PointXd(std::array<T, dimension> coordinates)
59 : m_coordinates{ coordinates }
60 {}
61
69 PEAK_COMMON_NO_DISCARD constexpr T GetX() const
70 {
71 return m_coordinates.at(0);
72 }
73
81 PEAK_COMMON_NO_DISCARD constexpr T GetY() const
82 {
83 return m_coordinates.at(1);
84 }
85
97 template <typename H,
98 size_t d
100 ,
101 typename std::enable_if_t<std::is_integral<H>::value, int> = 0
103 >
104 constexpr bool operator==(const PointXd<H, d>& rhs) const
105 {
106 return std::equal(this->m_coordinates.begin(), this->m_coordinates.end(), rhs.GetCoordinates().begin());
107 }
108
120 template <typename H, size_t d>
121 constexpr bool operator!=(const PointXd<H, d>& rhs) const
122 {
123 return !(*this == rhs);
124 }
125
138 template <typename H,
139 size_t d
141 ,
142 typename std::enable_if_t<std::is_floating_point<H>::value, int> = 0
144 >
145 constexpr bool operator==(const PointXd<H, d>& rhs) const
146 {
147 using common_type = std::common_type_t<T, H>;
148
149 // Unit in the last place
150 constexpr uint32_t ulp = 4;
151
152 return std::equal(this->m_coordinates.begin(), this->m_coordinates.end(), rhs.GetCoordinates().begin(),
153 [ulp](const auto& left, const auto& right) {
154 return detail::AreAlmostEqual<common_type>(static_cast<common_type>(left), static_cast<common_type>(right), ulp);
155 });
156 }
157
165 PEAK_COMMON_NO_DISCARD constexpr std::array<T, dimension> GetCoordinates() const
166 {
167 return m_coordinates;
168 }
169
170private:
171 std::array<T, dimension> m_coordinates{};
172};
173
185template <typename T>
186class PointT : public PointXd<T, 2>
187{
188
189private:
190 using PointType = PointT<T>;
191
192public:
193 static_assert(std::is_arithmetic<T>::value, "Template argument of Point must be arithmetic.");
194
202 constexpr PointT() = default;
203
211 constexpr PointT(T x, T y)
212 : PointXd<T, 2>({ x, y })
213 {}
214
226 friend std::ostream& operator<<(std::ostream& os, PointT point)
227 {
228 return os << "{ x: " << point.GetX() << ", y: " << point.GetY() << " }";
229 }
230
241 {
242 std::ostringstream ss;
243 ss << *this;
244 return ss.str();
245 }
246
247private:
248 friend detail::BackendAccessor<PointT<T>>;
249
250 template <typename U = T, typename std::enable_if<has_c_type_of<PointT<U>>::value, bool>::type = false>
251 constexpr explicit PointT(peak::common::detail::c_type_of_t<PointT<U>> point)
252 : PointT(static_cast<T>(point.x), static_cast<T>(point.y))
253 {}
254
255 template <typename U = T, typename std::enable_if<has_c_type_of<PointT<U>>::value, bool>::type = false>
256 constexpr explicit operator peak::common::detail::c_type_of_t<PointT<U>>() const
257 {
258 return { this->GetX(), this->GetY() };
259 }
260
261 friend detail::BackendAccessor<PointType>;
262};
263
264} // namespace detail
265
279
293
307
308} // namespace common
309} // namespace peak
2D point template type.
Definition peak_common_point.hpp:187
constexpr PointT(T x, T y)
Point constructor from x and y coordinates.
Definition peak_common_point.hpp:211
friend std::ostream & operator<<(std::ostream &os, PointT point)
Stream output operator for PointT.
Definition peak_common_point.hpp:226
constexpr PointT()=default
Default constructor for PointT (2D point).
PEAK_COMMON_NO_DISCARD std::string ToString() const
Returns a string representation of the point.
Definition peak_common_point.hpp:240
PEAK_COMMON_NO_DISCARD constexpr T GetY() const
Returns the y-coordinate (second component).
Definition peak_common_point.hpp:81
constexpr bool operator==(const PointXd< H, d > &rhs) const
Equality operator for comparing integral coordinates.
Definition peak_common_point.hpp:104
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:165
constexpr PointXd(std::array< T, dimension > coordinates)
Constructs a PointXd from an array of coordinates.
Definition peak_common_point.hpp:58
constexpr bool operator!=(const PointXd< H, d > &rhs) const
Inequality operator for comparing coordinates.
Definition peak_common_point.hpp:121
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:69
detail::PointT< int32_t > Point
2D point with 32-bit signed integer coordinates.
Definition peak_common_point.hpp:278
detail::PointT< uint32_t > PointU
2D point with 32-bit unsigned integer coordinates.
Definition peak_common_point.hpp:306
detail::PointT< float > PointF
2D point with single-precision floating-point coordinates.
Definition peak_common_point.hpp:292
Namespace for internal implementation details and low-level types.
Definition peak_common_math.hpp:19
Root namespace for common types and interfaces shared across multiple libraries.
Definition peak_common_math.hpp:17
#define PEAK_COMMON_NO_DISCARD
Definition peak_common_defines.h:89