LSST Applications g0f08755f38+82efc23009,g12f32b3c4e+e7bdf1200e,g1653933729+a8ce1bb630,g1a0ca8cf93+50eff2b06f,g28da252d5a+52db39f6a5,g2bbee38e9b+37c5a29d61,g2bc492864f+37c5a29d61,g2cdde0e794+c05ff076ad,g3156d2b45e+41e33cbcdc,g347aa1857d+37c5a29d61,g35bb328faa+a8ce1bb630,g3a166c0a6a+37c5a29d61,g3e281a1b8c+fb992f5633,g414038480c+7f03dfc1b0,g41af890bb2+11b950c980,g5fbc88fb19+17cd334064,g6b1c1869cb+12dd639c9a,g781aacb6e4+a8ce1bb630,g80478fca09+72e9651da0,g82479be7b0+04c31367b4,g858d7b2824+82efc23009,g9125e01d80+a8ce1bb630,g9726552aa6+8047e3811d,ga5288a1d22+e532dc0a0b,gae0086650b+a8ce1bb630,gb58c049af0+d64f4d3760,gc28159a63d+37c5a29d61,gcf0d15dbbd+2acd6d4d48,gd7358e8bfb+778a810b6e,gda3e153d99+82efc23009,gda6a2b7d83+2acd6d4d48,gdaeeff99f8+1711a396fd,ge2409df99d+6b12de1076,ge79ae78c31+37c5a29d61,gf0baf85859+d0a5978c5a,gf3967379c6+4954f8c433,gfb92a5be7c+82efc23009,gfec2e1e490+2aaed99252,w.2024.46
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)