LSST Applications g0dd1f4853b+000f002471,g1635faa6d4+54714fab34,g1653933729+a8ce1bb630,g28da252d5a+640a8c72f3,g2bbee38e9b+c2093a7170,g2bc492864f+c2093a7170,g2cdde0e794+6a4d5c7d0d,g3156d2b45e+07302053f8,g347aa1857d+c2093a7170,g35bb328faa+a8ce1bb630,g3a166c0a6a+c2093a7170,g3e281a1b8c+f03ad10ea9,g4005a62e65+17cd334064,g414038480c+137065b082,g41af890bb2+0645dabe59,g531c6476d5+deede3a0e4,g57cf332d5c+a9d52f23c2,g781aacb6e4+a8ce1bb630,g80478fca09+80d930fbca,g82479be7b0+c2cd99eb83,g858d7b2824+deede3a0e4,g9125e01d80+a8ce1bb630,ga5288a1d22+bfa28a6548,gae0086650b+a8ce1bb630,gb58c049af0+d64f4d3760,gc28159a63d+c2093a7170,gca434f2906+4dfa4c7e5b,gcf0d15dbbd+4cd0bd49fc,gcfacc2a72f+8eff0b70f0,gda3e153d99+deede3a0e4,gda6a2b7d83+4cd0bd49fc,gdaeeff99f8+1711a396fd,gdd07d4df61+5730f16813,ge2409df99d+ccdeb281c7,ge33fd446bb+deede3a0e4,ge79ae78c31+c2093a7170,gf0baf85859+147a0692ba,gf3967379c6+1be82fceb0,v28.0.0.rc1
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)