def meshes_to_network(meshes): network = Network() network.update_default_node_attributes(mesh=None, vkey=None, fkey=None) network.update_default_edge_attributes(mesh=None, fkey=None) for i, mesh in enumerate(meshes): for vkey in mesh.vertices(): x, y, z = mesh.vertex_coordinates(vkey) network.add_node(x=x, y=y, z=z, mesh=i, vkey=vkey) for u, v in mesh.edges(): u1 = next(network.nodes_where({"vkey": u, "mesh": i})) v1 = next(network.nodes_where({"vkey": v, "mesh": i})) network.add_edge(u1, v1, mesh=i) return network
import os from sklearn.cluster import KMeans from numpy import array from compas.geometry import Pointcloud from compas.geometry import centroid_points from compas.datastructures import Network HERE = os.path.dirname(__file__) FILE = os.path.join(HERE, 'clusters.json') network = Network() network.update_default_node_attributes({'cluster': None, 'base': False}) cloud = Pointcloud.from_bounds(10, 5, 3, 100) kmeans = KMeans(n_clusters=10, n_init=500, max_iter=100).fit(array(cloud, dtype=float)) clusters = {} for i, point in zip(kmeans.labels_, cloud): print(i) if i not in clusters: clusters[i] = [] clusters[i].append(point) for index in clusters: nodes = [] for point in clusters[index]: node = network.add_node(x=point[0], y=point[1], z=point[2], cluster=index) nodes.append(node) x, y, z = centroid_points(clusters[index])
import compas_rhino from compas.datastructures import Network from compas_rhino.artists import NetworkArtist # clear the Rhino model compas_rhino.clear() # create a network network = Network() network.update_default_node_attributes(is_anchor=False) network.update_default_node_attributes(rx=0, ry=0, rz=0) network.update_default_edge_attributes(f=1) a = network.add_node(x=0, y=0, z=0, is_anchor=True) b = network.add_node(x=10, y=0, z=10, is_anchor=True) c = network.add_node(x=10, y=10, z=0, is_anchor=True) d = network.add_node(x=0, y=10, z=10, is_anchor=True) e = network.add_node(x=5, y=5, z=0) network.add_edge(a, e) network.add_edge(b, e) network.add_edge(c, e) network.add_edge(d, e) # visualize the geometry artist = NetworkArtist(network)