lsst.afw  15.0-25-gacd707fca
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 
63 
67  explicit Polygon(Box const& box);
68 
69  Polygon(Polygon const&);
70  Polygon(Polygon&&);
71  Polygon& operator=(Polygon const&);
73 
74  virtual ~Polygon();
75 
84  Polygon(Box const& box,
86  );
87 
97  Polygon(Box const& box,
99  );
100 
102  explicit Polygon(std::vector<Point> const& vertices);
104 
106  void swap(Polygon& other) { std::swap(this->_impl, other._impl); }
107 
111  size_t getNumEdges() const;
112 
114  Box getBBox() const;
115 
116  Point calculateCenter() const;
117  double calculateArea() const;
118  double calculatePerimeter() const;
119 
126 
135 
140 
141  bool operator==(Polygon const& other) const;
142  bool operator!=(Polygon const& other) const { return !(*this == other); }
143 
145  bool contains(Point const& point) const;
146 
148  bool overlaps(Polygon const& other) const;
153  bool overlaps(Box const& box) const;
155 
163  std::shared_ptr<Polygon> intersectionSingle(Box const& box) const;
165 
171  std::vector<std::shared_ptr<Polygon>> intersection(Box const& box) const;
173 
175  std::shared_ptr<Polygon> unionSingle(Polygon const& other) const;
180  std::shared_ptr<Polygon> unionSingle(Box const& box) const;
182 
190  std::vector<std::shared_ptr<Polygon>> union_(Box const& box) const;
192 
198 
202  std::shared_ptr<Polygon> simplify(double const distance) const;
203 
205  std::vector<std::shared_ptr<Polygon>> operator&(Polygon const& rhs) const { return intersection(rhs); }
207  std::vector<std::shared_ptr<Polygon>> operator&(Box const& rhs) const { return intersection(rhs); }
208  std::vector<std::shared_ptr<Polygon>> operator|(Polygon const& rhs) const { return union_(rhs); }
209  std::vector<std::shared_ptr<Polygon>> operator|(Box const& rhs) const { return union_(rhs); }
211  std::vector<std::shared_ptr<Polygon>> operator^(Box const& rhs) const { return symDifference(rhs); }
213 
216 
225  ) const;
227  AffineTransform const& transform
228  ) const;
230 
232  std::shared_ptr<Polygon> subSample(size_t num) const;
236  std::shared_ptr<Polygon> subSample(double maxLength) const;
238 
250  return createImage(Box2I(Point2I(0, 0), extent));
251  }
253 
255  virtual bool isPersistable() const { return true; }
257 
258 protected:
259  virtual std::string getPersistenceName() const;
260 
261  virtual void write(OutputArchiveHandle& handle) const;
262 
263 private:
265  struct Impl;
267  std::shared_ptr<Impl> _impl;
268  Polygon(std::shared_ptr<Impl> impl) : _impl(impl) {}
270 };
271 
274 }
275 }
276 }
277 } // namespace lsst::afw::geom::polygon
278 
279 #endif
std::shared_ptr< Polygon > convexHull() const
Produce a polygon from the convex hull.
Definition: Polygon.cc:388
bool operator==(Polygon const &other) const
Definition: Polygon.cc:336
virtual bool isPersistable() const
Whether Polygon is persistable which is always true.
Definition: Polygon.h:256
std::vector< Point >::const_iterator begin() const
Iterator for vertices.
Definition: Polygon.cc:330
std::vector< std::shared_ptr< Polygon > > intersection(Polygon const &other) const
Returns the intersection of two polygons.
Definition: Polygon.cc:354
bool operator!=(Polygon const &other) const
Definition: Polygon.h:142
Polygon & operator=(Polygon const &)
std::vector< std::shared_ptr< Polygon > > operator^(Polygon const &rhs) const
Operators for syntactic sugar.
Definition: Polygon.h:210
bool overlaps(Polygon const &other) const
Returns whether the polygons overlap each other.
Definition: Polygon.cc:342
virtual std::string getPersistenceName() const
Whether Polygon is persistable which is always true.
Definition: Polygon.cc:566
An object passed to Persistable::write to allow it to persist itself.
T swap(T... args)
Polygon(Box const &box)
Construct a rectangular Polygon whose vertices are the corners of a box.
Definition: Polygon.cc:279
ItemVariant const * other
Definition: Schema.cc:55
A coordinate class intended to represent absolute positions.
An integer coordinate rectangle.
Definition: Box.h:55
std::vector< std::shared_ptr< Polygon > > symDifference(Polygon const &other) const
Return the symmetric difference of two polygons.
Definition: Polygon.cc:374
STL class.
A base class for objects that can be persisted via afw::table::io Archive classes.
Definition: Persistable.h:74
std::shared_ptr< Polygon > unionSingle(Polygon const &other) const
Returns the union of two polygons.
Definition: Polygon.cc:362
std::vector< std::shared_ptr< Polygon > > operator|(Polygon const &rhs) const
Operators for syntactic sugar.
Definition: Polygon.h:208
Box getBBox() const
Return bounding box.
Definition: Polygon.cc:305
virtual void write(OutputArchiveHandle &handle) const
Whether Polygon is persistable which is always true.
Definition: Polygon.cc:568
A base class for image defects.
std::vector< std::pair< Point, Point > > getEdges() const
Get vector of edges.
Definition: Polygon.cc:317
std::vector< std::shared_ptr< Polygon > > operator|(Box const &rhs) const
Operators for syntactic sugar.
Definition: Polygon.h:209
An affine coordinate transformation consisting of a linear transformation and an offset.
std::vector< std::shared_ptr< Polygon > > union_(Polygon const &other) const
Returns the union of two polygons.
Definition: Polygon.cc:368
std::vector< std::shared_ptr< Polygon > > operator^(Box const &rhs) const
Operators for syntactic sugar.
Definition: Polygon.h:211
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:206
std::shared_ptr< Polygon > subSample(size_t num) const
Sub-sample each edge.
Definition: Polygon.cc:409
std::vector< Point > getVertices() const
Get vector of vertices.
Definition: Polygon.cc:328
STL class.
size_t getNumEdges() const
Return number of edges.
Definition: Polygon.cc:300
std::shared_ptr< Polygon > simplify(double const distance) const
Return a simplified polygon.
Definition: Polygon.cc:382
std::shared_ptr< Polygon > intersectionSingle(Polygon const &other) const
Returns the intersection of two polygons.
Definition: Polygon.cc:346
std::ostream & operator<<(std::ostream &os, Polygon const &poly)
Stream polygon.
Definition: Polygon.cc:167
std::vector< Point >::const_iterator end() const
Iterator for vertices.
Definition: Polygon.cc:332
table::Box2IKey bbox
std::shared_ptr< afw::image::Image< float > > createImage(Extent2I const &extent) const
Create image of polygon.
Definition: Polygon.h:249
Cartesian polygons.
Definition: Polygon.h:58
Point< int, 2 > Point2I
Definition: Point.h:301
A floating-point coordinate rectangle geometry.
Definition: Box.h:266
std::shared_ptr< afw::image::Image< float > > createImage(Box2I const &bbox) const
Create image of polygon.
Definition: Polygon.cc:431
void swap(Polygon &other)
Swap two polygons.
Definition: Polygon.h:106
std::ostream * os
Definition: Schema.cc:736
#define LSST_EXCEPTION_TYPE(t, b, c)
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:340
STL class.
std::shared_ptr< Polygon > transform(TransformPoint2ToPoint2 const &transform) const
Transform the polygon.
Definition: Polygon.cc:394
Transform LSST spatial data, such as Point2D and SpherePoint, using an AST mapping.
Definition: Transform.h:67
afw::table::PointKey< double > vertices
Definition: Polygon.cc:516
double calculatePerimeter() const
Definition: Polygon.cc:315