Skip to content

Commit

Permalink
bounding box polishing
Browse files Browse the repository at this point in the history
  • Loading branch information
detonator413 committed Oct 28, 2015
1 parent 3f4201c commit 2098204
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
Expand Up @@ -21,7 +21,7 @@ private BoundingBox(int page, double x, double y, double width, double height) {
}

public static BoundingBox fromTwoPoints(int page, double x1, double y1, double x2, double y2) {
if (x1 >= x2 || y1 > y2) {
if (x1 > x2 || y1 > y2) {
throw new IllegalArgumentException("Invalid points provided: (" + x1 + ";" + y1 + ")-(" + x2 + ";" + y2 + ")");
}
return new BoundingBox(page, x1, y1, x2 - x1, y2 - y1);
Expand Down
Expand Up @@ -11,8 +11,8 @@
* Utilities to calculate bounding boxes from coordinates
*/
public class BoundingBoxCalculator {
private static final double EPS_X = 10;
private static final double EPS_Y = 3;
private static final double EPS_X = 15;
private static final double EPS_Y = 4;

public static List<BoundingBox> calculate(List<LayoutToken> tokens) {
List<BoundingBox> result = Lists.newArrayList();
Expand All @@ -26,6 +26,9 @@ public static List<BoundingBox> calculate(List<LayoutToken> tokens) {
BoundingBox lastBox = firstBox;
for (int i = 1; i < tokens.size(); i++) {
BoundingBox b = BoundingBox.fromLayoutToken(tokens.get(i));
if (Math.abs(b.getWidth()) <= Double.MIN_VALUE || Math.abs(b.getHeight()) <= Double.MIN_VALUE) {
continue;
}

if (near(lastBox, b)) {
result.set(result.size() - 1, result.get(result.size() - 1).boundBox(b));
Expand All @@ -37,8 +40,11 @@ public static List<BoundingBox> calculate(List<LayoutToken> tokens) {
return result;
}

//same page, Y is more or less the same, b2 follows b1 on X, and b2 close to the end of b1
private static boolean near(BoundingBox b1, BoundingBox b2) {
return Math.abs(b1.getY() - b2.getY()) < EPS_Y && Math.abs(b1.getY2() - b2.getY2()) < EPS_Y && b2.getX() - b1.getX2() < EPS_X;
return b1.getPage() == b2.getPage()
&& Math.abs(b1.getY() - b2.getY()) < EPS_Y && Math.abs(b1.getY2() - b2.getY2()) < EPS_Y
&& b2.getX() - b1.getX2() < EPS_X && b2.getX() >= b1.getX();
}

}

0 comments on commit 2098204

Please sign in to comment.