forked from pierotofy/OpenSplat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolmap.cpp
More file actions
193 lines (151 loc) · 6.58 KB
/
colmap.cpp
File metadata and controls
193 lines (151 loc) · 6.58 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include <filesystem>
#include <vector>
#include <unordered_map>
#include <iostream>
#include "colmap.hpp"
#include "point_io.hpp"
#include "tensor_math.hpp"
namespace fs = std::filesystem;
using namespace torch::indexing;
namespace cm{
static fs::path findColmapRoot(const fs::path &projectRoot){
// Priority 1: cameras.bin in project root
if (fs::exists(projectRoot / "cameras.bin")) return projectRoot;
// Priority 2: pick subdir in sparse/ with largest cameras.bin
fs::path sparseRoot = projectRoot / "sparse";
std::uintmax_t bestSize = 0;
fs::path bestDir;
if (fs::exists(sparseRoot) && fs::is_directory(sparseRoot)){
for (const auto &entry : fs::directory_iterator(sparseRoot)){
if (!entry.is_directory()) continue;
fs::path camPath = entry.path() / "cameras.bin";
if (!fs::exists(camPath)) continue;
std::uintmax_t sz = fs::file_size(camPath);
if (sz > bestSize){
bestSize = sz;
bestDir = entry.path();
}
}
}
if (bestSize > 0){
std::cout << "[COLMAP] Using reconstruction in: " << bestDir << std::endl;
return bestDir;
}
// Fallback to original project root (error will be thrown later if files still missing)
return projectRoot;
}
InputData inputDataFromColmap(const std::string &projectRoot,
const std::string &colmapImageSourcePath){
InputData ret;
fs::path cmRoot = findColmapRoot(projectRoot);
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;
}
if (colmapImageSourcePath.empty())
cam.filePath = (fs::path(projectRoot) / "images" / filePath).string();
else
cam.filePath = (fs::path(colmapImageSourcePath) / 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();
torch::Tensor poses = unorientedPoses;
ret.translation = torch::zeros({3});
ret.scale = 1.0f;
// Log chosen normalization
std::cout << "[COLMAP] Applied normalization – translation: " << ret.translation << ", scale: " << ret.scale << std::endl;
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.points.rgb = pSet->colorsTensor().clone();
RELEASE_POINTSET(pSet);
return ret;
}
}