-
Notifications
You must be signed in to change notification settings - Fork 5
/
mesh.py
843 lines (685 loc) · 24 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
"""
This library will try to provide mesh conversion to and from every DCC and file format
we use here at Blur
The Mesh object itself will try to provide an efficient way to run algorithms on
3d mesh data
There are also convenience classes that act as function sets for mesh object components
Verts and Faces are the most common convenience classes and give access to adjacency
data (for backwards compatibility)
VertSet and FaceSet classes are just sets that also contain references back to the mesh
"""
class Mesh(object):
"""
The inputs to this mesh object are inspired by the .obj file format
Arguments:
verts: list of 3d vectors ((x,y,z), ...)
faces: list of lists of vertex indices formatted like:
[[1, 2, 3], [4, 5, 6, 7] ...]
uvs (default:None): list of 2d vectors ((u, v), ...)
uvFaces (default:none): face representations used for UVs
uvMap (defaul:None): dict of lists of 2d vectors {channelName:((u, v), ...), ...}
uvFaceMap (default:none): dict of face representations used for UVs
ensureWinding (default:True): Ensure that neighbor operations return items in order
Faces are ccw wound
The idea for this data structure is to keep all the data in one place and have the
individual convenience objects reference back to it. Storing in one place means I
can store it efficiently, and allow for efficient editing and querying. The
convenience layer on top means you can get "good enough" performance for high-level
tasks without having to directly manipulate the underlying data.
This will error out with non-manifold geometry. This is a feature not a bug ;)
The only strange property is "Edge Adjacency", and this borrows some data from
the winged edge structure. Given two neighboring vertices, return the bordering
faces. The first face contains the vertices wound backwards (clockwise),
the second face contains the vertices wound forwards (counter clockwise)
For instance, we could pass the vert indices (0, 1), and get back (10, 13) where
faces[10].verts is (1, 0, 2) and faces[13].verts is (0, 1, 3).
UV's have a default mapping as part of class. However, named UV sets are supported
through the uvMap and uvFaceMap arguments. The default mapping is automatically
given the name 'default'. Partial uv sets are not handled
Float Data:
vertArray : array of 3d floats
uvMap : map of d[name] => array of 2d floats
Face Data:
faceVertArray : 2d array of vertex indices per face
uvFaceMap : map of d[name] => 2d array of UV indices per face
Vert Data:
vertToFaces : 2d array of face indices per vert
vertNeighbors : 2d array of vert indices per vert
vertWindingPairs : map of vertIdx => [(ccw Vert Pair), ...]
Edge Adjacency:
faceEdgeAdjacency : map of d[(vert1, vert2)] => (order, reverse) face indices
where `order` contains (vert1, vert2) in the winding order
and `reverse` contains (vert2, vert1) in the winding order
"""
def __init__(self, verts, faces, uvs=None, uvFaces=None, uvMap=None, uvFaceMap=None, ensureWinding=False):
self._verts = None
self._faces = None
self._uvs = {}
self._uvFaces = {}
self._wound = ensureWinding
self.children = []
# load the float data
self.vertArray = verts
self.uvMap = uvMap or {}
if uvs is not None:
self.uvMap['default'] = uvs
# build the empty face data
self.faceVertArray = faces
self.uvFaceMap = uvFaceMap or {}
if uvFaces:
self.uvFaceMap['default'] = uvFaces
vertToFaces = {}
neighborDict = {}
hedgeCCWDict = {}
hedgeCWDict = {}
self.vertWindingPairs = {}
for f, face in enumerate(self.faceVertArray):
for v, vert in enumerate(face):
vertToFaces.setdefault(vert, []).append(f)
self.vertWindingPairs.setdefault(face[v-1], []).append((face[v-2], face[v]))
neighborDict.setdefault(face[v-1], []).append(face[v])
neighborDict.setdefault(face[v-1], []).append(face[v-2])
hedgeCCWDict[(face[v-1], face[v])] = f
hedgeCWDict[(face[v], face[v-1])] = f
vertCount = len(self.vertArray)
if ensureWinding:
self.vertNeighbors = [self._linkPairs(self.vertWindingPairs[i]) for i in xrange(vertCount)]
else:
self.vertNeighbors = [neighborDict[i] for i in xrange(vertCount)]
self.faceEdgeAdjacency = {}
for i in xrange(vertCount):
for j in self.vertNeighbors[i]:
pair = (i, j)
ccw = hedgeCCWDict.get(pair, None)
cw = hedgeCWDict.get(pair, None)
self.faceEdgeAdjacency[pair] = (ccw, cw)
if ensureWinding:
self.vertToFaces = []
for i in xrange(vertCount):
wings = [self.faceEdgeAdjacency[(i, j)] for j in self.vertNeighbors[i]]
wings = [i for i in wings if None not in i]
self.vertToFaces.append(self._linkPairs(wings))
else:
self.vertToFaces = [vertToFaces[i] for i in xrange(vertCount)]
def ensureWinding(self):
""" Ensure the winding of the mesh after-the-fact """
if self._wound:
return
self._wound = True
self.vertNeighbors = [self._linkPairs(self.vertWindingPairs[i]) for i in xrange(self.vertCount())]
self.vertToFaces = []
for i in xrange(self.vertCount()):
wings = [self.faceEdgeAdjacency[(i, j)] for j in self.vertNeighbors[i]]
wings = [i for i in wings if None not in i]
self.vertToFaces.append(self._linkPairs(wings))
@classmethod
def loadObj(cls, path, ensureWinding=True):
""" Read a .obj file and produce a Mesh object
Args:
path: The path to the .obj formatted file
Returns:
A Mesh() object containing lists of linked
vertices, edges, and faces
Raises:
IOError: If the file cannot be opened
"""
vertices = []
faces = []
uvs = []
uvIdxs = []
nIdxs = []
with open(path, 'r') as inFile:
lines = inFile.readlines()
for line in lines:
sp = line.split()
if sp == []:
pass
elif sp[0] == "v":
v = [float(i) for i in sp[1:4]]
vertices.append(v)
elif sp[0] == 'vt':
uv = [float(i) for i in sp[1:3]]
uvs.append(uv)
elif sp[0] == "f":
face = []
for s in sp[1:]:
vt = [int(i)-1 if i else None for i in s.split('/')]
# Pad out the face vert/uv/normal triples
face.append(vt + [None]*3)
# Then cut them back to 3
# Still doing this even though I'm ignoring normals
face = [i[:3] for i in face]
f, u, n = zip(*face)
faces.append(f)
if any(u):
uvIdxs.extend(u)
if any(n):
nIdxs.extend(n)
flatUVs = None
if uvIdxs:
flatUVs = [uvs[i] for i in uvIdxs]
return cls(vertices, faces, flatUVs, ensureWinding)
@classmethod
def loadAbc(cls, path, meshName=None, ensureWinding=True):
""" Read a .abc file and produce a Mesh object
Args:
path: The path to the .abc formatted file
Returns:
A Mesh() object containing lists of linked
vertices, edges, and faces
Raises:
IOError: If the file cannot be opened
"""
from alembic.Abc import IArchive
from alembic.AbcGeom import IPolyMesh
from blur3d.lib.alembiclib import findAlembicObject
iarch = IArchive(str(path)) # because alembic hates unicode
mesh = findAlembicObject(iarch.getTop(), abcType=IPolyMesh, name=meshName)
sch = mesh.getSchema()
rawVerts = sch.getPositionsProperty().samples[0]
rawFaces = sch.getFaceIndicesProperty().samples[0]
rawCounts = sch.getFaceCountsProperty().samples[0]
iuvs = sch.getUVsParam()
faces = []
faceCounter = 0
for count in rawCounts:
faces.append(list(rawFaces[faceCounter: faceCounter+count]))
faceCounter += count
uvs = None
uvFaces = None
if iuvs.valid():
uvValue = iuvs.getValueProperty().getValue()
uvs = zip(uvValue.x, uvValue.y)
if iuvs.isIndexed():
idxs = list(iuvs.getIndexProperty().getValue())
uvFaces = []
uvCounter = 0
for count in rawCounts:
uvFaces.append(list(idxs[uvCounter: uvCounter+count]))
uvCounter += count
verts = []
for v in rawVerts:
verts.append(list(v))
return cls(verts, faces, ensureWinding=ensureWinding)
@staticmethod
def _linkPairs(pairs):
"""
Take a list of paired items, and order them so the
second item of a pair matches the first of the next pair
Then return the first item of each pair for each cycle.
for instance, with two cycles:
input: [(1, 2), (11, 12), (3, 1), (10, 11), (2, 3), (12, 10)]
reorder: [[(1, 2), (2, 3), (3, 1)], [(10, 11), (11, 12), (12, 10)]]
output: [1, 2, 3, 10, 11, 12]
"""
fwPairs = dict(pairs)
out = []
while fwPairs:
linked = []
# pick a random start from whatever's left
nxt = next(fwPairs.iterkeys())
while nxt is not None:
# Follow the pairs around until I cant find more
nnxt = fwPairs.pop(nxt, None)
linked.append((nxt, nnxt))
nxt = nnxt
if fwPairs and linked[0][0] != linked[-1][0]:
# if there's still some left and we didn't find a cycle
# then search backwards
bkPairs = {j: i for i, j in fwPairs.iteritems()}
inv = []
nxt = linked[0][0]
while nxt is not None:
# Follow the pairs around until I cant find more
nnxt = bkPairs.pop(nxt, None)
inv.append((nnxt, nxt))
nxt = nnxt
#reverse and remove the extra (idx, None) pair
linked = inv[-2::-1] + linked
# Rebuild what's left into a new dict for the next group
fwPairs = {j: i for i, j in bkPairs.iteritems()}
# Parse the final output
fin = [i for i, _ in linked]
if fin[0] == fin[-1]:
# Get rid of the doubled values when finding cycles
fin = fin[:-1]
out.extend(fin)
return out
def adjacentFacesByEdge(self, faceIdx):
""" Get all faces that share an edge with the given face
Winding Guaranteed Counterclockwise
Args:
faceIdx (int): Face Index
Returns:
list: List of faces indices that share an edge with the input
Raises:
IndexError: The input is out of range
"""
verts = self.faceVertArray[faceIdx]
out = []
for i in xrange(verts):
edge = (verts[i-1], verts[i])
_, rev = self.faceEdgeAdjacency.get(edge, (None, None))
out.append(rev)
return out
def adjacentFacesByVert(self, faceIdx):
""" Get all faces that share a vert with the given face
Winding Not Guaranteed
Args:
faceIdx (int): Face Index
Returns:
list: List of faces indices that share a vert with the input
Raises:
IndexError: The input is out of range
"""
out = set()
verts = self.faceVertArray[faceIdx]
for v in verts:
out.update(self.vertToFaces[v])
return list(out)
def adjacentVertsByFace(self, vertIdx):
""" Get all verts that share a face with the given vert
Winding Not Guaranteed
Args:
vertIdx (int): Vertex Index
Returns:
list: List of vertex indices that share a face with the input
Raises:
IndexError: The input is out of range
"""
faces = self.vertToFaces[vertIdx]
out = set()
for f in faces:
out.update(self.faceVertArray[f])
return list(out)
def adjacentVertsByEdge(self, vertIdx):
""" Get all verts that share an edge with the given vert
Winding Guaranteed Counterclockwise
Args:
vertIdx (int): Vertex Index
Returns:
list: List of vertex indices that share an edge with the input
Raises:
IndexError: The input is out of range
"""
return self.vertNeighbors[vertIdx]
def vertCount(self):
""" Returns: The number of vertices in this mesh """
return len(self.vertArray)
def faceCount(self):
""" Returns: The number of faces in this mesh """
return len(self.faceVertArray)
def verts(self):
""" Get all vertex convenience objects
Returns:
list: List of vertex objects
"""
if self._verts is None:
self._verts = [Vert(self, i) for i in xrange(len(self.vertArray))]
return self._verts
def faces(self):
""" Get all face convenience objects
Returns:
list: List of face objects
"""
if self._faces is None:
self._faces = [Face(self, i) for i in xrange(len(self.faceVertArray))]
return self._faces
def vertSet(self):
""" Get a vertex set containing the whole mesh
Returns:
VertSet: A vertex set containing the whole mesh
"""
ret = VertSet(self, [])
ret.update(range(len(self.vertArray)))
return ret
def faceSet(self):
""" Get a face set containing the whole mesh
Returns:
FaceSet: A face set containing the whole mesh
"""
ret = FaceSet(self, [])
ret.update(range(len(self.faceVertArray)))
return ret
def uvs(self, channelName='default'):
""" Get all UV convenience objects
Returns:
list: List of UV objects
"""
if channelName not in self._uvs:
uvm = self.uvMap.get(channelName)
if uvm is not None:
self._uvs[channelName] = [UV(self, channelName, i) for i in uvm]
return self._uvs.get(channelName)
def uvFaces(self, channelName='default'):
""" Get all UV convenience objects
Returns:
list: List of UV objects
"""
if channelName not in self._uvFaces:
uvfm = self.uvFaceMap.get(channelName, [])
if uvfm is not None:
self._uvFaces[channelName] = [UVFace(self, channelName, i) for i in uvfm]
return self._uvFaces.get(channelName)
def isBorderVert(self, vertIdx):
""" Check if the given vertex index is along a border
Returns:
Bool: Whether the given vertex index is along a border
"""
neighbors = self.vertNeighbors[vertIdx]
for n in neighbors:
if None in self.faceEdgeAdjacency[(vertIdx, n)]:
return True
return False
def getBorderVerts(self):
""" Get a vertex set of the border vertices
Returns:
VertSet: VertSet of border vertices
"""
out = VertSet(self)
for edge, adj in self.faceEdgeAdjacency.iteritems():
if None in adj:
out.update(edge)
return out
def clearCache(self):
""" Clear all cached convenience classes """
self._verts = None
self._faces = None
self._uvs = {}
self._uvFaces = {}
self.children = []
#######################################################################################
class MeshComponent(object):
""" Base class for all mesh components
Handles keeping track of the mesh and index
Arguments:
mesh (Mesh): The mesh object that this is a component of
index (int): The index of this component
"""
__slot__ = 'mesh', 'index'
def __init__(self, mesh, index):
self.mesh = mesh
self.index = index
self.mesh.children.append(self)
def __int__(self):
return self.index
def clear(self):
""" Remove all reference data from this object """
self.mesh = None
self.mesh.children.remove(self)
def __eq__(self, other):
if isinstance(other, type(self)):
if self.mesh is other.mesh:
return self.index == other.index
# Not worrying about floating point equality
return self.value() == other.value()
return NotImplemented
def __hash__(self):
return self.index
class Vert(MeshComponent):
''' A convenience class for accessing and manipulating vertices '''
def adjacentVertsByEdge(self):
""" Get all verts that share an edge with the given vert
Returns:
list: List of verts that share an edge with the input
"""
idxs = self.mesh.adjacentVertsByEdge(self.index)
verts = self.mesh.verts()
return [verts[i] for i in idxs]
def adjacentVertsByFace(self):
""" Get all verts that share a face with the given vert
Returns:
list: List of verts that share a face with the input
"""
idxs = self.mesh.adjacentVertsByFace(self.index)
verts = self.mesh.verts()
return [verts[i] for i in idxs]
def adjacentFaces(self):
""" Get all faces that use this vertex
Returns:
list: List of faces that use this vertex
"""
idxs = self.mesh.vertToFaces[self.index]
faces = self.mesh.faces()
return [faces[i] for i in idxs]
def value(self):
""" Get the vertex's position
Returns:
tuple: (x, y, z) vertex position
"""
return self.mesh.vertArray[self.index]
def setValue(self, pos):
""" Set the vertex position
Args:
pos (tuple): (x, y, z) vertex position
"""
t = tuple(pos)
assert len(t) == 3
self.mesh.vertArray[self.index] = t
class Face(MeshComponent):
''' A convenience class for accessing and manipulating faces '''
def adjacentFacesByEdge(self):
""" Get all faces that share an edge with the given face
Returns:
list: List of faces that share an edge with the input
"""
idxs = self.mesh.adjacentFacesByEdge(self.index)
faces = self.mesh.faces()
return [faces[i] for i in idxs]
def adjacentFacesByVert(self):
""" Get all faces that share a vert with the given face
Returns:
list: List of faces that share a vert with the input
"""
idxs = self.mesh.adjacentFacesByVert(self.index)
faces = self.mesh.faces()
return [faces[i] for i in idxs]
def __eq__(self, other):
if isinstance(other, Face):
return set(self.verts()) == set(other.verts())
return NotImplemented
def __hash__(self):
return hash(self.verts())
def verts(self):
""" Get all verts that make up this face
Returns:
list: List of vertexes that make up this face
"""
idxs = self.mesh.faceVertArray[self.index]
verts = self.mesh.verts()
return [verts[i] for i in idxs]
def uvs(self, name='default'):
""" Get all uvs that make up this face
Returns:
list: List of uvs that make up this face
"""
idxs = self.mesh.faceUVArray[name][self.index]
uvs = self.mesh.uvs(name)
return [uvs[i] for i in idxs]
class UV(MeshComponent):
''' A convenience class for accessing and manipulating uvs '''
__slot__ = 'mesh', 'index', 'name'
def __init__(self, mesh, name, index):
self.name = name
super(UV, self).__init__(mesh, index)
def value(self):
""" Get the uv's position
Returns:
tuple: (u, v) position
"""
return self.mesh.uvMap[self.name][self.index]
def setValue(self, pos):
""" Set the uv's position
Args:
pos (tuple): (u, v) position
"""
t = tuple(pos)
assert len(t) == 2
self.mesh.uvMap[self.name][self.index] = t
def __hash__(self):
return hash(self.name, self.index)
class UVFace(MeshComponent):
''' A convenience class for accessing and manipulating faces '''
__slot__ = 'mesh', 'index', 'name'
def __init__(self, mesh, name, index):
self.name = name
super(UVFace, self).__init__(mesh, index)
def __eq__(self, other):
if isinstance(other, UVFace):
return set(self.uvs()) == set(other.uvs())
return NotImplemented
def __hash__(self):
return hash(self.verts())
def verts(self):
""" Get all verts that make up this UVFace
Returns:
list: List of vertexes that make up this face
"""
idxs = self.mesh.faceVertArray[self.index]
verts = self.mesh.verts()
return [verts[i] for i in idxs]
def uvs(self, name='default'):
""" Get all uvs that make up this UVFace
Returns:
list: List of uvs that make up this face
"""
idxs = self.mesh.uvFaceMap[name][self.index]
uvs = self.mesh.uvs(name)
return [uvs[i] for i in idxs]
#######################################################################################
class MeshSetMeta(type):
""" Wraps the magic methods to ensure that a reference to the mesh is kept """
def __new__(mcs, clsName, bases, dct):
names = ['__and__', '__or__', '__sub__', '__xor__', 'copy',
'difference', 'intersection', 'union', 'symmetric_difference']
rnames = ['__rand__', '__ror__', '__rsub__', '__rxor__']
def wrap_closure(name, right):
def inner(self, *args):
result = getattr(set, name)(self, *args)
if not hasattr(result, 'mesh'):
if right:
# Gotta special-case the __r*__ methods
# because they get the mesh from the args
result.mesh = args[0].mesh
else:
result.mesh = self.mesh
return result
inner.fn_name = name
inner.__name__ = name
return inner
if set in bases:
for attr in names:
dct[attr] = wrap_closure(attr, False)
for attr in rnames:
dct[attr] = wrap_closure(attr, True)
return super(MeshSetMeta, mcs).__new__(mcs, clsName, bases, dct)
class MeshSet(set):
""" An set-like object that deals with geometry """
__metaclass__ = MeshSetMeta
def __init__(self, mesh, indices=None):
idxs = [] if indices is None else [int(i) for i in indices]
super(MeshSet, self).__init__(idxs)
self.mesh = mesh
self.mesh.children.append(self)
def grow(self, growMethod, exclude=None, track=False):
""" Add adjacent objects as determined by the growMethod, and
keep track of the previous growth for optimization purposes
Args:
growMethod (function): A function that returns the neighbors
for each object index
exclude (MeshSet): A set of objects to exclude from growth
track (bool): Whether to keep track of the exclude input as well
Returns:
MeshSet: An updated MeshSet
MeshSet: (Only if track=True) The updated exclude set
"""
if not isinstance(exclude, type(self)):
exclude = exclude or []
exclude = type(self)(self.mesh, exclude)
grown = type(self)(self.mesh)
growSet = self - exclude
for vert in growSet:
grown.update(growMethod(vert))
newGrown = grown - exclude
if track:
newExclude = exclude | growSet
return newGrown, newExclude
else:
return newGrown
def _partitionIslands(self, growMethod):
''' Separate the current set into sets of neighboring objects
Args:
growMethod (function): A function that returns the neighbors
for each object index
Returns:
list(MeshSet): A list of interconnected object sets
'''
myType = type(self)
allVerts = myType(self.mesh, [])
allVerts.update(self)
islands = []
while allVerts:
seed = myType(self.mesh, [allVerts.pop()])
island = myType(self.mesh, [])
while seed:
grown = myType(self.mesh, [])
for vert in seed:
grown.update(growMethod(vert))
newIsland = (island | seed) & self
seed = (grown - island) & self
island = newIsland
islands.append(island)
allVerts.difference_update(island)
return islands
class VertSet(MeshSet):
""" A set-like object that deals with vertices """
def growByEdge(self, exclude=None, track=False):
""" Add verts that share edges with the current set
Args:
exclude (VertSet): A set of vertices to exclude from growth
track (bool): Whether to keep track of the exclude input as well
Returns:
VertSet: An updated vertex set
"""
return self.grow(self.mesh.adjacentVertsByEdge, exclude=exclude, track=track)
def growByFace(self, exclude=None, track=False):
""" Add verts that share faces with the current set
Args:
exclude (VertSet): A set of vertices to exclude from growth
track (bool): Whether to keep track of the exclude input as well
Returns:
VertSet: An updated vertex set
"""
return self.grow(self.mesh.adjacentVertsByFace, exclude=exclude, track=track)
def partitionIslands(self):
''' Separate the current set into sets of neighboring objects
Returns:
list(VertSet): A list of interconnected object sets
'''
return super(VertSet, self)._partitionIslands(self.mesh.adjacentVertsByFace)
class FaceSet(MeshSet):
""" A set-like object that deals with faces """
def growByEdge(self, exclude=None, track=False):
""" Add faces that share edges with the current set
Args:
exclude (FaceSet): A set of faces to exclude from growth
track (bool): Whether to keep track of the exclude input as well
Returns:
FaceSet: An updated face set
"""
return self.grow(self.mesh.adjacentFacesByEdge, exclude=exclude, track=track)
def growByVert(self, exclude=None, track=False):
""" Add faces that share verts with the current set
Args:
exclude (FaceSet): A set of faces to exclude from growth
track (bool): Whether to keep track of the exclude input as well
Returns:
FaceSet: An updated face set
"""
return self.grow(self.mesh.adjacentFacesByVert, exclude=exclude, track=track)
def partitionIslands(self):
''' Separate the current set into sets of neighboring objects
Returns:
list(FaceSet): A list of interconnected object sets
'''
return super(FaceSet, self)._partitionIslands(self.mesh.adjacentFacesByVert)