-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDissCell.py
241 lines (197 loc) · 8.82 KB
/
DissCell.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
from util import *
from PyraCutObject import *
class DissCell:
def __init__(self, sixCellPyras):
self.verts, self.faces, self.facesPlaneDiKeyTuples = [], [], []
vOffset = 0
for di, cellPyra in enumerate(sixCellPyras):
vs, fs, fpKeys = cellPyra.getHullData(True)
self.verts.append(vs)
self.faces += [(f + vOffset).tolist() for f in fs]
vOffset += len(vs)
self.facesPlaneDiKeyTuples += [(di, fpKey) for fpKey in fpKeys]
self.verts = np.vstack(self.verts)
self.facesEdgeHashs = [[] for face in self.faces]
self.computeFacesEdgeHashs()
def dissolve(self):
if min(list(zip(*self.facesPlaneDiKeyTuples))[1]) in [-6,-7,-8,-9,-10]:
self.clearInnerFaces()
self.dissolveFaces()
self.removeDuplicateVerts()
self.dissolveEdges()
self.dissolveVerts()
# check if second pass is required?
self.dissolveFaces(ignoreDis=True)
self.dissolveEdges()
self.removeDuplicateVerts()
def computeFacesEdgeHashs(self, fIdxs=[]):
fIdxs = range(len(self.faces)) if not len(fIdxs) else fIdxs
for fIdx in fIdxs:
es = faceToEdges(self.faces[fIdx])
self.facesEdgeHashs[fIdx] = set(cantorPiV(es))
def clearInnerFaces(self):
fIdxs, faces, fpMaps = map(list, zip(*[(fIdx, face, fpKeyTup[0]*10-fpKeyTup[1]-6) for fIdx, (face, fpKeyTup) in enumerate(zip(self.faces, self.facesPlaneDiKeyTuples)) if fpKeyTup[1] <= -6]))
fCenters = map(lambda face: self.verts[face].mean(axis=0), faces)
fpMapIdxs = np.concatenate([[fpMap]*(len(face)-2) for fpMap, face in zip(fpMaps, faces)])
fTriMasks = map(lambda fpMap: fpMapIdxs == pyraFaceMaps[fpMap], fpMaps)
tris = facesToTris(faces)
ABCs = self.verts[tris]
uvws = ABCs[:,[1,2,0]] - ABCs
ns = np.transpose(np.dstack([cross(uvws[:,i], -uvws[:,(i+2)%3], True) for i in range(3)]), axes = [0,2,1])
self.removeFaces([fIdx for fIdx, fCenter, m in zip(fIdxs, fCenters, fTriMasks) if pointInTriangles3D(ABCs[m], fCenter, uvws[m], ns[m], True)])
def removeFaces(self, fIdxs):
for fIdx in sorted(np.unique(fIdxs))[::-1]:
self.removeFace(fIdx)
def removeFace(self, fIdx):
self.faces.pop(fIdx)
self.facesEdgeHashs.pop(fIdx)
self.facesPlaneDiKeyTuples.pop(fIdx)
def mergeFaces(self, fIdx, fJdx):
fIdx, fJdx = min(fIdx, fJdx), max(fIdx, fJdx)
idges = faceToEdges(self.faces[fIdx])
jdges = faceToEdges(self.faces[fJdx])
edges = filterForSingleEdges(np.vstack([idges, jdges]))
vIdxs = edges.ravel()
if len(np.unique(vIdxs)) != len(vIdxs)//2:
return False
newFace = edgesToPath(edges)
if newFace is None:
return False
self.faces[fIdx] = newFace
self.computeFacesEdgeHashs([fIdx])
self.removeFace(fJdx)
return True
def areFacesCoplanar(self, fIdx, fJdx, ignoreDis=False):
if ignoreDis:
if self.facesPlaneDiKeyTuples[fIdx][1] <= 0 and self.facesPlaneDiKeyTuples[fJdx][1] <= 0:
return self.facesPlaneDiKeyTuples[fIdx][1] == self.facesPlaneDiKeyTuples[fJdx][1]
return self.facesPlaneDiKeyTuples[fIdx] == self.facesPlaneDiKeyTuples[fJdx]
def dissolveFaces(self, ignoreDis=False):
iLast = 0
while True:
for fIdx in range(iLast, len(self.faces)):
iLast = fIdx
iHashs = self.facesEdgeHashs[fIdx]
merged = False
for fJdx in range(fIdx+1, len(self.faces)):
if not iHashs.isdisjoint(self.facesEdgeHashs[fJdx]):
if self.areFacesCoplanar(fIdx, fJdx, ignoreDis):
merged = self.mergeFaces(fIdx, fJdx)
if merged:
break
if merged:
break
else:
break
def removeVertex(self, vIdx):
self.verts = np.vstack([self.verts[:vIdx], self.verts[vIdx+1:]])
facesToRemove = []
for fIdx, face in enumerate(self.faces):
if vIdx in face:
face.pop(face.index(vIdx))
if len(face) < 3:
facesToRemove.append(fIdx)
for i in range(len(face)):
if face[i] > vIdx:
face[i] -= 1
self.removeFaces(facesToRemove)
def dissolveEdges(self):
vRemove = [[] for v in self.verts]
for face in self.faces:
n = len(face)
for i in range(n):
pdx = face[(i-1) % n]
idx = face[i]
ndx = face[(i+1) % n]
if pdx == ndx:
vRemove[idx].append(True)
continue
vP, vI, vN = self.verts[[pdx, idx, ndx]]
dst = distPointToEdge(vP, vN, vI)
if dst < eps:
vRemove[idx].append(True)
continue
u, v = normVec(self.verts[[pdx, ndx]] - self.verts[idx])
dt = 1-np.abs(np.dot(u, v))
if dt < eps:
vRemove[idx].append(True)
continue
vRemove[idx].append(max(dst, dt) < eps*100)
vertsToRemove = [vIdx for vIdx, votes in enumerate(vRemove) if all(votes)]
for vIdx in sorted(np.unique(vertsToRemove))[::-1]:
self.removeVertex(vIdx)
def dissolveVerts(self):
vIdxs = np.unique(flatten(self.faces))
self.verts = self.verts[vIdxs]
self.faces = reIndexIndices(self.faces)
self.faces = [f[np.nonzero(f-np.roll(f, -1))[0]].tolist() for f in self.faces]
facesToRemove = [fIdx for fIdx, face in enumerate(self.faces) if len(face) < 3]
self.removeFaces(facesToRemove)
self.computeFacesEdgeHashs()
def removeDuplicateVerts(self, thresh=eps):
dupliVerts = []
duplis = set()
for vIdx, v in enumerate(self.verts[:-1]):
if vIdx in duplis:
continue
ds = norm(self.verts[(vIdx+1):] - v)
mIdxs = np.where(ds < thresh)[0]
if len(mIdxs):
dIdxs = (mIdxs + vIdx + 1).tolist()
dupliVerts.append((vIdx, dIdxs))
duplis.update(dIdxs)
def areNeighbors(face, idx, jdx):
iPos = face.index(idx)
jPos = face.index(jdx)
return max(iPos, jPos) - min(iPos, jPos) in [1, len(face)-1]
for vIdx, rIdxs in dupliVerts:
valid = True
valids = []
for fIdx, face in enumerate(self.faces):
directNeighbors = False
if vIdx in face:
for rIdx in rIdxs:
if rIdx in face:
if areNeighbors(face, rIdx, vIdx):
directNeighbors = True
else:
valid = False
break
for i, rIdx in enumerate(rIdxs):
for rJdx in rIdxs[i+1:]:
if rIdx in face and rJdx in face:
if not areNeighbors(face, rIdx, rJdx):
valid = False
break
if not valid:
break
valids.append(directNeighbors)
if not any(valids):
continue
if not all(valids):
break
else:
for face in self.faces:
for rIdx in rIdxs:
if rIdx in face:
face[face.index(rIdx)] = vIdx
self.dissolveVerts()
def getFacesTriangulated(self):
return np.vstack([np.int32(face)[triangulatePoly3D(self.verts[face])] for face in self.faces])
def plot(self, withVertIdxs=False, withFacePlaneKeys=False):
if mlabMissing:
warnings.warn("Mayavi missing.")
return
# wireframe
eTris = np.pad(facesToEdges(self.faces), [[0, 0], [0, 1]], "reflect")
mlab.triangular_mesh(self.verts[:, 0], self.verts[:, 1], self.verts[:, 2], eTris, representation="mesh", tube_radius=0.005)
if withVertIdxs:
for i, v in enumerate(self.verts):
mlab.text3d(v[0], v[1], v[2], str(i), scale=(0.01, 0.01, 0.01))
if withFacePlaneKeys:
for i, face in enumerate(self.faces):
fc = self.verts[face].mean(axis=0)
mlab.text3d(fc[0], fc[1], fc[2], str(
self.facesPlaneDiKeyTuples[i]), scale=(0.01, 0.01, 0.01))
def writeToObj(self, fName="dissCell.obj"):
writeObjFile(fName, self.verts, list(map(np.int32, self.faces)))