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

  1. Does not add any information to the object for simple shapes
  2. 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

  1. Why this conversion is required for simple polygon shapes such as those above even if no additional information is added ?
  2. Whether there is a way for functions such as RegionUnion[] to work directly with graphics primitives ?
  3. 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.

  • $\begingroup$ But RegionCentroid[RegionUnion @@ testpoly] works for me. Do you care because RegionUnion 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$
    – halirutan
    Jan 5, 2015 at 16:26
  • $\begingroup$ Thanks - just to clarify that I'm after the RegionUnion[] function on it's own. RegionCentroid[] and RegionMeasure[] were shown as examples of region functions which did not require the (slow) conversion from the graphics primitives to region objects. $\endgroup$
    – Mac
    Jan 5, 2015 at 16:33
  • $\begingroup$ You could try DiscretizeGraphics @ Graphics @ testpoly. This lets DiscretizeGraphics work on all polygons at once instead of on each one separately. Is that faster? $\endgroup$ Jan 5, 2015 at 23:38
  • $\begingroup$ Thanks Sjoerd - tried your suggestion (DiscretizeGraphics @ Graphics @ testpoly) to see what happens. Unfortunately working on all polygons at once is much slower by roughly a factor 20 with respect to Map[]. Looks like I'm stuck unless Wolfram Research can come up with a better solution. $\endgroup$
    – Mac
    Jan 6, 2015 at 8:44
  • $\begingroup$ @Sjoerd's suggestion is slower probably because of overlapping polygons. It seems to be fast on disjoint polygons. $\endgroup$
    – Michael E2
    May 26, 2015 at 22:52


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.