4 Commits

Author SHA1 Message Date
WhyteTiger
3b382f8650 refactoring : createHouseSkeleton, remove another double for loop 2024-09-28 13:24:58 +02:00
WhyteTiger
e9449901a7 refactor : createHouseSkeleton 2024-09-28 13:07:24 +02:00
WhyteTiger
86166905c8 refactoring : createHouseSkeleton, remove double for loop for floor 2024-09-28 12:08:40 +02:00
WhyteTiger
22c2c1bae9 refactoring : createHouseSkeleton, x_max z_max 2024-09-28 11:16:50 +02:00

View File

@@ -4,6 +4,8 @@ import numpy as np
import math import math
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
from pyxtension.streams import stream
class House: class House:
def __init__(self, editor, coordinates_min, coordinates_max, direction, list_block): def __init__(self, editor, coordinates_min, coordinates_max, direction, list_block):
@@ -49,37 +51,38 @@ class House:
x_min += 1 x_min += 1
z_min += 1 z_min += 1
x_max -= 1 x_max -= 2
z_max -= 1 z_max -= 2
if x_min + 1 > x_max - 1: if x_min + 1 > x_max :
x = np.random.randint(x_max - 1, x_min + 1) x = np.random.randint(x_max, x_min + 1)
else: else:
x = np.random.randint(x_min + 1, x_max - 1) x = np.random.randint(x_min + 1, x_max)
if z_min + 1 > z_max - 1: if z_min + 1 > z_max:
z = np.random.randint(z_max - 1, z_min + 1) z = np.random.randint(z_max, z_min + 1)
else: else:
z = np.random.randint(z_min + 1, z_max - 1) z = np.random.randint(z_min + 1, z_max)
width = perimeter_width // 2 width = perimeter_width // 2
depth = perimeter_depth // 2 depth = perimeter_depth // 2
height = y_max - y_min height = y_max - y_min
if x + width - 1 > x_max - 1: if x + width - 1 > x_max:
x = x_max - width - 1 x = x_max - width
if z + depth - 1 > z_max - 1: if z + depth - 1 > z_max:
z = z_max - depth - 1 z = z_max - depth
x_plan3d = x - x_min x_plan3d = x - x_min
z_plan3d = z - z_min z_plan3d = z - z_min
for i in range(0, width - 1): geometry.placeCuboid(self.editor, (x, y_min, z), (x + width - 1, y_min, z + depth - 1), self.floor)
for j in range(0, depth - 1): x_range = slice(x_plan3d, x_plan3d + width - 1)
self.editor.placeBlock((x + i, y_min, z + j), self.floor) z_range = slice(z_plan3d, z_plan3d + depth - 1)
self.grid3d[x_plan3d + i, 0, z_plan3d + j] = True, 1 self.grid3d[x_range, 0, z_range] = True, 1
self.skeleton.append((x, z, width - 1, depth - 1, height)) self.skeleton.append((x, z, width - 1, depth - 1, height))
print("Coordinates of the corners: ", (x, z), (x, z + depth - 1), (x + width - 1, z), print("Coordinates of the corners: ", (x, z), (x, z + depth - 1),
(x + width - 1, z + depth - 1)) (x + width - 1, z), (x + width - 1, z + depth - 1))
x_min -= 1 x_min -= 1
x_max -= 1 x_max -= 1
@@ -104,19 +107,19 @@ class House:
else: else:
new_width = np.random.randint(width - 2, 5) new_width = np.random.randint(width - 2, 5)
if max(x_min+1, x-new_width) > min(x_max-new_width-1, x+width): if max(x_min+1, x-new_width) > min(x_max-new_width, x+width):
new_x = np.random.randint( new_x = np.random.randint(min(x_max - new_width, x + width),
min(x_max - new_width - 1, x + width), max(x_min + 1, x - new_width)) max(x_min + 1, x - new_width))
else: else:
new_x = np.random.randint( new_x = np.random.randint(max(x_min + 1, x - new_width),
max(x_min + 1, x - new_width), min(x_max - new_width - 1, x + width)) min(x_max - new_width, x + width))
if max(z_min+1, z-new_depth) > min(z_max-new_depth-1, z+depth): if max(z_min+1, z-new_depth) > min(z_max-new_depth, z+depth):
new_z = np.random.randint( new_z = np.random.randint(min(z_max - new_depth, z + depth),
min(z_max - new_depth - 1, z + depth), max(z_min + 1, z - new_depth)) max(z_min + 1, z - new_depth))
else: else:
new_z = np.random.randint( new_z = np.random.randint(max(z_min + 1, z - new_depth),
max(z_min + 1, z - new_depth), min(z_max - new_depth - 1, z + depth)) min(z_max - new_depth, z + depth))
new_x_plan3d = new_x - x_min - 1 new_x_plan3d = new_x - x_min - 1
new_z_plan3d = new_z - z_min + 1 new_z_plan3d = new_z - z_min + 1
@@ -130,28 +133,21 @@ class House:
self.grid3d[i, 0, j + 1]['bool'] and self.grid3d[i, 0, j + 1]['int'] == 1: self.grid3d[i, 0, j + 1]['bool'] and self.grid3d[i, 0, j + 1]['int'] == 1:
adjacent_blocks += 1 adjacent_blocks += 1
if adjacent_blocks < 3: new_x_range = slice(new_x_plan3d, new_x_plan3d + new_width)
new_z_range = slice(new_z_plan3d, new_z_plan3d + new_depth)
if adjacent_blocks < 3 or np.any(self.grid3d[new_x_range, 0, new_z_range]['bool']) :
continue continue
if not np.any( geometry.placeCuboid(self.editor, (new_x, y_min, new_z), (new_x+new_width-1, y_min, new_z+new_depth-1), self.floor)
self.grid3d[new_x_plan3d:new_x_plan3d + new_width, 0, new_z_plan3d:new_z_plan3d + new_depth][ new_x_plan3d += 1
'bool']): new_z_plan3d -= 1
new_x_plan3d = new_x - x_min new_x_range = slice(new_x_plan3d, new_x_plan3d + new_width)
new_z_plan3d = new_z - z_min new_z_range = slice(new_z_plan3d, new_z_plan3d + new_depth)
for i in range(0, new_width): self.grid3d[new_x_range, 0, new_z_range] = True, 2
for j in range(0, new_depth):
self.grid3d[new_x_plan3d + i, 0,
new_z_plan3d + j] = True, 2
if i == 0 or i == new_width - 1 or j == 0 or j == new_depth - 1: self.skeleton.append((new_x, new_z, new_width, new_depth, height))
continue
else:
self.editor.placeBlock(
(new_x + i, y_min, new_z + j), self.floor)
self.skeleton.append(
(new_x, new_z, new_width, new_depth, height))
break break
else: else:
print("Failed to place rectangle after 100000 attempts.") print("Failed to place rectangle after 100000 attempts.")
@@ -1249,6 +1245,7 @@ class House:
self.placeStairs() self.placeStairs()
if __name__ == "__main__": if __name__ == "__main__":
editor = Editor(buffering=True) editor = Editor(buffering=True)
buildArea = editor.getBuildArea() buildArea = editor.getBuildArea()