LSST Applications  21.0.0-172-gfb10e10a+18fedfabac,22.0.0+297cba6710,22.0.0+80564b0ff1,22.0.0+8d77f4f51a,22.0.0+a28f4c53b1,22.0.0+dcf3732eb2,22.0.1-1-g7d6de66+2a20fdde0d,22.0.1-1-g8e32f31+297cba6710,22.0.1-1-geca5380+7fa3b7d9b6,22.0.1-12-g44dc1dc+2a20fdde0d,22.0.1-15-g6a90155+515f58c32b,22.0.1-16-g9282f48+790f5f2caa,22.0.1-2-g92698f7+dcf3732eb2,22.0.1-2-ga9b0f51+7fa3b7d9b6,22.0.1-2-gd1925c9+bf4f0e694f,22.0.1-24-g1ad7a390+a9625a72a8,22.0.1-25-g5bf6245+3ad8ecd50b,22.0.1-25-gb120d7b+8b5510f75f,22.0.1-27-g97737f7+2a20fdde0d,22.0.1-32-gf62ce7b1+aa4237961e,22.0.1-4-g0b3f228+2a20fdde0d,22.0.1-4-g243d05b+871c1b8305,22.0.1-4-g3a563be+32dcf1063f,22.0.1-4-g44f2e3d+9e4ab0f4fa,22.0.1-42-gca6935d93+ba5e5ca3eb,22.0.1-5-g15c806e+85460ae5f3,22.0.1-5-g58711c4+611d128589,22.0.1-5-g75bb458+99c117b92f,22.0.1-6-g1c63a23+7fa3b7d9b6,22.0.1-6-g50866e6+84ff5a128b,22.0.1-6-g8d3140d+720564cf76,22.0.1-6-gd805d02+cc5644f571,22.0.1-8-ge5750ce+85460ae5f3,master-g6e05de7fdc+babf819c66,master-g99da0e417a+8d77f4f51a,w.2021.48
LSST Data Management Base Package
Classes | Public Types | Public Member Functions | Static Public Member Functions | Protected Types | Protected Member Functions | Static Protected Member Functions | Related Functions | List of all members
lsst::afw::geom::polygon::Polygon Class Referencefinal

Cartesian polygons. More...

#include <Polygon.h>

Inheritance diagram for lsst::afw::geom::polygon::Polygon:
lsst::afw::table::io::PersistableFacade< Polygon > lsst::afw::typehandling::Storable lsst::afw::table::io::Persistable

Classes

struct  Impl
 

Public Types

using Box = lsst::geom::Box2D
 
using Point = lsst::geom::Point2D
 

Public Member Functions

 Polygon (Box const &box)
 Construct a rectangular Polygon whose vertices are the corners of a box. More...
 
 Polygon (Polygon const &)
 
 Polygon (Polygon &&)
 
Polygonoperator= (Polygon const &)
 
Polygonoperator= (Polygon &&)
 
 ~Polygon () override
 
 Polygon (Box const &box, TransformPoint2ToPoint2 const &transform)
 Construct a 4-sided Polygon from a transformed box. More...
 
 Polygon (Box const &box, lsst::geom::AffineTransform const &transform)
 Construct a 4-sided Polygon from a transformed box. More...
 
 Polygon (std::vector< Point > const &vertices)
 Construct a Polygon from a list of vertices. More...
 
void swap (Polygon &other) noexcept
 Swap two polygons. More...
 
size_t getNumEdges () const
 Return number of edges. More...
 
Box getBBox () const
 Return bounding box. More...
 
Point calculateCenter () const
 
double calculateArea () const
 
double calculatePerimeter () const
 
std::vector< PointgetVertices () const
 Get vector of vertices. More...
 
std::vector< Point >::const_iterator begin () const
 Iterator for vertices. More...
 
std::vector< Point >::const_iterator end () const
 
std::vector< std::pair< Point, Point > > getEdges () const
 Get vector of edges. More...
 
bool operator== (Polygon const &other) const
 
bool operator!= (Polygon const &other) const
 
std::size_t hash_value () const noexcept override
 Return a hash of this object. More...
 
bool contains (Point const &point) const
 Returns whether the polygon contains the point. More...
 
bool overlaps (Polygon const &other) const
 Returns whether the polygons overlap each other. More...
 
bool overlaps (Box const &box) const
 
std::shared_ptr< PolygonintersectionSingle (Polygon const &other) const
 Returns the intersection of two polygons. More...
 
std::shared_ptr< PolygonintersectionSingle (Box const &box) const
 
std::vector< std::shared_ptr< Polygon > > intersection (Polygon const &other) const
 Returns the intersection of two polygons. More...
 
std::vector< std::shared_ptr< Polygon > > intersection (Box const &box) const
 
std::shared_ptr< PolygonunionSingle (Polygon const &other) const
 Returns the union of two polygons. More...
 
std::shared_ptr< PolygonunionSingle (Box const &box) const
 
std::vector< std::shared_ptr< Polygon > > union_ (Polygon const &other) const
 Returns the union of two polygons. More...
 
std::vector< std::shared_ptr< Polygon > > union_ (Box const &box) const
 
std::vector< std::shared_ptr< Polygon > > symDifference (Polygon const &other) const
 Return the symmetric difference of two polygons. More...
 
std::vector< std::shared_ptr< Polygon > > symDifference (Box const &box) const
 
std::shared_ptr< Polygonsimplify (double const distance) const
 Return a simplified polygon. More...
 
std::vector< std::shared_ptr< Polygon > > operator& (Polygon const &rhs) const
 Operators for syntactic sugar. More...
 
std::vector< std::shared_ptr< Polygon > > operator& (Box const &rhs) const
 
std::vector< std::shared_ptr< Polygon > > operator| (Polygon const &rhs) const
 
std::vector< std::shared_ptr< Polygon > > operator| (Box const &rhs) const
 
std::vector< std::shared_ptr< Polygon > > operator^ (Polygon const &rhs) const
 
std::vector< std::shared_ptr< Polygon > > operator^ (Box const &rhs) const
 
std::shared_ptr< PolygonconvexHull () const
 Produce a polygon from the convex hull. More...
 
std::shared_ptr< Polygontransform (TransformPoint2ToPoint2 const &transform) const
 Transform the polygon. More...
 
std::shared_ptr< Polygontransform (lsst::geom::AffineTransform const &transform) const
 
std::shared_ptr< PolygonsubSample (size_t num) const
 Sub-sample each edge. More...
 
std::shared_ptr< PolygonsubSample (double maxLength) const
 
std::shared_ptr< afw::image::Image< float > > createImage (lsst::geom::Box2I const &bbox) const
 Create image of polygon. More...
 
std::shared_ptr< afw::image::Image< float > > createImage (lsst::geom::Extent2I const &extent) const
 
bool isPersistable () const noexcept override
 Whether Polygon is persistable which is always true. More...
 
std::shared_ptr< typehandling::StorablecloneStorable () const override
 Create a new Polygon that is a copy of this one. More...
 
std::string toString () const override
 Create a string representation of this object. More...
 
bool equals (typehandling::Storable const &other) const noexcept override
 Compare this object to another Storable. More...
 
def __repr__ (self)
 
def __reduce__ (self)
 
def __iter__ (self)
 
def __getitem__ (self, i)
 
def __len__ (self)
 
def __contains__ (self, point)
 
def display (self, xy0=None, frame=1, ctype=None)
 
def plot (self, axes=None, **kwargs)
 
void writeFits (std::string const &fileName, std::string const &mode="w") const
 Write the object to a regular FITS file. More...
 
void writeFits (fits::MemFileManager &manager, std::string const &mode="w") const
 Write the object to a FITS image in memory. More...
 
void writeFits (fits::Fits &fitsfile) const
 Write the object to an already-open FITS object. More...
 

Static Public Member Functions

static std::shared_ptr< PolygonreadFits (fits::Fits &fitsfile)
 Read an object from an already open FITS object. More...
 
static std::shared_ptr< PolygonreadFits (std::string const &fileName, int hdu=fits::DEFAULT_HDU)
 Read an object from a regular FITS file. More...
 
static std::shared_ptr< PolygonreadFits (fits::MemFileManager &manager, int hdu=fits::DEFAULT_HDU)
 Read an object from a FITS file in memory. More...
 
static std::shared_ptr< PolygondynamicCast (std::shared_ptr< Persistable > const &ptr)
 Dynamically cast a shared_ptr. More...
 

Protected Types

using OutputArchiveHandle = io::OutputArchiveHandle
 

Protected Member Functions

std::string getPersistenceName () const override
 Return the unique name used to persist this object and look up its factory. More...
 
void write (OutputArchiveHandle &handle) const override
 Write the object to one or more catalogs. More...
 
virtual std::string getPythonModule () const
 Return the fully-qualified Python module that should be imported to guarantee that its factory is registered. More...
 

Static Protected Member Functions

template<class T >
static bool singleClassEquals (T const &lhs, Storable const &rhs)
 Test if a Storable is of a particular class and equal to another object. More...
 

Related Functions

(Note that these are not member functions.)

std::ostreamoperator<< (std::ostream &os, Storable const &storable)
 Output operator for Storable. More...
 

Detailed Description

Cartesian polygons.

Polygons are defined by a set of vertices

Definition at line 59 of file Polygon.h.

Member Typedef Documentation

◆ Box

Definition at line 61 of file Polygon.h.

◆ OutputArchiveHandle

using lsst::afw::table::io::Persistable::OutputArchiveHandle = io::OutputArchiveHandle
protectedinherited

Definition at line 108 of file Persistable.h.

◆ Point

Definition at line 62 of file Polygon.h.

Constructor & Destructor Documentation

◆ Polygon() [1/6]

lsst::afw::geom::polygon::Polygon::Polygon ( Polygon::Box const &  box)
explicit

Construct a rectangular Polygon whose vertices are the corners of a box.

Definition at line 288 of file Polygon.cc.

288 : _impl(new Polygon::Impl(box)) {}

◆ Polygon() [2/6]

lsst::afw::geom::polygon::Polygon::Polygon ( Polygon const &  )
default

◆ Polygon() [3/6]

lsst::afw::geom::polygon::Polygon::Polygon ( Polygon &&  )
default

◆ ~Polygon()

lsst::afw::geom::polygon::Polygon::~Polygon ( )
overridedefault

◆ Polygon() [4/6]

lsst::afw::geom::polygon::Polygon::Polygon ( Polygon::Box const &  box,
TransformPoint2ToPoint2 const &  transform 
)

Construct a 4-sided Polygon from a transformed box.

The resulting polygon has 4 vertices: transform.applyForward(bbox.getCorners())

Parameters
[in]boxInitial box
[in]transformCoordinate transform

Definition at line 292 of file Polygon.cc.

293  : _impl(new Polygon::Impl()) {
294  auto corners = transform.applyForward(boxToCorners(box));
295  boost::geometry::assign(_impl->poly, corners);
296  _impl->check();
297 }
std::shared_ptr< Polygon > transform(TransformPoint2ToPoint2 const &transform) const
Transform the polygon.
Definition: Polygon.cc:410

◆ Polygon() [5/6]

lsst::afw::geom::polygon::Polygon::Polygon ( Polygon::Box const &  box,
lsst::geom::AffineTransform const &  transform 
)

Construct a 4-sided Polygon from a transformed box.

The resulting polygon has 4 vertices: the corners of the box transformed by transform

Parameters
[in]boxInitial box
[in]transformCoordinate transform

Definition at line 299 of file Polygon.cc.

300  : _impl(new Polygon::Impl()) {
301  std::vector<LsstPoint> corners = boxToCorners(box);
302  for (auto & corner : corners) {
303  corner = transform(corner);
304  }
305  boost::geometry::assign(_impl->poly, corners);
306  _impl->check();
307 }

◆ Polygon() [6/6]

lsst::afw::geom::polygon::Polygon::Polygon ( std::vector< Point > const &  vertices)
explicit

Construct a Polygon from a list of vertices.

Definition at line 290 of file Polygon.cc.

290 : _impl(new Polygon::Impl(vertices)) {}
afw::table::PointKey< double > vertices
Definition: Polygon.cc:530

Member Function Documentation

◆ __contains__()

def lsst.afw.geom.polygon.Polygon.__contains__ (   self,
  point 
)
point in polygon?

Definition at line 47 of file polygon.py.

47  def __contains__(self, point):
48  """point in polygon?"""
49  return self.contains(point)
50 

◆ __getitem__()

def lsst.afw.geom.polygon.Polygon.__getitem__ (   self,
  i 
)

Definition at line 41 of file polygon.py.

41  def __getitem__(self, i):
42  return [pt for pt in self][i]
43 

◆ __iter__()

def lsst.afw.geom.polygon.Polygon.__iter__ (   self)
Iterator over vertices

Definition at line 36 of file polygon.py.

36  def __iter__(self):
37  """Iterator over vertices"""
38  vertices = self.getVertices()
39  return iter(vertices)
40 

◆ __len__()

def lsst.afw.geom.polygon.Polygon.__len__ (   self)

Definition at line 44 of file polygon.py.

44  def __len__(self):
45  return self.getNumEdges()
46 

◆ __reduce__()

def lsst.afw.geom.polygon.Polygon.__reduce__ (   self)

Definition at line 33 of file polygon.py.

33  def __reduce__(self):
34  return self.__class__, (self.getVertices(),)
35 

◆ __repr__()

def lsst.afw.geom.polygon.Polygon.__repr__ (   self)

Definition at line 30 of file polygon.py.

30  def __repr__(self):
31  return f"{self.__class__.__name__}({[p for p in self.getVertices()]})"
32 

◆ begin()

std::vector< LsstPoint >::const_iterator lsst::afw::geom::polygon::Polygon::begin ( ) const

Iterator for vertices.

Iterates only over the "open" polygon vertices (i.e., same number as returned by "getNumEdges").

Definition at line 339 of file Polygon.cc.

339 { return _impl->poly.outer().begin(); }

◆ calculateArea()

double lsst::afw::geom::polygon::Polygon::calculateArea ( ) const

Definition at line 322 of file Polygon.cc.

322 { return boost::geometry::area(_impl->poly); }

◆ calculateCenter()

LsstPoint lsst::afw::geom::polygon::Polygon::calculateCenter ( ) const

Definition at line 318 of file Polygon.cc.

318  {
319  return boost::geometry::return_centroid<LsstPoint>(_impl->poly);
320 }

◆ calculatePerimeter()

double lsst::afw::geom::polygon::Polygon::calculatePerimeter ( ) const

Definition at line 324 of file Polygon.cc.

324 { return boost::geometry::perimeter(_impl->poly); }

◆ cloneStorable()

std::shared_ptr< typehandling::Storable > lsst::afw::geom::polygon::Polygon::cloneStorable ( ) const
overridevirtual

Create a new Polygon that is a copy of this one.

Reimplemented from lsst::afw::typehandling::Storable.

Definition at line 593 of file Polygon.cc.

593  {
594  return std::make_unique<Polygon>(*this);
595 }

◆ contains()

bool lsst::afw::geom::polygon::Polygon::contains ( Point const &  point) const

Returns whether the polygon contains the point.

Definition at line 356 of file Polygon.cc.

356 { return boost::geometry::within(point, _impl->poly); }

◆ convexHull()

std::shared_ptr< Polygon > lsst::afw::geom::polygon::Polygon::convexHull ( ) const

Produce a polygon from the convex hull.

Definition at line 404 of file Polygon.cc.

404  {
405  BoostPolygon hull;
406  boost::geometry::convex_hull(_impl->poly, hull);
407  return std::shared_ptr<Polygon>(new Polygon(std::make_shared<Impl>(hull)));
408 }
boost::geometry::model::polygon< LsstPoint > BoostPolygon
Definition: Polygon.cc:20
Polygon(Box const &box)
Construct a rectangular Polygon whose vertices are the corners of a box.
Definition: Polygon.cc:288

◆ createImage() [1/2]

std::shared_ptr< afw::image::Image< float > > lsst::afw::geom::polygon::Polygon::createImage ( lsst::geom::Box2I const &  bbox) const

Create image of polygon.

Pixels entirely contained within the polygon receive value unity, pixels entirely outside the polygon receive value zero, and pixels on the border receive a value equal to the fraction of the pixel within the polygon.

Note that the center of the lower-left pixel is 0,0.

Definition at line 446 of file Polygon.cc.

446  {
447  using Image = afw::image::Image<float>;
448  std::shared_ptr<Image> image = std::make_shared<Image>(bbox);
449  image->setXY0(bbox.getMin());
450  *image = 0.0;
451  lsst::geom::Box2D bounds = getBBox(); // Polygon bounds
452  int xMin = std::max(static_cast<int>(bounds.getMinX()), bbox.getMinX());
453  int xMax = std::min(static_cast<int>(::ceil(bounds.getMaxX())), bbox.getMaxX());
454  int yMin = std::max(static_cast<int>(bounds.getMinY()), bbox.getMinY());
455  int yMax = std::min(static_cast<int>(::ceil(bounds.getMaxY())), bbox.getMaxY());
456  for (int y = yMin; y <= yMax; ++y) {
457  double const yPixelMin = (double)y - 0.5, yPixelMax = (double)y + 0.5;
458  BoostPolygon row; // A polygon of row y
459  boost::geometry::assign(
460  row, LsstBox(lsst::geom::Point2D(xMin, yPixelMin), lsst::geom::Point2D(xMax, yPixelMax)));
461  std::vector<BoostPolygon> intersections;
462  boost::geometry::intersection(_impl->poly, row, intersections);
463 
464  if (intersections.size() == 1 && boost::geometry::num_points(intersections[0]) == 5) {
465  // This row is fairly tame, and should have a long run of pixels within the polygon
466  BoostPolygon const& row = intersections[0];
467  std::vector<double> top, bottom;
468  top.reserve(2);
469  bottom.reserve(2);
470  bool failed = false;
471  for (std::vector<Point>::const_iterator i = row.outer().begin(); i != row.outer().end() - 1;
472  ++i) {
473  double const xCoord = i->getX(), yCoord = i->getY();
474  if (yCoord == yPixelMin) {
475  bottom.push_back(xCoord);
476  } else if (yCoord == yPixelMax) {
477  top.push_back(xCoord);
478  } else {
479  failed = true;
480  break;
481  }
482  }
483  if (!failed && top.size() == 2 && bottom.size() == 2) {
484  std::sort(top.begin(), top.end());
485  std::sort(bottom.begin(), bottom.end());
486  int const xMin = std::min(top[0], bottom[0]);
487  int const xStart = ::ceil(std::max(top[0], bottom[0])) + 1;
488  int const xStop = std::min(top[1], bottom[1]) - 1;
489  int const xMax = ::ceil(std::max(top[1], bottom[1]));
490  pixelRowOverlap(image, _impl->poly, std::max(xMin, bbox.getMinX()),
491  std::min(xStart, bbox.getMaxX()), y);
492  int x = xStart;
493  for (Image::x_iterator i = image->x_at(std::max(xStart, bbox.getMinX()) - image->getX0(),
494  y - image->getY0());
495  x <= std::min(xStop, bbox.getMaxX()); ++i, ++x) {
496  *i = 1.0;
497  }
498  pixelRowOverlap(image, _impl->poly, std::max(xStop, bbox.getMinX()),
499  std::min(xMax, bbox.getMaxX()), y);
500  continue;
501  }
502  }
503 
504  // Last resort: do each pixel independently...
505  for (auto const &intersection : intersections) {
506  double xMinRow = xMax, xMaxRow = xMin;
508  for (auto const &vertice : vertices) {
509  double const x = vertice.getX();
510  if (x < xMinRow) xMinRow = x;
511  if (x > xMaxRow) xMaxRow = x;
512  }
513 
514  pixelRowOverlap(image, _impl->poly, std::max(static_cast<int>(xMinRow), bbox.getMinX()),
515  std::min(static_cast<int>(::ceil(xMaxRow)), bbox.getMaxX()), y);
516  }
517  }
518  return image;
519 }
AmpInfoBoxKey bbox
Definition: Amplifier.cc:117
double x
afw::table::Key< afw::table::Array< ImagePixelT > > image
lsst::afw::geom::polygon::Polygon::Box LsstBox
Definition: Polygon.cc:18
int y
Definition: SpanSet.cc:48
T begin(T... args)
Box getBBox() const
Return bounding box.
Definition: Polygon.cc:314
std::vector< std::shared_ptr< Polygon > > intersection(Polygon const &other) const
Returns the intersection of two polygons.
Definition: Polygon.cc:370
A floating-point coordinate rectangle geometry.
Definition: Box.h:413
double getMaxY() const noexcept
Definition: Box.h:519
double getMaxX() const noexcept
Definition: Box.h:518
double getMinY() const noexcept
Definition: Box.h:515
double getMinX() const noexcept
Definition: Box.h:514
T end(T... args)
T max(T... args)
T min(T... args)
Backwards-compatibility support for depersisting the old Calib (FluxMag0/FluxMag0Err) objects.
Extent< int, N > ceil(Extent< double, N > const &input) noexcept
Return the component-wise ceil (round towards more positive).
Definition: Extent.cc:118
T push_back(T... args)
T reserve(T... args)
T size(T... args)
T sort(T... args)
int row
Definition: CR.cc:145

◆ createImage() [2/2]

std::shared_ptr<afw::image::Image<float> > lsst::afw::geom::polygon::Polygon::createImage ( lsst::geom::Extent2I const &  extent) const
inline

Definition at line 248 of file Polygon.h.

248  {
249  return createImage(lsst::geom::Box2I(lsst::geom::Point2I(0, 0), extent));
250  }
std::shared_ptr< afw::image::Image< float > > createImage(lsst::geom::Box2I const &bbox) const
Create image of polygon.
Definition: Polygon.cc:446
An integer coordinate rectangle.
Definition: Box.h:55

◆ display()

def lsst.afw.geom.polygon.Polygon.display (   self,
  xy0 = None,
  frame = 1,
  ctype = None 
)
Display polygon on existing frame

Definition at line 51 of file polygon.py.

51  def display(self, xy0=None, frame=1, ctype=None):
52  """Display polygon on existing frame"""
53  import lsst.geom
54  import lsst.afw.display as afwDisplay
55  xy0 = lsst.geom.Extent2D(0, 0) if xy0 is None else lsst.geom.Extent2D(xy0)
56  disp = afwDisplay.Display(frame=frame)
57  with disp.Buffering():
58  for p1, p2 in self.getEdges():
59  disp.line((p1 - xy0, p2 - xy0), ctype=ctype)
60 

◆ dynamicCast()

Dynamically cast a shared_ptr.

Dynamically cast a shared pointer and raise on failure.

You must provide an explicit template instantiation in the .cc file for each class that inherits from PersistableFacade. Designed to work around RTTI issues on macOS with hidden symbols;

Exceptions
lsst::pex::exceptions::LogicErrorif the cast fails

param[in] ptr The pointer to be cast.

Returns
The cast pointer.
Exceptions
lsst::pex::exceptions::TypeErrorIf the dynamic cast fails.

Definition at line 218 of file Persistable.cc.

18  {
19  auto result = std::dynamic_pointer_cast<T>(ptr);
20  if (!result) {
21  throw LSST_EXCEPT(pex::exceptions::TypeError, "Dynamic pointer cast failed");
22  }
23  return result;
24 }
py::object result
Definition: _schema.cc:429
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
uint64_t * ptr
Definition: RangeSet.cc:88

◆ end()

std::vector< LsstPoint >::const_iterator lsst::afw::geom::polygon::Polygon::end ( ) const

Definition at line 341 of file Polygon.cc.

341  {
342  return _impl->poly.outer().end() - 1; // Note removal of final "closed" point
343 }

◆ equals()

bool lsst::afw::geom::polygon::Polygon::equals ( typehandling::Storable const &  other) const
overridevirtualnoexcept

Compare this object to another Storable.

Returns
*this == other if other is a Polygon; otherwise false.

Reimplemented from lsst::afw::typehandling::Storable.

Definition at line 603 of file Polygon.cc.

603  {
604  return singleClassEquals(*this, other);
605 }
static bool singleClassEquals(T const &lhs, Storable const &rhs)
Test if a Storable is of a particular class and equal to another object.
Definition: Storable.h:151

◆ getBBox()

Polygon::Box lsst::afw::geom::polygon::Polygon::getBBox ( ) const

Return bounding box.

Definition at line 314 of file Polygon.cc.

314  {
315  return boostBoxToLsst(boost::geometry::return_envelope<BoostBox>(_impl->poly));
316 }

◆ getEdges()

std::vector< std::pair< LsstPoint, LsstPoint > > lsst::afw::geom::polygon::Polygon::getEdges ( ) const

Get vector of edges.

Returns edges, as pairs of vertices.

Definition at line 326 of file Polygon.cc.

326  {
329  edges.reserve(getNumEdges());
330  for (std::vector<LsstPoint>::const_iterator i = vertices.begin(), j = vertices.begin() + 1;
331  j != vertices.end(); ++i, ++j) {
332  edges.emplace_back(*i, *j);
333  }
334  return edges;
335 }
size_t getNumEdges() const
Return number of edges.
Definition: Polygon.cc:309
std::vector< Point > getVertices() const
Get vector of vertices.
Definition: Polygon.cc:337
T emplace_back(T... args)

◆ getNumEdges()

size_t lsst::afw::geom::polygon::Polygon::getNumEdges ( ) const

Return number of edges.

Identical with the number of points

Definition at line 309 of file Polygon.cc.

309  {
310  // boost::geometry::models::polygon uses a "closed" polygon: the start/end point is included twice
311  return boost::geometry::num_points(_impl->poly) - 1;
312 }

◆ getPersistenceName()

std::string lsst::afw::geom::polygon::Polygon::getPersistenceName ( ) const
overrideprotectedvirtual

Return the unique name used to persist this object and look up its factory.

Must be less than ArchiveIndexSchema::MAX_NAME_LENGTH characters.

Reimplemented from lsst::afw::table::io::Persistable.

Definition at line 578 of file Polygon.cc.

578 { return getPolygonPersistenceName(); }

◆ getPythonModule()

std::string lsst::afw::table::io::Persistable::getPythonModule ( ) const
protectedvirtualinherited

◆ getVertices()

std::vector< LsstPoint > lsst::afw::geom::polygon::Polygon::getVertices ( ) const

Get vector of vertices.

Note that the "closed" polygon vertices are returned, so the first and last vertex are identical and there is one more vertex than otherwise expected.

Definition at line 337 of file Polygon.cc.

337 { return _impl->poly.outer(); }

◆ hash_value()

std::size_t lsst::afw::geom::polygon::Polygon::hash_value ( ) const
overridevirtualnoexcept

Return a hash of this object.

Reimplemented from lsst::afw::typehandling::Storable.

Definition at line 349 of file Polygon.cc.

349  {
350  // boost::hash allows hash functions to throw, but the container hashes throw
351  // only if the element [geom::Point] has a throwing hash
352  static boost::hash<BoostPolygon::ring_type> polygonHash;
353  return polygonHash(_impl->poly.outer());
354 }

◆ intersection() [1/2]

std::vector< std::shared_ptr< Polygon > > lsst::afw::geom::polygon::Polygon::intersection ( Box const &  box) const

Definition at line 374 of file Polygon.cc.

374  {
375  return _impl->intersection(box);
376 }
std::vector< std::shared_ptr< Polygon > > intersection(PolyT const &other) const
Definition: Polygon.cc:249

◆ intersection() [2/2]

std::vector< std::shared_ptr< Polygon > > lsst::afw::geom::polygon::Polygon::intersection ( Polygon const &  other) const

Returns the intersection of two polygons.

Handles the full range of possibilities.

Definition at line 370 of file Polygon.cc.

370  {
371  return _impl->intersection(other._impl->poly);
372 }

◆ intersectionSingle() [1/2]

std::shared_ptr< Polygon > lsst::afw::geom::polygon::Polygon::intersectionSingle ( Box const &  box) const

Definition at line 366 of file Polygon.cc.

366  {
367  return _impl->intersectionSingle(box);
368 }
std::shared_ptr< Polygon > intersectionSingle(PolyT const &other) const
Definition: Polygon.cc:234

◆ intersectionSingle() [2/2]

std::shared_ptr< Polygon > lsst::afw::geom::polygon::Polygon::intersectionSingle ( Polygon const &  other) const

Returns the intersection of two polygons.

Does not handle non-convex polygons (which might have multiple independent intersections), and is provided as a convenience for when the polygons are known to be convex (e.g., image borders) and overlapping.

Definition at line 362 of file Polygon.cc.

362  {
363  return _impl->intersectionSingle(other._impl->poly);
364 }

◆ isPersistable()

bool lsst::afw::geom::polygon::Polygon::isPersistable ( ) const
inlineoverridevirtualnoexcept

Whether Polygon is persistable which is always true.

Reimplemented from lsst::afw::table::io::Persistable.

Definition at line 254 of file Polygon.h.

254 { return true; }

◆ operator!=()

bool lsst::afw::geom::polygon::Polygon::operator!= ( Polygon const &  other) const
inline

Definition at line 138 of file Polygon.h.

138 { return !(*this == other); }

◆ operator&() [1/2]

std::vector<std::shared_ptr<Polygon> > lsst::afw::geom::polygon::Polygon::operator& ( Box const &  rhs) const
inline

Definition at line 206 of file Polygon.h.

206 { return intersection(rhs); }

◆ operator&() [2/2]

std::vector<std::shared_ptr<Polygon> > lsst::afw::geom::polygon::Polygon::operator& ( Polygon const &  rhs) const
inline

Operators for syntactic sugar.

Definition at line 205 of file Polygon.h.

205 { return intersection(rhs); }

◆ operator=() [1/2]

Polygon & lsst::afw::geom::polygon::Polygon::operator= ( Polygon &&  )
default

◆ operator=() [2/2]

Polygon & lsst::afw::geom::polygon::Polygon::operator= ( Polygon const &  )
default

◆ operator==()

bool lsst::afw::geom::polygon::Polygon::operator== ( Polygon const &  other) const

Definition at line 345 of file Polygon.cc.

345  {
346  return boost::geometry::equals(_impl->poly, other._impl->poly);
347 }

◆ operator^() [1/2]

std::vector<std::shared_ptr<Polygon> > lsst::afw::geom::polygon::Polygon::operator^ ( Box const &  rhs) const
inline

Definition at line 210 of file Polygon.h.

210 { return symDifference(rhs); }
std::vector< std::shared_ptr< Polygon > > symDifference(Polygon const &other) const
Return the symmetric difference of two polygons.
Definition: Polygon.cc:390

◆ operator^() [2/2]

std::vector<std::shared_ptr<Polygon> > lsst::afw::geom::polygon::Polygon::operator^ ( Polygon const &  rhs) const
inline

Definition at line 209 of file Polygon.h.

209 { return symDifference(rhs); }

◆ operator|() [1/2]

std::vector<std::shared_ptr<Polygon> > lsst::afw::geom::polygon::Polygon::operator| ( Box const &  rhs) const
inline

Definition at line 208 of file Polygon.h.

208 { return union_(rhs); }
std::vector< std::shared_ptr< Polygon > > union_(Polygon const &other) const
Returns the union of two polygons.
Definition: Polygon.cc:384

◆ operator|() [2/2]

std::vector<std::shared_ptr<Polygon> > lsst::afw::geom::polygon::Polygon::operator| ( Polygon const &  rhs) const
inline

Definition at line 207 of file Polygon.h.

207 { return union_(rhs); }

◆ overlaps() [1/2]

bool lsst::afw::geom::polygon::Polygon::overlaps ( Box const &  box) const

Definition at line 360 of file Polygon.cc.

360 { return _impl->overlaps(box); }
bool overlaps(PolyT const &other) const
Definition: Polygon.cc:200

◆ overlaps() [2/2]

bool lsst::afw::geom::polygon::Polygon::overlaps ( Polygon const &  other) const

Returns whether the polygons overlap each other.

Note that there may be no intersection if the polygons only share a boundary.

Definition at line 358 of file Polygon.cc.

358 { return _impl->overlaps(other._impl->poly); }

◆ plot()

def lsst.afw.geom.polygon.Polygon.plot (   self,
  axes = None,
**  kwargs 
)
Plot polygon with matplotlib

Parameters
----------
axes : `matplotlib.axes.Axes`
    Matplotlib axes to use, or None
kwargs : any
    Additional arguments to `matplotlib.axes.Axes.plot`
    or `matplotlib.axes.Axes.scatter`.

Returns
-------
axes : `matplotlib.axes.Axes`
    The axes used to make the plot (same as ``axes``, if provided).

Definition at line 61 of file polygon.py.

61  def plot(self, axes=None, **kwargs):
62  """Plot polygon with matplotlib
63 
64  Parameters
65  ----------
66  axes : `matplotlib.axes.Axes`
67  Matplotlib axes to use, or None
68  kwargs : any
69  Additional arguments to `matplotlib.axes.Axes.plot`
70  or `matplotlib.axes.Axes.scatter`.
71 
72  Returns
73  -------
74  axes : `matplotlib.axes.Axes`
75  The axes used to make the plot (same as ``axes``, if provided).
76  """
77  import numpy
78  if axes is None:
79  import matplotlib.pyplot as plt
80  plt.figure()
81  axes = plt.axes()
82  for p1, p2 in self.getEdges():
83  x = (p1.getX(), p2.getX())
84  y = (p1.getY(), p2.getY())
85  axes.plot(x, y, **kwargs)
86  vertices = self.getVertices()
87  x = numpy.array([p[0] for p in vertices])
88  y = numpy.array([p[1] for p in vertices])
89  axes.scatter(x, y, **kwargs)
90  return axes
def plot(mag, width, centers, clusterId, marker="o", markersize=2, markeredgewidth=0, ltype='-', magType="model", clear=True)

◆ readFits() [1/3]

static std::shared_ptr<Polygon > lsst::afw::table::io::PersistableFacade< Polygon >::readFits ( fits::Fits fitsfile)
inlinestaticinherited

Read an object from an already open FITS object.

Parameters
[in]fitsfileFITS object to read from, already positioned at the desired HDU.

Definition at line 183 of file Persistable.h.

183  {
184  return dynamicCast(Persistable::_readFits(fitsfile));
185  }
static std::shared_ptr< Polygon > dynamicCast(std::shared_ptr< Persistable > const &ptr)
Dynamically cast a shared_ptr.
Definition: Persistable.cc:18

◆ readFits() [2/3]

static std::shared_ptr<Polygon > lsst::afw::table::io::PersistableFacade< Polygon >::readFits ( fits::MemFileManager manager,
int  hdu = fits::DEFAULT_HDU 
)
inlinestaticinherited

Read an object from a FITS file in memory.

Parameters
[in]managerManager for the memory to read from.
[in]hduHDU to read, where 0 is the primary. The special value of afw::fits::DEFAULT_HDU skips the primary HDU if it is empty.

Definition at line 205 of file Persistable.h.

205  {
206  return dynamicCast(Persistable::_readFits(manager, hdu));
207  }

◆ readFits() [3/3]

static std::shared_ptr<Polygon > lsst::afw::table::io::PersistableFacade< Polygon >::readFits ( std::string const &  fileName,
int  hdu = fits::DEFAULT_HDU 
)
inlinestaticinherited

Read an object from a regular FITS file.

Parameters
[in]fileNameName of the file to read.
[in]hduHDU to read, where 0 is the primary. The special value of afw::fits::DEFAULT_HDU skips the primary HDU if it is empty.

Definition at line 194 of file Persistable.h.

194  {
195  return dynamicCast(Persistable::_readFits(fileName, hdu));
196  }

◆ simplify()

std::shared_ptr< Polygon > lsst::afw::geom::polygon::Polygon::simplify ( double const  distance) const

Return a simplified polygon.

Removes unnecessary points (using the Douglas-Peucker algorithm).

Definition at line 398 of file Polygon.cc.

398  {
400  boost::geometry::simplify(_impl->poly, result, distance);
401  return std::shared_ptr<Polygon>(new Polygon(std::make_shared<Impl>(result)));
402 }

◆ singleClassEquals()

template<class T >
static bool lsst::afw::typehandling::Storable::singleClassEquals ( T const &  lhs,
Storable const &  rhs 
)
inlinestaticprotectedinherited

Test if a Storable is of a particular class and equal to another object.

This method template simplifies implementations of equals that delegate to operator== without supporting cross-class comparisons.

Template Parameters
TThe class expected of the two objects to be compared.
Parameters
lhs,rhsThe objects to compare. Note that rhs need not be a T, while lhs must be.
Returns
true if rhs is a T and lhs == rhs; false otherwise.
Exception Safety
Provides the same level of exception safety as operator==. Most implementations of operator== do not throw.
Note
This method template calls operator== with both arguments of compile-time type T const&. Its use is not recommended if there would be any ambiguity as to which operator== gets picked by overload resolution.

This method template is typically called from equals as:

bool MyType::equals(Storable const& other) const noexcept {
    return singleClassEquals(*this, other);
}

Definition at line 151 of file Storable.h.

151  {
152  auto typedRhs = dynamic_cast<T const*>(&rhs);
153  if (typedRhs != nullptr) {
154  return lhs == *typedRhs;
155  } else {
156  return false;
157  }
158  }

◆ subSample() [1/2]

std::shared_ptr< Polygon > lsst::afw::geom::polygon::Polygon::subSample ( double  maxLength) const

Definition at line 434 of file Polygon.cc.

434  {
435  std::vector<LsstPoint> vertices; // New vertices
436  vertices.reserve(getNumEdges() + static_cast<size_t>(::ceil(calculatePerimeter() / maxLength)));
438  for (auto const &edge : edges) {
439  Point const &p1 = edge.first, p2 = edge.second;
440  double const dist = ::sqrt(p1.distanceSquared(p2));
441  addSubSampledEdge(vertices, p1, p2, static_cast<size_t>(::ceil(dist / maxLength)));
442  }
443  return std::shared_ptr<Polygon>(new Polygon(std::make_shared<Impl>(vertices)));
444 }
lsst::geom::Point2D Point
Definition: Polygon.h:62
std::vector< std::pair< Point, Point > > getEdges() const
Get vector of edges.
Definition: Polygon.cc:326
double calculatePerimeter() const
Definition: Polygon.cc:324

◆ subSample() [2/2]

std::shared_ptr< Polygon > lsst::afw::geom::polygon::Polygon::subSample ( size_t  num) const

Sub-sample each edge.

This should provide greater fidelity when transforming with a non-linear transform.

Definition at line 424 of file Polygon.cc.

424  {
425  std::vector<LsstPoint> vertices; // New vertices
426  vertices.reserve(getNumEdges() * num);
428  for (auto const &edge : edges) {
429  addSubSampledEdge(vertices, edge.first, edge.second, num);
430  }
431  return std::shared_ptr<Polygon>(new Polygon(std::make_shared<Impl>(vertices)));
432 }

◆ swap()

void lsst::afw::geom::polygon::Polygon::swap ( Polygon other)
inlinenoexcept

Swap two polygons.

Definition at line 102 of file Polygon.h.

102 { std::swap(this->_impl, other._impl); }
T swap(T... args)

◆ symDifference() [1/2]

std::vector< std::shared_ptr< Polygon > > lsst::afw::geom::polygon::Polygon::symDifference ( Box const &  box) const

Definition at line 394 of file Polygon.cc.

394  {
395  return _impl->symDifference(box);
396 }
std::vector< std::shared_ptr< Polygon > > symDifference(PolyT const &other) const
Definition: Polygon.cc:275

◆ symDifference() [2/2]

std::vector< std::shared_ptr< Polygon > > lsst::afw::geom::polygon::Polygon::symDifference ( Polygon const &  other) const

Return the symmetric difference of two polygons.

Definition at line 390 of file Polygon.cc.

390  {
391  return _impl->symDifference(other._impl->poly);
392 }

◆ toString()

std::string lsst::afw::geom::polygon::Polygon::toString ( ) const
overridevirtual

Create a string representation of this object.

Reimplemented from lsst::afw::typehandling::Storable.

Definition at line 597 of file Polygon.cc.

597  {
598  std::stringstream buffer;
599  buffer << "Polygon(" << this->getVertices() << ")";
600  return buffer.str();
601 }
T str(T... args)

◆ transform() [1/2]

std::shared_ptr< Polygon > lsst::afw::geom::polygon::Polygon::transform ( lsst::geom::AffineTransform const &  transform) const
Parameters
transformTransform from original to target frame

Definition at line 415 of file Polygon.cc.

415  {
416  std::vector<LsstPoint> vertices; // New vertices
417  vertices.reserve(getNumEdges());
418  for (auto const &i : _impl->poly.outer()) {
419  vertices.push_back(transform(i));
420  }
421  return std::shared_ptr<Polygon>(new Polygon(std::make_shared<Impl>(vertices)));
422 }

◆ transform() [2/2]

std::shared_ptr< Polygon > lsst::afw::geom::polygon::Polygon::transform ( TransformPoint2ToPoint2 const &  transform) const

Transform the polygon.

The transformation is only applied to the vertices. If the transformation is non-linear, the edges will not reflect that, but simply join the vertices. Greater fidelity might be achieved by using "subSample" before transforming.

Parameters
transformTransform from original to target frame

Definition at line 410 of file Polygon.cc.

410  {
411  auto newVertices = transform.applyForward(getVertices());
412  return std::shared_ptr<Polygon>(new Polygon(std::make_shared<Impl>(newVertices)));
413 }

◆ union_() [1/2]

std::vector< std::shared_ptr< Polygon > > lsst::afw::geom::polygon::Polygon::union_ ( Box const &  box) const

Definition at line 388 of file Polygon.cc.

388 { return _impl->union_(box); }
std::vector< std::shared_ptr< Polygon > > union_(PolyT const &other) const
Definition: Polygon.cc:268

◆ union_() [2/2]

std::vector< std::shared_ptr< Polygon > > lsst::afw::geom::polygon::Polygon::union_ ( Polygon const &  other) const

Returns the union of two polygons.

Handles the full range of possibilities.

Note the trailing underscore in C++, due to "union" being a reserved word.

Definition at line 384 of file Polygon.cc.

384  {
385  return _impl->union_(other._impl->poly);
386 }

◆ unionSingle() [1/2]

std::shared_ptr< Polygon > lsst::afw::geom::polygon::Polygon::unionSingle ( Box const &  box) const

Definition at line 382 of file Polygon.cc.

382 { return _impl->unionSingle(box); }
std::shared_ptr< Polygon > unionSingle(PolyT const &other) const
Definition: Polygon.cc:256

◆ unionSingle() [2/2]

std::shared_ptr< Polygon > lsst::afw::geom::polygon::Polygon::unionSingle ( Polygon const &  other) const

Returns the union of two polygons.

Does not handle non-overlapping polygons, the union of which would be disjoint.

Definition at line 378 of file Polygon.cc.

378  {
379  return _impl->unionSingle(other._impl->poly);
380 }

◆ write()

void lsst::afw::geom::polygon::Polygon::write ( OutputArchiveHandle handle) const
overrideprotectedvirtual

Write the object to one or more catalogs.

The handle object passed to this function provides an interface for adding new catalogs and adding nested objects to the same archive (while checking for duplicates). See OutputArchiveHandle for more information.

Reimplemented from lsst::afw::table::io::Persistable.

Definition at line 580 of file Polygon.cc.

580  {
581  static PolygonSchema const& keys = PolygonSchema::get();
582  afw::table::BaseCatalog catalog = handle.makeCatalog(keys.schema);
583 
585  for (auto const &vertice : vertices) {
586  std::shared_ptr<afw::table::BaseRecord> record = catalog.addNew();
587  record->set(keys.vertices, vertice);
588  }
589 
590  handle.saveCatalog(catalog);
591 }
CatalogT< BaseRecord > BaseCatalog
Definition: fwd.h:72

◆ writeFits() [1/3]

void lsst::afw::table::io::Persistable::writeFits ( fits::Fits fitsfile) const
inherited

Write the object to an already-open FITS object.

Parameters
[in]fitsfileOpen FITS object to write to.

Definition at line 18 of file Persistable.cc.

18  {
19  OutputArchive archive;
20  archive.put(this);
21  archive.writeFits(fitsfile);
22 }

◆ writeFits() [2/3]

void lsst::afw::table::io::Persistable::writeFits ( fits::MemFileManager manager,
std::string const &  mode = "w" 
) const
inherited

Write the object to a FITS image in memory.

Parameters
[in]managerName of the file to write to.
[in]modeIf "w", any existing file with the given name will be overwritten. If "a", new HDUs will be appended to an existing file.

Definition at line 29 of file Persistable.cc.

29  {
30  fits::Fits fitsfile(manager, mode, fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
31  writeFits(fitsfile);
32 }
void writeFits(std::string const &fileName, std::string const &mode="w") const
Write the object to a regular FITS file.
Definition: Persistable.cc:24

◆ writeFits() [3/3]

void lsst::afw::table::io::Persistable::writeFits ( std::string const &  fileName,
std::string const &  mode = "w" 
) const
inherited

Write the object to a regular FITS file.

Parameters
[in]fileNameName of the file to write to.
[in]modeIf "w", any existing file with the given name will be overwritten. If "a", new HDUs will be appended to an existing file.

Definition at line 24 of file Persistable.cc.

24  {
25  fits::Fits fitsfile(fileName, mode, fits::Fits::AUTO_CLOSE | fits::Fits::AUTO_CHECK);
26  writeFits(fitsfile);
27 }

Friends And Related Function Documentation

◆ operator<<()

std::ostream & operator<< ( std::ostream os,
Storable const &  storable 
)
related

Output operator for Storable.

Parameters
osthe desired output stream
storablethe object to print
Returns
a reference to os
Exceptions
UnsupportedOperationExceptionThrown if storable does not have an implementation of Storable::toString.

Definition at line 174 of file Storable.h.

174  {
175  return os << storable.toString();
176 }
std::ostream * os
Definition: Schema.cc:557

The documentation for this class was generated from the following files: