The theory of waveguides and cavities