-
Notifications
You must be signed in to change notification settings - Fork 86
/
colmap.cpp
157 lines (121 loc) · 5.36 KB
/
colmap.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <filesystem>
#include "colmap.hpp"
#include "point_io.hpp"
#include "tensor_math.hpp"
namespace fs = std::filesystem;
using namespace torch::indexing;
namespace cm{
InputData inputDataFromColmap(const std::string &projectRoot){
InputData ret;
fs::path cmRoot(projectRoot);
if (!fs::exists(cmRoot / "cameras.bin") && fs::exists(cmRoot / "sparse" / "0" / "cameras.bin")){
cmRoot = cmRoot / "sparse" / "0";
}
fs::path camerasPath = cmRoot / "cameras.bin";
fs::path imagesPath = cmRoot / "images.bin";
fs::path pointsPath = cmRoot / "points3D.bin";
if (!fs::exists(camerasPath)) throw std::runtime_error(camerasPath.string() + " does not exist");
if (!fs::exists(imagesPath)) throw std::runtime_error(imagesPath.string() + " does not exist");
if (!fs::exists(pointsPath)) throw std::runtime_error(pointsPath.string() + " does not exist");
std::ifstream camf(camerasPath.string(), std::ios::binary);
if (!camf.is_open()) throw std::runtime_error("Cannot open " + camerasPath.string());
std::ifstream imgf(imagesPath.string(), std::ios::binary);
if (!imgf.is_open()) throw std::runtime_error("Cannot open " + imagesPath.string());
size_t numCameras = readBinary<uint64_t>(camf);
std::vector<Camera> cameras(numCameras);
std::unordered_map<uint32_t, Camera *> camMap;
for (size_t i = 0; i < numCameras; i++) {
Camera *cam = &cameras[i];
cam->id = readBinary<uint32_t>(camf);
CameraModel model = static_cast<CameraModel>(readBinary<int>(camf)); // model ID
cam->width = readBinary<uint64_t>(camf);
cam->height = readBinary<uint64_t>(camf);
if (model == SimplePinhole){
cam->fx = readBinary<double>(camf);
cam->fy = cam->fx;
cam->cx = readBinary<double>(camf);
cam->cy = readBinary<double>(camf);
}else if (model == Pinhole){
cam->fx = readBinary<double>(camf);
cam->fy = readBinary<double>(camf);
cam->cx = readBinary<double>(camf);
cam->cy = readBinary<double>(camf);
}else if (model == SimpleRadial){
cam->fx = readBinary<double>(camf);
cam->fy = cam->fx;
cam->cx = readBinary<double>(camf);
cam->cy = readBinary<double>(camf);
cam->k1 = readBinary<double>(camf);
}else if (model == OpenCV){
cam->fx = readBinary<double>(camf);
cam->fy = readBinary<double>(camf);
cam->cx = readBinary<double>(camf);
cam->cy = readBinary<double>(camf);
cam->k1 = readBinary<double>(camf);
cam->k2 = readBinary<double>(camf);
cam->p1 = readBinary<double>(camf);
cam->p2 = readBinary<double>(camf);
}else{
throw std::runtime_error("Unsupported camera model: " + std::to_string(model));
}
camMap[cam->id] = cam;
}
camf.close();
size_t numImages = readBinary<uint64_t>(imgf);
torch::Tensor unorientedPoses = torch::zeros({static_cast<long int>(numImages), 4, 4}, torch::kFloat32);
for (size_t i = 0; i < numImages; i++){
readBinary<uint32_t>(imgf); // imageId
torch::Tensor qVec = torch::tensor({
readBinary<double>(imgf),
readBinary<double>(imgf),
readBinary<double>(imgf),
readBinary<double>(imgf)
}, torch::kFloat32);
torch::Tensor R = quatToRotMat(qVec);
torch::Tensor T = torch::tensor({
{ readBinary<double>(imgf) },
{ readBinary<double>(imgf) },
{ readBinary<double>(imgf) }
}, torch::kFloat32);
torch::Tensor Rinv = R.transpose(0, 1);
torch::Tensor Tinv = torch::matmul(-Rinv, T);
uint32_t camId = readBinary<uint32_t>(imgf);
Camera cam = *camMap[camId];
char ch = '\0';
std::string filePath = "";
while(true){
imgf.read(&ch, 1);
if (ch == '\0') break;
filePath += ch;
}
// TODO: should "images" be an option?
cam.filePath = (fs::path(projectRoot) / "images" / filePath).string();
unorientedPoses[i].index_put_({Slice(None, 3), Slice(None, 3)}, Rinv);
unorientedPoses[i].index_put_({Slice(None, 3), Slice(3, 4)}, Tinv);
unorientedPoses[i][3][3] = 1.0f;
// Convert COLMAP's camera CRS (OpenCV) to OpenGL
unorientedPoses[i].index_put_({Slice(0, 3), Slice(1,3)}, unorientedPoses[i].index({Slice(0, 3), Slice(1,3)}) * -1.0f);
size_t numPoints2D = readBinary<uint64_t>(imgf);
for (size_t j = 0; j < numPoints2D; j++){
readBinary<double>(imgf); // x
readBinary<double>(imgf); // y
readBinary<uint64_t>(imgf); // point3D ID
}
ret.cameras.push_back(cam);
}
imgf.close();
auto r = autoScaleAndCenterPoses(unorientedPoses);
torch::Tensor poses = std::get<0>(r);
ret.translation = std::get<1>(r);
ret.scale = std::get<2>(r);
for (size_t i = 0; i < ret.cameras.size(); i++){
ret.cameras[i].camToWorld = poses[i];
}
PointSet *pSet = readPointSet(pointsPath.string());
torch::Tensor points = pSet->pointsTensor().clone();
ret.points.xyz = (points - ret.translation) * ret.scale;
ret.points.rgb = pSet->colorsTensor().clone();
RELEASE_POINTSET(pSet);
return ret;
}
}