How to use the compas.utilities.pairwise function in compas

To help you get started, we’ve selected a few compas examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github compas-dev / compas / src / compas / geometry / offset / offset.py View on Github external
The normal of the offset plane.

    Returns
    -------
    offset polyline : list of point
        The XYZ coordinates of the resulting polyline.

    """

    if not is_item_iterable(distance):
        distance = [distance]
    distances = iterable_like(polyline, distance, distance[-1], as_single=False)
    segments = offset_segments(polyline, distances, normal)

    offset = [segments[0][0]]
    for s1, s2 in pairwise(segments):
        point = intersect(s1, s2, tol)
        offset.append(point)
    offset.append(segments[-1][1])

    return offset
github compas-dev / compas / src / compas / datastructures / network / operations / join.py View on Github external
polyline = list(edges_to_visit.pop())

        # get adjacent edges until the polyline is closed...
        while polyline[0] != polyline[-1]:

            # ... or until both end are non-two-valent vertices
            if len(network.vertex_neighbors(polyline[-1])) != 2 or geometric_key(network.vertex_coordinates(polyline[-1])) in stop_geom_keys:
                polyline = list(reversed(polyline))
                if len(network.vertex_neighbors(polyline[-1])) != 2 or geometric_key(network.vertex_coordinates(polyline[-1])) in stop_geom_keys:
                    break

            # add next edge
            polyline.append([nbr for nbr in network.vertex_neighbors(polyline[-1]) if nbr != polyline[-2]][0])

        # delete polyline edges from the list of univisted edges
        for u, v in pairwise(polyline):
            if (u, v) in edges_to_visit:
                edges_to_visit.remove((u, v))
            elif (v, u) in edges_to_visit:
                edges_to_visit.remove((v, u))

        polylines.append(polyline)

    return [[network.vertex_coordinates(vkey) for vkey in polyline] for polyline in polylines]
github compas-dev / compas / src / compas / geometry / size.py View on Github external
.. [1] Nurnberg, R. *Calculating the area and centroid of a polygon in 2d*.
           Available at: http://wwwf.imperial.ac.uk/~rn/centroid.pdf

    """
    xyz, faces = polyhedron

    V = 0
    for vertices in faces:
        if len(vertices) == 3:
            triangles = [vertices]
        else:
            centroid = centroid_points([xyz[i] for i in vertices])
            i = len(xyz)
            xyz.append(centroid)
            triangles = []
            for u, v in pairwise(vertices + vertices[0:1]):
                triangles.append([i, u, v])

        for u, v, w in triangles:
            a = xyz[u]
            b = xyz[v]
            c = xyz[w]
            ab = subtract_vectors(b, a)
            ac = subtract_vectors(c, a)
            n = cross_vectors(ab, ac)
            V += dot_vectors(a, n)
    return V / 6.
github compas-dev / compas / docs / .source / workshops / acadia2017 / day2 / formfinding / formfinding_simple-result.py View on Github external
'start': mesh.vertex_coordinates(u, 'xy'),
            'end'  : mesh.vertex_coordinates(v, 'xy'),
            'color': '#cccccc',
            'width': 0.5 
        })

    plotter.draw_lines(lines)

    # find the cables and modify their force densities

    fixed    = [k for k in mesh.vertices() if mesh.vertex_degree(k) == 2]
    boundary = mesh.vertices_on_boundary(ordered=True)
    corners  = sorted(boundary.index(k) for k in fixed)

    cables = []
    for a, b in pairwise(corners):
        cables.append(boundary[a: b + 1])

    cables.append(boundary[corners[-1]: ] + boundary[: corners[0] + 1])

    for cable in cables:
        for u, v in pairwise(cable):
            mesh.set_edge_attribute((u, v), 'q', 3.0, directed=False)
    
    # preprocess

    k_i  = mesh.key_index()
    i_k  = mesh.index_key()
    uv_i = mesh.uv_index()
    i_uv = mesh.index_uv()

    fixed = [k_i[k] for k in fixed]
github compas-dev / compas / src / compas_viewers / viewer.py View on Github external
for fkey in mesh.faces():
            fvertices = mesh.face_vertices(fkey)
            f = len(fvertices)
            if f < 3:
                pass
            elif f == 3:
                faces.append(fvertices)
            elif f == 4:
                a, b, c, d = fvertices
                faces.append([a, b, c])
                faces.append([c, d, a])
            else:
                o = mesh.face_centroid(fkey)
                v = len(xyz)
                xyz.append(o)
                for a, b in pairwise(fvertices + fvertices[0:1]):
                    faces.append([a, b, v])

        self._xyz = xyz
        self._faces = faces
github compas-dev / compas / src / compas / topology / orientation.py View on Github external
""""""
    points = [centroid_points([xyz[index] for index in face]) for face in faces]

    tree = KDTree(points)
    closest = [tree.nearest_neighbors(point, nmax) for point in points]
    closest = [[index for _, index, _ in nnbrs] for nnbrs in closest]

    adjacency = {}

    for face, vertices in enumerate(faces):
        nbrs = []
        found = set()

        nnbrs = set(closest[face])

        for u, v in pairwise(vertices + vertices[0:1]):
            for nbr in nnbrs:

                if nbr == face:
                    continue
                if nbr in found:
                    continue

                for a, b in pairwise(faces[nbr] + faces[nbr][0:1]):
                    if v == a and u == b:
                        nbrs.append(nbr)
                        found.add(nbr)
                        break

                for a, b in pairwise(faces[nbr] + faces[nbr][0:1]):
                    if u == a and v == b:
                        nbrs.append(nbr)
github compas-dev / compas / src / compas / geometry / algorithms / hull.py View on Github external
from compas.plotters import Plotter
    from compas.utilities import pairwise

    cloud = pointcloud_xy(50, (0, 100), (0, 100))
    hull = convex_hull_xy(cloud)

    points = []
    for a in cloud:
        points.append({
            'pos'       : a,
            'facecolour' : '#0000ff',
            'radius'    : 0.5
        })

    lines = []
    for a, b in pairwise(hull + hull[:1]):
        lines.append({
            'start' : a,
            'end'   : b,
            'colour' : '#ff0000',
            'width' : 2.0
        })

    plotter = Plotter()

    plotter.draw_points(points)
    plotter.draw_lines(lines)

    plotter.show()
github compas-dev / compas / docs / workshops / acadia2017 / day2 / formfinding / formfinding_simple-result.py View on Github external
plotter.draw_lines(lines)

    # find the cables and modify their force densities

    fixed    = [k for k in mesh.vertices() if mesh.vertex_degree(k) == 2]
    boundary = mesh.vertices_on_boundary(ordered=True)
    corners  = sorted(boundary.index(k) for k in fixed)

    cables = []
    for a, b in pairwise(corners):
        cables.append(boundary[a: b + 1])

    cables.append(boundary[corners[-1]: ] + boundary[: corners[0] + 1])

    for cable in cables:
        for u, v in pairwise(cable):
            mesh.set_edge_attribute((u, v), 'q', 3.0, directed=False)
    
    # preprocess

    k_i  = mesh.key_index()
    i_k  = mesh.index_key()
    uv_i = mesh.uv_index()
    i_uv = mesh.index_uv()

    fixed = [k_i[k] for k in fixed]
    xyz   = mesh.get_vertices_attributes('xyz')
    loads = mesh.get_vertices_attributes(('px', 'py', 'pz'))
    edges = mesh.indexed_edges()
    q     = mesh.get_edges_attribute('q')

    # compute equilibrium
github compas-dev / compas / src / compas / plotters / meshplotter.py View on Github external
def highlight_path(self, path, edgecolour=None, edgetext=None, edgewidth=None):
        lines = []
        for u, v in pairwise(path):
            sp = self.mesh.vertex_coordinates(u, 'xy')
            ep = self.mesh.vertex_coordinates(v, 'xy')
            lines.append({
                'start' : sp,
                'end'   : ep,
                'width' : edgewidth or self.defaults.get('edge.width', 2.0),
                'colour' : edgecolour or self.defaults.get('edge.colour', '#ff0000')
            })
        self.draw_lines(lines)
github compas-dev / compas / src / compas / geometry / offset / offset.py View on Github external
def offset_segments(point_list, distances, normal):
    """
    """
    segments = []
    for line, distance in zip(pairwise(point_list), distances):
        segments.append(offset_line(line, distance, normal))
    return segments