26#include "boost/format.hpp"
36using BigElement =
long long;
42void checkForOverflow(T
x,
char const* where) {
47 (boost::format(
"Integer overflow (%d) in interval %s.") %
x % where).str());
54 return _fromMinMaxChecked(
static_cast<BigElement
>(
min),
static_cast<BigElement
>(
max));
61 BigElement
max =
static_cast<BigElement
>(
min) +
static_cast<BigElement
>(size) - 1;
62 checkForOverflow(
max,
"maximum");
70 BigElement
min =
static_cast<BigElement
>(
max) -
static_cast<BigElement
>(size) + 1;
71 checkForOverflow(
min,
"minimum");
79 double min = center - 0.5 * size;
82 checkForOverflow(
min,
"minimum");
83 checkForOverflow(
min + size - 1,
"maximum");
94 "Cannot convert non-finite IntervalD to IntervalI");
97 switch (edgeHandling) {
109 *
this = _fromMinMaxChecked(
min,
max);
119 return point >= this->getMin() && point <= this->getMax();
125 return other.isEmpty() || (other.getMin() >= this->getMin() && other.getMax() <= this->getMax());
131 if (isEmpty() || other.isEmpty()) {
134 return getMin() > other.getMax() || getMax() < other.getMin();
141 BigElement
min =
static_cast<BigElement
>(
getMin()) - buffer;
142 BigElement
max =
static_cast<BigElement
>(
getMax()) + buffer;
143 return _fromMinMaxChecked(
min,
max);
150 BigElement
min =
static_cast<BigElement
>(
getMin()) + offset;
151 BigElement
max =
static_cast<BigElement
>(
getMax()) + offset;
152 checkForOverflow(
min,
"minimum");
153 checkForOverflow(
max,
"maximum");
161 BigElement
max = 2 *
static_cast<BigElement
>(point) -
getMin();
162 BigElement
min = 2 *
static_cast<BigElement
>(point) -
getMax();
163 return _fromMinMaxChecked(
min,
max);
170 return _fromMinMaxChecked(
std::min(
static_cast<BigElement
>(point),
static_cast<BigElement
>(
getMin())),
171 std::max(
static_cast<BigElement
>(point),
static_cast<BigElement
>(
getMax())));
181 return _fromMinMaxChecked(
187 if (isEmpty() || other.isEmpty()) {
190 return fromMinMax(
std::max(getMin(), other.getMin()),
std::min(getMax(), other.getMax()));
194 return other._min == this->_min && other._size == this->_size;
205 return (boost::format(
"(min=%s, max=%s)") %
getMin() %
getMax()).str();
213 checkForOverflow(
min,
"minimum");
214 checkForOverflow(
max,
"maximum");
216 checkForOverflow(size,
"size");
231 "Ambiguously infinite interval parameters; use fromMinMax to "
232 "construct infinite intervals instead.");
240 "Ambiguously infinite interval parameters; use fromMinMax to "
241 "construct infinite intervals instead.");
255 : _min(other.getMin() - 0.5), _max(other.getMax() + 0.5) {
256 if (other.isEmpty()) *
this =
IntervalD();
266 "Cannot test whether an interval contains NaN.");
268 return point >= this->
getMin() && point <= this->
getMax();
272 return other.isEmpty() || (other.getMin() >= this->getMin() && other.getMax() <= this->getMax());
278 if (isEmpty() || other.isEmpty()) {
281 return getMin() > other.getMax() || getMax() < other.getMin();
287 "Cannot dilate or erode with a non-finite buffer.");
289 return fromMinMax(_min - buffer, _max + buffer);
297 return fromMinMax(_min + offset, _max + offset);
308 return fromMinMax(2 * point - _max, 2 * point - _min);
326 if (other.isEmpty()) {
328 }
else if (this->isEmpty()) {
331 return fromMinMax(
std::min(this->getMin(), other.getMin()),
std::max(this->getMax(), other.getMax()));
336 if (this->isEmpty() || other.isEmpty()) {
339 return fromMinMax(
std::max(this->getMin(), other.getMin()),
std::min(this->getMax(), other.getMax()));
344 return (other.isEmpty() && this->isEmpty()) || (other._min == this->_min && other._max == this->_max);
355 return (boost::format(
"(min=%s, max=%s)") %
getMin() %
getMax()).str();
363 "Cannot set interval minimum to +infinity.");
365 throw LSST_EXCEPT(pex::exceptions::InvalidParameterError,
366 "Cannot set interval maximum to -infinity.");
371 if (interval.
isEmpty())
return os <<
"IntervalI()";
372 return os <<
"IntervalI" << interval.
toString();
376 if (interval.
isEmpty())
return os <<
"IntervalD()";
377 return os <<
"IntervalD" << interval.
toString();
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
A floating-point coordinate rectangle geometry.
Element getMin() const noexcept
Return the size of the interval.
IntervalD reflectedAbout(Element point) const
Reflect an interval about a point (returning a new object).
bool operator==(IntervalD const &other) const noexcept
Compare two intervals for equality.
std::size_t hash_value() const noexcept
Return a hash of this object.
static IntervalD fromMaxSize(Element max, Element size)
Construct an interval from its upper bound and size.
bool isDisjointFrom(IntervalD const &other) const noexcept
Return true if there are no points in both this and other.
static IntervalD fromMinMax(Element min, Element max)
Construct an interval from its lower and upper bounds.
bool operator!=(IntervalD const &other) const noexcept
Compare two intervals for equality.
bool overlaps(IntervalD const &other) const noexcept
Return true if any points in other are also in this.
Element getMax() const noexcept
Return the size of the interval.
Element getSize() const noexcept
Return the size of the interval.
IntervalD() noexcept
Construct an empty interval.
bool contains(Element point) const
Return true if the interval contains the point.
std::string toString() const
Return the size of the interval.
static IntervalD fromCenterSize(double center, Element size)
Construct an interval centered on a particular point.
IntervalD dilatedBy(Element buffer) const
Increase the size of the interval by the given amount in both directions (returning a new object).
IntervalD clippedTo(IntervalD const &other) const noexcept
Shrink an interval to ensure that it is contained by other (returning a new object).
IntervalD expandedTo(Element other) const
Expand an interval to ensure that contains(other) is true.
bool isEmpty() const noexcept
Return true if the interval contains no points.
IntervalD shiftedBy(Element offset) const
Shift the position of the interval by the given offset (returning a new object).
Element getCenter() const noexcept
Return the center coordinate of the interval.
static IntervalD fromMinSize(Element min, Element size)
Construct an interval from its lower bound and size.
A 1-d integer coordinate range.
bool contains(Element point) const noexcept
Return true if the interval contains the point.
static IntervalI fromMinSize(Element min, Element size)
Construct an interval from its lower bound and size.
bool overlaps(IntervalI const &other) const noexcept
Return true if there are any points in both this and other.
ndarray::View< boost::fusion::vector1< ndarray::index::Range > > getSlice() const
Return slice to extract the interval's region from an ndarray::Array.
IntervalI() noexcept
Construct an empty interval.
IntervalI clippedTo(IntervalI const &other) const noexcept
Shrink an interval to ensure that it is contained by other (returning a new)
IntervalI dilatedBy(Element buffer) const
Increase the size of the interval by the given amount in both directions (returning a new object).
bool isEmpty() const noexcept
Return true if the interval contains no points.
bool isDisjointFrom(IntervalI const &other) const noexcept
Return true if there are no points in both this and other.
Element getMax() const noexcept
bool operator!=(IntervalI const &other) const noexcept
Compare two intervals for equality.
EdgeHandlingEnum
Enum used to indicate how to handle conversions from floating-point to integer intervals.
@ SHRINK
Include only pixels that are wholly contained by the floating-point interval.
@ EXPAND
Include all pixels that overlap the floating-point interval at all.
bool operator==(IntervalI const &other) const noexcept
Compare two intervals for equality.
Element getSize() const noexcept
Return slice to extract the interval's region from an ndarray::Array.
std::size_t hash_value() const noexcept
Return a hash of this object.
static IntervalI fromMaxSize(Element max, Element size)
Construct an interval from its upper bound and size.
Element getEnd() const noexcept
std::string toString() const
Return slice to extract the interval's region from an ndarray::Array.
static IntervalI fromCenterSize(double center, Element size)
Create an interval centered as closely as possible on a particular point.
static IntervalI fromMinMax(Element min, Element max)
Construct an interval from its lower and upper bounds.
IntervalI shiftedBy(Element offset) const
Shift the position of the interval by the given offset (returning a new object).)
Element getBegin() const noexcept
IntervalI expandedTo(Element other) const
Expand an interval to ensure that contains(other) is true (returning a new object).
Element getMin() const noexcept
IntervalI reflectedAbout(Element point) const
Reflect an interval about a point (returning a new object).
Reports invalid arguments.
Reports errors in the logical structure of the program.
std::size_t hashCombine(std::size_t seed) noexcept
Combine hashes.
std::ostream & operator<<(std::ostream &os, lsst::geom::AffineTransform const &transform)