Search in sources :

Example 1 with MinimumBoundingCircle

use of org.locationtech.jts.algorithm.MinimumBoundingCircle in project qupath by qupath.

the class ObjectMeasurements method addShapeMeasurements.

private static void addShapeMeasurements(MeasurementList ml, Geometry geom, String baseName, String units, Collection<ShapeFeatures> features) {
    boolean isArea = geom instanceof Polygonal;
    boolean isLine = geom instanceof Lineal;
    var units2 = units + "^2";
    if (!baseName.isEmpty() && !baseName.endsWith(" "))
        baseName += " ";
    double area = geom.getArea();
    double length = geom.getLength();
    if (isArea && features.contains(ShapeFeatures.AREA))
        ml.putMeasurement(baseName + "Area " + units2, area);
    if ((isArea || isLine) && features.contains(ShapeFeatures.LENGTH))
        ml.putMeasurement(baseName + "Length " + units, length);
    if (isArea && features.contains(ShapeFeatures.CIRCULARITY)) {
        if (geom instanceof Polygon) {
            var polygon = (Polygon) geom;
            double ringArea, ringLength;
            if (polygon.getNumInteriorRing() == 0) {
                ringArea = area;
                ringLength = length;
            } else {
                var ring = ((Polygon) geom).getExteriorRing().getCoordinateSequence();
                ringArea = Area.ofRing(ring);
                ringLength = Length.ofLine(ring);
            }
            double circularity = Math.PI * 4 * ringArea / (ringLength * ringLength);
            ml.putMeasurement(baseName + "Circularity", circularity);
        } else {
            logger.debug("Cannot compute circularity for {}", geom.getClass());
        }
    }
    if (isArea && features.contains(ShapeFeatures.SOLIDITY)) {
        double solidity = area / geom.convexHull().getArea();
        ml.putMeasurement(baseName + "Solidity", solidity);
    }
    if (features.contains(ShapeFeatures.MAX_DIAMETER)) {
        double minCircleRadius = new MinimumBoundingCircle(geom).getRadius();
        ml.putMeasurement(baseName + "Max diameter " + units, minCircleRadius * 2);
    }
    if (features.contains(ShapeFeatures.MIN_DIAMETER)) {
        double minDiameter = new MinimumDiameter(geom).getLength();
        ml.putMeasurement(baseName + "Min diameter " + units, minDiameter);
    }
}
Also used : MinimumBoundingCircle(org.locationtech.jts.algorithm.MinimumBoundingCircle) MinimumDiameter(org.locationtech.jts.algorithm.MinimumDiameter) Lineal(org.locationtech.jts.geom.Lineal) Polygonal(org.locationtech.jts.geom.Polygonal) Polygon(org.locationtech.jts.geom.Polygon)

Aggregations

MinimumBoundingCircle (org.locationtech.jts.algorithm.MinimumBoundingCircle)1 MinimumDiameter (org.locationtech.jts.algorithm.MinimumDiameter)1 Lineal (org.locationtech.jts.geom.Lineal)1 Polygon (org.locationtech.jts.geom.Polygon)1 Polygonal (org.locationtech.jts.geom.Polygonal)1