A Morse theory for massive particles and photons in general relativity