def fitSphere(coords):
    """
    Fits a sphere to a set of points.

    Extra info is stored in ``actor.info['radius']``, ``actor.info['center']``, ``actor.info['residue']``.

    .. hint:: Example: |fitspheres1.py|_

        |fitspheres2| |fitspheres2.py|_
    """
    coords = np.array(coords)
    n = len(coords)
    A = np.zeros((n, 4))
    A[:, :-1] = coords * 2
    A[:, 3] = 1
    f = np.zeros((n, 1))
    x = coords[:, 0]
    y = coords[:, 1]
    z = coords[:, 2]
    f[:, 0] = x * x + y * y + z * z
    C, residue, rank, sv = np.linalg.lstsq(A, f)  # solve AC=f
    if rank < 4:
        return None
    t = (C[0] * C[0]) + (C[1] * C[1]) + (C[2] * C[2]) + C[3]
    radius = np.sqrt(t)[0]
    center = np.array([C[0][0], C[1][0], C[2][0]])
    if len(residue):
        residue = np.sqrt(residue[0]) / n
    else:
        residue = 0
    s = vs.Sphere(center, radius, c="r", alpha=1).wire(1)
    s.info["radius"] = radius
    s.info["center"] = center
    s.info["residue"] = residue
    return s
def fitSphere ( coords ) : coords = np . array ( coords ) n = len ( coords ) A = np . zeros ( ( n , 4 ) ) A [ : , : - 1 ] = coords * 2 A [ : , 3 ] = 1 f = np . zeros ( ( n , 1 ) ) x = coords [ : , 0 ] y = coords [ : , 1 ] z = coords [ : , 2 ] f [ : , 0 ] = x * x + y * y + z * z C , residue , rank , sv = np . linalg . lstsq ( A , f ) if rank < 4 : return None t = ( C [ 0 ] * C [ 0 ] ) + ( C [ 1 ] * C [ 1 ] ) + ( C [ 2 ] * C [ 2 ] ) + C [ 3 ] radius = np . sqrt ( t ) [ 0 ] center = np . array ( [ C [ 0 ] [ 0 ] , C [ 1 ] [ 0 ] , C [ 2 ] [ 0 ] ] ) if len ( residue ) : residue = np . sqrt ( residue [ 0 ] ) / n else : residue = 0 s = vs . Sphere ( center , radius , c = "r" , alpha = 1 ) . wire ( 1 ) s . info [ "radius" ] = radius s . info [ "center" ] = center s . info [ "residue" ] = residue return s
Fits a sphere to a set of points.

    Extra info is stored in ``actor.info['radius']``, ``actor.info['center']``, ``actor.info['residue']``.

    .. hint:: Example: |fitspheres1.py|_

        |fitspheres2| |fitspheres2.py|_