Speed up histograms with KD trees