# Visualize 3D Vector Field in Voxels with Mask

Is there a way to view a vector field specified in voxels with a mask?

I have three 3D arrays with $x,y,z$ components of the fields at each voxel, as well as a 3D array of booleans saying which voxels should be viewed. All four arrays are the same size (roughly 60 x 60 x 60.)

So for a little test example:

testX = { (* X components *)
{{-1, -1, -1}, {1, 1, 1}, {-1, -1, -1}},
{{1, 1, 1}, {1, 1, 1}, {1, 1, 1}},
{{-1, -1, -1}, {1, 1, 1}, {1, 1, 1}}
};
testY = { (* Y components *)
{{1, 1, 1}, {-1, -1, -1}, {-1, -1, -1}},
{{-1, -1, -1}, {1, 1, 1}, {-1, -1, -1}},
{{-1, -1, -1}, {-1, -1, -1}, {1, 1, 1}}
};
testZ = { (* Z components *)
{{1, 1, 1}, {-1, -1, -1}, {1, 1, 1}},
{{1, 1, 1}, {1, 1, 1}, {1, 1, 1}},
{{-1, -1, -1}, {-1, -1, -1}, {1, 1, 1}}
};
{{False, True, False}, {True, False, True}, {False, True, False}},
{{False, True, False}, {True, False, True}, {False, True, False}},
{{False, True, False}, {True, False, True}, {False, True, False}}
};


So in this test example the voxel at $(1,1,2)$ should show the vector $(-1,1,1)$, but voxel $(1,1,1)$ should not display.

• How should a voxel "show" a vector? I am not sure that I can see what visualization method you had in mind. – MarcoB Feb 9 '17 at 17:06
• @MarcoB just draw the vector in it (i.e. a 3d vector field). Like VectorPlot3D. But I want to mask some voxels. If necessary, I could assign $x,y,z$ values to each voxel (say voxel $(1,1,1)$ would be at $(1,1,1)$ in space) I guess. (But I figured there might be something like matlab's quiver which doesn't require $x,y$ values.) – user3658307 Feb 9 '17 at 17:22
• So you imagine a three-dimensional grid of cubes and there is one arrow in each cube. But some of the arrows are not being displayed, as specified by mask? – C. E. Feb 9 '17 at 23:05
• @C.E. Yes :) I'm hoping to be able to slice it, draw local streamlines in specific slices of the array, simulate a PDE in them, etc... later on. – user3658307 Feb 10 '17 at 0:51

I managed to do it crudely on my own (although I am certain it is suboptimal in many ways). In particular, it is not very visually pleasing and is slow for many vectors.

Here it is:

possibleElems = Array[{#1, #2, #3} &, {3, 3, 3}, {{1, 3}, {1, 3}, {1, 3}}];
hasMaskTrue = Select[ (mask[[  #[[1]], #[[2]], #[[3]]  ]]) &];
elemsToKeep = Flatten[hasMaskTrue /@ Flatten[possibleElems , 1], 1];
vecList = Map[
{{#[[1]], #[[2]], #[[3]]},
{ testX[[#[[1]], #[[2]], #[[3]]]],
testY[[#[[1]], #[[2]], #[[3]]]],
testZ[[#[[1]], #[[2]], #[[3]]]]
}
} &,
possibleElems, {3}]
ListVectorPlot3D[ vecList, VectorPoints -> elemsToKeep ]


If anyone has a better solution, I encourage them to post it. :)