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;
158 }
else if (started) {
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;
317 void SpanSet::_label(
320 auto currentIndex = spn.
getY();
321 if (currentIndex > 0) {
323 for (
auto const& tup : sortMap[currentIndex - 1]) {
324 if (!labelVector[tup.first] && spansOverlap(spn, *(tup.second),
false)) {
325 labelVector[tup.first] = currentLabel;
326 _label(*(tup.second), labelVector, currentLabel, sortMap);
330 if (currentIndex <= _spanVector.back().getY() - 1) {
332 for (
auto& tup : sortMap[currentIndex + 1]) {
333 if (!labelVector[tup.first] && spansOverlap(spn, *(tup.second),
false)) {
334 labelVector[tup.first] = currentLabel;
335 _label(*(tup.second), labelVector, currentLabel, sortMap);
348 for (
auto const& spn : _spanVector) {
352 for (
auto const& currentSpan : _spanVector) {
353 if (!labelVector[index]) {
354 labelVector[index] = currentLabel;
355 _label(currentSpan, labelVector, currentLabel, sortMap);
370 auto labeledPair = _makeLabels();
375 return labeledPair.second <= 2;
379 auto labeledPair = _makeLabels();
380 auto labels = labeledPair.first;
381 auto numberOfLabels = labeledPair.second;
389 if (numberOfLabels == 1) {
390 subRegions.
push_back(std::make_shared<SpanSet>());
393 subRegions.
reserve(numberOfLabels - 1);
397 for (
std::size_t i = 0; i < _spanVector.size(); ++i) {
398 subSpanLists[labels[i] - 1].
push_back(_spanVector[i]);
401 for (
std::size_t i = 0; i < numberOfLabels - 1; ++i) {
402 subRegions.
push_back(std::make_shared<SpanSet>(subSpanLists[i]));
409 setMask(tempMask, static_cast<image::MaskPixel>(1));
411 erodedSpanSet->clearMask(tempMask, static_cast<image::MaskPixel>(1));
417 return makeShift(x, y);
422 return makeShift(offset.getX(), offset.getY());
428 tempVec.
reserve(_spanVector.size());
429 for (
auto const& spn : _spanVector) {
432 return std::make_shared<SpanSet>(
std::move(tempVec),
false);
440 for (
auto const& spn : _spanVector) {
447 return std::make_shared<SpanSet>(
std::move(tempVec),
false);
452 for (
auto const& otherSpan : other) {
453 for (
auto const& spn : _spanVector) {
454 if (spansOverlap(otherSpan, spn)) {
468 for (
auto const& otherSpn : other) {
470 for (
auto const& spn : _spanVector) {
490 for (
auto& spn : _spanVector) {
501 double xc = 0, yc = 0;
502 for (
auto const& spn : _spanVector) {
503 int const y = spn.
getY();
506 int const npix = x1 - x0 + 1;
509 xc += npix * 0.5 * (x1 + x0);
520 double const xc = cen.getX();
521 double const yc = cen.getY();
523 double sumxx = 0, sumxy = 0, sumyy = 0;
524 for (
auto const& spn : _spanVector) {
525 int const y = spn.
getY();
526 int const x0 = spn.
getX0();
527 int const x1 = spn.
getX1();
528 int const npix = x1 - x0 + 1;
530 for (
int x = x0; x <= x1; ++
x) {
531 sumxx += (x - xc) * (x - xc);
533 sumxy += npix * (0.5 * (x1 + x0) - xc) * (y - yc);
534 sumyy += npix * (y - yc) * (y - yc);
544 return dilated(*stencilToSpanSet);
549 if (other.
size() == 0) {
550 return std::make_shared<SpanSet>(_spanVector.begin(), _spanVector.end(),
false);
556 for (
auto const& spn : _spanVector) {
557 for (
auto const& otherSpn : other) {
558 int const xmin = spn.
getMinX() + otherSpn.getMinX();
559 int const xmax = spn.
getMaxX() + otherSpn.getMaxX();
560 int const yval = spn.
getY() + otherSpn.getY();
561 tempVec.push_back(
Span(yval, xmin, xmax));
565 return std::make_shared<SpanSet>(
std::move(tempVec));
572 return eroded(*stencilToSpanSet);
577 if (other.
size() == 0 || this->
size() == 0) {
578 return std::make_shared<SpanSet>(_spanVector.begin(), _spanVector.end(),
false);
586 for (
auto const& spn : _spanVector) {
588 for (
auto const& otherSpn : other) {
589 if ((otherSpn.getMaxX() - otherSpn.getMinX()) <= (spn.
getMaxX() - spn.
getMinX())) {
592 int y = spn.
getY() - otherSpn.getY();
603 for (
int y = primaryRuns.
front().y; y <= primaryRuns.
back().y; ++
y) {
611 auto otherYRange = other.
back().getY() - other.
front().getY() + 1;
612 if (
std::distance(yRange.first, yRange.second) < otherYRange) {
623 for (
int m = 0;
m < otherYRange; ++
m) {
624 auto mRange =
std::equal_range(yRange.first, yRange.second,
m, ComparePrimaryRunM());
625 if ((mRange.first == mRange.second)) {
632 int startX = mRange.first->xmin;
633 int endX = mRange.first->xmax;
634 for (
auto run = mRange.first + 1;
run != mRange.second; ++
run) {
635 if (
run->xmin > endX) {
637 candidateRuns.
push_back(PrimaryRun{
m,
y, startX, endX});
645 candidateRuns.
push_back(PrimaryRun{
m,
y, startX, endX});
654 for (
auto& good : goodRuns) {
655 for (
auto& cand : candidateRuns) {
656 int start =
std::max(good.xmin, cand.xmin);
667 for (
auto&
run : goodRuns) {
671 return std::make_shared<SpanSet>(
std::move(tempVec));
676 return _spanVector == other._spanVector;
681 return _spanVector != other._spanVector;
690 for (
auto dy = -r; dy <= r; ++dy) {
691 int dx =
static_cast<int>(sqrt(r * r - dy * dy));
692 tempVec.
push_back(
Span(dy + offset.getY(), -dx + offset.getX(), dx + offset.getX()));
696 for (
auto dy = -r; dy <= r; ++dy) {
697 int dx = r - abs(dy);
698 tempVec.
push_back(
Span(dy + offset.getY(), -dx + offset.getX(), dx + offset.getX()));
702 for (
auto dy = -r; dy <= r; ++dy) {
704 tempVec.
push_back(
Span(dy + offset.getY(), -dx + offset.getX(), dx + offset.getX()));
708 return std::make_shared<SpanSet>(
std::move(tempVec),
false);
713 return std::make_shared<SpanSet>(pr.
begin(), pr.
end());
719 return std::make_shared<SpanSet>();
722 if (other == *
this) {
723 return std::make_shared<SpanSet>(this->_spanVector);
726 auto otherIter = other.
begin();
727 for (
auto const& spn : _spanVector) {
728 while (otherIter != other.
end() && otherIter->getY() <= spn.
getY()) {
729 if (spansOverlap(spn, *otherIter)) {
732 auto newSpan =
Span(spn.
getY(), newMin, newMax);
733 tempVec.push_back(newSpan);
738 return std::make_shared<SpanSet>(
std::move(tempVec));
744 return std::make_shared<SpanSet>(this->
begin(), this->
end());
747 if (other == *
this) {
748 return std::make_shared<SpanSet>();
757 auto otherIter = other.
begin();
758 for (
auto const& spn : _spanVector) {
760 bool spanStarted =
false;
762 while (otherIter != other.
end() && otherIter->getY() <= spn.
getY()) {
763 if (spansOverlap(spn, *otherIter)) {
779 if (spn.
getMinX() < otherIter->getMinX()) {
780 tempVec.push_back(
Span(spn.
getY(), spn.
getMinX(), otherIter->getMinX() - 1));
786 if (spn.
getMaxX() > otherIter->getMaxX()) {
788 spanBottom = otherIter->getMaxX() + 1;
797 if (spn.
getMaxX() > otherIter->getMaxX()) {
799 spanBottom = otherIter->getMaxX() + 1;
803 if (otherIter->getMaxX() > spn.
getMaxX()) {
817 tempVec.push_back(
Span(spn));
820 return std::make_shared<SpanSet>(
std::move(tempVec));
831 tempVec.
insert(tempVec.
end(), _spanVector.begin(), _spanVector.end());
834 return std::make_shared<SpanSet>(
std::move(tempVec));
857 for (
auto const& tc : toPoints) {
872 newBoxPoints.
clear();
877 auto oldBoxPointIter = oldBoxPoints.cbegin();
878 for (
int x = newBBoxI.
getBeginX(); x < newBBoxI.
getEndX(); ++
x, ++oldBoxPointIter) {
879 auto p = *oldBoxPointIter;
880 int const xSource =
std::floor(0.5 + p.getX());
881 int const ySource =
std::floor(0.5 + p.getY());
897 return std::make_shared<SpanSet>(
std::move(tempVec));
900 template <
typename ImageT>
909 auto setterFunc = [](
lsst::geom::Point2I const& point, ImageT& out, ImageT in) { out = in; };
913 tmpSpan->applyFunctor(setterFunc, image, val);
919 "Footprint Bounds Outside image, set doClip to true");
923 template <
typename T>
926 auto targetArray = target.
getArray();
930 T bitmask) { maskVal |= bitmask; };
934 template <
typename T>
937 auto targetArray = target.
getArray();
941 T bitmask) { maskVal &= ~bitmask; };
945 template <
typename T>
947 return maskIntersect<T, false>(*
this,
other, bitmask);
950 template <
typename T>
952 return maskIntersect<T, true>(*
this,
other, bitmask);
955 template <
typename T>
957 auto comparator = [bitmask](T pixelValue) {
return (pixelValue & bitmask); };
958 auto spanSetFromMask =
fromMask(other, comparator);
959 return union_(*spanSetFromMask);
964 class SpanSetPersistenceHelper {
971 static SpanSetPersistenceHelper
const&
get() {
972 static SpanSetPersistenceHelper instance;
977 SpanSetPersistenceHelper(
const SpanSetPersistenceHelper&) =
delete;
978 SpanSetPersistenceHelper&
operator=(
const SpanSetPersistenceHelper&) =
delete;
981 SpanSetPersistenceHelper(SpanSetPersistenceHelper&&) =
delete;
982 SpanSetPersistenceHelper&
operator=(SpanSetPersistenceHelper&&) =
delete;
985 SpanSetPersistenceHelper()
987 spanY(spanSetSchema.addField<
int>(
"y",
"The row of the span",
"pixel")),
988 spanX0(spanSetSchema.addField<
int>(
"x0",
"First column of span (inclusive)",
"pixel")),
989 spanX1(spanSetSchema.addField<
int>(
"x1",
"Second column of span (inclusive)",
"pixel")) {
990 spanSetSchema.getCitizen().markPersistent();
994 std::string getSpanSetPersistenceName() {
return "SpanSet"; }
996 class SpanSetFactory :
public table::io::PersistableFactory {
999 CatalogVector
const& catalogs)
const {
1003 auto spansCatalog = catalogs.front();
1005 auto const&
keys = SpanSetPersistenceHelper::get();
1008 tempVec.
reserve(spansCatalog.size());
1009 for (
auto const& val : spansCatalog) {
1012 return std::make_shared<SpanSet>(
std::move(tempVec));
1014 explicit SpanSetFactory(
std::string const&
name) : table::io::PersistableFactory(name) {}
1019 SpanSetFactory registration(getSpanSetPersistenceName());
1023 std::string SpanSet::getPersistenceName()
const {
return getSpanSetPersistenceName(); }
1026 auto const&
keys = SpanSetPersistenceHelper::get();
1027 auto spanCat = handle.makeCatalog(
keys.spanSetSchema);
1028 spanCat.reserve(
size());
1029 for (
auto const& val : *
this) {
1030 auto record = spanCat.addNew();
1031 record->set(
keys.spanY, val.getY());
1032 record->set(
keys.spanX0, val.getX0());
1033 record->set(
keys.spanX1, val.getX1());
1035 handle.saveCatalog(spanCat);
1040 #define INSTANTIATE_IMAGE_TYPE(T) \ 1041 template void SpanSet::setImage<T>(image::Image<T> & image, T val, \ 1042 lsst::geom::Box2I const& region = lsst::geom::Box2I(), \ 1043 bool doClip = false) const; 1045 #define INSTANTIATE_MASK_TYPE(T) \ 1046 template void SpanSet::setMask<T>(image::Mask<T> & target, T bitmask) const; \ 1047 template void SpanSet::clearMask<T>(image::Mask<T> & target, T bitmask) const; \ 1048 template std::shared_ptr<SpanSet> SpanSet::intersect<T>(image::Mask<T> const& other, T bitmask) const; \ 1049 template std::shared_ptr<SpanSet> SpanSet::intersectNot<T>(image::Mask<T> const& other, T bitmask) \ 1051 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.
Key< Flag > const & target
bool contains(SpanSet const &other) const
Check if a SpanSet instance entirely contains another SpanSet.
const_iterator end() const
Point2I const getMin() 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
static std::shared_ptr< geom::SpanSet > fromShape(int r, Stencil s=Stencil::CIRCLE, lsst::geom::Point2I offset=lsst::geom::Point2I())
Factory function for creating SpanSets from a Stencil.
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 ...
ItemVariant const * other
bool isContiguous() const
Defines if the SpanSet is simply contiguous.
std::vector< Point2I > getCorners() const
ellipses::Quadrupole computeShape() const
Compute the shape parameters for the distribution of points in the SpanSet.
void setImage(image::Image< ImageT > &image, ImageT val, lsst::geom::Box2I const ®ion=lsst::geom::Box2I(), bool doClip=false) const
Set the values of an Image at points defined by the SpanSet.
lsst::geom::Point2D computeCentroid() const
Compute the point about which the SpanSet's first moment is zero.
Point< double, 2 > Point2D
lsst::geom::Box2I getBBox() const
Return a new integer box which is the minimum size to contain the pixels.
int getX0() const
Return the starting x-value.
Relationship invert(Relationship r)
A base class for image defects.
table::Schema spanSetSchema
lsst::geom::Box2I getBBox(ImageOrigin origin=PARENT) const
Represent a 2-dimensional array of bitmask pixels.
const_reference front() const
def run(suite, exit=True)
#define LSST_EXCEPT(type,...)
SpanSet()
Default constructor.
size_type getArea() const
Return the number of pixels in the SpanSet.
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.
int getX1() const
Return the ending x-value.
bool overlaps(Box2I const &other) const
const_reference back() const
io::OutputArchiveHandle OutputArchiveHandle
std::shared_ptr< SpanSet > clippedTo(lsst::geom::Box2I const &box) const
Return a new SpanSet which has all pixel values inside specified box.
std::shared_ptr< SpanSet > transformedBy(lsst::geom::LinearTransform const &t) const
Return a new SpanSet who's pixels are the product of applying the specified transformation.
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.
bool contains(int x) const
void include(Point2D const &point)
details::ImageNdGetter< T, inA, inB > ndImage(ndarray::Array< T, inA, inB > const &array, lsst::geom::Point2I xy0=lsst::geom::Point2I())
Marks a ndarray to be interpreted as an image when applying a functor from a SpanSet.
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.
std::shared_ptr< TransformPoint2ToPoint2 > makeTransform(lsst::geom::AffineTransform const &affine)
Wrap an lsst::geom::AffineTransform as a Transform.
T emplace_back(T... args)