lsst.afw  14.0-40-g0271be365
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  typedef int Element;
60 
62 
64  Box2I() : _minimum(0), _dimensions(0) {}
65 
74  Box2I(Point2I const& minimum, Point2I const& maximum, bool invert = true);
75 
84  Box2I(Point2I const& minimum, Extent2I const& dimensions, bool invert = true);
85 
101  explicit Box2I(Box2D const& other, EdgeHandlingEnum edgeHandling = EXPAND);
102 
104  Box2I(Box2I const& other) : _minimum(other._minimum), _dimensions(other._dimensions) {}
105 
106  void swap(Box2I& other) {
107  _minimum.swap(other._minimum);
108  _dimensions.swap(other._dimensions);
109  }
110 
112  Box2I& operator=(Box2I const& other) {
113  _minimum = other._minimum;
114  _dimensions = other._dimensions;
115  return *this;
116  }
117 
124  Point2I const getMin() const { return _minimum; }
125  int getMinX() const { return _minimum.getX(); }
126  int getMinY() const { return _minimum.getY(); }
127 
128  Point2I const getMax() const { return _minimum + _dimensions - Extent2I(1); }
129  int getMaxX() const { return _minimum.getX() + _dimensions.getX() - 1; }
130  int getMaxY() const { return _minimum.getY() + _dimensions.getY() - 1; }
132 
139  Point2I const getBegin() const { return _minimum; }
140  int getBeginX() const { return _minimum.getX(); }
141  int getBeginY() const { return _minimum.getY(); }
142 
143  Point2I const getEnd() const { return _minimum + _dimensions; }
144  int getEndX() const { return _minimum.getX() + _dimensions.getX(); }
145  int getEndY() const { return _minimum.getY() + _dimensions.getY(); }
147 
154  Extent2I const getDimensions() const { return _dimensions; }
155  int getWidth() const { return _dimensions.getX(); }
156  int getHeight() const { return _dimensions.getY(); }
157  int getArea() const { return getWidth() * getHeight(); }
159 
161  ndarray::View<boost::fusion::vector2<ndarray::index::Range, ndarray::index::Range> > getSlices() const;
162 
164  bool isEmpty() const { return _dimensions.getX() == 0 && _dimensions.getY() == 0; }
165 
167  bool contains(Point2I const& point) const;
168 
174  bool contains(Box2I const& other) const;
175 
181  bool overlaps(Box2I const& other) const;
182 
189  void grow(int buffer) { grow(Extent2I(buffer)); }
190 
197  void grow(Extent2I const& buffer);
198 
200  void shift(Extent2I const& offset);
201 
203  void flipLR(int xExtent);
204 
206  void flipTB(int yExtent);
207 
209  void include(Point2I const& point);
210 
212  void include(Box2I const& other);
213 
215  void clip(Box2I const& other);
216 
222  bool operator==(Box2I const& other) const;
223 
229  bool operator!=(Box2I const& other) const;
230 
238 
240  return (boost::format("Box2I(%s,%s)") % _minimum.toString() % _dimensions.toString()).str();
241  }
242 
243 private:
244  Point2I _minimum;
245  Extent2I _dimensions;
246 };
247 
267 class Box2D {
268 public:
269  typedef Point2D Point;
270  typedef Extent2D Extent;
271  typedef double Element;
272 
277  static double const EPSILON;
278 
280  static double const INVALID;
281 
283  Box2D();
284 
295  Box2D(Point2D const& minimum, Point2D const& maximum, bool invert = true);
296 
305  Box2D(Point2D const& minimum, Extent2D const& dimensions, bool invert = true);
306 
316  explicit Box2D(Box2I const& other);
317 
319  Box2D(Box2D const& other) : _minimum(other._minimum), _maximum(other._maximum) {}
320 
321  void swap(Box2D& other) {
322  _minimum.swap(other._minimum);
323  _maximum.swap(other._maximum);
324  }
325 
327  Box2D& operator=(Box2D const& other) {
328  _minimum = other._minimum;
329  _maximum = other._maximum;
330  return *this;
331  }
332 
339  Point2D const getMin() const { return _minimum; }
340  double getMinX() const { return _minimum.getX(); }
341  double getMinY() const { return _minimum.getY(); }
342 
343  Point2D const getMax() const { return _maximum; }
344  double getMaxX() const { return _maximum.getX(); }
345  double getMaxY() const { return _maximum.getY(); }
347 
354  Extent2D const getDimensions() const { return isEmpty() ? Extent2D(0.0) : _maximum - _minimum; }
355  double getWidth() const { return isEmpty() ? 0 : _maximum.getX() - _minimum.getX(); }
356  double getHeight() const { return isEmpty() ? 0 : _maximum.getY() - _minimum.getY(); }
357  double getArea() const {
358  Extent2D dim(getDimensions());
359  return dim.getX() * dim.getY();
360  }
362 
369  Point2D const getCenter() const { return Point2D((_minimum.asEigen() + _maximum.asEigen()) * 0.5); }
370  double getCenterX() const { return (_minimum.getX() + _maximum.getX()) * 0.5; }
371  double getCenterY() const { return (_minimum.getY() + _maximum.getY()) * 0.5; }
373 
375  bool isEmpty() const { return _minimum.getX() != _minimum.getX(); }
376 
378  bool contains(Point2D const& point) const;
379 
385  bool contains(Box2D const& other) const;
386 
392  bool overlaps(Box2D const& other) const;
393 
400  void grow(double buffer) { grow(Extent2D(buffer)); }
401 
408  void grow(Extent2D const& buffer);
409 
411  void shift(Extent2D const& offset);
412 
414  void flipLR(float xExtent);
415 
417  void flipTB(float yExtent);
418 
426  void include(Point2D const& point);
427 
429  void include(Box2D const& other);
430 
432  void clip(Box2D const& other);
433 
439  bool operator==(Box2D const& other) const;
440 
446  bool operator!=(Box2D const& other) const;
447 
455 
457  return (boost::format("Box2D(%s,%s)") % _minimum.toString() % _maximum.toString()).str();
458  }
459 
460 private:
461  void _tweakMax(int n) {
462  if (_maximum[n] < 0.0) {
463  _maximum[n] *= (1.0 - EPSILON);
464  } else if (_maximum[n] > 0.0) {
465  _maximum[n] *= (1.0 + EPSILON);
466  } else {
467  _maximum[n] = EPSILON;
468  }
469  }
470  Point2D _minimum;
471  Point2D _maximum;
472 };
473 
474 typedef Box2D BoxD;
475 typedef Box2I BoxI;
476 
478 
480 }
481 }
482 }
483 
484 #endif
void grow(double buffer)
Increase the size of the box by the given buffer amount in all directions.
Definition: Box.h:400
std::vector< Point2I > getCorners() const
Get the corner points.
Point2I const getMin() const
Definition: Box.h:124
std::string toString() const
Definition: Point.h:125
bool isEmpty() const
Return true if the box contains no points.
Definition: Box.h:164
Point2I const getBegin() const
Definition: Box.h:139
Point2I const getEnd() const
Definition: Box.h:143
int getMinX() const
Definition: Box.h:125
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:104
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.
Extent< double, 2 > Extent2D
Definition: Extent.h:344
Box2I()
Construct an empty box.
Definition: Box.h:64
std::ostream & operator<<(std::ostream &os, lsst::afw::geom::AffineTransform const &transform)
static double const INVALID
Value used to specify undefined coordinate values.
Definition: Box.h:280
void swap(Point &other)
Definition: Point.h:176
int getArea() const
Definition: Box.h:157
A coordinate class intended to represent offsets and dimensions.
void include(Point2I const &point)
Expand this to ensure that this->contains(point).
Box2D(Box2D const &other)
Standard copy constructor.
Definition: Box.h:319
void swap(Extent &other)
Definition: Extent.h:214
Extent< int, 2 > Extent2I
Definition: Extent.h:341
double getMaxX() const
Definition: Box.h:344
std::string toString() const
Definition: Extent.h:157
int getHeight() const
Definition: Box.h:156
Box2I BoxI
Definition: Box.h:475
std::string toString() const
Definition: Box.h:456
bool isEmpty() const
Return true if the box contains no points.
Definition: Box.h:375
Extent2I Extent
Definition: Box.h:58
A coordinate class intended to represent absolute positions.
Box2D & operator=(Box2D const &other)
Standard assignment operator.
Definition: Box.h:327
An integer coordinate rectangle.
Definition: Box.h:55
Extent2D const getDimensions() const
Definition: Box.h:354
STL class.
Point2I const getMax() const
Definition: Box.h:128
Point2I Point
Definition: Box.h:57
afw::table::PointKey< int > dimensions
Definition: GaussianPsf.cc:44
bool overlaps(Box2I const &other) const
Return true if any points in other are also in this.
A base class for image defects.
std::string toString() const
Definition: Box.h:239
double getCenterX() const
Definition: Box.h:370
Point< double, 2 > Point2D
Definition: Point.h:273
Box2D BoxD
Definition: Box.h:474
Box2I & operator=(Box2I const &other)
Standard assignment operator.
Definition: Box.h:112
Extent2D Extent
Definition: Box.h:270
Point2D const getCenter() const
Definition: Box.h:369
double getMaxY() const
Definition: Box.h:345
double getMinX() const
Definition: Box.h:340
double getHeight() const
Definition: Box.h:356
bool operator==(Box2I const &other) const
Compare two boxes for equality.
static double const EPSILON
Value the maximum coordinate is multiplied by to increase it by the smallest possible amount...
Definition: Box.h:277
int getWidth() const
Definition: Box.h:155
bool contains(Point2I const &point) const
Return true if the box contains the point.
int getEndY() const
Definition: Box.h:145
int getBeginX() const
Definition: Box.h:140
void flipLR(int xExtent)
Flip a bounding box about the y-axis given a parent box of extent (xExtent).
STL class.
void grow(int buffer)
Increase the size of the box by the given buffer amount in all directions.
Definition: Box.h:189
void swap(Box2D &other)
Definition: Box.h:321
int getBeginY() const
Definition: Box.h:141
Extent2I const getDimensions() const
Definition: Box.h:154
int getMaxX() const
Definition: Box.h:129
void flipTB(int yExtent)
Flip a bounding box about the x-axis given a parent box of extent (yExtent).
double getCenterY() const
Definition: Box.h:371
double getArea() const
Definition: Box.h:357
bool operator!=(Box2I const &other) const
Compare two boxes for equality.
int getMaxY() const
Definition: Box.h:130
void clip(Box2I const &other)
Shrink this to ensure that other.contains(*this).
void swap(Box2I &other)
Definition: Box.h:106
A floating-point coordinate rectangle geometry.
Definition: Box.h:267
int getMinY() const
Definition: Box.h:126
int getEndX() const
Definition: Box.h:144
STL class.
double getWidth() const
Definition: Box.h:355
double getMinY() const
Definition: Box.h:341
Point2D const getMax() const
Definition: Box.h:343
Point2D const getMin() const
Definition: Box.h:339