This commit is contained in:
2024-06-25 03:08:11 +02:00
parent af2c5c66c8
commit 4d07754f3e
31 changed files with 87 additions and 48 deletions

View File

@@ -6,44 +6,53 @@ from buildings.Facade import Facade
from buildings.Entrance import Entrance from buildings.Entrance import Entrance
from buildings.Roof import Roof from buildings.Roof import Roof
class Building: class Building:
def __init__(self,rdata, positions : list[tuple[int,int,int]], matrice : list[list[int]], doors_direction : DIRECTION): def __init__(self, rdata, positions: list[tuple[int, int, int]], matrice: list[list[int]], doors_direction: DIRECTION):
self.position = (0,0,0) self.position = (0, 0, 0)
self.length, self.width, self.height = 0,0,0 self.length, self.width, self.height = 0, 0, 0
self.matrice = matrice self.matrice = matrice
self.get_pos_and_size(positions) self.get_pos_and_size(positions)
tile_size = self.gen_tile_size() tile_size = self.gen_tile_size()
self.foundations = Foundations(rdata["foundations"], (self.length,self.width), matrice, tile_size,) self.foundations = Foundations(
self.facade = Facade(rdata["facade"], self.foundations.vertices, self.foundations.is_inner_or_outer) rdata["foundations"], (self.length, self.width), matrice, tile_size,)
self.entrance = Entrance(rdata, self.foundations.vertices, doors_direction, self.foundations.is_inner_or_outer) self.facade = Facade(
rdata["facade"], self.foundations.vertices, self.foundations.is_inner_or_outer)
self.entrance = Entrance(rdata, self.foundations.vertices,
doors_direction, self.foundations.is_inner_or_outer)
self.roof = Roof(rdata["roof"], self.foundations.polygon) self.roof = Roof(rdata["roof"], self.foundations.polygon)
def build(self, editor : Editor, materials : list[str]): def build(self, editor: Editor, materials: list[str]):
y=0 y = self.position[1]
while y < self.height: while y < self.position[1] + self.height:
with editor.pushTransform((self.position[0], y -1, self.position[1])): with editor.pushTransform((self.position[0], y - 1, self.position[2])):
self.foundations.build(editor, materials) self.foundations.build(editor, materials)
if y == 0: self.entrance.build(editor, materials) if y == 0:
else : self.facade.build(editor, materials) self.entrance.build(editor, materials)
y+=self.foundations.floor_height+1 else:
with editor.pushTransform((self.position[0], y -1, self.position[1])): self.roof.build(editor, materials) self.facade.build(editor, materials)
y += self.foundations.floor_height+1
with editor.pushTransform((self.position[0], y - 1, self.position[2])):
self.roof.build(editor, materials)
def gen_tile_size(self) -> int: def gen_tile_size(self) -> int:
# Tiles are constant square units different for each buildings # Tiles are constant square units different for each buildings
smaller_side = min(self.length, self.width) smaller_side = min(self.length, self.width)
# area is too small, will work but not very well # area is too small, will work but not very well
if smaller_side <= 5 : return smaller_side if smaller_side <= 5:
if smaller_side <= 15 : return smaller_side // 5 return smaller_side
if smaller_side <= 15:
return smaller_side // 5
return rd.randint(3, smaller_side // len(self.matrice)) return rd.randint(3, smaller_side // len(self.matrice))
def get_pos_and_size(self, pos : list[tuple[int,int,int]]) -> tuple[tuple[int,int],int,int]: def get_pos_and_size(self, pos: list[tuple[int, int, int]]) -> tuple[tuple[int, int], int, int]:
pos1, pos2 = pos[0], pos[1] pos1, pos2 = pos[0], pos[1]
self.position = (min(pos1[0], pos2[0]), min(pos1[1], pos2[1]), min(pos1[2], pos2[2])) self.position = (min(pos1[0], pos2[0]), min(
pos1[1], pos2[1]), min(pos1[2], pos2[2]))
self.length = abs(pos1[0] - pos2[0]) self.length = abs(pos1[0] - pos2[0])
self.height = abs(pos1[1] - pos2[1]) self.height = abs(pos1[1] - pos2[1])
self.width = abs(pos1[2] - pos2[2]) self.width = abs(pos1[2] - pos2[2])

68
main.py
View File

@@ -22,11 +22,15 @@ from buildings.Building import Building
from utils.functions import * from utils.functions import *
from utils.Enums import DIRECTION from utils.Enums import DIRECTION
import time
def main(): def main():
start_time_all = time.time()
start_time = time.time()
rectangle_house_mountain, rectangle_building, skeleton_highway, skeleton_mountain, road_grid = world_maker() rectangle_house_mountain, rectangle_building, skeleton_highway, skeleton_mountain, road_grid = world_maker()
time_world_maker = time.time() - start_time
print(f"[TIME] World_maker {time_world_maker}")
editor = Editor(buffering=True) editor = Editor(buffering=True)
buildArea = editor.getBuildArea() buildArea = editor.getBuildArea()
origin = ((buildArea.begin).x, (buildArea.begin).z) origin = ((buildArea.begin).x, (buildArea.begin).z)
@@ -34,13 +38,23 @@ def main():
abs(buildArea.begin.z - buildArea.end.z) / 2) abs(buildArea.begin.z - buildArea.end.z) / 2)
length_world = sqrt((center[0]*2) ** 2 + (center[1]*2) ** 2) length_world = sqrt((center[0]*2) ** 2 + (center[1]*2) ** 2)
# remove_trees('./world_maker/data/heightmap.png', './world_maker/data/treemap.png', start_time = time.time()
# './world_maker/data/smooth_sobel_watermap.png') remove_trees('./world_maker/data/heightmap.png', './world_maker/data/treemap.png',
# smooth_terrain('./world_maker/data/heightmap.png', './world_maker/data/smooth_sobel_watermap.png')
# './world_maker/data/heightmap_smooth.png', './world_maker/data/smooth_sobel_watermap.png') time_remove_tree = time.time() - start_time
print(f"[TIME] Remove tree {time_remove_tree}")
# set_roads(skeleton_highway, origin) start_time = time.time()
# set_roads(skeleton_mountain, origin) smooth_terrain('./world_maker/data/heightmap.png',
'./world_maker/data/heightmap_smooth.png', './world_maker/data/smooth_sobel_watermap.png')
time_smooth_terrain = time.time() - start_time
print(f"[TIME] Smooth terrain {time_smooth_terrain}")
start_time = time.time()
set_roads(skeleton_highway, origin)
set_roads(skeleton_mountain, origin)
time_roads = time.time() - start_time
print(f"[TIME] Roads {time_roads}")
# set_roads_grids(road_grid, origin) # set_roads_grids(road_grid, origin)
# roads.setRoads(skeleton_mountain) # roads.setRoads(skeleton_mountain)
# roads.setRoads(skeleton_highway) # roads.setRoads(skeleton_highway)
@@ -75,29 +89,45 @@ def main():
# build it with your custom materials # build it with your custom materials
start_time = time.time()
for buildings in rectangle_building: for buildings in rectangle_building:
height = get_height_building_from_center( height = get_height_building_from_center(
center, (buildings[0][0], buildings[0][2]), length_world) center, (buildings[0][0], buildings[0][2]), length_world)
start = (min(buildings[0][0], buildings[1][0]) + origin[0], buildings[0] start = (min(buildings[0][0], buildings[1][0]) + origin[0], buildings[0]
[1]+height, min(buildings[0][2], buildings[1][2]) + origin[1]) [1], min(buildings[0][2], buildings[1][2]) + origin[1])
end = (max(buildings[0][0], buildings[1][0]) + origin[0], end = (max(buildings[0][0], buildings[1][0]) + origin[0],
buildings[1] buildings[1]
[1], max(buildings[0][2], buildings[1][2]) + origin[1]) [1]+height, max(buildings[0][2], buildings[1][2]) + origin[1])
print("---", start, end)
building = Building(random_data["buildings"], [ building = Building(random_data["buildings"], [
start, end], baseShape, DIRECTION.EAST) start, end], baseShape, DIRECTION.EAST)
building.build(editor, ["stone_bricks", "glass_pane", "glass", "cobblestone_wall", "stone_brick_stairs", building.build(editor, ["stone_bricks", "glass_pane", "glass", "cobblestone_wall", "stone_brick_stairs",
"oak_planks", "white_concrete", "cobblestone", "stone_brick_slab", "iron_bars"]) "oak_planks", "white_concrete", "cobblestone", "stone_brick_slab", "iron_bars"])
# for buildings in rectangle_house_mountain: time_buildings = time.time() - start_time
# start = (buildings[0][0] + origin[0], buildings[0] print(f"[TIME] Buildings {time_buildings}")
# [1], buildings[0][2] + origin[1])
# end = (buildings[1][0] + origin[0], buildings[1] start_time = time.time()
# [1], buildings[1][2] + origin[1]) for buildings in rectangle_house_mountain:
# house = House(editor, start, end, start = (buildings[0][0] + origin[0], buildings[0]
# entranceDirection[random.randint(0, 3)], blocks) [1], buildings[0][2] + origin[1])
# house.build() end = (buildings[1][0] + origin[0], buildings[1]
[1], buildings[1][2] + origin[1])
house = House(editor, start, end,
entranceDirection[random.randint(0, 3)], blocks)
house.build()
time_houses = time.time() - start_time
print(f"[TIME] Houses {time_houses}")
print("[GDMC] Done!\n\n")
print(f"[TIME] Total {time.time() - start_time_all}")
print(f"[TIME] World_maker {time_world_maker}")
print(f"[TIME] Remove tree {time_remove_tree}")
print(f"[TIME] Smooth terrain {time_smooth_terrain}")
print(f"[TIME] Roads {time_roads}")
print(f"[TIME] Buildings {time_buildings}")
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):
@@ -133,7 +163,7 @@ def set_roads(skeleton: Skeleton, origin):
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 = Image.open(
'./world_maker/data/full_road_heightmap_smooth.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

@@ -155,7 +155,6 @@ class Road:
for j in range(len(circle_list)): for j in range(len(circle_list)):
if j != middle_lane_index and len(circle_list[j]) > 0: if j != middle_lane_index and len(circle_list[j]) > 0:
print(len(circle_list[j]), circle_list[j])
circle_list[j] = circle_list[j][0].optimized_path( circle_list[j] = circle_list[j][0].optimized_path(
circle_list[j]) circle_list[j])
if len(circle_list[j]) != 1: if len(circle_list[j]) != 1:

Binary file not shown.

Before

Width:  |  Height:  |  Size: 345 B

After

Width:  |  Height:  |  Size: 562 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 525 B

After

Width:  |  Height:  |  Size: 1014 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 863 B

After

Width:  |  Height:  |  Size: 4.5 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.8 KiB

After

Width:  |  Height:  |  Size: 8.6 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.1 KiB

After

Width:  |  Height:  |  Size: 3.4 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.6 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 11 KiB

After

Width:  |  Height:  |  Size: 34 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.3 KiB

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 6.1 KiB

After

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.3 KiB

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.5 KiB

After

Width:  |  Height:  |  Size: 2.7 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 101 B

After

Width:  |  Height:  |  Size: 117 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.1 KiB

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 760 B

After

Width:  |  Height:  |  Size: 2.2 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 11 KiB

After

Width:  |  Height:  |  Size: 36 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 466 B

After

Width:  |  Height:  |  Size: 2.5 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 12 KiB

After

Width:  |  Height:  |  Size: 34 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 769 B

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 339 B

After

Width:  |  Height:  |  Size: 1.0 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.0 KiB

After

Width:  |  Height:  |  Size: 3.1 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.8 KiB

After

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 15 KiB

After

Width:  |  Height:  |  Size: 46 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 11 KiB

After

Width:  |  Height:  |  Size: 36 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.8 KiB

After

Width:  |  Height:  |  Size: 765 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 154 B

After

Width:  |  Height:  |  Size: 3.5 KiB

View File

@@ -67,6 +67,7 @@ def pack_rectangles(grid, min_width: int = 10, max_width: int = 25):
bin = Bin(grid) bin = Bin(grid)
while True: while True:
rectangle = generate_rectangle(min_width, max_width) rectangle = generate_rectangle(min_width, max_width)
print(f"[Pack rectangles] Number of rectangles: {len(bin.rectangles)}")
if not bin.place_rectangle(rectangle): if not bin.place_rectangle(rectangle):
break break
return bin.rectangles return bin.rectangles
@@ -97,7 +98,7 @@ def area_of_rectangles(rectangles):
def generate_building(image: str | Image.Image, heightmap: str | Image.Image, output: str = './world_maker/data/building.png', def generate_building(image: str | Image.Image, heightmap: str | Image.Image, output: str = './world_maker/data/building.png',
number_of_try: int = 3, min_width: int = 10, max_width: int = 25): number_of_try: int = 1, min_width: int = 10, max_width: int = 25):
print("[Building] Start generating building position...") print("[Building] Start generating building position...")
image = handle_import_image(image).convert('L') image = handle_import_image(image).convert('L')
rectangles_output = [] rectangles_output = []

View File

@@ -46,7 +46,7 @@ def world_maker():
'./world_maker/data/mountain_map.png').save('./world_maker/data/city_map.png') './world_maker/data/mountain_map.png').save('./world_maker/data/city_map.png')
rectangle_building = generate_building( rectangle_building = generate_building(
'./world_maker/data/city_map.png', './world_maker/data/heightmap.png', output='./world_maker/data/building.png', min_width=16, max_width=30) './world_maker/data/city_map.png', './world_maker/data/heightmap.png', output='./world_maker/data/building.png', min_width=30, max_width=40)
rectangle_building = rectangle_2D_to_3D(rectangle_building) rectangle_building = rectangle_2D_to_3D(rectangle_building)
# Houses # Houses