Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/main/java/com/mindee/geometry/Bbox.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.mindee.geometry;

import java.util.Arrays;
import java.util.List;
import lombok.Getter;

Expand Down Expand Up @@ -34,8 +33,8 @@ public Bbox(double minX, double maxX, double minY, double maxY) {
* Get the Bbox as a Polygon.
*/
public Polygon getAsPolygon() {
List<Point> points = Arrays
.asList(
var points = List
.of(
new Point(this.minX, this.minY),
new Point(this.maxX, this.minY),
new Point(this.maxX, this.maxY),
Expand Down
31 changes: 7 additions & 24 deletions src/main/java/com/mindee/geometry/BboxUtils.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.mindee.geometry;

import java.util.DoubleSummaryStatistics;
import java.util.List;
import java.util.Optional;

/**
* Methods for working with BBoxes.
Expand All @@ -20,17 +18,8 @@ public static Bbox generate(Polygon polygon) {
return null;
}

DoubleSummaryStatistics statsX = polygon
.getCoordinates()
.stream()
.mapToDouble(Point::getX)
.summaryStatistics();

DoubleSummaryStatistics statsY = polygon
.getCoordinates()
.stream()
.mapToDouble(Point::getY)
.summaryStatistics();
var statsX = polygon.getCoordinates().stream().mapToDouble(Point::getX).summaryStatistics();
var statsY = polygon.getCoordinates().stream().mapToDouble(Point::getY).summaryStatistics();

return new Bbox(statsX.getMin(), statsX.getMax(), statsY.getMin(), statsY.getMax());
}
Expand All @@ -44,13 +33,7 @@ public static Bbox generate(List<Polygon> polygons) {
return null;
}

Optional<Polygon> mergedPolygon = polygons.stream().reduce(PolygonUtils::combine);

if (!mergedPolygon.isPresent()) {
return null;
}

return generate(mergedPolygon.get());
return polygons.stream().reduce(PolygonUtils::combine).map(BboxUtils::generate).orElse(null);
}

/**
Expand All @@ -62,10 +45,10 @@ public static Bbox merge(List<Bbox> bboxes) {
}

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()
bboxes.stream().mapToDouble(Bbox::getMinX).min().getAsDouble(),
bboxes.stream().mapToDouble(Bbox::getMaxX).max().getAsDouble(),
bboxes.stream().mapToDouble(Bbox::getMinY).min().getAsDouble(),
bboxes.stream().mapToDouble(Bbox::getMaxY).max().getAsDouble()
);
}

Expand Down
36 changes: 0 additions & 36 deletions src/main/java/com/mindee/geometry/BoundingBoxUtils.java

This file was deleted.

27 changes: 18 additions & 9 deletions src/main/java/com/mindee/geometry/Polygon.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,23 @@ public Bbox getAsBbox() {
return BboxUtils.generate(this);
}

/**
* Get a bounding box that encloses the Polygon.
*/
public Polygon getBoundingBox() {
var minMaxX = getMinMaxX();
var minMaxY = getMinMaxY();
return new Polygon(
List
.of(
new Point(minMaxX.getMin(), minMaxY.getMin()),
new Point(minMaxX.getMax(), minMaxY.getMin()),
new Point(minMaxX.getMax(), minMaxY.getMax()),
new Point(minMaxX.getMin(), minMaxY.getMax())
)
);
}

/**
* Get the central coordinates (centroid) of the Polygon.
*/
Expand Down Expand Up @@ -84,15 +101,7 @@ public String toString() {
* String representation with precise coordinates.
*/
public String toStringPrecise() {
StringBuilder builder = new StringBuilder("(");
for (int i = 0; i < coordinates.size(); i++) {
if (i > 0) {
builder.append(", ");
}
builder.append(coordinates.get(i));
}
builder.append(")");
return builder.toString();
return coordinates.stream().map(Point::toString).collect(Collectors.joining(", ", "(", ")"));
}

/**
Expand Down
93 changes: 17 additions & 76 deletions src/main/java/com/mindee/geometry/PolygonUtils.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.mindee.geometry;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

Expand All @@ -19,25 +17,25 @@ private PolygonUtils() {
public static Point getCentroid(List<Point> vertices) {
int verticesSum = vertices.size();

double xSum = vertices.stream().map(Point::getX).mapToDouble(Double::doubleValue).sum();
double ySum = vertices.stream().map(Point::getY).mapToDouble(Double::doubleValue).sum();
double xSum = vertices.stream().mapToDouble(Point::getX).sum();
double ySum = vertices.stream().mapToDouble(Point::getY).sum();
return new Point(xSum / verticesSum, ySum / verticesSum);
}

/**
* Get the maximum and minimum Y coordinates in a given list of Points.
*/
public static MinMax getMinMaxY(List<Point> vertices) {
List<Double> points = vertices.stream().map(Point::getY).collect(Collectors.toList());
return new MinMax(Collections.min(points), Collections.max(points));
var stats = vertices.stream().mapToDouble(Point::getY).summaryStatistics();
return new MinMax(stats.getMin(), stats.getMax());
}

/**
* Get the maximum and minimum X coordinates in a given list of Points.
*/
public static MinMax getMinMaxX(List<Point> vertices) {
List<Double> points = vertices.stream().map(Point::getX).collect(Collectors.toList());
return new MinMax(Collections.min(points), Collections.max(points));
var stats = vertices.stream().mapToDouble(Point::getX).summaryStatistics();
return new MinMax(stats.getMin(), stats.getMax());
}

/**
Expand Down Expand Up @@ -70,77 +68,20 @@ public static Polygon combine(Polygon base, Polygon target) {
target = base;
}

Double maxx = Math
.max(
target
.getCoordinates()
.stream()
.map(Point::getX)
.max(Double::compareTo)
.orElse(Double.MIN_VALUE),
base
.getCoordinates()
.stream()
.map(Point::getX)
.max(Double::compareTo)
.orElse(Double.MIN_VALUE)
);
var combinedCoords = java.util.stream.Stream
.concat(base.getCoordinates().stream(), target.getCoordinates().stream())
.collect(Collectors.toList());

Double minx = Math
.min(
target
.getCoordinates()
.stream()
.map(Point::getX)
.min(Double::compareTo)
.orElse(Double.MAX_VALUE),
base
.getCoordinates()
.stream()
.map(Point::getX)
.min(Double::compareTo)
.orElse(Double.MAX_VALUE)
);

Double maxy = Math
.max(
target
.getCoordinates()
.stream()
.map(Point::getY)
.max(Double::compareTo)
.orElse(Double.MIN_VALUE),
base
.getCoordinates()
.stream()
.map(Point::getY)
.max(Double::compareTo)
.orElse(Double.MIN_VALUE)
);

Double miny = Math
.min(
target
.getCoordinates()
.stream()
.map(Point::getY)
.min(Double::compareTo)
.orElse(Double.MAX_VALUE),
base
.getCoordinates()
.stream()
.map(Point::getY)
.min(Double::compareTo)
.orElse(Double.MAX_VALUE)
);
var xStats = combinedCoords.stream().mapToDouble(Point::getX).summaryStatistics();
var yStats = combinedCoords.stream().mapToDouble(Point::getY).summaryStatistics();

return new Polygon(
Arrays
.asList(
new Point(minx, miny),
new Point(maxx, miny),
new Point(maxx, maxy),
new Point(minx, maxy)
List
.of(
new Point(xStats.getMin(), yStats.getMin()),
new Point(xStats.getMax(), yStats.getMin()),
new Point(xStats.getMax(), yStats.getMax()),
new Point(xStats.getMin(), yStats.getMax())
)
);
}
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/com/mindee/http/MindeeApiCommon.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import org.apache.hc.core5.http.HttpEntity;

/**
Expand All @@ -10,7 +11,7 @@
public abstract class MindeeApiCommon {
/**
* Retrieves the user agent.
*
*
* @return the user agent.
*/
protected String getUserAgent() {
Expand All @@ -36,7 +37,7 @@ protected String getUserAgent() {

/**
* Checks if the status code is out of the 2xx-3xx range.
*
*
* @param statusCode the status code to check.
* @return {@code true} if the status code is in the 2xx range, false otherwise.
*/
Expand All @@ -50,6 +51,6 @@ protected String readRawResponse(HttpEntity responseEntity) throws IOException {
for (int length; (length = responseEntity.getContent().read(buffer)) != -1;) {
contentRead.write(buffer, 0, length);
}
return contentRead.toString("UTF-8");
return contentRead.toString(StandardCharsets.UTF_8);
}
}
11 changes: 4 additions & 7 deletions src/main/java/com/mindee/image/ExtractedImage.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.mindee.input.LocalInputSource;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand Down Expand Up @@ -40,9 +39,7 @@ public ExtractedImage(BufferedImage image, String filename, String saveFormat) {
* @throws IOException Throws if the file can't be accessed.
*/
public void writeToFile(String outputPath) throws IOException {
Path imagePath = Paths.get(outputPath, this.filename);
File outputfile = imagePath.toFile();
ImageIO.write(this.image, this.saveFormat, outputfile);
writeToFile(Paths.get(outputPath));
}

/**
Expand All @@ -53,8 +50,8 @@ public void writeToFile(String outputPath) throws IOException {
* @throws IOException Throws if the file can't be accessed.
*/
public void writeToFile(Path outputPath) throws IOException {
Path imagePath = outputPath.resolve(this.filename);
File outputfile = imagePath.toFile();
var imagePath = outputPath.resolve(this.filename);
var outputfile = imagePath.toFile();
ImageIO.write(this.image, this.saveFormat, outputfile);
}

Expand All @@ -65,7 +62,7 @@ public void writeToFile(Path outputPath) throws IOException {
* @throws IOException Throws if the file can't be accessed.
*/
public LocalInputSource asInputSource() throws IOException {
ByteArrayOutputStream output = new ByteArrayOutputStream();
var output = new ByteArrayOutputStream();
ImageIO.write(this.image, this.saveFormat, output);
return new LocalInputSource(output.toByteArray(), this.filename);
}
Expand Down
Loading
Loading