def test_diag(self): out = np.zeros((10, 11), dtype=np.int) geometry.bressenham(out, [2, 2, 6, 5], 1) expected = np.array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]) self.assertTrue(np.array_equal(out, expected)) out = np.zeros((10, 11), dtype=np.int) geometry.bressenham(out, [7, 3, 1, 8], 1) expected = np.array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]) self.assertTrue(np.array_equal(out, expected))
def test_compound(self): cats = [1, 2] out = np.zeros((5, 1, len(cats))) geometry.bressenham(out, [0, 0, 0, 4], cats) expected = np.empty(out.shape) expected[:, :, 0] = 1 expected[:, :, 1] = 2 self.assertTrue(np.array_equal(out, expected), "Complex value")
def glyphAggregates(self, glyph, shapeCode, val, default): """Create a set of aggregates for a single glyph. The set of aggregates will be tight to the bound box of the shape but may not be completely filled (thus the need for both 'val' and 'default'). * glyph -- Points that define the glyph * shapeCode -- Code that indicates how to interpret the glyph * val -- Value to place in bins that are hit by the shape * default -- Value to place in bins not hit by the shape """ def scalar(array, val): array.fill(val) def nparray(array, val): array[:] = val if type(val) == np.ndarray: fill = nparray extShape = val.shape else: fill = scalar extShape = () # TODO: These are selectors...rename and move this somewhere else if shapeCode == glyphset.ShapeCodes.POINT: array = np.copy( val) # TODO: Not sure this is always an array...verify elif shapeCode == glyphset.ShapeCodes.RECT: array = np.empty( (glyph[3] - glyph[1], glyph[2] - glyph[0]) + extShape, dtype=np.int32) fill(array, val) elif shapeCode == glyphset.ShapeCodes.LINE: array = np.empty( (glyph[3] - glyph[1], glyph[2] - glyph[0]) + extShape, dtype=np.int32) fill(array, default) glyph = [ 0, 0, array.shape[1] - 1, array.shape[0] - 1 ] # Translate shape to be in the corner of the update canvas geometry.bressenham(array, glyph, val) return array
def glyphAggregates(self, glyph, shapeCode, val, default): """Create a set of aggregates for a single glyph. The set of aggregates will be tight to the bound box of the shape but may not be completely filled (thus the need for both 'val' and 'default'). * glyph -- Points that define the glyph * shapeCode -- Code that indicates how to interpret the glyph * val -- Value to place in bins that are hit by the shape * default -- Value to place in bins not hit by the shape """ def scalar(array, val): array.fill(val) def nparray(array, val): array[:] = val if type(val) == np.ndarray: fill = nparray extShape = val.shape else: fill = scalar extShape = () # TODO: These are selectors...rename and move this somewhere else if shapeCode == glyphset.ShapeCodes.POINT: array = np.copy(val) # TODO: Not sure this is always an array...verify elif shapeCode == glyphset.ShapeCodes.RECT: array = np.empty((glyph[3]-glyph[1], glyph[2]-glyph[0])+extShape, dtype=np.int32) fill(array, val) elif shapeCode == glyphset.ShapeCodes.LINE: array = np.empty((glyph[3]-glyph[1], glyph[2]-glyph[0])+extShape, dtype=np.int32) fill(array, default) glyph = [0, 0, array.shape[1]-1, array.shape[0]-1] # Translate shape to be in the corner of the update canvas geometry.bressenham(array, glyph, val) return array
def test_vertical(self): out = np.zeros((10, 1)) geometry.bressenham(out, [0, 0, 0, 9], 1) expected = np.ones((10, 1)) self.assertTrue(np.array_equal(out, expected), "Simple vertical") geometry.bressenham(out, [0, 9, 0, 0], 1) expected = np.ones((10, 1)) self.assertTrue(np.array_equal(out, expected), "Reverse vertical") expected.fill(7) geometry.bressenham(out, [0, 0, 0, 9], 7) self.assertTrue(np.array_equal(out, expected), "Vertical: Another value") out.fill(0) expected.fill(7) expected[0, 0] = 0 expected[9, 0] = 0 geometry.bressenham(out, [0, 1, 0, 8], 7) self.assertTrue(np.array_equal(out, expected), "Vertical: Not space filling")