MCPcopy Create free account
hub / github.com/apple/ml-pointersect / get_grid_idx

Function get_grid_idx

pointersect/pr/cpp/pr.cpp:239–274  ·  view source on GitHub ↗

Compute the grid index given xyz_w. Args: points: (*, n, 3) grid_size: (*, 3) long. number of grid cells in x y z. center: (*, 3) center of the grid grid_width: (*, 3) length (full width) of the grid in xyz mode: 'subidx': return sub idx 'ind': return linear index Returns: grid_idx: if mode == 'subidx': (*, n, 3) long elif mode == 'ind': (*, n) long valid_mask: (*, n) bool Algorithm: Let x_

Source from the content-addressed store, hash-verified

237// grid_idx = z_idx + y_dix * grid_size_z + x_idx * (grid_size_y * grid_size_z)
238//
239std::vector<torch::Tensor> get_grid_idx(
240 const torch::Tensor & points, // (*, n, 3), float
241 const torch::Tensor & grid_size, // (*, 3), long
242 const torch::Tensor & center, // (*, 3), float
243 const torch::Tensor & grid_width, // (*, 3), float
244 const std::string & mode = "ind"
245) {
246
247 assert(grid_size.dtype() == torch::kLong);
248
249 auto grid_from = (center - grid_width / 2).unsqueeze(-2); // (*, 1, 3)
250 auto grid_to = (center + grid_width / 2).unsqueeze(-2); // (*, 1, 3)
251 auto cell_width = (grid_width / grid_size).unsqueeze(-2); // (*, 1, 3)
252 auto p_idx = ((points - grid_from) / cell_width).floor().to(torch::kLong); // (*, n, 3), sub_idx on the grid
253
254 // we will mark any out-of-bound points invalid
255 auto valid_mask = torch::logical_and(
256 points >= grid_from,
257 points <= grid_to
258 ).all(-1); // (*, n), bool
259
260 if (mode.compare("ind") == 0) {
261 auto grid_idx = sub2ind(
262 p_idx,
263 grid_size
264 ); // (*, n) long
265 return {grid_idx, valid_mask};
266 }
267 else if (mode.compare("subidx") == 0) {
268 return {p_idx, valid_mask};
269 }
270 else {
271 throw;
272 }
273
274}
275
276
277

Callers 3

grid_ray_intersectionFunction · 0.70
grid_ray_intersection_v2Function · 0.70

Calls 3

dtypeMethod · 0.80
sub2indFunction · 0.70
toMethod · 0.45

Tested by

no test coverage detected