Move to_vectors to Points

This commit is contained in:
2024-06-11 18:59:14 +02:00
parent 229c43c308
commit f98af90b3e
6 changed files with 29 additions and 58 deletions

View File

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

View File

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

View File

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

View File

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

View File

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