refactor : suppression d'une double boucle for dans l'algo de remove_trees

This commit is contained in:
Melvyn
2024-09-27 19:47:01 +02:00
parent aa5c70b590
commit f747dd5b46
3 changed files with 54 additions and 59 deletions

16
main.py
View File

@@ -15,7 +15,8 @@ from networks.geometry.Point3D import Point3D
from networks.geometry.Point2D import Point2D from networks.geometry.Point2D import Point2D
from networks.geometry.Circle import Circle from networks.geometry.Circle import Circle
from PIL import Image from PIL import Image as img
from PIL.Image import Image
from utils.JsonReader import JsonReader from utils.JsonReader import JsonReader
from utils.YamlReader import YamlReader from utils.YamlReader import YamlReader
from buildings.Building import Building from buildings.Building import Building
@@ -44,6 +45,7 @@ def main():
time_remove_tree = time.time() - start_time time_remove_tree = time.time() - start_time
print(f"[TIME] Remove tree {time_remove_tree}") print(f"[TIME] Remove tree {time_remove_tree}")
"""
start_time = time.time() start_time = time.time()
smooth_terrain('./world_maker/data/heightmap.png', smooth_terrain('./world_maker/data/heightmap.png',
'./world_maker/data/heightmap_smooth.png', './world_maker/data/smooth_sobel_watermap.png') './world_maker/data/heightmap_smooth.png', './world_maker/data/smooth_sobel_watermap.png')
@@ -117,17 +119,17 @@ def main():
entranceDirection[random.randint(0, 3)], blocks) entranceDirection[random.randint(0, 3)], blocks)
house.build() house.build()
time_houses = time.time() - start_time time_houses = time.time() - start_time
print(f"[TIME] Houses {time_houses}") print(f"[TIME] Houses {time_houses}") """
print("[GDMC] Done!\n\n") print("[GDMC] Done!\n\n")
print(f"[TIME] Total {time.time() - start_time_all}") print(f"[TIME] Total {time.time() - start_time_all}")
print(f"[TIME] World_maker {time_world_maker}") print(f"[TIME] World_maker {time_world_maker}")
print(f"[TIME] Remove tree {time_remove_tree}") print(f"[TIME] Remove tree {time_remove_tree}")
print(f"[TIME] Smooth terrain {time_smooth_terrain}") #print(f"[TIME] Smooth terrain {time_smooth_terrain}")
print(f"[TIME] Roads {time_roads}") #print(f"[TIME] Roads {time_roads}")
print(f"[TIME] Buildings {time_buildings}") #print(f"[TIME] Buildings {time_buildings}")
print(f"[TIME] Houses {time_houses}") #print(f"[TIME] Houses {time_houses}")
def get_height_building_from_center(center, position, length_world): def get_height_building_from_center(center, position, length_world):
@@ -162,7 +164,7 @@ def set_roads(skeleton: Skeleton, origin):
for j in range(len(skeleton.lines[i])): for j in range(len(skeleton.lines[i])):
xyz = transpose_form_heightmap('./world_maker/data/heightmap.png', xyz = transpose_form_heightmap('./world_maker/data/heightmap.png',
skeleton.coordinates[skeleton.lines[i][j]], origin) skeleton.coordinates[skeleton.lines[i][j]], origin)
heightmap_smooth = Image.open( heightmap_smooth = img.open(
'./world_maker/data/road_heightmap.png') './world_maker/data/road_heightmap.png')
skeleton.lines[i][j] = [xyz[0], heightmap_smooth.getpixel( skeleton.lines[i][j] = [xyz[0], heightmap_smooth.getpixel(
(skeleton.coordinates[skeleton.lines[i][j]][0], skeleton.coordinates[skeleton.lines[i][j]][-1])), xyz[2]] (skeleton.coordinates[skeleton.lines[i][j]][0], skeleton.coordinates[skeleton.lines[i][j]][-1])), xyz[2]]

View File

@@ -4,7 +4,8 @@ from typing import List, Union
import numpy as np import numpy as np
from gdpc import Editor from gdpc import Editor
from PIL import Image, ImageDraw from PIL.Image import Image
from PIL import Image as img, ImageDraw
from skan.csr import skeleton_to_csgraph from skan.csr import skeleton_to_csgraph
from skimage.morphology import skeletonize from skimage.morphology import skeletonize
from networks.geometry.Point3D import Point3D from networks.geometry.Point3D import Point3D
@@ -12,7 +13,7 @@ from networks.geometry.Point3D import Point3D
def handle_import_image(image: Union[str, Image]) -> Image: def handle_import_image(image: Union[str, Image]) -> Image:
if isinstance(image, str): if isinstance(image, str):
return Image.open(image) return img.open(image)
return image return image
@@ -203,7 +204,7 @@ class Skeleton:
# xzDistance = (max(buildRect.end[0], buildRect.begin[0]) - min(buildRect.end[0], buildRect.begin[0]), # xzDistance = (max(buildRect.end[0], buildRect.begin[0]) - min(buildRect.end[0], buildRect.begin[0]),
# max(buildRect.end[1], buildRect.begin[1]) - min(buildRect.end[1], buildRect.begin[1])) # max(buildRect.end[1], buildRect.begin[1]) - min(buildRect.end[1], buildRect.begin[1]))
heightmap = Image.open( heightmap = img.open(
"./world_maker/data/heightmap.png").convert('RGB') "./world_maker/data/heightmap.png").convert('RGB')
# roadsArea = Image.new("L", xzDistance, 0) # roadsArea = Image.new("L", xzDistance, 0)
# width, height = heightmap.size # width, height = heightmap.size
@@ -264,9 +265,9 @@ class Skeleton:
def road_area(self, name: str, radius: int = 10) -> Image: def road_area(self, name: str, radius: int = 10) -> Image:
print("[Skeleton] Start mapping the road area...") print("[Skeleton] Start mapping the road area...")
heightmap = Image.open("./world_maker/data/heightmap.png") heightmap = img.open("./world_maker/data/heightmap.png")
width, height = heightmap.size width, height = heightmap.size
road_area_map = Image.new("L", (width, height), 0) road_area_map = img.new("L", (width, height), 0)
road_area_map_draw = ImageDraw.Draw(road_area_map) road_area_map_draw = ImageDraw.Draw(road_area_map)
# Lines # Lines

View File

@@ -2,7 +2,8 @@ from typing import Union
import numpy as np import numpy as np
from gdpc import Editor, Block, geometry, lookup from gdpc import Editor, Block, geometry, lookup
from PIL import Image from PIL import Image as img
from PIL.Image import Image
from skimage import morphology from skimage import morphology
from world_maker.data_analysis import handle_import_image from world_maker.data_analysis import handle_import_image
@@ -16,38 +17,28 @@ def remove_trees(heightmap: Union[str, Image], treesmap: Union[str, Image], mask
start = build_rectangle.begin start = build_rectangle.begin
distance = (max(build_rectangle.end[0], build_rectangle.begin[0]) - min(build_rectangle.end[0], build_rectangle.begin[0]), max( distance = (max(build_rectangle.end[0], build_rectangle.begin[0]) - min(build_rectangle.end[0], build_rectangle.begin[0]),
build_rectangle.end[1], build_rectangle.begin[1]) - min(build_rectangle.end[1], build_rectangle.begin[1])) max(build_rectangle.end[1], build_rectangle.begin[1]) - min(build_rectangle.end[1], build_rectangle.begin[1]))
heightmap = handle_import_image(heightmap).convert('L') heightmap = handle_import_image(heightmap).convert('L')
treesmap = handle_import_image(treesmap).convert('L') treesmap = handle_import_image(treesmap ).convert('L')
mask = handle_import_image(mask) mask = handle_import_image(mask)
removed_treesmap = Image.new("L", distance, 0) removed_treesmap = img.new("L", distance, 0)
removed = [] for (x,z) in [(x,z) for x in range(distance[0]) for z in range(distance[1])] :
for x in range(0, distance[0]):
for z in range(0, distance[1]):
if mask.getpixel((x, z)) != 0 and treesmap.getpixel((x, z)) > 0 and (x, z) not in removed: if mask.getpixel((x, z)) != 0 and treesmap.getpixel((x, z)) > 0 :
tree_area = morphology.flood(treesmap, (z, x), tolerance=1) tree_area = morphology.flood(treesmap, (z, x), tolerance=1)
blend = Image.blend(Image.fromarray(tree_area).convert( blend = img.blend(img.fromarray(tree_area).convert('L'), removed_treesmap.convert('L'), 0.5)
'L'), removed_treesmap.convert('L'), 0.5) array = np.array(blend.convert('L'))
bool_array = array > 1
removed_treesmap = img.fromarray(bool_array)
array = np.array(blend.convert('L')) y = heightmap.getpixel((x, z))
bool_array = array > 1 y_top = removed_treesmap.getpixel((x, z))
removed_treesmap = Image.fromarray(bool_array) geometry.placeLine(editor, (start[0] + x, y+1, start[1] + z), (start[0] + x, y_top, start[1] + z), Block('air'))
removed.append((x, z))
for x in range(0, distance[0]):
for z in range(0, distance[1]):
if removed_treesmap.getpixel((x, z)) != 0:
y = heightmap.getpixel((x, z))
y_top = removed_treesmap.getpixel((x, z))
geometry.placeLine(
editor, (start[0] + x, y+1, start[1] + z), (start[0] + x, y_top, start[1] + z), Block('air'))
removed_treesmap.save('./world_maker/data/removed_treesmap.png') removed_treesmap.save('./world_maker/data/removed_treesmap.png')
print("[Remove tree] Done.") print("[Remove tree] Done.")
@@ -62,14 +53,14 @@ def smooth_terrain(heightmap: Union[str, Image], heightmap_smooth: Union[str, Im
start = build_rectangle.begin start = build_rectangle.begin
distance = (max(build_rectangle.end[0], build_rectangle.begin[0]) - min(build_rectangle.end[0], build_rectangle.begin[0]), max( distance = (max(build_rectangle.end[0], build_rectangle.begin[0]) - min(build_rectangle.end[0], build_rectangle.begin[0]),
build_rectangle.end[1], build_rectangle.begin[1]) - min(build_rectangle.end[1], build_rectangle.begin[1])) max(build_rectangle.end[1], build_rectangle.begin[1]) - min(build_rectangle.end[1], build_rectangle.begin[1]))
heightmap = handle_import_image(heightmap).convert('L') heightmap = handle_import_image(heightmap).convert('L')
heightmap_smooth = handle_import_image(heightmap_smooth).convert('L') heightmap_smooth = handle_import_image(heightmap_smooth).convert('L')
mask = handle_import_image(mask).convert('L') mask = handle_import_image(mask).convert('L')
smooth_terrain_delta = Image.new("RGB", distance, 0) smooth_terrain_delta = img.new("RGB", distance, 0)
slice = editor.loadWorldSlice(build_rectangle) slice = editor.loadWorldSlice(build_rectangle)
smoothable_blocks = lookup.OVERWORLD_SOILS | lookup.OVERWORLD_STONES | lookup.SNOWS smoothable_blocks = lookup.OVERWORLD_SOILS | lookup.OVERWORLD_STONES | lookup.SNOWS
@@ -77,24 +68,25 @@ def smooth_terrain(heightmap: Union[str, Image], heightmap_smooth: Union[str, Im
for x in range(0, distance[0]): for x in range(0, distance[0]):
for z in range(0, distance[1]): for z in range(0, distance[1]):
if mask.getpixel((x, z)) != 0: if mask.getpixel((x, z)) == 0:
y = heightmap.getpixel((x, z)) continue
y_smooth = heightmap_smooth.getpixel((x, z)) y = heightmap.getpixel((x, z))
delta = y - y_smooth y_smooth = heightmap_smooth.getpixel((x, z))
smooth_terrain_delta.putpixel((x, z), delta) delta = y - y_smooth
smooth_terrain_delta.putpixel((x, z), delta)
if delta != 0: if delta == 0:
block = slice.getBlock((x, y, z)) continue
if block.id in smoothable_blocks: block = slice.getBlock((x, y, z))
if delta > 0:
geometry.placeLine(
editor, (start[0] + x, y, start[1] + z), (start[0] + x, y_smooth, start[1] + z), Block('air'))
editor.placeBlock(
(start[0] + x, y_smooth, start[1] + z), block)
else: if block.id not in smoothable_blocks:
geometry.placeLine( continue
editor, (start[0] + x, y, start[1] + z), (start[0] + x, y_smooth, start[1] + z), block) if delta > 0:
geometry.placeLine(editor, (start[0] + x, y, start[1] + z), (start[0] + x, y_smooth, start[1] + z), Block('air'))
editor.placeBlock((start[0] + x, y_smooth, start[1] + z), block)
else:
geometry.placeLine(editor, (start[0] + x, y, start[1] + z), (start[0] + x, y_smooth, start[1] + z), block)
smooth_terrain_delta.save('./world_maker/data/smooth_terrain_delta.png') smooth_terrain_delta.save('./world_maker/data/smooth_terrain_delta.png')
print("[Smooth terrain] Done.") print("[Smooth terrain] Done.")