From f98af90b3e090bd282d500c4acc503cf1053a8c2 Mon Sep 17 00:00:00 2001 From: Xeon0X Date: Tue, 11 Jun 2024 18:59:14 +0200 Subject: [PATCH] Move to_vectors to Points --- main.py | 3 +- networks/geometry/Point2D.py | 11 ++++ networks/geometry/Point3D.py | 12 +++++ networks/geometry/Polyline.py | 6 ++- networks/geometry/point_tools.py | 53 -------------------- networks/roads/intersections/Intersection.py | 2 +- 6 files changed, 29 insertions(+), 58 deletions(-) diff --git a/main.py b/main.py index 84cc31e..b08ddef 100644 --- a/main.py +++ b/main.py @@ -266,5 +266,4 @@ block_list = ["blue_concrete", "red_concrete", "green_concrete", # # polyline._alpha_assign(1, polyline.length_polyline-1) # print(polyline.alpha_radii) -print( - Point2D(-10, -10).sort_by_rotation([Point2D(10, 10), Point2D(-10, 10), Point2D(10, -10)], rotation=ROTATION.CLOCKWISE)) +print(Polyline((Point2D(0, 0), Point2D(0, 10), Point2D(50, 10), Point2D(20, 20)))) diff --git a/networks/geometry/Point2D.py b/networks/geometry/Point2D.py index 3d905b8..472332a 100644 --- a/networks/geometry/Point2D.py +++ b/networks/geometry/Point2D.py @@ -181,3 +181,14 @@ class Point2D: self.y = round(self.y, ndigits) self.coordinate = (self.x, self.y) return self + + @staticmethod + def to_vectors(points: List["Point3D"]): + vectors = [] + for point in points: + vectors.append(np.array(point.coordinate)) + + if (len(vectors) == 1): + return vectors[0] + else: + return vectors diff --git a/networks/geometry/Point3D.py b/networks/geometry/Point3D.py index 28fe129..e3aad33 100644 --- a/networks/geometry/Point3D.py +++ b/networks/geometry/Point3D.py @@ -1,5 +1,6 @@ from typing import List from math import atan2, sqrt +import numpy as np class Point3D: @@ -66,3 +67,14 @@ class Point3D: self.z = round(self.z, ndigits) self.coordinate = (self.x, self.y, self.z) return self + + @staticmethod + def to_vectors(points: List["Point3D"]): + vectors = [] + for point in points: + vectors.append(np.array(point.coordinate)) + + if (len(vectors) == 1): + return vectors[0] + else: + return vectors diff --git a/networks/geometry/Polyline.py b/networks/geometry/Polyline.py index 6f3a37e..393b154 100644 --- a/networks/geometry/Polyline.py +++ b/networks/geometry/Polyline.py @@ -1,5 +1,4 @@ from networks.geometry.Point2D import Point2D -from networks.geometry.point_tools import coordinates_to_vectors from math import sqrt, inf import numpy as np @@ -19,7 +18,7 @@ class Polyline: >>> Polyline((Point2D(0, 0), Point2D(0, 10), Point2D(50, 10), Point2D(20, 20))) """ - self.points = coordinates_to_vectors(points) + self.points = Point2D.to_vectors(points) self.length_polyline = len(points) if self.length_polyline < 4: @@ -37,6 +36,9 @@ class Polyline: self._alpha_assign(0, self.length_polyline-1) + def __repr__(self): + return str(self.alpha_radii) + def _alpha_assign(self, start_index: int, end_index: int): """ The alpha-assign procedure assigning radii based on a polyline. diff --git a/networks/geometry/point_tools.py b/networks/geometry/point_tools.py index eb4b46b..d5c889d 100644 --- a/networks/geometry/point_tools.py +++ b/networks/geometry/point_tools.py @@ -3,59 +3,6 @@ import numpy as np from networks.geometry.segment_tools import discrete_segment, middle_point, parallel -def sort_by_clockwise(points): - """ - Sort point in a rotation order. Works in 2d but supports 3d. - - https://stackoverflow.com/questions/58377015/counterclockwise-sorting-of-x-y-data - - Args: - points: List of points to sort in the form of [(x, y, z), (x, y, - z)] or [(x, y), (x, y), (x, y), (x, y)]... - - Returns: - list: List of tuples of coordinates sorted (2d or 3d). - - >>> sort_by_clockwise([(0, 45, 100), (4, -5, 5),(-5, 36, -2)]) - [(0, 45, 100), (-5, 36, -2), (4, -5, 5)] - """ - x, y = [], [] - for i in range(len(points)): - x.append(points[i][0]) - y.append(points[i][-1]) - x, y = np.array(x), np.array(y) - - x0 = np.mean(x) - y0 = np.mean(y) - - r = np.sqrt((x - x0) ** 2 + (y - y0) ** 2) - - angles = np.where( - (y - y0) > 0, - np.arccos((x - x0) / r), - 2 * np.pi - np.arccos((x - x0) / r), - ) - - mask = np.argsort(angles) - - x_sorted = list(x[mask]) - y_sorted = list(y[mask]) - - # Rearrange tuples to get the right coordinates. - sorted_points = [] - for i in range(len(points)): - j = 0 - while (x_sorted[i] != points[j][0]) and (y_sorted[i] != points[j][-1]): - j += 1 - else: - if len(points[0]) == 3: - sorted_points.append((x_sorted[i], points[j][1], y_sorted[i])) - else: - sorted_points.append((x_sorted[i], y_sorted[i])) - - return sorted_points - - def segments_intersection(line0, line1, full_line=True): """ Find (or not) intersection between two lines. Works in 2d but diff --git a/networks/roads/intersections/Intersection.py b/networks/roads/intersections/Intersection.py index 29b5ed9..f8ff073 100644 --- a/networks/roads/intersections/Intersection.py +++ b/networks/roads/intersections/Intersection.py @@ -1,5 +1,5 @@ from networks.geometry.segment_tools import parallel, orthogonal -from networks.geometry.point_tools import sort_by_clockwise, segments_intersection, curved_corner_by_distance, curved_corner_by_curvature +from networks.geometry.point_tools import segments_intersection, curved_corner_by_distance, curved_corner_by_curvature from networks.roads import Road