4 #include "boost/geometry/geometry.hpp"
5 #include <boost/container_hash/hash.hpp>
21 using BoostBox = boost::geometry::model::box<LsstPoint>;
39 using type = cs::cartesian;
42 struct dimension<
LsstPoint> : boost::mpl::int_<2> {};
43 template <std::
size_t dim>
116 for (
size_t i = 1; i < num; ++i) {
125 boost::geometry::intersection(
poly,
pixel, overlap);
127 for (
auto const &i : overlap) {
128 double const polyArea = boost::geometry::area(i);
136 int const xStart,
int const xStop,
int const y) {
139 x <= xStop; ++i, ++
x) {
140 *i = pixelOverlap(
poly,
x,
y);
159 for (
size_t i = 0; i < num - 1; ++i) {
168 return os <<
"BoostPolygon(" <<
poly.outer() <<
")";
179 boost::geometry::assign(
poly, box);
194 template <
class PolyT>
196 return !boost::geometry::disjoint(
poly, other);
199 template <
class PolyT>
202 template <
class PolyT>
205 template <
class PolyT>
208 template <
class PolyT>
211 template <
class PolyT>
221 for (
auto const &boostPolygon : boostPolygons) {
228 template <
class PolyT>
231 boost::geometry::intersection(
poly, other,
result);
238 (
boost::format(
"Multiple polygons (%d) created by intersection()") %
result.size()).str());
243 template <
class PolyT>
246 boost::geometry::intersection(
poly, other, boostResult);
247 return convertBoostPolygons(boostResult);
250 template <
class PolyT>
262 template <
class PolyT>
265 boost::geometry::union_(
poly, other, boostResult);
266 return convertBoostPolygons(boostResult);
269 template <
class PolyT>
272 boost::geometry::sym_difference(
poly, other, boostResult);
273 return convertBoostPolygons(boostResult);
289 auto corners =
transform.applyForward(boxToCorners(box));
290 boost::geometry::assign(_impl->
poly, corners);
297 for (
auto & corner : corners) {
300 boost::geometry::assign(_impl->
poly, corners);
306 return boost::geometry::num_points(_impl->
poly) - 1;
310 return boostBoxToLsst(boost::geometry::return_envelope<BoostBox>(_impl->
poly));
314 return boost::geometry::return_centroid<LsstPoint>(_impl->
poly);
337 return _impl->
poly.outer().end() - 1;
341 return boost::geometry::equals(_impl->
poly, other._impl->
poly);
347 static boost::hash<BoostPolygon::ring_type> polygonHash;
348 return polygonHash(_impl->
poly.outer());
401 boost::geometry::convex_hull(_impl->
poly, hull);
413 for (
auto const &i : _impl->
poly.outer()) {
423 for (
auto const &edge : edges) {
424 addSubSampledEdge(
vertices, edge.first, edge.second, num);
433 for (
auto const &edge : edges) {
434 Point const &p1 = edge.first, p2 = edge.second;
436 addSubSampledEdge(
vertices, p1, p2,
static_cast<size_t>(::
ceil(dist / maxLength)));
451 for (
int y = yMin;
y <= yMax; ++
y) {
452 double const yPixelMin = (double)
y - 0.5, yPixelMax = (
double)
y + 0.5;
454 boost::geometry::assign(
457 boost::geometry::intersection(_impl->
poly,
row, intersections);
459 if (intersections.
size() == 1 && boost::geometry::num_points(intersections[0]) == 5) {
468 double const xCoord = i->getX(), yCoord = i->getY();
469 if (yCoord == yPixelMin) {
471 }
else if (yCoord == yPixelMax) {
478 if (!failed && top.
size() == 2 && bottom.
size() == 2) {
481 int const xMin =
std::min(top[0], bottom[0]);
483 int const xStop =
std::min(top[1], bottom[1]) - 1;
501 double xMinRow = xMax, xMaxRow = xMin;
503 for (
auto const &vertice :
vertices) {
504 double const x = vertice.getX();
505 if (
x < xMinRow) xMinRow =
x;
506 if (
x > xMaxRow) xMaxRow =
x;
523 struct PolygonSchema {
527 static PolygonSchema
const& get() {
528 static PolygonSchema instance;
533 PolygonSchema(
const PolygonSchema&) =
delete;
534 PolygonSchema&
operator=(
const PolygonSchema&) =
delete;
537 PolygonSchema(PolygonSchema&&) =
delete;
538 PolygonSchema&
operator=(PolygonSchema&&) =
delete;
543 vertices(
afw::table::PointKey<double>::addFields(
schema,
"vertices",
"list of vertex points",
547 class PolygonFactory :
public table::io::PersistableFactory {
552 CatalogVector
const& catalogs)
const override {
553 static PolygonSchema
const&
keys = PolygonSchema::get();
567 std::string getPolygonPersistenceName() {
return "Polygon"; }
569 PolygonFactory registration(getPolygonPersistenceName());
576 static PolygonSchema
const&
keys = PolygonSchema::get();
580 for (
auto const &vertice :
vertices) {
582 record->set(
keys.vertices, vertice);
589 return std::make_unique<Polygon>(*
this);
594 buffer <<
"Polygon(" << this->
getVertices() <<
")";
599 return singleClassEquals(*
this, other);
table::Key< std::string > name
table::PointKey< int > pixel
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
lsst::afw::geom::polygon::Polygon::Box LsstBox
boost::geometry::model::linestring< LsstPoint > BoostLineString
afw::table::PointKey< double > vertices
afw::table::Schema schema
lsst::afw::geom::polygon::Polygon::Point LsstPoint
boost::geometry::model::box< LsstPoint > BoostBox
boost::geometry::model::polygon< LsstPoint > BoostPolygon
#define LSST_ARCHIVE_ASSERT(EXPR)
An assertion macro used to validate the structure of an InputArchive.
lsst::geom::Point2D Point
std::shared_ptr< Polygon > unionSingle(Polygon const &other) const
Returns the union of two polygons.
std::vector< Point >::const_iterator begin() const
Iterator for vertices.
bool overlaps(Polygon const &other) const
Returns whether the polygons overlap each other.
Box getBBox() const
Return bounding box.
std::shared_ptr< Polygon > convexHull() const
Produce a polygon from the convex hull.
std::vector< std::pair< Point, Point > > getEdges() const
Get vector of edges.
std::shared_ptr< Polygon > intersectionSingle(Polygon const &other) const
Returns the intersection of two polygons.
std::shared_ptr< afw::image::Image< float > > createImage(lsst::geom::Box2I const &bbox) const
Create image of polygon.
Polygon(Box const &box)
Construct a rectangular Polygon whose vertices are the corners of a box.
std::shared_ptr< Polygon > subSample(size_t num) const
Sub-sample each edge.
std::vector< std::shared_ptr< Polygon > > intersection(Polygon const &other) const
Returns the intersection of two polygons.
Point calculateCenter() const
size_t getNumEdges() const
Return number of edges.
std::shared_ptr< Polygon > transform(TransformPoint2ToPoint2 const &transform) const
Transform the polygon.
double calculateArea() const
std::shared_ptr< typehandling::Storable > cloneStorable() const override
Create a new Polygon that is a copy of this one.
std::vector< Point > getVertices() const
Get vector of vertices.
std::shared_ptr< Polygon > simplify(double const distance) const
Return a simplified polygon.
bool operator==(Polygon const &other) const
std::string toString() const override
Create a string representation of this object.
std::vector< std::shared_ptr< Polygon > > union_(Polygon const &other) const
Returns the union of two polygons.
std::size_t hash_value() const noexcept override
Return a hash of this object.
std::vector< std::shared_ptr< Polygon > > symDifference(Polygon const &other) const
Return the symmetric difference of two polygons.
std::string getPersistenceName() const override
Return the unique name used to persist this object and look up its factory.
bool contains(Point const &point) const
Returns whether the polygon contains the point.
void write(OutputArchiveHandle &handle) const override
Write the object to one or more catalogs.
Polygon & operator=(Polygon const &)
std::vector< Point >::const_iterator end() const
bool equals(typehandling::Storable const &other) const noexcept override
Compare this object to another Storable.
double calculatePerimeter() const
An exception that indicates the single-polygon assumption has been violated.
typename _view_t::x_iterator x_iterator
An iterator for traversing the pixels in a row.
A class to represent a 2-dimensional array of pixels.
std::shared_ptr< RecordT > addNew()
Create a new record, add it to the end of the catalog, and return a pointer to it.
CatalogIterator< typename Internal::const_iterator > const_iterator
reference front() const
Return the first record.
An object passed to Persistable::write to allow it to persist itself.
void saveCatalog(BaseCatalog const &catalog)
Save a catalog in the archive.
BaseCatalog makeCatalog(Schema const &schema)
Return a new, empty catalog with the given schema.
Interface supporting iteration over heterogenous containers.
A floating-point coordinate rectangle geometry.
double getMaxY() const noexcept
Point2D const getMax() const noexcept
double getMaxX() const noexcept
double getMinY() const noexcept
Point2D const getMin() const noexcept
double getMinX() const noexcept
An integer coordinate rectangle.
double distanceSquared(PointBase< T, N > const &other) const noexcept(Super::IS_ELEMENT_NOTHROW_COPYABLE)
Cast this object to an Extent of the same numeric type and dimensionality.
T emplace_back(T... args)
std::ostream & operator<<(std::ostream &os, Polygon const &poly)
Stream polygon.
Backwards-compatibility support for depersisting the old Calib (FluxMag0/FluxMag0Err) objects.
FilterProperty & operator=(FilterProperty const &)=default
CatalogT< BaseRecord > BaseCatalog
Low-level polynomials (including special polynomials) in C++.
Extent< int, N > ceil(Extent< double, N > const &input) noexcept
Return the component-wise ceil (round towards more positive).
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
A base class for image defects.
static void set(LsstPoint &p, LsstPoint::Element const &value)
static double get(LsstPoint const &p)
static double get(LsstBox const &box)
static double get(LsstBox const &box)
static double get(LsstBox const &box)
static double get(LsstBox const &box)
static std::vector< std::shared_ptr< Polygon > > convertBoostPolygons(std::vector< BoostPolygon > const &boostPolygons)
Impl(BoostPolygon const &_poly)
std::vector< std::shared_ptr< Polygon > > symDifference(PolyT const &other) const
Impl(Polygon::Box const &box)
bool overlaps(PolyT const &other) const
std::shared_ptr< Polygon > intersectionSingle(PolyT const &other) const
std::vector< std::shared_ptr< Polygon > > union_(PolyT const &other) const
std::shared_ptr< Polygon > unionSingle(PolyT const &other) const
std::vector< std::shared_ptr< Polygon > > intersection(PolyT const &other) const
Impl(std::vector< LsstPoint > const &vertices)
std::shared_ptr< table::io::Persistable > read(table::io::InputArchive const &archive, table::io::CatalogVector const &catalogs) const override