LSSTApplications  19.0.0-10-g4a5fae6+3,19.0.0-10-g920eed2,19.0.0-11-g48a0200+2,19.0.0-18-gfc4e62b+16,19.0.0-2-g3b2f90d+2,19.0.0-2-gd671419+6,19.0.0-20-g5a5a17ab+14,19.0.0-21-g2644856+17,19.0.0-24-g0913cb1,19.0.0-24-g878c510+4,19.0.0-25-g6c8df7140+1,19.0.0-25-gb330496+4,19.0.0-3-g2b32d65+6,19.0.0-3-g8227491+15,19.0.0-3-g9c54d0d+15,19.0.0-3-gca68e65+11,19.0.0-3-gcfc5f51+6,19.0.0-3-ge110943+14,19.0.0-3-ge74d124,19.0.0-30-g9c3fd16+5,19.0.0-4-g06f5963+6,19.0.0-4-g10df615,19.0.0-4-g3d16501+17,19.0.0-4-g4a9c019+6,19.0.0-4-g5a8b323,19.0.0-4-g66397f0+1,19.0.0-4-g8557e14,19.0.0-4-g8964aba+16,19.0.0-4-ge404a01+15,19.0.0-5-g40f3a5a,19.0.0-5-g4db63b3,19.0.0-5-gb9eeb60,19.0.0-5-gfb03ce7+16,19.0.0-6-gbaebbfb+15,19.0.0-61-gec4c6e08+5,19.0.0-7-g039c0b5+15,19.0.0-7-gbea9075+4,19.0.0-7-gc567de5+16,19.0.0-72-g37abf38+2,19.0.0-9-g463f923+15,v20.0.0.rc1
LSSTDataManagementBasePackage
Interval.h
Go to the documentation of this file.
1 /*
2  * LSST Data Management System
3  * Copyright 2014-2015 AURA/LSST.
4  *
5  * This product includes software developed by the
6  * LSST Project (http://www.lsst.org/).
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the LSST License Statement and
19  * the GNU General Public License along with this program. If not,
20  * see <https://www.lsstcorp.org/LegalNotices/>.
21  */
22 
23 #ifndef LSST_SPHGEOM_INTERVAL_H_
24 #define LSST_SPHGEOM_INTERVAL_H_
25 
28 
29 #include <algorithm>
30 
31 #include "Relationship.h"
32 
33 
34 namespace lsst {
35 namespace sphgeom {
36 
47 template <typename Derived, typename Scalar>
48 class Interval {
49 public:
51  Interval() : _a(1.0), _b(0.0) {}
52 
54  explicit Interval(Scalar x) : _a(x), _b(x) {}
55 
57  Interval(Scalar x, Scalar y) : _a(x), _b(y) {}
58 
61  bool operator==(Interval const & i) const {
62  return (_a == i._a && _b == i._b) || (i.isEmpty() && isEmpty());
63  }
64 
65  bool operator!=(Interval const & i) const { return !(*this == i); }
66 
68  bool operator==(Scalar x) const {
69  return (_a == x && _b == x) || (x != x && isEmpty());
70  }
71 
72  bool operator!=(Scalar x) const { return !(*this == x); }
73 
76  Scalar getA() const { return _a; }
77 
80  Scalar getB() const { return _b; }
81 
83  bool isEmpty() const {
84  return !(_a <= _b); // returns true when _a and/or _b is NaN
85  }
86 
89  Scalar getCenter() const { return 0.5 * (_a + _b); }
90 
93  Scalar getSize() const { return _b - _a; }
94 
98  bool contains(Scalar x) const {
99  return (_a <= x && x <= _b) || x != x;
100  }
101 
102  bool contains(Interval const & x) const {
103  if (x.isEmpty()) {
104  return true;
105  } else if (isEmpty()) {
106  return false;
107  }
108  return _a <= x._a && _b >= x._b;
109  }
111 
115  bool isDisjointFrom(Scalar x) const {
116  return !intersects(x);
117  }
118 
119  bool isDisjointFrom(Interval const & x) const {
120  if (isEmpty() || x.isEmpty()) {
121  return true;
122  }
123  return _a > x._b || _b < x._a;
124  }
126 
130  bool intersects(Scalar x) const { return _a <= x && x <= _b; }
131 
132  bool intersects(Interval const & x) const {
133  return !isDisjointFrom(x);
134  }
136 
140  bool isWithin(Scalar x) const {
141  return (_a == x && _b == x) || isEmpty();
142  }
143 
144  bool isWithin(Interval const & x) const {
145  return x.contains(*this);
146  }
148 
154  Relationship relate(Interval const & x) const;
156 
160  if (x != x) {
161  _a = x;
162  _b = x;
163  } else {
164  _a = std::max(_a, x);
165  _b = std::min(_b, x);
166  }
167  return *this;
168  }
169 
170  Interval & clipTo(Interval const & x) {
171  if (x.isEmpty()) {
172  *this = x;
173  } else if (!isEmpty()) {
174  _a = std::max(_a, x._a);
175  _b = std::min(_b, x._b);
176  }
177  return *this;
178  }
180 
183  Derived clippedTo(Scalar x) const { return Interval(*this).clipTo(x); }
184 
185  Derived clippedTo(Interval const & x) const {
186  return Interval(*this).clipTo(x);
187  }
189 
193  if (isEmpty()) {
194  _a = x;
195  _b = x;
196  } else if (x < _a) {
197  _a = x;
198  } else if (x > _b) {
199  _b = x;
200  }
201  return *this;
202  }
203 
205  if (isEmpty()) {
206  *this = x;
207  } else if (!x.isEmpty()) {
208  _a = std::min(_a, x._a);
209  _b = std::max(_b, x._b);
210  }
211  return *this;
212  }
214 
218  Derived expandedTo(Scalar x) const { return Interval(*this).expandTo(x); }
219 
220  Derived expandedTo(Interval const & x) const {
221  return Interval(*this).expandTo(x);
222  }
224 
231  if (x == x && !isEmpty()) {
232  _a = _a - x;
233  _b = _b + x;
234  }
235  return *this;
236  }
237 
238  Interval & erodeBy(Scalar x) { return dilateBy(-x); }
239  Derived dilatedBy(Scalar x) const { return Interval(*this).dilateBy(x); }
240  Derived erodedBy(Scalar x) const { return Interval(*this).erodeBy(x); }
241 
242 private:
243  Scalar _a;
244  Scalar _b;
245 };
246 
247 
248 template <typename Derived, typename Scalar>
250  if (isEmpty()) {
251  if (x != x) {
252  return CONTAINS | DISJOINT | WITHIN;
253  }
254  return DISJOINT | WITHIN;
255  }
256  if (x != x) {
257  return CONTAINS | DISJOINT;
258  }
259  if (_a == x && _b == x) {
260  return CONTAINS | WITHIN;
261  }
262  if (intersects(x)) {
263  return CONTAINS;
264  }
265  return DISJOINT;
266 }
267 
268 template <typename Derived, typename Scalar>
270  Interval<Derived, Scalar> const & x) const
271 {
272  if (isEmpty()) {
273  if (x.isEmpty()) {
274  return CONTAINS | DISJOINT | WITHIN;
275  }
276  return DISJOINT | WITHIN;
277  }
278  if (x.isEmpty()) {
279  return CONTAINS | DISJOINT;
280  }
281  if (_a == x._a && _b == x._b) {
282  return CONTAINS | WITHIN;
283  }
284  if (_a > x._b || _b < x._a) {
285  return DISJOINT;
286  }
287  if (_a <= x._a && _b >= x._b) {
288  return CONTAINS;
289  }
290  if (x._a <= _a && x._b >= _b) {
291  return WITHIN;
292  }
293  return INTERSECTS;
294 }
295 
296 }} // namespace lsst::sphgeom
297 
298 #endif // LSST_SPHGEOM_INTERVAL_H_
lsst::sphgeom::Interval::getCenter
Scalar getCenter() const
getCenter returns the center of this interval.
Definition: Interval.h:89
y
int y
Definition: SpanSet.cc:49
lsst::sphgeom::Interval::clippedTo
Derived clippedTo(Scalar x) const
Definition: Interval.h:183
std::bitset
STL class.
lsst::sphgeom::Interval::clipTo
Interval & clipTo(Interval const &x)
Definition: Interval.h:170
lsst::sphgeom::Interval::erodeBy
Interval & erodeBy(Scalar x)
Definition: Interval.h:238
lsst::sphgeom::Interval::isWithin
bool isWithin(Interval const &x) const
Definition: Interval.h:144
lsst::sphgeom::Interval
Interval represents a closed interval of the real numbers by its upper and lower bounds.
Definition: Interval.h:48
lsst::meas::modelfit::Scalar
double Scalar
Typedefs to be used for probability and parameter values.
Definition: common.h:44
Relationship.h
This file provides a type alias for describing set relationships.
lsst::sphgeom::Interval::clipTo
Interval & clipTo(Scalar x)
Definition: Interval.h:159
lsst::sphgeom::Interval::intersects
bool intersects(Interval const &x) const
Definition: Interval.h:132
lsst::sphgeom::Interval::getSize
Scalar getSize() const
getSize returns the size (length, width) of this interval.
Definition: Interval.h:93
lsst::sphgeom::Interval::isDisjointFrom
bool isDisjointFrom(Interval const &x) const
Definition: Interval.h:119
lsst::sphgeom::Interval::operator==
bool operator==(Scalar x) const
A closed interval is equal to a point x if both endpoints equal x.
Definition: Interval.h:68
lsst::sphgeom::Interval::getA
Scalar getA() const
getA returns the lower endpoint of this interval.
Definition: Interval.h:76
lsst::sphgeom::Interval::isEmpty
bool isEmpty() const
isEmpty returns true if this interval does not contain any points.
Definition: Interval.h:83
lsst::sphgeom::Interval::Interval
Interval()
This constructor creates an empty interval.
Definition: Interval.h:51
lsst::sphgeom::Interval::Interval
Interval(Scalar x)
This constructor creates a closed interval containing only x.
Definition: Interval.h:54
lsst::sphgeom::Interval::relate
Relationship relate(Scalar x) const
Definition: Interval.h:249
lsst::sphgeom::Interval::erodedBy
Derived erodedBy(Scalar x) const
Definition: Interval.h:240
x
double x
Definition: ChebyshevBoundedField.cc:277
lsst::sphgeom::Interval::getB
Scalar getB() const
getB returns the upper endpoint of this interval.
Definition: Interval.h:80
lsst::sphgeom::Interval::isDisjointFrom
bool isDisjointFrom(Scalar x) const
Definition: Interval.h:115
lsst::sphgeom::Interval::contains
bool contains(Scalar x) const
Definition: Interval.h:98
lsst::sphgeom::Interval::isWithin
bool isWithin(Scalar x) const
Definition: Interval.h:140
lsst::sphgeom::Interval::relate
Relationship relate(Interval const &x) const
Definition: Interval.h:269
lsst
A base class for image defects.
Definition: imageAlgorithm.dox:1
lsst::sphgeom::Interval::contains
bool contains(Interval const &x) const
Definition: Interval.h:102
std::min
T min(T... args)
lsst::sphgeom::Interval::expandedTo
Derived expandedTo(Scalar x) const
Definition: Interval.h:218
lsst::sphgeom::Interval::operator!=
bool operator!=(Scalar x) const
Definition: Interval.h:72
lsst::sphgeom::Interval::clippedTo
Derived clippedTo(Interval const &x) const
Definition: Interval.h:185
lsst::sphgeom::Interval::Interval
Interval(Scalar x, Scalar y)
This constructor creates an interval from the given endpoints.
Definition: Interval.h:57
lsst::sphgeom::Interval::dilateBy
Interval & dilateBy(Scalar x)
For positive x, dilateBy morphologically dilates this interval by [-x,x], which is equivalent to the ...
Definition: Interval.h:230
lsst::sphgeom::Interval::operator!=
bool operator!=(Interval const &i) const
Definition: Interval.h:65
lsst::sphgeom::Interval::expandTo
Interval & expandTo(Scalar x)
Definition: Interval.h:192
std::max
T max(T... args)
lsst::sphgeom::Interval::operator==
bool operator==(Interval const &i) const
Two closed intervals are equal if their endpoints are the same, or both are empty.
Definition: Interval.h:61
lsst::sphgeom::Interval::intersects
bool intersects(Scalar x) const
Definition: Interval.h:130
lsst::sphgeom::Interval::dilatedBy
Derived dilatedBy(Scalar x) const
Definition: Interval.h:239
lsst::sphgeom::Interval::expandedTo
Derived expandedTo(Interval const &x) const
Definition: Interval.h:220
lsst::sphgeom::Interval::expandTo
Interval & expandTo(Interval const &x)
Definition: Interval.h:204