ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
camera_rig.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 #include <unordered_map>
11 #include <vector>
12 
13 #include "base/camera.h"
14 #include "base/pose.h"
15 #include "base/reconstruction.h"
16 #include "util/alignment.h"
17 #include "util/types.h"
18 
19 namespace colmap {
20 
21 // This class holds information about the relative configuration of camera rigs.
22 // Camera rigs are composed of multiple cameras with a rigid relative extrinsic
23 // configuration over multiple snapshots. Snapshots are defined as the
24 // collection of images captured simultaneously by all cameras in the rig.
25 class CameraRig {
26 public:
27  CameraRig();
28 
29  // The number of cameras in the rig.
30  size_t NumCameras() const;
31 
32  // The number of snapshots captured by this rig.
33  size_t NumSnapshots() const;
34 
35  // Check whether the given camera is part of the rig.
36  bool HasCamera(const camera_t camera_id) const;
37 
38  // Access the reference camera.
39  camera_t RefCameraId() const;
40  void SetRefCameraId(const camera_t camera_id);
41 
42  // Get the identifiers of the cameras in the rig.
43  std::vector<camera_t> GetCameraIds() const;
44 
45  // Get the snapshots of the camera rig.
46  const std::vector<std::vector<image_t>>& Snapshots() const;
47 
48  // Add a new camera to the rig. The relative pose may contain dummy values
49  // and can then be computed automatically from a given reconstruction using
50  // the method `ComputeRelativePoses`.
51  void AddCamera(const camera_t camera_id,
52  const Eigen::Vector4d& rel_qvec,
53  const Eigen::Vector3d& rel_tvec);
54 
55  // Add the images of a single snapshot to rig. A snapshot consists of the
56  // captured images of all cameras of the rig. All images of a snapshot share
57  // the same global camera rig pose, i.e. all images in the camera rig are
58  // captured simultaneously.
59  void AddSnapshot(const std::vector<image_t>& image_ids);
60 
61  // Check whether the camera rig setup is valid.
62  void Check(const Reconstruction& reconstruction) const;
63 
64  // Get the relative poses of the cameras in the rig.
65  Eigen::Vector4d& RelativeQvec(const camera_t camera_id);
66  const Eigen::Vector4d& RelativeQvec(const camera_t camera_id) const;
67  Eigen::Vector3d& RelativeTvec(const camera_t camera_id);
68  const Eigen::Vector3d& RelativeTvec(const camera_t camera_id) const;
69 
70  // Compute the scaling factor from the reconstruction to the camera rig
71  // dimensions by averaging over the distances between the projection
72  // centers. Note that this assumes that there is at least one camera pair in
73  // the rig with non-zero baseline, otherwise the function returns NaN.
74  double ComputeScale(const Reconstruction& reconstruction) const;
75 
76  // Compute the relative poses in the rig from the reconstruction by
77  // averaging the relative poses over all snapshots. The pose of the
78  // reference camera will be the identity transformation. This assumes that
79  // the camera rig has snapshots that are registered in the reconstruction.
80  bool ComputeRelativePoses(const Reconstruction& reconstruction);
81 
82  // Compute the absolute camera pose of the rig. The absolute camera pose of
83  // the rig is computed as the average of all relative camera poses in the
84  // rig and their corresponding image poses in the reconstruction.
85  void ComputeAbsolutePose(const size_t snapshot_idx,
86  const Reconstruction& reconstruction,
87  Eigen::Vector4d* abs_qvec,
88  Eigen::Vector3d* abs_tvec) const;
89 
90 private:
91  struct RigCamera {
92  Eigen::Vector4d rel_qvec = ComposeIdentityQuaternion();
93  Eigen::Vector3d rel_tvec = Eigen::Vector3d(0, 0, 0);
94  };
95 
96  camera_t ref_camera_id_ = kInvalidCameraId;
97  std::unordered_map<camera_t, RigCamera> rig_cameras_;
98  std::vector<std::vector<image_t>> snapshots_;
99 };
100 
101 } // namespace colmap
void SetRefCameraId(const camera_t camera_id)
Definition: camera_rig.cc:50
camera_t RefCameraId() const
Definition: camera_rig.cc:48
void Check(const Reconstruction &reconstruction) const
Definition: camera_rig.cc:86
void AddCamera(const camera_t camera_id, const Eigen::Vector4d &rel_qvec, const Eigen::Vector3d &rel_tvec)
Definition: camera_rig.cc:68
bool HasCamera(const camera_t camera_id) const
Definition: camera_rig.cc:44
size_t NumSnapshots() const
Definition: camera_rig.cc:42
Eigen::Vector3d & RelativeTvec(const camera_t camera_id)
Definition: camera_rig.cc:120
void AddSnapshot(const std::vector< image_t > &image_ids)
Definition: camera_rig.cc:79
size_t NumCameras() const
Definition: camera_rig.cc:40
bool ComputeRelativePoses(const Reconstruction &reconstruction)
Definition: camera_rig.cc:167
std::vector< camera_t > GetCameraIds() const
Definition: camera_rig.cc:55
const std::vector< std::vector< image_t > > & Snapshots() const
Definition: camera_rig.cc:64
Eigen::Vector4d & RelativeQvec(const camera_t camera_id)
Definition: camera_rig.cc:112
void ComputeAbsolutePose(const size_t snapshot_idx, const Reconstruction &reconstruction, Eigen::Vector4d *abs_qvec, Eigen::Vector3d *abs_tvec) const
Definition: camera_rig.cc:230
double ComputeScale(const Reconstruction &reconstruction) const
Definition: camera_rig.cc:128
Eigen::Vector4d ComposeIdentityQuaternion()
Definition: pose.h:209
const camera_t kInvalidCameraId
Definition: types.h:75
uint32_t camera_t
Definition: types.h:58