# -----------------------------------------------------------------------------
# Hide connected components of a surface with small enclosed volume.
#
# Tested with Chimera version 1.2500.  Will not work with 1.2470 or earlier.
#
def hide_small_blobs(p, volume):

    varray, tarray = p.geometry
    from numpy  import ones, intc
    mask = ones((len(varray),), intc)
    from _surface import connected_pieces
    cplist = connected_pieces(tarray)
    hid = 0
    for vi, ti in cplist:
        ta = tarray.take(ti, axis = 0)
        from MeasureVolume import enclosed_volume
        vol, holes = enclosed_volume(varray, ta)
        if not vol is None and vol < volume:
            mask.put(vi, 0)
            hid += 1
    p.setTriangleMaskFromVertexMask(mask)
    print ('Hid %d of %d connected surface components having volume < %.5g'
           % (hid, len(cplist), volume))

# -----------------------------------------------------------------------------
# Return all surface pieces if none are selected.
#
def selected_surface_pieces():

    import Surface
    plist = Surface.selected_surface_pieces()
    if len(plist) > 0:
        return plist

    from chimera import openModels
    from _surface import SurfaceModel
    plist = []
    for m in openModels.list(modelTypes = [SurfaceModel]):
        plist.extend(m.surfacePieces)
    return plist

# -----------------------------------------------------------------------------
#
volume = 100.0
for p in selected_surface_pieces():
    hide_small_blobs(p, volume)
