Distance to Viewport View-Camera How can I get the distance between specific points in the 3D View and the virtual camera of my viewport?

Using the distance, I could select the closest or furthest (farthest) vertex/object from my perspective view.



Answering my own question, although I still don't know the difference between perspective_matrix, window_matrix and view_matrix. I also would prefer more explanation on why we don't need to use the inverted matrix.)
This question came up while creating this answer.
Snapping an empty to the closest vertex of a cube. "Closest" meaning closest to the viewer.

First, loop through the areas or obtain a SpaceView3D by other means. The type of this area returns VIEW_3D. It's first space will be a VIEW_3D space. This space has a region_3d property, which has a perspective_matrix property.

The perspective_matrix appears to be the perspective view transform matrix. Mutliple a 3D Vector by the matrix, to get its position relativ to the camera.

Here is a working sample.

import bpy

# function returning the closest vertex of 
# param 'ob' is an object in the scene with vertices
def get_closest_vertex_position(scene, ob):
    # get an area of type VIEW_3D
    areas = [a for a in bpy.context.screen.areas if a.type == 'VIEW_3D']
    if not len(areas):
        return

    region3d = areas[0].spaces[0].region_3d

    # get the view matrix
    view_mat_inv = region3d.view_matrix

    if region3d.is_perspective:
        vertices = [[v, (view_mat_inv*v.co).length] for v in ob.data.vertices]
    else:
        vertices = [[v, -(view_mat_inv*v.co).z] for v in ob.data.vertices]

    # use a lamda expression to get closest vertex
    return min(vertices, key = lambda x: x[1])[0].co