Topological control of the structures of molecules and solids