LSSTApplications  19.0.0-14-gb0260a2+72efe9b372,20.0.0+7927753e06,20.0.0+8829bf0056,20.0.0+995114c5d2,20.0.0+b6f4b2abd1,20.0.0+bddc4f4cbe,20.0.0-1-g253301a+8829bf0056,20.0.0-1-g2b7511a+0d71a2d77f,20.0.0-1-g5b95a8c+7461dd0434,20.0.0-12-g321c96ea+23efe4bbff,20.0.0-16-gfab17e72e+fdf35455f6,20.0.0-2-g0070d88+ba3ffc8f0b,20.0.0-2-g4dae9ad+ee58a624b3,20.0.0-2-g61b8584+5d3db074ba,20.0.0-2-gb780d76+d529cf1a41,20.0.0-2-ged6426c+226a441f5f,20.0.0-2-gf072044+8829bf0056,20.0.0-2-gf1f7952+ee58a624b3,20.0.0-20-geae50cf+e37fec0aee,20.0.0-25-g3dcad98+544a109665,20.0.0-25-g5eafb0f+ee58a624b3,20.0.0-27-g64178ef+f1f297b00a,20.0.0-3-g4cc78c6+e0676b0dc8,20.0.0-3-g8f21e14+4fd2c12c9a,20.0.0-3-gbd60e8c+187b78b4b8,20.0.0-3-gbecbe05+48431fa087,20.0.0-38-ge4adf513+a12e1f8e37,20.0.0-4-g97dc21a+544a109665,20.0.0-4-gb4befbc+087873070b,20.0.0-4-gf910f65+5d3db074ba,20.0.0-5-gdfe0fee+199202a608,20.0.0-5-gfbfe500+d529cf1a41,20.0.0-6-g64f541c+d529cf1a41,20.0.0-6-g9a5b7a1+a1cd37312e,20.0.0-68-ga3f3dda+5fca18c6a4,20.0.0-9-g4aef684+e18322736b,w.2020.45
LSSTDataManagementBasePackage
Interval.cc
Go to the documentation of this file.
1 /*
2  * Developed for the LSST Data Management System.
3  * This product includes software developed by the LSST Project
4  * (https://www.lsst.org).
5  * See the COPYRIGHT file at the top-level directory of this distribution
6  * for details of code ownership.
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 GNU General Public License
19  * along with this program. If not, see <https://www.gnu.org/licenses/>.
20  */
21 
22 #include <cmath>
23 #include <limits>
24 #include <type_traits>
25 
26 #include "boost/format.hpp"
27 
28 #include "lsst/utils/hashCombine.h"
29 #include "lsst/geom/Interval.h"
30 
31 namespace lsst {
32 namespace geom {
33 
34 namespace {
35 
36 using BigElement = long long;
37 
38 // Templated so we can check both floating point and BigElement values.
39 // Should never be invoked with IntervalI::Element values, as that won't
40 // accomplish anything.
41 template <typename T>
42 void checkForOverflow(T x, char const* where) {
43  static_assert(sizeof(T) > sizeof(IntervalI::Element) || !std::is_integral<T>::value);
46  throw LSST_EXCEPT(pex::exceptions::OverflowError,
47  (boost::format("Integer overflow (%d) in interval %s.") % x % where).str());
48  }
49 }
50 
51 } // namespace
52 
54  return _fromMinMaxChecked(static_cast<BigElement>(min), static_cast<BigElement>(max));
55 }
56 
58  if (size <= 0) {
59  return IntervalI();
60  }
61  BigElement max = static_cast<BigElement>(min) + static_cast<BigElement>(size) - 1;
62  checkForOverflow(max, "maximum");
63  return IntervalI(min, size);
64 }
65 
67  if (size <= 0) {
68  return IntervalI();
69  }
70  BigElement min = static_cast<BigElement>(max) - static_cast<BigElement>(size) + 1;
71  checkForOverflow(min, "minimum");
72  return IntervalI(static_cast<Element>(min), size);
73 }
74 
76  if (size <= 0) {
77  return IntervalI();
78  }
79  double min = center - 0.5 * size;
80  // compensate for IntervalI's coordinate conventions (where max = min + size - 1)
81  min += 0.5;
82  checkForOverflow(min, "minimum");
83  checkForOverflow(min + size - 1, "maximum");
84  return IntervalI(static_cast<BigElement>(min), size);
85 }
86 
87 IntervalI::IntervalI(IntervalD const& other, EdgeHandlingEnum edgeHandling) : _min(), _size() {
88  if (other.isEmpty()) {
89  *this = IntervalI();
90  return;
91  }
92  if (!std::isfinite(other.getMin()) || !std::isfinite(other.getMax())) {
94  "Cannot convert non-finite IntervalD to IntervalI");
95  }
96  BigElement min, max;
97  switch (edgeHandling) {
99  min = static_cast<BigElement>(std::ceil(other.getMin() - 0.5));
100  max = static_cast<BigElement>(std::floor(other.getMax() + 0.5));
101  break;
103  min = static_cast<BigElement>(std::ceil(other.getMin() + 0.5));
104  max = static_cast<BigElement>(std::floor(other.getMax() - 0.5));
105  break;
106  default:
107  throw pex::exceptions::LogicError("Invalid enum value.");
108  }
109  *this = _fromMinMaxChecked(min, max);
110 }
111 
112 ndarray::View<boost::fusion::vector1<ndarray::index::Range>> IntervalI::getSlice() const {
113  return ndarray::view(getBegin(), getEnd());
114 }
115 
116 bool IntervalI::contains(Element point) const noexcept {
117  // The case where this->isEmpty() is handled implicitly by the invariant
118  // that empty intervals have max < min.
119  return point >= this->getMin() && point <= this->getMax();
120 }
121 
122 bool IntervalI::contains(IntervalI const& other) const noexcept {
123  // The case where this->isEmpty() is handled implicitly by the invariant
124  // that empty intervals have max < min.
125  return other.isEmpty() || (other.getMin() >= this->getMin() && other.getMax() <= this->getMax());
126 }
127 
128 bool IntervalI::overlaps(IntervalI const& other) const noexcept { return !isDisjointFrom(other); }
129 
130 bool IntervalI::isDisjointFrom(IntervalI const& other) const noexcept {
131  if (isEmpty() || other.isEmpty()) {
132  return true;
133  }
134  return getMin() > other.getMax() || getMax() < other.getMin();
135 }
136 
138  if (isEmpty()) {
139  return IntervalI();
140  }
141  BigElement min = static_cast<BigElement>(getMin()) - buffer;
142  BigElement max = static_cast<BigElement>(getMax()) + buffer;
143  return _fromMinMaxChecked(min, max);
144 }
145 
147  if (isEmpty()) {
148  return IntervalI();
149  }
150  BigElement min = static_cast<BigElement>(getMin()) + offset;
151  BigElement max = static_cast<BigElement>(getMax()) + offset;
152  checkForOverflow(min, "minimum");
153  checkForOverflow(max, "maximum");
154  return IntervalI(static_cast<Element>(min), getSize());
155 }
156 
158  if (isEmpty()) {
159  return IntervalI();
160  }
161  BigElement max = 2 * static_cast<BigElement>(point) - getMin();
162  BigElement min = 2 * static_cast<BigElement>(point) - getMax();
163  return _fromMinMaxChecked(min, max);
164 }
165 
167  if (isEmpty()) {
168  return IntervalI(point, 1);
169  }
170  return _fromMinMaxChecked(std::min(static_cast<BigElement>(point), static_cast<BigElement>(getMin())),
171  std::max(static_cast<BigElement>(point), static_cast<BigElement>(getMax())));
172 }
173 
175  if (other.isEmpty()) {
176  return *this;
177  }
178  if (this->isEmpty()) {
179  return other;
180  }
181  return _fromMinMaxChecked(
182  std::min(static_cast<BigElement>(other.getMin()), static_cast<BigElement>(getMin())),
183  std::max(static_cast<BigElement>(other.getMax()), static_cast<BigElement>(getMax())));
184 }
185 
187  if (isEmpty() || other.isEmpty()) {
188  return IntervalI();
189  }
190  return fromMinMax(std::max(getMin(), other.getMin()), std::min(getMax(), other.getMax()));
191 }
192 
193 bool IntervalI::operator==(IntervalI const& other) const noexcept {
194  return other._min == this->_min && other._size == this->_size;
195 }
196 
197 bool IntervalI::operator!=(IntervalI const& other) const noexcept { return !(other == *this); }
198 
200  // Completely arbitrary seed
201  return utils::hashCombine(17, _min, _size);
202 }
203 
205  return (boost::format("(min=%s, max=%s)") % getMin() % getMax()).str();
206 }
207 
208 template <typename T>
209 IntervalI IntervalI::_fromMinMaxChecked(T min, T max) {
210  if (max < min) {
211  return IntervalI();
212  }
213  checkForOverflow(min, "minimum");
214  checkForOverflow(max, "maximum");
215  T size = 1 + max - min;
216  checkForOverflow(size, "size");
217  return IntervalI(static_cast<Element>(min), static_cast<Element>(size));
218 }
219 
220 IntervalI::IntervalI(Element min, Element size) : _min(min), _size(size) {}
221 
223  : _min(std::numeric_limits<IntervalD::Element>::quiet_NaN()),
224  _max(std::numeric_limits<IntervalD::Element>::quiet_NaN()) {}
225 
227 
229  if (std::isinf(min) || (std::isinf(size) && size > 0)) {
231  "Ambiguously infinite interval parameters; use fromMinMax to "
232  "construct infinite intervals instead.");
233  }
234  return IntervalD(min, min + size);
235 }
236 
238  if (std::isinf(max) || (std::isinf(size) && size > 0)) {
240  "Ambiguously infinite interval parameters; use fromMinMax to "
241  "construct infinite intervals instead.");
242  }
243  return IntervalD(max - size, max);
244 }
245 
247  if (std::isinf(center) || std::isinf(size)) {
248  throw LSST_EXCEPT(pex::exceptions::InvalidParameterError, "Infinite values not supported.");
249  }
250  Element min = center - 0.5 * size;
252 }
253 
255  : _min(other.getMin() - 0.5), _max(other.getMax() + 0.5) {
256  if (other.isEmpty()) *this = IntervalD();
257 }
258 
259 IntervalD::Element IntervalD::getSize() const noexcept { return isEmpty() ? 0.0 : (_max - _min); }
260 
261 IntervalD::Element IntervalD::getCenter() const noexcept { return 0.5 * (_min + _max); }
262 
263 bool IntervalD::contains(Element point) const {
264  if (std::isnan(point)) {
266  "Cannot test whether an interval contains NaN.");
267  }
268  return point >= this->getMin() && point <= this->getMax();
269 }
270 
271 bool IntervalD::contains(IntervalD const& other) const noexcept {
272  return other.isEmpty() || (other.getMin() >= this->getMin() && other.getMax() <= this->getMax());
273 }
274 
275 bool IntervalD::overlaps(IntervalD const& other) const noexcept { return !isDisjointFrom(other); }
276 
277 bool IntervalD::isDisjointFrom(IntervalD const& other) const noexcept {
278  if (isEmpty() || other.isEmpty()) {
279  return true;
280  }
281  return getMin() > other.getMax() || getMax() < other.getMin();
282 }
283 
285  if (!std::isfinite(buffer)) {
287  "Cannot dilate or erode with a non-finite buffer.");
288  }
289  return fromMinMax(_min - buffer, _max + buffer);
290 }
291 
293  if (!std::isfinite(offset)) {
294  throw LSST_EXCEPT(pex::exceptions::InvalidParameterError, "Cannot shift with a non-finite offset.");
295  }
296  if (!isEmpty()) {
297  return fromMinMax(_min + offset, _max + offset);
298  } else {
299  return IntervalD();
300  }
301 }
302 
304  if (!std::isfinite(point)) {
305  throw LSST_EXCEPT(pex::exceptions::InvalidParameterError, "Cannot reflect about a non-finite point.");
306  }
307  if (!isEmpty()) {
308  return fromMinMax(2 * point - _max, 2 * point - _min);
309  } else {
310  return IntervalD();
311  }
312 }
313 
315  if (!std::isfinite(point)) {
316  throw LSST_EXCEPT(pex::exceptions::InvalidParameterError, "Cannot expand to a non-finite point.");
317  }
318  if (isEmpty()) {
319  return fromMinMax(point, point);
320  } else {
321  return fromMinMax(std::min(point, _min), std::max(point, _max));
322  }
323 }
324 
326  if (other.isEmpty()) {
327  return *this;
328  } else if (this->isEmpty()) {
329  return other;
330  } else {
331  return fromMinMax(std::min(this->getMin(), other.getMin()), std::max(this->getMax(), other.getMax()));
332  }
333 }
334 
336  if (this->isEmpty() || other.isEmpty()) {
337  return IntervalD();
338  } else {
339  return fromMinMax(std::max(this->getMin(), other.getMin()), std::min(this->getMax(), other.getMax()));
340  }
341 }
342 
343 bool IntervalD::operator==(IntervalD const& other) const noexcept {
344  return (other.isEmpty() && this->isEmpty()) || (other._min == this->_min && other._max == this->_max);
345 }
346 
347 bool IntervalD::operator!=(IntervalD const& other) const noexcept { return !(other == *this); }
348 
350  // Completely arbitrary seed
351  return utils::hashCombine(17, _min, _max);
352 }
353 
355  return (boost::format("(min=%s, max=%s)") % getMin() % getMax()).str();
356 }
357 
358 IntervalD::IntervalD(Element min, Element max) : _min(min), _max(max) {
359  if (max < min || std::isnan(min) || std::isnan(max)) {
360  *this = IntervalD();
361  } else if (std::isinf(min) && min > 0.0) {
363  "Cannot set interval minimum to +infinity.");
364  } else if (std::isinf(max) && max < 0.0) {
365  throw LSST_EXCEPT(pex::exceptions::InvalidParameterError,
366  "Cannot set interval maximum to -infinity.");
367  }
368 }
369 
371  if (interval.isEmpty()) return os << "IntervalI()";
372  return os << "IntervalI" << interval.toString();
373 }
374 
376  if (interval.isEmpty()) return os << "IntervalD()";
377  return os << "IntervalD" << interval.toString();
378 }
379 
380 } // namespace geom
381 } // namespace lsst
lsst::geom::IntervalD::getMin
Element getMin() const noexcept
Return the size of the interval.
Definition: Interval.h:559
lsst::geom::IntervalI
A 1-d integer coordinate range.
Definition: Interval.h:50
lsst::geom::IntervalI::isEmpty
bool isEmpty() const noexcept
Return true if the interval contains no points.
Definition: Interval.h:262
std::floor
T floor(T... args)
lsst::geom::IntervalD::isDisjointFrom
bool isDisjointFrom(IntervalD const &other) const noexcept
Return true if there are no points in both this and other.
Definition: Interval.cc:277
lsst::geom::IntervalD::toString
std::string toString() const
Return the size of the interval.
Definition: Interval.cc:354
lsst::geom::IntervalD::IntervalD
IntervalD() noexcept
Construct an empty interval.
Definition: Interval.cc:222
lsst::geom::IntervalI::Element
int Element
Definition: Interval.h:52
std::string
STL class.
lsst::geom::IntervalD::hash_value
std::size_t hash_value() const noexcept
Return a hash of this object.
Definition: Interval.cc:349
lsst::geom::IntervalI::fromMinMax
static IntervalI fromMinMax(Element min, Element max)
Construct an interval from its lower and upper bounds.
Definition: Interval.cc:53
lsst::geom::IntervalI::getBegin
Element getBegin() const noexcept
Definition: Interval.h:238
lsst::geom::IntervalD::getSize
Element getSize() const noexcept
Return the size of the interval.
Definition: Interval.cc:259
lsst::geom::IntervalI::getSlice
ndarray::View< boost::fusion::vector1< ndarray::index::Range > > getSlice() const
Return slice to extract the interval's region from an ndarray::Array.
Definition: Interval.cc:112
lsst::geom::IntervalD::getMax
Element getMax() const noexcept
Return the size of the interval.
Definition: Interval.h:560
lsst::geom::IntervalI::expandedTo
IntervalI expandedTo(Element other) const
Expand an interval to ensure that contains(other) is true (returning a new object).
Definition: Interval.cc:166
lsst::geom::IntervalD::operator==
bool operator==(IntervalD const &other) const noexcept
Compare two intervals for equality.
Definition: Interval.cc:343
lsst::geom::IntervalD::clippedTo
IntervalD clippedTo(IntervalD const &other) const noexcept
Shrink an interval to ensure that it is contained by other (returning a new object).
Definition: Interval.cc:335
lsst::geom::IntervalI::isDisjointFrom
bool isDisjointFrom(IntervalI const &other) const noexcept
Return true if there are no points in both this and other.
Definition: Interval.cc:130
lsst::geom::IntervalI::shiftedBy
IntervalI shiftedBy(Element offset) const
Shift the position of the interval by the given offset (returning a new object).)
Definition: Interval.cc:146
lsst.pex.config.history.format
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
Definition: history.py:174
lsst::geom::IntervalI::EdgeHandlingEnum
EdgeHandlingEnum
Enum used to indicate how to handle conversions from floating-point to integer intervals.
Definition: Interval.h:64
lsst::geom::IntervalI::fromCenterSize
static IntervalI fromCenterSize(double center, Element size)
Create an interval centered as closely as possible on a particular point.
Definition: Interval.cc:75
lsst::geom::IntervalI::hash_value
std::size_t hash_value() const noexcept
Return a hash of this object.
Definition: Interval.cc:199
_size
table::Key< table::Array< int > > _size
Definition: PsfexPsf.cc:364
std::isnan
T isnan(T... args)
hashCombine.h
std::isfinite
T isfinite(T... args)
lsst::geom::IntervalD::expandedTo
IntervalD expandedTo(Element other) const
Expand an interval to ensure that contains(other) is true.
Definition: Interval.cc:314
lsst::geom::IntervalD::isEmpty
bool isEmpty() const noexcept
Return true if the interval contains no points.
Definition: Interval.h:579
lsst::geom::IntervalI::IntervalI
IntervalI() noexcept
Construct an empty interval.
Definition: Interval.h:77
std::ostream
STL class.
std::isinf
T isinf(T... args)
lsst::geom::IntervalI::getMin
Element getMin() const noexcept
Definition: Interval.h:227
x
double x
Definition: ChebyshevBoundedField.cc:277
Interval.h
lsst::geom::IntervalI::EdgeHandlingEnum::EXPAND
@ EXPAND
Include all pixels that overlap the floating-point interval at all.
other
ItemVariant const * other
Definition: Schema.cc:56
lsst::geom::IntervalD::reflectedAbout
IntervalD reflectedAbout(Element point) const
Reflect an interval about a point (returning a new object).
Definition: Interval.cc:303
lsst.pex::exceptions::LogicError
Reports errors in the logical structure of the program.
Definition: Runtime.h:46
lsst::geom::IntervalD::dilatedBy
IntervalD dilatedBy(Element buffer) const
Increase the size of the interval by the given amount in both directions (returning a new object).
Definition: Interval.cc:284
lsst::geom::IntervalI::fromMinSize
static IntervalI fromMinSize(Element min, Element size)
Construct an interval from its lower bound and size.
Definition: Interval.cc:57
lsst::geom::IntervalI::clippedTo
IntervalI clippedTo(IntervalI const &other) const noexcept
Shrink an interval to ensure that it is contained by other (returning a new)
Definition: Interval.cc:186
max
int max
Definition: BoundedField.cc:104
lsst::geom::IntervalI::operator==
bool operator==(IntervalI const &other) const noexcept
Compare two intervals for equality.
Definition: Interval.cc:193
std::is_integral
std::ceil
T ceil(T... args)
lsst
A base class for image defects.
Definition: imageAlgorithm.dox:1
lsst::geom::operator<<
std::ostream & operator<<(std::ostream &os, lsst::geom::AffineTransform const &transform)
Definition: AffineTransform.cc:72
LSST_EXCEPT
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
lsst::geom::IntervalD::overlaps
bool overlaps(IntervalD const &other) const noexcept
Return true if any points in other are also in this.
Definition: Interval.cc:275
std::min
T min(T... args)
lsst::geom::IntervalD
A floating-point coordinate rectangle geometry.
Definition: Interval.h:413
lsst::geom::IntervalD::fromMinSize
static IntervalD fromMinSize(Element min, Element size)
Construct an interval from its lower bound and size.
Definition: Interval.cc:228
lsst::geom
Definition: AffineTransform.h:36
lsst::geom::IntervalD::getCenter
Element getCenter() const noexcept
Return the center coordinate of the interval.
Definition: Interval.cc:261
os
std::ostream * os
Definition: Schema.cc:746
lsst::geom::IntervalD::contains
bool contains(Element point) const
Return true if the interval contains the point.
Definition: Interval.cc:263
lsst::utils::hashCombine
std::size_t hashCombine(std::size_t seed) noexcept
Combine hashes.
Definition: hashCombine.h:35
lsst::geom::IntervalI::getSize
Element getSize() const noexcept
Return slice to extract the interval's region from an ndarray::Array.
Definition: Interval.h:247
lsst::geom::IntervalI::fromMaxSize
static IntervalI fromMaxSize(Element max, Element size)
Construct an interval from its upper bound and size.
Definition: Interval.cc:66
lsst.pex::exceptions::InvalidParameterError
Reports invalid arguments.
Definition: Runtime.h:66
min
int min
Definition: BoundedField.cc:103
std
STL namespace.
lsst::geom::IntervalI::EdgeHandlingEnum::SHRINK
@ SHRINK
Include only pixels that are wholly contained by the floating-point interval.
lsst::geom::IntervalD::operator!=
bool operator!=(IntervalD const &other) const noexcept
Compare two intervals for equality.
Definition: Interval.cc:347
lsst::geom::IntervalI::contains
bool contains(Element point) const noexcept
Return true if the interval contains the point.
Definition: Interval.cc:116
lsst::geom::IntervalI::operator!=
bool operator!=(IntervalI const &other) const noexcept
Compare two intervals for equality.
Definition: Interval.cc:197
lsst::geom::IntervalD::shiftedBy
IntervalD shiftedBy(Element offset) const
Shift the position of the interval by the given offset (returning a new object).
Definition: Interval.cc:292
std::size_t
lsst::geom::IntervalI::dilatedBy
IntervalI dilatedBy(Element buffer) const
Increase the size of the interval by the given amount in both directions (returning a new object).
Definition: Interval.cc:137
lsst::geom::IntervalI::reflectedAbout
IntervalI reflectedAbout(Element point) const
Reflect an interval about a point (returning a new object).
Definition: Interval.cc:157
lsst::geom::IntervalD::fromCenterSize
static IntervalD fromCenterSize(double center, Element size)
Construct an interval centered on a particular point.
Definition: Interval.cc:246
std::max
T max(T... args)
lsst::geom::IntervalI::getEnd
Element getEnd() const noexcept
Definition: Interval.h:239
lsst::geom::IntervalD::Element
double Element
Definition: Interval.h:415
std::numeric_limits
lsst::geom::IntervalI::overlaps
bool overlaps(IntervalI const &other) const noexcept
Return true if there are any points in both this and other.
Definition: Interval.cc:128
lsst::geom::IntervalD::fromMinMax
static IntervalD fromMinMax(Element min, Element max)
Construct an interval from its lower and upper bounds.
Definition: Interval.cc:226
lsst::geom::IntervalI::toString
std::string toString() const
Return slice to extract the interval's region from an ndarray::Array.
Definition: Interval.cc:204
lsst::geom::IntervalD::fromMaxSize
static IntervalD fromMaxSize(Element max, Element size)
Construct an interval from its upper bound and size.
Definition: Interval.cc:237
lsst::geom::IntervalI::getMax
Element getMax() const noexcept
Definition: Interval.h:228