lsst.afw  14.0-25-g0e8fc950e+7
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 <memory>
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 {
43 namespace afw {
44 namespace geom {
45 namespace polygon {
46 
53 
57 
59 public:
60  typedef Box2D Box;
61  typedef Point2D Point;
62 
64  explicit Polygon(Box const& box);
66  Polygon(Box const& box,
68  );
69  Polygon(Box const& box,
71  );
72  explicit Polygon(std::vector<Point> const& vertices);
74 
76  void swap(Polygon& other) { std::swap(this->_impl, other._impl); }
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 
96 
105 
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 
133  std::shared_ptr<Polygon> intersectionSingle(Box const& box) const;
135 
141  std::vector<std::shared_ptr<Polygon>> intersection(Box const& box) const;
143 
145  std::shared_ptr<Polygon> unionSingle(Polygon const& other) const;
150  std::shared_ptr<Polygon> unionSingle(Box const& box) const;
152 
160  std::vector<std::shared_ptr<Polygon>> union_(Box const& box) const;
162 
168 
172  std::shared_ptr<Polygon> simplify(double const distance) const;
173 
175  std::vector<std::shared_ptr<Polygon>> operator&(Polygon const& rhs) const { return intersection(rhs); }
177  std::vector<std::shared_ptr<Polygon>> operator&(Box const& rhs) const { return intersection(rhs); }
178  std::vector<std::shared_ptr<Polygon>> operator|(Polygon const& rhs) const { return union_(rhs); }
179  std::vector<std::shared_ptr<Polygon>> operator|(Box const& rhs) const { return union_(rhs); }
181  std::vector<std::shared_ptr<Polygon>> operator^(Box const& rhs) const { return symDifference(rhs); }
183 
186 
195  ) const;
197  AffineTransform const& transform
198  ) const;
200 
202  std::shared_ptr<Polygon> subSample(size_t num) const;
206  std::shared_ptr<Polygon> subSample(double maxLength) const;
208 
220  return createImage(Box2I(Point2I(0, 0), extent));
221  }
223 
225  virtual bool isPersistable() const { return true; }
227 
228 protected:
229  virtual std::string getPersistenceName() const;
230 
231  virtual void write(OutputArchiveHandle& handle) const;
232 
233 private:
235  struct Impl;
237  std::shared_ptr<Impl> _impl;
238  Polygon(std::shared_ptr<Impl> impl) : _impl(impl) {}
240 };
241 
243 std::ostream& operator<<(std::ostream& os, Polygon const& poly);
244 }
245 }
246 }
247 } // namespace lsst::afw::geom::polygon
248 
249 #endif
bool operator==(Polygon const &other) const
virtual bool isPersistable() const
Whether Polygon is persistable which is always true.
Definition: Polygon.h:226
bool operator!=(Polygon const &other) const
Definition: Polygon.h:112
std::vector< std::shared_ptr< Polygon > > operator^(Polygon const &rhs) const
Operators for syntactic sugar.
Definition: Polygon.h:180
bool overlaps(Polygon const &other) const
Returns whether the polygons overlap each other.
An object passed to Persistable::write to allow it to persist itself.
Box getBBox() const
Return bounding box.
T swap(T... args)
std::shared_ptr< afw::image::Image< float > > createImage(Box2I const &bbox) const
Create image of polygon.
std::vector< std::shared_ptr< Polygon > > symDifference(Polygon const &other) const
Return the symmetric difference of two polygons.
Polygon(Box const &box)
Constructors.
std::vector< Point >::const_iterator begin() const
Iterator for vertices.
std::vector< std::pair< Point, Point > > getEdges() const
Get vector of edges.
An integer coordinate rectangle.
Definition: Box.h:55
std::shared_ptr< Polygon > subSample(size_t num) const
Sub-sample each edge.
STL class.
A base class for objects that can be persisted via afw::table::io Archive classes.
Definition: Persistable.h:73
std::vector< std::shared_ptr< Polygon > > operator|(Polygon const &rhs) const
Operators for syntactic sugar.
Definition: Polygon.h:178
std::shared_ptr< Polygon > convexHull() const
Produce a polygon from the convex hull.
std::shared_ptr< Polygon > unionSingle(Polygon const &other) const
Returns the union of two polygons.
A base class for image defects.
std::vector< std::shared_ptr< Polygon > > intersection(Polygon const &other) const
Returns the intersection of two polygons.
std::vector< std::shared_ptr< Polygon > > union_(Polygon const &other) const
Returns the union of two polygons.
std::vector< std::shared_ptr< Polygon > > operator|(Box const &rhs) const
Operators for syntactic sugar.
Definition: Polygon.h:179
An affine coordinate transformation consisting of a linear transformation and an offset.
std::ostream & operator<<(std::ostream &os, Polygon const &poly)
Stream polygon.
std::vector< Point >::const_iterator end() const
Iterator for vertices.
virtual std::string getPersistenceName() const
Whether Polygon is persistable which is always true.
std::shared_ptr< Polygon > simplify(double const distance) const
Return a simplified polygon.
std::vector< std::shared_ptr< Polygon > > operator^(Box const &rhs) const
Operators for syntactic sugar.
Definition: Polygon.h:181
An exception that indicates the single-polygon assumption has been violated.
Definition: Polygon.h:52
std::shared_ptr< Polygon > intersectionSingle(Polygon const &other) const
Returns the intersection of two polygons.
std::vector< std::shared_ptr< Polygon > > operator &(Polygon const &rhs) const
Operators for syntactic sugar.
Definition: Polygon.h:176
STL class.
size_t getNumEdges() const
Return number of edges.
std::shared_ptr< Polygon > transform(std::shared_ptr< XYTransform const > const &transform) const
Transform the polygon.
std::shared_ptr< afw::image::Image< float > > createImage(Extent2I const &extent) const
Create image of polygon.
Definition: Polygon.h:219
Cartesian polygons.
Definition: Polygon.h:58
std::vector< Point > getVertices() const
Get vector of vertices.
Point< int, 2 > Point2I
Definition: Point.h:270
A floating-point coordinate rectangle geometry.
Definition: Box.h:266
void swap(Polygon &other)
Swap two polygons.
Definition: Polygon.h:76
#define LSST_EXCEPTION_TYPE(t, b, c)
A CRTP facade class for subclasses of Persistable.
Definition: Persistable.h:173
bool contains(Point const &point) const
Returns whether the polygon contains the point.
STL class.
virtual void write(OutputArchiveHandle &handle) const
Whether Polygon is persistable which is always true.