# 3D indicator function of a box with balls

I want to define the indicator function of finite number of identical balls with given centers and diameter in a box in Wolfram Mathematica. The box has the dimensions $0 \leq x_1 \leq l$, $0 \leq x_2 \leq l$, $0 \leq x_3 \leq 1$, the diameter of a typical ball is $d$.

My try is as follows (for 1 ball):

UnitStep[1/4 d^2 - (x1 - x01)^2 - (x2 - x02)^2 - (x3 - x03)^2]

which seems to be logical. However, for d = 0.1, x01 = 0.75, x02 = 0.5, x03 = 0.5, l = 5 and any 0 < x3 < 1, this function results in

How to define the distribution of, e.g., 10 equidistant balls in the box, if x03 = 0.5? Any hint or suggestion is highly appreciated.

Edit

I also tried with Piecewise:

Piecewise[{{1, (x1 - 0.5)^2 + (x2 - 0.5)^2 <=
1/4 0.5^2}, {1, (x1 - 1.5)^2 + (x2 - 0.5)^2 <=
1/4 0.5^2}, {1, (x1 - 2.5)^2 + (x2 - 0.5)^2 <=
1/4 0.5^2}, {1, (x1 - 3.5)^2 + (x2 - 0.5)^2 <=
1/4 0.5^2}, {1, (x1 - 4.5)^2 + (x2 - 0.5)^2 <= 1/4 0.5^2}}, 0]


which plots

But I am not sure that this will provide the required result if I add (x3-x03)^2 to the conditions at Piecewise.

• Try with RegionPlot3D instead of ContourPlot3D, which I'm guessing is what you are using. – Daniel Lichtblau Aug 19 '18 at 12:47
• No, I use Plot3D. – Asatur Khurshudyan Aug 19 '18 at 23:50

## 1 Answer

Here are two solutions: one with ImplicitRegion and Show that is somewhat slow, and one with RegionPlot3D as Dan suggested. (Since indicator function is requested, I guess second solution's part Apply[Or, First /@ balls] is of interest.)

d = 0.05;
cs = RandomReal[{0.1, 0.9}, {10, 3}];

balls = ImplicitRegion[
Sqrt[(#[[1]] - x)^2 + (#[[2]] - y)^2 + (#[[3]] - z)^2] <= d, {x, y, z}] & /@ cs;

Show[Region /@ balls, Boxed -> True,
PlotRange -> {{0, 1}, {0, 1}, {0, 1}}]


RegionPlot3D[
Apply[Or, First /@ balls], {x, 0, 1}, {y, 0, 1}, {z, 0, 1},
PerformanceGoal -> "Quality", PlotPoints -> 40]


• Since I need to integrate the requested indicator function with respect to x3, I need it to be expressed in terms of Piecewise, UnitStep, HeavisideTheta, etc. I tried to integrate Apply[...], and got integral of inequalities with ||. – Asatur Khurshudyan Aug 20 '18 at 0:07
• @AsaturKhurshudyan You can use Boole[Apply[Or, First /@ balls]] in NIntegrate and Integrate. (Also, in your question you did not say anything about integration...) – Anton Antonov Aug 20 '18 at 0:50
• Theoretically, UnitStep[0.25 d^2 - x1^2 - x2^2 - x3^2] characterizes a ball of diameter d centered at [0, 0, 0]. Why it is not giving what it sopposed to give? – Asatur Khurshudyan Aug 20 '18 at 1:51
• Plot3D and ContourPlot3D I guess are looking for 2 dimensional surfaces and not getting the depth part. That's just a guess though. As noted, Boole is just fine for integration. Likewise ImplicitRegion I believe. – Daniel Lichtblau Aug 20 '18 at 2:06
• @AsaturKhurshudyan Try this: Block[{x01, x02, x03 = 0.5, d = 0.03}, x01[n_] := 2 n*d; x02[m_] := 2 m*d; Table[{x01[n], x02[n], x03}, {n, 1, 50}] ] – Anton Antonov Aug 21 '18 at 20:03