Skip to main content

graphics3d - Trouble with discrete MeshRegions: Integrating over plane slices


I would like to slice a discrete 3D region with a plane, and integrate some field over the resulting 2D region (embedded in 3D). Let me be clear: I do not mean analytical regions (the kind you could define via ImplicitRegion), but specifically discrete regions, as in my project I am importing those from experimental data.


There are two strategies I have tried. Here is a quick summary:



  • Strategy 1 is to use SliceContourPlot3D with RegionFunction to obtain a 2D region (i.e. surface) embedded in 3D. The problem is that the mesh is faulty and has no boundary, so I cannot e.g. compute fluxes through it. Any suggestions fixing this are more than welcome.


  • Strategy 2 is to transform the problem to in-plane coordinates (i.e. make it a genuine 2D problem) and use the standard two-dimensional RegionPlot. This works. The drawback is that the mesh is purely 2D (no longer embedded in 3D), so I cannot superimpose the result with the original geometry, which is important to me. If there is a nice way of translating/rotating DG2 (effectively increasing its RegionEmbeddingDimension from 2 to 3) I would be very grateful to know.


Now I will construct a simple discrete region and expand the strategies 1 & 2:


IR = ImplicitRegion[
x^6 - 5 x^4 y + 3 x^4 y^2 + 10 x^2 y^3 + 3 x^2 y^4 - y^5 + y^6 +
z^2 <= 1, {x, y, z}];
MR = DiscretizeRegion[IR];

Now I would like to slice MR with some plane.


Strategy 1: SliceContourPlot3D with RegionFunction



Ideally the outcome would be a MeshRegion with RegionDimension 2 and RegionEmbeddingDimension 3. According to this discussion, because RegionPlot3D performs badly with logical statements, a good way to do it is via ContourPlot3D combined with RegionFunction. Here is my attempt:


reg = 1.5;
RP = SliceContourPlot3D[
1, {x, y, z}.{-1, -1, 1} == 0, {x, -reg, reg}, {y, -reg,
reg}, {z, -reg, reg}, PlotPoints -> 20,
RegionFunction ->
Function[{x, y, z}, SignedRegionDistance[MR, {x, y, z}] < 0]];
DG = DiscretizeGraphics[RP];
DG // FindMeshDefects
RegionBoundary[DG]


enter image description here


Unfortunately there are mesh defects and probably as a result of that, the RegionBoundary is not computed correctly - it should be a 1D curve. This becomes a big problem when I try to integrate some field over it:


Integrate[Exp[x^2 + y^2 + z^2], {x, y, z} \[Element] DG]

enter image description here


In summary, I have sliced a 3D mesh successfully, obtained a 2D slice embedded in 3D. However, the problem with the boundary prevents me from analysing the slice (e.g. computing fluxes through it). Any suggestions for fixing this are very welcome.


Strategy 2: A plain two dimensional RegionPlot


Here is an alternative approach to the problem, which does work but is less convenient. The idea is to find the in-plane coordinates (not shown here) and treat a 2D region using the classic RegionPlot:


reg = 1.5;

RP2 = RegionPlot[
RegionMember[MR, {x, y, 0}] == True, {x, -reg, reg}, {y, -reg,
reg}, PlotPoints -> 10];
DG2 = DiscretizeGraphics[RP2];
FindMeshDefects[DG2]
DG2 // RegionBoundary

enter image description here


There are no mesh defects, and the boundary is computed correctly. Integrating some field over DG2 or its boundary is now no problem (thanks to Henrik Schumacher for helping with this in the comments):


Integrate[Exp[x^2 + y^2], {x, y} \[Element] DG2]

Integrate[Exp[x^2 + y^2], {x, y} \[Element] RegionBoundary[DG2]]

The drawback is that the mesh is purely 2D (no longer embedded in 3D), so I cannot superimpose the result with the original geometry, which is important to me. If there is a nice way of translating/rotating DG2 (effectively increasing its RegionEmbeddingDimension from 2 to 3) I would be very grateful to know.


Comment/Disclaimer


I have a feeling Mathematica is not designed for what I am trying to do, but this is not completely obvious to me from documentation. MeshRegions Which come from ImplicitRegion or predefined regions like Ball[] appear to be discretized very differently from inherently discrete regions. The current question is an extension of my previous question.



Answer



If your surface to intersect with is given by an implicit equation (such as $z=0$ in your example), SliceContourPlot3D can produce quite acceptable results.


IR = ImplicitRegion[
x^6 - 5 x^4 y + 3 x^4 y^2 + 10 x^2 y^3 + 3 x^2 y^4 - y^5 + y^6 +
z^2 <= 1, {x, y, z}];

MR = DiscretizeRegion[IR];
f = {x, y, z} \[Function] z;
plot = SliceContourPlot3D[1, f[x, y, z] == 0, {x, y, z} \[Element] MR];
DG3 = DiscretizeGraphics[plot];

Show[
MeshRegion[RegionBoundary[MR], PlotTheme -> "Lines"],
DG3,
Axes -> True,
Boxed -> True

]

enter image description here


Unfortunately, RegionBoundary[DG3] will not produce any MeshRegion that represents the boundary curve any more...


This must be done by hand. Fortunately, SliceContourPlot3D creates line objects for the boundary curves, deeply hidden within a GraphicsComplex. We just have to parse it and reorder some vertex positions:


BDG3 = Module[{gc, lines, plist, pts, s},
gc = Extract[plot, Position[plot, _GraphicsComplex]][[1]];
lines = Extract[gc, Position[gc, _Line]][[All, 1]];
plist = Union @@ lines;
pts = gc[[1, plist]];

s = AssociationThread[plist -> Range[Length[plist]]];
MeshRegion[
gc[[1, plist]], (x \[Function] Line[Lookup[s, x]]) /@ lines]
];
Show[
MeshRegion[RegionBoundary[MR], PlotTheme -> "Lines"],
DG3,
BDG3,
Axes -> True,
Boxed -> True

]

enter image description here


Comments

Popular posts from this blog

functions - Get leading series expansion term?

Given a function f[x] , I would like to have a function leadingSeries that returns just the leading term in the series around x=0 . For example: leadingSeries[(1/x + 2)/(4 + 1/x^2 + x)] x and leadingSeries[(1/x + 2 + (1 - 1/x^3)/4)/(4 + x)] -(1/(16 x^3)) Is there such a function in Mathematica? Or maybe one can implement it efficiently? EDIT I finally went with the following implementation, based on Carl Woll 's answer: lds[ex_,x_]:=( (ex/.x->(x+O[x]^2))/.SeriesData[U_,Z_,L_List,Mi_,Ma_,De_]:>SeriesData[U,Z,{L[[1]]},Mi,Mi+1,De]//Quiet//Normal) The advantage is, that this one also properly works with functions whose leading term is a constant: lds[Exp[x],x] 1 Answer Update 1 Updated to eliminate SeriesData and to not return additional terms Perhaps you could use: leadingSeries[expr_, x_] := Normal[expr /. x->(x+O[x]^2) /. a_List :> Take[a, 1]] Then for your examples: leadingSeries[(1/x + 2)/(4 + 1/x^2 + x), x] leadingSeries[Exp[x], x] leadingSeries[(1/x + 2 + (1 - 1/x...

How to thread a list

I have data in format data = {{a1, a2}, {b1, b2}, {c1, c2}, {d1, d2}} Tableform: I want to thread it to : tdata = {{{a1, b1}, {a2, b2}}, {{a1, c1}, {a2, c2}}, {{a1, d1}, {a2, d2}}} Tableform: And I would like to do better then pseudofunction[n_] := Transpose[{data2[[1]], data2[[n]]}]; SetAttributes[pseudofunction, Listable]; Range[2, 4] // pseudofunction Here is my benchmark data, where data3 is normal sample of real data. data3 = Drop[ExcelWorkBook[[Column1 ;; Column4]], None, 1]; data2 = {a #, b #, c #, d #} & /@ Range[1, 10^5]; data = RandomReal[{0, 1}, {10^6, 4}]; Here is my benchmark code kptnw[list_] := Transpose[{Table[First@#, {Length@# - 1}], Rest@#}, {3, 1, 2}] &@list kptnw2[list_] := Transpose[{ConstantArray[First@#, Length@# - 1], Rest@#}, {3, 1, 2}] &@list OleksandrR[list_] := Flatten[Outer[List, List@First[list], Rest[list], 1], {{2}, {1, 4}}] paradox2[list_] := Partition[Riffle[list[[1]], #], 2] & /@ Drop[list, 1] RM[list_] := FoldList[Transpose[{First@li...

front end - keyboard shortcut to invoke Insert new matrix

I frequently need to type in some matrices, and the menu command Insert > Table/Matrix > New... allows matrices with lines drawn between columns and rows, which is very helpful. I would like to make a keyboard shortcut for it, but cannot find the relevant frontend token command (4209405) for it. Since the FullForm[] and InputForm[] of matrices with lines drawn between rows and columns is the same as those without lines, it's hard to do this via 3rd party system-wide text expanders (e.g. autohotkey or atext on mac). How does one assign a keyboard shortcut for the menu item Insert > Table/Matrix > New... , preferably using only mathematica? Thanks! Answer In the MenuSetup.tr (for linux located in the $InstallationDirectory/SystemFiles/FrontEnd/TextResources/X/ directory), I changed the line MenuItem["&New...", "CreateGridBoxDialog"] to read MenuItem["&New...", "CreateGridBoxDialog", MenuKey["m", Modifiers-...