Common framework for calculating distance matrices
