NUMERIC COMPUTING WITH GEOMETRIC REGIONS:
Methods and Options

Paritosh Mokhasi, Rob Knapp

Join the Conversation #WolframTechConf

Introduction

Zero-dimensional components:

mr = DiscretizeRegion[ImplicitRegion[x^2+y^2==1 && y==Sin[2 Pi x],{x,y}]];
mr = HighlightMesh[mr,Style[0,Directive[PointSize[0.03],Red]]];
Show[mr, ContourPlot[{x^2+y^2==1,y==Sin[2*Pi*x]},{x,-1,1},{y,-1,1}]]

One-dimensional components:

{DiscretizeRegion[Circle[]], DiscretizeRegion[ImplicitRegion[x^2+y^2==1&&z==Sin[2 Pi x],{x,y,z}]]}

Two-dimensional components:

{DiscretizeRegion[Disk[]],DiscretizeRegion[ImplicitRegion[-3/4+(-3*x+4*x^3)^2-(-3*x+4*x^3)*(1-8*y^2+8*y^4)+(1-8*y^2+8*y^4)^2≤0,{x,y}]], DiscretizeRegion[ImplicitRegion[x^2+y^2==z,{x,y,z}]], DiscretizeRegion[Sphere[],PlotRange->{{0,1},{0,1},{0,1}}]}

Three-dimensional components:

DiscretizeRegion[Ball[],PlotRange->{{0,1},{0,1},{0,1}}, ImageSize->300]

Multiple components can exist in the same region.

DiscretizeRegion[ImplicitRegion[(x^2+y^2==1&&z==0.1*Sin[2Pi x]) || (x^2+y^2==z && z≤1) || (x^2 + y^2+(z-2)^2≤1),{x,y,z}]]

Zero-Dimensional Components

2D/3D: NSolve, Sampling

mr = DiscretizeRegion[ImplicitRegion[x^2+y^2==1 &&y-2x^2+3/2==0,{x,y}],{{-2,2},{-2,2}},Method->{"Discretization"-> {0-> "NSolve"} }];
NSolve[x^2+y^2==1 &&y-2x^2+3/2==0,{x,y},Reals]
Show[HighlightMesh[mr,Style[0,Directive[PointSize[0.05],Red]]],ContourPlot[{x^2+y^2==1,y-2x^2+3/2==0},{x,-2,2},{y,-2,2}]]

mr = DiscretizeRegion[ImplicitRegion[x^2+y^2==1 &&y-2x^2+3/2==0,{x,y}],{{-2,2},{-2,2}},Method->{"Discretization"-> {0-> {"Sampling","SamplePoints"-> 1000}} }];
Show[HighlightMesh[mr,Style[0,Directive[PointSize[0.05],Red]]],ContourPlot[{x^2+y^2==1,y-2x^2+3/2==0},{x,-2,2},{y,-2,2}]]

One-Dimensional Components

2D: Continuation, DiscretizeGraphics, MarchingSquares, DualMarchingSquares, Semialgebraic.
3D: MarchingCubes, DualMarchingCubes, Semialgebraic.

DiscretizeRegion[ImplicitRegion[x^2+y^2==1,{x,y}],Method->{"Discretization"-> #}]& /@ {"Continuation","DiscretizeGraphics","MarchingSquares", "DualMarchingSquares", "Semialgebraic"}

TimeConstrained[DiscretizeRegion[ImplicitRegion[(x^2+y^2==1&&z==Sin[3Pi x]),{x,y,z}],Method->#],2]&/@{"MarchingCubes", "DualMarchingCubes","Semialgebraic"}

TimeConstrained[DiscretizeRegion[ImplicitRegion[(x^2+y^2==1&&z==Sin[3Pi x]),{x,y,z}],Method->{"Discretization"-> #}],2]&/@{"MarchingCubes", "DualMarchingCubes","Semialgebraic"}

Two-Dimensional Components

2D: Continuation, MarchingSquares, DualMarchingSquares, RegionPlot,Semialgebraic, Boolean.
3D: MarchingCubes, DualMarchingCubes, ContourPlot3D, RegionPlot3D, Semialgebraic.

ir2D = ImplicitRegion[-3/4+(-3*x+4*x^3)^2-(-3*x+4*x^3)*(1-8*y^2+8*y^4)+(1-8*y^2+8*y^4)^2≤0,{x,y}];
DiscretizeRegion[ir2D, Method->{"Discretization"-> #}]& /@ {"Continuation", "MarchingSquares","DualMarchingSquares", "RegionPlot", "Semialgebraic", "Boolean"}

To get better results with MarchingSquares, DualMarchingSquares, increase the resolution at which the curve is sampled.

DiscretizeRegion[ir2D, Method->{"Discretization"-> {#,"PlotPoints"->1000}}]&/@{"MarchingSquares","DualMarchingSquares"}

Use DualMarchingSquares to better resolve sharp corners.

DiscretizeRegion[ImplicitRegion[0≤x≤1&&0≤y≤1,{x,y}],Method->{"Discretization"-> #}]&/@ {"MarchingSquares","DualMarchingSquares"}

Three-Dimensional Components

3D: MarchingCubes, DualMarchingCubes, Semialgebraic, RegionPlot3D, ContourPlot3D.

DiscretizeRegion[ImplicitRegion[x^2 + y^2+z^2≤1,{x,y,z}],Method->{"Discretization"->#}]&/@{"MarchingCubes","DualMarchingCubes","Semialgebraic","RegionPlot3D","ContourPlot3D"}

The differences between the methods become apparent without post-processing:

DiscretizeRegion[ImplicitRegion[x^2 + y^2+z^2≤1,{x,y,z}],Method->{"Discretization"->#, "PostProcessing"->None }]&/@{"MarchingCubes","DualMarchingCubes","Semialgebraic","RegionPlot3D","ContourPlot3D"}

PostProcessing

2D : ResampleCurves, ImproveBoundaries, SmoothMesh.
3D : ImproveBoundaries, SmoothMesh.

At the end of every discretization, the result is processed to generate better quality results.

DiscretizeRegion[ImplicitRegion[x^2+y^2≤1,{x,y}], Method->{"Discretization"-> #, "PostProcessing"->None}]&/@{"Continuation", "MarchingSquares", "Semialgebraic"}

DiscretizeRegion[ImplicitRegion[x^2+y^2-z^2==1,{x,y,z}],{{-2,2},{-2,2},{-2,2}}, Method->{"Discretization"->#,"PostProcessing"->None}]& /@ {"MarchingCubes","DualMarchingCubes", "Semialgebraic"}

Triangulation

{mr1 = DiscretizeRegion[Disk[]],mr2 = DiscretizeRegion[Disk[], Method->{"Triangulation"->None}]}

ir2D = ImplicitRegion[-3/4+(-3*x+4*x^3)^2-(-3*x+4*x^3)*(1-8*y^2+8*y^4)+(1-8*y^2+8*y^4)^2≤0,{x,y}];
DiscretizeRegion[ir2D,Method->{"Triangulation"->None}]

In 3D, there are only small class of problems (with the given methods) where no triangulation can be applied.

g=MeshRegion[{{1.,-1.,0.},{-1.,-1.,0.},{1.,1.,0.},{0.,0.,1.},{-1.,1.,0.}},{Polygon[{{1,2,3},{2,1,4},{1,3,4},{4,3,5},{3,2,5},{2,4,5}}]}];
DiscretizeRegion[g,Method->{"Triangulation"->#},MaxCellMeasure->{"Length"->1}]& /@ {True, None}

Thanks!