My kernel (v13.1.0 Windows 64-bit) crashes when repeatedly (~9 times) calling Region functions like RegionIntersection upon simple Polygon.
Take for example these simple input polygons:
polygons = {Polygon[{{-6,298},{98,298},{98,198},{-6,198}}],Polygon[{{202.,163.36904761904762},{171.,163.},{171.,100.},{2.,100.},{2.,0.},{171.,0.},{171.,-24.},{202.,-44.66666666666667}}],Polygon[{{370.6,0.},{502.,0.},{502.,100.},{358.1666666666667,100.},{339.,165.},{300.,164.53571428571428},{300.,-44.125}}],Polygon[{{248.82501070724447,-87.90184472304296},{202.00000237873843,-56.68517250403892},{202.,-44.66666666666667},{202.,173.36975621072258},{300.,174.5364228773892},{300.00000220959214,-55.91747503407568},{300.,-55.917476415070766}}],Polygon[{{202.,277.22222270743714},{202.,173.36975621072256},{300.,174.5364228773892},{300.,249.9999966555138}}],Polygon[{{300.,-200.},{300.00000220959214,-55.91747503407568},{248.82501070724447,-87.90184472304296},{202.00000237873843,-56.68517250403891},{202.,-200.}}],Polygon[{{400.,222.22222222222223},{400.,194.},{498.,194.},{498.,300.},{400.,300.},{400.,257.77777777777777},{480.,200.}}],Polygon[{{202.,277.22222222222223},{202.,296.},{300.,296.},{300.,250.}}],Polygon[{{400.,222.22222222222223},{480.,200.},{400.,257.77777777777777},{390.,264.99999031574845},{390.,225.00000438132946}}],Polygon[{{390.,225.00000438132946},{390.,264.99999031574845},{300.,330.},{150.,350.},{120.,300.},{202.,277.22222240762164},{202.,296.},{300.,296.},{300.,250.00000687306255}}]};
They resemble:
While some functions behave fine...
RegionDisjoint @@ polygons
>>> False
as does RegionIntersection when called once upon any individual pair of Polygons:
RegionIntersection @@ polygons[[{2, 4}]]
>>> MeshRegion[{{202., 163.36904761904762}, {202., -44.666666666666664}}, {Line[{{1, 2}}]}]
Alas, calling RegionIntersection repeatedly causes it to crash after about ~9 calls!
RegionIntersection /@ Subsets[polygons, {2}];
(kernel exits without notice)
The crash occurs only when repeatedly calling RegionIntersection upon actually intersecting pairs. It runs fine when acting only upon the 36 disjoint region pairs:
RegionIntersection /@
Select[Subsets[polygons, {2}], RegionDisjoint @@ # &]
>>> {EmptyRegion[2], ... }
It also runs fine when run upon any individual intersecting pair:
badPairs = Select[Subsets[polygons, {2}], Not[RegionDisjoint @@ #] &];
i = 1
(* below runs fine when re-evaluated for every i *)
RegionIntersection @ badPairs[[i++]]
Yet, it crashes when mapped onto even only the first 7 of the 9 pairs!
RegionIntersection /@ badPairs[[;; 7]]
(kernel crash)
Maddeningly, I can reduce the likelihood of this crash by insert Echo statements into the loop:
(* crashes ~4/5 times *)
Do[
Echo["Stephen pls fix"];
RegionIntersection @ pair,
{pair, badPairs}
]
(* crashes never (slower Echo) *)
Do[
Echo @ RegionIntersection @ pair,
{pair, badPairs}
]
This isn't merely slowing the loop, since replacing Echo with Pause[1] (with and without ClearSystemCache[]) still causes a crash.
Since RegionIntersection sometimes returns a MeshRegion (an irritation in itself, since other times it returns a Polygon), I suspect this crash occurs due to communication with an FEM module. Although it seems insane to think 7 calls with incredibly simple Polygon could overload a local module!
Is there an obvious mistake I am making leading to this unstable behaviour? Is this otherwise a core Mathematica bug? I have seen complaints online that the meshing facilities (especially with functions like RegionDifference is unstable).
