LSST Applications g1653933729+b86e4b8053,g2303c004a3+a99d6d42bd,g28da252d5a+3e0fd49a16,g2bbee38e9b+db5ba6d06c,g2bc492864f+db5ba6d06c,g2cdde0e794+b03b09fb0a,g3156d2b45e+b0ccf75d80,g347aa1857d+db5ba6d06c,g35bb328faa+b86e4b8053,g3a166c0a6a+db5ba6d06c,g3e281a1b8c+c9a36f98fc,g414038480c+70d5606f74,g42360bd172+a99d6d42bd,g461a3dce89+b86e4b8053,g5c9cb89fa5+b7dae1cdc6,g7f102674c4+d27c5e2769,g7f343241b9+516b07f5d7,g80478fca09+ee10125a54,g82479be7b0+11c35b2845,g858d7b2824+a99d6d42bd,g8cd86fa7b1+cb4c24add2,g9125e01d80+b86e4b8053,g979bb04a14+30ae7a28c8,g9ddcbc5298+b4cb6f9276,ga44b09e51c+390e0c76f2,ga5288a1d22+c32cd41bfc,gae0086650b+b86e4b8053,gb58c049af0+2fe46b4317,gc28159a63d+db5ba6d06c,gcd45df26be+a99d6d42bd,gcf0d15dbbd+be93eff7c7,gda6a2b7d83+be93eff7c7,gdaeeff99f8+b9455e0a82,ge5cf12406b+e20940066e,ge79ae78c31+db5ba6d06c,ge7c99ddb59+26aa1bc97f,gf048a9a2f4+f6d4943a12,gf0baf85859+e0282cf37d,w.2024.32
LSST Data Management Base Package
Loading...
Searching...
No Matches
Interval.h
Go to the documentation of this file.
1/*
2 * This file is part of sphgeom.
3 *
4 * Developed for the LSST Data Management System.
5 * This product includes software developed by the LSST Project
6 * (http://www.lsst.org).
7 * See the COPYRIGHT file at the top-level directory of this distribution
8 * for details of code ownership.
9 *
10 * This software is dual licensed under the GNU General Public License and also
11 * under a 3-clause BSD license. Recipients may choose which of these licenses
12 * to use; please see the files gpl-3.0.txt and/or bsd_license.txt,
13 * respectively. If you choose the GPL option then the following text applies
14 * (but note that there is still no warranty even if you opt for BSD instead):
15 *
16 * This program is free software: you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation, either version 3 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28 */
29
30#ifndef LSST_SPHGEOM_INTERVAL_H_
31#define LSST_SPHGEOM_INTERVAL_H_
32
35
36#include <algorithm>
37
38#include "Relationship.h"
39
40
41namespace lsst {
42namespace sphgeom {
43
54template <typename Derived, typename Scalar>
55class Interval {
56public:
58 Interval() : _a(1.0), _b(0.0) {}
59
61 explicit Interval(Scalar x) : _a(x), _b(x) {}
62
64 Interval(Scalar x, Scalar y) : _a(x), _b(y) {}
65
68 bool operator==(Interval const & i) const {
69 return (_a == i._a && _b == i._b) || (i.isEmpty() && isEmpty());
70 }
71
72 bool operator!=(Interval const & i) const { return !(*this == i); }
73
75 bool operator==(Scalar x) const {
76 return (_a == x && _b == x) || (x != x && isEmpty());
77 }
78
79 bool operator!=(Scalar x) const { return !(*this == x); }
80
83 Scalar getA() const { return _a; }
84
87 Scalar getB() const { return _b; }
88
90 bool isEmpty() const {
91 return !(_a <= _b); // returns true when _a and/or _b is NaN
92 }
93
96 Scalar getCenter() const { return 0.5 * (_a + _b); }
97
100 Scalar getSize() const { return _b - _a; }
101
105 bool contains(Scalar x) const {
106 return (_a <= x && x <= _b) || x != x;
107 }
108
109 bool contains(Interval const & x) const {
110 if (x.isEmpty()) {
111 return true;
112 } else if (isEmpty()) {
113 return false;
114 }
115 return _a <= x._a && _b >= x._b;
116 }
118
122 bool isDisjointFrom(Scalar x) const {
123 return !intersects(x);
124 }
125
126 bool isDisjointFrom(Interval const & x) const {
127 if (isEmpty() || x.isEmpty()) {
128 return true;
129 }
130 return _a > x._b || _b < x._a;
131 }
133
137 bool intersects(Scalar x) const { return _a <= x && x <= _b; }
138
139 bool intersects(Interval const & x) const {
140 return !isDisjointFrom(x);
141 }
143
147 bool isWithin(Scalar x) const {
148 return (_a == x && _b == x) || isEmpty();
149 }
150
151 bool isWithin(Interval const & x) const {
152 return x.contains(*this);
153 }
155
160 Relationship relate(Scalar x) const;
163
166 Interval & clipTo(Scalar x) {
167 if (x != x) {
168 _a = x;
169 _b = x;
170 } else {
171 _a = std::max(_a, x);
172 _b = std::min(_b, x);
173 }
174 return *this;
175 }
176
178 if (x.isEmpty()) {
179 *this = x;
180 } else if (!isEmpty()) {
181 _a = std::max(_a, x._a);
182 _b = std::min(_b, x._b);
183 }
184 return *this;
185 }
187
190 Derived clippedTo(Scalar x) const { return Interval(*this).clipTo(x); }
191
192 Derived clippedTo(Interval const & x) const {
193 return Interval(*this).clipTo(x);
194 }
196
199 Interval & expandTo(Scalar x) {
200 if (isEmpty()) {
201 _a = x;
202 _b = x;
203 } else if (x < _a) {
204 _a = x;
205 } else if (x > _b) {
206 _b = x;
207 }
208 return *this;
209 }
210
212 if (isEmpty()) {
213 *this = x;
214 } else if (!x.isEmpty()) {
215 _a = std::min(_a, x._a);
216 _b = std::max(_b, x._b);
217 }
218 return *this;
219 }
221
225 Derived expandedTo(Scalar x) const { return Interval(*this).expandTo(x); }
226
227 Derived expandedTo(Interval const & x) const {
228 return Interval(*this).expandTo(x);
229 }
231
237 Interval & dilateBy(Scalar x) {
238 if (x == x && !isEmpty()) {
239 _a = _a - x;
240 _b = _b + x;
241 }
242 return *this;
243 }
244
245 Interval & erodeBy(Scalar x) { return dilateBy(-x); }
246 Derived dilatedBy(Scalar x) const { return Interval(*this).dilateBy(x); }
247 Derived erodedBy(Scalar x) const { return Interval(*this).erodeBy(x); }
248
249private:
250 Scalar _a;
251 Scalar _b;
252};
253
254
255template <typename Derived, typename Scalar>
257 if (isEmpty()) {
258 if (x != x) {
259 return CONTAINS | DISJOINT | WITHIN;
260 }
261 return DISJOINT | WITHIN;
262 }
263 if (x != x) {
264 return CONTAINS | DISJOINT;
265 }
266 if (_a == x && _b == x) {
267 return CONTAINS | WITHIN;
268 }
269 if (intersects(x)) {
270 return CONTAINS;
271 }
272 return DISJOINT;
273}
274
275template <typename Derived, typename Scalar>
277 Interval<Derived, Scalar> const & x) const
278{
279 if (isEmpty()) {
280 if (x.isEmpty()) {
281 return CONTAINS | DISJOINT | WITHIN;
282 }
283 return DISJOINT | WITHIN;
284 }
285 if (x.isEmpty()) {
286 return CONTAINS | DISJOINT;
287 }
288 if (_a == x._a && _b == x._b) {
289 return CONTAINS | WITHIN;
290 }
291 if (_a > x._b || _b < x._a) {
292 return DISJOINT;
293 }
294 if (_a <= x._a && _b >= x._b) {
295 return CONTAINS;
296 }
297 if (x._a <= _a && x._b >= _b) {
298 return WITHIN;
299 }
300 return INTERSECTS;
301}
302
303}} // namespace lsst::sphgeom
304
305#endif // LSST_SPHGEOM_INTERVAL_H_
This file provides a type alias for describing set relationships.
int y
Definition SpanSet.cc:48
Interval represents a closed interval of the real numbers by its upper and lower bounds.
Definition Interval.h:55
Interval()
This constructor creates an empty interval.
Definition Interval.h:58
bool isDisjointFrom(Scalar x) const
Definition Interval.h:122
Interval(Scalar x, Scalar y)
This constructor creates an interval from the given endpoints.
Definition Interval.h:64
bool isWithin(Scalar x) const
Definition Interval.h:147
Derived dilatedBy(Scalar x) const
Definition Interval.h:246
bool operator==(Scalar x) const
A closed interval is equal to a point x if both endpoints equal x.
Definition Interval.h:75
Derived clippedTo(Interval const &x) const
Definition Interval.h:192
bool operator!=(Scalar x) const
Definition Interval.h:79
Interval & dilateBy(Scalar x)
For positive x, dilateBy morphologically dilates this interval by [-x,x], which is equivalent to the ...
Definition Interval.h:237
bool isWithin(Interval const &x) const
Definition Interval.h:151
bool intersects(Interval const &x) const
Definition Interval.h:139
Scalar getSize() const
getSize returns the size (length, width) of this interval.
Definition Interval.h:100
Derived expandedTo(Interval const &x) const
Definition Interval.h:227
Interval & clipTo(Scalar x)
Definition Interval.h:166
bool isEmpty() const
isEmpty returns true if this interval does not contain any points.
Definition Interval.h:90
Derived expandedTo(Scalar x) const
Definition Interval.h:225
Derived clippedTo(Scalar x) const
Definition Interval.h:190
bool operator!=(Interval const &i) const
Definition Interval.h:72
Relationship relate(Interval const &x) const
Definition Interval.h:276
bool contains(Interval const &x) const
Definition Interval.h:109
Derived erodedBy(Scalar x) const
Definition Interval.h:247
Interval & erodeBy(Scalar x)
Definition Interval.h:245
bool isDisjointFrom(Interval const &x) const
Definition Interval.h:126
Relationship relate(Scalar x) const
Definition Interval.h:256
bool operator==(Interval const &i) const
Two closed intervals are equal if their endpoints are the same, or both are empty.
Definition Interval.h:68
bool intersects(Scalar x) const
Definition Interval.h:137
Scalar getCenter() const
getCenter returns the center of this interval.
Definition Interval.h:96
Scalar getA() const
getA returns the lower endpoint of this interval.
Definition Interval.h:83
Interval & expandTo(Scalar x)
Definition Interval.h:199
Interval & clipTo(Interval const &x)
Definition Interval.h:177
Interval & expandTo(Interval const &x)
Definition Interval.h:211
bool contains(Scalar x) const
Definition Interval.h:105
Scalar getB() const
getB returns the upper endpoint of this interval.
Definition Interval.h:87
Interval(Scalar x)
This constructor creates a closed interval containing only x.
Definition Interval.h:61
T max(T... args)
T min(T... args)