4 #include "boost/geometry/geometry.hpp" 20 typedef boost::geometry::model::box<LsstPoint>
BoostBox;
38 typedef cs::cartesian
type;
41 struct dimension<
LsstPoint> : boost::mpl::int_<2> {};
42 template <std::
size_t dim>
43 struct access<LsstPoint, dim> {
44 static double get(LsstPoint
const& p) {
return p[dim]; }
115 for (
size_t i = 1; i < num; ++i) {
121 double pixelOverlap(
BoostPolygon const& poly,
int const x,
int const y) {
125 boost::geometry::intersection(poly, pixel, 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);
155 for (
size_t i = 0; i < num - 1; ++i) {
164 return os <<
"BoostPolygon(" << poly.outer() <<
")";
175 boost::geometry::assign(poly, box);
179 boost::geometry::assign(poly, vertices);
184 void check() { boost::geometry::correct(poly); }
190 template <
class PolyT>
192 return !boost::geometry::disjoint(poly, other);
195 template <
class PolyT>
198 template <
class PolyT>
201 template <
class PolyT>
204 template <
class PolyT>
207 template <
class PolyT>
224 template <
class PolyT>
227 boost::geometry::intersection(poly, other, result);
228 if (result.
size() == 0) {
231 if (result.
size() > 1) {
234 (boost::format(
"Multiple polygons (%d) created by intersection()") % result.
size()).str());
239 template <
class PolyT>
242 boost::geometry::intersection(poly, other, boostResult);
243 return convertBoostPolygons(boostResult);
246 template <
class PolyT>
249 boost::geometry::union_(poly, other, result);
250 if (result.
size() != 1) {
253 (boost::format(
"Multiple polygons (%d) created by union_()") % result.
size()).str());
258 template <
class PolyT>
261 boost::geometry::union_(poly, other, boostResult);
262 return convertBoostPolygons(boostResult);
265 template <
class PolyT>
268 boost::geometry::sym_difference(poly, other, boostResult);
269 return convertBoostPolygons(boostResult);
272 Polygon::Polygon(
Polygon const&) =
default;
273 Polygon::Polygon(
Polygon&&) =
default;
277 Polygon::~Polygon() =
default;
285 auto corners = transform.
applyForward(boxToCorners(box));
286 boost::geometry::assign(_impl->poly, corners);
296 boost::geometry::assign(_impl->poly, corners);
302 return boost::geometry::num_points(_impl->poly) - 1;
306 return boostBoxToLsst(boost::geometry::return_envelope<BoostBox>(_impl->poly));
310 return boost::geometry::return_centroid<LsstPoint>(_impl->poly);
322 j != vertices.
end(); ++i, ++j) {
333 return _impl->poly.outer().end() - 1;
337 return boost::geometry::equals(_impl->poly, other._impl->poly);
347 return _impl->intersectionSingle(other._impl->poly);
351 return _impl->intersectionSingle(box);
355 return _impl->intersection(other._impl->poly);
359 return _impl->intersection(box);
363 return _impl->unionSingle(other._impl->poly);
369 return _impl->union_(other._impl->poly);
375 return _impl->symDifference(other._impl->poly);
379 return _impl->symDifference(box);
384 boost::geometry::simplify(_impl->poly, result, distance);
390 boost::geometry::convex_hull(_impl->poly, hull);
403 i != _impl->poly.outer().end(); ++i) {
414 addSubSampledEdge(vertices, i->first, i->second, num);
424 Point const &p1 = i->first, p2 = i->second;
426 addSubSampledEdge(vertices, p1, p2, static_cast<size_t>(::
ceil(dist / maxLength)));
434 image->setXY0(bbox.
getMin());
441 for (
int y = yMin;
y <= yMax; ++
y) {
442 double const yPixelMin = (double)
y - 0.5, yPixelMax = (
double)
y + 0.5;
444 boost::geometry::assign(
447 boost::geometry::intersection(_impl->poly, row, intersections);
449 if (intersections.
size() == 1 && boost::geometry::num_points(intersections[0]) == 5) {
458 double const xCoord = i->getX(), yCoord = i->getY();
459 if (yCoord == yPixelMin) {
461 }
else if (yCoord == yPixelMax) {
468 if (!failed && top.
size() == 2 && bottom.
size() == 2) {
471 int const xMin =
std::min(top[0], bottom[0]);
473 int const xStop =
std::min(top[1], bottom[1]) - 1;
478 for (Image::x_iterator i = image->x_at(
std::max(xStart, bbox.
getMinX()) - image->getX0(),
492 double xMinRow = xMax, xMaxRow = xMin;
495 double const x = q->getX();
496 if (x < xMinRow) xMinRow =
x;
497 if (x > xMaxRow) xMaxRow =
x;
500 pixelRowOverlap(image, _impl->poly,
std::max(static_cast<int>(xMinRow), bbox.
getMinX()),
514 struct PolygonSchema {
518 static PolygonSchema
const&
get() {
519 static PolygonSchema instance;
524 PolygonSchema(
const PolygonSchema&) =
delete;
525 PolygonSchema&
operator=(
const PolygonSchema&) =
delete;
528 PolygonSchema(PolygonSchema&&) =
delete;
529 PolygonSchema&
operator=(PolygonSchema&&) =
delete;
536 schema.getCitizen().markPersistent();
540 class PolygonFactory :
public table::io::PersistableFactory {
542 explicit PolygonFactory(
std::string const&
name) : table::io::PersistableFactory(name) {}
545 CatalogVector
const& catalogs)
const {
546 static PolygonSchema
const&
keys = PolygonSchema::get();
553 vertices.
push_back(iter->get(keys.vertices));
560 std::string getPolygonPersistenceName() {
return "Polygon"; }
562 PolygonFactory registration(getPolygonPersistenceName());
569 static PolygonSchema
const&
keys = PolygonSchema::get();
575 record->set(keys.vertices, *i);
std::shared_ptr< Polygon > convexHull() const
Produce a polygon from the convex hull.
bool operator==(Polygon const &other) const
std::vector< Point >::const_iterator begin() const
Iterator for vertices.
Point2I const getMin() const
std::vector< std::shared_ptr< Polygon > > intersection(Polygon const &other) const
Returns the intersection of two polygons.
Polygon & operator=(Polygon const &)
Impl(BoostPolygon const &_poly)
bool overlaps(Polygon const &other) const
Returns whether the polygons overlap each other.
CatalogT< BaseRecord > BaseCatalog
virtual std::string getPersistenceName() const
Whether Polygon is persistable which is always true.
An object passed to Persistable::write to allow it to persist itself.
#define LSST_ARCHIVE_ASSERT(EXPR)
An assertion macro used to validate the structure of an InputArchive.
Impl(std::vector< LsstPoint > const &vertices)
Polygon(Box const &box)
Construct a rectangular Polygon whose vertices are the corners of a box.
_view_t::x_iterator x_iterator
An iterator for traversing the pixels in a row.
std::vector< LsstPoint > LsstRing
bool overlaps(PolyT const &other) const
static PointKey addFields(Schema &schema, std::string const &name, std::string const &doc, std::string const &unit)
Add a pair of _x, _y fields to a Schema, and return a PointKey that points to them.
double calculateArea() const
ItemVariant const * other
boost::geometry::model::box< LsstPoint > BoostBox
An integer coordinate rectangle.
std::vector< std::shared_ptr< Polygon > > symDifference(Polygon const &other) const
Return the symmetric difference of two polygons.
std::shared_ptr< Polygon > unionSingle(Polygon const &other) const
Returns the union of two polygons.
Box getBBox() const
Return bounding box.
virtual void write(OutputArchiveHandle &handle) const
Whether Polygon is persistable which is always true.
lsst::afw::geom::polygon::Polygon::Point LsstPoint
A base class for image defects.
reference front() const
Return the first record.
boost::geometry::model::polygon< LsstPoint > BoostPolygon
std::vector< std::pair< Point, Point > > getEdges() const
Get vector of edges.
std::vector< std::shared_ptr< Polygon > > union_(Polygon const &other) const
Returns the union of two polygons.
boost::geometry::model::linestring< LsstPoint > BoostLineString
Impl(Polygon::Box const &box)
double distanceSquared(PointBase< T, N > const &other) const
An exception that indicates the single-polygon assumption has been violated.
std::unique_ptr< SchemaItem< U > > result
CatalogIterator< typename Internal::const_iterator > const_iterator
std::shared_ptr< Polygon > subSample(size_t num) const
Sub-sample each edge.
BaseCatalog makeCatalog(Schema const &schema)
Return a new, empty catalog with the given schema.
Extent< int, N > ceil(Extent< double, N > const &input)
Return the component-wise ceil (round towards more positive).
Point calculateCenter() const
std::vector< Point > getVertices() const
Get vector of vertices.
#define LSST_EXCEPT(type,...)
size_t getNumEdges() const
Return number of edges.
std::shared_ptr< Polygon > simplify(double const distance) const
Return a simplified polygon.
std::shared_ptr< Polygon > intersectionSingle(Polygon const &other) const
Returns the intersection of two polygons.
std::ostream & operator<<(std::ostream &os, BoostPolygon const &poly)
std::vector< Point >::const_iterator end() const
Iterator for vertices.
table::PointKey< int > pixel
afw::table::Schema schema
void saveCatalog(BaseCatalog const &catalog)
Save a catalog in the archive.
A floating-point coordinate rectangle geometry.
std::shared_ptr< afw::image::Image< float > > createImage(Box2I const &bbox) const
Create image of polygon.
bool contains(Point const &point) const
Returns whether the polygon contains the point.
A class to represent a 2-dimensional array of pixels.
std::shared_ptr< Polygon > transform(TransformPoint2ToPoint2 const &transform) const
Transform the polygon.
lsst::afw::geom::polygon::Polygon::Box LsstBox
afw::table::PointKey< double > vertices
std::shared_ptr< RecordT > addNew()
Create a new record, add it to the end of the catalog, and return a pointer to it.
Point2D const getMax() const
Point2D const getMin() const
double calculatePerimeter() const