LSSTApplications  16.0-10-g9d3e444,16.0-10-gb7df26b+3,16.0-10-gbd9fc95,16.0-11-g09ed895+3,16.0-11-g12e47bd+11,16.0-12-g5c924a4+21,16.0-12-g71e5ef5+10,16.0-15-g7af1f30+2,16.0-15-gdd5ca33+2,16.0-16-gf0259e2+1,16.0-18-gcf94535+13,16.0-19-g283fd30+1,16.0-19-g9d290d5+13,16.0-2-g0febb12+22,16.0-2-g9d5294e+76,16.0-20-g42ca1a7,16.0-20-g8d11721+2,16.0-23-g28ad22d+2,16.0-25-g723f23e8+2,16.0-29-g44d5b4c,16.0-32-ga392cb8+2,16.0-32-gc794025+1,16.0-4-g18f3627+20,16.0-4-g5f3a788+21,16.0-4-g6d309fa,16.0-4-ga3eb747+11,16.0-4-gabf74b7+42,16.0-4-gb13d127+7,16.0-5-g27fb78a+20,16.0-5-g6a53317+47,16.0-5-gb3f8a4b+101,16.0-6-g9321be7+5,16.0-6-gcbc7b31+58,16.0-6-gf49912c+44,16.0-65-g12857137,16.0-76-g802307b6d+2,16.0-8-g21fd5fe+45,16.0-8-g3a9f023+27,16.0-8-gc11f1cf+11,master-g5605b1aa4d+2,w.2019.05
LSSTDataManagementBasePackage
Box.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_BOX_H
23 #define LSST_GEOM_BOX_H
24 
25 #include <vector>
26 
27 #include "boost/format.hpp"
28 #include "ndarray.h"
29 
30 #include "lsst/geom/Point.h"
31 #include "lsst/geom/Extent.h"
32 
33 namespace lsst {
34 namespace geom {
35 
36 class Box2D;
37 
54 class Box2I final {
55 public:
56  typedef Point2I Point;
57  typedef Extent2I Extent;
58  typedef int Element;
59 
61 
63  Box2I() noexcept : _minimum(0), _dimensions(0) {}
64 
73  Box2I(Point2I const& minimum, Point2I const& maximum, bool invert = true);
74 
87  Box2I(Point2I const& corner, Extent2I const& dimensions, bool invert = true);
88 
106  explicit Box2I(Box2D const& other, EdgeHandlingEnum edgeHandling = EXPAND);
107 
109  Box2I(Box2I const&) noexcept = default;
110  Box2I(Box2I&&) noexcept = default;
111  ~Box2I() noexcept = default;
112 
126  static Box2I makeCenteredBox(Point2D const& center, Extent const& size);
127 
128  void swap(Box2I& other) noexcept {
129  _minimum.swap(other._minimum);
130  _dimensions.swap(other._dimensions);
131  }
132 
134  Box2I& operator=(Box2I const&) noexcept = default;
135  Box2I& operator=(Box2I&&) noexcept = default;
136 
143  Point2I const getMin() const noexcept { return _minimum; }
144  int getMinX() const noexcept { return _minimum.getX(); }
145  int getMinY() const noexcept { return _minimum.getY(); }
146 
147  Point2I const getMax() const noexcept { return _minimum + _dimensions - Extent2I(1); }
148  int getMaxX() const noexcept { return _minimum.getX() + _dimensions.getX() - 1; }
149  int getMaxY() const noexcept { return _minimum.getY() + _dimensions.getY() - 1; }
151 
158  Point2I const getBegin() const noexcept { return _minimum; }
159  int getBeginX() const noexcept { return _minimum.getX(); }
160  int getBeginY() const noexcept { return _minimum.getY(); }
161 
162  Point2I const getEnd() const noexcept { return _minimum + _dimensions; }
163  int getEndX() const noexcept { return _minimum.getX() + _dimensions.getX(); }
164  int getEndY() const noexcept { return _minimum.getY() + _dimensions.getY(); }
166 
173  Extent2I const getDimensions() const noexcept { return _dimensions; }
174  int getWidth() const noexcept { return _dimensions.getX(); }
175  int getHeight() const noexcept { return _dimensions.getY(); }
176  int getArea() const { return getWidth() * getHeight(); }
178 
180  ndarray::View<boost::fusion::vector2<ndarray::index::Range, ndarray::index::Range> > getSlices() const;
181 
183  bool isEmpty() const noexcept { return _dimensions.getX() == 0 && _dimensions.getY() == 0; }
184 
186  bool contains(Point2I const& point) const noexcept;
187 
193  bool contains(Box2I const& other) const noexcept;
194 
200  bool overlaps(Box2I const& other) const noexcept;
201 
208  void grow(int buffer) { grow(Extent2I(buffer)); }
209 
216  void grow(Extent2I const& buffer);
217 
219  void shift(Extent2I const& offset);
220 
222  void flipLR(int xExtent);
223 
225  void flipTB(int yExtent);
226 
228  void include(Point2I const& point);
229 
231  void include(Box2I const& other);
232 
239  void clip(Box2I const& other) noexcept;
240 
246  bool operator==(Box2I const& other) const noexcept;
247 
253  bool operator!=(Box2I const& other) const noexcept;
254 
256  std::size_t hash_value() const noexcept;
257 
265 
267  return (boost::format("Box2I(%s,%s)") % _minimum.toString() % _dimensions.toString()).str();
268  }
269 
270 private:
271  Point2I _minimum;
272  Extent2I _dimensions;
273 };
274 
294 class Box2D final {
295 public:
296  typedef Point2D Point;
297  typedef Extent2D Extent;
298  typedef double Element;
299 
304  static double const EPSILON;
305 
307  static double const INVALID;
308 
310  Box2D() noexcept;
311 
322  Box2D(Point2D const& minimum, Point2D const& maximum, bool invert = true) noexcept;
323 
334  Box2D(Point2D const& corner, Extent2D const& dimensions, bool invert = true) noexcept;
335 
345  explicit Box2D(Box2I const& other) noexcept;
346 
348  Box2D(Box2D const&) noexcept = default;
349  Box2D(Box2D&&) noexcept = default;
350 
351  ~Box2D() noexcept = default;
352 
364  // It's hard to guarantee postconditions (especially size) with non-finite inputs
365  static Box2D makeCenteredBox(Point2D const& center, Extent const& size) noexcept;
366 
367  void swap(Box2D& other) noexcept {
368  _minimum.swap(other._minimum);
369  _maximum.swap(other._maximum);
370  }
371 
373  Box2D& operator=(Box2D const&) noexcept = default;
374  Box2D& operator=(Box2D&&) noexcept = default;
375 
382  Point2D const getMin() const noexcept { return _minimum; }
383  double getMinX() const noexcept { return _minimum.getX(); }
384  double getMinY() const noexcept { return _minimum.getY(); }
385 
386  Point2D const getMax() const noexcept { return _maximum; }
387  double getMaxX() const noexcept { return _maximum.getX(); }
388  double getMaxY() const noexcept { return _maximum.getY(); }
390 
397  Extent2D const getDimensions() const noexcept { return isEmpty() ? Extent2D(0.0) : _maximum - _minimum; }
398  double getWidth() const noexcept { return isEmpty() ? 0 : _maximum.getX() - _minimum.getX(); }
399  double getHeight() const noexcept { return isEmpty() ? 0 : _maximum.getY() - _minimum.getY(); }
400  double getArea() const noexcept {
401  Extent2D dim(getDimensions());
402  return dim.getX() * dim.getY();
403  }
405 
412  Point2D const getCenter() const noexcept {
413  return Point2D((_minimum.asEigen() + _maximum.asEigen()) * 0.5);
414  }
415  double getCenterX() const noexcept { return (_minimum.getX() + _maximum.getX()) * 0.5; }
416  double getCenterY() const noexcept { return (_minimum.getY() + _maximum.getY()) * 0.5; }
418 
420  bool isEmpty() const noexcept { return _minimum.getX() != _minimum.getX(); }
421 
423  bool contains(Point2D const& point) const noexcept;
424 
430  bool contains(Box2D const& other) const noexcept;
431 
437  bool overlaps(Box2D const& other) const noexcept;
438 
445  void grow(double buffer) { grow(Extent2D(buffer)); }
446 
453  void grow(Extent2D const& buffer);
454 
456  void shift(Extent2D const& offset);
457 
459  void flipLR(float xExtent);
460 
462  void flipTB(float yExtent);
463 
471  void include(Point2D const& point) noexcept;
472 
474  void include(Box2D const& other) noexcept;
475 
482  void clip(Box2D const& other) noexcept;
483 
489  bool operator==(Box2D const& other) const noexcept;
490 
496  bool operator!=(Box2D const& other) const noexcept;
497 
499  std::size_t hash_value() const noexcept;
500 
508 
510  return (boost::format("Box2D(%s,%s)") % _minimum.toString() % _maximum.toString()).str();
511  }
512 
513 private:
514  void _tweakMax(int n) noexcept {
515  if (_maximum[n] < 0.0) {
516  _maximum[n] *= (1.0 - EPSILON);
517  } else if (_maximum[n] > 0.0) {
518  _maximum[n] *= (1.0 + EPSILON);
519  } else {
520  _maximum[n] = EPSILON;
521  }
522  }
523  Point2D _minimum;
524  Point2D _maximum;
525 };
526 
527 typedef Box2D BoxD;
528 typedef Box2I BoxI;
529 
531 
533 
534 } // namespace geom
535 } // namespace lsst
536 
537 namespace std {
538 template <>
539 struct hash<lsst::geom::Box2I> {
542  size_t operator()(argument_type const& x) const noexcept { return x.hash_value(); }
543 };
544 
545 template <>
546 struct hash<lsst::geom::Box2D> {
549  size_t operator()(argument_type const& x) const noexcept { return x.hash_value(); }
550 };
551 } // namespace std
552 
553 #endif
bool operator==(Box2I const &other) const noexcept
Compare two boxes for equality.
Definition: Box.cc:214
double getMinY() const noexcept
Definition: Box.h:384
Extent2I const getDimensions() const noexcept
Definition: Box.h:173
Point2D Point
Definition: Box.h:296
double getHeight() const noexcept
Definition: Box.h:399
afw::table::PointKey< int > dimensions
Definition: GaussianPsf.cc:49
Point2I const getMax() const noexcept
Definition: Box.h:147
constexpr double EPSILON
Definition: constants.h:54
std::string toString() const
Definition: Box.h:509
void flipLR(int xExtent)
Flip a bounding box about the y-axis given a parent box of extent (xExtent).
Definition: Box.cc:139
double getWidth() const noexcept
Definition: Box.h:398
int getHeight() const noexcept
Definition: Box.h:175
A floating-point coordinate rectangle geometry.
Definition: Box.h:294
Point2D const getCenter() const noexcept
Definition: Box.h:412
double getMinX() const noexcept
Definition: Box.h:383
A coordinate class intended to represent absolute positions.
bool isEmpty() const noexcept
Return true if the box contains no points.
Definition: Box.h:183
double getMaxX() const noexcept
Definition: Box.h:387
STL namespace.
Box2D BoxD
Definition: Box.h:527
Extent2D const getDimensions() const noexcept
Definition: Box.h:397
std::string toString() const
Definition: Point.h:143
Relationship invert(Relationship r)
Given the relationship between two sets A and B (i.e.
Definition: Relationship.h:55
Point2D const getMin() const noexcept
Definition: Box.h:382
std::vector< Point2I > getCorners() const
Get the corner points.
Definition: Box.cc:227
int getEndY() const noexcept
Definition: Box.h:164
std::string toString() const
Definition: Extent.h:179
Point< double, 2 > Point2D
Definition: Point.h:324
static double const EPSILON
Value the maximum coordinate is multiplied by to increase it by the smallest possible amount...
Definition: Box.h:304
int getBeginY() const noexcept
Definition: Box.h:160
STL class.
Point2I const getMin() const noexcept
Definition: Box.h:143
bool isEmpty() const noexcept
Return true if the box contains no points.
Definition: Box.h:420
double getMaxY() const noexcept
Definition: Box.h:388
double getCenterX() const noexcept
Definition: Box.h:415
A base class for image defects.
void grow(int buffer)
Increase the size of the box by the given buffer amount in all directions.
Definition: Box.h:208
int getMaxY() const noexcept
Definition: Box.h:149
static double const INVALID
Value used to specify undefined coordinate values.
Definition: Box.h:307
int getArea() const
Definition: Box.h:176
size_t operator()(argument_type const &x) const noexcept
Definition: Box.h:542
Point2I Point
Definition: Box.h:56
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
Definition: history.py:168
void grow(double buffer)
Increase the size of the box by the given buffer amount in all directions.
Definition: Box.h:445
int getBeginX() const noexcept
Definition: Box.h:159
int getWidth() const noexcept
Definition: Box.h:174
std::ostream & operator<<(std::ostream &os, lsst::geom::AffineTransform const &transform)
int Element
Definition: Box.h:58
A coordinate class intended to represent offsets and dimensions.
int getMaxX() const noexcept
Definition: Box.h:148
Point2I const getEnd() const noexcept
Definition: Box.h:162
double x
void swap(Point &other) noexcept
Definition: Point.h:202
Extent2I Extent
Definition: Box.h:57
double getArea() const noexcept
Definition: Box.h:400
std::size_t hash_value() const noexcept
Return a hash of this object.
Definition: Box.cc:222
Point2D const getMax() const noexcept
Definition: Box.h:386
void swap(Extent &other) noexcept
Definition: Extent.h:245
bool operator!=(Box2I const &other) const noexcept
Compare two boxes for equality.
Definition: Box.cc:218
double getCenterY() const noexcept
Definition: Box.h:416
int getMinX() const noexcept
Definition: Box.h:144
Extent< int, 2 > Extent2I
Definition: Extent.h:397
STL class.
Extent2D Extent
Definition: Box.h:297
int getEndX() const noexcept
Definition: Box.h:163
std::string toString() const
Definition: Box.h:266
void include(Point2I const &point)
Expand this to ensure that this->contains(point).
Definition: Box.cc:153
double Element
Definition: Box.h:298
void shift(Extent2I const &offset)
Shift the position of the box by the given offset.
Definition: Box.cc:134
bool overlaps(Box2I const &other) const noexcept
Return true if any points in other are also in this.
Definition: Box.cc:122
bool contains(Point2I const &point) const noexcept
Return true if the box contains the point.
Definition: Box.cc:113
~Box2I() noexcept=default
void clip(Box2I const &other) noexcept
Shrink this to ensure that other.contains(*this).
Definition: Box.cc:190
ndarray::View< boost::fusion::vector2< ndarray::index::Range, ndarray::index::Range > > getSlices() const
Return slices to extract the box&#39;s region from an ndarray::Array.
Definition: Box.cc:108
ItemVariant const * other
Definition: Schema.cc:56
static Box2I makeCenteredBox(Point2D const &center, Extent const &size)
Create a box centered as closely as possible on a particular point.
Definition: Box.cc:96
Box2I() noexcept
Construct an empty box.
Definition: Box.h:63
size_t operator()(argument_type const &x) const noexcept
Definition: Box.h:549
Extent< double, 2 > Extent2D
Definition: Extent.h:400
Point2I const getBegin() const noexcept
Definition: Box.h:158
An integer coordinate rectangle.
Definition: Box.h:54
STL class.
int getMinY() const noexcept
Definition: Box.h:145
std::ostream * os
Definition: Schema.cc:746
Box2I & operator=(Box2I const &) noexcept=default
Standard assignment operator.
void flipTB(int yExtent)
Flip a bounding box about the x-axis given a parent box of extent (yExtent).
Definition: Box.cc:146
void swap(Box2I &other) noexcept
Definition: Box.h:128
Box2I BoxI
Definition: Box.h:528