A versatile and highly efficient algorithm to generate representative microstructures for heterogeneous materials