LSSTApplications  20.0.0
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"
42 
43 namespace lsst {
44 namespace afw {
45 namespace geom {
46 namespace polygon {
47 
54 
58 
60 public:
63 
67  explicit Polygon(Box const& box);
68 
69  Polygon(Polygon const&);
73 
74  ~Polygon() override;
75 
84  Polygon(Box const& box, TransformPoint2ToPoint2 const& transform);
85 
96 
98  explicit Polygon(std::vector<Point> const& vertices);
100 
102  void swap(Polygon& other) noexcept { std::swap(this->_impl, other._impl); }
103 
107  size_t getNumEdges() const;
108 
110  Box getBBox() const;
111 
112  Point calculateCenter() const;
113  double calculateArea() const;
114  double calculatePerimeter() const;
115 
122 
131 
136 
137  bool operator==(Polygon const& other) const;
138  bool operator!=(Polygon const& other) const { return !(*this == other); }
139 
141  std::size_t hash_value() const noexcept override;
142 
144  bool contains(Point const& point) const;
145 
147  bool overlaps(Polygon const& other) const;
152  bool overlaps(Box const& box) const;
154 
156  std::shared_ptr<Polygon> intersectionSingle(Polygon const& other) const;
162  std::shared_ptr<Polygon> intersectionSingle(Box const& box) const;
164 
166  std::vector<std::shared_ptr<Polygon>> intersection(Polygon const& other) const;
170  std::vector<std::shared_ptr<Polygon>> intersection(Box const& box) const;
172 
174  std::shared_ptr<Polygon> unionSingle(Polygon const& other) const;
179  std::shared_ptr<Polygon> unionSingle(Box const& box) const;
181 
183  std::vector<std::shared_ptr<Polygon>> union_(Polygon const& other) const;
189  std::vector<std::shared_ptr<Polygon>> union_(Box const& box) const;
191 
193  std::vector<std::shared_ptr<Polygon>> symDifference(Polygon const& other) const;
195  std::vector<std::shared_ptr<Polygon>> symDifference(Box const& box) const;
197 
201  std::shared_ptr<Polygon> simplify(double const distance) const;
202 
204  std::vector<std::shared_ptr<Polygon>> operator&(Polygon const& rhs) const { return intersection(rhs); }
207  std::vector<std::shared_ptr<Polygon>> operator|(Polygon const& rhs) const { return union_(rhs); }
208  std::vector<std::shared_ptr<Polygon>> operator|(Box const& rhs) const { return union_(rhs); }
212 
215 
224  ) const;
227  ) const;
229 
231  std::shared_ptr<Polygon> subSample(size_t num) const;
235  std::shared_ptr<Polygon> subSample(double maxLength) const;
237 
249  return createImage(lsst::geom::Box2I(lsst::geom::Point2I(0, 0), extent));
250  }
252 
254  bool isPersistable() const noexcept override { return true; }
255 
258 
260  std::string toString() const override;
261 
267  bool equals(typehandling::Storable const& other) const noexcept override;
268 
269 protected:
270  std::string getPersistenceName() const override;
271 
272  void write(OutputArchiveHandle& handle) const override;
273 
274 private:
276  struct Impl;
278  std::shared_ptr<Impl> _impl;
279  Polygon(std::shared_ptr<Impl> impl) : _impl(impl) {}
281 };
282 
284 std::ostream& operator<<(std::ostream& os, Polygon const& poly);
285 } // namespace polygon
286 } // namespace geom
287 } // namespace afw
288 } // namespace lsst
289 
290 namespace std {
291 template <>
292 struct hash<lsst::afw::geom::polygon::Polygon> {
295  size_t operator()(argument_type const& obj) const noexcept { return obj.hash_value(); }
296 };
297 } // namespace std
298 
299 #endif
lsst::afw::geom::polygon::Polygon::transform
std::shared_ptr< Polygon > transform(TransformPoint2ToPoint2 const &transform) const
Transform the polygon.
Definition: Polygon.cc:406
lsst::afw::geom::polygon::Polygon::Box
lsst::geom::Box2D Box
Definition: Polygon.h:61
Box.h
lsst::afw::geom::polygon::Polygon::Polygon
Polygon(Box const &box)
Construct a rectangular Polygon whose vertices are the corners of a box.
Definition: Polygon.cc:284
std::string
STL class.
std::shared_ptr
STL class.
lsst::afw::geom::polygon::Polygon::cloneStorable
std::shared_ptr< typehandling::Storable > cloneStorable() const override
Create a new Polygon that is a copy of this one.
Definition: Polygon.cc:591
vertices
afw::table::PointKey< double > vertices
Definition: Polygon.cc:528
lsst::afw::geom::Transform
Transform LSST spatial data, such as lsst::geom::Point2D and lsst::geom::SpherePoint,...
Definition: Transform.h:68
lsst::afw::geom::polygon::Polygon::operator|
std::vector< std::shared_ptr< Polygon > > operator|(Box const &rhs) const
Definition: Polygon.h:208
lsst::afw::geom::polygon::Polygon::calculatePerimeter
double calculatePerimeter() const
Definition: Polygon.cc:320
MaskedImage.h
lsst::afw::geom::polygon::Polygon::end
std::vector< Point >::const_iterator end() const
Definition: Polygon.cc:337
std::vector
STL class.
lsst::afw::geom::polygon::Polygon::calculateArea
double calculateArea() const
Definition: Polygon.cc:318
AffineTransform.h
lsst::afw::geom::polygon::Polygon::createImage
std::shared_ptr< afw::image::Image< float > > createImage(lsst::geom::Extent2I const &extent) const
Definition: Polygon.h:248
lsst::afw
Definition: imageAlgorithm.dox:1
lsst::afw::typehandling::Storable
Interface supporting iteration over heterogenous containers.
Definition: Storable.h:58
Transform.h
Image.h
lsst::afw::geom::polygon::Polygon::Polygon
Polygon(Polygon &&)
lsst::afw::geom::polygon::Polygon::subSample
std::shared_ptr< Polygon > subSample(size_t num) const
Sub-sample each edge.
Definition: Polygon.cc:421
lsst::afw::geom::polygon::Polygon::overlaps
bool overlaps(Polygon const &other) const
Returns whether the polygons overlap each other.
Definition: Polygon.cc:354
lsst::geom::AffineTransform
An affine coordinate transformation consisting of a linear transformation and an offset.
Definition: AffineTransform.h:75
lsst::afw::geom::polygon::Polygon::operator|
std::vector< std::shared_ptr< Polygon > > operator|(Polygon const &rhs) const
Definition: Polygon.h:207
lsst::afw::geom::polygon::Polygon::unionSingle
std::shared_ptr< Polygon > unionSingle(Polygon const &other) const
Returns the union of two polygons.
Definition: Polygon.cc:374
lsst::afw::geom::polygon::Polygon::equals
bool equals(typehandling::Storable const &other) const noexcept override
Compare this object to another Storable.
Definition: Polygon.cc:601
lsst::afw::geom::polygon::SinglePolygonException
An exception that indicates the single-polygon assumption has been violated.
Definition: Polygon.h:53
lsst::afw::geom::polygon::Polygon::hash_value
std::size_t hash_value() const noexcept override
Return a hash of this object.
Definition: Polygon.cc:345
lsst::afw::geom::polygon::Polygon::operator=
Polygon & operator=(Polygon const &)
lsst::afw::geom::polygon::Polygon
Cartesian polygons.
Definition: Polygon.h:59
lsst::afw::geom::polygon::Polygon::~Polygon
~Polygon() override
std::ostream
STL class.
lsst::afw::geom::polygon::Polygon::intersection
std::vector< std::shared_ptr< Polygon > > intersection(Polygon const &other) const
Returns the intersection of two polygons.
Definition: Polygon.cc:366
lsst::afw::geom::polygon::Polygon::isPersistable
bool isPersistable() const noexcept override
Whether Polygon is persistable which is always true.
Definition: Polygon.h:254
lsst::afw::geom::polygon::Polygon::createImage
std::shared_ptr< afw::image::Image< float > > createImage(lsst::geom::Box2I const &bbox) const
Create image of polygon.
Definition: Polygon.cc:443
lsst::afw::geom::polygon::Polygon::union_
std::vector< std::shared_ptr< Polygon > > union_(Polygon const &other) const
Returns the union of two polygons.
Definition: Polygon.cc:380
other
ItemVariant const * other
Definition: Schema.cc:56
lsst::afw::geom::polygon::Polygon::getBBox
Box getBBox() const
Return bounding box.
Definition: Polygon.cc:310
lsst::afw::geom::polygon::operator<<
std::ostream & operator<<(std::ostream &os, Polygon const &poly)
Stream polygon.
Definition: Polygon.cc:172
lsst::afw::geom::polygon::Polygon::operator=
Polygon & operator=(Polygon &&)
lsst::afw::geom::polygon::Polygon::toString
std::string toString() const override
Create a string representation of this object.
Definition: Polygon.cc:595
lsst::afw::geom::polygon::Polygon::contains
bool contains(Point const &point) const
Returns whether the polygon contains the point.
Definition: Polygon.cc:352
lsst::afw::geom::polygon::Polygon::calculateCenter
Point calculateCenter() const
Definition: Polygon.cc:314
Storable.h
lsst::afw::table._match.distance
distance
Definition: _match.py:80
lsst::afw::geom::polygon::Polygon::getVertices
std::vector< Point > getVertices() const
Get vector of vertices.
Definition: Polygon.cc:333
base.h
lsst
A base class for image defects.
Definition: imageAlgorithm.dox:1
std::swap
T swap(T... args)
lsst::geom
Definition: geomOperators.dox:4
os
std::ostream * os
Definition: Schema.cc:746
lsst::afw::geom::polygon::Polygon::simplify
std::shared_ptr< Polygon > simplify(double const distance) const
Return a simplified polygon.
Definition: Polygon.cc:394
lsst::geom::polynomials
Definition: Basis1d.h:26
lsst::afw::geom::polygon::Polygon::getEdges
std::vector< std::pair< Point, Point > > getEdges() const
Get vector of edges.
Definition: Polygon.cc:322
std
STL namespace.
lsst::afw::geom::polygon::Polygon::begin
std::vector< Point >::const_iterator begin() const
Iterator for vertices.
Definition: Polygon.cc:335
lsst::geom::Point
A coordinate class intended to represent absolute positions.
Definition: CoordinateBase.h:39
lsst::afw::geom::polygon::Polygon::operator^
std::vector< std::shared_ptr< Polygon > > operator^(Box const &rhs) const
Definition: Polygon.h:210
lsst::afw::geom::polygon::Polygon::swap
void swap(Polygon &other) noexcept
Swap two polygons.
Definition: Polygon.h:102
lsst::afw::table::io::PersistableFacade
A CRTP facade class for subclasses of Persistable.
Definition: Persistable.h:176
lsst::afw::geom::polygon::Polygon::write
void write(OutputArchiveHandle &handle) const override
Write the object to one or more catalogs.
Definition: Polygon.cc:578
lsst::geom::Box2I
An integer coordinate rectangle.
Definition: Box.h:55
lsst::afw::geom::polygon::Polygon::intersectionSingle
std::shared_ptr< Polygon > intersectionSingle(Polygon const &other) const
Returns the intersection of two polygons.
Definition: Polygon.cc:358
lsst::afw::geom::polygon::Polygon::getPersistenceName
std::string getPersistenceName() const override
Return the unique name used to persist this object and look up its factory.
Definition: Polygon.cc:576
lsst::afw::geom::polygon::Polygon::getNumEdges
size_t getNumEdges() const
Return number of edges.
Definition: Polygon.cc:305
std::size_t
Point.h
lsst::afw::geom::polygon::Polygon::convexHull
std::shared_ptr< Polygon > convexHull() const
Produce a polygon from the convex hull.
Definition: Polygon.cc:400
std::hash< lsst::afw::geom::polygon::Polygon >::operator()
size_t operator()(argument_type const &obj) const noexcept
Definition: Polygon.h:295
lsst::geom::Box2D
A floating-point coordinate rectangle geometry.
Definition: Box.h:413
lsst::afw::geom::polygon::Polygon::Point
lsst::geom::Point2D Point
Definition: Polygon.h:62
lsst::afw::geom::polygon::Polygon::Polygon
Polygon(Polygon const &)
lsst::afw::geom::polygon::Polygon::symDifference
std::vector< std::shared_ptr< Polygon > > symDifference(Polygon const &other) const
Return the symmetric difference of two polygons.
Definition: Polygon.cc:386
LSST_EXCEPTION_TYPE
#define LSST_EXCEPTION_TYPE(t, b, c)
Macro used to define new types of exceptions without additional data.
Definition: Exception.h:69
lsst::afw::geom::polygon::Polygon::operator^
std::vector< std::shared_ptr< Polygon > > operator^(Polygon const &rhs) const
Definition: Polygon.h:209
lsst::afw::geom::polygon::Polygon::operator!=
bool operator!=(Polygon const &other) const
Definition: Polygon.h:138
lsst::afw::table::io::Persistable::OutputArchiveHandle
io::OutputArchiveHandle OutputArchiveHandle
Definition: Persistable.h:108
lsst::geom::Extent< int, 2 >
lsst::afw::geom::polygon::Polygon::operator&
std::vector< std::shared_ptr< Polygon > > operator&(Box const &rhs) const
Definition: Polygon.h:206
exceptions.h
lsst::afw::geom::polygon::Polygon::operator==
bool operator==(Polygon const &other) const
Definition: Polygon.cc:341
bbox
AmpInfoBoxKey bbox
Definition: Amplifier.cc:117
lsst::pex::exceptions::RuntimeError
Reports errors that are due to events beyond the control of the program.
Definition: Runtime.h:104
std::hash