Recti 1.2.4
Loading...
Searching...
No Matches
generic.hpp
Go to the documentation of this file.
1
5#pragma once
6
7// #include <algorithm> // import std::min, std::max
8#include <cassert>
9// #include <cmath> // for abs
10
11namespace recti {
23 template <typename T> constexpr auto my_abs(const T& value) -> T {
24 return value < 0 ? -value : value;
25 }
26
53 template <typename U1, typename U2> //
54 constexpr auto overlap(const U1& lhs, const U2& rhs) -> bool {
55 if constexpr (requires { lhs.overlaps(rhs); }) {
56 return lhs.overlaps(rhs);
57 } else if constexpr (requires { rhs.overlaps(lhs); }) {
58 return rhs.overlaps(lhs);
59 } else /* constexpr */ {
60 return lhs == rhs;
61 }
62 }
63
89 template <typename U1, typename U2> //
90 constexpr auto contain(const U1& lhs, const U2& rhs) -> bool {
91 if constexpr (requires { lhs.contains(rhs); }) {
92 return lhs.contains(rhs);
93 } else if constexpr (requires { rhs.contains(lhs); }) {
94 return false;
95 } else /* constexpr */ {
96 return lhs == rhs;
97 }
98 }
99
127 template <typename U1, typename U2> //
128 constexpr auto intersection(const U1& lhs, const U2& rhs) {
129 if constexpr (requires { lhs.intersect_with(rhs); }) {
130 return lhs.intersect_with(rhs);
131 } else if constexpr (requires { rhs.intersect_with(lhs); }) {
132 return rhs.intersect_with(lhs);
133 } else /* constexpr */ {
134 assert(lhs == rhs);
135 return lhs;
136 }
137 }
138
165 template <typename U1, typename U2> //
166 constexpr auto min_dist(const U1& lhs, const U2& rhs) {
167 if constexpr (requires { lhs.min_dist_with(rhs); }) {
168 return lhs.min_dist_with(rhs);
169 } else if constexpr (requires { rhs.min_dist_with(lhs); }) {
170 return rhs.min_dist_with(lhs);
171 } else /* constexpr */ {
172 return my_abs(lhs - rhs);
173 }
174 }
175
194 template <typename U1, typename U2> //
195 constexpr auto min_dist_change(U1& lhs, U2& rhs) {
196 if constexpr (requires { lhs.min_dist_change_with(rhs); }) {
197 return lhs.min_dist_change_with(rhs);
198 } else if constexpr (requires { rhs.min_dist_change_with(lhs); }) {
199 return rhs.min_dist_change_with(lhs);
200 } else /* constexpr */ {
201 return my_abs(lhs - rhs);
202 }
203 }
204
227 template <typename U1, typename U2> constexpr auto nearest(const U1& lhs, const U2& rhs) {
228 if constexpr (requires { lhs.nearest_to(rhs); }) {
229 return lhs.nearest_to(rhs);
230 } else {
231 return lhs;
232 }
233 }
234
245 template <typename U> constexpr auto measure_of(const U& obj) {
246 if constexpr (requires { obj.measure(); }) {
247 return obj.measure();
248 } else {
249 return 1;
250 }
251 }
252
263 template <typename U> constexpr auto center(const U& obj) {
264 if constexpr (requires { obj.get_center(); }) {
265 return obj.get_center();
266 } else {
267 return obj;
268 }
269 }
270
281 template <typename U> constexpr auto lower(const U& obj) {
282 if constexpr (requires { obj.lower_corner(); }) {
283 return obj.lower_corner();
284 } else {
285 return obj;
286 }
287 }
288
299 template <typename U> constexpr auto upper(const U& obj) {
300 if constexpr (requires { obj.upper_corner(); }) {
301 return obj.upper_corner();
302 } else {
303 return obj;
304 }
305 }
306} // namespace recti
Definition svg_utils.hpp:12
constexpr auto contain(const U1 &lhs, const U2 &rhs) -> bool
Check if one object contains another object.
Definition generic.hpp:90
constexpr auto nearest(const U1 &lhs, const U2 &rhs)
Returns the nearest point on lhs to rhs.
Definition generic.hpp:227
constexpr auto intersection(const U1 &lhs, const U2 &rhs)
Computes the intersection of two objects.
Definition generic.hpp:128
constexpr auto min_dist_change(U1 &lhs, U2 &rhs)
Calculates the minimum distance between two objects lhs and rhs, with the ability to handle a change ...
Definition generic.hpp:195
constexpr auto min_dist(const U1 &lhs, const U2 &rhs)
Calculates the minimum distance between two objects lhs and rhs.
Definition generic.hpp:166
constexpr auto lower(const U &obj)
Calculates the lower corner of an object.
Definition generic.hpp:281
constexpr auto my_abs(const T &value) -> T
Compute the absolute value of a number.
Definition generic.hpp:23
constexpr auto overlap(const U1 &lhs, const U2 &rhs) -> bool
Checks if two objects overlap.
Definition generic.hpp:54
constexpr auto measure_of(const U &obj)
Calculates the measure (length, area, etc.) of an object.
Definition generic.hpp:245
constexpr auto upper(const U &obj)
Calculates the upper corner of an object.
Definition generic.hpp:299
constexpr auto center(const U &obj)
Calculates the center of an object.
Definition generic.hpp:263