LSSTApplications  18.0.0+106,18.0.0+50,19.0.0,19.0.0+1,19.0.0+10,19.0.0+11,19.0.0+13,19.0.0+17,19.0.0+2,19.0.0-1-g20d9b18+6,19.0.0-1-g425ff20,19.0.0-1-g5549ca4,19.0.0-1-g580fafe+6,19.0.0-1-g6fe20d0+1,19.0.0-1-g7011481+9,19.0.0-1-g8c57eb9+6,19.0.0-1-gb5175dc+11,19.0.0-1-gdc0e4a7+9,19.0.0-1-ge272bc4+6,19.0.0-1-ge3aa853,19.0.0-10-g448f008b,19.0.0-12-g6990b2c,19.0.0-2-g0d9f9cd+11,19.0.0-2-g3d9e4fb2+11,19.0.0-2-g5037de4,19.0.0-2-gb96a1c4+3,19.0.0-2-gd955cfd+15,19.0.0-3-g2d13df8,19.0.0-3-g6f3c7dc,19.0.0-4-g725f80e+11,19.0.0-4-ga671dab3b+1,19.0.0-4-gad373c5+3,19.0.0-5-ga2acb9c+2,19.0.0-5-gfe96e6c+2,w.2020.01
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 
226 IntervalD IntervalD::fromMinMax(Element min, Element max) { return IntervalD(min, max); }
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;
251  return lsst::geom::IntervalD::fromMinSize(min, 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) {
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
static IntervalI fromMaxSize(Element max, Element size)
Construct an interval from its upper bound and size.
Definition: Interval.cc:66
Element getBegin() const noexcept
Definition: Interval.h:238
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
Definition: history.py:174
EdgeHandlingEnum
Enum used to indicate how to handle conversions from floating-point to integer intervals.
Definition: Interval.h:64
Element getMax() const noexcept
Definition: Interval.h:228
T ceil(T... args)
bool isEmpty() const noexcept
Return true if the interval contains no points.
Definition: Interval.h:579
static IntervalD fromMinSize(Element min, Element size)
Construct an interval from its lower bound and size.
Definition: Interval.cc:228
IntervalD reflectedAbout(Element point) const
Reflect an interval about a point (returning a new object).
Definition: Interval.cc:303
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
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
STL namespace.
bool overlaps(IntervalI const &other) const noexcept
Return true if there are any points in both this and other.
Definition: Interval.cc:128
static IntervalD fromMaxSize(Element max, Element size)
Construct an interval from its upper bound and size.
Definition: Interval.cc:237
bool contains(Element point) const noexcept
Return true if the interval contains the point.
Definition: Interval.cc:116
T floor(T... args)
IntervalI expandedTo(Element other) const
Expand an interval to ensure that contains(other) is true (returning a new object).
Definition: Interval.cc:166
ItemVariant const * other
Definition: Schema.cc:56
Element getMin() const noexcept
Definition: Interval.h:559
IntervalD shiftedBy(Element offset) const
Shift the position of the interval by the given offset (returning a new object).
Definition: Interval.cc:292
Element getSize() const noexcept
Return the size of the interval.
Definition: Interval.cc:259
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
Element getMax() const noexcept
Definition: Interval.h:560
bool isDisjointFrom(IntervalI const &other) const noexcept
Return true if there are no points in both this and other.
Definition: Interval.cc:130
IntervalI reflectedAbout(Element point) const
Reflect an interval about a point (returning a new object).
Definition: Interval.cc:157
int min
Include all pixels that overlap the floating-point interval at all.
Element getEnd() const noexcept
Definition: Interval.h:239
A floating-point coordinate rectangle geometry.
Definition: Interval.h:413
STL class.
T min(T... args)
IntervalI shiftedBy(Element offset) const
Shift the position of the interval by the given offset (returning a new object).) ...
Definition: Interval.cc:146
static IntervalI fromMinMax(Element min, Element max)
Construct an interval from its lower and upper bounds.
Definition: Interval.cc:53
static IntervalI fromCenterSize(double center, Element size)
Create an interval centered as closely as possible on a particular point.
Definition: Interval.cc:75
static IntervalD fromMinMax(Element min, Element max)
Construct an interval from its lower and upper bounds.
Definition: Interval.cc:226
A base class for image defects.
bool operator==(IntervalI const &other) const noexcept
Compare two intervals for equality.
Definition: Interval.cc:193
bool isEmpty() const noexcept
Return true if the interval contains no points.
Definition: Interval.h:262
bool overlaps(IntervalD const &other) const noexcept
Return true if any points in other are also in this.
Definition: Interval.cc:275
bool isDisjointFrom(IntervalD const &other) const noexcept
Return true if there are no points in both this and other.
Definition: Interval.cc:277
IntervalD expandedTo(Element other) const
Expand an interval to ensure that contains(other) is true.
Definition: Interval.cc:314
int max
T isfinite(T... args)
Element getSize() const noexcept
Return slice to extract the interval&#39;s region from an ndarray::Array.
Definition: Interval.h:247
std::ostream & operator<<(std::ostream &os, lsst::geom::AffineTransform const &transform)
Element getMin() const noexcept
Definition: Interval.h:227
Reports errors in the logical structure of the program.
Definition: Runtime.h:46
T max(T... args)
static IntervalI fromMinSize(Element min, Element size)
Construct an interval from its lower bound and size.
Definition: Interval.cc:57
Include only pixels that are wholly contained by the floating-point interval.
double x
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
bool operator!=(IntervalI const &other) const noexcept
Compare two intervals for equality.
Definition: Interval.cc:197
ndarray::View< boost::fusion::vector1< ndarray::index::Range > > getSlice() const
Return slice to extract the interval&#39;s region from an ndarray::Array.
Definition: Interval.cc:112
A 1-d integer coordinate range.
Definition: Interval.h:50
bool operator==(IntervalD const &other) const noexcept
Compare two intervals for equality.
Definition: Interval.cc:343
std::string toString() const
Definition: Interval.cc:354
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
static IntervalD fromCenterSize(double center, Element size)
Construct an interval centered on a particular point.
Definition: Interval.cc:246
Element getCenter() const noexcept
Return the center coordinate of the interval.
Definition: Interval.cc:261
std::size_t hash_value() const noexcept
Return a hash of this object.
Definition: Interval.cc:349
Reports invalid arguments.
Definition: Runtime.h:66
T isnan(T... args)
bool contains(Element point) const
Return true if the interval contains the point.
Definition: Interval.cc:263
std::string toString() const
Definition: Interval.cc:204
std::size_t hashCombine(std::size_t seed) noexcept
Combine hashes.
Definition: hashCombine.h:35
std::size_t hash_value() const noexcept
Return a hash of this object.
Definition: Interval.cc:199
T isinf(T... args)
bool operator!=(IntervalD const &other) const noexcept
Compare two intervals for equality.
Definition: Interval.cc:347
std::ostream * os
Definition: Schema.cc:746
STL class.
IntervalI() noexcept
Construct an empty interval.
Definition: Interval.h:77
IntervalD() noexcept
Construct an empty interval.
Definition: Interval.cc:222