47 bool comparePrimaryRun(PrimaryRun
const&
first, PrimaryRun
const&
second) {
48 if (first.y != second.y) {
49 return first.y < second.y;
50 }
else if (first.m != second.m) {
51 return first.m < second.m;
53 return first.xmin < second.xmin;
57 class ComparePrimaryRunY {
59 bool operator()(PrimaryRun
const& pr,
int yval) {
return pr.y < yval; }
60 bool operator()(
int yval, PrimaryRun
const& pr) {
return yval < pr.y; }
63 class ComparePrimaryRunM {
65 bool operator()(PrimaryRun
const& pr,
int mval) {
return pr.m < mval; }
66 bool operator()(
int mval, PrimaryRun
const& pr) {
return mval < pr.m; }
75 bool spansOverlap(Span
const&
a, Span
const&
b,
bool compareY =
true) {
78 yTruth = a.getY() == b.getY();
80 return (yTruth && ((a.getMaxX() >= b.getMinX() && a.getMinX() <= b.getMinX()) ||
81 (b.getMaxX() >= a.getMinX() && b.getMinX() <= a.getMinX())))
93 bool spansContiguous(Span
const& a, Span
const& b,
bool compareY =
true) {
96 yTruth = a.getY() == b.getY();
98 return (yTruth && ((a.getMaxX() + 1 >= b.getMinX() && a.getMinX() <= b.getMinX()) ||
99 (b.getMaxX() + 1 >= a.getMinX() && b.getMinX() <= a.getMinX())))
115 template <
typename T,
bool invert>
119 auto maskBBox = mask.
getBBox();
120 for (
auto const & spn : spanSet) {
123 bool started =
false;
126 if (y < maskBBox.getMinY() || y > maskBBox.getMaxY()) {
133 int startX =
std::max(spn.getMinX(), maskBBox.getMinX());
134 int endX =
std::min(spn.getMaxX(), maskBBox.getMaxX());
135 for (
int x = startX;
x <= endX; ++
x){
137 bool pixelCompare = mask.
get0(
x, y) & bitmask;
141 pixelCompare = !pixelCompare;
166 return std::make_shared<SpanSet>(
std::move(newVec));
182 for (
int i = beginY; i < _bbox.
getEndY(); ++i) {
183 _spanVector.push_back(
Span(i, beginX, maxX));
190 if (_spanVector.empty()) {
204 if (_spanVector.size() == 0) {
215 void SpanSet::_runNormalize() {
221 std::sort(_spanVector.begin(), _spanVector.end());
226 newSpans.
reserve(_spanVector.size());
228 newSpans.push_back(*_spanVector.begin());
234 for (
auto iter = ++(_spanVector.begin()); iter != _spanVector.end(); ++iter) {
235 auto & newSpansEnd = newSpans.back();
236 if (spansContiguous(newSpansEnd, *iter)) {
237 newSpansEnd =
Span(newSpansEnd.getY(),
std::min(newSpansEnd.getMinX(), iter->getMinX()),
238 std::max(newSpansEnd.getMaxX(), iter->getMaxX()));
240 newSpans.push_back(
Span(*iter));
248 void SpanSet::_initialize() {
260 int minX = _spanVector[0].getMinX();
261 int maxX = _spanVector[0].getMaxX();
264 for (
const auto&
span : _spanVector) {
265 if (
span.getMinX() < minX) {
266 minX =
span.getMinX();
268 if (
span.getMaxX() > maxX) {
269 maxX =
span.getMaxX();
272 _area +=
span.getMaxX() -
span.getMinX() + 1;
274 _bbox =
Box2I(
Point2I(minX, _spanVector.front().getY()),
Point2I(maxX, _spanVector.back().getY()));
316 void SpanSet::_label(
319 auto currentIndex = spn.
getY();
320 if (currentIndex > 0) {
322 for (
auto const& tup : sortMap[currentIndex - 1]) {
323 if (!labelVector[tup.first] && spansOverlap(spn, *(tup.second),
false)) {
324 labelVector[tup.first] = currentLabel;
325 _label(*(tup.second), labelVector, currentLabel, sortMap);
329 if (currentIndex <= _spanVector.back().getY() - 1) {
331 for (
auto& tup : sortMap[currentIndex + 1]) {
332 if (!labelVector[tup.first] && spansOverlap(spn, *(tup.second),
false)) {
333 labelVector[tup.first] = currentLabel;
334 _label(*(tup.second), labelVector, currentLabel, sortMap);
347 for (
auto const& spn : _spanVector) {
351 for (
auto const& currentSpan : _spanVector) {
352 if (!labelVector[index]) {
353 labelVector[index] = currentLabel;
354 _label(currentSpan, labelVector, currentLabel, sortMap);
369 auto labeledPair = _makeLabels();
374 return labeledPair.second <= 2;
378 auto labeledPair = _makeLabels();
379 auto labels = labeledPair.first;
380 auto numberOfLabels = labeledPair.second;
388 if (numberOfLabels == 1) {
389 subRegions.
push_back(std::make_shared<SpanSet>());
392 subRegions.
reserve(numberOfLabels - 1);
396 for (
std::size_t i = 0; i < _spanVector.size(); ++i) {
397 subSpanLists[labels[i] - 1].
push_back(_spanVector[i]);
400 for (
std::size_t i = 0; i < numberOfLabels - 1; ++i) {
401 subRegions.
push_back(std::make_shared<SpanSet>(subSpanLists[i]));
408 setMask(tempMask, static_cast<image::MaskPixel>(1));
410 erodedSpanSet->clearMask(tempMask, static_cast<image::MaskPixel>(1));
416 return makeShift(x, y);
421 return makeShift(offset.getX(), offset.getY());
427 tempVec.
reserve(_spanVector.size());
428 for (
auto const& spn : _spanVector) {
431 return std::make_shared<SpanSet>(
std::move(tempVec),
false);
439 for (
auto const& spn : _spanVector) {
446 return std::make_shared<SpanSet>(
std::move(tempVec),
false);
451 for (
auto const& otherSpan : other) {
452 for (
auto const& spn : _spanVector) {
453 if (spansOverlap(otherSpan, spn)) {
467 for (
auto const& otherSpn : other) {
469 for (
auto const& spn : _spanVector) {
489 for (
auto& spn : _spanVector) {
500 double xc = 0, yc = 0;
501 for (
auto const& spn : _spanVector) {
502 int const y = spn.
getY();
505 int const npix = x1 - x0 + 1;
508 xc += npix * 0.5 * (x1 + x0);
513 return Point2D(xc / _area, yc / _area);
519 double const xc = cen.getX();
520 double const yc = cen.getY();
522 double sumxx = 0, sumxy = 0, sumyy = 0;
523 for (
auto const& spn : _spanVector) {
524 int const y = spn.
getY();
525 int const x0 = spn.
getX0();
526 int const x1 = spn.
getX1();
527 int const npix = x1 - x0 + 1;
529 for (
int x = x0; x <= x1; ++
x) {
530 sumxx += (x - xc) * (x - xc);
532 sumxy += npix * (0.5 * (x1 + x0) - xc) * (y - yc);
533 sumyy += npix * (y - yc) * (y - yc);
543 return dilated(*stencilToSpanSet);
548 if (other.
size() == 0) {
549 return std::make_shared<SpanSet>(_spanVector.begin(), _spanVector.end(),
false);
555 for (
auto const& spn : _spanVector) {
556 for (
auto const& otherSpn : other) {
557 int const xmin = spn.
getMinX() + otherSpn.getMinX();
558 int const xmax = spn.
getMaxX() + otherSpn.getMaxX();
559 int const yval = spn.
getY() + otherSpn.getY();
560 tempVec.push_back(
Span(yval, xmin, xmax));
564 return std::make_shared<SpanSet>(
std::move(tempVec));
571 return eroded(*stencilToSpanSet);
576 if (other.
size() == 0 || this->
size() == 0) {
577 return std::make_shared<SpanSet>(_spanVector.begin(), _spanVector.end(),
false);
585 for (
auto const& spn : _spanVector) {
587 for (
auto const& otherSpn : other) {
588 if ((otherSpn.getMaxX() - otherSpn.getMinX()) <= (spn.
getMaxX() - spn.
getMinX())) {
591 int y = spn.
getY() - otherSpn.getY();
602 for (
int y = primaryRuns.
front().y; y <= primaryRuns.
back().y; ++
y) {
610 auto otherYRange = other.
back().getY() - other.
front().getY() + 1;
611 if (
std::distance(yRange.first, yRange.second) < otherYRange) {
622 for (
int m = 0;
m < otherYRange; ++
m) {
623 auto mRange =
std::equal_range(yRange.first, yRange.second,
m, ComparePrimaryRunM());
624 if ((mRange.first == mRange.second)) {
631 int startX = mRange.first->xmin;
632 int endX = mRange.first->xmax;
633 for (
auto run = mRange.first + 1;
run != mRange.second; ++
run) {
634 if (
run->xmin > endX) {
636 candidateRuns.
push_back(PrimaryRun{
m,
y, startX, endX});
644 candidateRuns.
push_back(PrimaryRun{
m,
y, startX, endX});
653 for (
auto& good : goodRuns) {
654 for (
auto& cand : candidateRuns) {
655 int start =
std::max(good.xmin, cand.xmin);
666 for (
auto&
run : goodRuns) {
670 return std::make_shared<SpanSet>(
std::move(tempVec));
675 return _spanVector == other._spanVector;
680 return _spanVector != other._spanVector;
689 for (
auto dy = -r; dy <= r; ++dy) {
690 int dx =
static_cast<int>(sqrt(r * r - dy * dy));
691 tempVec.
push_back(
Span(dy + offset.getY(), -dx + offset.getX(), dx + offset.getX()));
695 for (
auto dy = -r; dy <= r; ++dy) {
696 int dx = r - abs(dy);
697 tempVec.
push_back(
Span(dy + offset.getY(), -dx + offset.getX(), dx + offset.getX()));
701 for (
auto dy = -r; dy <= r; ++dy) {
703 tempVec.
push_back(
Span(dy + offset.getY(), -dx + offset.getX(), dx + offset.getX()));
707 return std::make_shared<SpanSet>(
std::move(tempVec),
false);
712 return std::make_shared<SpanSet>(pr.
begin(), pr.
end());
718 return std::make_shared<SpanSet>();
721 if (other == *
this) {
722 return std::make_shared<SpanSet>(this->_spanVector);
725 auto otherIter = other.
begin();
726 for (
auto const& spn : _spanVector) {
727 while (otherIter != other.
end() && otherIter->getY() <= spn.
getY()) {
728 if (spansOverlap(spn, *otherIter)) {
731 auto newSpan =
Span(spn.
getY(), newMin, newMax);
732 tempVec.push_back(newSpan);
737 return std::make_shared<SpanSet>(
std::move(tempVec));
743 return std::make_shared<SpanSet>(this->
begin(), this->
end());
746 if (other == *
this) {
747 return std::make_shared<SpanSet>();
756 auto otherIter = other.
begin();
757 for (
auto const& spn : _spanVector) {
759 bool spanStarted =
false;
761 while (otherIter != other.
end() && otherIter->getY() <= spn.
getY()) {
762 if (spansOverlap(spn, *otherIter)) {
778 if (spn.
getMinX() < otherIter->getMinX()) {
779 tempVec.push_back(
Span(spn.
getY(), spn.
getMinX(), otherIter->getMinX() - 1));
785 if (spn.
getMaxX() > otherIter->getMaxX()) {
787 spanBottom = otherIter->getMaxX() + 1;
791 tempVec.push_back(
Span(spn.
getY(), spanBottom,
796 if (spn.
getMaxX() > otherIter->getMaxX()){
798 spanBottom = otherIter->getMaxX() + 1;
802 if (otherIter->getMaxX() > spn.
getMaxX()) {
816 tempVec.push_back(
Span(spn));
819 return std::make_shared<SpanSet>(
std::move(tempVec));
830 tempVec.
insert(tempVec.
end(), _spanVector.begin(), _spanVector.end());
833 return std::make_shared<SpanSet>(
std::move(tempVec));
856 for (
auto const & tc: toPoints) {
860 Box2I newBBoxI(newBBoxD);
871 newBoxPoints.
clear();
876 auto oldBoxPointIter = oldBoxPoints.cbegin();
877 for (
int x = newBBoxI.
getBeginX(); x < newBBoxI.
getEndX(); ++
x, ++oldBoxPointIter) {
878 auto p = *oldBoxPointIter;
879 int const xSource =
std::floor(0.5 + p.getX());
880 int const ySource =
std::floor(0.5 + p.getY());
896 return std::make_shared<SpanSet>(
std::move(tempVec));
899 template <
typename ImageT>
907 auto setterFunc = [](
Point2I const& point, ImageT& out, ImageT in) { out = in; };
911 tmpSpan->applyFunctor(setterFunc, image, val);
917 "Footprint Bounds Outside image, set doClip to true");
921 template <
typename T>
924 auto targetArray = target.
getArray();
925 auto xy0 = target.
getBBox().getMin();
927 T bitmask) { maskVal |= bitmask; };
931 template <
typename T>
934 auto targetArray = target.
getArray();
935 auto xy0 = target.
getBBox().getMin();
936 auto clearMaskFunctor = [](
Point2I const& point,
938 T bitmask) { maskVal &= ~bitmask; };
942 template <
typename T>
944 return maskIntersect<T, false>(*
this,
other, bitmask);
947 template <
typename T>
949 return maskIntersect<T, true>(*
this,
other, bitmask);
952 template <
typename T>
954 auto comparator = [bitmask](T pixelValue) {
return (pixelValue & bitmask); };
955 auto spanSetFromMask =
fromMask(other, comparator);
956 return union_(*spanSetFromMask);
961 class SpanSetPersistenceHelper {
968 static SpanSetPersistenceHelper
const&
get() {
969 static SpanSetPersistenceHelper instance;
974 SpanSetPersistenceHelper(
const SpanSetPersistenceHelper&) =
delete;
975 SpanSetPersistenceHelper&
operator=(
const SpanSetPersistenceHelper&) =
delete;
978 SpanSetPersistenceHelper(SpanSetPersistenceHelper&&) =
delete;
979 SpanSetPersistenceHelper&
operator=(SpanSetPersistenceHelper&&) =
delete;
982 SpanSetPersistenceHelper()
984 spanY(spanSetSchema.addField<
int>(
"y",
"The row of the span",
"pixel")),
985 spanX0(spanSetSchema.addField<
int>(
"x0",
"First column of span (inclusive)",
"pixel")),
986 spanX1(spanSetSchema.addField<
int>(
"x1",
"Second column of span (inclusive)",
"pixel")) {
987 spanSetSchema.getCitizen().markPersistent();
991 std::string getSpanSetPersistenceName() {
return "SpanSet"; }
993 class SpanSetFactory :
public table::io::PersistableFactory {
996 CatalogVector
const& catalogs)
const {
1000 auto spansCatalog = catalogs.front();
1002 auto const&
keys = SpanSetPersistenceHelper::get();
1005 tempVec.
reserve(spansCatalog.size());
1006 for (
auto const& val : spansCatalog) {
1009 return std::make_shared<SpanSet>(
std::move(tempVec));
1011 explicit SpanSetFactory(
std::string const&
name) : table::io::PersistableFactory(name) {}
1016 SpanSetFactory registration(getSpanSetPersistenceName());
1020 std::string SpanSet::getPersistenceName()
const {
return getSpanSetPersistenceName(); }
1023 auto const&
keys = SpanSetPersistenceHelper::get();
1024 auto spanCat = handle.makeCatalog(
keys.spanSetSchema);
1025 spanCat.reserve(
size());
1026 for (
auto const& val : *
this) {
1027 auto record = spanCat.addNew();
1028 record->set(
keys.spanY, val.getY());
1029 record->set(
keys.spanX0, val.getX0());
1030 record->set(
keys.spanX1, val.getX1());
1032 handle.saveCatalog(spanCat);
1037 #define INSTANTIATE_IMAGE_TYPE(T) \ 1038 template void SpanSet::setImage<T>(image::Image<T> & image, T val, \ 1039 geom::Box2I const& region = geom::Box2I(), bool doClip = false) \ 1042 #define INSTANTIATE_MASK_TYPE(T) \ 1043 template void SpanSet::setMask<T>(image::Mask<T> & target, T bitmask) const; \ 1044 template void SpanSet::clearMask<T>(image::Mask<T> & target, T bitmask) const; \ 1045 template std::shared_ptr<SpanSet> SpanSet::intersect<T>(image::Mask<T> const& other, T bitmask) const; \ 1046 template std::shared_ptr<SpanSet> SpanSet::intersectNot<T>(image::Mask<T> const& other, T bitmask) \ 1048 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)
std::shared_ptr< TransformPoint2ToPoint2 > makeTransform(AffineTransform const &affine)
Wrap an AffineTransform as a Transform.
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)