LSST Applications g1653933729+34a971ddd9,g1a997c3884+34a971ddd9,g28da252d5a+32b12a4112,g2bbee38e9b+ffc559ee46,g2bc492864f+ffc559ee46,g2cdde0e794+704103fe75,g3156d2b45e+6e87dc994a,g347aa1857d+ffc559ee46,g35bb328faa+34a971ddd9,g3a166c0a6a+ffc559ee46,g3e281a1b8c+8ec26ec694,g4005a62e65+ba0306790b,g414038480c+9f5be647b3,g41af890bb2+686ab7cd58,g4e1a3235cc+1fd785b14f,g7b55021d1b+da0d0eec6b,g80478fca09+6e21f9dc20,g82479be7b0+6fa35d7547,g858d7b2824+356eaf3287,g9125e01d80+34a971ddd9,ga5288a1d22+ba1f9bb3ef,gae0086650b+34a971ddd9,gb4748cc0e8+fd26a265a0,gb4ec7eb0ab+356eaf3287,gb58c049af0+ace264a4f2,gc081298178+30f12d1e69,gc28159a63d+ffc559ee46,gcf0d15dbbd+4adb3139bd,gd6b7c0dfd1+aae9efebf5,gda3e153d99+356eaf3287,gda6a2b7d83+4adb3139bd,gdaeeff99f8+7774323b41,gdd5a9049c5+ea78b51778,ge2409df99d+fa478de0b0,ge33fd446bb+356eaf3287,ge79ae78c31+ffc559ee46,gf0baf85859+890af219f9,gf5289d68f6+e11c78aeea,w.2024.41
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)