LSSTApplications  10.0-2-g4f67435,11.0.rc2+1,11.0.rc2+12,11.0.rc2+3,11.0.rc2+4,11.0.rc2+5,11.0.rc2+6,11.0.rc2+7,11.0.rc2+8
LSSTDataManagementBasePackage
Box.h
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 /*
3  * LSST Data Management System
4  * Copyright 2008-2014 LSST Corporation.
5  *
6  * This product includes software developed by the
7  * LSST Project (http://www.lsst.org/).
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the LSST License Statement and
20  * the GNU General Public License along with this program. If not,
21  * see <http://www.lsstcorp.org/LegalNotices/>.
22  */
23 
24 #ifndef LSST_AFW_GEOM_BOX_H
25 #define LSST_AFW_GEOM_BOX_H
26 
27 #include <vector>
28 #include "boost/format.hpp"
29 #include "lsst/afw/geom/Point.h"
30 #include "lsst/afw/geom/Extent.h"
31 #include "ndarray.h"
32 
33 namespace lsst { namespace afw { namespace geom {
34 
35 class Box2D;
36 
53 class Box2I {
54 public:
55 
56  typedef Point2I Point;
57  typedef Extent2I Extent;
58 
60 
62  Box2I() : _minimum(0), _dimensions(0) {}
63 
72  Box2I(Point2I const & minimum, Point2I const & maximum, bool invert=true);
73 
82  Box2I(Point2I const & minimum, Extent2I const & dimensions, bool invert=true);
83 
84 
100  explicit Box2I(Box2D const & other, EdgeHandlingEnum edgeHandling=EXPAND);
101 
103  Box2I(Box2I const & other) : _minimum(other._minimum), _dimensions(other._dimensions) {}
104 
105  void swap(Box2I & other) {
106  _minimum.swap(other._minimum);
108  }
109 
111  Box2I & operator=(Box2I const & other) {
112  _minimum = other._minimum;
113  _dimensions = other._dimensions;
114  return *this;
115  }
116 
123  Point2I const getMin() const { return _minimum; }
124  int getMinX() const { return _minimum.getX(); }
125  int getMinY() const { return _minimum.getY(); }
126 
127  Point2I const getMax() const { return _minimum + _dimensions - Extent2I(1); }
128  int getMaxX() const { return _minimum.getX() + _dimensions.getX() - 1; }
129  int getMaxY() const { return _minimum.getY() + _dimensions.getY() - 1; }
131 
138  Point2I const getBegin() const { return _minimum; }
139  int getBeginX() const { return _minimum.getX(); }
140  int getBeginY() const { return _minimum.getY(); }
141 
142  Point2I const getEnd() const { return _minimum + _dimensions; }
143  int getEndX() const { return _minimum.getX() + _dimensions.getX(); }
144  int getEndY() const { return _minimum.getY() + _dimensions.getY(); }
146 
153  Extent2I const getDimensions() const { return _dimensions; }
154  int getWidth() const { return _dimensions.getX(); }
155  int getHeight() const { return _dimensions.getY(); }
156  int getArea() const { return getWidth() * getHeight(); }
158 
161  boost::fusion::vector2< ndarray::index::Range, ndarray::index::Range >
162  >
163  getSlices() const;
164 
166  bool isEmpty() const {
167  return _dimensions.getX() == 0 && _dimensions.getY() == 0;
168  }
169 
171  bool contains(Point2I const & point) const;
172 
178  bool contains(Box2I const & other) const;
179 
185  bool overlaps(Box2I const & other) const;
186 
193  void grow(int buffer) { grow(Extent2I(buffer)); }
194 
201  void grow(Extent2I const & buffer);
202 
204  void shift(Extent2I const & offset);
205 
207  void flipLR(int xExtent);
208 
210  void flipTB(int yExtent);
211 
213  void include(Point2I const & point);
214 
216  void include(Box2I const & other);
217 
219  void clip(Box2I const & other);
220 
226  bool operator==(Box2I const & other) const;
227 
233  bool operator!=(Box2I const & other) const;
234 
241  std::vector<Point2I> getCorners() const;
242 
243  std::string toString() const {
244  return (boost::format("Box2I(%s,%s)") % _minimum.toString() % _dimensions.toString()).str();
245  }
246 
247 private:
250 };
251 
271 class Box2D {
272 public:
273 
274  typedef Point2D Point;
275  typedef Extent2D Extent;
276 
281  static double const EPSILON;
282 
284  static double const INVALID;
285 
287  Box2D();
288 
299  Box2D(Point2D const & minimum, Point2D const & maximum, bool invert=true);
300 
309  Box2D(Point2D const & minimum, Extent2D const & dimensions, bool invert=true);
310 
320  explicit Box2D(Box2I const & other);
321 
323  Box2D(Box2D const & other) : _minimum(other._minimum), _maximum(other._maximum) {}
324 
325  void swap(Box2D & other) {
326  _minimum.swap(other._minimum);
327  _maximum.swap(other._maximum);
328  }
329 
331  Box2D & operator=(Box2D const & other) {
332  _minimum = other._minimum;
333  _maximum = other._maximum;
334  return *this;
335  }
336 
343  Point2D const getMin() const { return _minimum; }
344  double getMinX() const { return _minimum.getX(); }
345  double getMinY() const { return _minimum.getY(); }
346 
347  Point2D const getMax() const { return _maximum; }
348  double getMaxX() const { return _maximum.getX(); }
349  double getMaxY() const { return _maximum.getY(); }
351 
358  Extent2D const getDimensions() const { return isEmpty() ? Extent2D(0.0) : _maximum - _minimum; }
359  double getWidth() const { return isEmpty() ? 0 : _maximum.getX() - _minimum.getX(); }
360  double getHeight() const { return isEmpty() ? 0 : _maximum.getY() - _minimum.getY(); }
361  double getArea() const {
362  Extent2D dim(getDimensions());
363  return dim.getX() * dim.getY();
364  }
366 
373  Point2D const getCenter() const { return Point2D((_minimum.asEigen() + _maximum.asEigen())*0.5); }
374  double getCenterX() const { return (_minimum.getX() + _maximum.getX())*0.5; }
375  double getCenterY() const { return (_minimum.getY() + _maximum.getY())*0.5; }
377 
379  bool isEmpty() const { return _minimum.getX() != _minimum.getX(); }
380 
382  bool contains(Point2D const & point) const;
383 
389  bool contains(Box2D const & other) const;
390 
396  bool overlaps(Box2D const & other) const;
397 
404  void grow(double buffer) { grow(Extent2D(buffer)); }
405 
412  void grow(Extent2D const & buffer);
413 
415  void shift(Extent2D const & offset);
416 
418  void flipLR(float xExtent);
419 
421  void flipTB(float yExtent);
422 
430  void include(Point2D const & point);
431 
433  void include(Box2D const & other);
434 
436  void clip(Box2D const & other);
437 
443  bool operator==(Box2D const & other) const;
444 
450  bool operator!=(Box2D const & other) const;
451 
458  std::vector<Point2D> getCorners() const;
459 
460  std::string toString() const {
461  return (boost::format("Box2D(%s,%s)") % _minimum.toString() % _maximum.toString()).str();
462  }
463 
464 private:
465  void _tweakMax(int n) {
466  if (_maximum[n] < 0.0) {
467  _maximum[n] *= (1.0 - EPSILON);
468  } else if (_maximum[n] > 0.0) {
469  _maximum[n] *= (1.0 + EPSILON);
470  } else {
471  _maximum[n] = EPSILON;
472  }
473  }
476 };
477 
478 typedef Box2D BoxD;
479 typedef Box2I BoxI;
480 
481 std::ostream & operator<<(std::ostream & os, Box2I const & box);
482 
483 std::ostream & operator<<(std::ostream & os, Box2D const & box);
484 
485 }}}
486 
487 #endif
void flipLR(float xExtent)
Flip a bounding box about the y-axis given a parent box of extent (xExtent).
void grow(double buffer)
Increase the size of the box by the given buffer amount in all directions.
Definition: Box.h:404
void swap(Point &other)
Definition: Point.h:174
void clip(Box2I const &other)
Shrink this to ensure that other.contains(*this).
A coordinate class intended to represent absolute positions.
void _tweakMax(int n)
Definition: Box.h:465
Box2D BoxD
Definition: Box.h:478
void clip(Box2D const &other)
Shrink this to ensure that other.contains(*this).
bool operator!=(Box2D const &other) const
Compare two boxes for equality.
int getEndX() const
Definition: Box.h:143
double getMinX() const
Definition: Box.h:344
int getBeginX() const
Definition: Box.h:139
void grow(int buffer)
Increase the size of the box by the given buffer amount in all directions.
Definition: Box.h:193
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.
std::ostream & operator<<(std::ostream &os, lsst::afw::geom::AffineTransform const &transform)
Box2I BoxI
Definition: Box.h:479
bool operator==(Box2I const &other) const
Compare two boxes for equality.
A coordinate class intended to represent offsets and dimensions.
Extent< double, 2 > Extent2D
Definition: Extent.h:358
static double const EPSILON
Definition: Box.h:281
void swap(Extent &other)
Definition: Extent.h:213
void include(Point2D const &point)
Expand this to ensure that this-&gt;contains(point).
Extent2D Extent
Definition: Box.h:275
double getHeight() const
Definition: Box.h:360
int getMinY() const
Definition: Box.h:125
Extent2D const getDimensions() const
Definition: Box.h:358
Extent2I Extent
Definition: Box.h:57
Point2D _maximum
Definition: Box.h:475
Box2I & operator=(Box2I const &other)
Standard assignment operator.
Definition: Box.h:111
Point2I Point
Definition: Box.h:56
int getMaxX() const
Definition: Box.h:128
int getEndY() const
Definition: Box.h:144
Point2D Point
Definition: Box.h:274
bool operator==(Box2D const &other) const
Compare two boxes for equality.
bool overlaps(Box2D const &other) const
Return true if any points in other are also in this.
double getMaxY() const
Definition: Box.h:349
void swap(Box2D &other)
Definition: Box.h:325
A coordinate class intended to represent absolute positions.
Definition: PSF.h:39
An integer coordinate rectangle.
Definition: Box.h:53
void flipTB(int yExtent)
Flip a bounding box about the x-axis given a parent box of extent (yExtent).
double getCenterX() const
Definition: Box.h:374
Extent2I _dimensions
Definition: Box.h:249
void swap(Box2I &other)
Definition: Box.h:105
afw::table::PointKey< int > dimensions
void shift(Extent2I const &offset)
Shift the position of the box by the given offset.
int getHeight() const
Definition: Box.h:155
double getArea() const
Definition: Box.h:361
Extent2I const getDimensions() const
Definition: Box.h:153
Box2I()
Construct an empty box.
Definition: Box.h:62
Point2I const getBegin() const
Definition: Box.h:138
double getWidth() const
Definition: Box.h:359
std::string toString() const
Definition: Extent.h:155
Point2D const getMin() const
Definition: Box.h:343
bool operator!=(Box2I const &other) const
Compare two boxes for equality.
EigenVector const & asEigen() const
Return a fixed-size Eigen representation of the coordinate object.
Box2D(Box2D const &other)
Standard copy constructor.
Definition: Box.h:323
bool contains(Point2I const &point) const
Return true if the box contains the point.
int getArea() const
Definition: Box.h:156
std::string toString() const
Definition: Box.h:243
int getMaxY() const
Definition: Box.h:129
double getMinY() const
Definition: Box.h:345
int getMinX() const
Definition: Box.h:124
void include(Point2I const &point)
Expand this to ensure that this-&gt;contains(point).
void flipLR(int xExtent)
Flip a bounding box about the y-axis given a parent box of extent (xExtent).
std::vector< Point2I > getCorners() const
void shift(Extent2D const &offset)
Shift the position of the box by the given offset.
Box2I(Box2I const &other)
Standard copy constructor.
Definition: Box.h:103
bool isEmpty() const
Return true if the box contains no points.
Definition: Box.h:166
static double const INVALID
Value used to specify undefined coordinate values.
Definition: Box.h:284
std::vector< Point2D > getCorners() const
Point2I const getMin() const
Definition: Box.h:123
A template meta-sequence that defines an arbitrary view into an unspecified array.
Definition: views.h:84
Box2D & operator=(Box2D const &other)
Standard assignment operator.
Definition: Box.h:331
Point2I const getEnd() const
Definition: Box.h:142
Point2D const getMax() const
Definition: Box.h:347
double getCenterY() const
Definition: Box.h:375
Point< double, 2 > Point2D
Definition: Point.h:286
void flipTB(float yExtent)
Flip a bounding box about the x-axis given a parent box of extent (yExtent).
int getBeginY() const
Definition: Box.h:140
Point2I const getMax() const
Definition: Box.h:127
A floating-point coordinate rectangle geometry.
Definition: Box.h:271
std::string toString() const
Definition: Box.h:460
bool isEmpty() const
Return true if the box contains no points.
Definition: Box.h:379
bool overlaps(Box2I const &other) const
Return true if any points in other are also in this.
bool contains(Point2D const &point) const
Return true if the box contains the point.
Point2D _minimum
Definition: Box.h:474
double getMaxX() const
Definition: Box.h:348
A coordinate class intended to represent offsets and dimensions.
Extent< int, 2 > Extent2I
Definition: Extent.h:355
int getWidth() const
Definition: Box.h:154
Point2D const getCenter() const
Definition: Box.h:373
Point2I _minimum
Definition: Box.h:248
Box2D()
Construct an empty box.
std::string toString() const
Definition: Point.h:123