-
Notifications
You must be signed in to change notification settings - Fork 0
/
point.py
109 lines (86 loc) · 3.26 KB
/
point.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
from __future__ import annotations
from typing import Any
import numpy as np
class Point:
def __init__(self, location: list, x: float = None, y: float = None, z: float = None) -> None:
if x and y and z:
self.x = x
self.y = y
self.z = z
self.location = (x, y, z)
else:
self.x = location[0]
self.y = location[1]
self.z = location[2]
self.location = location
def __call__(self, *args: Any, **kwds: Any) -> tuple:
return self.location
def __repr__(self) -> str:
return f"<Point {self.x, self.y, self.z}>"
def find_neighbouring_vertices(self, point_cloud: np.array, radius: float) -> np.array:
"""
Find the neighbouring vertices within a certain radius
:param point_cloud: The point cloud to find neighbours within
:param radius: The radius to search for neighbours
:return: An array of neighbouring points in the radius
"""
neighbours = []
for point in point_cloud:
if point == self:
continue
distance = self.distance_to_point(point)
if distance < radius:
neighbours.append(point)
return np.array(neighbours)
def find_neighbouring_vertices_with_distance(self, point_cloud: np.array, radius: float) -> np.array:
"""
Find the neighbouring vertices within a certain radius
:param point_cloud: The point cloud to find neighbours within
:param radius: The radius to search for neighbours
:return: An array of neighbouring points in the radius with distances
"""
neighbours = []
distances = []
for point in point_cloud:
if point == self:
continue
distance = self.distance_to_point(point)
if distance < radius:
neighbours.append(point)
distances.append(distance)
return np.array(neighbours), np.array(distances)
def distance_to_point(self, point) -> float:
"""
Find distance of a point in relation to this point
:param point: Point to compare location to
:return: distance to
"""
# √((a2-a1)^2 + (b2-b1)^2 + (c2-c1)^2)
x = np.power(point.x-self.x, 2)
y = np.power(point.y-self.y, 2)
z = np.power(point.z-self.z, 2)
distance = np.sqrt(
np.add(
np.add(
x, y
),
z
)
)
return float(distance)
def get_closest_point(self, points: np.array, distances: np.array, exclude: list = None) -> Point:
"""
Get the vertex closest to this vertex
:param points: NumPy Array of Point objects -> return the closest
:param distances: NumPy Array of floating point numbers to find the minimum
:return: Point object with the closest relative position
"""
closest_index = np.where(distances == min(distances))
closest = points[closest_index]
return closest[0]
def get_location(self) -> tuple:
"""
Get the location of the point
:return: The location of the point
"""
return self.location