24 #include "pybind11/pybind11.h"
25 #include "pybind11/stl.h"
32 #include "ndarray/pybind11.h"
39 using namespace pybind11::literals;
47 using PySpanSet = py::class_<SpanSet, std::shared_ptr<SpanSet>>;
49 template <
typename Pixel,
typename PyClass>
52 (ndarray::Array<Pixel, 1, 1>(SpanSet::*)(ndarray::Array<Pixel, 2, 0>
const &,
54 SpanSet::flatten<Pixel, 2, 0>,
57 (ndarray::Array<Pixel, 2, 2>(SpanSet::*)(ndarray::Array<Pixel, 3, 0>
const &,
59 SpanSet::flatten<Pixel, 3, 0>,
62 (
void (SpanSet::*)(ndarray::Array<Pixel, 1, 0>
const &, ndarray::Array<Pixel, 2, 0>
const &,
64 SpanSet::flatten<Pixel, Pixel, 2, 0, 0>,
67 (
void (SpanSet::*)(ndarray::Array<Pixel, 2, 0>
const &, ndarray::Array<Pixel, 3, 0>
const &,
69 SpanSet::flatten<Pixel, Pixel, 3, 0, 0>,
73 template <
typename Pixel,
typename PyClass>
76 (ndarray::Array<Pixel, 2, 2>(SpanSet::*)(ndarray::Array<Pixel, 1, 0>
const &input)
const) &
77 SpanSet::unflatten<Pixel, 1, 0>);
79 (ndarray::Array<Pixel, 3, 3>(SpanSet::*)(ndarray::Array<Pixel, 2, 0>
const &input)
const) &
80 SpanSet::unflatten<Pixel, 2, 0>);
82 (
void (SpanSet::*)(ndarray::Array<Pixel, 2, 0>
const &, ndarray::Array<Pixel, 1, 0>
const &,
84 SpanSet::unflatten<Pixel, Pixel, 1, 0, 0>,
87 (
void (SpanSet::*)(ndarray::Array<Pixel, 3, 0>
const &, ndarray::Array<Pixel, 2, 0>
const &,
89 SpanSet::unflatten<Pixel, Pixel, 2, 0, 0>,
93 template <
typename Pixel,
typename PyClass>
98 template <
typename Pixel,
typename PyClass>
103 template <
typename Pixel,
typename PyClass>
108 "other"_a,
"bitmask"_a);
112 auto tempSpanSet = SpanSet::fromMask(
mask);
113 return self.intersect(*tempSpanSet);
118 template <
typename Pixel,
typename PyClass>
119 void declareIntersectNotMethod(
PyClass &
cls) {
120 cls.def(
"intersectNot",
122 SpanSet::intersectNot,
123 "other"_a,
"bitmask"_a);
125 cls.def(
"intersectNot",
127 auto tempSpanSet = SpanSet::fromMask(
mask);
128 return self.intersectNot(*tempSpanSet);
133 template <
typename Pixel,
typename PyClass>
137 "other"_a,
"bitmask"_a);
141 auto tempSpanSet = SpanSet::fromMask(
mask);
142 return self.union_(*tempSpanSet);
147 template <
typename ImageT,
typename PyClass>
149 cls.def(
"copyImage", &SpanSet::copyImage<ImageT>);
152 template <
typename ImageT,
typename PyClass>
156 cls.def(
"copyMaskedImage", &SpanSet::copyMaskedImage<ImageT, MaskPixel, VariancePixel>);
159 template <
typename ImageT,
typename PyClass>
167 template <
typename MaskPixel,
typename PyClass>
171 return SpanSet::fromMask(
mask, bitmask);
175 template <
typename Pixel,
typename PyClass>
177 declareSetMaskMethod<Pixel>(
cls);
178 declareClearMaskMethod<Pixel>(
cls);
179 declareIntersectMethod<Pixel>(
cls);
180 declareIntersectNotMethod<Pixel>(
cls);
181 declareUnionMethod<Pixel>(
cls);
184 template <
typename Pixel,
typename PyClass>
186 declareFlattenMethod<Pixel>(
cls);
187 declareUnflattenMethod<Pixel>(
cls);
188 declareCopyImage<Pixel>(
cls);
189 declareCopyMaskedImage<Pixel>(
cls);
190 declareSetImage<Pixel>(
cls);
198 py::module::import(
"lsst.geom");
199 py::module::import(
"lsst.afw.geom.span");
201 py::enum_<Stencil>(mod,
"Stencil")
202 .value(
"CIRCLE", Stencil::CIRCLE)
203 .value(
"BOX", Stencil::BOX)
204 .value(
"MANHATTAN", Stencil::MANHATTAN);
206 PySpanSet
cls(mod,
"SpanSet");
209 cls.def(py::init<>());
210 cls.def(py::init<lsst::geom::Box2I>(),
"box"_a);
213 table::io::python::addPersistableMethods<SpanSet>(
cls);
216 cls.def(
"getArea", &SpanSet::getArea);
217 cls.def(
"getBBox", &SpanSet::getBBox);
218 cls.def(
"isContiguous", &SpanSet::isContiguous);
222 cls.def(
"clippedTo", &SpanSet::clippedTo);
223 cls.def(
"transformedBy",
225 SpanSet::transformedBy);
226 cls.def(
"transformedBy",
228 SpanSet::transformedBy);
230 SpanSet::transformedBy);
231 cls.def(
"overlaps", &SpanSet::overlaps);
234 cls.def(
"computeCentroid", &SpanSet::computeCentroid);
235 cls.def(
"computeShape", &SpanSet::computeShape);
237 "radius"_a,
"stencil"_a = Stencil::CIRCLE);
240 "radius"_a,
"stencil"_a = Stencil::CIRCLE);
243 cls.def(
"intersectNot",
246 cls.def_static(
"fromShape",
249 cls.def_static(
"fromShape",
254 cls.def_static(
"fromShape",
256 cls.def(
"split", &SpanSet::split);
257 cls.def(
"findEdgePixels", &SpanSet::findEdgePixels);
263 for (
auto const &span :
self) {
264 auto y = span.getY();
265 for (
int x = span.getX0();
x <= span.getX1(); ++
x) {
278 cls.def(
"__iter__", [](
SpanSet &
self) {
return py::make_iterator(
self.begin(),
self.
end()); },
279 py::keep_alive<0, 1>());
280 cls.def(
"__len__", [](
SpanSet const &
self) -> decltype(
self.size()) {
return self.size(); });
282 cls.def(
"__contains__",
287 self.setMask(tempMask,
static_cast<MaskPixel>(1));
289 auto dims = array.getShape();
294 if (j != dims[1] - 1) {
304 for (
auto const &span :
self) {
305 os << span.getY() <<
": " << span.getMinX() <<
".." << span.getMaxX() <<
std::endl;
311 declareMaskMethods<MaskPixel>(
cls);
313 declareImageTypes<std::uint16_t>(
cls);
314 declareImageTypes<std::uint64_t>(
cls);
315 declareImageTypes<int>(
cls);
316 declareImageTypes<float>(
cls);
317 declareImageTypes<double>(
cls);
320 declareFlattenMethod<long>(
cls);
321 declareUnflattenMethod<long>(
cls);
323 declarefromMask<MaskPixel>(
cls);