def iter_chain(self, start=None, end=None): """Iterator over the chain of all elements between a pair of start and end elements. Args: start: Name of the starting element of the chain, or ``None`` to start at the root link. end: Name of the final element of the chain, or ``None`` to try to identify the last element. Returns: Iterator of the chain of links and joints. """ if not start: if not self.root: raise Exception('No root link found') start = self.root.name if not end: # Normally URDF will contain the end links at the end # so we break out faster by reversing the list for link in reversed(self.links): if not link.joints: end = link.name break shortest_chain = shortest_path(self._adjacency, start, end) if not shortest_chain: raise Exception('No chain found between the specified element') for name in shortest_chain: yield name
def on_pick(event): index = event.ind[0] start = index_node[index] nodes = shortest_path(network.adjacency, start, goal) colors = default_colors[:] widths = default_linewidths[:] colors[start] = highlight_color for u, v in pairwise(nodes): colors[v] = highlight_color widths[edge_index[u, v]] = highlight_width plotter.nodecollection.set_facecolor(colors) plotter.edgecollection.set_linewidths(widths) plotter.update()
def network_shortest_path(network, start, end): """Find the shortest path between two nodes of the network. Parameters ---------- network : :class:`compas.datastructures.Network` A network data structure. start : hashable The identifier of the start node. end : hashable The identifier of the end node. Returns ------- list[hashable] The nodes of the network leading from start to end. """ return shortest_path(network.adjacency, start, end)
def shortest_path_to(end): if start == end: return path = shortest_path(network.adjacency, start, end) edges = [(v, u) if not network.has_edge(u, v) else (u, v) for u, v in pairwise(path)] plotter.clear_vertices() plotter.clear_edges() plotter.draw_vertices(facecolor={ start: '#ff0000', end: '#ff0000' }, radius=0.15, picker=10) plotter.draw_edges(color={uv: '#ff0000' for uv in edges}, width={uv: 5.0 for uv in edges}) plotter.update()
def network_shortest_path(network, start, end): """Find the shortest path between two nodes of the network. Parameters ---------- network : :class:`compas.datastructures.Network` start : int end : int Returns ------- list of int The nodes of the network leading from start to end. Examples -------- >>> """ return shortest_path(network.adjacency, start, end)
def iter_chain(self, start=None, end=None): """Iterator over the chain of all elements between a pair of start and end elements. Parameters ---------- start : str, optional Name of the starting element of the chain. Defaults to the root link name. end : str, optional Name of the final element of the chain. Defaults to the name of the last element. Returns ------- Iterator of the chain of links and joints (names). Examples -------- >>> robot = RobotModel.ur5() >>> list(robot.iter_chain('world', 'forearm_link')) ['world', 'world_joint', 'base_link', 'shoulder_pan_joint', 'shoulder_link', 'shoulder_lift_joint', 'upper_arm_link', 'elbow_joint', 'forearm_link'] """ if not start: if not self.root: raise Exception('No root link found') start = self.root.name if not end: # Normally URDF will contain the end links at the end # so we break out faster by reversing the list for link in reversed(self.links): if not link.joints: end = link.name break shortest_chain = shortest_path(self._adjacency, start, end) if not shortest_chain: raise Exception('No chain found between the specified element') for name in shortest_chain: yield name
from compas.topology import shortest_path from compas.plotters import NetworkPlotter # path to the sample file DATA = os.path.join(os.path.dirname(__file__), '..', 'data') FILE = os.path.join(DATA, 'grid_irregular.obj') # make network from sample file network = Network.from_obj(FILE) # specify start and end start = 21 end = 11 # compute the shortest path taking into account the edge weights path = shortest_path(network.adjacency, start, end) # convert the path to network edges edges = [(v, u) if not network.has_edge(u, v) else (u, v) for u, v in pairwise(path)] # make a plotter plotter = NetworkPlotter(network, figsize=(10, 7)) # set default font sizes plotter.defaults['vertex.fontsize'] = 6 plotter.defaults['edge.fontsize'] = 6 # draw the vertices plotter.draw_vertices( text='key',
from compas.datastructures import Network from compas.topology import shortest_path from compas_plotters import NetworkPlotter network = Network.from_obj(compas.get('grid_irregular.obj')) adjacency = { key: network.vertex_neighbors(key) for key in network.vertices() } start = 21 end = 11 path = shortest_path(adjacency, start, end) edges = [] for i in range(len(path) - 1): u = path[i] v = path[i + 1] if v not in network.edge[u]: u, v = v, u edges.append([u, v]) plotter = NetworkPlotter(network, figsize=(10, 8), fontsize=6) plotter.draw_vertices( text={key: key for key in network.vertices()}, facecolor={key: '#ff0000'
import random import compas from compas.datastructures import Mesh from compas.topology import shortest_path from compas.utilities import pairwise from compas_plotters import MeshPlotter mesh = Mesh.from_obj(compas.get('faces.obj')) start, goal = random.choices(mesh.vertices_on_boundary(), k=2) vertices = shortest_path(mesh.adjacency, start, goal) vertexcolor = {start: (255, 0, 0)} edgecolor = {} for u, v in pairwise(vertices): vertexcolor[v] = (0, 255, 0) edgecolor[u, v] = (0, 255, 0) edgecolor[v, u] = (0, 255, 0) vertexcolor[goal] = (0, 0, 255) plotter = MeshPlotter(mesh, figsize=(12, 7.5)) plotter.draw_vertices(facecolor=vertexcolor) plotter.draw_faces() plotter.draw_edges(keys=list(pairwise(vertices)), color=edgecolor, width=2.0) plotter.show()