Draw / select a sphere in a three-dimensional grid

I want to perform voxel-based voxel measurement presented in a numpy array. Because of the selection, these spheres are represented as a group of cubes (because they are selected in an array). I want to simulate an introduced error with this grid constraint. Is there a way to draw a 3D sphere in a multi-dot grid to trigger my simulation? (So, basically, the sphere of unit length is one, it will be one point in the array)

Or is there another way to calculate the error introduced by sampling?

In 2-D, this seems easy ... Sampling in 2D

+4
source share
1 answer

The most direct approach is to create an array of bounding boxes, keeping at each point the distance to the center of the sphere:

>>> radius = 3 >>> r2 = np.arange(-radius, radius+1)**2 >>> dist2 = r2[:, None, None] + r2[:, None] + r2 >>> volume = np.sum(dist2 <= radius**2) >>> volume 123 

A 2D document is easier to visualize:

 >>> dist2 = r2[:, None] + r2 >>> (dist2 <= radius**2).astype(np.int) array([[0, 0, 0, 1, 0, 0, 0], [0, 1, 1, 1, 1, 1, 0], [0, 1, 1, 1, 1, 1, 0], [1, 1, 1, 1, 1, 1, 1], [0, 1, 1, 1, 1, 1, 0], [0, 1, 1, 1, 1, 1, 0], [0, 0, 0, 1, 0, 0, 0]]) >>> np.sum(dist2 <= radius**2) 29 
+5
source

All Articles