Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Support for 3d Points #7

Open
darwinharianto opened this issue Jun 6, 2022 · 1 comment
Open

Support for 3d Points #7

darwinharianto opened this issue Jun 6, 2022 · 1 comment

Comments

@darwinharianto
Copy link

darwinharianto commented Jun 6, 2022

Thank you for making this repository, I have a little request to support on 3d points
I take a look on the repository, and seems like I only need to change the triangle area calculation.
Is it correct to do it like this?


def triangle_area(p1, p2, p3):
    """
    calculates the area of a triangle given its vertices
    """
    return np.linalg.norm(np.cross(p2 - p1, p3 - p1)) / 2


def triangle_areas_from_array(arr):
    """
    take an (N,2) or (N,3) array of points and return an (N,1)
    array of the areas of those triangles, where the first
    and last areas are np.inf
    see triangle_area for algorithm
    """

    result = np.empty((len(arr),), arr.dtype)
    result[0] = np.inf
    result[-1] = np.inf

    p1 = arr[:-2]
    p2 = arr[1:-1]
    p3 = arr[2:]

    area = np.cross(p2 - p1, p3 - p1, axis=1)
    if len(area.shape) > 1:
        area = np.linalg.norm(area, axis=1) / 2
    else:
        area = np.abs(area) / 2

    result[1:-1] = area
    return result
fitnr added a commit that referenced this issue Jun 6, 2022
@fitnr
Copy link
Owner

fitnr commented Jun 6, 2022

This looks pretty good. I just added these commits to a new branch and it doesn't seem to break anything. Before merging, I would like to add some tests that use 3d points

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants