/
mesh.py
260 lines (206 loc) · 11.1 KB
/
mesh.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
from bisect import bisect
from collections import defaultdict
from itertools import combinations
from math import floor, sqrt
import trimesh
from progress.bar import Bar
PADDING = 0.05
VERTICAL_SCALE = sqrt(2) / 4 # Vertical scale. Constant a from paper.
def normalise(mesh):
"""Rescale and translate the input mesh so that it is inside the unit cube.
Return the new mesh along with the scale and translation needed to undo this transformation."""
mins, maxes = mesh.bounds
scale = (1 - 2 * PADDING) / max(i - j for i, j in zip(maxes, mins))
translate = [-i * scale + PADDING for i in mins]
return affine(mesh, scale, translate), 1 / scale, [x - PADDING / scale for x in mins]
def affine(mesh, scale, translate):
"""Rescale and translate a mesh."""
mesh = mesh.copy()
mesh.apply_transform(trimesh.transformations.scale_and_translate(scale, translate))
return mesh
def midnormal(N, mesh):
"""Return the mesh obtained from middle normal disks."""
# Using mesh.contains([P]) is incredibly slow.
# So we will take advantage of the fact that our vertices are in vertical stacks.
dx = 1 / N
dy = dx * sqrt(3) / 2
dz = VERTICAL_SCALE * dx
heights = defaultdict(list) # Mapping lattice point (i, j) |--> list of heights of triangles of mesh above (i, j, 0).
tetrahedra = set() # The set of tetrahedra that could contain a normal disk.
with Bar('Evaluating triangles', max=len(mesh.triangles), width=80, suffix='%(percent)d%% ETA: %(eta)ds') as bar:
for index, triangle in enumerate(mesh.triangles):
if index % 1000 == 0:
bar.next(1000)
# Find the bounding box of this triangle.
x_min, y_min, z_min = triangle.min(axis=0)
x_max, y_max, z_max = triangle.max(axis=0)
i_min, i_max = floor(x_min / dx), floor(x_max / dx) + 2
j_min, j_max = floor(y_min / dy), floor(y_max / dy) + 1
# Find the plane that contains this triangle.
(Nx, Ny, Nz), K = spanning_plane(*triangle)
# To compute z coordinate where the vertical line above (x, y, 0) meets the plane, we use the fact that
# the plane is defined by N . X == K
# So rearrange Nx * x0 + Ny * y0 + Nz * z == K, to solve for z.
# We use the standard triangulation of the xy-plane via equilateral triangles:
# *-----*-----*
# \ / \ / \
# \ / \ / \
# *-----*-----*
# / \ / \ /
# / \ / \ /
# *---(i,j)---*
# \ / \ / \
# \ / \ / \
# *-----*---- *
# Each lattice point determines a parallelogram (pair of triangles) with 4 vertices. On even rows it looks like this:
# V3 V2
# *-----*-----*
# \ B / \ / \
# \ / A \ / \
# (i,j)---*---- *
# V0 V1
#
# while odd rows look like this:
# V0 V1
# *-----*-----*
# / \ / \ A /
# / \ / B \ /
# *---(i,j)---*
# V3 V2
# Above each triangle, the vertices of the tetrahedra spiral around the three corners of the triangle:
# -#-----------------#-->
# |\ |\ |\ |\
# | \ | \ | \ | \
# | \ | \ | \ | \
# -|-----|-----|---#-|----->
# | / | / | / | /
# | / | / | / | /
# |/ |/ |/ |/
# -------#-------------->
#
# -------> z
#
# The next tetrahedra above starts one click around while tetrahedra in the adjacent stacks
# spiral in the opposite directions.
# Unfortunately we need a bunch of conditionals to handle the case when the plane is vertical and so Nz == 0.
if Nz:
# A dictionary mapping the (i, j) lattice point to the height of the plane above that point.
lattice_heights = dict(((i, j), (K - Nx * (i - (j % 2) / 2) * dx - Ny * j * dy) / Nz) for i in range(i_min, i_max + 2) for j in range(j_min, j_max + 2))
for i in range(i_min, i_max + 1):
for j in range(j_min, j_max + 1):
# Define the four vertices of the parallelogram:
V0 = (i + 0, j + j % 2)
V1 = (i + 1, j + j % 2)
V2 = (i + 1/2, j + 1 - j % 2)
V3 = (i - 1/2, j + 1 - j % 2)
# Get the height of the plane above each corner of the parallelogram
# If the plane is absolutely vertical then use the entire vertical range [0, 1].
corner_heights = [lattice_heights[i, j], lattice_heights[i + 1, j], lattice_heights[i, j + 1], lattice_heights[i + 1, j + 1]] if Nz else [0, 1]
corner_min, corner_max = max(min(corner_heights), z_min), min(max(corner_heights), z_max)
k_min, k_max = floor(corner_min / dz) - 4, floor(corner_max / dz) + 1
# Catalogue all the tetrahedra that occur in this vertical range.
for V in [(V0, V1, V2), (V0, V3, V2)]: # The A stack and the B stack.
for k in range(k_min, k_max):
v0, v1, v2 = V[k % 3], V[(k + 1) % 3], V[(k + 2) % 3]
A = (v0[0], v0[1], k + 0 + i % 3)
B = (v1[0], v1[1], k + 1 + i % 3)
C = (v2[0], v2[1], k + 2 + i % 3)
D = (v0[0], v0[1], k + 3 + i % 3)
tetrahedra.add((A, B, C, D))
if Nz:
x, y = (i - (j % 2) / 2) * dx, j * dy
if point_is_under_triangle(x, y, *triangle):
heights[i - (j % 2) / 2, j].append(lattice_heights[i, j])
# Sort the heights above each point.
for above in heights.values():
above.sort()
empty = 0 # Count the number of tetrahedra that don't contain any normal disks.
vertices = []
print(f"Tiling unit cube with {len(tetrahedra)} tetrahedra")
with Bar('Evaluating tetrahedra', max=len(tetrahedra), width=80, suffix='%(percent)d%% ETA: %(eta)ds') as bar:
for index, (a, b, c, d) in enumerate(tetrahedra): # Note the quads depend on this ordering.
if index % 1000 == 0:
bar.next(1000)
A = (a[0] * dx, a[1] * dy, a[2] * dz)
B = (b[0] * dx, b[1] * dy, b[2] * dz)
C = (c[0] * dx, c[1] * dy, c[2] * dz)
D = (d[0] * dx, d[1] * dy, d[2] * dz)
# Find Euclidean coordinates for edge midpoints
AB, AC, AD, BC, BD, CD = [midpoint(p, q) for p, q in combinations([A, B, C, D], r=2)]
signs = [((len(heights[i, j]) - bisect(heights[i, j], k * dz)) % 2 == 1 if (i, j) in heights else False) for i, j, k in [a, b, c, d]]
if not signs[0]: # Flip so sign fA is True.
signs = [not sign for sign in signs]
if signs == [True, True, True, True]: # No disk.
empty += 1
elif signs == [True, False, False, False]: # Triangle near vertex A.
vertices.extend([AB, AC, AD])
elif signs == [True, False, True, True]: # Triangle near vertex B.
vertices.extend([AB, BC, BD])
elif signs == [True, True, False, True]: # Triangle near vertex C.
vertices.extend([AC, BC, CD])
elif signs == [True, True, True, False]: # Triangle near vertex D.
vertices.extend([AD, BD, CD])
elif signs == [True, False, False, True]: # Quadrilateral separating AD & BC.
vertices.extend([AB, BD, AC])
vertices.extend([BD, AC, CD])
elif signs == [True, True, False, False]: # Quadrilateral separating AB & CD
vertices.extend([AD, BD, AC])
vertices.extend([BD, AC, BC])
elif signs == [True, False, True, False]: # Quadrilateral separating AC & BD. Since VERTICAL_SCALE <= sqrt(2) / 4, we need the AD--BC diagonal.
vertices.extend([AB, AD, BC])
vertices.extend([AD, BC, CD])
faces = [[i, i+1, i+2] for i in range(0, len(vertices), 3)]
print(f"Empty tetrahedra {empty}")
mesh = trimesh.Trimesh(vertices, faces)
# assert mesh.is_watertight
return mesh
def remove_quads(mesh):
"""Return a new mesh where degree 4 vertices have been removed."""
vertices = mesh.vertices
faces = mesh.faces
# A list where the ith entry contains the set of vertices that are adjacent in the 1-skeleton to the ith vertex.
neighbours = [set(x) for x in mesh.vertex_neighbors]
valences = [len(x) for x in neighbours]
new_triangles = [face for face in faces if all(valences[v_index] != 4 for v_index in face)]
# For a face that is a quad, identify its good diagonal.
# This is the one not between two valence 6 vertices
for index in range(len(vertices)):
if valences[index] == 4:
N = neighbours[index]
a = next(iter(N)) # Get one of the neighbours.
b, c = N.intersection(neighbours[a]) # Get the two neighbours adjacent to a.
[d] = N.difference({a, b, c}) # Get the final neighbor.
if valences[a] + valences[d] > valences[b] + valences[c]:
new_triangles.extend([[a, b, c], [b, c, d]]) # Diagonal b--c.
else:
new_triangles.extend([[b, a, d], [a, d, c]]) # Diagonal a--d.
return trimesh.Trimesh(vertices, new_triangles)
def displace_to_surface(surface, original):
"""Return a new mesh by pushing the vertices of surface to the closest point of original."""
surface = surface.copy()
surface.vertices, _, _ = original.nearest.on_surface(surface.vertices)
return surface
### Some boring functions for basic Euclidean geometry.
def midpoint(p1, p2):
return tuple(0.5 * a + 0.5 * b for a, b in zip(p1, p2))
def point_is_under_triangle(x, y, p0, p1, p2):
"""Determine whether the vertical line through (x, y, 0) intersects the triangle (p0, p1, p2)."""
dX = x - p2[0]
dY = y - p2[1]
dX21 = p2[0] - p1[0]
dY12 = p1[1] - p2[1]
# The Barycentric coordinates of (x, y) wrt the triangle.
s = dY12 * dX + dX21 * dY
t = (p2[1] - p0[1]) * dX + (p0[0] - p2[0]) * dY
D = dY12 * (p0[0] - p2[0]) + dX21 * (p0[1] - p2[1])
if D < 0:
return s <= 0 and t <= 0 and s + t >= D
return s >= 0 and t >= 0 and s + t <= D
def spanning_plane(v0, v1, v2):
"""Return the normal N and offset K of the plane containing v0, v1 and v2.
These are so that X = (x, y, z) is on the plane if and only if N . X == K"""
Nx = (v1[1] - v0[1]) * (v2[2] - v0[2]) - (v1[2] - v0[2]) * (v2[1] - v0[1])
Ny = (v1[2] - v0[2]) * (v2[0] - v0[0]) - (v1[0] - v0[0]) * (v2[2] - v0[2])
Nz = (v1[0] - v0[0]) * (v2[1] - v0[1]) - (v1[1] - v0[1]) * (v2[0] - v0[0])
K = Nx * v0[0] + Ny * v0[1] + Nz * v0[2]
return (Nx, Ny, Nz), K