Skip to content

Commit b2acc37

Browse files
committed
Merge branch 'master' of https://github.com/compas-dev/compas
2 parents 72e330d + 74468ba commit b2acc37

File tree

2 files changed

+124
-23
lines changed

2 files changed

+124
-23
lines changed

src/compas/datastructures/mesh/_mesh.py

Lines changed: 88 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import pickle
66
import json
7+
import collections
78

89
from copy import deepcopy
910
from 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()

src/compas/datastructures/network/_network.py

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import pickle
66
import pprint
77
import json
8+
import collections
89

910
from copy import deepcopy
1011
from ast import literal_eval
@@ -444,8 +445,8 @@ def from_vertices_and_edges(cls, vertices, edges):
444445
445446
Parameters
446447
----------
447-
vertices : list of list of float
448-
A list of vertex coordinates.
448+
vertices : list , dict
449+
A list of vertex coordinates or a dictionary of keys pointing to vertex coordinates to specify keys.
449450
edges : list of tuple of int
450451
451452
Returns
@@ -461,8 +462,14 @@ def from_vertices_and_edges(cls, vertices, edges):
461462
462463
"""
463464
network = cls()
464-
for x, y, z in vertices:
465-
network.add_vertex(x=x, y=y, z=z)
465+
466+
if isinstance(vertices, collections.Sequence):
467+
for x, y, z in vertices:
468+
network.add_vertex(x=x, y=y, z=z)
469+
elif isinstance(vertices, collections.Mapping):
470+
for key, xyz in vertices.items():
471+
network.add_vertex(key = key, attr_dict = {i: j for i, j in zip(['x', 'y', 'z'], xyz)})
472+
466473
for u, v in edges:
467474
network.add_edge(u, v)
468475
return network
@@ -1338,3 +1345,28 @@ def plot(self,
13381345
plotter.draw_edges()
13391346

13401347
plotter.show()
1348+
1349+
vertices = {44: [0.0, 0.0, 0.0], 38: [1.0, 0.0, 0.0], 2: [2.0, 0.0, 0.0]}
1350+
edges = [(44, 38), (38, 2)]
1351+
1352+
network = Network.from_vertices_and_edges(vertices, edges)
1353+
print(network)
1354+
1355+
plotter = NetworkPlotter(network, figsize=(10, 7))
1356+
plotter.defaults['vertex.fontsize'] = 8
1357+
plotter.draw_vertices(text='key', radius=0.2)
1358+
plotter.draw_edges()
1359+
plotter.show()
1360+
1361+
vertices = [[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [2.0, 0.0, 0.0]]
1362+
edges = [(0, 1), (1, 2)]
1363+
1364+
network = Network.from_vertices_and_edges(vertices, edges)
1365+
print(network)
1366+
1367+
plotter = NetworkPlotter(network, figsize=(10, 7))
1368+
plotter.defaults['vertex.fontsize'] = 8
1369+
plotter.draw_vertices(text='key', radius=0.2)
1370+
plotter.draw_edges()
1371+
plotter.show()
1372+

0 commit comments

Comments
 (0)