def test_tree_hmap(self): f1 = FeatureTest(100, influence="notall") f2 = FeatureTest(200, influence="notall") f3 = FeatureTestReplace(100, influence="notall", val_influence=1) f4 = FeatureTestAddition(100, influence="notall") f1.shape = geom.box(0, 0, 50, 50) f2.shape = geom.box(30, 30, 80, 80) f3.shape = geom.box(0, 60, 50, 100) f4.shape = geom.box(60, 60, 100, 100) tree = FeatureTree([f1, f2, f3, f4]) heightmap = HeightMap(100, 100, tree.z) heightmap.export("mtree.png") compare_imgs("tests/img/mtree.png", "mtree.png", self)
class Environment: """Environment class""" def __init__(self, features, x=200, y=200): self.res_x = x self.res_y = y self.tree = FeatureTree(features) self.models = self.tree.models # compute z coordinate and gather abstract models self.abstract_models = set() for model in self.models: (x, y) = model.pos z = self.tree.z(model.pos) model.pos3D = (x, y, z) self.abstract_models.add(model.model) self.heightmap_init = False # def translate_hm(self, coords): # return self.tree.z((coords[0] + self.res_x // 2, coords[1] + self.res_y // 2)) def init_heightmap(self, res_x, res_y): self.res_x = res_x self.res_y = res_y self.heightmap = HeightMap(res_x, res_y, self.tree.z) self.heightmap_init = True def export_heightmap(self, filename, res_x=None, res_y=None): if res_x is None: res_x = self.res_x if res_y is None: res_y = self.res_y if not self.heightmap_init: self.init_heightmap(res_x, res_y) self.heightmap.export(filename)
class TestHeightMap(unittest.TestCase): def __init__(self, *args, **kwargs): super(TestHeightMap, self).__init__(*args, **kwargs) self.m1 = HeightMap(100, 100) self.m2 = HeightMap(100, 100, lambda z: z[0] + 1 + z[1]) def test_hmap(self): self.assertEqual(self.m1.hmap[0][0], 0) self.assertEqual(self.m2.hmap[0][0], 1) self.assertEqual(self.m2.hmap[1][2], 4) def test_export(self): self.m1.export("m1.png") self.m2.export("m2.png") self.m2.export("m3.png", 2) compare_imgs("tests/img/m1.png", "m1.png", self) compare_imgs("tests/img/m2.png", "m2.png", self) compare_imgs("tests/img/m3.png", "m3.png", self) def test_change_res(self): res = 2 self.m = HeightMap(10, 10, lambda z: z[0] + 10 * z[1]) self.assertEqual(self.m.hmap[9][9], 99) self.m.change_res(2) self.assertEqual(self.m.hmap[19][19], 99) self.assertEqual(self.m.hmap[19][18], 99) self.assertEqual(self.m.hmap[18][19], 99) self.assertEqual(self.m.hmap[18][18], 99)
def test_mountain(self): m2 = Mountain(10**4, 0, (50, 50)) t = FeatureTree([m2]) u = HeightMap(100, 100, t.z) u.export("mountain_as_ft.png")
def test_two_roads(self): tree = FeatureTree([self.background, self.r1, self.r2]) hm2 = HeightMap(80, 80, tree.z) hm2.export("mroad2.png") compare_imgs("tests/img/mroad2.png", "mroad2.png", self)
def test_basic_road(self): hm1 = HeightMap(100, 100, self.r2.z) hm1.export("mroad1.png") compare_imgs("tests/img/mroad1.png", "mroad1.png", self)
def test_gen_img(self): hm = HeightMap(100, 100, self.m1.z) hm.export("mountain_img.png")
def test_gen_img(self): m2 = Mountain(100, 0, (50, 50)) hm = HeightMap(100, 100, m2.z) hm.export("mountain_test.png")