From e879e9c034c1a17250c17b148d0cdcfa28c3a868 Mon Sep 17 00:00:00 2001 From: Xeon0X Date: Tue, 28 May 2024 00:51:29 +0200 Subject: [PATCH] First prototype of roads intersecitons --- main.py | 72 ++++++++++++++++++-- networks/geometry/point_tools.py | 13 +++- networks/roads/Road.py | 1 + networks/roads/intersections/Intersection.py | 29 +++++++- 4 files changed, 106 insertions(+), 9 deletions(-) diff --git a/main.py b/main.py index 62447a1..d7fffcc 100644 --- a/main.py +++ b/main.py @@ -9,6 +9,9 @@ import json from buildings.Building import Building import random +from networks.roads import Road as Road +from networks.roads.intersections import Intersection as Intersection + from networks.geometry.point_tools import curved_corner_intersection editor = Editor(buffering=True) @@ -93,25 +96,84 @@ block_list = ["blue_concrete", "red_concrete", "green_concrete", # # for coordinate in curve_surface.left_side[current_range]: # # editor.placeBlock(coordinate, Block("yellow_concrete")) +# --- # coordinates = [(0, 0, 0), (0, 0, 10), (0, 0, 20)] -# with open('networks/lines/lines.json') as f: +# with open('networks/roads/lines/lines.json') as f: # lines_type = json.load(f) # l = Line.Line(coordinates, lines_type.get('solid_white')) # print(l.get_surface()) -# with open('networks/lanes/lanes.json') as f: +# with open('networks/roads/lanes/lanes.json') as f: # lanes_type = json.load(f) # l = Lane.Lane(coordinates, lanes_type.get('classic_lane'), 5) # print(l.get_surface()) -circle = curved_corner_intersection( - ((-1313, 392), (-1378, 415)), ((-1371, 348), (-1341, 439)), 30, angle_adaptation=True, output_only_points=False) +# circle = curved_corner_intersection( +# ((-1313, 392), (-1378, 415)), ((-1371, 348), (-1341, 439)), 30, angle_adaptation=True, output_only_points=False) -print(circle) +# print(circle) # for coordinate in circle[0]: # editor.placeBlock( # (round(coordinate[0]), 100, round(coordinate[1])), Block("green_concrete")) + +# --- + +# r1 = Road.Road((-1341, 100, 439), "None") +# r2 = Road.Road((-1378, 100, 415), "None") + +# i = Intersection.Intersection( +# (-1352, 100, 405), [(-1345, 100, 426), (-1369, 100, 412)], [r1, r2]) + + +# --- + +r1 = Road.Road((-1337, 71, 472), "None") +r2 = Road.Road((-1269, 80, 574), "None") +r3 = Road.Road((-1392, 79, 527), "None") + +i = Intersection.Intersection( + (-1327, 71, 533), [(-1335, 71, 494), (-1298, 75, 553), (-1366, 78, 530)], [r1, r2, r3]) + +# --- + +# y = 100 + +# r1 = Road.Road((-1337, y, 472), "None") +# r2 = Road.Road((-1269, y, 574), "None") +# r3 = Road.Road((-1392, y, 527), "None") + +# i = Intersection.Intersection( +# (-1327, y, 533), [(-1335, y, 494), (-1298, y, 553), (-1366, y, 530)], [r1, r2, r3]) + + +i.compute_curved_corner() + +for j in range(len(i.orthogonal_delimitations)): + + coordinates = segment_tools.discrete_segment( + i.orthogonal_delimitations[j][0][0], i.orthogonal_delimitations[j][0][1]) + for coordinate in coordinates: + editor.placeBlock(coordinate, Block("purple_concrete")) + + coordinates = segment_tools.discrete_segment( + i.orthogonal_delimitations[j][1][0], i.orthogonal_delimitations[j][1][1]) + for coordinate in coordinates: + editor.placeBlock(coordinate, Block("pink_concrete")) + + coordinates = segment_tools.discrete_segment( + i.parallel_delimitations[j][0][0], i.parallel_delimitations[j][0][1]) + for coordinate in coordinates: + editor.placeBlock(coordinate, Block("orange_concrete")) + + coordinates = segment_tools.discrete_segment( + i.parallel_delimitations[j][1][0], i.parallel_delimitations[j][1][1]) + for coordinate in coordinates: + editor.placeBlock(coordinate, Block("yellow_concrete")) + +for coordinate in i.intersections: + if coordinate != None: + editor.placeBlock(coordinate, Block("black_concrete")) diff --git a/networks/geometry/point_tools.py b/networks/geometry/point_tools.py index a195205..9182efe 100644 --- a/networks/geometry/point_tools.py +++ b/networks/geometry/point_tools.py @@ -1,5 +1,6 @@ from math import sqrt, cos, pi, sin import numpy as np +from networks.geometry.segment_tools import discrete_segment, middle_point def circle(center, radius): @@ -51,7 +52,7 @@ def is_in_triangle(point, xy0, xy1, xy2): def distance(xy1, xy2): # TODO : Can be better. - return sqrt((xy2[0] - xy1[0]) ** 2 + (xy2[1] - xy1[1]) ** 2) + return sqrt((xy2[0] - xy1[0]) ** 2 + (xy2[-1] - xy1[-1]) ** 2) def get_angle(xy0, xy1, xy2): @@ -210,11 +211,17 @@ def segments_intersection(line0, line1, full_line=True): <= y <= max(line1[0][-1], line1[1][-1]) ): - return x, y + if len(line0[0]) > 2: + return middle_point(nearest(discrete_segment(line1[0], line1[1], pixel_perfect=True), (x, y)), nearest(discrete_segment(line0[0], line0[1], pixel_perfect=True), (x, y))) + else: + return x, y else: return None else: - return x, y + if len(line0[0]) > 2: + return middle_point(nearest(discrete_segment(line1[0], line1[1], pixel_perfect=True), (x, y)), nearest(discrete_segment(line0[0], line0[1], pixel_perfect=True), (x, y))) + else: + return x, y def circle_segment_intersection( diff --git a/networks/roads/Road.py b/networks/roads/Road.py index 176c9f9..4eeb66b 100644 --- a/networks/roads/Road.py +++ b/networks/roads/Road.py @@ -2,6 +2,7 @@ class Road: def __init__(self, coordinates, road_configuration): self.coordinates = coordinates # List of tuples (x1, y1, z1) in order self.road_configuration = road_configuration # 'road', 'highway' + self.width = 10 # TODO def place_roads(self): pass diff --git a/networks/roads/intersections/Intersection.py b/networks/roads/intersections/Intersection.py index cdd9713..a29db30 100644 --- a/networks/roads/intersections/Intersection.py +++ b/networks/roads/intersections/Intersection.py @@ -1,3 +1,30 @@ +from networks.geometry.segment_tools import parallel, orthogonal +from networks.geometry.point_tools import sort_by_clockwise, segments_intersection +from networks.roads import Road + + class Intersection: - def __init__(self, Roads): + def __init__(self, center, coordinates, Roads): + self.center = center + self.coordinates = coordinates self.Roads = Roads + self.parallel_delimitations = [] + self.orthogonal_delimitations = [] + self.intersections = [] + + def compute_curved_corner(self): + # Necessary to test nearby intersection + self.coordinates = sort_by_clockwise(self.coordinates) + + for i, coordinate in enumerate(self.coordinates): + right_side, left_side = parallel((coordinate, self.center), self.Roads[i].width), parallel( + (coordinate, self.center), -self.Roads[i].width) + self.parallel_delimitations.append((right_side, left_side)) + self.orthogonal_delimitations.append( + ((right_side[0], left_side[0]), (right_side[-1], left_side[-1]))) + + for j in range(len(self.Roads)): + self.intersections.append(segments_intersection( + self.parallel_delimitations[j][1], self.parallel_delimitations[(j+1) % len(self.Roads)][0], full_line=False)) + + print(self.intersections)