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) {
124 boost::geometry::intersection(poly, pixel, overlap);
127 double const polyArea = boost::geometry::area(*i);
135 int const xStart,
int const xStop,
int const y) {
138 x <= xStop; ++i, ++
x) {
139 *i = pixelOverlap(poly, x, y);
154 for (
size_t i = 0; i < num - 1; ++i) {
163 return os <<
"BoostPolygon(" << poly.outer() <<
")";
174 boost::geometry::assign(poly, box);
178 boost::geometry::assign(poly, vertices);
183 void check() { boost::geometry::correct(poly); }
189 template <
class PolyT>
191 return !boost::geometry::disjoint(poly, other);
194 template <
class PolyT>
197 template <
class PolyT>
200 template <
class PolyT>
203 template <
class PolyT>
206 template <
class PolyT>
223 template <
class PolyT>
226 boost::geometry::intersection(poly, other, result);
227 if (result.
size() == 0) {
230 if (result.
size() > 1) {
233 (boost::format(
"Multiple polygons (%d) created by intersection()") % result.
size()).str());
238 template <
class PolyT>
241 boost::geometry::intersection(poly, other, boostResult);
242 return convertBoostPolygons(boostResult);
245 template <
class PolyT>
248 boost::geometry::union_(poly, other, result);
249 if (result.
size() != 1) {
252 (boost::format(
"Multiple polygons (%d) created by union_()") % result.
size()).str());
257 template <
class PolyT>
260 boost::geometry::union_(poly, other, boostResult);
261 return convertBoostPolygons(boostResult);
264 template <
class PolyT>
267 boost::geometry::sym_difference(poly, other, boostResult);
268 return convertBoostPolygons(boostResult);
271 Polygon::Polygon(
Polygon const&) =
default;
272 Polygon::Polygon(
Polygon&&) =
default;
276 Polygon::~Polygon() =
default;
284 auto corners = transform.
applyForward(boxToCorners(box));
285 boost::geometry::assign(_impl->poly, corners);
295 boost::geometry::assign(_impl->poly, corners);
301 return boost::geometry::num_points(_impl->poly) - 1;
305 return boostBoxToLsst(boost::geometry::return_envelope<BoostBox>(_impl->poly));
309 return boost::geometry::return_centroid<LsstPoint>(_impl->poly);
321 j != vertices.
end(); ++i, ++j) {
332 return _impl->poly.outer().end() - 1;
336 return boost::geometry::equals(_impl->poly, other._impl->poly);
346 return _impl->intersectionSingle(other._impl->poly);
350 return _impl->intersectionSingle(box);
354 return _impl->intersection(other._impl->poly);
358 return _impl->intersection(box);
362 return _impl->unionSingle(other._impl->poly);
368 return _impl->union_(other._impl->poly);
374 return _impl->symDifference(other._impl->poly);
378 return _impl->symDifference(box);
383 boost::geometry::simplify(_impl->poly, result, distance);
389 boost::geometry::convex_hull(_impl->poly, hull);
402 i != _impl->poly.outer().end(); ++i) {
413 addSubSampledEdge(vertices, i->first, i->second, num);
423 Point const &p1 = i->first, p2 = i->second;
425 addSubSampledEdge(vertices, p1, p2, static_cast<size_t>(::ceil(dist / maxLength)));
433 image->setXY0(bbox.
getMin());
440 for (
int y = yMin;
y <= yMax; ++
y) {
441 double const yPixelMin = (double)
y - 0.5, yPixelMax = (
double)
y + 0.5;
443 boost::geometry::assign(
446 boost::geometry::intersection(_impl->poly, row, intersections);
448 if (intersections.
size() == 1 && boost::geometry::num_points(intersections[0]) == 5) {
457 double const xCoord = i->getX(), yCoord = i->getY();
458 if (yCoord == yPixelMin) {
460 }
else if (yCoord == yPixelMax) {
467 if (!failed && top.
size() == 2 && bottom.
size() == 2) {
470 int const xMin =
std::min(top[0], bottom[0]);
471 int const xStart = ::ceil(
std::max(top[0], bottom[0])) + 1;
472 int const xStop =
std::min(top[1], bottom[1]) - 1;
473 int const xMax = ::ceil(
std::max(top[1], bottom[1]));
477 for (Image::x_iterator i = image->x_at(
std::max(xStart, bbox.
getMinX()) - image->getX0(),
491 double xMinRow = xMax, xMaxRow = xMin;
494 double const x = q->getX();
495 if (x < xMinRow) xMinRow =
x;
496 if (x > xMaxRow) xMaxRow =
x;
499 pixelRowOverlap(image, _impl->poly,
std::max(static_cast<int>(xMinRow), bbox.
getMinX()),
513 struct PolygonSchema {
517 static PolygonSchema
const&
get() {
518 static PolygonSchema instance;
523 PolygonSchema(
const PolygonSchema&) =
delete;
524 PolygonSchema&
operator=(
const PolygonSchema&) =
delete;
527 PolygonSchema(PolygonSchema&&) =
delete;
528 PolygonSchema&
operator=(PolygonSchema&&) =
delete;
535 schema.getCitizen().markPersistent();
539 class PolygonFactory :
public table::io::PersistableFactory {
541 explicit PolygonFactory(
std::string const&
name) : table::io::PersistableFactory(name) {}
544 CatalogVector
const& catalogs)
const {
545 static PolygonSchema
const&
keys = PolygonSchema::get();
552 vertices.
push_back(iter->get(keys.vertices));
559 std::string getPolygonPersistenceName() {
return "Polygon"; }
561 PolygonFactory registration(getPolygonPersistenceName());
568 static PolygonSchema
const&
keys = PolygonSchema::get();
574 record->set(keys.vertices, *i);
std::shared_ptr< Polygon > convexHull() const
Produce a polygon from the convex hull.
double getMinY() const noexcept
bool operator==(Polygon const &other) const
std::vector< Point >::const_iterator begin() const
Iterator for vertices.
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.
double getMinX() const noexcept
#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 getMaxX() const noexcept
double calculateArea() const
ItemVariant const * other
Point2D const getMin() const noexcept
boost::geometry::model::box< LsstPoint > BoostBox
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.
Point2I const getMin() const noexcept
Box getBBox() const
Return bounding box.
double getMaxY() const noexcept
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.
int getMaxY() const noexcept
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)
int getMaxX() const noexcept
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.
Point2D const getMax() const noexcept
Point calculateCenter() const
int getMinX() const noexcept
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.
bool contains(Point const &point) const
Returns whether the polygon contains the point.
std::shared_ptr< afw::image::Image< float > > createImage(lsst::geom::Box2I const &bbox) const
Create image of polygon.
A class to represent a 2-dimensional array of pixels.
std::shared_ptr< Polygon > transform(TransformPoint2ToPoint2 const &transform) const
Transform the polygon.
double distanceSquared(PointBase< double, N > const &other) const noexcept(Super::IS_ELEMENT_NOTHROW_COPYABLE)
lsst::afw::geom::polygon::Polygon::Box LsstBox
int getMinY() const noexcept
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.
double calculatePerimeter() const