def main(): colors = { 'red' : '#f00', 'white' : '#fff', } float_colors = { } for key, value in colors.items(): color = parse_color(value) colors[key] = np.array([ color ], dtype=Pixel) float_colors[key] = np.array(tuple(c / 255. for c in color), dtype=FloatPixel) image = Tile((0, 0), (512, 512), sample_rate=4, dtype=FloatPixel) image.buffer[:,:] = float_colors['white'] tile_width, tile_height = image.buffer.shape mesh = MicropolygonMesh((1,1)) mesh_width, mesh_height = mesh.buffer.shape margin = 16 width = 128 right = image.shape[0] top = image.shape[1] buffer = np.array([ [ (right - margin - width, top - margin, 1, 1, 1.5, 0, 0, 1), (right - margin , top - margin, 1, 1, 0 , 0, 0, 1) ], [ (margin , margin, 1, 1, .5, 0, 0, 1), (margin + width, margin, 1, 1, 0 , 0, 0, 1) ], ], dtype=np.float32 ) mesh.buffer.view(dtype=(np.float32, 8))[:] = buffer fill_micropolygon_mesh( mesh_width, mesh_height, generate_numpy_begin(mesh.buffer), tile_width, tile_height, generate_numpy_begin(image.coordinate_image), generate_numpy_begin(image.buffer) ) buffer = array_view(image.downsample(1)) buffer = np.clip(buffer, 0., 1.) buffer = (255. * buffer).astype(dtype=np.uint8) save_array_as_image(pixel_view(buffer), 'render/002_micropolygon.tiff', 'RGBA')
def render_mesh(mesh): cache = TileCache((16, 16), 4, FloatPixel) mesh_bounds = mesh.outer_bounds mesh_rows, mesh_columns = mesh.buffer.shape for tile in cache.get_tiles_for_bounds(mesh_bounds): tile_rows, tile_columns = tile.buffer.shape fill_micropolygon_mesh( mesh_rows, mesh_columns, generate_numpy_begin(mesh.buffer), generate_numpy_begin(mesh.bounds), tile_rows, tile_columns, tile.bounds, generate_numpy_begin(tile.coordinate_image), generate_numpy_begin(tile.buffer) ) return cache
def test_tile_render(): from handsome.MicropolygonMesh import MicropolygonMesh from handsome.Tile import Tile mesh = MicropolygonMesh((1, 1)) mesh.buffer[:,:]['position'] = [ [ (16, 16, 1, 1), (32, 16, 1, 1) ], [ (0, 0, 1, 1), (16, 0, 1, 1) ], ] red = color(1, 0, 0) green = color(0, 1, 0) orange = color(1, 1, 0) black = color(0, 0, 0, 1) zero = color(0, 0, 0, 0) mesh.buffer[:,:]['color'] = [ [ green, orange, ], [ black, red, ], ] mesh_rows, mesh_columns = mesh.buffer.shape tile = Tile((0,0), (32, 16), 1, FloatPixel) tile_rows, tile_columns = tile.buffer.shape fill_micropolygon_mesh( mesh_rows, mesh_columns, generate_numpy_begin(mesh.buffer), generate_numpy_begin(mesh.bounds), tile_rows, tile_columns, tile.bounds, generate_numpy_begin(tile.coordinate_image), generate_numpy_begin(tile.buffer), ) buffer = array_view(tile.buffer) rows, columns, channels = buffer.shape def image(x, y): return buffer[rows - 1 - y, x] low, high = 1/16, 15/16 expected = [ ((0, 0), color(0, 0, 0, 1)), ((15, 0), color(15/16, 0, 0, 1)), ((16, 0), color(0, 0, 0, 0)), ((31, 0), color(0, 0, 0, 0)), ((0, 8), color(0, 0, 0, 0)), ((8,8), color(0, 1/2, 0, 1)), ((16,8), color(1/2, 1/2, 0, 1)), ((31, 8), color(0, 0, 0, 0)), ((0, 15), color(0, 0, 0, 0)), ((15, 15), low * black + high * green), ((30, 15), low * (low * black + high * green) + high * (low * red + high * orange)), ] for index, c in expected: assert points_are_close(image(*index), c)