A Tutorial on Distance Metric Learning: Mathematical Foundations, Algorithms and Software