Skip to content

Commit

Permalink
Add Region.getRegions()
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthias Wittgen authored and Matthias Wittgen committed Oct 23, 2024
1 parent d98c780 commit d7018da
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 28 deletions.
1 change: 0 additions & 1 deletion include/lsst/sphgeom/CompoundRegion.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ class CompoundRegion : public Region {
}
static std::unique_ptr<CompoundRegion> decode(std::uint8_t const *buffer, size_t n);
///@}
static void flatten(Region const &region, std::vector<std::unique_ptr<Region>> &result);

protected:

Expand Down
4 changes: 2 additions & 2 deletions include/lsst/sphgeom/Region.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ class Region {
static std::unique_ptr<Region> decode(std::uint8_t const * buffer, size_t n);
///@}

/// `flatten` flattens a nested Region to a list of regions.
static void flatten(Region const &region, std::vector<std::unique_ptr<Region>> &result);
/// `getRegions` returns a vector of Region.
static std::vector<std::unique_ptr<Region>> getRegions(Region const &region);
///@}
};

Expand Down
6 changes: 1 addition & 5 deletions python/lsst/sphgeom/_region.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,7 @@ void defineClass(py::class_<Region, std::unique_ptr<Region>> &cls) {
"region"_a);
cls.def("encode", &python::encode);
cls.def_static("decode", &python::decode<Region>, "bytes"_a);
cls.def_static("flatten", [](Region const &region) {
std::vector<std::unique_ptr<Region>> result;
Region::flatten(region, result);
return result;
}, "region"_a);
cls.def_static("getRegions", Region::getRegions, "region"_a);
}

} // sphgeom
Expand Down
4 changes: 2 additions & 2 deletions src/PixelFinder.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
#include "lsst/sphgeom/CompoundRegion.h"
#include "ConvexPolygonImpl.h"

#include <typeinfo>
#include <typeid>

namespace lsst {
namespace sphgeom {
Expand Down Expand Up @@ -177,7 +177,7 @@ RangeSet findPixels(Region const & r, size_t maxRanges, int level) {
auto rs1 = findPixels<Finder, InteriorOnly>(region1, maxRanges, level);
auto rs2 = findPixels<Finder, InteriorOnly>(region2, maxRanges, level);
s = rs1.join(rs2);
}else if (auto intersection_region = dynamic_cast<IntersectionRegion const *>(&r)) {
} else if (auto intersection_region = dynamic_cast<IntersectionRegion const *>(&r)) {
Region const &region1 = intersection_region->getOperand(0);
Region const &region2 = intersection_region->getOperand(1);
auto rs1 = findPixels<Finder, InteriorOnly>(region1, maxRanges, level);
Expand Down
28 changes: 15 additions & 13 deletions src/Region.cc
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,22 @@ std::unique_ptr<Region> Region::decode(std::uint8_t const * buffer, size_t n) {
}
throw std::runtime_error("Byte-string is not an encoded Region");
}
void Region::flatten(Region const &region, std::vector<std::unique_ptr<Region>> &result) {
if (auto union_region = dynamic_cast<UnionRegion const *>(&region)) {
for(int i = 0; i < 2; ++i) {
union_region->getOperand(i);
flatten(union_region->getOperand(i), result);
}
} else if(auto intersection_region = dynamic_cast<IntersectionRegion const *>(&region)) {
for(int i = 0; i < 2; ++i) {
intersection_region->getOperand(i);
flatten(intersection_region->getOperand(i), result);
}
} else {
result.emplace_back(region.clone());

std::vector<std::unique_ptr<Region>> Region::getRegions(Region const &region) {
std::vector<std::unique_ptr<Region>> result;
if (auto union_region = dynamic_cast<UnionRegion const *>(&region)) {
for(int i = 0; i < 2; ++i) {
result.emplace_back(union_region->getOperand(i).clone());
}
} else if(auto intersection_region = dynamic_cast<IntersectionRegion const *>(&region)) {
for(int i = 0; i < 2; ++i) {
intersection_region->getOperand(i);
result.emplace_back(intersection_region->getOperand(i).clone());
}
} else {
result.emplace_back(region.clone());
}
return result;
}

}} // namespace lsst:sphgeom
14 changes: 10 additions & 4 deletions tests/test_CompoundRegion.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,16 +201,22 @@ def testRelate(self):
self.assertEqual(self.box.relate(self.instance), CONTAINS)
self.assertEqual(self.faraway.relate(self.instance), DISJOINT)

def testFlatten(self):
def testGetRegion(self):
c1 = Circle(UnitVector3d(0.0, 0.0, 1.0), 1.0)
c2 = Circle(UnitVector3d(1.0, 0.0, 1.0), 2.0)
b1 = Box.fromDegrees(90, 0, 180, 45)
b2 = Box.fromDegrees(135, 15, 135, 30)
u1 = UnionRegion(c1, b1)
u2 = UnionRegion(c2, b2)
c = UnionRegion(u1, u2)
d = Region.flatten(c)
self.assertEqual(d, [c1, b1, c2, b2])
i1 = IntersectionRegion(c1, b1)
i2 = IntersectionRegion(c2, b2)
ur = UnionRegion(u1, u2)
ir = IntersectionRegion(i1, i2)
self.assertEqual(Region.getRegions(c1), [c1])
self.assertEqual(Region.getRegions(Region.getRegions(ir)[0]), [c1, b1])
self.assertEqual(Region.getRegions(Region.getRegions(ir)[1]), [c2, b2])
self.assertEqual(Region.getRegions(Region.getRegions(ur)[0]), [c1, b1])
self.assertEqual(Region.getRegions(Region.getRegions(ur)[1]), [c2, b2])


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion tests/test_HtmPixelization.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def test_envelope_and_interior(self):
rsu2 = pixelization.envelope(union2)
self.assertEqual(rsu2, rsu2 | rsu)

# CHeck with intersection
# Check with intersection
c4 = Circle(UnitVector3d(1.0, 1.0, 2.0), 1)
c5 = Circle(UnitVector3d(1.0, 1.0, 2.5), 0.5)
rs4 = pixelization.envelope(c4)
Expand Down

0 comments on commit d7018da

Please sign in to comment.