Red variables in the OGLE-II data base - III. Constraints on the three-dimensional structures of the Large and Small Magellanic Clouds