3

I'm looking for the fastest way to access the mesh cell (i.e. polygon) in a Region that intersects a Line in 3D. For example,

reg = BoundaryDiscretizeRegion[Ball[], PrecisionGoal -> 1, 
   MaxCellMeasure -> 0.1];
Graphics3D[{{Blue, Thick, Line[{{-1, -1, -1}, {0, 0, 0}}]}, 
  Opacity[0.8], reg}, Axes -> True]

enter image description here

Here's a slow naive way:

ps = MeshPrimitives[reg, 2];
intersections = 
  If[EmptyRegion[3] =!= 
      RegionIntersection[#, Line[{{0, 0, 0}, {-1, -1, -1}}]], #, 
     Nothing] & /@ ps;
Graphics3D[{{Red, Line[{{0, 0, 0}, {-1, -1, -1}}]}, intersections}, 
 Axes -> True]

enter image description here

There must be an easy efficient way to find the cell that intersects the line, I'm just not seeing it.

Henrik Schumacher
  • 106,770
  • 7
  • 179
  • 309
M.R.
  • 31,425
  • 8
  • 90
  • 281
  • 1
    What version are you using?, my 11.3 says that BDR is not a graphics3d primitive. – Kuba Dec 11 '18 at 07:31

1 Answers1

2

Before I begin, I'd like to say that this is indeed something that I really miss in Mathematica 11.3.

Well, we can try to employ RegionDistance to find the intersection point as follows:

R = DiscretizeRegion[Sphere[], MaxCellMeasure -> {1 -> 0.5}];
L = Line[{{0, 0, 0}, {-1, -1, -1}}];

d = RegionDistance[R];
curve = (1 - t) L[[1, 1]] + t L[[1, 2]];
pt = curve /. FindRoot[d[curve] == 0, {t, 0, 1}, Method -> "Secant"]

{-0.576338, -0.576338, -0.576338}

For more than one intersection, one may use the good ol' MeshFunction approach:

gc = Cases[
   Plot[d[curve], {t, 0, 1}, MeshFunctions -> {#2 &}, 
    Mesh -> {{0.01}}, MeshStyle -> Red],
   _GraphicsComplex,
   ∞
   ];
candidates = Join[{0.}, Sort[gc[[1, 1, Cases[gc, _Point, ∞][[1, 1]], 1]]], {1.}];
pts = curve /. FindRoot[
 d[curve] == 0, 
 {t, #[[1]], #[[2]]}, 
 Method -> "Secant"] & /@ Partition[candidates, 2, 1];

This returns only the intersection points, not the intersected triangles. But the cell indices of the nearest mesh cells of highest dimension(?) can be obtained by

intersectedtriangles = DeleteDuplicates[Region`Mesh`MeshNearestCellIndex[R, pts]]

{{2, 744}}

Graphics3D[{Thick, L, MeshPrimitives[R, intersectedtriangles]}]

enter image description here

Henrik Schumacher
  • 106,770
  • 7
  • 179
  • 309