-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtexture_mapping.cpp
187 lines (166 loc) · 4.51 KB
/
texture_mapping.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
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
//
// Created by Harold on 2021/11/15.
//
#include "m_texture_mapping.hpp"
#include "../utils/stopwatch.h"
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <iostream>
#include <fstream>
using namespace M_MATH;
using namespace texture_mapping;
std::ifstream& go_to_line(std::ifstream& file, unsigned int num) {
file.seekg(std::ios::beg);
for (int i = 0; i < num - 1; ++i) {
file.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
return (file);
}
bool read_cam_pose_file(std::string filename,
Camera& cam) {
std::ifstream myReadFile;
myReadFile.open(filename.c_str(), std::ios::in);
if (!myReadFile.is_open()) {
printf("Error opening file %s\n", filename.c_str());
return false;
}
myReadFile.seekg(std::ios::beg);
double val;
go_to_line(myReadFile, 1);
myReadFile >> val;
std::cout << ("t ");
std::cout << val;
cam.pose(0, 3) = val; // TX
myReadFile >> val;
std::cout << (" ");
std::cout << val;
cam.pose(1, 3) = val; // TY
myReadFile >> val;
std::cout << (" ");
std::cout << val;
std::cout << ("\n");
cam.pose(2, 3) = val; // TZ
go_to_line(myReadFile, 2);
myReadFile >> val;
std::cout << ("r1 ");
std::cout << val;
cam.pose(0, 0) = val;
myReadFile >> val;
std::cout << (" ");
std::cout << val;
cam.pose(0, 1) = val;
myReadFile >> val;
std::cout << (" ");
std::cout << val;
std::cout << ("\n");
cam.pose(0, 2) = val;
go_to_line(myReadFile, 3);
myReadFile >> val;
std::cout << ("r2 ");
std::cout << val;
cam.pose(1, 0) = val;
myReadFile >> val;
std::cout << (" ");
std::cout << val;
cam.pose(1, 1) = val;
myReadFile >> val;
std::cout << (" ");
std::cout << val;
std::cout << ("\n");
cam.pose(1, 2) = val;
go_to_line(myReadFile, 4);
myReadFile >> val;
std::cout << ("r3");
std::cout << val;
cam.pose(2, 0) = val;
myReadFile >> val;
std::cout << (" ");
std::cout << val;
cam.pose(2, 1) = val;
myReadFile >> val;
std::cout << (" ");
std::cout << val;
std::cout << ("\n");
cam.pose(2, 2) = val;
cam.pose(3, 0) = 0.0;
cam.pose(3, 1) = 0.0;
cam.pose(3, 2) = 0.0;
cam.pose(3, 3) = 1.0; // Scale
go_to_line(myReadFile, 5);
myReadFile >> val;
std::cout << ("fx ");
std::cout << val;
std::cout << ("\n");
cam.focal_length_w = val;
go_to_line(myReadFile, 6);
myReadFile >> val;
std::cout << ("fy ");
std::cout << val;
std::cout << ("\n");
cam.focal_length_h = val;
go_to_line(myReadFile, 7);
myReadFile >> val;
std::cout << ("cx ");
std::cout << val;
std::cout << ("\n");
cam.center_w = val;
go_to_line(myReadFile, 8);
myReadFile >> val;
std::cout << ("cy ");
std::cout << val;
std::cout << ("\n");
cam.center_h = val;
go_to_line(myReadFile, 9);
myReadFile >> val;
std::cout << ("h ");
std::cout << val;
std::cout << ("\n");
cam.height = val;
go_to_line(myReadFile, 10);
myReadFile >> val;
std::cout << ("w ");
std::cout << val;
std::cout << ("\n");
cam.width = val;
// close file
myReadFile.close();
return true;
}
std::shared_ptr<open3d::geometry::Image> img_cv2o3d(std::string const& path)
{
auto cv_img = cv::imread(path, cv::IMREAD_COLOR);
auto o3d_img = std::make_shared<open3d::geometry::Image>();
int bytes_per_channel = (cv_img.depth() / 2 + 1);
o3d_img->Prepare(cv_img.cols, cv_img.rows, cv_img.channels(), bytes_per_channel);
std::memcpy(o3d_img->data_.data(), cv_img.data, cv_img.total() * cv_img.channels() * bytes_per_channel);
return o3d_img;
}
// .\m_texture_mapping.exe un_textured.obj camera.txt camera_img.png occluded.jpg
int main(int argc, char* argv[])
{
std::shared_ptr<open3d::geometry::TriangleMesh> mesh;
{
TIME_BLOCK("- load mesh: ");
mesh = open3d::io::CreateMeshFromFile(argv[1], false);
}
open3d::visualization::DrawGeometries({ mesh }, "original mesh", 1920, 1080);
CameraVector cameras;
Camera camera;
read_cam_pose_file(argv[2], camera);
camera.texture_file = argv[3];
cameras.push_back(camera);
// add material
auto texture_img = img_cv2o3d(camera.texture_file);
mesh->textures_ = std::vector<open3d::geometry::Image>({*texture_img});
if (argc >= 5)
{
auto occluded_img = img_cv2o3d(argv[4]);
mesh->textures_.push_back(*occluded_img);
}
{
TIME_BLOCK("- texture mapping: ");
TextureMeshWithMultipleCameras(mesh, cameras);
}
open3d::visualization::DrawGeometries({ mesh }, "textured mesh", 1920, 1080);
return 0;
}