# Mathematica function intersection points with 3D grid

I need to produce a 3-dimensional equispaced grid over a given function in a way, that I can calculate intersection points of the function with the grids edges. So my first question is how to produce this grid in Mathematica. I don't really get if this is possible by modeling the grid by Polygons? For this I might need some help then, too. Or can it be done by using more simple graphics objects like done here?

-

## migrated from stackoverflow.comJul 25 '12 at 12:19

This question came from our site for professional and enthusiast programmers.

What do you exactly mean by grid? A grid of points? Gridlines? A grid of planes? Can you elaborate? It's not clear to me what the "intersection of a function with a grid" is. Also, exactly what kind of function do you have? –  Szabolcs Jul 24 '12 at 8:47
In general, I have a function giving a surface of an object. Now, I want to get the points at which this surface intersects with edges of some kind of grid. Its like getting the intersection of a plane going through a cube. I hope this makes it more clear... –  MathM Jul 24 '12 at 14:04

Do I understand it correctly that you are looking for the intersection of an implicitly defined surface with planes (planes that could make up a grid)?

Suppose we have this surface ...

j = 1.25;

ContourPlot3D[
x^4 + y^4 + z^4 - (x^2 + y^2 + z^2) == -2/5,
{x, -j, j}, {y, -j, j}, {z, -j, j},
Mesh -> False, Axes -> False, PlotPoints -> 30]


... and we want to visualize the intersection with the plane $x + 2y - z$:

ContourPlot3D[x + 2 y - z == 0, {x, -j, j}, {y, -j, j}, {z, -j, j},
Axes -> False]


The simplest way is to use custom MeshFunctions with ContourPlot3D:

gr =ContourPlot3D[x^4 + y^4 + z^4 - (x^2 + y^2 + z^2) == -2/5,
{x, -j, j}, {y, -j, j}, {z, -j, j},
Axes -> False, PlotPoints -> 30,
MeshFunctions -> {Function[{x, y, z}, x + 2 y - z]},
Mesh -> {{0.}}, MeshStyle -> Thick]


Or take the intersections with planes parallel to $yz$:

Update: You can extract the coordinates for the points making up the lines like this: Cases[Normal[gr], _Line, Infinity].

-
Thanks for your answer! But I do not only need to get it as a Meshfunction because I need to calculate some points at which the function is intersected by the plane. Like in the answer below. Maybe you have a idea? –  MathM Aug 1 '12 at 10:38
@MathM I realise this is not ideal, but you can extract the mesh lines from the graphics expression. Try Graphics3D@Cases[Normal[gr], _Line, Infinity], where gr contains the intersection line. It'll show you the lines only. Now write Cases[Normal[gr], _Line, Infinity] only and you'll see all coordinates that make up the line. Caveat: since the methods that compute the mesh lines are meant for visualization, they are not going to give very precise results. MaxRecursion and PlotPoints can be used to increase precision a bit (at significant cost to computation time). –  Szabolcs Nov 12 '12 at 20:08

Here's an answer to my interpretation of the question:

Start with a three dimensional grid of lines:

lines = With[{j = 1.25, h = 0.25}, {
Table[Line[{{x, y, -j}, {x, y, j}}], {x, -j, j, h}, {y, -j, j, h}],
Table[Line[{{x, -j, z}, {x, j, z}}], {x, -j, j, h}, {z, -j, j, h}],
Table[Line[{{-j, y, z}, {j, y, z}}], {y, -j, j, h}, {z, -j, j, h}]
}];


Turn each line into rule which gives a parameterisation of the line:

rules = Flatten[lines] /. Line[{{ax_, ay_, az_}, {bx_, by_, bz_}}] ->
{x -> ax + t (bx - ax), y -> ay + t (by - ay), z -> az + (bz - az)};


Now solve t in whatever implicit equation you have, and extract the valid solution points:

points = Cases[{x, y, z} /. # /. NSolve[x^4 + y^4 + z^4 - (x^2 + y^2 + z^2) == -2/5 /. #,
t, Reals] & /@ rules, {_?NumberQ, _?NumberQ, _?NumberQ}, Infinity];


And plot the results:

Graphics3D[{PointSize[0.015], Point[points], Opacity[0.25], lines}, Boxed -> False]


-

From your description, it seems that you need to find the points of intersection between a set of lines and your function, the lines being part of your "grid."

When you say intersection with the grid's edges, I am assuming that you mean points on the grid lines in the following sense: each grid unit makes cube of regular dimensions. You want to determine the intersection of the function with cube faces.

If this is the case, all you need to do is solve for intersection with sets of plane equations.

-
That is exactly what I like to do! So you mean using the Mathematica Solve-Command with plane equations. But I don't really know how do it with my function (which is a implicit defined surface like Szabolcs showed us on the top of this page). –  MathM Aug 1 '12 at 10:30