46 bool comparePrimaryRun(PrimaryRun
const&
first, PrimaryRun
const&
second) {
47 if (first.y != second.y) {
48 return first.y < second.y;
49 }
else if (first.m != second.m) {
50 return first.m < second.m;
52 return first.xmin < second.xmin;
56 class ComparePrimaryRunY {
58 bool operator()(PrimaryRun
const& pr,
int yval) {
return pr.y < yval; }
59 bool operator()(
int yval, PrimaryRun
const& pr) {
return yval < pr.y; }
62 class ComparePrimaryRunM {
64 bool operator()(PrimaryRun
const& pr,
int mval) {
return pr.m < mval; }
65 bool operator()(
int mval, PrimaryRun
const& pr) {
return mval < pr.m; }
74 bool spansOverlap(Span
const&
a, Span
const&
b,
bool compareY =
true) {
77 yTruth = a.getY() == b.getY();
79 return (yTruth && ((a.getMaxX() >= b.getMinX() && a.getMinX() <= b.getMinX()) ||
80 (b.getMaxX() >= a.getMinX() && b.getMinX() <= a.getMinX())))
92 bool spansContiguous(Span
const& a, Span
const& b,
bool compareY =
true) {
95 yTruth = a.getY() == b.getY();
97 return (yTruth && ((a.getMaxX() + 1 >= b.getMinX() && a.getMinX() <= b.getMinX()) ||
98 (b.getMaxX() + 1 >= a.getMinX() && b.getMinX() <= a.getMinX())))
114 template <
typename T,
bool invert>
118 auto maskBBox = mask.
getBBox();
119 for (
auto const & spn : spanSet) {
122 bool started =
false;
125 if (y < maskBBox.getMinY() || y > maskBBox.getMaxY()) {
132 int startX =
std::max(spn.getMinX(), maskBBox.getMinX());
133 int endX =
std::min(spn.getMaxX(), maskBBox.getMaxX());
134 for (
int x = startX;
x <= endX; ++
x){
136 bool pixelCompare = mask.
get0(
x, y) & bitmask;
140 pixelCompare = !pixelCompare;
165 return std::make_shared<SpanSet>(
std::move(newVec));
181 for (
int i = beginY; i < _bbox.
getEndY(); ++i) {
182 _spanVector.push_back(
Span(i, beginX, maxX));
189 if (_spanVector.empty()) {
203 if (_spanVector.size() == 0) {
214 void SpanSet::_runNormalize() {
220 std::sort(_spanVector.begin(), _spanVector.end());
225 newSpans.
reserve(_spanVector.size());
227 newSpans.push_back(*_spanVector.begin());
233 for (
auto iter = ++(_spanVector.begin()); iter != _spanVector.end(); ++iter) {
234 auto & newSpansEnd = newSpans.back();
235 if (spansContiguous(newSpansEnd, *iter)) {
236 newSpansEnd =
Span(newSpansEnd.getY(),
std::min(newSpansEnd.getMinX(), iter->getMinX()),
237 std::max(newSpansEnd.getMaxX(), iter->getMaxX()));
239 newSpans.push_back(
Span(*iter));
247 void SpanSet::_initialize() {
259 int minX = _spanVector[0].getMinX();
260 int maxX = _spanVector[0].getMaxX();
263 for (
const auto&
span : _spanVector) {
264 if (
span.getMinX() < minX) {
265 minX =
span.getMinX();
267 if (
span.getMaxX() > maxX) {
268 maxX =
span.getMaxX();
271 _area +=
span.getMaxX() -
span.getMinX() + 1;
273 _bbox =
Box2I(
Point2I(minX, _spanVector.front().getY()),
Point2I(maxX, _spanVector.back().getY()));
315 void SpanSet::_label(
318 auto currentIndex = spn.
getY();
319 if (currentIndex > 0) {
321 for (
auto const& tup : sortMap[currentIndex - 1]) {
322 if (!labelVector[tup.first] && spansOverlap(spn, *(tup.second),
false)) {
323 labelVector[tup.first] = currentLabel;
324 _label(*(tup.second), labelVector, currentLabel, sortMap);
328 if (currentIndex <= _spanVector.back().getY() - 1) {
330 for (
auto& tup : sortMap[currentIndex + 1]) {
331 if (!labelVector[tup.first] && spansOverlap(spn, *(tup.second),
false)) {
332 labelVector[tup.first] = currentLabel;
333 _label(*(tup.second), labelVector, currentLabel, sortMap);
346 for (
auto const& spn : _spanVector) {
350 for (
auto const& currentSpan : _spanVector) {
351 if (!labelVector[index]) {
352 labelVector[index] = currentLabel;
353 _label(currentSpan, labelVector, currentLabel, sortMap);
368 auto labeledPair = _makeLabels();
373 return labeledPair.second <= 2;
377 auto labeledPair = _makeLabels();
378 auto labels = labeledPair.first;
379 auto numberOfLabels = labeledPair.second;
387 if (numberOfLabels == 1) {
388 subRegions.
push_back(std::make_shared<SpanSet>());
391 subRegions.
reserve(numberOfLabels - 1);
395 for (
std::size_t i = 0; i < _spanVector.size(); ++i) {
396 subSpanLists[labels[i] - 1].
push_back(_spanVector[i]);
399 for (
std::size_t i = 0; i < numberOfLabels - 1; ++i) {
400 subRegions.
push_back(std::make_shared<SpanSet>(subSpanLists[i]));
407 setMask(tempMask, static_cast<image::MaskPixel>(1));
409 erodedSpanSet->clearMask(tempMask, static_cast<image::MaskPixel>(1));
415 return makeShift(x, y);
420 return makeShift(offset.getX(), offset.getY());
426 tempVec.
reserve(_spanVector.size());
427 for (
auto const& spn : _spanVector) {
430 return std::make_shared<SpanSet>(
std::move(tempVec),
false);
438 for (
auto const& spn : _spanVector) {
445 return std::make_shared<SpanSet>(
std::move(tempVec),
false);
450 for (
auto const& otherSpan : other) {
451 for (
auto const& spn : _spanVector) {
452 if (spansOverlap(otherSpan, spn)) {
466 for (
auto const& otherSpn : other) {
468 for (
auto const& spn : _spanVector) {
488 for (
auto& spn : _spanVector) {
499 double xc = 0, yc = 0;
500 for (
auto const& spn : _spanVector) {
501 int const y = spn.
getY();
504 int const npix = x1 - x0 + 1;
507 xc += npix * 0.5 * (x1 + x0);
512 return Point2D(xc / _area, yc / _area);
518 double const xc = cen.getX();
519 double const yc = cen.getY();
521 double sumxx = 0, sumxy = 0, sumyy = 0;
522 for (
auto const& spn : _spanVector) {
523 int const y = spn.
getY();
524 int const x0 = spn.
getX0();
525 int const x1 = spn.
getX1();
526 int const npix = x1 - x0 + 1;
528 for (
int x = x0; x <= x1; ++
x) {
529 sumxx += (x - xc) * (x - xc);
531 sumxy += npix * (0.5 * (x1 + x0) - xc) * (y - yc);
532 sumyy += npix * (y - yc) * (y - yc);
542 return dilated(*stencilToSpanSet);
547 if (other.
size() == 0) {
548 return std::make_shared<SpanSet>(_spanVector.begin(), _spanVector.end(),
false);
554 for (
auto const& spn : _spanVector) {
555 for (
auto const& otherSpn : other) {
556 int const xmin = spn.
getMinX() + otherSpn.getMinX();
557 int const xmax = spn.
getMaxX() + otherSpn.getMaxX();
558 int const yval = spn.
getY() + otherSpn.getY();
559 tempVec.push_back(
Span(yval, xmin, xmax));
563 return std::make_shared<SpanSet>(
std::move(tempVec));
570 return eroded(*stencilToSpanSet);
575 if (other.
size() == 0 || this->
size() == 0) {
576 return std::make_shared<SpanSet>(_spanVector.begin(), _spanVector.end(),
false);
584 for (
auto const& spn : _spanVector) {
586 for (
auto const& otherSpn : other) {
587 if ((otherSpn.getMaxX() - otherSpn.getMinX()) <= (spn.
getMaxX() - spn.
getMinX())) {
590 int y = spn.
getY() - otherSpn.getY();
601 for (
int y = primaryRuns.
front().y; y <= primaryRuns.
back().y; ++
y) {
609 auto otherYRange = other.
back().getY() - other.
front().getY() + 1;
610 if (
std::distance(yRange.first, yRange.second) < otherYRange) {
621 for (
int m = 0;
m < otherYRange; ++
m) {
622 auto mRange =
std::equal_range(yRange.first, yRange.second,
m, ComparePrimaryRunM());
623 if ((mRange.first == mRange.second)) {
630 int startX = mRange.first->xmin;
631 int endX = mRange.first->xmax;
632 for (
auto run = mRange.first + 1;
run != mRange.second; ++
run) {
633 if (
run->xmin > endX) {
635 candidateRuns.
push_back(PrimaryRun{
m,
y, startX, endX});
643 candidateRuns.
push_back(PrimaryRun{
m,
y, startX, endX});
652 for (
auto& good : goodRuns) {
653 for (
auto& cand : candidateRuns) {
654 int start =
std::max(good.xmin, cand.xmin);
665 for (
auto&
run : goodRuns) {
669 return std::make_shared<SpanSet>(
std::move(tempVec));
674 return _spanVector == other._spanVector;
679 return _spanVector != other._spanVector;
688 for (
auto dy = -r; dy <= r; ++dy) {
689 int dx =
static_cast<int>(sqrt(r * r - dy * dy));
690 tempVec.
push_back(
Span(dy + offset.getY(), -dx + offset.getX(), dx + offset.getX()));
694 for (
auto dy = -r; dy <= r; ++dy) {
695 int dx = r - abs(dy);
696 tempVec.
push_back(
Span(dy + offset.getY(), -dx + offset.getX(), dx + offset.getX()));
700 for (
auto dy = -r; dy <= r; ++dy) {
702 tempVec.
push_back(
Span(dy + offset.getY(), -dx + offset.getX(), dx + offset.getX()));
706 return std::make_shared<SpanSet>(
std::move(tempVec),
false);
711 return std::make_shared<SpanSet>(pr.
begin(), pr.
end());
717 return std::make_shared<SpanSet>();
720 if (other == *
this) {
721 return std::make_shared<SpanSet>(this->_spanVector);
724 auto otherIter = other.
begin();
725 for (
auto const& spn : _spanVector) {
726 while (otherIter != other.
end() && otherIter->getY() <= spn.
getY()) {
727 if (spansOverlap(spn, *otherIter)) {
730 auto newSpan =
Span(spn.
getY(), newMin, newMax);
731 tempVec.push_back(newSpan);
736 return std::make_shared<SpanSet>(
std::move(tempVec));
742 return std::make_shared<SpanSet>(this->
begin(), this->
end());
745 if (other == *
this) {
746 return std::make_shared<SpanSet>();
755 auto otherIter = other.
begin();
756 for (
auto const& spn : _spanVector) {
758 bool spanStarted =
false;
760 while (otherIter != other.
end() && otherIter->getY() <= spn.
getY()) {
761 if (spansOverlap(spn, *otherIter)) {
777 if (spn.
getMinX() < otherIter->getMinX()) {
778 tempVec.push_back(
Span(spn.
getY(), spn.
getMinX(), otherIter->getMinX() - 1));
784 if (spn.
getMaxX() > otherIter->getMaxX()) {
786 spanBottom = otherIter->getMaxX() + 1;
790 tempVec.push_back(
Span(spn.
getY(), spanBottom,
795 if (spn.
getMaxX() > otherIter->getMaxX()){
797 spanBottom = otherIter->getMaxX() + 1;
801 if (otherIter->getMaxX() > spn.
getMaxX()) {
815 tempVec.push_back(
Span(spn));
818 return std::make_shared<SpanSet>(
std::move(tempVec));
829 tempVec.
insert(tempVec.
end(), _spanVector.begin(), _spanVector.end());
832 return std::make_shared<SpanSet>(
std::move(tempVec));
854 Box2I newBBoxI(newBBoxD);
863 int const xSource =
std::floor(0.5 + p.getX());
864 int const ySource =
std::floor(0.5 + p.getY());
880 return std::make_shared<SpanSet>(
std::move(tempVec));
893 for (
auto const & tc: toPoints) {
897 Box2I newBBoxI(newBBoxD);
908 newBoxPoints.
clear();
913 auto oldBoxPointIter = oldBoxPoints.cbegin();
914 for (
int x = newBBoxI.
getBeginX(); x < newBBoxI.
getEndX(); ++
x, ++oldBoxPointIter) {
915 auto p = *oldBoxPointIter;
916 int const xSource =
std::floor(0.5 + p.getX());
917 int const ySource =
std::floor(0.5 + p.getY());
933 return std::make_shared<SpanSet>(
std::move(tempVec));
936 template <
typename ImageT>
944 auto setterFunc = [](
Point2I const& point, ImageT& out, ImageT in) { out = in; };
948 tmpSpan->applyFunctor(setterFunc, image, val);
954 "Footprint Bounds Outside image, set doClip to true");
958 template <
typename T>
961 auto targetArray = target.
getArray();
962 auto xy0 = target.
getBBox().getMin();
964 T bitmask) { maskVal |= bitmask; };
968 template <
typename T>
971 auto targetArray = target.
getArray();
972 auto xy0 = target.
getBBox().getMin();
973 auto clearMaskFunctor = [](
Point2I const& point,
975 T bitmask) { maskVal &= ~bitmask; };
979 template <
typename T>
981 return maskIntersect<T, false>(*
this,
other, bitmask);
984 template <
typename T>
986 return maskIntersect<T, true>(*
this,
other, bitmask);
989 template <
typename T>
991 auto comparator = [bitmask](T pixelValue) {
return (pixelValue & bitmask); };
992 auto spanSetFromMask =
fromMask(other, comparator);
993 return union_(*spanSetFromMask);
998 class SpanSetPersistenceHelper {
1005 static SpanSetPersistenceHelper
const&
get() {
1006 static SpanSetPersistenceHelper instance;
1011 SpanSetPersistenceHelper(
const SpanSetPersistenceHelper&) =
delete;
1012 SpanSetPersistenceHelper&
operator=(
const SpanSetPersistenceHelper&) =
delete;
1015 SpanSetPersistenceHelper(SpanSetPersistenceHelper&&) =
delete;
1016 SpanSetPersistenceHelper&
operator=(SpanSetPersistenceHelper&&) =
delete;
1019 SpanSetPersistenceHelper()
1021 spanY(spanSetSchema.addField<
int>(
"y",
"The row of the span",
"pixel")),
1022 spanX0(spanSetSchema.addField<
int>(
"x0",
"First column of span (inclusive)",
"pixel")),
1023 spanX1(spanSetSchema.addField<
int>(
"x1",
"Second column of span (inclusive)",
"pixel")) {
1024 spanSetSchema.getCitizen().markPersistent();
1028 std::string getSpanSetPersistenceName() {
return "SpanSet"; }
1030 class SpanSetFactory :
public table::io::PersistableFactory {
1033 CatalogVector
const& catalogs)
const {
1037 auto spansCatalog = catalogs.front();
1039 auto const&
keys = SpanSetPersistenceHelper::get();
1042 tempVec.
reserve(spansCatalog.size());
1043 for (
auto const& val : spansCatalog) {
1046 return std::make_shared<SpanSet>(
std::move(tempVec));
1048 explicit SpanSetFactory(
std::string const&
name) : table::io::PersistableFactory(name) {}
1053 SpanSetFactory registration(getSpanSetPersistenceName());
1057 std::string SpanSet::getPersistenceName()
const {
return getSpanSetPersistenceName(); }
1060 auto const&
keys = SpanSetPersistenceHelper::get();
1061 auto spanCat = handle.makeCatalog(
keys.spanSetSchema);
1062 spanCat.reserve(
size());
1063 for (
auto const& val : *
this) {
1064 auto record = spanCat.addNew();
1065 record->set(
keys.spanY, val.getY());
1066 record->set(
keys.spanX0, val.getX0());
1067 record->set(
keys.spanX1, val.getX1());
1069 handle.saveCatalog(spanCat);
1074 #define INSTANTIATE_IMAGE_TYPE(T) \ 1075 template void SpanSet::setImage<T>(image::Image<T> & image, T val, \ 1076 geom::Box2I const& region = geom::Box2I(), bool doClip = false) \ 1079 #define INSTANTIATE_MASK_TYPE(T) \ 1080 template void SpanSet::setMask<T>(image::Mask<T> & target, T bitmask) const; \ 1081 template void SpanSet::clearMask<T>(image::Mask<T> & target, T bitmask) const; \ 1082 template std::shared_ptr<SpanSet> SpanSet::intersect<T>(image::Mask<T> const& other, T bitmask) const; \ 1083 template std::shared_ptr<SpanSet> SpanSet::intersectNot<T>(image::Mask<T> const& other, T bitmask) \ 1085 template std::shared_ptr<SpanSet> SpanSet::union_<T>(image::Mask<T> const& other, T bitmask) const; An ellipse core with quadrupole moments as parameters.
std::shared_ptr< SpanSet > union_(SpanSet const &other) const
Create a new SpanSet that contains all points from two SpanSets.
std::shared_ptr< geom::SpanSet > findEdgePixels() const
Select pixels within the SpanSet which touch its edge.
std::vector< std::shared_ptr< geom::SpanSet > > split() const
Split a discontinuous SpanSet into multiple SpanSets which are contiguous.
bool operator!=(SpanSet const &other) const
Stencil
An enumeration class which describes the shapes.
std::shared_ptr< SpanSet > intersect(SpanSet const &other) const
Determine the common points between two SpanSets, and create a new SpanSet.
bool isEmpty() const
Return true if the box contains no points.
Key< Flag > const & target
bool contains(SpanSet const &other) const
Check if a SpanSet instance entirely contains another SpanSet.
const_iterator end() const
geom::Box2I getBBox(ImageOrigin origin=PARENT) const
static std::shared_ptr< geom::SpanSet > fromMask(image::Mask< T > const &mask, UnaryPredicate comparator=details::AnyBitSetFunctor< T >())
Create a SpanSet from a mask.
#define LSST_ARCHIVE_ASSERT(EXPR)
An assertion macro used to validate the structure of an InputArchive.
A compact representation of a collection of pixels.
A range of pixels within one row of an Image.
void applyFunctor(Functor &&func, Args &&... args) const
Apply functor on individual elements from the supplied parameters.
std::shared_ptr< SpanSet > shiftedBy(int x, int y) const
Return a new SpanSet shifted by specified amount.
std::shared_ptr< SpanSet > dilated(int r, Stencil s=Stencil::CIRCLE) const
Perform a set dilation operation, and return a new object.
typename ndarray::Array< T, N, C >::Reference::Reference Reference
const_iterator begin() const
std::shared_ptr< SpanSet > intersectNot(SpanSet const &other) const
Determine the common points between a SpanSet and the logical inverse of a second SpanSet and return ...
std::shared_ptr< SpanSet > clippedTo(Box2I const &box) const
Return a new SpanSet which has all pixel values inside specified box.
ItemVariant const * other
bool isContiguous() const
Defines if the SpanSet is simply contiguous.
ellipses::Quadrupole computeShape() const
Compute the shape parameters for the distribution of points in the SpanSet.
An integer coordinate rectangle.
int getX0() const
Return the starting x-value.
bool overlaps(Box2I const &other) const
Return true if any points in other are also in this.
A base class for image defects.
table::Schema spanSetSchema
Point< double, 2 > Point2D
Represent a 2-dimensional array of bitmask pixels.
void setImage(image::Image< ImageT > &image, ImageT val, geom::Box2I const ®ion=geom::Box2I(), bool doClip=false) const
Set the values of an Image at points defined by the SpanSet.
void include(Point2D const &point)
Expand this to ensure that this->contains(point).
const_reference front() const
def run(suite, exit=True)
details::ImageNdGetter< T, inA, inB > ndImage(ndarray::Array< T, inA, inB > const &array, afwGeom::Point2I xy0=afwGeom::Point2I())
Marks a ndarray to be interpreted as an image when applying a functor from a SpanSet.
#define LSST_EXCEPT(type,...)
SpanSet()
Default constructor.
static std::shared_ptr< geom::SpanSet > fromShape(int r, Stencil s=Stencil::CIRCLE, Point2I offset=Point2I())
Factory function for creating SpanSets from a Stencil.
size_type getArea() const
Return the number of pixels in the SpanSet.
std::vector< Point2I > getCorners() const
Get the corner points.
void setMask(lsst::afw::image::Mask< T > &target, T bitmask) const
Set a Mask at pixels defined by the SpanSet.
int getMaxX() const
Maximum x-value.
Point2D computeCentroid() const
Compute the point about which the SpanSet's first moment is zero.
int getX1() const
Return the ending x-value.
const_reference back() const
io::OutputArchiveHandle OutputArchiveHandle
void clearMask(lsst::afw::image::Mask< T > &target, T bitmask) const
Unset a Mask at pixels defined by the SpanSet.
int getY() const
Return the y-value.
SpanSet & operator=(SpanSet const &)=default
#define INSTANTIATE_MASK_TYPE(T)
bool overlaps(SpanSet const &other) const
Specifies if this SpanSet overlaps with another SpanSet.
std::shared_ptr< SpanSet > transformedBy(LinearTransform const &t) const
Return a new SpanSet who's pixels are the product of applying the specified transformation.
bool contains(int x) const
A floating-point coordinate rectangle geometry.
int getMinX() const
Minimum x-value.
#define INSTANTIATE_IMAGE_TYPE(T)
bool operator==(SpanSet const &other) const
Compute equality between two SpanSets.
PixelConstReference get0(int x, int y) const
A class to represent a 2-dimensional array of pixels.
std::shared_ptr< SpanSet > eroded(int r, Stencil s=Stencil::CIRCLE) const
Perform a set erosion, and return a new object.
Box2I getBBox() const
Return a new integer box which is the minimum size to contain the pixels.
T emplace_back(T... args)