<commit_before>from __future__ import print_function
from __future__ import absolute_import
from __future__ import division

from math import pi
from compas.utilities import flatten

__all__ = ['mesh_dual']


PI2 = 2.0 * pi


def mesh_dual(mesh, cls=None, boundary=0):
    """Construct the dual of a mesh.

    Parameters
    ----------
    mesh : :class:`~compas.datastructures.Mesh`
        A mesh object.
    cls : Type[:class:`~compas.datastructures.Mesh`], optional
        The type of the dual mesh.
        Defaults to the type of the provided mesh object.
    boundary: float, optional
        boundary mode for the dual mesh
        Default mode is 0, not create faces on boundaries.
        1, create faces on mesh edges, not include original mesh boundary vertices.
        2. create faces on mesh edges and include original mesh boundary vertices on the corner.
        3. create faces on mesh edges and include all original mesh boundary vertices.

    Returns
    -------
    :class:`~compas.datastructures.Mesh`
        The dual mesh object.

    Examples
    --------
    >>> import compas
    >>> from compas.datastructures import Mesh
    >>> mesh = Mesh.from_obj(compas.get('faces.obj'))
    >>> mesh.delete_face(11)
    >>> mesh.delete_face(6)
    >>> mesh.delete_face(7)
    >>> mesh.quads_to_triangles()
    >>> mesh = mesh.subdivide('corner')
    >>> dual = mesh.dual(boundary=3)

    """
    if not cls:
        cls = type(mesh)

    dual = cls()

    mesh.unify_cycles()
    mesh.flip_cycles()

    face_centroid = {face: mesh.face_centroid(face) for face in mesh.faces()}
    outer = list(flatten(mesh.vertices_on_boundaries()))
    vertex_xyz = {}
    face_vertices = {}
    edge_vertex = {}

    # safe guarded if face index is arbitrary, random, not starting from 0
    num_faces = max(max(list(mesh.faces())) + 1, mesh.number_of_faces())
    for vertex in mesh.vertices():
        faces = mesh.vertex_faces(vertex, ordered=True)
        for face in faces:
            if face not in vertex_xyz:
                vertex_xyz[face] = face_centroid[face]
        face_vertices[vertex] = faces

        if not boundary:
            continue

        if boundary > 3:
            raise ValueError("edge mode from 0 to 3!")

        if vertex not in outer or len(faces) < 1:
            continue<commit_msg>remove potentially expensive unify_cycles operation<commit_after>from __future__ import print_function
from __future__ import absolute_import
from __future__ import division

from math import pi
from compas.utilities import flatten

__all__ = ['mesh_dual']


PI2 = 2.0 * pi


def mesh_dual(mesh, cls=None, boundary=0):
    """Construct the dual of a mesh.

    Parameters
    ----------
    mesh : :class:`~compas.datastructures.Mesh`
        A mesh object.
    cls : Type[:class:`~compas.datastructures.Mesh`], optional
        The type of the dual mesh.
        Defaults to the type of the provided mesh object.
    boundary: float, optional
        boundary mode for the dual mesh
        Default mode is 0, not create faces on boundaries.
        1, create faces on mesh edges, not include original mesh boundary vertices.
        2. create faces on mesh edges and include original mesh boundary vertices on the corner.
        3. create faces on mesh edges and include all original mesh boundary vertices.

    Returns
    -------
    :class:`~compas.datastructures.Mesh`
        The dual mesh object.

    Examples
    --------
    >>> import compas
    >>> from compas.datastructures import Mesh
    >>> mesh = Mesh.from_obj(compas.get('faces.obj'))
    >>> mesh.delete_face(11)
    >>> mesh.delete_face(6)
    >>> mesh.delete_face(7)
    >>> mesh.quads_to_triangles()
    >>> mesh = mesh.subdivide('corner')
    >>> dual = mesh.dual(boundary=3)

    """
    if not cls:
        cls = type(mesh)

    dual = cls()

    mesh.flip_cycles()

    face_centroid = {face: mesh.face_centroid(face) for face in mesh.faces()}
    outer = list(flatten(mesh.vertices_on_boundaries()))
    vertex_xyz = {}
    face_vertices = {}
    edge_vertex = {}

    # safe guarded if face index is arbitrary, random, not starting from 0
    num_faces = max(max(list(mesh.faces())) + 1, mesh.number_of_faces())
    for vertex in mesh.vertices():
        faces = mesh.vertex_faces(vertex, ordered=True)
        for face in faces:
            if face not in vertex_xyz:
                vertex_xyz[face] = face_centroid[face]
        face_vertices[vertex] = faces

        if not boundary:
            continue

        if boundary > 3:
            raise ValueError("edge mode from 0 to 3!")

        if vertex not in outer or len(faces) < 1:
            continue
{'fraction_non_alphanumeric': 0.1033818493150685, 'fraction_numerical': 0.008133561643835616, 'mean_word_length': 2.5321239606953894, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5438883', 'n_tokens_mistral': 1401, 'n_tokens_neox': 1330, 'n_words': 442}