def test_triangle_area():
    a = geom.Point2D( [0.0, 0.0] )
    b = geom.Point2D( [1.0, 0.0] )
    c = geom.Point2D( [1.0, 1.0] )

    triangle2D_1 = geom.Triangle2D( a, b, c )
    if geom.triangle_area2D( triangle2D_1 ) != 0.5:
        raise ValueError( "[Test] Wrong result for triangle_area with query triangle triangle2D_1" )

    triangle2D_2 = geom.Triangle2D( a, b, a )
    if geom.triangle_area2D( triangle2D_2 ) != 0.0:
        raise ValueError( "[Test] Wrong result for triangle_area with query triangle triangle2D_2" )
def test_point_triangle_distance():
    a = geom.Point2D([0.0, 0.0])
    b = geom.Point2D([1.0, 0.0])
    c = geom.Point2D([1.0, 1.0])
    triangle2D = geom.Triangle2D(a, b, c)

    distance, closest_point = geom.point_triangle_distance2D(a, triangle2D)
    if distance != 0 or closest_point != a:
        raise ValueError(
            "[Test] Wrong result for point_triangle_distance2D with query point a"
        )

    distance, closest_point = geom.point_triangle_distance2D(b, triangle2D)
    if distance != 0 or closest_point != b:
        raise ValueError(
            "[Test] Wrong result for point_triangle_distance2D with query point b"
        )

    q1 = geom.Point2D([0.5, 0.5])
    distance, closest_point = geom.point_triangle_distance2D(q1, triangle2D)
    if distance != 0 or closest_point != q1:
        raise ValueError(
            "[Test] Wrong result for point_triangle_distance2D with query point q1"
        )

    q2 = geom.Point2D([0.0, 1.0])
    distance, closest_point = geom.point_triangle_distance2D(q2, triangle2D)
    result_q2 = geom.Point2D([0.5, 0.5])
    if distance != math.sqrt(2) / 2. or closest_point != result_q2:
        raise ValueError(
            "[Test] Wrong result for point_triangle_distance2D with query point q2"
        )

    q3 = geom.Point2D([2.0, 1.0])
    distance, closest_point = geom.point_triangle_distance2D(q3, triangle2D)
    if distance != 1 or closest_point != c:
        raise ValueError(
            "[Test] Wrong result for point_triangle_distance2D with query point q3"
        )

    q4 = geom.Point2D([0.5, 0.5])
    distance, closest_point = geom.point_triangle_distance2D(q4, triangle2D)
    if distance != 0 or closest_point != q4:
        raise ValueError(
            "[Test] Wrong result for point_triangle_distance2D with query point q4"
        )
Example #3
0
def test_triangle_barycentric_coordinates():
    a = geom.Point2D([0.0, 0.0])
    b = geom.Point2D([1.0, 0.0])
    c = geom.Point2D([1.0, 1.0])
    triangle2D = geom.Triangle2D(a, b, c)

    if not check_triangle_bary_coords(
            geom.triangle_barycentric_coordinates2D(a, triangle2D), [1, 0, 0]):
        raise ValueError(
            "[Test] Wrong result for triangle_barycentric_coordinates with query point a"
        )
    if not check_triangle_bary_coords(
            geom.triangle_barycentric_coordinates2D(b, triangle2D), [0, 1, 0]):
        raise ValueError(
            "[Test] Wrong result for triangle_barycentric_coordinates with query point b"
        )
    if not check_triangle_bary_coords(
            geom.triangle_barycentric_coordinates2D(c, triangle2D), [0, 0, 1]):
        raise ValueError(
            "[Test] Wrong result for triangle_barycentric_coordinates with query point c"
        )

    q1 = geom.Point2D([0.25, 0.0])
    if not check_triangle_bary_coords(
            geom.triangle_barycentric_coordinates2D(q1, triangle2D),
        [0.75, 0.25, 0]):
        raise ValueError(
            "[Test] Wrong result for triangle_barycentric_coordinates with query point q1"
        )

    q2 = geom.Point2D([0.5, 0.25])
    if not check_triangle_bary_coords(
            geom.triangle_barycentric_coordinates2D(q2, triangle2D),
        [0.5, 0.25, 0.25]):
        raise ValueError(
            "[Test] Wrong result for triangle_barycentric_coordinates with query point q2"
        )

    q3 = geom.Point2D([0.0, 1.0])
    if not check_triangle_bary_coords(
            geom.triangle_barycentric_coordinates2D(q3, triangle2D),
        [1, -1, 1]):
        raise ValueError(
            "[Test] Wrong result for triangle_barycentric_coordinates with query point q3"
        )