LSSTApplications  12.1-5-gbdcc3ab+2,15.0+14,15.0+30,15.0-1-g19261fa+21,15.0-1-g417ea41,15.0-1-g60afb23+30,15.0-1-g615e0bb+22,15.0-1-g788a293+30,15.0-1-ga91101e+30,15.0-1-gae1598d+13,15.0-1-gd076f1f+28,15.0-1-gdf18595+5,15.0-12-g3681e7a+8,15.0-12-g7952b551+2,15.0-16-g6f0eb036+3,15.0-17-g076ea75+3,15.0-2-g100d730+23,15.0-2-g8aea5f4+1,15.0-2-gf38729e+25,15.0-2-gf60f3cf,15.0-3-g707930d+3,15.0-3-g9103c06+12,15.0-30-g9378914ca+1,15.0-4-g9ee0f43+3,15.0-4-gf6f1c6c+3,15.0-4-gf906033+2,15.0-5-g23e394c+18,15.0-5-g4be42a9+4,15.0-5-gae1eaf0+3,15.0-6-g09241ba+6,15.0-6-g69628aa+4,15.0-6-g81517ef+3,15.0-6-gc1213af+3,15.0-6-gfa9b38f+8,15.0-7-ged79c87+3,15.0-8-g13fca11+3,15.0-8-g67a62d3+5,15.0-8-gcf05001+5,15.0-9-g1e7c341+2,w.2018.25
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 {
55 public:
56  typedef Point2I Point;
57  typedef Extent2I Extent;
58  typedef int Element;
59 
61 
63  Box2I() : _minimum(0), _dimensions(0) {}
64 
73  Box2I(Point2I const& minimum, Point2I const& maximum, bool invert = true);
74 
83  Box2I(Point2I const& minimum, Extent2I const& dimensions, bool invert = true);
84 
100  explicit Box2I(Box2D const& other, EdgeHandlingEnum edgeHandling = EXPAND);
101 
103  Box2I(Box2I const&) = default;
104  Box2I(Box2I&&) = default;
105  ~Box2I() = default;
106 
107  void swap(Box2I& other) {
108  _minimum.swap(other._minimum);
109  _dimensions.swap(other._dimensions);
110  }
111 
113  Box2I& operator=(Box2I const&) = default;
114  Box2I& operator=(Box2I&&) = default;
115 
122  Point2I const getMin() const { return _minimum; }
123  int getMinX() const { return _minimum.getX(); }
124  int getMinY() const { return _minimum.getY(); }
125 
126  Point2I const getMax() const { return _minimum + _dimensions - Extent2I(1); }
127  int getMaxX() const { return _minimum.getX() + _dimensions.getX() - 1; }
128  int getMaxY() const { return _minimum.getY() + _dimensions.getY() - 1; }
130 
137  Point2I const getBegin() const { return _minimum; }
138  int getBeginX() const { return _minimum.getX(); }
139  int getBeginY() const { return _minimum.getY(); }
140 
141  Point2I const getEnd() const { return _minimum + _dimensions; }
142  int getEndX() const { return _minimum.getX() + _dimensions.getX(); }
143  int getEndY() const { return _minimum.getY() + _dimensions.getY(); }
145 
152  Extent2I const getDimensions() const { return _dimensions; }
153  int getWidth() const { return _dimensions.getX(); }
154  int getHeight() const { return _dimensions.getY(); }
155  int getArea() const { return getWidth() * getHeight(); }
157 
159  ndarray::View<boost::fusion::vector2<ndarray::index::Range, ndarray::index::Range> > getSlices() const;
160 
162  bool isEmpty() const { return _dimensions.getX() == 0 && _dimensions.getY() == 0; }
163 
165  bool contains(Point2I const& point) const;
166 
172  bool contains(Box2I const& other) const;
173 
179  bool overlaps(Box2I const& other) const;
180 
187  void grow(int buffer) { grow(Extent2I(buffer)); }
188 
195  void grow(Extent2I const& buffer);
196 
198  void shift(Extent2I const& offset);
199 
201  void flipLR(int xExtent);
202 
204  void flipTB(int yExtent);
205 
207  void include(Point2I const& point);
208 
210  void include(Box2I const& other);
211 
213  void clip(Box2I const& other);
214 
220  bool operator==(Box2I const& other) const;
221 
227  bool operator!=(Box2I const& other) const;
228 
236 
238  return (boost::format("Box2I(%s,%s)") % _minimum.toString() % _dimensions.toString()).str();
239  }
240 
241 private:
242  Point2I _minimum;
243  Extent2I _dimensions;
244 };
245 
265 class Box2D {
266 public:
267  typedef Point2D Point;
268  typedef Extent2D Extent;
269  typedef double Element;
270 
275  static double const EPSILON;
276 
278  static double const INVALID;
279 
281  Box2D();
282 
293  Box2D(Point2D const& minimum, Point2D const& maximum, bool invert = true);
294 
303  Box2D(Point2D const& minimum, Extent2D const& dimensions, bool invert = true);
304 
314  explicit Box2D(Box2I const& other);
315 
317  Box2D(Box2D const&) = default;
318  Box2D(Box2D&&) = default;
319 
320  ~Box2D() = default;
321 
322  void swap(Box2D& other) {
323  _minimum.swap(other._minimum);
324  _maximum.swap(other._maximum);
325  }
326 
328  Box2D& operator=(Box2D const&) = default;
329  Box2D& operator=(Box2D&&) = default;
330 
337  Point2D const getMin() const { return _minimum; }
338  double getMinX() const { return _minimum.getX(); }
339  double getMinY() const { return _minimum.getY(); }
340 
341  Point2D const getMax() const { return _maximum; }
342  double getMaxX() const { return _maximum.getX(); }
343  double getMaxY() const { return _maximum.getY(); }
345 
352  Extent2D const getDimensions() const { return isEmpty() ? Extent2D(0.0) : _maximum - _minimum; }
353  double getWidth() const { return isEmpty() ? 0 : _maximum.getX() - _minimum.getX(); }
354  double getHeight() const { return isEmpty() ? 0 : _maximum.getY() - _minimum.getY(); }
355  double getArea() const {
356  Extent2D dim(getDimensions());
357  return dim.getX() * dim.getY();
358  }
360 
367  Point2D const getCenter() const { return Point2D((_minimum.asEigen() + _maximum.asEigen()) * 0.5); }
368  double getCenterX() const { return (_minimum.getX() + _maximum.getX()) * 0.5; }
369  double getCenterY() const { return (_minimum.getY() + _maximum.getY()) * 0.5; }
371 
373  bool isEmpty() const { return _minimum.getX() != _minimum.getX(); }
374 
376  bool contains(Point2D const& point) const;
377 
383  bool contains(Box2D const& other) const;
384 
390  bool overlaps(Box2D const& other) const;
391 
398  void grow(double buffer) { grow(Extent2D(buffer)); }
399 
406  void grow(Extent2D const& buffer);
407 
409  void shift(Extent2D const& offset);
410 
412  void flipLR(float xExtent);
413 
415  void flipTB(float yExtent);
416 
424  void include(Point2D const& point);
425 
427  void include(Box2D const& other);
428 
430  void clip(Box2D const& other);
431 
437  bool operator==(Box2D const& other) const;
438 
444  bool operator!=(Box2D const& other) const;
445 
453 
455  return (boost::format("Box2D(%s,%s)") % _minimum.toString() % _maximum.toString()).str();
456  }
457 
458 private:
459  void _tweakMax(int n) {
460  if (_maximum[n] < 0.0) {
461  _maximum[n] *= (1.0 - EPSILON);
462  } else if (_maximum[n] > 0.0) {
463  _maximum[n] *= (1.0 + EPSILON);
464  } else {
465  _maximum[n] = EPSILON;
466  }
467  }
468  Point2D _minimum;
469  Point2D _maximum;
470 };
471 
472 typedef Box2D BoxD;
473 typedef Box2I BoxI;
474 
476 
478 
479 } // namespace geom
480 } // namespace lsst
481 
482 #endif
Box2I()
Construct an empty box.
Definition: Box.h:63
Point2D Point
Definition: Box.h:267
afw::table::PointKey< int > dimensions
Definition: GaussianPsf.cc:44
double getCenterY() const
Definition: Box.h:369
Extent2I const getDimensions() const
Definition: Box.h:152
constexpr double EPSILON
Definition: constants.h:54
std::string toString() const
Definition: Box.h:454
int getEndY() const
Definition: Box.h:143
void flipLR(int xExtent)
Flip a bounding box about the y-axis given a parent box of extent (xExtent).
Definition: Box.cc:126
void swap(Box2D &other)
Definition: Box.h:322
int getMaxY() const
Definition: Box.h:128
bool isEmpty() const
Return true if the box contains no points.
Definition: Box.h:162
void swap(Extent &other)
Definition: Extent.h:223
A floating-point coordinate rectangle geometry.
Definition: Box.h:265
Point2I const getMin() const
Definition: Box.h:122
Point2I const getEnd() const
Definition: Box.h:141
double getHeight() const
Definition: Box.h:354
A coordinate class intended to represent absolute positions.
Extent2D const getDimensions() const
Definition: Box.h:352
Point2D const getMin() const
Definition: Box.h:337
Box2D BoxD
Definition: Box.h:472
std::string toString() const
Definition: Point.h:127
double getMinX() const
Definition: Box.h:338
Box2I & operator=(Box2I const &)=default
Standard assignment operator.
Relationship invert(Relationship r)
Given the relationship between two sets A and B (i.e.
Definition: Relationship.h:55
double getWidth() const
Definition: Box.h:353
std::vector< Point2I > getCorners() const
Get the corner points.
Definition: Box.cc:209
Point2I const getMax() const
Definition: Box.h:126
int getEndX() const
Definition: Box.h:142
std::string toString() const
Definition: Extent.h:159
Point< double, 2 > Point2D
Definition: Point.h:300
static double const EPSILON
Value the maximum coordinate is multiplied by to increase it by the smallest possible amount...
Definition: Box.h:275
Point2D const getCenter() const
Definition: Box.h:367
STL class.
double getArea() const
Definition: Box.h:355
double getCenterX() const
Definition: Box.h:368
void swap(Box2I &other)
Definition: Box.h:107
A base class for image defects.
Definition: cameraGeom.dox:3
double getMaxX() const
Definition: Box.h:342
int getHeight() const
Definition: Box.h:154
void grow(int buffer)
Increase the size of the box by the given buffer amount in all directions.
Definition: Box.h:187
int getMinX() const
Definition: Box.h:123
static double const INVALID
Value used to specify undefined coordinate values.
Definition: Box.h:278
int getArea() const
Definition: Box.h:155
Point2I Point
Definition: Box.h:56
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
Definition: history.py:134
void grow(double buffer)
Increase the size of the box by the given buffer amount in all directions.
Definition: Box.h:398
Point2D const getMax() const
Definition: Box.h:341
int getBeginY() const
Definition: Box.h:139
int getMinY() const
Definition: Box.h:124
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.
Extent2I Extent
Definition: Box.h:57
void swap(Point &other)
Definition: Point.h:185
int getWidth() const
Definition: Box.h:153
Extent< int, 2 > Extent2I
Definition: Extent.h:376
STL class.
bool contains(Point2I const &point) const
Return true if the box contains the point.
Definition: Box.cc:100
bool isEmpty() const
Return true if the box contains no points.
Definition: Box.h:373
Extent2D Extent
Definition: Box.h:268
int getMaxX() const
Definition: Box.h:127
std::string toString() const
Definition: Box.h:237
void include(Point2I const &point)
Expand this to ensure that this->contains(point).
Definition: Box.cc:140
double Element
Definition: Box.h:269
void shift(Extent2I const &offset)
Shift the position of the box by the given offset.
Definition: Box.cc:121
bool overlaps(Box2I const &other) const
Return true if any points in other are also in this.
Definition: Box.cc:109
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:95
ItemVariant const * other
Definition: Schema.cc:55
Point2I const getBegin() const
Definition: Box.h:137
Extent< double, 2 > Extent2D
Definition: Extent.h:379
bool operator==(Box2I const &other) const
Compare two boxes for equality.
Definition: Box.cc:201
void clip(Box2I const &other)
Shrink this to ensure that other.contains(*this).
Definition: Box.cc:177
bool operator!=(Box2I const &other) const
Compare two boxes for equality.
Definition: Box.cc:205
An integer coordinate rectangle.
Definition: Box.h:54
STL class.
double getMaxY() const
Definition: Box.h:343
std::ostream * os
Definition: Schema.cc:737
double getMinY() const
Definition: Box.h:339
void flipTB(int yExtent)
Flip a bounding box about the x-axis given a parent box of extent (yExtent).
Definition: Box.cc:133
int getBeginX() const
Definition: Box.h:138
Box2I BoxI
Definition: Box.h:473