Search in sources :

Example 1 with Paths

use of com.itextpdf.kernel.pdf.canvas.parser.clipper.Paths in project i7j-pdfsweep by itext.

the class PdfCleanUpFilter method checkIfRectanglesIntersect.

/**
 * Return true if two given rectangles (specified by an array of points) intersect.
 *
 * @param rect1 the first rectangle, considered as a subject of intersection. Even if it's width is zero,
 *              it still can be intersected by second rectangle.
 * @param rect2 the second rectangle, considered as intersecting rectangle. If it has zero width rectangles
 *              are never considered as intersecting.
 * @return true if the rectangles intersect, false otherwise
 */
static boolean checkIfRectanglesIntersect(Point[] rect1, Point[] rect2) {
    IClipper clipper = new DefaultClipper();
    // If the redaction area is degenerate, the result will be false
    if (!ClipperBridge.addPolygonToClipper(clipper, rect2, PolyType.CLIP)) {
        // If the content area is degenerate, let's process this case specifically
        if (!ClipperBridge.addPolygonToClipper(clipper, rect1, PolyType.SUBJECT)) {
            // because the redaction line corresponds to the descent line of the content.
            if (!ClipperBridge.addPolylineSubjectToClipper(clipper, rect2)) {
                return false;
            }
            if (rect1.length != rect2.length) {
                return false;
            }
            Point startPoint = rect2[0];
            Point endPoint = rect2[0];
            for (int i = 1; i < rect2.length; i++) {
                if (rect2[i].distance(startPoint) > EPS) {
                    endPoint = rect2[i];
                    break;
                }
            }
            for (int i = 0; i < rect1.length; i++) {
                if (isPointOnALineSegment(rect1[i], startPoint, endPoint, true)) {
                    return true;
                }
            }
        }
        return false;
    }
    // According to clipper documentation:
    // The function will return false if the path is invalid for clipping. A path is invalid for clipping when:
    // - it has less than 2 vertices;
    // - it has 2 vertices but is not an open path;
    // - the vertices are all co-linear and it is not an open path.
    // Reference: http://www.angusj.com/delphi/clipper/documentation/Docs/Units/ClipperLib/Classes/ClipperBase/Methods/AddPath.htm
    // If addition returns false, this means that there are less than 3 distinct points, because of rectangle zero width.
    // Let's in this case specify the path as polyline, because we still want to know if redaction area
    // intersects even with zero-width rectangles.
    boolean intersectionSubjectAdded = ClipperBridge.addPolygonToClipper(clipper, rect1, PolyType.SUBJECT);
    if (intersectionSubjectAdded) {
        // working with paths is considered to be a bit faster in terms of performance.
        Paths paths = new Paths();
        clipper.execute(ClipType.INTERSECTION, paths, PolyFillType.NON_ZERO, PolyFillType.NON_ZERO);
        return !checkIfIntersectionRectangleDegenerate(paths.getBounds(), false) && !paths.isEmpty();
    } else {
        int rect1Size = rect1.length;
        intersectionSubjectAdded = ClipperBridge.addPolylineSubjectToClipper(clipper, rect1);
        if (!intersectionSubjectAdded) {
            // According to the comment above,
            // this could have happened only if all four passed points are actually the same point.
            // Adding here a point really close to the original point, to make sure it's not covered by the
            // intersecting rectangle.
            double smallDiff = 0.01;
            List<Point> rect1List = new ArrayList<Point>(Arrays.asList(rect1));
            rect1List.add(new Point(rect1[0].getX() + smallDiff, rect1[0].getY()));
            rect1 = rect1List.toArray(new Point[rect1Size]);
            intersectionSubjectAdded = ClipperBridge.addPolylineSubjectToClipper(clipper, rect1);
            assert intersectionSubjectAdded;
        }
        PolyTree polyTree = new PolyTree();
        clipper.execute(ClipType.INTERSECTION, polyTree, PolyFillType.NON_ZERO, PolyFillType.NON_ZERO);
        Paths paths = Paths.makePolyTreeToPaths(polyTree);
        return !checkIfIntersectionRectangleDegenerate(paths.getBounds(), true) && !paths.isEmpty();
    }
}
Also used : PolyTree(com.itextpdf.kernel.pdf.canvas.parser.clipper.PolyTree) DefaultClipper(com.itextpdf.kernel.pdf.canvas.parser.clipper.DefaultClipper) ArrayList(java.util.ArrayList) IClipper(com.itextpdf.kernel.pdf.canvas.parser.clipper.IClipper) Point(com.itextpdf.kernel.geom.Point) Paths(com.itextpdf.kernel.pdf.canvas.parser.clipper.Paths) Point(com.itextpdf.kernel.geom.Point)

Aggregations

Point (com.itextpdf.kernel.geom.Point)1 DefaultClipper (com.itextpdf.kernel.pdf.canvas.parser.clipper.DefaultClipper)1 IClipper (com.itextpdf.kernel.pdf.canvas.parser.clipper.IClipper)1 Paths (com.itextpdf.kernel.pdf.canvas.parser.clipper.Paths)1 PolyTree (com.itextpdf.kernel.pdf.canvas.parser.clipper.PolyTree)1 ArrayList (java.util.ArrayList)1