LSSTApplications  21.0.0+1b62c9342b,21.0.0+45a059f35e,21.0.0-1-ga51b5d4+ceb9cf20a3,21.0.0-10-g68cce58c5+c7d3cce47e,21.0.0-2-g103fe59+c1ca725317,21.0.0-2-g1367e85+a1c2f7fe71,21.0.0-2-g2909d54+45a059f35e,21.0.0-2-g45278ab+1b62c9342b,21.0.0-2-g4bc9b9f+b2e40a4e47,21.0.0-2-g5242d73+a1c2f7fe71,21.0.0-2-g54e2caa+c00cf99ed0,21.0.0-2-g66bcc37+27b9d7859a,21.0.0-2-g7f82c8f+203cf74700,21.0.0-2-g8dde007+b0df52bfdd,21.0.0-2-g8f08a60+73884b2cf5,21.0.0-2-ga326454+203cf74700,21.0.0-2-ga63a54e+eec04437aa,21.0.0-2-gc738bc1+59028256f4,21.0.0-2-gde069b7+5a8f2956b8,21.0.0-2-ge17e5af+a1c2f7fe71,21.0.0-2-ge712728+9ad031c87e,21.0.0-2-gecfae73+d3766aec80,21.0.0-2-gfc62afb+a1c2f7fe71,21.0.0-20-g4449a12+38dfb87bce,21.0.0-22-gf0532904+afb8e7912b,21.0.0-3-g4c5b185+a403cb96fd,21.0.0-3-g6d51c4a+27b9d7859a,21.0.0-3-g8076721+e873df194c,21.0.0-3-gaa929c8+df5d87f43a,21.0.0-3-gd222c45+afc8332dbe,21.0.0-4-g1383c07+27b9d7859a,21.0.0-4-g3300ddd+1b62c9342b,21.0.0-4-g5873dc9+9a92674037,21.0.0-4-g8a80011+f67daf2f53,21.0.0-5-gcff38f6+bce43c5818,21.0.0-6-g463d161+44134145d4,21.0.0-6-gd3283ba+df5d87f43a,21.0.0-8-g19111d86+d6551531e4,w.2021.04
LSSTDataManagementBasePackage
Interval.h
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 #ifndef LSST_GEOM_INTERVAL_H
23 #define LSST_GEOM_INTERVAL_H
24 
25 #include <vector>
26 
27 #include "ndarray.h"
28 
29 #include "lsst/geom/Point.h"
30 #include "lsst/geom/Extent.h"
31 
32 namespace lsst {
33 namespace geom {
34 
35 class IntervalD;
36 
50 class IntervalI final {
51 public:
52  using Element = int;
53 
64  enum class EdgeHandlingEnum {
68  EXPAND,
73  SHRINK
74  };
75 
77  IntervalI() noexcept : _min(0), _size(0) {}
78 
91  template <typename Iter>
92  static IntervalI fromSpannedPoints(Iter first, Iter last) {
94  for (auto i = first; i != last; ++i) {
95  result = result.expandedTo(*i);
96  }
97  return result;
98  }
99 
101 
113  return fromSpannedPoints(elements.begin(), elements.end());
114  }
115  static IntervalI fromSpannedPoints(ndarray::Array<Element const, 1> const& elements) {
116  return fromSpannedPoints(elements.begin(), elements.end());
117  }
119 
132 
143  static IntervalI fromMinSize(Element min, Element size);
144 
155  static IntervalI fromMaxSize(Element max, Element size);
156 
175  static IntervalI fromCenterSize(double center, Element size);
176 
199  explicit IntervalI(IntervalD const& other, EdgeHandlingEnum edgeHandling = EdgeHandlingEnum::EXPAND);
200 
202  IntervalI(IntervalI const&) noexcept = default;
203 
205  IntervalI(IntervalI&&) noexcept = default;
206 
207  ~IntervalI() noexcept = default;
208 
209  void swap(IntervalI& other) noexcept {
210  using std::swap;
211  swap(_size, other._size);
212  swap(_min, other._min);
213  }
214 
216  IntervalI& operator=(IntervalI const&) noexcept = default;
217 
220 
227  Element getMin() const noexcept { return _min; }
228  Element getMax() const noexcept { return _min + _size - 1; }
230 
238  Element getBegin() const noexcept { return _min; }
239  Element getEnd() const noexcept { return _min + _size; }
241 
247  Element getSize() const noexcept { return _size; }
248 
259  ndarray::View<boost::fusion::vector1<ndarray::index::Range> > getSlice() const;
260 
262  bool isEmpty() const noexcept { return _size == 0; }
263 
265  bool contains(Element point) const noexcept;
266 
274  bool contains(IntervalI const& other) const noexcept;
275 
277 
282  bool overlaps(IntervalI const& other) const noexcept;
283  bool intersects(IntervalI const& other) const noexcept { return overlaps(other); }
285 
289  bool isDisjointFrom(IntervalI const& other) const noexcept;
290 
302  IntervalI dilatedBy(Element buffer) const;
303 
315  IntervalI erodedBy(Element buffer) const { return dilatedBy(-buffer); }
316 
323  IntervalI shiftedBy(Element offset) const;
324 
330  IntervalI reflectedAbout(Element point) const;
331 
333 
342  IntervalI expandedTo(IntervalI const& other) const;
344 
352  IntervalI clippedTo(IntervalI const& other) const noexcept;
353 
359  bool operator==(IntervalI const& other) const noexcept;
360 
366  bool operator!=(IntervalI const& other) const noexcept;
367 
369  std::size_t hash_value() const noexcept;
370 
371  std::string toString() const;
372 
373 private:
374  template <typename T>
375  static IntervalI _fromMinMaxChecked(T min, T max);
376 
378 
379  /*
380  * IntervalI internally stores its minimum point and size, because we
381  * expect these will be the most commonly accessed quantities.
382  *
383  * We set the minimum point to the origin for an empty interval, and
384  * use -1 for the maximum point in that case, but this is an internal
385  * detail - all the API guarantees is that for an empty interval,
386  * size == 0.
387  */
388 
389  Element _min;
390  Element _size;
391 };
392 
413 class IntervalD final {
414 public:
415  using Element = double;
416 
418  IntervalD() noexcept;
419 
433  template <typename Iter>
434  static IntervalD fromSpannedPoints(Iter first, Iter last) {
436  for (auto i = first; i != last; ++i) {
437  result = result.expandedTo(*i);
438  }
439  return result;
440  }
441 
443 
455  return fromSpannedPoints(elements.begin(), elements.end());
456  }
457  static IntervalD fromSpannedPoints(ndarray::Array<Element const, 1> const& elements) {
458  return fromSpannedPoints(elements.begin(), elements.end());
459  }
461 
479 
492  static IntervalD fromMinSize(Element min, Element size);
493 
506  static IntervalD fromMaxSize(Element max, Element size);
507 
520  static IntervalD fromCenterSize(double center, Element size);
521 
531  explicit IntervalD(IntervalI const& other) noexcept;
532 
534  IntervalD(IntervalD const&) noexcept = default;
535 
537  IntervalD(IntervalD&&) noexcept = default;
538 
539  ~IntervalD() noexcept = default;
540 
541  void swap(IntervalD& other) noexcept {
542  std::swap(_min, other._min);
543  std::swap(_max, other._max);
544  }
545 
547  IntervalD& operator=(IntervalD const&) noexcept = default;
548 
551 
559  Element getMin() const noexcept { return _min; }
560  Element getMax() const noexcept { return _max; }
562 
569  Element getSize() const noexcept;
570 
576  Element getCenter() const noexcept;
577 
579  bool isEmpty() const noexcept { return std::isnan(_min); }
580 
582  bool isFinite() const noexcept { return std::isfinite(getSize()); }
583 
590  bool contains(Element point) const;
591 
599  bool contains(IntervalD const& other) const noexcept;
600 
602 
607  bool overlaps(IntervalD const& other) const noexcept;
608  bool intersects(IntervalD const& other) const noexcept { return overlaps(other); }
610 
614  bool isDisjointFrom(IntervalD const& other) const noexcept;
615 
631  IntervalD dilatedBy(Element buffer) const;
632 
648  IntervalD erodedBy(Element buffer) const { return dilatedBy(-buffer); }
649 
660  IntervalD shiftedBy(Element offset) const;
661 
672  IntervalD reflectedAbout(Element point) const;
673 
684 
693  IntervalD expandedTo(IntervalD const& other) const noexcept;
694 
702  IntervalD clippedTo(IntervalD const& other) const noexcept;
703 
709  bool operator==(IntervalD const& other) const noexcept;
710 
716  bool operator!=(IntervalD const& other) const noexcept;
717 
719  std::size_t hash_value() const noexcept;
720 
721  std::string toString() const;
722 
723 private:
725 
726  Element _min;
727  Element _max;
728 };
729 
730 std::ostream& operator<<(std::ostream& os, IntervalI const& interval);
731 
732 std::ostream& operator<<(std::ostream& os, IntervalD const& interval);
733 
734 inline void swap(IntervalI& a, IntervalI& b) noexcept { a.swap(b); }
735 
736 inline void swap(IntervalD& a, IntervalD& b) noexcept { a.swap(b); }
737 
738 } // namespace geom
739 } // namespace lsst
740 
741 namespace std {
742 
743 template <>
744 inline void swap<lsst::geom::IntervalI>(lsst::geom::IntervalI& a, lsst::geom::IntervalI& b) noexcept {
745  a.swap(b);
746 }
747 
748 template <>
749 inline void swap<lsst::geom::IntervalD>(lsst::geom::IntervalD& a, lsst::geom::IntervalD& b) noexcept {
750  a.swap(b);
751 }
752 
753 template <>
757  size_t operator()(argument_type const& x) const noexcept { return x.hash_value(); }
758 };
759 
760 template <>
764  size_t operator()(argument_type const& x) const noexcept { return x.hash_value(); }
765 };
766 
767 } // namespace std
768 
769 #endif
lsst.pipe.tasks.cli.cmd.commands.default
default
Definition: commands.py:50
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
lsst::geom::IntervalI::Element
int Element
Definition: Interval.h:52
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::isFinite
bool isFinite() const noexcept
Return true if the interval's size is finite.
Definition: Interval.h:582
std::vector< Element >
lsst::geom::IntervalD::operator=
IntervalD & operator=(IntervalD &&) noexcept=default
Standard move assignment operator.
lsst::geom::IntervalD::intersects
bool intersects(IntervalD const &other) const noexcept
Return the size of the interval.
Definition: Interval.h:608
lsst::afw::table._match.first
first
Definition: _match.py:74
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::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::fromSpannedPoints
static IntervalI fromSpannedPoints(Iter first, Iter last)
Construct an interval that contains all of the given points.
Definition: Interval.h:92
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::geom::IntervalD::erodedBy
IntervalD erodedBy(Element buffer) const
Decrease the size of the interval by the given amount in both directions (returning a new object).
Definition: Interval.h:648
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::IntervalD::IntervalD
IntervalD(IntervalD &&) noexcept=default
Standard move constructor.
lsst::geom::IntervalI::hash_value
std::size_t hash_value() const noexcept
Return a hash of this object.
Definition: Interval.cc:199
std::isnan
T isnan(T... args)
Extent.h
lsst::geom::IntervalI::operator=
IntervalI & operator=(IntervalI &&) noexcept=default
Standard move assignment operatior.
std::isfinite
T isfinite(T... args)
lsst::geom::IntervalI::operator=
IntervalI & operator=(IntervalI const &) noexcept=default
Standard copy assignment operator.
lsst::geom::IntervalI::IntervalI
IntervalI() noexcept
Construct an empty interval.
Definition: Interval.h:77
std::hash< lsst::geom::IntervalD >::operator()
size_t operator()(argument_type const &x) const noexcept
Definition: Interval.h:764
lsst::geom::IntervalI::getMin
Element getMin() const noexcept
Definition: Interval.h:227
x
double x
Definition: ChebyshevBoundedField.cc:277
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::IntervalI::erodedBy
IntervalI erodedBy(Element buffer) const
Decrease the size of the interval by the given amount in both directions (returning a new object).
Definition: Interval.h:315
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
result
py::object result
Definition: _schema.cc:429
b
table::Key< int > b
Definition: TransmissionCurve.cc:467
lsst
A base class for image defects.
Definition: imageAlgorithm.dox:1
std::swap
T swap(T... args)
lsst::geom::IntervalD::fromSpannedPoints
static IntervalD fromSpannedPoints(std::vector< Element > const &elements)
Construct an interval that contains all of the given points.
Definition: Interval.h:454
lsst::geom::IntervalD
A floating-point coordinate rectangle geometry.
Definition: Interval.h:413
lsst::geom::IntervalI::IntervalI
IntervalI(IntervalI &&) noexcept=default
Standard move constructor.
lsst::geom
Definition: AffineTransform.h:36
os
std::ostream * os
Definition: Schema.cc:746
lsst::geom::IntervalI::intersects
bool intersects(IntervalI const &other) const noexcept
Return slice to extract the interval's region from an ndarray::Array.
Definition: Interval.h:283
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::geom::IntervalD::fromSpannedPoints
static IntervalD fromSpannedPoints(ndarray::Array< Element const, 1 > const &elements)
Definition: Interval.h:457
lsst::geom::IntervalI::fromSpannedPoints
static IntervalI fromSpannedPoints(ndarray::Array< Element const, 1 > const &elements)
Definition: Interval.h:115
a
table::Key< int > a
Definition: TransmissionCurve.cc:466
std::vector::begin
T begin(T... args)
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::IntervalI::swap
void swap(IntervalI &other) noexcept
Definition: Interval.h:209
lsst::geom::IntervalI::IntervalI
IntervalI(IntervalI const &) noexcept=default
Standard copy constructor.
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
Point.h
std::size_t
lsst::geom::IntervalD::IntervalD
IntervalD(IntervalD const &) noexcept=default
Standard copy constructor.
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
std::vector::end
T end(T... args)
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::IntervalI::getEnd
Element getEnd() const noexcept
Definition: Interval.h:239
lsst::geom::IntervalD::Element
double Element
Definition: Interval.h:415
lsst::geom::IntervalD::operator=
IntervalD & operator=(IntervalD const &) noexcept=default
Standard copy assignment operator.
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::IntervalI::toString
std::string toString() const
Return slice to extract the interval's region from an ndarray::Array.
Definition: Interval.cc:204
std::hash
std::hash< lsst::geom::IntervalI >::operator()
size_t operator()(argument_type const &x) const noexcept
Definition: Interval.h:757
lsst::geom::IntervalI::getMax
Element getMax() const noexcept
Definition: Interval.h:228
lsst::geom::IntervalI::fromSpannedPoints
static IntervalI fromSpannedPoints(std::vector< Element > const &elements)
Construct an interval that contains all of the given points.
Definition: Interval.h:112