Electromagnetic waveguides and cavities