def test_json_network(): before = Network() a = before.add_node() b = before.add_node() before.add_edge(a, b) after = compas.json_loads(compas.json_dumps(before)) assert before.dtype == after.dtype assert before.attributes == after.attributes assert all(before.has_node(node) for node in after.nodes()) assert all(after.has_node(node) for node in before.nodes()) assert all(before.has_edge(*edge) for edge in after.edges()) assert all(after.has_edge(*edge) for edge in before.edges())
from compas.geometry import Pointcloud, KDTree from compas.datastructures import Network cloud = Pointcloud.from_bounds(10, 5, 3, 200) tree = KDTree(cloud) network = Network() for point in cloud: network.add_node(x=point[0], y=point[1], z=point[2]) for node in network.nodes(): point = network.node_coordinates(node) for nbr in tree.nearest_neighbors(point, 4, distance_sort=True): if nbr[2] < 1e-6: continue if not network.has_edge(node, nbr[1], directed=False): network.add_edge(node, nbr[1]) start = network.get_any_node() goal = network.get_any_node() path = network.shortest_path(start, goal)
height = 1.0 world = Frame.worldXY() circle = [[0, 0, 0.5 * 0.7 * height], [0, 0, 1]], 0.05 cylinder = Cylinder(circle, 0.7 * height) circle = [[0, 0, 0.7 * height], [0, 0, 1]], 0.1 cone = Cone(circle, 0.3 * height) for node in network.nodes(): a = network.node_attributes(node, 'xyz') for nbr in network.neighbors(node): edge = node, nbr if not network.has_edge(*edge): edge = nbr, node b = network.node_attributes(nbr, 'xyz') force = network.edge_attribute(edge, 'f') direction = normalize_vector(subtract_vectors(b, a)) frame = Frame.from_plane([a, direction]) X = Transformation.from_frame_to_frame(world, frame) S = Scale.from_factors([force, 1, 1]) X = X * S shaft = cylinder.transformed(X) tip = cone.transformed(X) artist = CylinderArtist(shaft, layer=layer, color=(255, 0, 0))
from compas_rhino.artists import CylinderArtist network = Network() cloud = Pointcloud.from_bounds(10, 5, 3, 200) tree = KDTree(cloud) for point in cloud: network.add_node(x=point[0], y=point[1], z=point[2]) for node in network.nodes(): point = network.node_coordinates(node) for nbr in tree.nearest_neighbors(point, 4, distance_sort=True): if nbr[2] < 1e-6: continue if not network.has_edge(node, nbr[1], directed=False): network.add_edge(node, nbr[1]) start = network.get_any_node() goal = network.get_any_node() path = network.shortest_path(start, goal) edges = [(u, v) if network.has_edge(u, v) else (v, u) for u, v in pairwise(path)] artist = NetworkArtist(network, layer="ITA20::Network") artist.clear_layer() artist.draw_nodes(color={start: (255, 0, 0), goal: (0, 0, 255)}) artist.draw_edges(color={edge: (0, 255, 0) for edge in edges}) for u, v in edges: o = network.edge_midpoint(u, v)