Efficient calculation of the Green's function for the rectangular cavity