lsst.geom  16.0-2-ge920381
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 
85  Box2I(Point2I const& corner, Extent2I const& dimensions, bool invert = true);
86 
102  explicit Box2I(Box2D const& other, EdgeHandlingEnum edgeHandling = EXPAND);
103 
105  Box2I(Box2I const&) = default;
106  Box2I(Box2I&&) = default;
107  ~Box2I() = default;
108 
119  static Box2I makeCenteredBox(Point2D const& center, Extent const& size);
120 
121  void swap(Box2I& other) {
122  _minimum.swap(other._minimum);
123  _dimensions.swap(other._dimensions);
124  }
125 
127  Box2I& operator=(Box2I const&) = default;
128  Box2I& operator=(Box2I&&) = default;
129 
136  Point2I const getMin() const { return _minimum; }
137  int getMinX() const { return _minimum.getX(); }
138  int getMinY() const { return _minimum.getY(); }
139 
140  Point2I const getMax() const { return _minimum + _dimensions - Extent2I(1); }
141  int getMaxX() const { return _minimum.getX() + _dimensions.getX() - 1; }
142  int getMaxY() const { return _minimum.getY() + _dimensions.getY() - 1; }
144 
151  Point2I const getBegin() const { return _minimum; }
152  int getBeginX() const { return _minimum.getX(); }
153  int getBeginY() const { return _minimum.getY(); }
154 
155  Point2I const getEnd() const { return _minimum + _dimensions; }
156  int getEndX() const { return _minimum.getX() + _dimensions.getX(); }
157  int getEndY() const { return _minimum.getY() + _dimensions.getY(); }
159 
166  Extent2I const getDimensions() const { return _dimensions; }
167  int getWidth() const { return _dimensions.getX(); }
168  int getHeight() const { return _dimensions.getY(); }
169  int getArea() const { return getWidth() * getHeight(); }
171 
173  ndarray::View<boost::fusion::vector2<ndarray::index::Range, ndarray::index::Range> > getSlices() const;
174 
176  bool isEmpty() const { return _dimensions.getX() == 0 && _dimensions.getY() == 0; }
177 
179  bool contains(Point2I const& point) const;
180 
186  bool contains(Box2I const& other) const;
187 
193  bool overlaps(Box2I const& other) const;
194 
201  void grow(int buffer) { grow(Extent2I(buffer)); }
202 
209  void grow(Extent2I const& buffer);
210 
212  void shift(Extent2I const& offset);
213 
215  void flipLR(int xExtent);
216 
218  void flipTB(int yExtent);
219 
221  void include(Point2I const& point);
222 
224  void include(Box2I const& other);
225 
227  void clip(Box2I const& other);
228 
234  bool operator==(Box2I const& other) const;
235 
241  bool operator!=(Box2I const& other) const;
242 
250 
252  return (boost::format("Box2I(%s,%s)") % _minimum.toString() % _dimensions.toString()).str();
253  }
254 
255 private:
256  Point2I _minimum;
257  Extent2I _dimensions;
258 };
259 
279 class Box2D {
280 public:
281  typedef Point2D Point;
282  typedef Extent2D Extent;
283  typedef double Element;
284 
289  static double const EPSILON;
290 
292  static double const INVALID;
293 
295  Box2D();
296 
307  Box2D(Point2D const& minimum, Point2D const& maximum, bool invert = true);
308 
319  Box2D(Point2D const& corner, Extent2D const& dimensions, bool invert = true);
320 
330  explicit Box2D(Box2I const& other);
331 
333  Box2D(Box2D const&) = default;
334  Box2D(Box2D&&) = default;
335 
336  ~Box2D() = default;
337 
348  static Box2D makeCenteredBox(Point2D const& center, Extent const& size);
349 
350  void swap(Box2D& other) {
351  _minimum.swap(other._minimum);
352  _maximum.swap(other._maximum);
353  }
354 
356  Box2D& operator=(Box2D const&) = default;
357  Box2D& operator=(Box2D&&) = default;
358 
365  Point2D const getMin() const { return _minimum; }
366  double getMinX() const { return _minimum.getX(); }
367  double getMinY() const { return _minimum.getY(); }
368 
369  Point2D const getMax() const { return _maximum; }
370  double getMaxX() const { return _maximum.getX(); }
371  double getMaxY() const { return _maximum.getY(); }
373 
380  Extent2D const getDimensions() const { return isEmpty() ? Extent2D(0.0) : _maximum - _minimum; }
381  double getWidth() const { return isEmpty() ? 0 : _maximum.getX() - _minimum.getX(); }
382  double getHeight() const { return isEmpty() ? 0 : _maximum.getY() - _minimum.getY(); }
383  double getArea() const {
384  Extent2D dim(getDimensions());
385  return dim.getX() * dim.getY();
386  }
388 
395  Point2D const getCenter() const { return Point2D((_minimum.asEigen() + _maximum.asEigen()) * 0.5); }
396  double getCenterX() const { return (_minimum.getX() + _maximum.getX()) * 0.5; }
397  double getCenterY() const { return (_minimum.getY() + _maximum.getY()) * 0.5; }
399 
401  bool isEmpty() const { return _minimum.getX() != _minimum.getX(); }
402 
404  bool contains(Point2D const& point) const;
405 
411  bool contains(Box2D const& other) const;
412 
418  bool overlaps(Box2D const& other) const;
419 
426  void grow(double buffer) { grow(Extent2D(buffer)); }
427 
434  void grow(Extent2D const& buffer);
435 
437  void shift(Extent2D const& offset);
438 
440  void flipLR(float xExtent);
441 
443  void flipTB(float yExtent);
444 
452  void include(Point2D const& point);
453 
455  void include(Box2D const& other);
456 
458  void clip(Box2D const& other);
459 
465  bool operator==(Box2D const& other) const;
466 
472  bool operator!=(Box2D const& other) const;
473 
481 
483  return (boost::format("Box2D(%s,%s)") % _minimum.toString() % _maximum.toString()).str();
484  }
485 
486 private:
487  void _tweakMax(int n) {
488  if (_maximum[n] < 0.0) {
489  _maximum[n] *= (1.0 - EPSILON);
490  } else if (_maximum[n] > 0.0) {
491  _maximum[n] *= (1.0 + EPSILON);
492  } else {
493  _maximum[n] = EPSILON;
494  }
495  }
496  Point2D _minimum;
497  Point2D _maximum;
498 };
499 
500 typedef Box2D BoxD;
501 typedef Box2I BoxI;
502 
504 
506 
507 } // namespace geom
508 } // namespace lsst
509 
510 #endif
Box2I()
Construct an empty box.
Definition: Box.h:63
Point2D Point
Definition: Box.h:281
double getCenterY() const
Definition: Box.h:397
Extent2I const getDimensions() const
Definition: Box.h:166
std::string toString() const
Definition: Box.h:482
int getEndY() const
Definition: Box.h:157
void flipLR(int xExtent)
Flip a bounding box about the y-axis given a parent box of extent (xExtent).
Definition: Box.cc:134
void swap(Box2D &other)
Definition: Box.h:350
int getMaxY() const
Definition: Box.h:142
bool isEmpty() const
Return true if the box contains no points.
Definition: Box.h:176
void swap(Extent &other)
Definition: Extent.h:223
Relationship invert(Relationship r)
A floating-point coordinate rectangle geometry.
Definition: Box.h:279
Point2I const getMin() const
Definition: Box.h:136
Point2I const getEnd() const
Definition: Box.h:155
double getHeight() const
Definition: Box.h:382
A coordinate class intended to represent absolute positions.
Extent2D const getDimensions() const
Definition: Box.h:380
Point2D const getMin() const
Definition: Box.h:365
Box2D BoxD
Definition: Box.h:500
std::string toString() const
Definition: Point.h:127
double getMinX() const
Definition: Box.h:366
Box2I & operator=(Box2I const &)=default
Standard assignment operator.
double getWidth() const
Definition: Box.h:381
std::vector< Point2I > getCorners() const
Get the corner points.
Definition: Box.cc:217
Point2I const getMax() const
Definition: Box.h:140
int getEndX() const
Definition: Box.h:156
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:289
Point2D const getCenter() const
Definition: Box.h:395
STL class.
double getArea() const
Definition: Box.h:383
double getCenterX() const
Definition: Box.h:396
void swap(Box2I &other)
Definition: Box.h:121
double getMaxX() const
Definition: Box.h:370
int getHeight() const
Definition: Box.h:168
void grow(int buffer)
Increase the size of the box by the given buffer amount in all directions.
Definition: Box.h:201
int getMinX() const
Definition: Box.h:137
static double const INVALID
Value used to specify undefined coordinate values.
Definition: Box.h:292
int getArea() const
Definition: Box.h:169
Point2I Point
Definition: Box.h:56
void grow(double buffer)
Increase the size of the box by the given buffer amount in all directions.
Definition: Box.h:426
Point2D const getMax() const
Definition: Box.h:369
int getBeginY() const
Definition: Box.h:153
int getMinY() const
Definition: Box.h:138
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:167
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:108
bool isEmpty() const
Return true if the box contains no points.
Definition: Box.h:401
Extent2D Extent
Definition: Box.h:282
int getMaxX() const
Definition: Box.h:141
std::string toString() const
Definition: Box.h:251
void include(Point2I const &point)
Expand this to ensure that this->contains(point).
Definition: Box.cc:148
double Element
Definition: Box.h:283
void shift(Extent2I const &offset)
Shift the position of the box by the given offset.
Definition: Box.cc:129
bool overlaps(Box2I const &other) const
Return true if any points in other are also in this.
Definition: Box.cc:117
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:103
static Box2I makeCenteredBox(Point2D const &center, Extent const &size)
Create a box centered as closely as possible on a particular point.
Definition: Box.cc:95
Point2I const getBegin() const
Definition: Box.h:151
Extent< double, 2 > Extent2D
Definition: Extent.h:379
bool operator==(Box2I const &other) const
Compare two boxes for equality.
Definition: Box.cc:209
void clip(Box2I const &other)
Shrink this to ensure that other.contains(*this).
Definition: Box.cc:185
bool operator!=(Box2I const &other) const
Compare two boxes for equality.
Definition: Box.cc:213
An integer coordinate rectangle.
Definition: Box.h:54
STL class.
double getMaxY() const
Definition: Box.h:371
double getMinY() const
Definition: Box.h:367
void flipTB(int yExtent)
Flip a bounding box about the x-axis given a parent box of extent (yExtent).
Definition: Box.cc:141
int getBeginX() const
Definition: Box.h:152
Box2I BoxI
Definition: Box.h:501