Пример #1
0
    def test_clockwise(self):

        """
          C
        1 +
          |  \
          |     \
        0 +--------+
          A        B
          0        1
        """

        A = Point2D(0, 0)
        C = Point2D(1, 0)
        B = Point2D(0, 1)
        self.triangle = Triangle2D(A, B, C)

        x = np.array( [A.x, B.x, C.x] )
        y = np.array( [A.y, B.y, C.y] )
        self.data = self.f(x, y)

        self.check_interpolate_at(A.x, A.y)
        self.check_interpolate_at(B.x, B.y)
        self.check_interpolate_at(C.x, C.y)
        self.check_interpolate_at(0.25, 0.25)
        self.check_interpolate_at(0.1, 0.2)
        self.check_interpolate_at(0.2, 0.1)
Пример #2
0
    def test_parallel(self):
        """
        4

        3  P--------Q

        1  R--------S

        0
           0  1  2  3

        """

        #CASE00
        P,Q = Point2D(0, 3), Point2D(3, 3)
        R,S = Point2D(0, 1), Point2D(3, 1)

        PQ = Segment2D(P, Q)
        RS = Segment2D(R, S)

        I0, I1, coords = PQ.intersect_segment(RS, return_coords=True)

        self.assertIsNone(I0)
        self.assertIsNone(I1)

        self.assertIsNone(coords[0])
        self.assertIsNone(coords[1])
        self.assertIsNone(coords[2])
        self.assertIsNone(coords[3])
Пример #3
0
    def test_extremity(self):
        """
        4
                    S
        3  P--------Q
                    |
        1           |
                    |
        0           R
           0  1  2  3
        """

        P,Q = Point2D(0, 3), Point2D(3, 3)
        R,S = Point2D(3, 0), Point2D(3, 3)

        PQ = Segment2D(P, Q)
        RS = Segment2D(R, S)

        I0, I1, coords = PQ.intersect_segment(RS, return_coords=True)

        self.assertAlmostEqual(I0.x, 3.)
        self.assertAlmostEqual(I0.y, 3.)
        self.assertIsNone(I1)

        self.assertAlmostEqual(coords[0], 1)
        self.assertAlmostEqual(coords[1], 1)
        self.assertIsNone(coords[2])
        self.assertIsNone(coords[3])
Пример #4
0
    def test_collinear_point_not_in_segment(self):
        """
             P Q
           1  +   R-----S
              1   2     3
        """

        # CASE03
        P,Q = Point2D(1, 1), Point2D(1, 1)
        R,S = Point2D(1, 2), Point2D(1, 3)

        PQ = Segment2D(P, Q)
        RS = Segment2D(R, S)

        I0, I1, coords = PQ.intersect_segment(RS, return_coords=True)

        self.assertIsNone(I0)
        self.assertIsNone(I1)

        self.assertIsNone(coords[0])
        self.assertIsNone(coords[1])
        self.assertIsNone(coords[2])
        self.assertIsNone(coords[3])

        # CASE05
        I0, I1, coords = RS.intersect_segment(PQ, return_coords=True)

        self.assertIsNone(I0)
        self.assertIsNone(I1)

        self.assertIsNone(coords[0])
        self.assertIsNone(coords[1])
        self.assertIsNone(coords[2])
        self.assertIsNone(coords[3])
Пример #5
0
    def test_intersection(self):
        """
        4        S
                 |
        3  P-----I--Q
                 |
        1        |
                 |
        0        R
           0  1  2  3
        """

        # CASE12
        P,Q = Point2D(0, 3), Point2D(3, 3)
        R,S = Point2D(2, 0), Point2D(2, 4)

        PQ = Segment2D(P, Q)
        RS = Segment2D(R, S)

        I0, I1, coords = PQ.intersect_segment(RS, return_coords=True)

        self.assertAlmostEqual(I0.x, 2.)
        self.assertAlmostEqual(I0.y, 3.)
        self.assertIsNone(I1)

        self.assertAlmostEqual(coords[0], 2./3.)
        self.assertAlmostEqual(coords[1], 3./4.)
        self.assertIsNone(coords[2])
        self.assertIsNone(coords[3])
Пример #6
0
    def test_same_point(self):
        """
            P Q
          1  +
            R S
             1
        """

        # CASE02
        P,Q = Point2D(1, 1), Point2D(1, 1)
        R,S = Point2D(1, 1), Point2D(1, 1)

        PQ = Segment2D(P, Q)
        RS = Segment2D(R, S)

        I0, I1, coords = PQ.intersect_segment(RS, return_coords=True)

        self.assertAlmostEqual(I0.x, 1)
        self.assertAlmostEqual(I0.y, 1)
        self.assertIsNone(I1)

        self.assertAlmostEqual(coords[0], 0)
        self.assertAlmostEqual(coords[1], 0)
        self.assertIsNone(coords[2])
        self.assertIsNone(coords[3])
Пример #7
0
    def test_colinear_intersect_one_point(self):
        """
                Q R
        3  P-----+-----S

           0     1     2
        """

        # CASE08
        P,Q = Point2D(0, 3), Point2D(1, 3)
        R,S = Point2D(1, 3), Point2D(2, 3)

        PQ = Segment2D(P, Q)
        RS = Segment2D(R, S)

        I0, I1, coords = PQ.intersect_segment(RS, return_coords=True)

        self.assertAlmostEqual(I0.x, 1)
        self.assertAlmostEqual(I0.y, 3)
        self.assertIsNone(I1)

        self.assertAlmostEqual(coords[0], 1)
        self.assertAlmostEqual(coords[1], 0)
        self.assertIsNone(coords[2])
        self.assertIsNone(coords[3])
Пример #8
0
    def test_equal(self):
        """
        4

        3  P--------Q
           R        S
        1

        0
           0  1  2  3
        """

        P,Q = Point2D(0, 3), Point2D(3, 3)
        R,S = Point2D(0, 3), Point2D(3, 3)

        PQ = Segment2D(P, Q)
        RS = Segment2D(R, S)

        I0, I1, coords = PQ.intersect_segment(RS, return_coords=True)

        self.assertAlmostEqual(I0.x, P.x)
        self.assertAlmostEqual(I0.y, P.y)

        self.assertAlmostEqual(I1.x, Q.x)
        self.assertAlmostEqual(I1.y, Q.y)

        self.assertEqual(len(coords), 4)
        self.assertAlmostEqual(coords[0], 0)
        self.assertAlmostEqual(coords[1], 0)
        self.assertAlmostEqual(coords[2], 1)
        self.assertAlmostEqual(coords[3], 1)
Пример #9
0
    def test_find_cell(self):
        """Test the cell containing a point is found"""
        #     iy
        #  30    +-------+-------+-------+-------+
        #        |     4 |     5 |     6 |     7 |
        #      1 |       |       |   P   |       |
        #        |       |       |       |       |
        #  20    +-------+-------+-------+-------+
        #        |     0 |     1 |     2 |     3 |
        #      0 |   Q   |       |       |       |
        #        |       |       |       |       |
        #  10    +-------+-------+-------+-------+
        #        -1      -0.5    0       0.5     1
        #            0       1       2       3     ix

        grid = Grid2D(xmin=-1, xmax=1.0, nx=4, ymin=10, ymax=30, ny=2)
        P = Point2D(0.25, 25)
        Q = Point2D(-0.75, 15)

        cell = grid.find_cell(P)
        self.assertEqual(cell.ix, 2)
        self.assertEqual(cell.iy, 1)
        self.assertEqual(cell.index, 6)

        cell = grid.find_cell(Q)
        self.assertEqual(cell.ix, 0)
        self.assertEqual(cell.iy, 0)
        self.assertEqual(cell.index, 0)
Пример #10
0
    def test_colinear_overlap_segment(self):
        """
        3  P-----+-----Q-----+
                 R           S
           0   1   2   3
        """

        # CASE09
        P,Q = Point2D(0,   3), Point2D(3, 3)
        R,S = Point2D(1.5, 3), Point2D(4.5, 3)

        PQ = Segment2D(P, Q)
        RS = Segment2D(R, S)

        I0, I1, coords = PQ.intersect_segment(RS, return_coords=True)

        self.assertAlmostEqual(I0.x, R.x)
        self.assertAlmostEqual(I0.y, R.y)

        self.assertAlmostEqual(I1.x, Q.x)
        self.assertAlmostEqual(I1.y, Q.y)

        self.assertAlmostEqual(coords[0], 0.5)
        self.assertAlmostEqual(coords[1], 0)
        self.assertAlmostEqual(coords[2], 1)
        self.assertAlmostEqual(coords[3], 0.5)
Пример #11
0
 def test_on_line(self):
     """
     A----B----P
     """
     A = Point2D(0, 0)
     B = Point2D(1, 0)
     P = Point2D(2, 0)
     with self.assertRaisesRegex(ValueError, "Point is on line \(AB\)"):
         P.is_left(A,B)
Пример #12
0
    def test_is_left(self):
        """
        P

        A----B
        """
        A = Point2D(0, 0)
        B = Point2D(1, 0)
        P = Point2D(0, 1)
        self.assertTrue(P.is_left(A,B))
Пример #13
0
    def test_degenerated(self):
        """
        A----B----P
        """
        A = Point2D(0,0)
        B = Point2D(1,0)
        C = Point2D(2,0)

        with self.assertRaisesRegex(ValueError, "Triangle is degenerated"):
            triangle = Triangle2D(A, B, C)
Пример #14
0
 def test_inside_on_edge(self):
     # Example taken from:
     # https://totologic.blogspot.fr/2014/01/accurate-point-in-triangle-test.html
     A = Point2D(1 / 10, 1 / 9)
     B = Point2D(100 / 8, 100 / 3)
     C = Point2D(100 / 4, 100 / 9)
     D = Point2D(-100 / 8, 100 / 6)
     AB = Segment2D(A, C)
     P = A + (B - A) * (3 / 7)
     self.assert_inside(A, B, C, P, edge_width=1.E-03)
Пример #15
0
    def test_to_polar(self):
        A = Point2D(2, 0)
        P = A.to_polar()
        self.assertAlmostEqual(P.r, 2)
        self.assertAlmostEqual(P.theta, 0)

        A = Point2D(0, 2)
        P = A.to_polar()
        self.assertAlmostEqual(P.r, 2.)
        self.assertAlmostEqual(P.theta, pi/2)
Пример #16
0
    def test_is_right(self):
        """
        A----B

        P
        """
        A = Point2D(0,  0)
        B = Point2D(1,  0)
        P = Point2D(0, -1)
        self.assertFalse(P.is_left(A,B))
Пример #17
0
    def test_normal(self):

        A = Point2D(0,0)
        B = Point2D(3,0)
        C = Point2D(0,6)

        triangle = Triangle2D(A, B, C)

        C = triangle.center

        self.assertAlmostEqual(C.x, 1.)
        self.assertAlmostEqual(C.y, 2.)
Пример #18
0
    def test_create(self):
        A = Point2D(0,0)
        B = Point2D(1,0)
        C = Point2D(0,1)

        triangle = Triangle2D(A, B, C, index=4)
        self.assertAlmostEqual(triangle.area, 0.5)

        # Change coordiante, and recompute
        triangle.A.y = -1
        triangle.recompute()
        self.assertAlmostEqual(triangle.area, 1)

        # Change point, and recompute
        triangle.A = Point2D(0,0)
        triangle.recompute()
        self.assertAlmostEqual(triangle.area, 0.5)
Пример #19
0
    def test_no_intersection(self):
        """
        4              S
                       |
        3  P--------Q  |
                       |
        1              |
                       |
        0              R
           0  1  2  3  4
        """

        # CASE10
        P,Q = Point2D(0, 3), Point2D(3, 3)
        R,S = Point2D(4, 0), Point2D(4, 4)

        PQ = Segment2D(P, Q)
        RS = Segment2D(R, S)

        I0, I1, coords = PQ.intersect_segment(RS, return_coords=True)

        self.assertIsNone(I0)
        self.assertIsNone(I1)

        self.assertIsNone(coords[0])
        self.assertIsNone(coords[1])
        self.assertIsNone(coords[2])
        self.assertIsNone(coords[3])

        # CASE11
        I0, I1, coords = RS.intersect_segment(PQ, return_coords=True)

        self.assertIsNone(I0)
        self.assertIsNone(I1)

        self.assertIsNone(coords[0])
        self.assertIsNone(coords[1])
        self.assertIsNone(coords[2])
        self.assertIsNone(coords[3])
Пример #20
0
    def test_distinct_point(self):
        """
            P Q     R S
           1 +       +
             1       2
        """

        # CASE01
        P,Q = Point2D(1, 1), Point2D(1, 1)
        R,S = Point2D(1, 2), Point2D(1, 2)

        PQ = Segment2D(P, Q)
        RS = Segment2D(R, S)

        I0, I1, coords = PQ.intersect_segment(RS, return_coords=True)

        self.assertIsNone(I0)
        self.assertIsNone(I1)

        self.assertIsNone(coords[0])
        self.assertIsNone(coords[1])
        self.assertIsNone(coords[2])
        self.assertIsNone(coords[3])
Пример #21
0
    def test_clockwise(self):
        """
        B
        +
        |  \
        |     \
        +--------+
        A        C
        """
        A = Point2D(0,0)
        B = Point2D(0,1)
        C = Point2D(1,0)

        triangle = Triangle2D(A, B, C)

        self.assertAlmostEqual(triangle.signed_area, -0.5)
        self.assertAlmostEqual(triangle.area, 0.5)
        self.assertFalse(triangle.counterclockwise)

        triangle = Triangle2D(A, B, C, force_counterclockwise=True)

        self.assertAlmostEqual(triangle.signed_area, 0.5)
        self.assertAlmostEqual(triangle.area, 0.5)
        self.assertTrue(triangle.counterclockwise)
Пример #22
0
class TestSquareWinding(unittest.TestCase):
    """
    1  D-------C
       |       |
       |       |
       |       |
    0  A-------B
       0        1
    """

    A = Point2D(0, 0)
    B = Point2D(1, 0)
    C = Point2D(1, 1)
    D = Point2D(0, 1)

    polygon2d = Polygon2D.from_points2d([A, B, C, D])

    def test_point_is_inside(self):
        P = Point2D(0.5, 0.5)
        self.assertTrue(self.polygon2d.includes_point(P))

    def test_point_is_outside(self):
        P = Point2D(1.5, 0.5)
        self.assertFalse(self.polygon2d.includes_point(P))
Пример #23
0
    def test_colinear_no_overlap(self):
        """

        3  P-----Q     R-----S

           0     1     2     3
        """

        # CASE07
        P,Q = Point2D(0, 3), Point2D(1, 3)
        R,S = Point2D(2, 3), Point2D(3, 3)

        PQ = Segment2D(P, Q)
        RS = Segment2D(R, S)

        I0, I1, coords = PQ.intersect_segment(RS, return_coords=True)

        self.assertIsNone(I0)
        self.assertIsNone(I1)

        self.assertIsNone(coords[0])
        self.assertIsNone(coords[1])
        self.assertIsNone(coords[2])
        self.assertIsNone(coords[3])
Пример #24
0
    def test_collinear_point_in_segment(self):
        """
               P Q
           1 R--+--S
             1  2  3
        """

        # CASE04
        P,Q = Point2D(2, 1), Point2D(2, 1)
        R,S = Point2D(1, 1), Point2D(3, 1)

        PQ = Segment2D(P, Q)
        RS = Segment2D(R, S)

        I0, I1, coords = PQ.intersect_segment(RS, return_coords=True)

        self.assertAlmostEqual(I0.x, 2)
        self.assertAlmostEqual(I0.y, 1)
        self.assertIsNone(I1)

        self.assertAlmostEqual(coords[0], 0)
        self.assertAlmostEqual(coords[1], 0.5)
        self.assertIsNone(coords[2])
        self.assertIsNone(coords[3])

        # CASE06
        I0, I1, coords = RS.intersect_segment(PQ, return_coords=True)

        self.assertAlmostEqual(I0.x, 2)
        self.assertAlmostEqual(I0.y, 1)
        self.assertIsNone(I1)

        self.assertAlmostEqual(coords[0], 0.5)
        self.assertAlmostEqual(coords[1], 0)
        self.assertIsNone(coords[2])
        self.assertIsNone(coords[3])
Пример #25
0
    def test_create(self):
        """
        2  F----E
           |    |
        1  |    D----C
           |         |
        0  A---------B
           0    1    2
        """
        A = Point2D(0, 0)
        B = Point2D(2, 0)
        C = Point2D(2, 1)
        D = Point2D(1, 1)
        E = Point2D(1, 2)
        F = Point2D(0, 2)

        polygon2d = Polygon2D.from_points2d([A, B, C, D, E, F, A])

        np.testing.assert_equal(polygon2d.x, [0., 2., 2., 1., 1., 0., 0.])
        np.testing.assert_equal(polygon2d.y, [0., 0., 1., 1., 2., 2., 0.])
Пример #26
0
 def test_add_vector(self):
     A = Point2D(1, 2)
     v = Vector2D(3, 1)
     B = A + v
     self.assertEqual(B.x, 4)
     self.assertEqual(B.y, 3)
Пример #27
0
 def test_vector_from_point_sub_point(self):
     A = Point2D(1,2)
     B = Point2D(4,3)
     V = B - A
     self.assertEqual(V.x, 3)
     self.assertEqual(V.y, 1)
Пример #28
0
 def test_str(self):
     A = Point2D(2,1)
     expected_string = "Point2D(2.0, 1.0)"
     string = str(A)
     self.assertEqual(string, expected_string)
Пример #29
0
 def test_property_x(self):
     A = Point2D(1,2)
     self.assertEqual(A.x, 1)
     A.x = 10
     self.assertEqual(A.x, 10)
Пример #30
0
 def test_equal(self):
     A = Point2D(2, 1)
     B = Point2D(2, 1)
     self.assertEqual(A, B)