Write me a complete SLAM code in Python that performs real-time 3D mapping from an RGB video input. The code will be able to extract point clouds from the input frames using a depth estimation model, specifically utilizing Metric3D as the preferred depth model.

The SLAM system will function as follows:

Input Handling:

The input will be a video file (e.g., .mp4 format) or a camera stream. The code should be flexible to accept either type of input.
The user will provide the path to the input video or the camera ID to use for live feed.
Depth Model Integration:

The depth model should be pre-trained and pulled directly from the PyTorch Hub. Make sure the model is integrated efficiently to process each frame and generate depth information.
The Metric3D depth model will be used for generating the depth map from the RGB input.
The code should download the model automatically if it's not already available and ensure that it's loaded correctly for inference. like following snippet

self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
        self.metric3d_model = torch.hub.load('yvanyin/metric3d', 'metric3d_vit_large', pretrain=True)
        self.metric3d_model.to(self.device)
        self.metric3d_model.eval()


Calibration Matrix:

The code will read a pre-saved camera calibration matrix (intrinsic matrix) that will be used for mapping 2D image points to 3D world coordinates. The format of the matrix will be specified as follows:
Keys in the npz file: ['Camera_matrix', 'distCoeff', 'RotationalV', 'TranslationV']
Content of Camera_matrix:
[[726.65937849   0.         419.37380341]
 [  0.         726.16188065 249.3021722 ]
 [  0.           0.           1.        ]]
Content of distCoeff:
[[ 1.69558413e-01 -6.27337858e-01 -1.37015088e-03  7.96379361e-04
   8.22763173e-01]]
Content of RotationalV:
[[[ 0.0796394 ]
  [ 0.43195933]
  [-0.07977802]]

The calibration matrix will be loaded from a .npz file.
If the user does not provide a calibration matrix, the code will have a default matrix or prompt the user to provide one.
Pose Estimation:

The system will incorporate a pose estimation model to track the camera's movement through space.
The estimated poses will be combined with the depth information to correctly place each point cloud in the global 3D space, thus enabling the construction of a cohesive map.
Use a pre-trained pose estimator that’s optimized for real-time performance, potentially one from PyTorch or another suitable library.
Frame Interval Control:

The code will ask the user for a frame interval (i.e., how often to process frames from the video). For example, if the interval is set to 5, every fifth frame will be processed for depth and pose estimation.
This feature allows the user to control the trade-off between speed and accuracy.
Point Cloud Generation:

The depth data will be converted into 3D point clouds using the depth map and camera poses. Each frame's point cloud will be stored and combined into a global point cloud.
The code should automatically align point clouds from consecutive frames, ensuring that the final map is a single unified point cloud and not disjointed segments.
PCD Output:

The final point cloud will be saved in .pcd (Point Cloud Data) format for use in visualization tools such as Open3D or CloudCompare.
The code will provide an option to visualize the real-time mapping progress using Open3D, allowing users to observe the building of the map frame by frame.
Implementation:

The code will be structured in a modular way with clear sections for input handling, depth estimation, pose estimation, point cloud generation, and final output.
Ensure the code is well-documented, with comments explaining the purpose of each section and how it works.
Include error handling, e.g., for missing input files, invalid matrix formats, or model download failures.
Write all the necessary code in Python and ensure that it is easily executable. Provide the full code block without missing any details. The code should be structured so that it can be easily copied and run in a Python environment.