LSSTApplications  17.0+50,17.0+84,17.0+9,18.0.0+14,18.0.0+2,18.0.0+30,18.0.0+4,18.0.0+9,18.0.0-2-ge43143a+4,18.1.0-1-g0001055,18.1.0-1-g0896a44+6,18.1.0-1-g1349e88+4,18.1.0-1-g2505f39+3,18.1.0-1-g380d4d4+4,18.1.0-1-g5e4b7ea,18.1.0-1-g85f8cd4+3,18.1.0-1-g9a6769a+2,18.1.0-1-ga1a4c1a+2,18.1.0-1-gc037db8,18.1.0-1-gd55f500+1,18.1.0-1-ge10677a+3,18.1.0-10-g73b8679e+7,18.1.0-11-g311e899+3,18.1.0-12-g0d73a3591,18.1.0-12-gc95f69a+3,18.1.0-2-g000ad9a+3,18.1.0-2-g31c43f9+3,18.1.0-2-g9c63283+4,18.1.0-2-gdf0b915+4,18.1.0-2-gf03bb23,18.1.0-3-g2e29e3d+6,18.1.0-3-g52aa583+2,18.1.0-3-g9cb968e+3,18.1.0-4-gd2e8982+6,18.1.0-5-g510c42a+3,18.1.0-5-gaeab27e+4,18.1.0-6-gdda7f3e+6,18.1.0-7-g89824ecc+4,w.2019.32
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 
185  Point2D const getCenter() const noexcept;
186  double getCenterX() const noexcept { return this->getCenter().getX(); }
187  double getCenterY() const noexcept { return this->getCenter().getY(); }
189 
191  ndarray::View<boost::fusion::vector2<ndarray::index::Range, ndarray::index::Range> > getSlices() const;
192 
194  bool isEmpty() const noexcept { return _dimensions.getX() == 0 && _dimensions.getY() == 0; }
195 
197  bool contains(Point2I const& point) const noexcept;
198 
204  bool contains(Box2I const& other) const noexcept;
205 
211  bool overlaps(Box2I const& other) const noexcept;
212 
219  void grow(int buffer) { grow(Extent2I(buffer)); }
220 
227  void grow(Extent2I const& buffer);
228 
230  void shift(Extent2I const& offset);
231 
233  void flipLR(int xExtent);
234 
236  void flipTB(int yExtent);
237 
239  void include(Point2I const& point);
240 
242  void include(Box2I const& other);
243 
250  void clip(Box2I const& other) noexcept;
251 
257  bool operator==(Box2I const& other) const noexcept;
258 
264  bool operator!=(Box2I const& other) const noexcept;
265 
267  std::size_t hash_value() const noexcept;
268 
276 
278  return (boost::format("Box2I(%s,%s)") % _minimum.toString() % _dimensions.toString()).str();
279  }
280 
281 private:
282  Point2I _minimum;
283  Extent2I _dimensions;
284 };
285 
305 class Box2D final {
306 public:
307  typedef Point2D Point;
308  typedef Extent2D Extent;
309  typedef double Element;
310 
315  static double const EPSILON;
316 
318  static double const INVALID;
319 
321  Box2D() noexcept;
322 
333  Box2D(Point2D const& minimum, Point2D const& maximum, bool invert = true) noexcept;
334 
345  Box2D(Point2D const& corner, Extent2D const& dimensions, bool invert = true) noexcept;
346 
356  explicit Box2D(Box2I const& other) noexcept;
357 
359  Box2D(Box2D const&) noexcept = default;
360  Box2D(Box2D&&) noexcept = default;
361 
362  ~Box2D() noexcept = default;
363 
375  // It's hard to guarantee postconditions (especially size) with non-finite inputs
376  static Box2D makeCenteredBox(Point2D const& center, Extent const& size) noexcept;
377 
378  void swap(Box2D& other) noexcept {
379  _minimum.swap(other._minimum);
380  _maximum.swap(other._maximum);
381  }
382 
384  Box2D& operator=(Box2D const&) noexcept = default;
385  Box2D& operator=(Box2D&&) noexcept = default;
386 
393  Point2D const getMin() const noexcept { return _minimum; }
394  double getMinX() const noexcept { return _minimum.getX(); }
395  double getMinY() const noexcept { return _minimum.getY(); }
396 
397  Point2D const getMax() const noexcept { return _maximum; }
398  double getMaxX() const noexcept { return _maximum.getX(); }
399  double getMaxY() const noexcept { return _maximum.getY(); }
401 
408  Extent2D const getDimensions() const noexcept { return isEmpty() ? Extent2D(0.0) : _maximum - _minimum; }
409  double getWidth() const noexcept { return isEmpty() ? 0 : _maximum.getX() - _minimum.getX(); }
410  double getHeight() const noexcept { return isEmpty() ? 0 : _maximum.getY() - _minimum.getY(); }
411  double getArea() const noexcept {
412  Extent2D dim(getDimensions());
413  return dim.getX() * dim.getY();
414  }
416 
423  Point2D const getCenter() const noexcept {
424  return Point2D((_minimum.asEigen() + _maximum.asEigen()) * 0.5);
425  }
426  double getCenterX() const noexcept { return (_minimum.getX() + _maximum.getX()) * 0.5; }
427  double getCenterY() const noexcept { return (_minimum.getY() + _maximum.getY()) * 0.5; }
429 
431  bool isEmpty() const noexcept { return _minimum.getX() != _minimum.getX(); }
432 
434  bool contains(Point2D const& point) const noexcept;
435 
441  bool contains(Box2D const& other) const noexcept;
442 
448  bool overlaps(Box2D const& other) const noexcept;
449 
456  void grow(double buffer) { grow(Extent2D(buffer)); }
457 
464  void grow(Extent2D const& buffer);
465 
467  void shift(Extent2D const& offset);
468 
470  void flipLR(float xExtent);
471 
473  void flipTB(float yExtent);
474 
482  void include(Point2D const& point) noexcept;
483 
485  void include(Box2D const& other) noexcept;
486 
493  void clip(Box2D const& other) noexcept;
494 
500  bool operator==(Box2D const& other) const noexcept;
501 
507  bool operator!=(Box2D const& other) const noexcept;
508 
510  std::size_t hash_value() const noexcept;
511 
519 
521  return (boost::format("Box2D(%s,%s)") % _minimum.toString() % _maximum.toString()).str();
522  }
523 
524 private:
525  void _tweakMax(int n) noexcept {
526  if (_maximum[n] < 0.0) {
527  _maximum[n] *= (1.0 - EPSILON);
528  } else if (_maximum[n] > 0.0) {
529  _maximum[n] *= (1.0 + EPSILON);
530  } else {
531  _maximum[n] = EPSILON;
532  }
533  }
534  Point2D _minimum;
535  Point2D _maximum;
536 };
537 
538 typedef Box2D BoxD;
539 typedef Box2I BoxI;
540 
542 
544 
545 } // namespace geom
546 } // namespace lsst
547 
548 namespace std {
549 template <>
553  size_t operator()(argument_type const& x) const noexcept { return x.hash_value(); }
554 };
555 
556 template <>
560  size_t operator()(argument_type const& x) const noexcept { return x.hash_value(); }
561 };
562 } // namespace std
563 
564 #endif
bool operator==(Box2I const &other) const noexcept
Compare two boxes for equality.
Definition: Box.cc:218
double getMinY() const noexcept
Definition: Box.h:395
Extent2I const getDimensions() const noexcept
Definition: Box.h:173
Point2D Point
Definition: Box.h:307
double getHeight() const noexcept
Definition: Box.h:410
Point2I const getMax() const noexcept
Definition: Box.h:147
constexpr double EPSILON
Definition: constants.h:54
std::string toString() const
Definition: Box.h:520
void flipLR(int xExtent)
Flip a bounding box about the y-axis given a parent box of extent (xExtent).
Definition: Box.cc:143
double getWidth() const noexcept
Definition: Box.h:409
int getHeight() const noexcept
Definition: Box.h:175
A floating-point coordinate rectangle geometry.
Definition: Box.h:305
Point2D const getCenter() const noexcept
Definition: Box.h:423
double getMinX() const noexcept
Definition: Box.h:394
A coordinate class intended to represent absolute positions.
bool isEmpty() const noexcept
Return true if the box contains no points.
Definition: Box.h:194
double getMaxX() const noexcept
Definition: Box.h:398
STL namespace.
Box2D BoxD
Definition: Box.h:538
Extent2D const getDimensions() const noexcept
Definition: Box.h:408
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
ItemVariant const * other
Definition: Schema.cc:56
Point2D const getMin() const noexcept
Definition: Box.h:393
std::vector< Point2I > getCorners() const
Get the corner points.
Definition: Box.cc:231
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:315
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:431
afw::table::PointKey< int > dimensions
Definition: GaussianPsf.cc:49
double getMaxY() const noexcept
Definition: Box.h:399
double getCenterX() const noexcept
Definition: Box.h:186
double getCenterX() const noexcept
Definition: Box.h:426
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:219
int getMaxY() const noexcept
Definition: Box.h:149
static double const INVALID
Value used to specify undefined coordinate values.
Definition: Box.h:318
int getArea() const
Definition: Box.h:176
size_t operator()(argument_type const &x) const noexcept
Definition: Box.h:553
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:456
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:411
std::size_t hash_value() const noexcept
Return a hash of this object.
Definition: Box.cc:226
Point2D const getMax() const noexcept
Definition: Box.h:397
double getCenterY() const noexcept
Definition: Box.h:187
void swap(Extent &other) noexcept
Definition: Extent.h:245
bool operator!=(Box2I const &other) const noexcept
Compare two boxes for equality.
Definition: Box.cc:222
double getCenterY() const noexcept
Definition: Box.h:427
int getMinX() const noexcept
Definition: Box.h:144
Extent< int, 2 > Extent2I
Definition: Extent.h:397
STL class.
Extent2D Extent
Definition: Box.h:308
int getEndX() const noexcept
Definition: Box.h:163
std::string toString() const
Definition: Box.h:277
void include(Point2I const &point)
Expand this to ensure that this->contains(point).
Definition: Box.cc:157
double Element
Definition: Box.h:309
void shift(Extent2I const &offset)
Shift the position of the box by the given offset.
Definition: Box.cc:138
bool overlaps(Box2I const &other) const noexcept
Return true if any points in other are also in this.
Definition: Box.cc:126
bool contains(Point2I const &point) const noexcept
Return true if the box contains the point.
Definition: Box.cc:117
~Box2I() noexcept=default
void clip(Box2I const &other) noexcept
Shrink this to ensure that other.contains(*this).
Definition: Box.cc:194
Point2D const getCenter() const noexcept
Definition: Box.cc:96
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:112
static Box2I makeCenteredBox(Point2D const &center, Extent const &size)
Create a box centered as closely as possible on a particular point.
Definition: Box.cc:100
Box2I() noexcept
Construct an empty box.
Definition: Box.h:63
size_t operator()(argument_type const &x) const noexcept
Definition: Box.h:560
Extent< double, 2 > Extent2D
Definition: Extent.h:400
Point2I const getBegin() const noexcept
Definition: Box.h:158
std::ostream * os
Definition: Schema.cc:746
An integer coordinate rectangle.
Definition: Box.h:54
STL class.
int getMinY() const noexcept
Definition: Box.h:145
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:150
void swap(Box2I &other) noexcept
Definition: Box.h:128
Box2I BoxI
Definition: Box.h:539