25 #ifndef LSST_AFW_GEOM_SPANSET_H 26 #define LSST_AFW_GEOM_SPANSET_H 33 #include "lsst/pex/exceptions.h" 53 bool operator()(T pixelValue) {
return pixelValue != 0; }
86 const_iterator
begin()
const {
return _spanVector.cbegin(); }
87 const_iterator
end()
const {
return _spanVector.cend(); }
88 const_iterator
cbegin()
const {
return _spanVector.cbegin(); }
89 const_iterator
cend()
const {
return _spanVector.cend(); }
90 const_reference
front()
const {
return const_cast<geom::Span &
>(_spanVector.front()); }
91 const_reference
back()
const {
return const_cast<geom::Span &
>(_spanVector.back()); }
92 size_type
size()
const {
return _spanVector.size(); }
93 bool empty()
const {
return _spanVector.empty(); }
120 template <
typename iter>
121 SpanSet(iter begin, iter
end,
bool normalize =
true) : _spanVector(begin, end) {
123 if (_spanVector.size() == 0) {
145 explicit SpanSet(std::vector<Span>
const &vec,
bool normalize =
true);
158 explicit SpanSet(std::vector<Span> &&vec,
bool normalize =
true);
167 size_type getArea()
const;
171 Box2I getBBox()
const;
180 bool isContiguous()
const;
187 std::shared_ptr<SpanSet> shiftedBy(
int x,
int y)
const;
193 std::shared_ptr<SpanSet> shiftedBy(
Extent2I const &offset)
const;
199 std::shared_ptr<SpanSet> clippedTo(
Box2I const &box)
const;
205 std::shared_ptr<SpanSet> transformedBy(
LinearTransform const &t)
const;
211 std::shared_ptr<SpanSet> transformedBy(
AffineTransform const &t)
const;
217 std::shared_ptr<SpanSet> transformedBy(
XYTransform const &t)
const;
223 bool overlaps(
SpanSet const &other)
const;
229 bool contains(
SpanSet const &other)
const;
235 bool contains(
Point2I const &point)
const;
239 Point2D computeCentroid()
const;
261 std::shared_ptr<SpanSet> dilated(
SpanSet const &other)
const;
279 std::shared_ptr<SpanSet> eroded(
SpanSet const &other)
const;
295 template <
typename Pixel,
int inN,
int inC>
296 ndarray::Array<typename std::remove_const<Pixel>::type, inN - 1, inN - 1>
flatten(
297 ndarray::Array<Pixel, inN, inC>
const &input,
Point2I const &xy0 =
Point2I())
const {
299 auto outputShape = ndarray::concatenate(ndarray::makeVector(getArea()),
300 input.getShape().template last<inN - 2>());
301 ndarray::Array<typename std::remove_const<Pixel>::type, inN - 1, inN - 1> outputArray =
302 ndarray::allocate(outputShape);
303 outputArray.deep() = 0;
304 flatten(outputArray, input, xy0);
327 template <
typename PixelIn,
typename PixelOut,
int inA,
int outC,
int inC>
328 void flatten(ndarray::Array<PixelOut, inA - 1, outC>
const &output,
329 ndarray::Array<PixelIn, inA, inC>
const &input,
Point2I const &xy0 =
Point2I())
const {
330 auto ndAssigner = [](
Point2I const &point,
350 template <
typename Pixel,
int inA,
int inC>
351 ndarray::Array<typename std::remove_const<Pixel>::type, inA + 1, inA + 1>
unflatten(
352 ndarray::Array<Pixel, inA, inC>
const &input)
const {
356 auto existingShape = input.getShape();
357 typename decltype(existingShape)::Element height = _bbox.getHeight();
358 typename decltype(existingShape)::Element width = _bbox.getWidth();
359 auto outputShape = ndarray::concatenate(ndarray::makeVector(height, width),
360 input.getShape().template last<inA - 1>());
361 ndarray::Array<typename std::remove_const<Pixel>::type, inA + 1, inA + 1> outputArray =
362 ndarray::allocate(outputShape);
363 outputArray.deep() = 0;
364 unflatten(outputArray, input,
Point2I(_bbox.getMinX(), _bbox.getMinY()));
387 template <
typename PixelIn,
typename PixelOut,
int inA,
int outC,
int inC>
388 void unflatten(ndarray::Array<PixelOut, inA + 1, outC>
const &output,
389 ndarray::Array<PixelIn, inA, inC>
const &input,
Point2I const &xy0 =
Point2I())
const {
392 auto ndAssigner = [](
Point2I const &point,
405 template <
typename ImageT>
410 applyFunctor(copyFunc, src, dest);
422 template <
typename ImageT,
typename MaskT,
typename VarT>
426 VarT
const &srcVar, ImageT &destPix, MaskT &destMask, VarT &destVar) {
448 template <
typename ImageT>
450 bool doClip =
false)
const;
497 template <
typename Functor,
typename... Args>
521 template <
typename T>
531 template <
typename T>
539 std::shared_ptr<SpanSet> intersect(
SpanSet const &other)
const;
548 template <
typename T>
549 std::shared_ptr<SpanSet> intersect(
image::Mask<T> const &other, T bitmask)
const;
556 std::shared_ptr<SpanSet> intersectNot(
SpanSet const &other)
const;
566 template <
typename T>
567 std::shared_ptr<SpanSet> intersectNot(
image::Mask<T> const &other, T bitmask)
const;
573 std::shared_ptr<SpanSet> union_(
SpanSet const &other)
const;
582 template <
typename T>
583 std::shared_ptr<SpanSet> union_(
image::Mask<T> const &other, T bitmask)
const;
633 template <
typename T,
typename UnaryPredicate = details::AnyBitSetFunctor<T>>
637 std::vector<Span> tempVec;
640 auto const maskArray = mask.
getArray();
641 auto const minPoint = mask.
getBBox().getMin();
643 auto const minY = minPoint.getY();
644 auto const minX = minPoint.getX();
650 std::size_t runCounter = 0;
651 auto arrIter = maskArray.begin();
652 for (
size_t y = 0; y < yDim; ++y) {
655 auto yWithOffset = y + minY;
656 for (
size_t x = 0; x < xDim; ++
x) {
660 auto compareValue = comparator((*arrIter)[x]);
661 runCounter += compareValue;
666 if (!compareValue && runCounter) {
667 tempVec.push_back(
Span(yWithOffset, x - runCounter + minX, x - 1 + minX));
676 Span(yWithOffset, dimMinusOne - (runCounter - 1) + minX, dimMinusOne + minX));
682 return std::make_shared<SpanSet>(std::move(tempVec),
false);
694 template <
typename T>
696 return fromMask(mask, [bitmask](T
const &bitPattern) {
return bitPattern & bitmask; });
701 std::vector<std::shared_ptr<geom::SpanSet>> split()
const;
709 std::shared_ptr<geom::SpanSet> findEdgePixels()
const;
714 std::string getPersistenceName()
const override;
718 inline std::string getPythonModule()
const override {
return "lsst.afw.geom"; }
726 friend class SpansSetFactory;
730 void _runNormalize();
739 void _label(
geom::Span const &spn, std::vector<std::size_t> &labelVector, std::size_t currentLabel,
740 std::unordered_map<
int, std::vector<std::pair<std::size_t, const Span *>>> &sortVector)
const;
741 std::pair<std::vector<std::size_t>, std::size_t> _makeLabels()
const;
743 std::shared_ptr<SpanSet> makeShift(
int x,
int y)
const;
745 template <
typename F,
typename... T>
746 void applyFunctorImpl(F &&f, T... args)
const {
753 for (
auto const &spn : _spanVector) {
756 for (
int x = spn.getX0(); x <= spn.getX1(); ++
x) {
758 f(point, args.get()...);
765 std::vector<Span> _spanVector;
777 #endif // LSST_AFW_GEOM_SPANSET_H An ellipse core with quadrupole moments as parameters.
VariancePtr getVariance() const
Return a (shared_ptr to) the MaskedImage's variance.
void variadicSpanSetter(Span const spn, T &x)
void unflatten(ndarray::Array< PixelOut, inA+1, outC > const &output, ndarray::Array< PixelIn, inA, inC > const &input, Point2I const &xy0=Point2I()) const
Expand an array by one spatial dimension at points given by SpanSet.
Stencil
An enumeration class which describes the shapes.
bool operator!=(Coord const &lhs, Coord const &rhs)
Inequality; the complement of equality.
const_iterator end() const
geom::Box2I getBBox(ImageOrigin origin=PARENT) const
An object passed to Persistable::write to allow it to persist itself.
static std::shared_ptr< geom::SpanSet > fromMask(image::Mask< T > const &mask, UnaryPredicate comparator=details::AnyBitSetFunctor< T >())
Create a SpanSet from a mask.
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.
typename ndarray::Array< T, N, C >::Reference::Reference Reference
const_iterator cbegin() const
bool operator==(CoordKey const &lhs, CoordKey const &rhs)
Compare CoordKeys for equality using the constituent Keys.
typename ndarray::Array< T, inA, inC >::Reference Reference
const_iterator begin() const
void copyImage(image::Image< ImageT > const &src, image::Image< ImageT > &dest)
Copy contents of source Image into destination image at the positions defined in the SpanSet...
static std::shared_ptr< geom::SpanSet > fromMask(image::Mask< T > const &mask, T bitmask)
Create a SpanSet from a mask.
An integer coordinate rectangle.
ImagePtr getImage() const
Return a (shared_ptr to) the MaskedImage's image.
A base class for objects that can be persisted via afw::table::io Archive classes.
void copyMaskedImage(image::MaskedImage< ImageT, MaskT, VarT > const &src, image::MaskedImage< ImageT, MaskT, VarT > &dest)
Copy contents of source MaskedImage into destination image at the positions defined in the SpanSet...
value_type const & const_reference
afw::table::PointKey< int > dimensions
const_iterator cend() const
A base class for image defects.
Represent a 2-dimensional array of bitmask pixels.
An ellipse defined by an arbitrary BaseCore and a center point.
A class to manipulate images, masks, and variance as a single object.
details::FlatNdGetter< T, inA, inB > ndFlat(ndarray::Array< T, inA, inB > const &array)
Marks a ndarray to be interpreted as a 1D vector when applying a functor from a SpanSet.
FlatNdGetter< T, inA, inC > makeGetter(FlatNdGetter< T, inA, inC > &getter)
void flatten(ndarray::Array< PixelOut, inA - 1, outC > const &output, ndarray::Array< PixelIn, inA, inC > const &input, Point2I const &xy0=Point2I()) const
Reduce the pixel dimensionality from 2 to 1 of an array at points given by SpanSet.
const_reference front() const
SpanSet(iter begin, iter end, bool normalize=true)
Construct a SpanSet from an iterator.
void variadicBoundChecker(Box2I const box, int area, T const &x)
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.
ndarray::Array< typename std::remove_const< Pixel >::type, inN - 1, inN - 1 > flatten(ndarray::Array< Pixel, inN, inC > const &input, Point2I const &xy0=Point2I()) const
Reduce the pixel dimensionality from 2 to 1 of an array at points given by SpanSet.
MaskPtr getMask() const
Return a (shared_ptr to) the MaskedImage's mask.
bool operator()(T pixelValue)
std::vector< Span >::const_iterator const_iterator
void variadicIncrementPosition(T &x)
std::vector< Span >::size_type size_type
bool isPersistable() const override
Return true if this particular object can be persisted using afw::table::io.
ndarray::Array< typename std::remove_const< Pixel >::type, inA+1, inA+1 > unflatten(ndarray::Array< Pixel, inA, inC > const &input) const
Expand an array by one spatial dimension at points given by SpanSet.
const_reference back() const
Abstract base class for function objects.
A CRTP facade class for subclasses of Persistable.
A class to represent a 2-dimensional array of pixels.