I mentioned above, that you need a decent stitcher. Actually, there is only one: Panorama Tools, the father of the stitchers. PTGui, PT Assembler and Hugin are front-ends to Panorama Tools (PTGui have partly re-coded it).
PT starts out with creating a projection of each frame on the surface of a sphere. This projection is based on the field of view covered by the image, which, of course, depends on the actual focal length, which, in turn, depends on the focusing distance.
When stitching, you have to specify the angular field of view (you don't need to calculate that, the front-ends are doing it). There is a firm correlation between the angle of view of the frames and the matching points between the frames. If you have good (accurate) matching points, you can tell the optimizer (a pre-processor for the stitching) to calculate the actual angle of view from the specified values (which are approximations) and the correlations.
After having projected all frames on the surface of a unity sphere, all frames aligned, a second pass projects the sphere surface on another surface, which depends on the projection method: on a plane (in case of rectilinear projection), on the surface of a cylinder (in case of cylindrical projection), etc.
Consequently, you can make even several shots of the same frame with different focusing; the frames will be slightly different, as the closer focusing yields a larger field of view. All these will be projected together, and you can mask their blendings, as the blending is a separate program step. Alternatively, you can use the relatively new feature of PTA, focus blending.