Unified theory of ion optics