To my knowledge some (but not all) of the new geometric region functions in Mathematica version 10 require an additional step to convert from geometric shapes into regions. However this step
- Does not add any information to the object for simple shapes
- Is very slow even for simple geometrical shapes
As an example consider 2 square test polygons defined by a 4 xy coordinates with a random lower left corner point. These functions work
testpoly =
Map[Polygon[{#, # + {0, 5}, # + {5, 5}, # + {5, 0}}] &,
RandomReal[{1, 100}, {2, 2}]];
RegionCentroid /@ testpoly
RegionMeasure /@ testpoly
but this does not
RegionUnion @@ testpoly
For reasons that remain a mystery to me to use derived region functions such as RegionUnion[] we need to make an extra conversion from Polygon to Region via DiscretizeGraphics[]. For instance this works:
DiscretizeGraphics /@ testpoly // Apply[RegionUnion]
The main problem here is that - in addition to extra conversion step which adds no information to the original object - this takes a lot of time for larger sets of polygons. As an example lets consider 5000 simple polygons.
testpoly =
Map[Polygon[{#, # + {0, 5}, # + {5, 5}, # + {5, 0}}] &,
RandomReal[{1, 100}, {5000, 2}]];
Timing[DiscretizeGraphics /@ testpoly;]
which gives {13.463067, Null} or over 13 seconds on a top of the line MacBook from 2014. In practice I have many more polygons that I would like to test.
My questions are
- Why this conversion is required for simple polygon shapes such as those above even if no additional information is added ?
- Whether there is a way for functions such as RegionUnion[] to work directly with graphics primitives ?
- Failing this how to speed up the DiscretizeGraphics[] conversion step ?
Any help is much appreciated. I'm hoping that some of these elements will be addressed in future releases of Mathematica.
RegionCentroid[RegionUnion @@ testpoly]
works for me. Do you care becauseRegionUnion
stays unevaluated? What I have to say is that I too think that many region functions are extremely slow. Up to the point where I wait forever. $\endgroup$DiscretizeGraphics @ Graphics @ testpoly
. This lets DiscretizeGraphics work on all polygons at once instead of on each one separately. Is that faster? $\endgroup$