ValueError: shapes (9,) and (3,) not aligned: 9 (dim 0) != 3 (dim 0)

45 Views Asked by At

I try to create a function that changes the coordinate system of a point cloud from global to local. My inputs are these: point_cloud (numpy.ndarray): Array representing the point cloud with shape (N,9), start (numpy.ndarray): Array representing the start point of the new coordinate system with shape (3,). Represents the x, y, z coordinates of the start point, stop (numpy.ndarray): Array representing the stop point of the new coordinate system with shape (3,). Represents the x, y, z coordinates of the stop point. And I want to return a numpy.ndarray (Array representing the transformed points of the point cloud in the new coordinate system). The problem is that when i calculate the dot product of the point cloud and the vector I get this error: ValueError: shapes (9,) and (3,) not aligned: 9 (dim 0) != 3 (dim 0)

This is the function I use:

def change_coordinate_system(point_cloud, start, stop):
    """
    Changes the coordinate system of a point cloud from global to local.

    Args:
        point_cloud (numpy.ndarray): Array representing the point cloud with shape (N, 9).
        start (numpy.ndarray): Array representing the start point of the new coordinate system
            with shape (3,). Represents the x, y, z coordinates of the start point.
        stop (numpy.ndarray): Array representing the stop point of the new coordinate system
            with shape (3,). Represents the x, y, z coordinates of the stop point.

    Returns:
        numpy.ndarray: Array representing the transformed points of the point cloud in the new
        coordinate system. Has shape (N,) where N is the number of points.

    Raises:
        ValueError: If the shapes of the input arrays are not valid.

    Note:
        The function assumes that the point cloud is given in the global coordinate system,
        and it transforms the points to the local coordinate system defined by the start and
        stop points.

        The transformation is done by calculating the forward and right vectors of the new
        coordinate system based on the start and stop points. The translation vector is calculated
        using the first point of the point cloud as the reference point. The points are then
        projected onto the right vector using dot product.

        The function returns an array of transformed points in the local coordinate system.
    """
    # Calculate forward vector
    forward = stop - start
    forward /= np.linalg.norm(forward)

    # Calculate right vector
    right = np.cross(forward, np.array([0, 0, 1]))
    right /= np.linalg.norm(right)

    # Calculate translation vector
    reference_point = point_cloud[0]  # Select the first point as the reference point
    translation = reference_point

    # Project the points onto the right vector
    transformed_points = []
    for point in point_cloud:
        transformed_points.append(np.dot(point - translation, right))

    return np.array(transformed_points)

The purpose of changing the coordinate system from global to local is that I want to create a profile of the 9th column of my point cloud. So in this profile I want the coordinates to be orientated, that's why I started from this function, to change the coordinate system of the points.

0

There are 0 best solutions below