: [outpol, npol] = clipPolygon_mrf (inpol, clippol)
: [outpol, npol] = clipPolygon_mrf (inpol, clippol, op)

Perform boolean operation on polygon(s) using the algorithm by Martinez, Rueda and Feito.

inpol = Nx2 matrix of (X, Y) coordinates constituting the polygons(s) to be clipped (subject polygon). clippol = Nx2 matrix of (X, Y) coordinates representing the clip polygon(s).

Polygons may have multiple non-intersecting regions. The contours are separated by [Nan NaN] rows. Polygons may also contain holes. Holes are defined as the region enclosed within the boundaries of polygon which are not part of the polygon region. Here again, the inner and outer contours are separated by [Nan NaN] rows. Specifically, if a contour is lying inside another contour, it is automatically treated as a hole.

The following features are allowed in input polygons:

  • 0: Contours can be described in clockwise or counterclockwise order.
  • 1: Holes.
  • 2: A vertex of a polygon can touch (in a point) a vertex or edge of the same polygon.
  • 3: Self-intersecting polygons.

The following features are not allowed in input polygons:

  • The intersection of two edges of the same polygon can be a point, but cannot be a segment.

The argument op, the boolean operation, can be:

  • 0: difference inpol - clippol
  • 1: intersection ("AND") of inpol and clippol (= default)
  • 2: exclusiveOR ("XOR") of inpol and clippol
  • 3: union ("OR") of inpol and clippol

Output array outpol will be an Nx2 array of polygons resulting from the requested boolean operation

Optional output argument npol indicates the number of output polygons.

Know more about the algorithm by Martinez, Rueda and Feito[1].

[1]: http://www4.ujaen.es/~fmartin/bool_op.html

See also: clipPolygon_clipper,clipPolygon.

Demonstration 1

The following code

 pol1 = [0.15 0.15; 0.55 0.45; 0.15 0.75];
 pol2 = [0.35 0.45; 0.75 0.15; 0.75 0.75];
 pol1a = polygon2patch(pol1);
 pol2a = polygon2patch(pol2);
 lw = 2;
 subplot (2, 6, [2 3])
 patch (pol1a(:, 1), pol1a(:, 2), 'facecolor', 'c', 'edgecolor', 'k', 'linewidth', lw);
 axis image
 grid on
 title ("1. Subject polygon")

 subplot (2, 6, [4 5])
 patch (pol1a(:, 1), pol1a(:, 2), 'facecolor', 'c', 'edgecolor', 'none');
 hold on
 patch (pol2a(:, 1), pol2a(:, 2), 'facecolor', 'y', 'edgecolor', 'b', 'linewidth', lw);
 axis image
 grid on
 title "2. Clip polygon"

 op   = {"Sub -clip", "AND / Intersection", "Exclusive OR", "OR / Union"};
 for i=1:numel(op)
   subplot (6, 4, [12 16]+i);
   [opol, npol] = clipPolygon_mrf (pol1, pol2, i-1);
   opol = polygon2patch (opol);
   patch (pol1a(:, 1), pol1a(:, 2), 'facecolor', 'c', 'edgecolor', 'none');
   hold on
   patch (pol2a(:, 1), pol2a(:, 2), 'facecolor', 'y', 'edgecolor', 'none');
   patch (opol(:,1),opol(:,2), 'facecolor', 'g', 'edgecolor', 'r', ...
         'linewidth', lw);
   axis image
   grid on
   title (sprintf("%d. %s", i+2, op{i}));
   axis off
 endfor

 subplot (10, 4, 37);
   [opol, npol] = clipPolygon_mrf (pol2, pol1, 0);
   opol = polygon2patch (opol);
   patch (pol1a(:, 1), pol1a(:, 2), 'facecolor', 'c', 'edgecolor', 'none');
   hold on
   patch (pol2a(:, 1), pol2a(:, 2), 'facecolor', 'y', 'edgecolor', 'none');
   patch (opol(:,1),opol(:,2), 'facecolor', 'g', 'edgecolor', 'r', ...
         'linewidth', lw);
   axis image
   grid on
   axis off
   title "7. Clip - sub";

Produces the following figure

Figure 1

Demonstration 2

The following code

 pol1 = [1 1; 5 1; 3 7; NaN NaN; 2 2; 3 5; 4 2];
 pol2 = [3 1; 5 6; 1 6];
 pol1a = polygon2patch(pol1);
 pol2a = polygon2patch(pol2);
 lw = 2;
 subplot (2, 6, [2 3])
 patch (pol1a(:, 1), pol1a(:, 2), 'facecolor', 'c', 'edgecolor', 'k', 'linewidth', lw);
 axis image
 grid on
 title ("1. Subject polygon")

 subplot (2, 6, [4 5])
 patch (pol1a(:, 1), pol1a(:, 2), 'facecolor', 'c', 'edgecolor', 'none');
 hold on
 patch (pol2a(:, 1), pol2a(:, 2), 'facecolor', 'y', 'edgecolor', 'b', 'linewidth', lw);
 axis image
 grid on
 title "2. Clip polygon"

 op   = {"Sub -clip", "AND / Intersection", "Exclusive OR", "OR / Union"};
 for i=1:numel(op)
   subplot (6, 4, [12 16]+i);
   [opol, npol] = clipPolygon_mrf (pol1, pol2, i-1);
   opol = polygon2patch (opol);
   patch (pol1a(:, 1), pol1a(:, 2), 'facecolor', 'c', 'edgecolor', 'none');
   hold on
   patch (pol2a(:, 1), pol2a(:, 2), 'facecolor', 'y', 'edgecolor', 'none');
   patch (opol(:,1),opol(:,2), 'facecolor', 'g', 'edgecolor', 'r', ...
         'linewidth', lw);
   axis image
   grid on
   title (sprintf("%d. %s", i+2, op{i}));
   axis off
 endfor

 subplot (10, 4, 37);
   [opol, npol] = clipPolygon_mrf (pol2, pol1, 0);
   opol = polygon2patch (opol);
   patch (pol1a(:, 1), pol1a(:, 2), 'facecolor', 'c', 'edgecolor', 'none');
   hold on
   patch (pol2a(:, 1), pol2a(:, 2), 'facecolor', 'y', 'edgecolor', 'none');
   patch (opol(:,1),opol(:,2), 'facecolor', 'g', 'edgecolor', 'r', ...
         'linewidth', lw);
   axis image
   grid on
   axis off
   title "7. Clip - sub";

Produces the following figure

Figure 1

Package: geometry