@@ -201,27 +201,37 @@ def backproject_box(bounding_box: BoundingBox, slice_rects: np.ndarray, slices:
201201 # No slices, just return
202202 return np .empty ((0 ), dtype = np .uint32 ), np .empty (0 , dtype = np .float32 ), np .empty (0 , dtype = np .float32 )
203203
204+ # Plot the backprojected X/Y/Z coordinates for each of the slice rects in this chunk.
205+ grid_call = partial (coordinate_grid , shape = slices [0 ].shape , floor = bounding_box .get_min (), flip = True )
206+ precise_points = np .concatenate (list (map (grid_call , slice_rects )))
207+
208+ # Flatten values to 1-Dimension Array for Efficient Allocation.
209+ # Use ZYX domain rather than XYZ as the former is what is written to disk.
204210 values = slices .flatten ()
205211 zyx_shape = np .flip (bounding_box .get_shape ())
206212 flat_shape = np .prod (zyx_shape )
207213
208- grid_call = partial ( coordinate_grid , shape = slices [ 0 ]. shape , floor = bounding_box . get_min (), flip = True )
209- precise_points = np .concatenate ( list ( map ( grid_call , slice_rects )) )
214+ # Determine minimum dtype for index of flattened shape.
215+ squish_type = np .min_scalar_type ( flat_shape )
210216
217+ # Allocate the flattened data volume.
211218 volume = np .zeros ((2 , flat_shape ), dtype = np .float32 )
212- squish_type = np .min_scalar_type (flat_shape )
213219
220+ # Get the top corner (integer) points and full weight matrix.
214221 points , weights = _points_and_weights (precise_points .reshape (- 1 , 3 ).T , zyx_shape , squish_type )
215222
223+ # Sequentially allocate the points and weights for each corner to the flattened array.
216224 for corner in np .array (list (np .ndindex (2 , 2 , 2 ))):
217225 w_values , c_weights = _apply_weights (values , weights , corner )
218226 point_inc = np .ravel_multi_index (corner , zyx_shape ).astype (squish_type )
219227
220228 np .add .at (volume [0 ], points + point_inc , w_values )
221229 np .add .at (volume [1 ], points + point_inc , c_weights )
222230
231+ # Get indicies of the flattened Z-Y-X backprojected domain that have values.
223232 nz_vol = np .flatnonzero (volume [0 ])
224233
234+ # Return indicies and only the volume region with values.
225235 return nz_vol , volume [0 , nz_vol ].squeeze (), volume [1 , nz_vol ].squeeze ()
226236
227237
0 commit comments