Add clustering functionality

This is a replacement PR of the work done in #2439 (originally #1903).

It includes a clean history of the commits by @kenny806 and @fredj.

We have a CLA from @kenny806.

References #1573