-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleObjWriter.cpp
More file actions
212 lines (185 loc) · 6.22 KB
/
SimpleObjWriter.cpp
File metadata and controls
212 lines (185 loc) · 6.22 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
//
// Created by ale on 19-3-12.
//
#include <opencv2/opencv.hpp>
#include <vector>
#include <iostream>
#include <ostream>
using namespace std;
using namespace cv;
int writeToSimpleObj(const Mat& depth, const vector<Point>& triangles, const float& f,
const string& filePath, const string& fileName = "temp", const string& texFileName = "texture.jpg")
{
if(depth.empty())
return -1;
if(triangles.empty())
return -2;
string objFileName(filePath), mtlFileName(filePath);
objFileName.append(fileName).append(".obj");
mtlFileName.append(fileName).append(".mtl");
FILE *objFileStream, *mtlFileStream;;
objFileStream = fopen(objFileName.c_str(), "w");
if(objFileStream == nullptr)
{
printf("无法打开obj文件! \n" );
return -3;
}
mtlFileStream=fopen(mtlFileName.c_str(),"w");
if(mtlFileStream == nullptr)
{
printf("无法打开mtl文件! \n" );
return -4;
}
fprintf(objFileStream, "# SimpleObjWriter by Alegriabaile\n\n");
fprintf(objFileStream, "mtllib %s\n\n",mtlFileName.c_str());
//fout vertices
float cx = (float)(depth.cols)/2;
float cy = (float)(depth.rows)/2;
for(int i=0; i<triangles.size(); ++i)
{
Point pt = triangles[i];
Point3f p3f;
p3f.z = depth.at<float>(pt.y, pt.x);
p3f.x = p3f.z*(pt.x-cx)/f;
p3f.y = p3f.z*(pt.y-cy)/f;
p3f = p3f/1000.0f;
fprintf(objFileStream, "v %.4f %.4f %.4f\n", p3f.x, p3f.y, p3f.z);
}
//fout texture uvs
fprintf(objFileStream, "\n");
float u, v;
for(int i=0; i<triangles.size(); ++i)
{
Point pt = triangles[i];
u = pt.x / (cx*2 - 1);
v = 1 - pt.y / (cy*2 - 1);
fprintf(objFileStream, "vt %.4f %.4f\n", u, v);
}
fprintf(objFileStream, "\n");
//fout face indices
string mtlName("texture_para");
fprintf(objFileStream, "usemtl %s\n\n", mtlName.c_str());
for(int i=0; i<triangles.size()/3; ++i)
{
fprintf(objFileStream, "f %d/%d %d/%d %d/%d\n",
i*3+1, i*3+1, i*3+2, i*3+2, i*3+3, i*3+3);
}
///////////////////////////fout .mtl file///////////////////////////
fprintf(mtlFileStream, "# SimpleObjWriter by Alegriabaile\n\n");
float Tr = 0.0;
Vec3f Tf = Vec3f(1.0, 1.0, 1.0);
Vec3f Ka(1.0, 1.0, 1.0), Kd(1.0, 1.0, 1.0);
fprintf(mtlFileStream, "newmtl %s\n", mtlName.c_str());
fprintf(mtlFileStream, " Tr %.4f\n", Tr);
fprintf(mtlFileStream, " Tf %.4f %.4f %.4f\n", Tf[0], Tf[1], Tf[2]);
fprintf(mtlFileStream, " Ka %.4f %.4f %.4f\n", Ka[0], Ka[1], Ka[2]);
fprintf(mtlFileStream, " Kd %.4f %.4f %.4f\n", Kd[0], Kd[1], Kd[2]);
fprintf(mtlFileStream, " map_Ka %s\n", texFileName.c_str());
fprintf(mtlFileStream, " mat_Kd %s\n", texFileName.c_str());
fclose(objFileStream);
fclose(mtlFileStream);
/*
ofstream objOfstream, mtlOfstream;
objOfstream.open(objFileName, ios_base::out|ios_base::trunc);
mtlOfstream.open(mtlFileName, ios_base::out|ios_base::trunc);
if(!objOfstream.is_open())
{
cout<<"无法打开obj文件"<<endl;
return -3;
}
if(!mtlOfstream.is_open())
{
cout<<"无法打开mtl文件"<<endl;
return -4;
}
///////////////////////////fout .obj file///////////////////////////
objOfstream<<"# SimpleObjWriter by Alegriabaile\n\n";
objOfstream<<"mtllib "<<mtlFileName<<"\n\n";
//fout vertices
float cx = (float)(depth.cols)/2;
float cy = (float)(depth.rows)/2;
for(int i=0; i<triangles.size(); ++i)
{
Point pt = triangles[i];
Point3f p3f;
p3f.z = depth.at<float>(pt.y, pt.x);
p3f.x = p3f.z*(pt.x-cx)/f;
p3f.y = p3f.z*(pt.y-cy)/f;
p3f = p3f/1000.0f;
objOfstream<<"v "<<p3f.x<<" "<<p3f.y<<" "<<p3f.z<<"\n";
}
//fout texture uvs
objOfstream<<"\n";
float u, v;
for(int i=0; i<triangles.size(); ++i)
{
Point pt = triangles[i];
u = pt.x / (cx*2 - 1);
v = 1 - pt.y / (cy*2 - 1);
objOfstream<<"vt "<<u<<" "<<v<<"\n";
}
//fout face indices
objOfstream<<"\n";
objOfstream<<"g my_pano\n";
string mtlName("texture_para");
objOfstream<<"usemtl "<<mtlName<<"\n"
<<"s 2"<<"\n";
for(int i=0; i<triangles.size()/3; ++i)
{
objOfstream<<"f "<<i*3+1<<"/"<<i*3+1<<" "
<<i*3+2<<"/"<<i*3+2<<" "<<i*3+3<<"/"<<i*3+3<<" \n";
}
///////////////////////////fout .mtl file///////////////////////////
mtlOfstream<<"# SimpleObjWriter by Alegriabaile\n\n";
float Tr = 0.0;
Vec3f Tf = Vec3f(1.0, 1.0, 1.0);
int illum = 2;
Vec3f Ka(1.0, 1.0, 1.0), Kd(1.0, 1.0, 1.0);
mtlOfstream<<"newmtl "<<mtlName<<"\n"
<<" Tr "<<Tr<<"\n"
<<" Tf "<<Tf[0]<<" "<<Tf[1]<<" "<<Tf[2]<<"\n"
<<" illum "<<illum<<"\n"
<<" Ka "<<Ka[0]<<" "<<Ka[1]<<" "<<Ka[2]<<"\n"
<<" Kd "<<Kd[0]<<" "<<Kd[1]<<" "<<Kd[2]<<"\n"
<<" map_Ka "<<texFileName<<"\n"
<<" map_Kd "<<texFileName<<"\n";
objOfstream.close();
mtlOfstream.close();
*/
return 0;
}
int writeToCustomizedFile(const Mat& depth, const vector<Point>& triangles, const float& f,
const string& filePath, const string& fileName = "temp")
{
if(depth.empty())
return -1;
if(triangles.empty())
return -2;
string objFileName(filePath);
objFileName.append(fileName).append(".txt");
FILE *objFileStream;
objFileStream = fopen(objFileName.c_str(), "w");
if(objFileStream == nullptr)
{
printf("无法打开txt文件! \n" );
return -3;
}
//fout vertices
const float cx = (float)(depth.cols)/2;
const float cy = (float)(depth.rows)/2;
float u, v;
fprintf(objFileStream, "%d\n", int(triangles.size()) );
for(int i=0; i<triangles.size(); ++i)
{
Point pt = triangles[i];
Point3f p3f;
p3f.z = depth.at<float>(pt.y, pt.x);
p3f.x = p3f.z*(pt.x-cx)/f;
p3f.y = p3f.z*(pt.y-cy)/f;
p3f = p3f/1000.0f;
u = pt.x / (cx*2 - 1);
v = 1 - pt.y / (cy*2 - 1);
fprintf(objFileStream, "%.4f %.4f %.4f %.4f %.4f\n", p3f.x, p3f.y, p3f.z, u, v);
}
return 0;
}