polyCrystal.py 文件源码

python
阅读 26 收藏 0 点赞 0 评论 0

项目:Graphene 作者: ashivni 项目源码 文件源码
def vorEdges(vor, far):
    """
    Given a voronoi tesselation, retuns the set of voronoi edges.
    far is the length of the "infinity" edges
    """
    edges = []
    for simplex in vor.ridge_vertices:
        simplex = numpy.asarray(simplex)
        if numpy.all(simplex >= 0):
            edge = {}
            edge['p1'], edge['p2'] = vor.vertices[simplex, 0], vor.vertices[simplex, 1]
            edge['p1'] = numpy.array([vor.vertices[simplex, 0][0], vor.vertices[simplex, 1][0]])
            edge['p2'] = numpy.array([vor.vertices[simplex, 0][1], vor.vertices[simplex, 1][1]])
            edge['t'] = (edge['p2'] - edge['p1']) / numpy.linalg.norm(edge['p2'] - edge['p1'])
            edges.append(edge)

    ptp_bound = vor.points.ptp(axis=0)
    center = vor.points.mean(axis=0)
    for pointidx, simplex in zip(vor.ridge_points, vor.ridge_vertices):
        simplex = numpy.asarray(simplex)
        if numpy.any(simplex < 0):
            i = simplex[simplex >= 0][0]  # finite end Voronoi vertex

            t = vor.points[pointidx[1]] - vor.points[pointidx[0]]  # tangent
            t /= numpy.linalg.norm(t)
            n = numpy.array([-t[1], t[0]])  # normal

            midpoint = vor.points[pointidx].mean(axis=0)
            direction = numpy.sign(numpy.dot(midpoint - center, n)) * n

            far_point = vor.vertices[i] + direction * ptp_bound.max() * far
            edge = {}
            edge['p1'], edge['p2'] = numpy.array([vor.vertices[i, 0], far_point[0]]), numpy.array(
                [vor.vertices[i, 1], far_point[1]])
            edge['p1'], edge['p2'] = vor.vertices[i, :], far_point
            edge['t'] = (edge['p2'] - edge['p1']) / numpy.linalg.norm(edge['p2'] - edge['p1'])
            edges.append(edge)
    return edges
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号