Skip to content

Commit 0461823

Browse files
authored
Merge pull request #251 from robin-oval/feature/extended_datastructure_from_elements
extension data structure from vertices and edges/faces
2 parents c94b97f + 9082470 commit 0461823

File tree

2 files changed

+102
-22
lines changed

2 files changed

+102
-22
lines changed

src/compas/datastructures/mesh/_mesh.py

Lines changed: 66 additions & 18 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
@@ -3169,24 +3179,62 @@ def plot(self,
31693179

31703180
# # mesh.add_face([a, b, c, d, e, f, g, h])
31713181

3172-
for k in mesh.faces():
3173-
print(k, mesh.is_face_on_boundary(k))
3182+
# for k in mesh.faces():
3183+
# print(k, mesh.is_face_on_boundary(k))
31743184

31753185

3176-
print(list(mesh.edges(True)))
3186+
# print(list(mesh.edges(True)))
31773187

31783188

3179-
plotter = MeshPlotter(mesh)
3189+
# plotter = MeshPlotter(mesh)
31803190

3181-
plotter.draw_vertices()
3182-
plotter.draw_edges()
3183-
plotter.draw_faces(text='key')
3184-
plotter.show()
3191+
# plotter.draw_vertices()
3192+
# plotter.draw_edges()
3193+
# plotter.draw_faces(text='key')
3194+
# plotter.show()
31853195

31863196
# print(mesh.get_vertices_attribute('x'))
31873197
# print(mesh.get_vertices_attributes('xy'))
31883198

31893199
# print(mesh.get_edges_attribute('q', 1.0))
31903200
# print(mesh.get_edges_attributes('qf', (1.0, 2.0)))
31913201

3202+
vertices = {
3203+
0: [0, 0, 0],
3204+
1: [1, 1, 0],
3205+
2: [1, -1, 0],
3206+
3: [-1, -1, 0],
3207+
18: [-1, 1, 0]
3208+
}
3209+
faces = {
3210+
0: [0, 2, 1],
3211+
45: [0, 18, 3]
3212+
}
31923213

3214+
mesh = Mesh.from_vertices_and_faces(vertices, faces)
3215+
3216+
plotter = MeshPlotter(mesh)
3217+
plotter.draw_vertices(text='key')
3218+
plotter.draw_edges()
3219+
plotter.draw_faces(text='key')
3220+
plotter.show()
3221+
3222+
vertices = [
3223+
[0, 0, 0],
3224+
[1, 1, 0],
3225+
[1, -1, 0],
3226+
[-1, -1, 0],
3227+
[-1, 1, 0]
3228+
]
3229+
faces = [
3230+
[0, 2, 1],
3231+
[0, 4, 3]
3232+
]
3233+
3234+
mesh = Mesh.from_vertices_and_faces(vertices, faces)
3235+
3236+
plotter = MeshPlotter(mesh)
3237+
plotter.draw_vertices(text='key')
3238+
plotter.draw_edges()
3239+
plotter.draw_faces(text='key')
3240+
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)