lsst.afw  13.0-73-gb2a4e38
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 {
34 namespace afw {
35 namespace geom {
36 
37 class Box2D;
38 
55 class Box2I {
56 public:
57  typedef Point2I Point;
58  typedef Extent2I Extent;
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& other) : _minimum(other._minimum), _dimensions(other._dimensions) {}
104 
105  void swap(Box2I& other) {
106  _minimum.swap(other._minimum);
107  _dimensions.swap(other._dimensions);
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 
160  ndarray::View<boost::fusion::vector2<ndarray::index::Range, ndarray::index::Range> > getSlices() const;
161 
163  bool isEmpty() const { return _dimensions.getX() == 0 && _dimensions.getY() == 0; }
164 
166  bool contains(Point2I const& point) const;
167 
173  bool contains(Box2I const& other) const;
174 
180  bool overlaps(Box2I const& other) const;
181 
188  void grow(int buffer) { grow(Extent2I(buffer)); }
189 
196  void grow(Extent2I const& buffer);
197 
199  void shift(Extent2I const& offset);
200 
202  void flipLR(int xExtent);
203 
205  void flipTB(int yExtent);
206 
208  void include(Point2I const& point);
209 
211  void include(Box2I const& other);
212 
214  void clip(Box2I const& other);
215 
221  bool operator==(Box2I const& other) const;
222 
228  bool operator!=(Box2I const& other) const;
229 
236  std::vector<Point2I> getCorners() const;
237 
238  std::string toString() const {
239  return (boost::format("Box2I(%s,%s)") % _minimum.toString() % _dimensions.toString()).str();
240  }
241 
242 private:
243  Point2I _minimum;
244  Extent2I _dimensions;
245 };
246 
266 class Box2D {
267 public:
268  typedef Point2D Point;
269  typedef Extent2D Extent;
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& other) : _minimum(other._minimum), _maximum(other._maximum) {}
318 
319  void swap(Box2D& other) {
320  _minimum.swap(other._minimum);
321  _maximum.swap(other._maximum);
322  }
323 
325  Box2D& operator=(Box2D const& other) {
326  _minimum = other._minimum;
327  _maximum = other._maximum;
328  return *this;
329  }
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 
452  std::vector<Point2D> getCorners() const;
453 
454  std::string toString() const {
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 
475 std::ostream& operator<<(std::ostream& os, Box2I const& box);
476 
477 std::ostream& operator<<(std::ostream& os, Box2D const& box);
478 }
479 }
480 }
481 
482 #endif
void grow(double buffer)
Increase the size of the box by the given buffer amount in all directions.
Definition: Box.h:398
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.
afw::table::PointKey< int > dimensions
Definition: GaussianPsf.cc:44
int getMaxY() const
Definition: Box.h:129
int getBeginX() const
Definition: Box.h:139
Extent2I const getDimensions() const
Definition: Box.h:153
bool contains(Point2D const &point) const
Return true if the box contains the point.
Point2D const getCenter() const
Definition: Box.h:367
double getMinY() const
Definition: Box.h:339
void shift(Extent2I const &offset)
Shift the position of the box by the given offset.
Box2I(Box2I const &other)
Standard copy constructor.
Definition: Box.h:103
int getEndY() const
Definition: Box.h:144
int getBeginY() const
Definition: Box.h:140
Extent< double, 2 > Extent2D
Definition: Extent.h:344
bool contains(Point2I const &point) const
Return true if the box contains the point.
Point2D const getMin() const
Definition: Box.h:337
Box2I()
Construct an empty box.
Definition: Box.h:63
std::ostream & operator<<(std::ostream &os, lsst::afw::geom::AffineTransform const &transform)
double getHeight() const
Definition: Box.h:354
static double const INVALID
Value used to specify undefined coordinate values.
Definition: Box.h:278
void swap(Point &other)
Definition: Point.h:176
bool isEmpty() const
Return true if the box contains no points.
Definition: Box.h:373
A coordinate class intended to represent offsets and dimensions.
void include(Point2I const &point)
Expand this to ensure that this-&gt;contains(point).
bool overlaps(Box2I const &other) const
Return true if any points in other are also in this.
bool operator==(Box2I const &other) const
Compare two boxes for equality.
Point2D const getMax() const
Definition: Box.h:341
bool operator!=(Box2I const &other) const
Compare two boxes for equality.
bool operator==(Box2D const &other) const
Compare two boxes for equality.
Box2D(Box2D const &other)
Standard copy constructor.
Definition: Box.h:317
void swap(Extent &other)
Definition: Extent.h:214
Extent< int, 2 > Extent2I
Definition: Extent.h:341
Point2I const getMax() const
Definition: Box.h:127
Box2I BoxI
Definition: Box.h:473
Extent2I Extent
Definition: Box.h:58
double getMaxX() const
Definition: Box.h:342
A coordinate class intended to represent absolute positions.
Box2D & operator=(Box2D const &other)
Standard assignment operator.
Definition: Box.h:325
An integer coordinate rectangle.
Definition: Box.h:55
void clip(Box2D const &other)
Shrink this to ensure that other.contains(*this).
Point2I Point
Definition: Box.h:57
std::string toString() const
Definition: Box.h:454
int getEndX() const
Definition: Box.h:143
double getCenterX() const
Definition: Box.h:368
Point< double, 2 > Point2D
Definition: Point.h:273
double getCenterY() const
Definition: Box.h:369
void flipTB(float yExtent)
Flip a bounding box about the x-axis given a parent box of extent (yExtent).
Box2D BoxD
Definition: Box.h:472
int getMinY() const
Definition: Box.h:125
Box2I & operator=(Box2I const &other)
Standard assignment operator.
Definition: Box.h:111
Extent2D Extent
Definition: Box.h:269
std::string toString() const
Definition: Point.h:125
void include(Point2D const &point)
Expand this to ensure that this-&gt;contains(point).
int getMinX() const
Definition: Box.h:124
static double const EPSILON
Value the maximum coordinate is multiplied by to increase it by the smallest possible amount...
Definition: Box.h:275
std::vector< Point2D > getCorners() const
Get the corner points.
std::string toString() const
Definition: Box.h:238
bool isEmpty() const
Return true if the box contains no points.
Definition: Box.h:163
std::string toString() const
Definition: Extent.h:157
void flipLR(int xExtent)
Flip a bounding box about the y-axis given a parent box of extent (xExtent).
Box2D()
Construct an empty box.
int getWidth() const
Definition: Box.h:154
void flipLR(float xExtent)
Flip a bounding box about the y-axis given a parent box of extent (xExtent).
void grow(int buffer)
Increase the size of the box by the given buffer amount in all directions.
Definition: Box.h:188
void swap(Box2D &other)
Definition: Box.h:319
int getHeight() const
Definition: Box.h:155
double getArea() const
Definition: Box.h:355
EigenVector const & asEigen() const
Return a fixed-size Eigen representation of the coordinate object.
int getArea() const
Definition: Box.h:156
void flipTB(int yExtent)
Flip a bounding box about the x-axis given a parent box of extent (yExtent).
Point2I const getEnd() const
Definition: Box.h:142
bool overlaps(Box2D const &other) const
Return true if any points in other are also in this.
void shift(Extent2D const &offset)
Shift the position of the box by the given offset.
double getMinX() const
Definition: Box.h:338
Point2I const getBegin() const
Definition: Box.h:138
void clip(Box2I const &other)
Shrink this to ensure that other.contains(*this).
double getWidth() const
Definition: Box.h:353
int getMaxX() const
Definition: Box.h:128
void swap(Box2I &other)
Definition: Box.h:105
A floating-point coordinate rectangle geometry.
Definition: Box.h:266
Point2I const getMin() const
Definition: Box.h:123
bool operator!=(Box2D const &other) const
Compare two boxes for equality.
std::vector< Point2I > getCorners() const
Get the corner points.
double getMaxY() const
Definition: Box.h:343
Extent2D const getDimensions() const
Definition: Box.h:352