LSSTApplications  10.0+286,10.0+36,10.0+46,10.0-2-g4f67435,10.1+152,10.1+37,11.0,11.0+1,11.0-1-g47edd16,11.0-1-g60db491,11.0-1-g7418c06,11.0-2-g04d2804,11.0-2-g68503cd,11.0-2-g818369d,11.0-2-gb8b8ce7
LSSTDataManagementBasePackage
Polygon.h
Go to the documentation of this file.
1 // -*- LSST-C++ -*-
2 
3 /*
4  * LSST Data Management System
5  * Copyright 2008-2014 LSST Corporation.
6  *
7  * This product includes software developed by the
8  * LSST Project (http://www.lsst.org/).
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the LSST License Statement and
21  * the GNU General Public License along with this program. If not,
22  * see <http://www.lsstcorp.org/LegalNotices/>.
23  */
24 
25 #if !defined(LSST_AFW_GEOM_POLYGON_POLYGON_H)
26 #define LSST_AFW_GEOM_POLYGON_POLYGON_H
27 
28 #include <vector>
29 #include <utility> // for std::pair
30 
31 #include "boost/make_shared.hpp"
32 
33 #include "lsst/base.h"
34 #include "lsst/pex/exceptions.h"
35 #include "lsst/afw/geom/Box.h"
36 #include "lsst/afw/geom/Point.h"
39 #include "lsst/afw/image/Image.h"
41 
42 namespace lsst { namespace afw { namespace geom { namespace polygon {
43 
48 LSST_EXCEPTION_TYPE(SinglePolygonException, lsst::pex::exceptions::RuntimeError,
49  lsst::afw::geom::polygon::SinglePolygonException);
50 
54 
56 public:
57  typedef Box2D Box;
58  typedef Point2D Point;
59 
61  explicit Polygon(Box const& box);
63  Polygon(Box const& box,
65  );
66  Polygon(Box const& box,
68  );
69  explicit Polygon(std::vector<Point> const& vertices);
71 
72 
74  void swap(Polygon& other) {
75  std::swap(this->_impl, other._impl);
76  }
77 
81  size_t getNumEdges() const;
82 
84  Box getBBox() const;
85 
86  Point calculateCenter() const;
87  double calculateArea() const;
88  double calculatePerimeter() const;
89 
95  std::vector<Point> getVertices() const;
96 
98  std::vector<Point>::const_iterator begin() const;
103  std::vector<Point>::const_iterator end() const;
105 
109  std::vector<std::pair<Point, Point> > getEdges() const;
110 
111  bool operator==(Polygon const& other) const;
112  bool operator!=(Polygon const& other) const { return !(*this == other); }
113 
115  bool contains(Point const& point) const;
116 
118  bool overlaps(Polygon const& other) const;
123  bool overlaps(Box const& box) const;
125 
127  PTR(Polygon) intersectionSingle(Polygon const& other) const;
133  PTR(Polygon) intersectionSingle(Box const& box) const;
135 
137  std::vector<PTR(Polygon)> intersection(Polygon const& other) const;
141  std::vector<PTR(Polygon)> intersection(Box const& box) const;
143 
145  PTR(Polygon) unionSingle(Polygon const& other) const;
150  PTR(Polygon) unionSingle(Box const& box) const;
152 
154  std::vector<PTR(Polygon)> union_(Polygon const& other) const;
160  std::vector<PTR(Polygon)> union_(Box const& box) const;
162 
164  std::vector<PTR(Polygon)> symDifference(Polygon const& other) const;
166  std::vector<PTR(Polygon)> symDifference(Box const& box) const;
168 
172  PTR(Polygon) simplify(double const distance) const;
173 
175  std::vector<PTR(Polygon)> operator&(Polygon const& rhs) const { return intersection(rhs); }
177  std::vector<PTR(Polygon)> operator&(Box const& rhs) const { return intersection(rhs); }
178  std::vector<PTR(Polygon)> operator|(Polygon const& rhs) const { return union_(rhs); }
179  std::vector<PTR(Polygon)> operator|(Box const& rhs) const { return union_(rhs); }
180  std::vector<PTR(Polygon)> operator^(Polygon const& rhs) const { return symDifference(rhs); }
181  std::vector<PTR(Polygon)> operator^(Box const& rhs) const { return symDifference(rhs); }
183 
185  PTR(Polygon) convexHull() const;
186 
195  ) const;
197  AffineTransform const& transform
198  ) const;
200 
202  PTR(Polygon) subSample(size_t num) const;
206  PTR(Polygon) subSample(double maxLength) const;
208 
210  PTR(afw::image::Image<float>) createImage(Box2I const& bbox) const;
219  PTR(afw::image::Image<float>) createImage(Extent2I const& extent) const {
220  return createImage(Box2I(Point2I(0, 0), extent));
221  }
223 
225  virtual bool isPersistable() const { return true; }
227 
228 protected:
229 
230  virtual std::string getPersistenceName() const;
231 
232  virtual void write(OutputArchiveHandle & handle) const;
233 
234 private:
236  struct Impl;
238  PTR(Impl) _impl;
239  Polygon(PTR(Impl) impl) : _impl(impl) {}
241 };
242 
244 std::ostream& operator<<(std::ostream& os, Polygon const& poly);
245 
246 }}}} // namespace lsst::afw::geom::polygon
247 
248 #endif
boost::shared_ptr< afw::image::Image< float > > createImage(Box2I const &bbox) const
A coordinate class intended to represent absolute positions.
boost::shared_ptr< Polygon > transform(boost::shared_ptr< XYTransform const > const &transform) const
std::vector< boost::shared_ptr< Polygon > > operator^(Polygon const &rhs) const
Operators for syntactic sugar.
Definition: Polygon.h:180
void swap(ImageBase< PixelT > &a, ImageBase< PixelT > &b)
Definition: Image.cc:291
std::vector< std::pair< Point, Point > > getEdges() const
Include files required for standard LSST Exception handling.
bool operator!=(Polygon const &other) const
Definition: Polygon.h:112
std::vector< Point > getVertices() const
Polygon(Box const &box)
Constructors.
std::vector< boost::shared_ptr< Polygon > > operator|(Polygon const &rhs) const
Operators for syntactic sugar.
Definition: Polygon.h:178
#define PTR(...)
Definition: base.h:41
std::vector< boost::shared_ptr< Polygon > > intersection(Polygon const &other) const
boost::shared_ptr< Polygon > unionSingle(Polygon const &other) const
Box getBBox() const
Return bounding box.
Point< int, 2 > Point2I
Definition: Point.h:283
bool contains(Point const &point) const
Returns whether the polygon contains the point.
boost::shared_ptr< Polygon > convexHull() const
Produce a polygon from the convex hull.
An integer coordinate rectangle.
Definition: Box.h:53
table::Key< table::Array< Kernel::Pixel > > image
Definition: FixedKernel.cc:117
A base class for objects that can be persisted via afw::table::io Archive classes.
Definition: Persistable.h:74
std::vector< boost::shared_ptr< Polygon > > operator&(Box const &rhs) const
Operators for syntactic sugar.
Definition: Polygon.h:177
boost::shared_ptr< Polygon > subSample(size_t num) const
boost::shared_ptr< Polygon > intersectionSingle(Polygon const &other) const
An affine coordinate transformation consisting of a linear transformation and an offset.
std::ostream & operator<<(std::ostream &os, Polygon const &poly)
Stream polygon.
boost::shared_ptr< Impl > _impl
Whether Polygon is persistable which is always true.
Definition: Polygon.h:237
bool operator==(Polygon const &other) const
Support for 2-D images.
virtual void write(OutputArchiveHandle &handle) const
Whether Polygon is persistable which is always true.
virtual bool isPersistable() const
Whether Polygon is persistable which is always true.
Definition: Polygon.h:226
std::vector< Point >::const_iterator begin() const
std::vector< Point >::const_iterator end() const
bool overlaps(Polygon const &other) const
LSST_EXCEPTION_TYPE(SinglePolygonException, lsst::pex::exceptions::RuntimeError, lsst::afw::geom::polygon::SinglePolygonException)
Virtual base class for 2D transforms.
Definition: XYTransform.h:48
boost::shared_ptr< Polygon > simplify(double const distance) const
virtual std::string getPersistenceName() const
Whether Polygon is persistable which is always true.
std::vector< boost::shared_ptr< Polygon > > symDifference(Polygon const &other) const
Return the symmetric difference of two polygons.
#define CONST_PTR(...)
Definition: base.h:47
io::OutputArchiveHandle OutputArchiveHandle
Definition: Persistable.h:114
Implementation of the Class MaskedImage.
std::vector< boost::shared_ptr< Polygon > > operator|(Box const &rhs) const
Operators for syntactic sugar.
Definition: Polygon.h:179
A floating-point coordinate rectangle geometry.
Definition: Box.h:271
void swap(Polygon &other)
Swap two polygons.
Definition: Polygon.h:74
A CRTP facade class for subclasses of Persistable.
Definition: Persistable.h:182
std::vector< boost::shared_ptr< Polygon > > union_(Polygon const &other) const
Class representing an invertible 2D transform.
std::vector< boost::shared_ptr< Polygon > > operator^(Box const &rhs) const
Operators for syntactic sugar.
Definition: Polygon.h:181