com.mindee.geometry.BboxUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mindee-api-java Show documentation
Show all versions of mindee-api-java Show documentation
Java Library to call Mindee's Off-The-Shelf and Custom APIs
package com.mindee.geometry;
import java.util.DoubleSummaryStatistics;
import java.util.List;
import java.util.Optional;
/**
* Methods for working with BBoxes.
*/
public final class BboxUtils {
private BboxUtils() {
}
/**
* Generate a BBox from a single Polygon.
*/
public static Bbox generate(Polygon polygon) {
if (polygon == null) {
return null;
}
DoubleSummaryStatistics statsX = polygon.getCoordinates()
.stream()
.mapToDouble(Point::getX)
.summaryStatistics();
DoubleSummaryStatistics statsY = polygon.getCoordinates()
.stream()
.mapToDouble(Point::getY)
.summaryStatistics();
return new Bbox(
statsX.getMin(),
statsX.getMax(),
statsY.getMin(),
statsY.getMax()
);
}
/**
* Generate a BBox from a list of Polygons.
*/
public static Bbox generate(List polygons) {
if (polygons.isEmpty()) {
return null;
}
Optional mergedPolygon = polygons.stream()
.reduce(PolygonUtils::combine);
if (!mergedPolygon.isPresent()) {
return null;
}
return generate(mergedPolygon.get());
}
/**
* Merge a list of BBoxes.
*/
public static Bbox merge(List bboxes) {
if (bboxes.isEmpty()) {
return null;
}
return new Bbox(
bboxes.stream().map(Bbox::getMinX).min(Double::compare).get(),
bboxes.stream().map(Bbox::getMaxX).max(Double::compare).get(),
bboxes.stream().map(Bbox::getMinY).min(Double::compare).get(),
bboxes.stream().map(Bbox::getMaxY).max(Double::compare).get()
);
}
}