LSSTApplications  16.0-1-gce273f5+17,16.0-1-gf77f410+12,16.0-10-g5a5abec+8,16.0-10-gc1446dd+12,16.0-12-g1dc09ba+6,16.0-12-g569485f,16.0-12-ga22ed6e+1,16.0-13-g4c33ca5+12,16.0-13-gb122224+3,16.0-13-gd9b1b71+12,16.0-14-g22e2ff2,16.0-14-g71e547a+8,16.0-17-g0bdc215+4,16.0-17-g6a7bfb3b+12,16.0-2-g0febb12+14,16.0-2-g839ba83+50,16.0-2-g9d5294e+39,16.0-20-ga7ad2685,16.0-3-g404ea43+9,16.0-3-gbc759ec+10,16.0-3-gcfd6c53+37,16.0-4-g03cf288+28,16.0-4-g13a27c5+14,16.0-4-g5f3a788+13,16.0-4-g8a0f11a+34,16.0-4-ga3eb747+3,16.0-45-g4805a823c,16.0-5-g1991253+12,16.0-5-g1e9226d+1,16.0-5-g865efd9+12,16.0-5-gb3f8a4b+44,16.0-5-gd0f1235+6,16.0-6-gf0acd13+31,16.0-6-gf9cb114+13,16.0-7-g6043bfc,16.0-7-ga8e1655+8,16.0-8-g23bbf3f+3,16.0-8-g4dec96c+25,16.0-8-gfd407c0+2,master-g965b868a3d+1,master-gdc6be1965f+1,w.2018.39
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 
262 
264  return (boost::format("Box2I(%s,%s)") % _minimum.toString() % _dimensions.toString()).str();
265  }
266 
267 private:
268  Point2I _minimum;
269  Extent2I _dimensions;
270 };
271 
291 class Box2D final {
292 public:
293  typedef Point2D Point;
294  typedef Extent2D Extent;
295  typedef double Element;
296 
301  static double const EPSILON;
302 
304  static double const INVALID;
305 
307  Box2D() noexcept;
308 
319  Box2D(Point2D const& minimum, Point2D const& maximum, bool invert = true) noexcept;
320 
331  Box2D(Point2D const& corner, Extent2D const& dimensions, bool invert = true) noexcept;
332 
342  explicit Box2D(Box2I const& other) noexcept;
343 
345  Box2D(Box2D const&) noexcept = default;
346  Box2D(Box2D&&) noexcept = default;
347 
348  ~Box2D() noexcept = default;
349 
361  // It's hard to guarantee postconditions (especially size) with non-finite inputs
362  static Box2D makeCenteredBox(Point2D const& center, Extent const& size) noexcept;
363 
364  void swap(Box2D& other) noexcept {
365  _minimum.swap(other._minimum);
366  _maximum.swap(other._maximum);
367  }
368 
370  Box2D& operator=(Box2D const&) noexcept = default;
371  Box2D& operator=(Box2D&&) noexcept = default;
372 
379  Point2D const getMin() const noexcept { return _minimum; }
380  double getMinX() const noexcept { return _minimum.getX(); }
381  double getMinY() const noexcept { return _minimum.getY(); }
382 
383  Point2D const getMax() const noexcept { return _maximum; }
384  double getMaxX() const noexcept { return _maximum.getX(); }
385  double getMaxY() const noexcept { return _maximum.getY(); }
387 
394  Extent2D const getDimensions() const noexcept { return isEmpty() ? Extent2D(0.0) : _maximum - _minimum; }
395  double getWidth() const noexcept { return isEmpty() ? 0 : _maximum.getX() - _minimum.getX(); }
396  double getHeight() const noexcept { return isEmpty() ? 0 : _maximum.getY() - _minimum.getY(); }
397  double getArea() const noexcept {
398  Extent2D dim(getDimensions());
399  return dim.getX() * dim.getY();
400  }
402 
409  Point2D const getCenter() const noexcept {
410  return Point2D((_minimum.asEigen() + _maximum.asEigen()) * 0.5);
411  }
412  double getCenterX() const noexcept { return (_minimum.getX() + _maximum.getX()) * 0.5; }
413  double getCenterY() const noexcept { return (_minimum.getY() + _maximum.getY()) * 0.5; }
415 
417  bool isEmpty() const noexcept { return _minimum.getX() != _minimum.getX(); }
418 
420  bool contains(Point2D const& point) const noexcept;
421 
427  bool contains(Box2D const& other) const noexcept;
428 
434  bool overlaps(Box2D const& other) const noexcept;
435 
442  void grow(double buffer) { grow(Extent2D(buffer)); }
443 
450  void grow(Extent2D const& buffer);
451 
453  void shift(Extent2D const& offset);
454 
456  void flipLR(float xExtent);
457 
459  void flipTB(float yExtent);
460 
468  void include(Point2D const& point) noexcept;
469 
471  void include(Box2D const& other) noexcept;
472 
479  void clip(Box2D const& other) noexcept;
480 
486  bool operator==(Box2D const& other) const noexcept;
487 
493  bool operator!=(Box2D const& other) const noexcept;
494 
502 
504  return (boost::format("Box2D(%s,%s)") % _minimum.toString() % _maximum.toString()).str();
505  }
506 
507 private:
508  void _tweakMax(int n) noexcept {
509  if (_maximum[n] < 0.0) {
510  _maximum[n] *= (1.0 - EPSILON);
511  } else if (_maximum[n] > 0.0) {
512  _maximum[n] *= (1.0 + EPSILON);
513  } else {
514  _maximum[n] = EPSILON;
515  }
516  }
517  Point2D _minimum;
518  Point2D _maximum;
519 };
520 
521 typedef Box2D BoxD;
522 typedef Box2I BoxI;
523 
525 
527 
528 } // namespace geom
529 } // namespace lsst
530 
531 #endif
bool operator==(Box2I const &other) const noexcept
Compare two boxes for equality.
Definition: Box.cc:213
double getMinY() const noexcept
Definition: Box.h:381
Extent2I const getDimensions() const noexcept
Definition: Box.h:173
Point2D Point
Definition: Box.h:293
double getHeight() const noexcept
Definition: Box.h:396
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:503
void flipLR(int xExtent)
Flip a bounding box about the y-axis given a parent box of extent (xExtent).
Definition: Box.cc:138
double getWidth() const noexcept
Definition: Box.h:395
int getHeight() const noexcept
Definition: Box.h:175
A floating-point coordinate rectangle geometry.
Definition: Box.h:291
Point2D const getCenter() const noexcept
Definition: Box.h:409
double getMinX() const noexcept
Definition: Box.h:380
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:384
Box2D BoxD
Definition: Box.h:521
Extent2D const getDimensions() const noexcept
Definition: Box.h:394
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:379
std::vector< Point2I > getCorners() const
Get the corner points.
Definition: Box.cc:221
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:301
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:417
double getMaxY() const noexcept
Definition: Box.h:385
double getCenterX() const noexcept
Definition: Box.h:412
A base class for image defects.
Definition: cameraGeom.dox:3
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:304
int getArea() const
Definition: Box.h:176
Point2I Point
Definition: Box.h:56
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
Definition: history.py:129
void grow(double buffer)
Increase the size of the box by the given buffer amount in all directions.
Definition: Box.h:442
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
void swap(Point &other) noexcept
Definition: Point.h:202
Extent2I Extent
Definition: Box.h:57
double getArea() const noexcept
Definition: Box.h:397
Point2D const getMax() const noexcept
Definition: Box.h:383
void swap(Extent &other) noexcept
Definition: Extent.h:245
bool operator!=(Box2I const &other) const noexcept
Compare two boxes for equality.
Definition: Box.cc:217
double getCenterY() const noexcept
Definition: Box.h:413
int getMinX() const noexcept
Definition: Box.h:144
Extent< int, 2 > Extent2I
Definition: Extent.h:393
STL class.
Extent2D Extent
Definition: Box.h:294
int getEndX() const noexcept
Definition: Box.h:163
std::string toString() const
Definition: Box.h:263
void include(Point2I const &point)
Expand this to ensure that this->contains(point).
Definition: Box.cc:152
double Element
Definition: Box.h:295
void shift(Extent2I const &offset)
Shift the position of the box by the given offset.
Definition: Box.cc:133
bool overlaps(Box2I const &other) const noexcept
Return true if any points in other are also in this.
Definition: Box.cc:121
bool contains(Point2I const &point) const noexcept
Return true if the box contains the point.
Definition: Box.cc:112
~Box2I() noexcept=default
void clip(Box2I const &other) noexcept
Shrink this to ensure that other.contains(*this).
Definition: Box.cc:189
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:107
ItemVariant const * other
Definition: Schema.cc:55
static Box2I makeCenteredBox(Point2D const &center, Extent const &size)
Create a box centered as closely as possible on a particular point.
Definition: Box.cc:95
Box2I() noexcept
Construct an empty box.
Definition: Box.h:63
Extent< double, 2 > Extent2D
Definition: Extent.h:396
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:737
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:145
void swap(Box2I &other) noexcept
Definition: Box.h:128
Box2I BoxI
Definition: Box.h:522