Cupy support

Hey,

I’ve playing with some code to speed up voxel generation, is cupy supported in extensions? I’m running win 10 btw

thanks

import cupy as cp
from pxr import Gf

def generate_voxel_grid(voxel_res, min_max, hoz_range, vert_range, voxel_size):
    min_val, max_val = min_max
    hoz_min, hoz_max = hoz_range
    vert_min, vert_max = vert_range

    # Calculate the number of voxels that fit in the range
    range_size = max_val - min_val
    num_voxels = int(range_size / voxel_size) + 1  # Ensuring it covers the entire range

    # Generate a cubic grid with spacing equal to voxel_size, including negative values to cover the entire volume
    x = cp.arange(-max_val, max_val + voxel_size, voxel_size)
    y = cp.arange(-max_val, max_val + voxel_size, voxel_size)
    z = cp.arange(-max_val, max_val + voxel_size, voxel_size)
    xx, yy, zz = cp.meshgrid(x, y, z, indexing='ij')
    points = cp.vstack((xx.ravel(), yy.ravel(), zz.ravel())).T

    # Convert to spherical coordinates
    r = cp.sqrt(cp.sum(points**2, axis=1))
    theta = cp.arctan2(points[:, 1], points[:, 0])
    phi = cp.arctan2(points[:, 2], cp.sqrt(points[:, 0]**2 + points[:, 1]**2))

    # Convert angles to degrees
    theta_deg = cp.degrees(theta)
    phi_deg = cp.degrees(phi)

    # Adjust theta to be in the range [0, 360)
    theta_deg = (theta_deg + 360) % 360

    # Apply filters (restoring original logic)
    mask = (r >= min_val) & (r <= max_val) & \
           ((theta_deg >= hoz_min) & (theta_deg <= hoz_max) | \
            (theta_deg >= hoz_min + 360) | (theta_deg <= hoz_max - 360)) & \
           (phi_deg >= vert_min) & (phi_deg <= vert_max)

    filtered_points = points[mask]
    
    # Convert filtered points to Gf.Vec3f
    filtered_points_cpu = cp.asnumpy(filtered_points)  # Convert to CPU for Gf.Vec3f conversion
    return [Gf.Vec3f(*point) for point in filtered_points_cpu]

def is_inside_voxel_grid(point, voxel_coordinates, voxel_size):
    """
    Check if a point is inside the voxel grid.
    
    :param point: Point to check
    :param voxel_coordinates: List of voxel center coordinates
    :param voxel_size: Size of each voxel
    :return: Boolean indicating if the point is inside the grid
    """
    half_size = voxel_size / 2
    for voxel in voxel_coordinates:
        if all(abs(p - v) <= half_size for p, v in zip(point, voxel)):
            return True
    return False

def check_corners_in_voxel_grid(world_corners, voxel_coordinates, voxel_size):
    """
    Check which corners of a bounding box are inside the voxel grid.
    
    :param world_corners: List of corner points of the bounding box
    :param voxel_coordinates: List of voxel center coordinates
    :param voxel_size: Size of each voxel
    :return: Tuple of (inside_corners, outside_corners)
    """
    inside_corners = []
    outside_corners = []

    for j, corner in enumerate(world_corners):
        if is_inside_voxel_grid(corner, voxel_coordinates, voxel_size):
            inside_corners.append((j+1, corner))
        else:
            outside_corners.append((j+1, corner))

    return inside_corners, outside_corners

This is amazing. What a great code. I will pass it along to the devs. Thanks !!