LSSTApplications  11.0-24-g0a022a1,15.0+13,15.0+9,15.0-1-g19261fa+5,15.0-1-g1eca518+15,15.0-1-g60afb23+12,15.0-1-g615e0bb+4,15.0-1-g6668b0b+5,15.0-1-g788a293+12,15.0-1-ga91101e+12,15.0-1-gae1598d+8,15.0-1-gc45031d+15,15.0-1-gd076f1f+12,15.0-1-gdf18595+2,15.0-1-gf4f1c34+8,15.0-2-g100d730+5,15.0-2-g18f3f21+5,15.0-2-g35685a8+6,15.0-2-gf38729e+5,15.0-21-g91b8abf62,15.0-3-g150fc43+14,15.0-3-g6f085af+5,15.0-3-g707930d,15.0-3-g9103c06+8,15.0-3-ga03b4ca+16,15.0-3-gaec6799+5,15.0-3-gb7a597c+12,15.0-3-ge6a6747+5,15.0-4-g45f767a+8,15.0-4-g654b129+10,15.0-4-gf5d1e39,15.0-4-gff20472+15,15.0-5-ga70c291+5,15.0-6-g9a9df217+5,15.0-7-gab4c137+6,15.0-7-gab79a70c+4
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 {
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&) = default;
105  Box2I(Box2I&&) = default;
106  ~Box2I() = default;
107 
108  void swap(Box2I& other) {
109  _minimum.swap(other._minimum);
110  _dimensions.swap(other._dimensions);
111  }
112 
114  Box2I& operator=(Box2I const&) = default;
115  Box2I& operator=(Box2I&&) = default;
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 
237 
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  typedef double Element;
271 
276  static double const EPSILON;
277 
279  static double const INVALID;
280 
282  Box2D();
283 
294  Box2D(Point2D const& minimum, Point2D const& maximum, bool invert = true);
295 
304  Box2D(Point2D const& minimum, Extent2D const& dimensions, bool invert = true);
305 
315  explicit Box2D(Box2I const& other);
316 
318  Box2D(Box2D const&) = default;
319  Box2D(Box2D&&) = default;
320 
321  ~Box2D() = default;
322 
323  void swap(Box2D& other) {
324  _minimum.swap(other._minimum);
325  _maximum.swap(other._maximum);
326  }
327 
329  Box2D& operator=(Box2D const&) = default;
330  Box2D& operator=(Box2D&&) = default;
331 
338  Point2D const getMin() const { return _minimum; }
339  double getMinX() const { return _minimum.getX(); }
340  double getMinY() const { return _minimum.getY(); }
341 
342  Point2D const getMax() const { return _maximum; }
343  double getMaxX() const { return _maximum.getX(); }
344  double getMaxY() const { return _maximum.getY(); }
346 
353  Extent2D const getDimensions() const { return isEmpty() ? Extent2D(0.0) : _maximum - _minimum; }
354  double getWidth() const { return isEmpty() ? 0 : _maximum.getX() - _minimum.getX(); }
355  double getHeight() const { return isEmpty() ? 0 : _maximum.getY() - _minimum.getY(); }
356  double getArea() const {
357  Extent2D dim(getDimensions());
358  return dim.getX() * dim.getY();
359  }
361 
368  Point2D const getCenter() const { return Point2D((_minimum.asEigen() + _maximum.asEigen()) * 0.5); }
369  double getCenterX() const { return (_minimum.getX() + _maximum.getX()) * 0.5; }
370  double getCenterY() const { return (_minimum.getY() + _maximum.getY()) * 0.5; }
372 
374  bool isEmpty() const { return _minimum.getX() != _minimum.getX(); }
375 
377  bool contains(Point2D const& point) const;
378 
384  bool contains(Box2D const& other) const;
385 
391  bool overlaps(Box2D const& other) const;
392 
399  void grow(double buffer) { grow(Extent2D(buffer)); }
400 
407  void grow(Extent2D const& buffer);
408 
410  void shift(Extent2D const& offset);
411 
413  void flipLR(float xExtent);
414 
416  void flipTB(float yExtent);
417 
425  void include(Point2D const& point);
426 
428  void include(Box2D const& other);
429 
431  void clip(Box2D const& other);
432 
438  bool operator==(Box2D const& other) const;
439 
445  bool operator!=(Box2D const& other) const;
446 
454 
456  return (boost::format("Box2D(%s,%s)") % _minimum.toString() % _maximum.toString()).str();
457  }
458 
459 private:
460  void _tweakMax(int n) {
461  if (_maximum[n] < 0.0) {
462  _maximum[n] *= (1.0 - EPSILON);
463  } else if (_maximum[n] > 0.0) {
464  _maximum[n] *= (1.0 + EPSILON);
465  } else {
466  _maximum[n] = EPSILON;
467  }
468  }
469  Point2D _minimum;
470  Point2D _maximum;
471 };
472 
473 typedef Box2D BoxD;
474 typedef Box2I BoxI;
475 
477 
479 }
480 }
481 }
482 
483 #endif
void grow(double buffer)
Increase the size of the box by the given buffer amount in all directions.
Definition: Box.h:399
Point2I const getMin() const
Definition: Box.h:123
afw::table::PointKey< int > dimensions
Definition: GaussianPsf.cc:44
std::string toString() const
Definition: Point.h:131
bool isEmpty() const
Return true if the box contains no points.
Definition: Box.h:163
Point2I const getBegin() const
Definition: Box.h:138
Point2I const getEnd() const
Definition: Box.h:142
int getMinX() const
Definition: Box.h:124
void shift(Extent2I const &offset)
Shift the position of the box by the given offset.
Definition: Box.cc:123
Extent< double, 2 > Extent2D
Definition: Extent.h:383
std::ostream & operator<<(std::ostream &os, lsst::afw::geom::AffineTransform const &transform)
Box2I()
Construct an empty box.
Definition: Box.h:64
static double const INVALID
Value used to specify undefined coordinate values.
Definition: Box.h:279
void swap(Point &other)
Definition: Point.h:189
int getArea() const
Definition: Box.h:156
A coordinate class intended to represent offsets and dimensions.
void include(Point2I const &point)
Expand this to ensure that this->contains(point).
Definition: Box.cc:142
void swap(Extent &other)
Definition: Extent.h:227
Extent< int, 2 > Extent2I
Definition: Extent.h:380
double getMaxX() const
Definition: Box.h:343
std::string toString() const
Definition: Extent.h:163
int getHeight() const
Definition: Box.h:155
Box2I BoxI
Definition: Box.h:474
std::string toString() const
Definition: Box.h:455
bool isEmpty() const
Return true if the box contains no points.
Definition: Box.h:374
Extent2I Extent
Definition: Box.h:58
A coordinate class intended to represent absolute positions.
An integer coordinate rectangle.
Definition: Box.h:55
Extent2D const getDimensions() const
Definition: Box.h:353
STL class.
Point2I const getMax() const
Definition: Box.h:127
Box2I & operator=(Box2I const &)=default
Standard assignment operator.
Point2I Point
Definition: Box.h:57
bool overlaps(Box2I const &other) const
Return true if any points in other are also in this.
Definition: Box.cc:111
A base class for image defects.
Definition: cameraGeom.dox:3
std::string toString() const
Definition: Box.h:238
double getCenterX() const
Definition: Box.h:369
Point< double, 2 > Point2D
Definition: Point.h:304
Box2D BoxD
Definition: Box.h:473
Extent2D Extent
Definition: Box.h:269
Point2D const getCenter() const
Definition: Box.h:368
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
Definition: history.py:134
double getMaxY() const
Definition: Box.h:344
double getMinX() const
Definition: Box.h:339
double getHeight() const
Definition: Box.h:355
bool operator==(Box2I const &other) const
Compare two boxes for equality.
Definition: Box.cc:203
static double const EPSILON
Value the maximum coordinate is multiplied by to increase it by the smallest possible amount...
Definition: Box.h:276
int getWidth() const
Definition: Box.h:154
bool contains(Point2I const &point) const
Return true if the box contains the point.
Definition: Box.cc:102
int getEndY() const
Definition: Box.h:144
int getBeginX() const
Definition: Box.h:139
void flipLR(int xExtent)
Flip a bounding box about the y-axis given a parent box of extent (xExtent).
Definition: Box.cc:128
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:97
STL class.
std::vector< Point2I > getCorners() const
Get the corner points.
Definition: Box.cc:211
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:323
int getBeginY() const
Definition: Box.h:140
Extent2I const getDimensions() const
Definition: Box.h:153
int getMaxX() const
Definition: Box.h:128
void flipTB(int yExtent)
Flip a bounding box about the x-axis given a parent box of extent (yExtent).
Definition: Box.cc:135
ItemVariant const * other
Definition: Schema.cc:55
double getCenterY() const
Definition: Box.h:370
double getArea() const
Definition: Box.h:356
bool operator!=(Box2I const &other) const
Compare two boxes for equality.
Definition: Box.cc:207
int getMaxY() const
Definition: Box.h:129
void clip(Box2I const &other)
Shrink this to ensure that other.contains(*this).
Definition: Box.cc:179
void swap(Box2I &other)
Definition: Box.h:108
A floating-point coordinate rectangle geometry.
Definition: Box.h:266
int getMinY() const
Definition: Box.h:125
int getEndX() const
Definition: Box.h:143
STL class.
std::ostream * os
Definition: Schema.cc:736
double getWidth() const
Definition: Box.h:354
double getMinY() const
Definition: Box.h:340
Point2D const getMax() const
Definition: Box.h:342
Point2D const getMin() const
Definition: Box.h:338