Add lane object

This commit is contained in:
2024-05-26 18:47:15 +02:00
parent 2e66a539cf
commit 1543530488
5 changed files with 24 additions and 17 deletions

16
main.py
View File

@@ -1,4 +1,5 @@
import networks.lines.Line as Line import networks.lines.Line as Line
import networks.lanes.Lane as Lane
from gdpc import Editor, Block, geometry from gdpc import Editor, Block, geometry
import networks.geometry.curve as curve import networks.geometry.curve as curve
import networks.geometry.CurveSurface as CurveSurface import networks.geometry.CurveSurface as CurveSurface
@@ -91,9 +92,14 @@ block_list = ["blue_concrete", "red_concrete", "green_concrete",
# # editor.placeBlock(coordinate, Block("yellow_concrete")) # # editor.placeBlock(coordinate, Block("yellow_concrete"))
coordinates = [(0, 0, 0), (0, 10, 0), (0, 20, 0)] coordinates = [(0, 0, 0), (0, 0, 10), (0, 0, 20)]
with open('networks/lines/line.json') as f: # with open('networks/lines/lines.json') as f:
lines_type = json.load(f) # lines_type = json.load(f)
l = Line.Line(coordinates, lines_type.get('solid_white')) # l = Line.Line(coordinates, lines_type.get('solid_white'))
print(l.get_surface()) # print(l.get_surface())
# with open('networks/lanes/lanes.json') as f:
# lanes_type = json.load(f)
# l = Lane.Lane(coordinates, lanes_type.get('classic_lane'), 5)
# print(l.get_surface())

View File

@@ -5,18 +5,17 @@ import random
class Lane: class Lane:
def __init__(self, coordinates, width, lane_type): def __init__(self, coordinates, lane_materials, width):
self.coordinates = coordinates self.coordinates = coordinates
self.width = width self.width = width
self.lane_type = lane_type
self.lane_materials = lane_materials self.lane_materials = lane_materials
self.surface = [] self.surface = []
def create_surface(self, coordinates): def get_surface(self):
resolution, distance = curve.resolution_distance(coordinates, 6) resolution, distance = curve.resolution_distance(self.coordinates, 6)
curve_points = curve.curve(coordinates, resolution) curve_points = curve.curve(self.coordinates, resolution)
curve_surface = CurveSurface.CurveSurface(coordinates) curve_surface = CurveSurface.CurveSurface(self.coordinates)
curve_surface.compute_curvature() curve_surface.compute_curvature()
# Set the road to be flat # Set the road to be flat
@@ -25,12 +24,14 @@ class Lane:
normals.append((0, 1, 0)) normals.append((0, 1, 0))
# Compute each line # Compute each line
for distance in range(width): for distance in range(self.width):
offset = curve.offset(curve_surface.curve, distance, normals) offset = curve.offset(curve_surface.curve, distance, normals)
for i in range(len(offset)-1): for i in range(len(offset)-1):
line = segment.discrete_segment(offset[i], offset[i+1]) line = segment.discrete_segment(offset[i], offset[i+1])
for coordinate in line: for coordinate in line:
self.surface.append((coordinate, random.choices( self.surface.append((coordinate, random.choices(
list(lane_materials.keys()), list(self.lane_materials.keys()),
weights=lane_materials.values(), weights=self.lane_materials.values(),
k=1,))) k=1,)))
return self.surface

View File

@@ -4,9 +4,9 @@ import random
class Line: class Line:
def __init__(self, coordinates, line_type): def __init__(self, coordinates, line_materials):
self.coordinates = coordinates self.coordinates = coordinates
self.line_type = line_type self.line_materials = line_materials
self.surface = [] self.surface = []
def get_surface(self): def get_surface(self):
@@ -18,7 +18,7 @@ class Line:
pattern_length = 0 pattern_length = 0
pattern_materials = [] pattern_materials = []
for key, value in self.line_type.items(): for key, value in self.line_materials.items():
pattern_length += int(key) pattern_length += int(key)
for _ in range(int(key)): for _ in range(int(key)):
pattern_materials.append(value) pattern_materials.append(value)