44
55import pickle
66import json
7+ import collections
78
89from copy import deepcopy
910from ast import literal_eval
@@ -619,11 +620,10 @@ def from_vertices_and_faces(cls, vertices, faces):
619620
620621 Parameters
621622 ----------
622- vertices : list
623- A list of vertices, represented by their XYZ coordinates.
624- faces : list
625- A list of faces.
626- Each face is a list of indices referencing the list of vertex coordinates.
623+ vertices : list, dict
624+ A list of vertices, represented by their XYZ coordinates, or a dictionary of vertex keys pointing to their XYZ coordinates.
625+ faces : list, dict
626+ A list of faces, represented by a list of indices referencing the list of vertex coordinates, or a dictionary of face keys pointing to a list of indices referencing the list of vertex coordinates.
627627
628628 Returns
629629 -------
@@ -644,11 +644,21 @@ def from_vertices_and_faces(cls, vertices, faces):
644644
645645 """
646646 mesh = cls ()
647- for vkey , vertex in enumerate (vertices ):
648- x , y , z = vertex
649- mesh .add_vertex (key = vkey , x = x , y = y , z = z )
650- for fkey , face in enumerate (faces ):
651- mesh .add_face (face , fkey = fkey )
647+
648+ if isinstance (vertices , collections .Sequence ):
649+ for x , y , z in iter (vertices ):
650+ mesh .add_vertex (x = x , y = y , z = z )
651+ elif isinstance (vertices , collections .Mapping ):
652+ for key , xyz in vertices .items ():
653+ mesh .add_vertex (key = key , attr_dict = {i : j for i , j in zip (['x' , 'y' , 'z' ], xyz )})
654+
655+ if isinstance (faces , collections .Sequence ):
656+ for face in iter (faces ):
657+ mesh .add_face (face )
658+ elif isinstance (faces , collections .Mapping ):
659+ for fkey , vertices in faces .items ():
660+ mesh .add_face (vertices , fkey )
661+
652662 return mesh
653663
654664 @classmethod
@@ -2256,7 +2266,7 @@ def face_vertex_descendant(self, fkey, key):
22562266 return self .face [fkey ][i + 1 ]
22572267
22582268 def face_adjacency_halfedge (self , f1 , f2 ):
2259- """Find the half-edge over which tow faces are adjacent.
2269+ """Find one half-edge over which two faces are adjacent.
22602270
22612271 Parameters
22622272 ----------
@@ -2282,6 +2292,27 @@ def face_adjacency_halfedge(self, f1, f2):
22822292 if self .halfedge [v ][u ] == f2 :
22832293 return u , v
22842294
2295+ def face_adjacency_vertices (self , f1 , f2 ):
2296+ """Find all vertices over which two faces are adjacent.
2297+
2298+ Parameters
2299+ ----------
2300+ f1 : hashable
2301+ The identifier of the first face.
2302+ f2 : hashable
2303+ The identifier of the second face.
2304+
2305+ Returns
2306+ -------
2307+ list
2308+ The vertices separating face 1 from face 2.
2309+ None
2310+ If the faces are not adjacent.
2311+
2312+ """
2313+
2314+ return [vkey for vkey in self .face_vertices (f1 ) if vkey in self .face_vertices (f2 )]
2315+
22852316 def is_face_on_boundary (self , key ):
22862317 """Verify that a face is on a boundary.
22872318
@@ -3169,24 +3200,62 @@ def plot(self,
31693200
31703201 # # mesh.add_face([a, b, c, d, e, f, g, h])
31713202
3172- for k in mesh .faces ():
3173- print (k , mesh .is_face_on_boundary (k ))
3203+ # for k in mesh.faces():
3204+ # print(k, mesh.is_face_on_boundary(k))
31743205
31753206
3176- print (list (mesh .edges (True )))
3207+ # print(list(mesh.edges(True)))
31773208
31783209
3179- plotter = MeshPlotter (mesh )
3210+ # plotter = MeshPlotter(mesh)
31803211
3181- plotter .draw_vertices ()
3182- plotter .draw_edges ()
3183- plotter .draw_faces (text = 'key' )
3184- plotter .show ()
3212+ # plotter.draw_vertices()
3213+ # plotter.draw_edges()
3214+ # plotter.draw_faces(text='key')
3215+ # plotter.show()
31853216
31863217 # print(mesh.get_vertices_attribute('x'))
31873218 # print(mesh.get_vertices_attributes('xy'))
31883219
31893220 # print(mesh.get_edges_attribute('q', 1.0))
31903221 # print(mesh.get_edges_attributes('qf', (1.0, 2.0)))
31913222
3223+ vertices = {
3224+ 0 : [0 , 0 , 0 ],
3225+ 1 : [1 , 1 , 0 ],
3226+ 2 : [1 , - 1 , 0 ],
3227+ 3 : [- 1 , - 1 , 0 ],
3228+ 18 : [- 1 , 1 , 0 ]
3229+ }
3230+ faces = {
3231+ 0 : [0 , 2 , 1 ],
3232+ 45 : [0 , 18 , 3 ]
3233+ }
3234+
3235+ mesh = Mesh .from_vertices_and_faces (vertices , faces )
31923236
3237+ plotter = MeshPlotter (mesh )
3238+ plotter .draw_vertices (text = 'key' )
3239+ plotter .draw_edges ()
3240+ plotter .draw_faces (text = 'key' )
3241+ plotter .show ()
3242+
3243+ vertices = [
3244+ [0 , 0 , 0 ],
3245+ [1 , 1 , 0 ],
3246+ [1 , - 1 , 0 ],
3247+ [- 1 , - 1 , 0 ],
3248+ [- 1 , 1 , 0 ]
3249+ ]
3250+ faces = [
3251+ [0 , 2 , 1 ],
3252+ [0 , 4 , 3 ]
3253+ ]
3254+
3255+ mesh = Mesh .from_vertices_and_faces (vertices , faces )
3256+
3257+ plotter = MeshPlotter (mesh )
3258+ plotter .draw_vertices (text = 'key' )
3259+ plotter .draw_edges ()
3260+ plotter .draw_faces (text = 'key' )
3261+ plotter .show ()
0 commit comments