<commit_before>"""
Copyright (C) 2013
Author(s): <NAME>

See LICENCE.txt for licensing and contact information.
"""

__author__ = 'matt'

__all__ = ['get_vertices_per_edge', 'get_faces_per_edge', 'get_vert_connectivity']

import zlib
import numpy as np
import scipy.sparse as sp
import cPickle as pickle
from chumpy.utils import row, col


def get_vert_connectivity(mesh_v, mesh_f):
    """Returns a sparse matrix (of size #verts x #verts) where each nonzero
    element indicates a neighborhood relation. For example, if there is a
    nonzero element in position (15,12), that means vertex 15 is connected
    by an edge to vertex 12."""

    vpv = sp.csc_matrix((len(mesh_v),len(mesh_v)))

    # for each column in the faces...
    for i in range(3):
        IS = mesh_f[:,i]
        JS = mesh_f[:,(i+1)%3]
        data = np.ones(len(IS))
        ij = np.vstack((row(IS.flatten()), row(JS.flatten())))
        mtx = sp.csc_matrix((data, ij), shape=vpv.shape)
        vpv = vpv + mtx + mtx.T

    return vpv


def get_vertices_per_edge(mesh_v, mesh_f, faces_per_edge=None):
    """Returns an Ex2 array of adjacencies between vertices, where
    each element in the array is a vertex index. Each edge is included
    only once. If output of get_faces_per_edge is provided, this is used to
    avoid call to get_vert_connectivity()"""

    faces = mesh_f
    suffix = str(zlib.crc32(faces_per_edge.flatten())) if faces_per_edge != None else ''
    cache_fname = '/tmp/verts_per_edge_cache_' + str(zlib.crc32(faces.flatten())) + '_' + suffix + '.pkl'
    try:
        with open(cache_fname, 'rb') as fp:
            return(pickle.load(fp))
    except:
        if faces_per_edge != None :
            result = np.asarray(np.vstack([row(np.intersect1d(mesh_f[k[0]], mesh_f[k[1]])) for k in faces_per_edge]), np.uint32)
        else:
            vc = sp.coo_matrix(get_vert_connectivity(mesh_v, mesh_f))
            result = np.hstack((col(vc.row), col(vc.col)))
            result = result[result[:,0] < result[:,1]] # for uniqueness

        with open(cache_fname, 'wb') as fp:
            pickle.dump(result, fp, -1)
        return result


def get_faces_per_edge(mesh_v, mesh_f):
    faces = mesh_f
    cache_fname = '/tmp/edgecache_new_' + str(zlib.crc32(faces.flatten())) + '.pkl'

    try:
        with open(cache_fname, 'rb') as fp:
            return(pickle.load(fp))
    except:
        f = faces
        IS = np.repeat(np.arange(len(f)), 3)
        JS = f.ravel()
        data = np.ones(IS.size)
        f2v = sp.csc_matrix((data, (IS, JS)), shape=(len(f), np.max(f.ravel())+1))
        f2f = f2v.dot(f2v.T)
        f2f = f2f.tocoo()
        f2f = np.hstack((col(f2f.row), col(f2f.col), col(f2f.data)))
        which = (f2f[:,0] < f2f[:,1]) & (f2f[:,2] >= 2)
        result = np.asarray(f2f[which, :2], np.uint32)

        with open(cache_fname, 'wb') as fp:
            pickle.dump(result, fp, -1)
        return result



def loop_subdivider(mesh_v, mesh_f):

    IS = []
    JS = []
    data = []

    vc = get_vert_connectivity(mesh_v, mesh_f)<commit_msg>topology.py: make faces_per_edge deal with open (not topologically a sphere) meshes<commit_after>"""
Copyright (C) 2013
Author(s): <NAME>

See LICENCE.txt for licensing and contact information.
"""

__author__ = 'matt'

__all__ = ['get_vertices_per_edge', 'get_faces_per_edge', 'get_vert_connectivity']

import zlib
import numpy as np
import scipy.sparse as sp
import cPickle as pickle
from chumpy.utils import row, col


def get_vert_connectivity(mesh_v, mesh_f):
    """Returns a sparse matrix (of size #verts x #verts) where each nonzero
    element indicates a neighborhood relation. For example, if there is a
    nonzero element in position (15,12), that means vertex 15 is connected
    by an edge to vertex 12."""

    vpv = sp.csc_matrix((len(mesh_v),len(mesh_v)))

    # for each column in the faces...
    for i in range(3):
        IS = mesh_f[:,i]
        JS = mesh_f[:,(i+1)%3]
        data = np.ones(len(IS))
        ij = np.vstack((row(IS.flatten()), row(JS.flatten())))
        mtx = sp.csc_matrix((data, ij), shape=vpv.shape)
        vpv = vpv + mtx + mtx.T

    return vpv


def get_vertices_per_edge(mesh_v, mesh_f):
    """Returns an Ex2 array of adjacencies between vertices, where
    each element in the array is a vertex index. Each edge is included
    only once. If output of get_faces_per_edge is provided, this is used to
    avoid call to get_vert_connectivity()"""

    faces = mesh_f
    cache_fname = '/tmp/verts_per_edge_cache_' + str(zlib.crc32(faces.flatten())) + '.pkl'
    try:
        with open(cache_fname, 'rb') as fp:
            return(pickle.load(fp))
    except:
        vc = sp.coo_matrix(get_vert_connectivity(mesh_v, mesh_f))
        result = np.hstack((col(vc.row), col(vc.col)))
        result = result[result[:,0] < result[:,1]] # for uniqueness

        with open(cache_fname, 'wb') as fp:
            pickle.dump(result, fp, -1)
        return result


def get_faces_per_edge(mesh_v, mesh_f, verts_per_edge=None):
    faces = mesh_f
    suffix = str(zlib.crc32(verts_per_edge.flatten())) if verts_per_edge != None else ''
    cache_fname = '/tmp/edgecache_new_' + str(zlib.crc32(faces.flatten())) + '_' + suffix + '.pkl'

    try:
        with open(cache_fname, 'rb') as fp:
            return(pickle.load(fp))
    except:
        if verts_per_edge is None:
            verts_per_edge = get_vertices_per_edge(mesh_v, mesh_f)

        v2f = {i: set([]) for i in range(len(mesh_v))}
        # TODO: cythonize?
        for idx, f in enumerate(mesh_f):
            v2f[f[0]].add(idx)
            v2f[f[1]].add(idx)
            v2f[f[2]].add(idx)

        fpe = -np.ones_like(verts_per_edge)
        for idx, edge in enumerate(verts_per_edge):
            faces = v2f[edge[0]].intersection(v2f[edge[1]])
            for i, f in enumerate(faces):
                fpe[idx,i] = f

        result = fpe
        # f = faces
        # IS = np.repeat(np.arange(len(f)), 3)
        # JS = f.ravel()
        # data = np.ones(IS.size)
        # f2v = sp.csc_matrix((data, (IS, JS)), shape=(len(f), np.max(f.ravel())+1))
        # f2f = f2v.dot(f2v.T)
        # f2f = f2f.tocoo()
        # f2f = np.hstack((col(f2f.row), col(f2f.col), col(f2f.data)))
        # which = (f2f[:,0] < f2f[:,1]) & (f2f[:,2] >= 2)
        # result = np.asarray(f2f[which, :2], np.uint32)

        with open(cache_fname, 'wb') as fp:
            pickle.dump(result, fp, -1)
        return result



def loop_subdivider(mesh_v, mesh_f):

    IS = []
    JS = []
    data = []

    vc = get_vert_connectivity(mesh_v, mesh_f)
{'fraction_non_alphanumeric': 0.13995758860951227, 'fraction_numerical': 0.017267494698576188, 'mean_word_length': 2.7326172979084227, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, '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': '2673580', 'n_tokens_mistral': 2490, 'n_tokens_neox': 2410, 'n_words': 591}