def __init__(self, triangulation):
     self.points = set()
     self.edges = set()
     finite_faces = (f for f in triangulation.faces if self._is_finite(f))
     for face in finite_faces:
         point = circumcenter(*face.points())
         self.points.add(point)
         for half_facet in face.iter_facets():
             if half_facet.twin:
                 adj_point = circumcenter(*half_facet.twin.face.points())
                 if not self._is_finite(half_facet.twin.face):
                     tmp_vec = ((adj_point.to_vector()[:-1]) *
                                (1 / 1000000000))
                     adj_point = Point(*tmp_vec.to_array())
                     adj_point = adj_point.lift(lambda *args: 0)
                 self.edges.add(frozenset([point, adj_point]))
示例#2
0
class PointTestCase(unittest.TestCase):
    """Unit tests for the Point class."""

    def setUp(self):
        """Make some objects that are useful for most tests."""
        self.a = Point(0, 0)
        self.b = Point(1, 2)
        self.c = Point(1, 2, 3)

    def test_point_equal(self):
        """Tests if comparison of points for equality works"""
        self.assertTrue(Point(0, -9, 11.2) == Point(0, -9, 11.2))
        self.assertFalse(Point(0, -9, 1600002) == Point(0, -8, 1600002))
        # Different location
        self.assertFalse(Point(1, 4) == Point(1, 4, 5))

        # Drew's tests:
        self.assertFalse(self.a == self.b)
        self.assertTrue(self.b == self.b)
        self.assertTrue(Point(1, 2, 3) == Point(1, 2, 3))
        self.assertFalse(self.b == self.c)
        self.assertFalse(self.b == (1, 2))

    def test_misc_niceties(self):
        """I don't want  the empty point to be a thing. You may disagree."""
        with self.assertRaises(ValueError):
            Point()  # Try to make an empty point.

        # Make sure that we can evaluate a point to True
        self.assertTrue(self.a)

    def test_lift(self):
        """Make sure lifting works nicely."""

        # Define a function to use for testing
        def f(x):
            return x[0] * 2
        self.assertTrue(self.b.lift(f) == Point(1, 2, 2))
        self.assertFalse(self.c.lift(f) == Point(1, 2, 3, 4))
        self.assertFalse(self.c.lift(f) == Point(1, 2, 3))

        # Make sure that lifting a Point returns a Point
        self.assertIsInstance(self.b.lift(f), Point)

    def test_length(self):
        """Test to see if length of a point makes sense"""
        self.assertTrue(len(self.c) == 3)
        self.assertTrue(len(Point(1, 2, 3, 4)) == 4)
        self.assertFalse(len(self.b) == 3)

    def test_indexing(self):
        """Make sure that we can access elements of a point by index"""
        self.assertTrue(self.a[0] == 0)
        self.assertTrue(self.c[2] == 3)
        self.assertFalse(self.b[-1] == 10)

        with self.assertRaises(Exception):
            # I want a warning when I accidentally try to set values:
            self.b[-1] = 10

        self.assertIsInstance(self.a[:-1], Point)

    def test_subtraction(self):
        """Test point subtraction"""
        self.assertIsInstance(self.b - self.a, Vector)
        self.assertTrue(self.b - self.b == Vector(0, 0))
        self.assertTrue(self.b - self.a == Vector(1, 2))
        self.assertFalse(self.b - self.b == Point(0, 0))

    def test_to_vector(self):
        """Test turning points into vectors"""
        self.assertEqual(self.c.to_vector(), Vector(1, 2, 3))
        self.assertEqual(self.a.to_vector(), Vector(0, 0))
        self.assertEqual(self.b.to_vector(), Vector(1, 2))
        self.assertNotEqual(self.b.to_vector(), Vector(1, 2, 0))

    def test_hashing(self):
        """Make sure point hashing works and does not cause collisions"""
        # Add 400 points' hashes to a set and make sure 400 things are in the
        # set
        some_hashes = set()
        for x in range(-10, 10):
            for y in range(-10, 10):
                some_hashes.add(hash(Point(x, y, 1)))
        self.assertEqual(len(some_hashes), 400)

        self.assertEqual(hash(Point(0, 1, 2)),
                         hash(Point(0, 1, 2)))