From 2e66a539cfd1a4963396b0abba94f81f86d014fa Mon Sep 17 00:00:00 2001 From: Xeon0X Date: Sun, 26 May 2024 18:37:33 +0200 Subject: [PATCH] Add line object --- main.py | 131 ++++++++++++++++++---------------- networks/lanes/Lane.py | 36 ++++++++++ networks/lanes/materials.json | 7 ++ networks/lines/Line.py | 41 +++++++++++ networks/lines/line.json | 9 +++ networks/roads/Lane.py | 3 - networks/roads/Road.py | 6 +- 7 files changed, 166 insertions(+), 67 deletions(-) create mode 100644 networks/lanes/Lane.py create mode 100644 networks/lanes/materials.json create mode 100644 networks/lines/Line.py create mode 100644 networks/lines/line.json delete mode 100644 networks/roads/Lane.py diff --git a/main.py b/main.py index c4d24f8..2fb0396 100644 --- a/main.py +++ b/main.py @@ -1,25 +1,26 @@ +import networks.lines.Line as Line from gdpc import Editor, Block, geometry -import networks.Curve as curve -import networks.CurveSurface as CurveSurface -import networks.Segment as segment +import networks.geometry.curve as curve +import networks.geometry.CurveSurface as CurveSurface +import networks.geometry.segment as segment import numpy as np import json from buildings.Building import Building import random -editor = Editor(buffering=True) +# editor = Editor(buffering=True) -f = open('buildings\shapes.json') -shapes = json.load(f) +# f = open('buildings\shapes.json') +# shapes = json.load(f) -# F = Foundations((0,0), (20,20), shapes[0]['matrice']) -# F.polygon.fill_polygon(editor, "stone", -60) -geometry.placeCuboid(editor, (-10, -60, -10), (85, -55, 85), Block("air")) -B = Building((0, 0), (75, 75), shapes[7]['matrice']) -B.foundations.polygon.fill_vertice(editor, "pink_wool", -60) -for collumn in B.foundations.collumns: - collumn.fill(editor, "white_concrete", -60, -55) -B.foundations.polygon.fill_polygon(editor, "white_concrete", -60) +# # F = Foundations((0,0), (20,20), shapes[0]['matrice']) +# # F.polygon.fill_polygon(editor, "stone", -60) +# geometry.placeCuboid(editor, (-10, -60, -10), (85, -55, 85), Block("air")) +# B = Building((0, 0), (75, 75), shapes[7]['matrice']) +# B.foundations.polygon.fill_vertice(editor, "pink_wool", -60) +# for collumn in B.foundations.collumns: +# collumn.fill(editor, "white_concrete", -60, -55) +# B.foundations.polygon.fill_polygon(editor, "white_concrete", -60) y = 25 block_list = ["blue_concrete", "red_concrete", "green_concrete", @@ -28,63 +29,71 @@ block_list = ["blue_concrete", "red_concrete", "green_concrete", # Over the hill # coordinates = [(-854, 87+y, -210), (-770, 99+y, -207), (-736, 85+y, -184)] -# Along the river -# coordinates = [(-456, 69, -283), (-588, 106, -374), (-720, 71, -384), (-775, 67, -289), (-822, 84, -265), (-868, 77, -188), (-927, 96, -127), -# (-926, 65, -29), (-906, 98, 42), (-902, 137, 2), (-909, 115, -62), (-924, 76, -6), (-985, 76, 37), (-1043, 76, 28), (-1102, 66, 63)] +# # Along the river +# # coordinates = [(-456, 69, -283), (-588, 106, -374), (-720, 71, -384), (-775, 67, -289), (-822, 84, -265), (-868, 77, -188), (-927, 96, -127), +# # (-926, 65, -29), (-906, 98, 42), (-902, 137, 2), (-909, 115, -62), (-924, 76, -6), (-985, 76, 37), (-1043, 76, 28), (-1102, 66, 63)] -# Though the loop -# coordinates = [(-1005, 113+y, -19), (-896, 113+y, 7), -# (-807, 76+y, 54), (-738, 76+y, -10), (-678, 76+y, -86)] +# # Though the loop +# # coordinates = [(-1005, 113+y, -19), (-896, 113+y, 7), +# # (-807, 76+y, 54), (-738, 76+y, -10), (-678, 76+y, -86)] -# Second zone -coordinates = [(-805, 78, 128), (-881, 91, 104), (-950, 119, 69), (-1005, 114, 58), (-1052, 86, 30), - (-1075, 83, 40), (-1104, 77, 63), (-1161, 69, 157), (-1144, 62, 226), (-1189, 76, 265), (-1210, 79, 329)] +# # Second zone +# coordinates = [(-805, 78, 128), (-881, 91, 104), (-950, 119, 69), (-1005, 114, 58), (-1052, 86, 30), +# (-1075, 83, 40), (-1104, 77, 63), (-1161, 69, 157), (-1144, 62, 226), (-1189, 76, 265), (-1210, 79, 329)] -resolution, distance = curve.resolution_distance(coordinates, 6) +# resolution, distance = curve.resolution_distance(coordinates, 6) -curve_points = curve.curve(coordinates, resolution) -curve_surface = CurveSurface.CurveSurface(coordinates) -curve_surface.compute_curvature() +# curve_points = curve.curve(coordinates, resolution) +# curve_surface = CurveSurface.CurveSurface(coordinates) +# curve_surface.compute_curvature() -curvature = [] -for i in range(len(curve_surface.curvature)): - curvature.append((0, 1, 0)) +# curvature = [] +# for i in range(len(curve_surface.curvature)): +# curvature.append((0, 1, 0)) -# Perpendicular -curve_surface.compute_surface_perpendicular(10, curvature) -for i in range(len(curve_surface.surface)): - for j in range(len(curve_surface.surface[i])): - # block = random.choice(block_list) - for k in range(len(curve_surface.surface[i][j])): - if k-16 < len(block_list) and k-16 >= 0: - editor.placeBlock( - curve_surface.surface[i][j][k], Block(block_list[k-16])) - else: - editor.placeBlock( - curve_surface.surface[i][j][k], Block("stone")) +# # Perpendicular +# curve_surface.compute_surface_perpendicular(10, curvature) +# for i in range(len(curve_surface.surface)): +# for j in range(len(curve_surface.surface[i])): +# # block = random.choice(block_list) +# for k in range(len(curve_surface.surface[i][j])): +# if k-16 < len(block_list) and k-16 >= 0: +# editor.placeBlock( +# curve_surface.surface[i][j][k], Block(block_list[k-16])) +# else: +# editor.placeBlock( +# curve_surface.surface[i][j][k], Block("stone")) -offset = curve.offset(curve_surface.curve, -9, curvature) -for i in range(len(offset)-1): - line = segment.discrete_segment(offset[i], offset[i+1]) - for coordinate in line: - editor.placeBlock(coordinate, Block("white_concrete")) +# offset = curve.offset(curve_surface.curve, -9, curvature) +# for i in range(len(offset)-1): +# line = segment.discrete_segment(offset[i], offset[i+1]) +# for coordinate in line: +# editor.placeBlock(coordinate, Block("white_concrete")) -offset = curve.offset(curve_surface.curve, 9, curvature) -for i in range(len(offset)-1): - line = segment.discrete_segment(offset[i], offset[i+1]) - for coordinate in line: - editor.placeBlock(coordinate, Block("white_concrete")) +# offset = curve.offset(curve_surface.curve, 9, curvature) +# for i in range(len(offset)-1): +# line = segment.discrete_segment(offset[i], offset[i+1]) +# for coordinate in line: +# editor.placeBlock(coordinate, Block("white_concrete")) -# for coordinate in curve_surface.surface: -# editor.placeBlock(coordinate, Block("black_concrete")) +# # for coordinate in curve_surface.surface: +# # editor.placeBlock(coordinate, Block("black_concrete")) -# for coordinate in curve_surface.curve: -# editor.placeBlock(coordinate, Block("red_concrete")) +# # for coordinate in curve_surface.curve: +# # editor.placeBlock(coordinate, Block("red_concrete")) -# # Parallel -# curve_surface.compute_surface_parallel(0, 10, 8, curvature) +# # # Parallel +# # curve_surface.compute_surface_parallel(0, 10, 8, curvature) -# for current_range in range(len(curve_surface.left_side)): -# for coordinate in curve_surface.left_side[current_range]: -# editor.placeBlock(coordinate, Block("yellow_concrete")) +# # for current_range in range(len(curve_surface.left_side)): +# # for coordinate in curve_surface.left_side[current_range]: +# # editor.placeBlock(coordinate, Block("yellow_concrete")) + + +coordinates = [(0, 0, 0), (0, 10, 0), (0, 20, 0)] + +with open('networks/lines/line.json') as f: + lines_type = json.load(f) + l = Line.Line(coordinates, lines_type.get('solid_white')) + print(l.get_surface()) diff --git a/networks/lanes/Lane.py b/networks/lanes/Lane.py new file mode 100644 index 0000000..dd1e5eb --- /dev/null +++ b/networks/lanes/Lane.py @@ -0,0 +1,36 @@ +import networks.geometry.curve as curve +import networks.geometry.CurveSurface as CurveSurface +import networks.geometry.segment as segment +import random + + +class Lane: + def __init__(self, coordinates, width, lane_type): + self.coordinates = coordinates + self.width = width + self.lane_type = lane_type + self.lane_materials = lane_materials + self.surface = [] + + def create_surface(self, coordinates): + resolution, distance = curve.resolution_distance(coordinates, 6) + + curve_points = curve.curve(coordinates, resolution) + curve_surface = CurveSurface.CurveSurface(coordinates) + curve_surface.compute_curvature() + + # Set the road to be flat + normals = [] + for i in range(len(curve_surface.curvature)): + normals.append((0, 1, 0)) + + # Compute each line + for distance in range(width): + offset = curve.offset(curve_surface.curve, distance, normals) + for i in range(len(offset)-1): + line = segment.discrete_segment(offset[i], offset[i+1]) + for coordinate in line: + self.surface.append((coordinate, random.choices( + list(lane_materials.keys()), + weights=lane_materials.values(), + k=1,))) diff --git a/networks/lanes/materials.json b/networks/lanes/materials.json new file mode 100644 index 0000000..c6602f8 --- /dev/null +++ b/networks/lanes/materials.json @@ -0,0 +1,7 @@ +{ + "classic_lane": {"stone": 3, "andesite": 1}, + "modern_lane": {"black_concrete": 3, "black_concrete_powder": 1}, + + "bike_lane_green": {"green_concrete": 3, "green_concrete_powder": 1}, + "bike_lane_red": {"red_concrete": 3, "red_concrete_powder": 1} +} \ No newline at end of file diff --git a/networks/lines/Line.py b/networks/lines/Line.py new file mode 100644 index 0000000..86e6dbd --- /dev/null +++ b/networks/lines/Line.py @@ -0,0 +1,41 @@ +import networks.geometry.curve as curve +import networks.geometry.segment as segment +import random + + +class Line: + def __init__(self, coordinates, line_type): + self.coordinates = coordinates + self.line_type = line_type + self.surface = [] + + def get_surface(self): + resolution, distance = curve.resolution_distance(self.coordinates, 6) + + curve_points = curve.curve(self.coordinates, resolution) + + # Compute the line + + pattern_length = 0 + pattern_materials = [] + for key, value in self.line_type.items(): + pattern_length += int(key) + for _ in range(int(key)): + pattern_materials.append(value) + + pattern_iteration = 0 + for i in range(len(curve_points)-1): + line = segment.discrete_segment(curve_points[i], curve_points[i+1]) + for coordinate in line: + block = random.choices( + list(pattern_materials[pattern_iteration].keys()), + weights=pattern_materials[pattern_iteration].values(), + k=1)[0] + if block != 'None': + self.surface.append((coordinate, block)) + + pattern_iteration += 1 + if pattern_iteration >= pattern_length: + pattern_iteration = 0 + + return self.surface diff --git a/networks/lines/line.json b/networks/lines/line.json new file mode 100644 index 0000000..d331aa5 --- /dev/null +++ b/networks/lines/line.json @@ -0,0 +1,9 @@ +{ + "solid_white": { + "1": {"white_concrete": 3, "white_concrete_powder": 1} + }, + "broken_white": { + "3": {"white_concrete": 3, "white_concrete_powder": 1}, + "1": {"None": 1} + } +} \ No newline at end of file diff --git a/networks/roads/Lane.py b/networks/roads/Lane.py deleted file mode 100644 index 1fb14c4..0000000 --- a/networks/roads/Lane.py +++ /dev/null @@ -1,3 +0,0 @@ -class Lane: - def __init__(self, coordinates, lane_type): - pass diff --git a/networks/roads/Road.py b/networks/roads/Road.py index f05719a..5a12aa2 100644 --- a/networks/roads/Road.py +++ b/networks/roads/Road.py @@ -1,7 +1,7 @@ class Road: - def __init__(self, coordinates, road_type): - self.coordinates = coordinates # List of tuples (x1, y1, z1) in order + def __init__(self, coordinates): + self.coordinates = coordinates # List of tuples (x1, y1, z1) in order self.road_type = road_type # 'road', 'highway' def place_roads(self): - pass \ No newline at end of file + pass