def test_slice2bounds(self): # test that if you ask to slice the matrix with the sheet's BoundingBox, you # get back the whole matrix # (I chose to use a 7 density, I don't know why I like 7 so much, it is kind of mystical) sheet_bb = BoundingBox(points=((-0.5, -0.5), (0.5, 0.5))) ct = SheetCoordinateSystem(sheet_bb, 7) slice_ = (0, 7, 0, 7) bounds = BoundingBox(points=Slice._slicespec2boundsspec(slice_, ct)) true_bounds_lbrt = (-0.5, -0.5, 0.5, 0.5) for a, b in zip(bounds.lbrt(), true_bounds_lbrt): self.assertAlmostEqual(a, b) # for the following tests, the values have been all computed # by hand and then tested (by JC). The boundingbox and density # tested have been chosen randomly, then drawn to get the slice # from it. # Test for 10 density ct = SheetCoordinateSystem(sheet_bb, 10) slice_ = (0, 9, 1, 5) bounds = BoundingBox(points=Slice._slicespec2boundsspec(slice_, ct)) true_bounds_lbrt = (-0.4, -0.4, 0, 0.5) for a, b in zip(bounds.lbrt(), true_bounds_lbrt): self.assertAlmostEqual(a, b) slice_ = (2, 3, 7, 10) bounds = BoundingBox(points=Slice._slicespec2boundsspec(slice_, ct)) true_bounds_lbrt = (0.2, 0.2, 0.5, 0.3) for a, b in zip(bounds.lbrt(), true_bounds_lbrt): self.assertAlmostEqual(a, b) # Test for 7 density ct = SheetCoordinateSystem(sheet_bb, 7) slice_ = (3, 7, 2, 5) bounds = BoundingBox(points=Slice._slicespec2boundsspec(slice_, ct)) true_bounds_lbrt = (-0.5 + 2.0 / 7.0, -0.5, -0.5 + 5.0 / 7.0, 0.5 - 3.0 / 7.0) for a, b in zip(bounds.lbrt(), true_bounds_lbrt): self.assertAlmostEqual(a, b) slice_ = (2, 6, 0, 1) bounds = BoundingBox(points=Slice._slicespec2boundsspec(slice_, ct)) true_bounds_lbrt = (-0.5, 0.5 - 6.0 / 7.0, -0.5 + 1.0 / 7.0, 0.5 - 2.0 / 7.0) for a, b in zip(bounds.lbrt(), true_bounds_lbrt): self.assertAlmostEqual(a, b) # Test for 25 density ct = SheetCoordinateSystem(sheet_bb, 25) slice_ = (0, 25, 4, 10) bounds = BoundingBox(points=Slice._slicespec2boundsspec(slice_, ct)) true_bounds_lbrt = (-0.5 + 4.0 / 25.0, -0.5, -0.5 + 10.0 / 25.0, 0.5) for a, b in zip(bounds.lbrt(), true_bounds_lbrt): self.assertAlmostEqual(a, b) slice_ = (7, 18, 3, 11) bounds = BoundingBox(points=Slice._slicespec2boundsspec(slice_, ct)) true_bounds_lbrt = (-0.5 + 3.0 / 25.0, 0.5 - 18.0 / 25.0, -0.5 + 11.0 / 25.0, 0.5 - 7.0 / 25.0) for a, b in zip(bounds.lbrt(), true_bounds_lbrt): self.assertAlmostEqual(a, b)
def test_connection_field_like(self): # test a ConnectionField-like example sheet = Sheet(nominal_density=10,nominal_bounds=BoundingBox(radius=0.5)) cf_bounds = BoundingBox(points=((0.3,0.3),(0.6,0.6))) slice_ = Slice(cf_bounds,sheet) slice_.crop_to_sheet(sheet) # check it's been cropped to fit onto sheet... self.assertEqual(slice_.tolist(),[0,2,8,10]) # now check that it gives the correct bounds... cropped_bounds = slice_.compute_bounds(sheet) true_cropped_bounds = BoundingBox(points=((0.3,0.3),(0.5,0.5))) for a,b in zip(cropped_bounds.lbrt(),true_cropped_bounds.lbrt()): self.assertAlmostEqual(a,b)