2

Updated

System Info: HONOR laptop AMD Ryzen 5 4600H 3Hz 6 cores 16GB Memory,
Windows 10 Pro 64 bit with updates.

Mathematica Info: Versions 12.2 and 13.2, both on the same laptop.

Examples:

p1 = RandomPolygon[{"Simple", 50}, DataRange -> {{1, 2}, {1, 2}}];
p2 = RandomPolygon[{"Simple", 50}, DataRange -> {{1, 2}, {1, 2}}];

Timing[RegionIntersection[p1, p2] === EmptyRegion[2]]

WM 12.2: 0.1-0.3 s vs WM 13.2: 3.1-3.6 s

Timing[RegionDifference[p1, p2]] 

WM 12.2: 0.3-0.34 s vs WM 13.2: 3.4-3.5 s

Timing[RegionUnion[p1, p2]]

WM 12.2: 0.1-0.2 s vs WM 13.2: 0.7-0.9 s

Other operations (graphs, big numbers) do not show such differences, but I still checking.

More specific question

For one demo, I'd like to create shapes like this one:
enter image description here
Not too complex (without holes and self-intersections), so the output is just simple polygon with 30-50 vertices:

bf[p_, s_] := 
  BSplineFunction[p, SplineClosed -> True, SplineDegree -> s];
initialShape = 
  With[{pts = RandomReal[{0.2, 0.6}, {9, 2}]*CirclePoints[9]},
   Polygon[Table[ bf[pts, 4][t], {t, 0, 1, 0.02}]]
   ];
shape[center_, size_] := 
  TranslationTransform[center][
   ScalingTransform[{size, size}][initialShape]];

For further I need to analyze the intersections of these figures. The problem with RegionDisjoint has already been described here, and in version 13.2 it still remained:

RegionDisjoint[shape[{1., 1.}, 1.], shape[{5., 5.}, 2.]]

Output:
enter image description here

OK, we can use RegionIntersection:

isNonIntersect[shape1_, shape2_] :=
  RegionIntersection[shape1, shape2] === EmptyRegion[2];

It works, but I found big oddities with the timing of RegionIntersection:

Result:
True Version 12.2: 0.06-0.08s Version 13.2: 1.2-1.3s
False Version 12.2: 0.0s Version 13.2: 0.01-0.2s

Why does it take longer to get True, and why Mathematica 13.2 is so slow?!
(all it runs on the same laptop with Win 10)

lesobrod
  • 1,657
  • 9
  • 14
  • 2
    I have no answer to this but I'd suggest applying BoundaryDiscretizeRegion or DiscretizeRegion to the polygon; as far as I know it should result equivalent region, at least for polygons with machine-precision coordinate values. Also, mesh regions tend to behave better on intersections etc. than polygons. – kirma Jan 20 '23 at 07:26
  • Can you provide an example of a specific pair of non-overlapping regions which show this performance difference? That is, a SeedRandom value and shape specifications. – kirma Jan 20 '23 at 07:59
  • @kirma Yes, soon, I'm preparing a separate question about the performance of version 13.2, it is slower than 12.2 in almost everything – lesobrod Jan 20 '23 at 08:23
  • That sounds puzzling, because I don't see generally significant slowdown - and I'm running v12.2 under emulation! (v12.2 is available only for Intel Macs, while I have an ARM laptop.) Somehow I suspect this is an issue related to your system and installation... – kirma Jan 20 '23 at 08:27
  • @kirma DiscretizeRegion runs 2-3 times slower ( – lesobrod Jan 20 '23 at 09:51
  • With your updated examples I see performance regressions from 12.3.1 to 13.2, but only RegionUnion has drastic reduction in performance with these inputs. My numbers are not very comparable because I have to run 12.3.1 under emulation, though. It could be possible that algorithms used by these functions for polygons have had bugs fixed for better correctness and performance regressions stem from that... – kirma Jan 20 '23 at 10:09
  • 1
    Please see How to report a bug in Mathematica. Making a post here is a great way to get a workaround for something, or confirmation that others can reproduce. But if bugs are to be fixed they need to be reported to Wolfram directly. – Jason B. Jan 20 '23 at 16:58
  • @JasonB. Yes, thank you, it needs to be done but i have to check my findings! – lesobrod Jan 20 '23 at 17:02
  • 1
    @kirma You was almost right thanks! Just not DiscretizeRegion but BoundaryMeshRegion works very well – lesobrod Jan 21 '23 at 07:28

1 Answers1

2

If simple polygons are given by lists of their vertices, then the fastest and most reliable way to check their intersection is:

myRegion[pts_] := 
  With[{bnd = Append[Range@Length@pts, 1]}, 
   BoundaryMeshRegion[pts, Line@bnd]];

isIntersect[verts1_, verts2_] := UnsameQ[RegionIntersection[myRegion/@{verts1, verts2}], EmptyRegion[2]];

It's 10-15 times faster than UnsameQ[RegionIntersection[Polygon@verts1, Polygon@verts2], EmptyRegion[2]] both on 12.2 and 13.2.

lesobrod
  • 1,657
  • 9
  • 14