From 323111f2f6eef4701d6ecf6aa8379d2bd6020d3d Mon Sep 17 00:00:00 2001 From: Xeon0X Date: Fri, 26 Apr 2024 12:38:15 +0200 Subject: [PATCH] First implementation of curve surface failed (not enough precision) --- main.py | 42 +++++++++++++--------------------------- networks/Curve.py | 12 +++++++----- networks/CurveSurface.py | 30 ++++++++++++++++++++++++++++ networks/roads/Lane.py | 3 +++ 4 files changed, 53 insertions(+), 34 deletions(-) create mode 100644 networks/CurveSurface.py create mode 100644 networks/roads/Lane.py diff --git a/main.py b/main.py index 11a9d75..edaa077 100644 --- a/main.py +++ b/main.py @@ -1,39 +1,23 @@ from gdpc import Editor, Block, geometry import networks.Curve as curve +import networks.CurveSurface as CurveSurface import networks.Segment as segment import numpy as np editor = Editor(buffering=True) -# # Get a block -# block = editor.getBlock((0,48,0)) +y = 20 +coordinates = [(-854, 87+y, -210), (-770, 99+y, -207), (-736, 85+y, -184)] +resolution, distance = curve.resolution_distance(coordinates, 10) -# # Place a block -# editor.placeBlock((394, 132, 741), Block("stone")) - -# # Build a cube -# geometry.placeCuboid(editor, (458, 92, 488), (468, 99, 471), Block("oak_planks")) - -# curve = curve.Curve([(396, 132, 740), (435, 138, 730), -# (443, 161, 758), (417, 73, 729)]) -# curve.compute_curve() - -# for point in curve.computed_points: -# print(point) -# editor.placeBlock(point, Block("stone")) - - -# print(segment.parallel(((0, 0, 0), (0, 0, 10)), 10)) -# print(segment.orthogonal((0, 0, 0), (1, 0, 0), 10)) -# print(curve.curvature(np.array(([0, 0, 0], [0, 1, 1], [1, 0, 1])))) - - -coordinates = [(-854, 77, -210), (-770, 89, -207), (-736, 75, -184)] - -resolution = curve.resolution_from_spacing(coordinates, 10) - -i = 10 curve_points = curve.curve(coordinates, resolution) +curve_surface = CurveSurface.CurveSurface(curve_points) +curve_surface.compute_curvature() +curve_surface.compute_surface(50, curve_surface.curvature, 1) + +for line_range in range(len(curve_surface.offset_points[0])): + for coordinate in curve_surface.offset_points[line_range]: + editor.placeBlock(coordinate, Block("white_concrete")) # offset = curve.offset(curve_points, i) @@ -45,7 +29,7 @@ curve_points = curve.curve(coordinates, resolution) # for coordinate in offset: # editor.placeBlock(coordinate, Block("red_concrete")) -for coordinate in curve_points: - editor.placeBlock(coordinate, Block("white_concrete")) +# for coordinate in curve_points: +# editor.placeBlock(coordinate, Block("white_concrete")) ### diff --git a/networks/Curve.py b/networks/Curve.py index 93d78fa..2139c7a 100644 --- a/networks/Curve.py +++ b/networks/Curve.py @@ -77,12 +77,14 @@ def curvature(curve): return normal -def offset(curve, distance): - curvature_values = curvature(curve) +def offset(curve, distance, normals): + if len(normals) != len(curve): + raise ValueError( + 'Number of normals and number of points in the curve do not match') # Offsetting offset_segments = [segment.parallel( - (curve[i], curve[i+1]), distance, curvature_values[i]) for i in range(len(curve) - 1)] + (curve[i], curve[i+1]), distance, normals[i]) for i in range(len(curve) - 1)] # Combining segments combined_curve = [] @@ -95,7 +97,7 @@ def offset(curve, distance): return combined_curve -def resolution_from_spacing(target_points, spacing_distance): +def resolution_distance(target_points, spacing_distance): length = 0 for i in range(len(target_points) - 1): length += sqrt( @@ -103,7 +105,7 @@ def resolution_from_spacing(target_points, spacing_distance): + ((target_points[i][1] - target_points[i + 1][1]) ** 2) + ((target_points[i][2] - target_points[i + 1][2]) ** 2) ) - return round(length / spacing_distance) + return round(length / spacing_distance), length def simplify_segments(points, epsilon): diff --git a/networks/CurveSurface.py b/networks/CurveSurface.py new file mode 100644 index 0000000..7eb9946 --- /dev/null +++ b/networks/CurveSurface.py @@ -0,0 +1,30 @@ +import networks.Curve as curve +import networks.Segment as segment +import numpy as np + + +class CurveSurface: + def __init__(self, points, reshape=True, spacing_distance=10): + self.points = np.array(points) + if reshape: + self.resolution, self.length = curve.resolution_distance( + self.points, spacing_distance=spacing_distance) + self.curve = curve.curve(self.points, self.resolution) + else: # Point can also be given already in curved form + self.curve = self.points + + def compute_curvature(self): + self.curvature = curve.curvature(self.curve) + + def compute_surface(self, width, normals, resolution): + self.offset_points = [None] * (width * resolution) + self.surface = [] + for line_range in range(width * resolution): + self.offset_points[line_range] = curve.offset( + self.curve, line_range/resolution, normals) + + for i in range(len(self.offset_points[line_range])-1): + self.surface.extend(segment.discrete_segment( + self.offset_points[line_range][i], self.offset_points[line_range][i+1], pixel_perfect=False)) + + print(self.surface) diff --git a/networks/roads/Lane.py b/networks/roads/Lane.py new file mode 100644 index 0000000..1fb14c4 --- /dev/null +++ b/networks/roads/Lane.py @@ -0,0 +1,3 @@ +class Lane: + def __init__(self, coordinates, lane_type): + pass