First implementation of curve surface failed (not enough precision)

This commit is contained in:
2024-04-26 12:38:15 +02:00
parent bdde8f54b1
commit 323111f2f6
4 changed files with 53 additions and 34 deletions

42
main.py
View File

@@ -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"))
###

View File

@@ -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):

30
networks/CurveSurface.py Normal file
View File

@@ -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)

3
networks/roads/Lane.py Normal file
View File

@@ -0,0 +1,3 @@
class Lane:
def __init__(self, coordinates, lane_type):
pass