LSST Applications g0265f82a02+c6dfa2ddaf,g2079a07aa2+1b2e822518,g2ab2c8e58b+dcaebcd53c,g2bbee38e9b+c6dfa2ddaf,g337abbeb29+c6dfa2ddaf,g3ddfee87b4+971473b56f,g420f1b9425+9c5d1f27f4,g4770a20bdc+7962a82c67,g50ff169b8f+2eb0e556e8,g52b1c1532d+90ebb246c7,g555ede804d+971473b56f,g591dd9f2cf+601419b17e,g5ec818987f+105adce70b,g858d7b2824+dcaebcd53c,g876c692160+5450b3d607,g8a8a8dda67+90ebb246c7,g8cdfe0ae6a+4fd9e222a8,g99cad8db69+ed556ab06a,g9ddcbc5298+a1346535a5,ga1e77700b3+df8f93165b,ga8c6da7877+e280585b77,gae46bcf261+c6dfa2ddaf,gb0e22166c9+8634eb87fb,gb3f2274832+5f6d78177c,gba4ed39666+1ac82b564f,gbb8dafda3b+d1938d02c0,gbeb006f7da+2258dca5ef,gc28159a63d+c6dfa2ddaf,gc86a011abf+dcaebcd53c,gcf0d15dbbd+971473b56f,gd2a12a3803+6772067a4c,gdaeeff99f8+1cafcb7cd4,ge79ae78c31+c6dfa2ddaf,gee10cc3b42+90ebb246c7,gf1cff7945b+dcaebcd53c,w.2024.14
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)