Skip to content

Commit

Permalink
Adding a simplify function to the geometry module
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasteuwen committed Aug 15, 2024
1 parent 7ff3d5f commit fdb4e50
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 26 deletions.
13 changes: 7 additions & 6 deletions dlup/annotations_experimental.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from dlup._exceptions import AnnotationError
from dlup._types import PathLike
from dlup.annotations import CoordinatesDict, GeoJsonDict, _geometry_to_geojson, _get_geojson_color
from dlup.geometry import DlupGeometryContainer, DlupPoint, DlupPolygon
from dlup.geometry import GeometryCollection, DlupPoint, DlupPolygon


class CoordinatesDict(TypedDict):
Expand Down Expand Up @@ -130,7 +130,7 @@ def shape(
class WsiAnnotationsExperimental:
"""Class that holds all annotations for a specific image"""

def __init__(self, layers: DlupGeometryContainer):
def __init__(self, layers: GeometryCollection):
self._layers = layers
self._tags = []

Expand Down Expand Up @@ -173,7 +173,7 @@ def from_geojson(
_geometry = shape(x["geometry"], label=_label, color=_color)
geometries += _geometry

container = DlupGeometryContainer()
container = GeometryCollection()
for layer in geometries:
if isinstance(layer, DlupPolygon):
container.add_polygon(layer)
Expand All @@ -189,7 +189,7 @@ def from_asap_xml(
cls,
asap_xml: PathLike,
scaling: float | None = None,
sorting: AnnotationSorting | str = AnnotationSorting.AREA,
sorting=None,
) -> WsiAnnotations:
pass

Expand All @@ -198,15 +198,15 @@ def from_halo_xml(
cls,
halo_xml: PathLike,
scaling: float | None = None,
sorting: AnnotationSorting | str = AnnotationSorting.NONE,
sorting=None,
) -> WsiAnnotations:
pass

@classmethod
def from_darwin_json(
cls,
darwin_json: PathLike,
sorting: AnnotationSorting | str = AnnotationSorting.NONE,
sorting,
z_indices: Optional[dict[str, int]] = None,
) -> WsiAnnotations:
pass
Expand Down Expand Up @@ -251,6 +251,7 @@ def simplify(self, tolerance: float, *, preserve_topology: bool = True) -> None:
-------
None
"""
self._layers.simplify(tolerance)

def __contains__(self, item) -> bool:
pass
Expand Down
19 changes: 18 additions & 1 deletion dlup/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ def dlup_point_factory(point):
_dg.set_point_factory(dlup_point_factory)


class DlupGeometryContainer(_dg.GeometryContainer):
class GeometryCollection(_dg.GeometryContainer):
def __init__(self):
super().__init__()

Expand All @@ -231,3 +231,20 @@ def color_lut(self):
LUT[key] = color

return LUT

def __getstate__(self):
state = {
"_polygons": [polygon.__getstate__() for polygon in self.polygons],
"_points": [point.__getstate__() for point in self.points],
}
return state

def __setstate__(self, state):
polygons = [DlupPolygon.__setstate__(polygon) for polygon in state["_polygons"]]
points = [DlupPoint.__setstate__(point) for point in state["_points"]]
self.__init__()
for polygon in polygons:
self.add_polygon(polygon)

for point in points:
self.add_point(point)
15 changes: 12 additions & 3 deletions src/geometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ std::vector<std::shared_ptr<Polygon>> Polygon::intersection(const BoostPolygon &
return result;
}

void Polygon::simplifyPolygons(double tolerance) {
bg::simplify(*polygon, *polygon, tolerance);
}

py::list AnnotationRegion::getPolygons() const {
#ifdef DLUPDEBUG
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
Expand Down Expand Up @@ -162,6 +166,11 @@ class GeometryCollection {
void scale(double scaling);
void setOffset(std::pair<double, double> offset);
void rebuildRTree() { rtreeWrapper.rebuild(); }
void simplifyPolygons(double tolerance) {
for (auto &polygon : polygons) {
polygon->simplifyPolygons(tolerance);
}
}

std::uintptr_t getPointerId() const { return reinterpret_cast<std::uintptr_t>(this); }

Expand All @@ -172,6 +181,8 @@ class GeometryCollection {

// TODO: Rethink the need for this function.
void reindexPolygons(const std::map<std::string, int> &indexMap);


};

GeometryCollection::GeometryCollection() : rtreeWrapper(this) {}
Expand Down Expand Up @@ -457,6 +468,7 @@ PYBIND11_MODULE(_geometry, m) {
.def("set_interiors", &Polygon::setInteriors)
.def("get_exterior", &Polygon::getExterior)
.def("get_interiors", &Polygon::getInteriors)
.def("simplify", &Polygon::simplifyPolygons)
.def_property_readonly("wkt", &Polygon::toWkt)
.def_property_readonly("area", &Polygon::getArea);

Expand All @@ -482,9 +494,6 @@ PYBIND11_MODULE(_geometry, m) {
.def("equals", &Point::equals)
.def("within", &Point::within)
.def("centroid", &Point::centroid)
.def("azimuth", &Point::azimuth)
.def("translate", &Point::translate)
.def("rotate", &Point::rotate, py::arg("angle"), py::arg("origin") = Point(0, 0))
.def("scale", &Point::scale, py::arg("scaling"), py::arg("origin") = Point(0, 0))
.def_property_readonly("wkt", &Point::toWkt);

Expand Down
19 changes: 3 additions & 16 deletions src/geometry.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ class Polygon : public BaseGeometry {

void setExterior(const std::vector<std::pair<double, double>> &coordinates);
void setInteriors(const std::vector<std::vector<std::pair<double, double>>> &interiors);

void simplifyPolygons(double tolerance);

};

class Point : public BaseGeometry {
Expand Down Expand Up @@ -119,22 +122,6 @@ class Point : public BaseGeometry {
return std::make_shared<Point>(centroid);
}

double azimuth(const Point &other) const { return bg::azimuth(*point, *(other.point)); }

std::shared_ptr<Point> translate(double dx, double dy) const {
BoostPoint translated;
bg::strategy::transform::translate_transformer<double, 2, 2> translate(dx, dy);
bg::transform(*point, translated, translate);
return std::make_shared<Point>(translated);
}

std::shared_ptr<Point> rotate(double angle, const Point &origin = Point(0, 0)) const {
BoostPoint rotated;
bg::strategy::transform::rotate_transformer<bg::degree, double, 2, 2> rotate(angle);
bg::transform(*point, rotated, rotate);
return std::make_shared<Point>(rotated);
}

std::shared_ptr<Point> scale(double scaling, const Point &origin = Point(0, 0)) const {
BoostPoint scaled;
double dx = getX() - origin.getX();
Expand Down

0 comments on commit fdb4e50

Please sign in to comment.