forked from Lotemn102/Ball-Pivoting-Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrid.py
More file actions
89 lines (71 loc) · 2.84 KB
/
grid.py
File metadata and controls
89 lines (71 loc) · 2.84 KB
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
from edge import Edge
import utils
class Grid:
def __init__(self, radius, points=None):
self.all_points = points
self.cells = {}
self.radius = radius
self.num_cells_per_axis = 0
self.bounding_box_size = 0
self.edges = []
self.triangles = []
self.cell_size = 0
if points is not None:
self.init_with_data(points)
def init_with_data(self, list_of_points):
min_x, max_x, min_y, max_y, min_z, max_z = 0, 0, 0, 0, 0, 0
# Find boundaries for the bounding box of the entire data.
for point in list_of_points:
min_x = point.x if point.x < min_x else min_x
max_x = point.x if point.x > max_x else max_x
min_y = point.y if point.y < min_y else min_y
max_y = point.y if point.y > max_y else max_y
min_z = point.z if point.z < min_z else min_z
max_z = point.z if point.z > max_z else max_z
x = max_x - min_x
y = max_y - min_y
z = max_z - min_z
# I'm taking the max since i want the bounding box to be a square.
self.bounding_box_size = max(x, y, z)
# Calculate each cell edge size.
self.num_cells_per_axis = self.bounding_box_size / (2 * self.radius)
self.cell_size = self.bounding_box_size / self.num_cells_per_axis
# Start appending the data points to their cells.
for point in list_of_points:
# Find the point's fitting cell. Assuming the bounding box's front-bottom-right corner is the origin (marked
# with * in the following figure)
'''
y
|
|________
/| |
/ | |
/ | |
| |________|______ x
| / /
| / /
|/________*/
/
/
z
'''
x_cell = int((point.x // self.cell_size) * self.cell_size)
y_cell = int((point.y // self.cell_size) * self.cell_size)
z_cell = int((point.z // self.cell_size) * self.cell_size)
# Encode cell location.
code = utils.encode_cell(x=x_cell, y=y_cell, z=z_cell)
point.cell_code = code
# Add the point to the cell in the hash table.
if code not in self.cells.keys():
self.cells[code] = []
self.cells[code].append(point)
def get_cell_points(self, cell_code):
points = []
if cell_code in self.cells.keys():
p = self.cells[cell_code]
points.extend(p)
return points
def add_edge(self, edge: Edge):
self.edges.append(edge)
def remove_edge(self, edge: Edge):
self.edges.remove(edge)