Recti 1.2.4
Loading...
Searching...
No Matches
manhattan_arc.hpp
Go to the documentation of this file.
1
5#pragma once
6
7// #include <algorithm> // import std::min, std::max
8#include <utility> // import std::move
9
10#include "interval.hpp"
11#include "point.hpp"
12
13namespace recti {
14
15#pragma pack(push, 1)
41 template <typename T1, typename T2 = T1> class ManhattanArc {
42 public:
44
45 public:
49 constexpr ManhattanArc() : impl{T1{}, T2{}} {}
50
63 constexpr ManhattanArc(T1 xcoord, T2 ycoord) : impl{std::move(xcoord), std::move(ycoord)} {}
64
71 static constexpr auto from_point(const Point<T1, T2>& pt) {
72 auto pt_xformed = pt.rotates();
73 return ManhattanArc<T1, T2>{pt_xformed.xcoord(), pt_xformed.ycoord()};
74 }
75
88 static constexpr auto construct(const T1& xcoord, const T2& ycoord) -> ManhattanArc {
89 return ManhattanArc{xcoord - ycoord, xcoord + ycoord};
90 }
91
96
108 template <typename U1, typename U2>
109 constexpr auto operator==(const ManhattanArc<U1, U2>& rhs) const -> bool {
110 return this->impl == rhs.impl;
111 }
112
126 template <typename U1, typename U2>
127 constexpr auto operator!=(const ManhattanArc<U1, U2>& rhs) const -> bool {
128 return this->impl != rhs.impl;
129 }
130
143 template <typename U1, typename U2> //
144 constexpr auto overlaps(const ManhattanArc<U1, U2>& other) const -> bool {
145 return this->impl.overlaps(other.impl);
146 }
147
164 template <typename U1, typename U2> //
165 constexpr auto intersect_with(const ManhattanArc<U1, U2>& other) const {
166 auto xcoord = intersection(this->impl.xcoord(), other.impl.xcoord());
167 auto ycoord = intersection(this->impl.ycoord(), other.impl.ycoord());
168 return ManhattanArc<decltype(xcoord), decltype(ycoord)>{std::move(xcoord),
169 std::move(ycoord)};
170 }
171
186 template <typename U1, typename U2> //
187 constexpr auto min_dist_with(const ManhattanArc<U1, U2>& other) const {
188 return std::max(min_dist(this->impl.xcoord(), other.impl.xcoord()),
189 min_dist(this->impl.ycoord(), other.impl.ycoord()));
190 }
191
203 template <typename R> //
204 constexpr auto enlarge_with(const R& alpha) const {
205 auto xcoord = enlarge(this->impl.xcoord(), alpha);
206 auto ycoord = enlarge(this->impl.ycoord(), alpha);
207 return ManhattanArc<decltype(xcoord), decltype(ycoord)>{std::move(xcoord),
208 std::move(ycoord)};
209 }
210
216 constexpr auto get_center() const {
217 auto m = impl.get_center();
218 return m.inv_rotates();
219 }
220
226 constexpr auto get_lower_corner() const {
227 auto m = impl.lower_corner();
228 return m.inv_rotates();
229 }
230
236 constexpr auto get_upper_corner() const {
237 auto m = impl.upper_corner();
238 return m.inv_rotates();
239 }
240
247 template <typename U1, typename U2>
248 constexpr auto nearest_point_to(const Point<U1, U2>& other) const {
249 auto ms = ManhattanArc<U1, U2>::from_point(other);
250 auto distance = this->min_dist_with(ms);
251 auto trr = ms.enlarge_with(distance);
252 auto lb = impl.lower_corner();
253 auto ub = impl.upper_corner();
254 auto m = impl.get_center();
255 if (trr.impl.contains(lb)) {
256 m = lb;
257 } else if (trr.impl.contains(ub)) {
258 m = ub;
259 }
260 return m.inv_rotates();
261 }
262
270 template <typename U1, typename U2>
271 constexpr auto merge_with(const ManhattanArc<U1, U2>& other, int alpha) const {
272 auto distance = min_dist_with(other);
273 auto trr1 = this->enlarge_with(alpha);
274 auto trr2 = other.enlarge_with(distance - alpha);
275 auto localimpl = trr1.impl.intersect_with(trr2.impl);
276 auto x = localimpl.xcoord();
277 auto y = localimpl.ycoord();
278 return ManhattanArc<decltype(x), decltype(y)>{std::move(x), std::move(y)};
279 }
280
293 template <class Stream>
294 friend auto operator<<(Stream& out, const ManhattanArc& manhattan_arc) -> Stream& {
295 out << "/" << manhattan_arc.impl.xcoord() << ", " << manhattan_arc.impl.ycoord() << "/";
296 return out;
297 }
298 };
299#pragma pack(pop)
300
301} // namespace recti
Merging Object (for deferred-merge embedding (DME) algorithm)
Definition manhattan_arc.hpp:41
static constexpr auto from_point(const Point< T1, T2 > &pt)
Construct a ManhattanArc from a Point.
Definition manhattan_arc.hpp:71
constexpr auto merge_with(const ManhattanArc< U1, U2 > &other, int alpha) const
Merge with another ManhattanArc using a given alpha.
Definition manhattan_arc.hpp:271
constexpr auto enlarge_with(const R &alpha) const
Enlarge the ManhattanArc object by a given scale factor.
Definition manhattan_arc.hpp:204
constexpr auto operator==(const ManhattanArc< U1, U2 > &rhs) const -> bool
Compares two ManhattanArc objects for equality.
Definition manhattan_arc.hpp:109
constexpr auto get_center() const
Calculate the center of the ManhattanArc.
Definition manhattan_arc.hpp:216
constexpr auto operator!=(const ManhattanArc< U1, U2 > &rhs) const -> bool
Not equal to.
Definition manhattan_arc.hpp:127
Point< T1, T2 > impl
Definition manhattan_arc.hpp:43
constexpr auto get_lower_corner() const
Calculate the lower corner of the ManhattanArc.
Definition manhattan_arc.hpp:226
constexpr auto intersect_with(const ManhattanArc< U1, U2 > &other) const
Compute the intersection of two ManhattanArc objects.
Definition manhattan_arc.hpp:165
constexpr auto overlaps(const ManhattanArc< U1, U2 > &other) const -> bool
Check if two ManhattanArc objects overlap.
Definition manhattan_arc.hpp:144
constexpr auto nearest_point_to(const Point< U1, U2 > &other) const
Find the nearest point to a given Point.
Definition manhattan_arc.hpp:248
friend auto operator<<(Stream &out, const ManhattanArc &manhattan_arc) -> Stream &
Overload the stream insertion operator << to output a ManhattanArc object.
Definition manhattan_arc.hpp:294
constexpr ManhattanArc(T1 xcoord, T2 ycoord)
Construct a new ManhattanArc object.
Definition manhattan_arc.hpp:63
constexpr auto get_upper_corner() const
Calculate the upper corner of the ManhattanArc.
Definition manhattan_arc.hpp:236
constexpr ManhattanArc()
Default constructor.
Definition manhattan_arc.hpp:49
constexpr auto min_dist_with(const ManhattanArc< U1, U2 > &other) const
Compute the minimum distance between the x and y coordinates of two ManhattanArc objects.
Definition manhattan_arc.hpp:187
static constexpr auto construct(const T1 &xcoord, const T2 &ycoord) -> ManhattanArc
Construct a new ManhattanArc object from the given x and y coordinates.
Definition manhattan_arc.hpp:88
Point.
Definition point.hpp:30
constexpr auto rotates() const -> Point< T1, T2 >
Rotate the point by 45 degrees (used for ManhattanArc).
Definition point.hpp:378
constexpr auto xcoord() const -> const T1 &
Gets the x coordinate of this Point.
Definition point.hpp:72
constexpr auto ycoord() const -> const T2 &
Gets the y coordinate of this Point.
Definition point.hpp:78
constexpr auto overlaps(const Point< U1, U2 > &other_point) const -> bool
Definition point.hpp:255
Interval (range) template class with arithmetic and set operations.
Definition svg_utils.hpp:12
constexpr auto enlarge(const U1 &left, const U2 &right)
Enlarges an interval or scalar value by adding and subtracting a given value.
Definition interval.hpp:579
constexpr auto intersection(const U1 &lhs, const U2 &rhs)
Computes the intersection of two objects.
Definition generic.hpp:128
constexpr auto min_dist(const U1 &lhs, const U2 &rhs)
Calculates the minimum distance between two objects lhs and rhs.
Definition generic.hpp:166
2D Point template class supporting intervals, arithmetic, and geometric queries.