Exemplo n.º 1
0
def intersection_line_triangle(line, triangle, tol=1e-6):
    """Computes the intersection point of a line (ray) and a triangle
    based on the Moeller Trumbore intersection algorithm

    Parameters
    ----------
    line : tuple
        Two points defining the line.
    triangle : list of list of float
        XYZ coordinates of the triangle corners.
    tol : float, optional
        A tolerance for membership verification.
        Default is ``1e-6``.

    Returns
    -------
    point : tuple
        The intersectin point.
    None
        If the intersection does not exist.

    """
    a, b, c = triangle
    ab = subtract_vectors(b, a)
    ac = subtract_vectors(c, a)
    n = cross_vectors(ab, ac)
    plane = a, n

    x = intersection_line_plane(line, plane, tol=tol)

    if x:
        if is_point_in_triangle(x, triangle):
            return x
Exemplo n.º 2
0
    def in_triangle(self, triangle):
        """Determine if the point lies in the given triangle.

        Parameters
        ----------
        triangle : triangle
            The triangle.

        Returns
        -------
        bool
            True, if the point lies in the triangle.
            False, otherwise.

        """
        return is_point_in_triangle(self, triangle)
Exemplo n.º 3
0
def intersection_line_triangle(line, triangle, tol=1e-6):
    """Computes the intersection point of a line (ray) and a triangle
    based on the Moeller Trumbore intersection algorithm

    Parameters
    ----------
    line : tuple
        Two points defining the line.
    triangle : list of list of float
        XYZ coordinates of the triangle corners.
    tol : float, optional
        A tolerance for membership verification.
        Default is ``1e-6``.

    Returns
    -------
    point : tuple
        The intersectin point.
    None
        If the intersection does not exist.

    """
    # a, b, c = triangle
    # v1 = subtract_vectors(line[1], line[0])
    # p1 = line[0]
    # # Find vectors for two edges sharing V1
    # e1 = subtract_vectors(b, a)
    # e2 = subtract_vectors(c, a)
    # # Begin calculating determinant - also used to calculate u parameter
    # p = cross_vectors(v1, e2)

    # # if determinant is near zero, ray lies in plane of triangle
    # det = dot_vectors(e1, p)
    # if det > - epsilon and det < epsilon:
    #     return None

    # inv_det = 1.0 / det
    # # calculate distance from V1 to ray origin
    # t = subtract_vectors(p1, a)

    # # Calculate u parameter
    # u = dot_vectors(t, p) * inv_det
    # # The intersection lies outside of the triangle
    # if u < 0.0 or u > 1.0:
    #     return None

    # # Prepare to make_blocks v parameter
    # q = cross_vectors(t, e1)
    # # Calculate V parameter

    # v = dot_vectors(v1, q) * inv_det
    # # The intersection lies outside of the triangle
    # if v < 0.0 or u + v > 1.0:
    #     return None

    # t = dot_vectors(e2, q) * inv_det
    # if t > epsilon:
    #     return add_vectors(p1, scale_vector(v1, t))

    # # No hit
    # return None

    a, b, c = triangle
    ab = subtract_vectors(b, a)
    ac = subtract_vectors(c, a)
    n = cross_vectors(ab, ac)
    plane = a, n

    x = intersection_line_plane(line, plane, tol=tol)

    if x:
        if is_point_in_triangle(x, triangle):
            return x