Invisibility cloaks with arbitrary geometries for layered and gradually changing backgrounds