LSSTApplications  16.0-11-g09ed895+2,16.0-11-g12e47bd,16.0-11-g9bb73b2+6,16.0-12-g5c924a4+6,16.0-14-g9a974b3+1,16.0-15-g1417920+1,16.0-15-gdd5ca33+1,16.0-16-gf0259e2,16.0-17-g31abd91+7,16.0-17-g7d7456e+7,16.0-17-ga3d2e9f+13,16.0-18-ga4d4bcb+1,16.0-18-gd06566c+1,16.0-2-g0febb12+21,16.0-2-g9d5294e+69,16.0-2-ga8830df+6,16.0-20-g21842373+7,16.0-24-g3eae5ec,16.0-28-gfc9ea6c+4,16.0-29-ge8801f9,16.0-3-ge00e371+34,16.0-4-g18f3627+13,16.0-4-g5f3a788+20,16.0-4-ga3eb747+10,16.0-4-gabf74b7+29,16.0-4-gb13d127+6,16.0-49-g42e581f7+6,16.0-5-g27fb78a+7,16.0-5-g6a53317+34,16.0-5-gb3f8a4b+87,16.0-6-g9321be7+4,16.0-6-gcbc7b31+42,16.0-6-gf49912c+29,16.0-7-gd2eeba5+51,16.0-71-ge89f8615e,16.0-8-g21fd5fe+29,16.0-8-g3a9f023+20,16.0-8-g4734f7a+1,16.0-8-g5858431+3,16.0-9-gf5c1f43+8,master-gd73dc1d098+1,w.2019.01
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 <memory>
32 
33 #include "lsst/base.h"
34 #include "lsst/pex/exceptions.h"
35 #include "lsst/geom/Box.h"
36 #include "lsst/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:
62 
66  explicit Polygon(Box const& box);
67 
68  Polygon(Polygon const&);
69  Polygon(Polygon&&);
70  Polygon& operator=(Polygon const&);
72 
73  ~Polygon() override;
74 
83  Polygon(Box const& box, TransformPoint2ToPoint2 const& transform);
84 
95 
97  explicit Polygon(std::vector<Point> const& vertices);
99 
101  void swap(Polygon& other) noexcept { std::swap(this->_impl, other._impl); }
102 
106  size_t getNumEdges() const;
107 
109  Box getBBox() const;
110 
111  Point calculateCenter() const;
112  double calculateArea() const;
113  double calculatePerimeter() const;
114 
121 
130 
135 
136  bool operator==(Polygon const& other) const;
137  bool operator!=(Polygon const& other) const { return !(*this == other); }
138 
140  std::size_t hash_value() const noexcept;
141 
143  bool contains(Point const& point) const;
144 
146  bool overlaps(Polygon const& other) const;
151  bool overlaps(Box const& box) const;
153 
161  std::shared_ptr<Polygon> intersectionSingle(Box const& box) const;
163 
169  std::vector<std::shared_ptr<Polygon>> intersection(Box const& box) const;
171 
173  std::shared_ptr<Polygon> unionSingle(Polygon const& other) const;
178  std::shared_ptr<Polygon> unionSingle(Box const& box) const;
180 
188  std::vector<std::shared_ptr<Polygon>> union_(Box const& box) const;
190 
196 
200  std::shared_ptr<Polygon> simplify(double const distance) const;
201 
203  std::vector<std::shared_ptr<Polygon>> operator&(Polygon const& rhs) const { return intersection(rhs); }
205  std::vector<std::shared_ptr<Polygon>> operator&(Box const& rhs) const { return intersection(rhs); }
206  std::vector<std::shared_ptr<Polygon>> operator|(Polygon const& rhs) const { return union_(rhs); }
207  std::vector<std::shared_ptr<Polygon>> operator|(Box const& rhs) const { return union_(rhs); }
209  std::vector<std::shared_ptr<Polygon>> operator^(Box const& rhs) const { return symDifference(rhs); }
211 
214 
223  ) const;
225  lsst::geom::AffineTransform const& transform
226  ) const;
228 
230  std::shared_ptr<Polygon> subSample(size_t num) const;
234  std::shared_ptr<Polygon> subSample(double maxLength) const;
236 
248  return createImage(lsst::geom::Box2I(lsst::geom::Point2I(0, 0), extent));
249  }
251 
253  bool isPersistable() const noexcept override { return true; }
255 
256 protected:
257  std::string getPersistenceName() const override;
258 
259  void write(OutputArchiveHandle& handle) const override;
260 
261 private:
263  struct Impl;
265  std::shared_ptr<Impl> _impl;
266  Polygon(std::shared_ptr<Impl> impl) : _impl(impl) {}
268 };
269 
272 } // namespace polygon
273 } // namespace geom
274 } // namespace afw
275 } // namespace lsst
276 
277 namespace std {
278 template <>
279 struct hash<lsst::afw::geom::polygon::Polygon> {
282  size_t operator()(argument_type const& obj) const noexcept { return obj.hash_value(); }
283 };
284 } // namespace std
285 
286 #endif
std::shared_ptr< Polygon > convexHull() const
Produce a polygon from the convex hull.
Definition: Polygon.cc:400
bool operator==(Polygon const &other) const
Definition: Polygon.cc:341
std::vector< Point >::const_iterator begin() const
Iterator for vertices.
Definition: Polygon.cc:335
std::shared_ptr< afw::image::Image< float > > createImage(lsst::geom::Extent2I const &extent) const
Create image of polygon.
Definition: Polygon.h:247
std::vector< std::shared_ptr< Polygon > > intersection(Polygon const &other) const
Returns the intersection of two polygons.
Definition: Polygon.cc:366
bool operator!=(Polygon const &other) const
Definition: Polygon.h:137
Polygon & operator=(Polygon const &)
lsst::geom::Point2D Point
Definition: Polygon.h:61
std::vector< std::shared_ptr< Polygon > > operator^(Polygon const &rhs) const
Operators for syntactic sugar.
Definition: Polygon.h:208
bool overlaps(Polygon const &other) const
Returns whether the polygons overlap each other.
Definition: Polygon.cc:354
A floating-point coordinate rectangle geometry.
Definition: Box.h:294
An object passed to Persistable::write to allow it to persist itself.
Low-level polynomials (including special polynomials) in C++.
Definition: Basis1d.h:26
An affine coordinate transformation consisting of a linear transformation and an offset.
T swap(T... args)
Polygon(Box const &box)
Construct a rectangular Polygon whose vertices are the corners of a box.
Definition: Polygon.cc:284
void swap(Polygon &other) noexcept
Swap two polygons.
Definition: Polygon.h:101
STL namespace.
afw::table::PointKey< double > vertices
Definition: Polygon.cc:528
std::vector< std::shared_ptr< Polygon > > symDifference(Polygon const &other) const
Return the symmetric difference of two polygons.
Definition: Polygon.cc:386
STL class.
A base class for objects that can be persisted via afw::table::io Archive classes.
Definition: Persistable.h:74
bool isPersistable() const noexcept override
Whether Polygon is persistable which is always true.
Definition: Polygon.h:254
std::shared_ptr< Polygon > unionSingle(Polygon const &other) const
Returns the union of two polygons.
Definition: Polygon.cc:374
std::vector< std::shared_ptr< Polygon > > operator|(Polygon const &rhs) const
Operators for syntactic sugar.
Definition: Polygon.h:206
Box getBBox() const
Return bounding box.
Definition: Polygon.cc:310
A base class for image defects.
Definition: cameraGeom.dox:3
std::vector< std::pair< Point, Point > > getEdges() const
Get vector of edges.
Definition: Polygon.cc:322
std::vector< std::shared_ptr< Polygon > > operator|(Box const &rhs) const
Operators for syntactic sugar.
Definition: Polygon.h:207
std::vector< std::shared_ptr< Polygon > > union_(Polygon const &other) const
Returns the union of two polygons.
Definition: Polygon.cc:380
Basic LSST definitions.
void write(OutputArchiveHandle &handle) const override
Whether Polygon is persistable which is always true.
Definition: Polygon.cc:580
size_t operator()(argument_type const &obj) const noexcept
Definition: Polygon.h:282
std::vector< std::shared_ptr< Polygon > > operator^(Box const &rhs) const
Operators for syntactic sugar.
Definition: Polygon.h:209
table::Box2IKey bbox
Definition: Detector.cc:166
An exception that indicates the single-polygon assumption has been violated.
Definition: Polygon.h:52
std::vector< std::shared_ptr< Polygon > > operator &(Polygon const &rhs) const
Operators for syntactic sugar.
Definition: Polygon.h:204
std::shared_ptr< Polygon > subSample(size_t num) const
Sub-sample each edge.
Definition: Polygon.cc:421
std::vector< Point > getVertices() const
Get vector of vertices.
Definition: Polygon.cc:333
STL class.
std::size_t hash_value() const noexcept
Return a hash of this object.
Definition: Polygon.cc:345
size_t getNumEdges() const
Return number of edges.
Definition: Polygon.cc:305
std::shared_ptr< Polygon > simplify(double const distance) const
Return a simplified polygon.
Definition: Polygon.cc:394
std::shared_ptr< Polygon > intersectionSingle(Polygon const &other) const
Returns the intersection of two polygons.
Definition: Polygon.cc:358
std::ostream & operator<<(std::ostream &os, Polygon const &poly)
Stream polygon.
Definition: Polygon.cc:172
std::vector< Point >::const_iterator end() const
Iterator for vertices.
Definition: Polygon.cc:337
ItemVariant const * other
Definition: Schema.cc:56
Cartesian polygons.
Definition: Polygon.h:58
std::string getPersistenceName() const override
Whether Polygon is persistable which is always true.
Definition: Polygon.cc:578
#define LSST_EXCEPTION_TYPE(t, b, c)
Macro used to define new types of exceptions without additional data.
Definition: Exception.h:69
A CRTP facade class for subclasses of Persistable.
Definition: Persistable.h:176
bool contains(Point const &point) const
Returns whether the polygon contains the point.
Definition: Polygon.cc:352
An integer coordinate rectangle.
Definition: Box.h:54
STL class.
std::shared_ptr< afw::image::Image< float > > createImage(lsst::geom::Box2I const &bbox) const
Create image of polygon.
Definition: Polygon.cc:443
std::shared_ptr< Polygon > transform(TransformPoint2ToPoint2 const &transform) const
Transform the polygon.
Definition: Polygon.cc:406
std::ostream * os
Definition: Schema.cc:746
Transform LSST spatial data, such as lsst::geom::Point2D and lsst::geom::SpherePoint, using an AST mapping.
Definition: Transform.h:67
Reports errors that are due to events beyond the control of the program.
Definition: Runtime.h:104
double calculatePerimeter() const
Definition: Polygon.cc:320