37 if (filename.size() < 5 || filename.substr(filename.size() - 4) != std::string(
".obj")) {
38 std::cerr <<
"Error: Expected OBJ file with filename of the form <name>.obj.\n";
42 std::ifstream infile(filename);
44 std::cerr <<
"Failed to open. Terminating.\n";
48 int ignored_lines = 0;
50 std::vector<Coord> vertList;
51 std::vector<Triangle> faceList;
52 std::vector<Triangle> faceVnList;
53 while (!infile.eof()) {
54 std::getline(infile, line);
57 if (line.substr(0, 1) == std::string(
"v") && line.substr(0, 2) != std::string(
"vn") && line.substr(0, 2) != std::string(
"vt")) {
58 std::stringstream data(line);
61 data >> c >> point[0] >> point[1] >> point[2];
62 vertList.push_back(point);
64 else if (line.substr(0, 2) == std::string(
"vt"))
66 std::stringstream data(line);
69 data >> c >> point[0] >> point[1];
73 else if (line.substr(0, 1) == std::string(
"f")) {
74 std::stringstream data(line);
84 data >> c >> ss1 >> ss2 >> ss3;
87 for (
int i = 0; i < strlen(ss1); i++)
91 if (ss1[i] ==
'/') cnt =
true;
100 if (ss1[i] ==
'/')
break;
110 for (
int i = 0; i < strlen(ss2); i++)
114 if (ss2[i] ==
'/') cnt =
true;
123 if (ss2[i] ==
'/')
break;
133 for (
int i = 0; i < strlen(ss3); i++)
137 if (ss3[i] ==
'/') cnt =
true;
146 if (ss3[i] ==
'/')
break;
156 faceList.push_back(
Triangle(v0 - 1, v1 - 1, v2 - 1));
157 faceVnList.push_back(
Triangle(vt0, vt1, vt2));
167 std::cout << faceList.size() << std::endl;
168 FaceId.resize(faceList.size());
169 for (
int i = 0; i < faceList.size(); i++)
171 FaceId[i].vertexId1 = faceList[i][0];
172 FaceId[i].vertexId2 = faceList[i][1];
173 FaceId[i].vertexId3 = faceList[i][2];
174 FaceId[i].uvId1 = faceVnList[i][0];
175 FaceId[i].uvId2 = faceVnList[i][1];
176 FaceId[i].uvId3 = faceVnList[i][2];
183 for (
int i = 0; i <
FaceStart.size(); i++)
185 for (
int i = 0; i <
FaceId.size(); i++)
187 if (i == 0 ||
FaceId[i].maxVertexID() !=
FaceId[i - 1].maxVertexID())
236 printf(
"------Tetra Mesh Writer: array nullptr \n");
242 std::ofstream
output(filename.c_str(), std::ios::out);
243 std::ofstream outputF(filenameF.c_str(), std::ios::out);
246 printf(
"------Tetra Mesh Writer: open file failed \n");
260 host_tri2tet.resize( (*(this->
ptr_tri2tet)).size() );
261 host_tets.resize((*(this->
ptr_tets)).size());
266 std::cout << host_VerId.size() << std::endl;
271 host_tets.assign(*(this->
ptr_tets));
277 onFace.resize(host_VerId.size());
279 for (
int i = 0; i <
onFace.size(); i++)
282 for (
int i = 0; i < host_tri2tet.size(); ++i) {
284 bool isOnSurface =
false;
285 if (tmp[0] < 0 || tmp[1] < 0) { isOnSurface =
true; }
288 onFace[host_triangles[i][0]] =
true;
289 onFace[host_triangles[i][1]] =
true;
290 onFace[host_triangles[i][2]] =
true;
293 int mapping_last = 0;
294 for (
int i = 0; i <
onFace.size(); i++)
304 std::cout <<
"mapping_last_size = "<< mapping_last << std::endl;
308 for (
uint i = 0; i < host_vertices.size(); ++i) {
309 output <<
"v " << host_vertices[i][0] <<
" " << host_vertices[i][1] <<
" " << host_vertices[i][2] << std::endl;
310 outputF <<
"v " << host_vertices[i][0] <<
" " << host_vertices[i][1] <<
" " << host_vertices[i][2] << std::endl;
317 for (
int i = 0; i < host_tri2tet.size(); ++i) {
319 bool isOnSurface =
false;
320 if (tmp[0] < 0 || tmp[1] < 0) { isOnSurface =
true; }
329 newTri[0] =
FaceMapping[host_VerId[host_triangles[i][0]]];
330 newTri[1] =
FaceMapping[host_VerId[host_triangles[i][1]]];
331 newTri[2] =
FaceMapping[host_VerId[host_triangles[i][2]]];
335 if (idInitSurface >= 0)
337 int newid1 = (newTri[0] ==
FaceId[idInitSurface].vertexId1) ? host_triangles[i][0] : ((newTri[1] ==
FaceId[idInitSurface].vertexId1) ? host_triangles[i][1] : host_triangles[i][2]);
338 int newid2 = (newTri[0] ==
FaceId[idInitSurface].vertexId2) ? host_triangles[i][0] : ((newTri[1] ==
FaceId[idInitSurface].vertexId2) ? host_triangles[i][1] : host_triangles[i][2]);
339 int newid3 = (newTri[0] ==
FaceId[idInitSurface].vertexId3) ? host_triangles[i][0] : ((newTri[1] ==
FaceId[idInitSurface].vertexId3) ? host_triangles[i][1] : host_triangles[i][2]);
341 << newid1 + 1 <<
"/" <<
FaceId[idInitSurface].uvId1 <<
" "
342 << newid2 + 1 <<
"/" <<
FaceId[idInitSurface].uvId2 <<
" "
343 << newid3 + 1 <<
"/" <<
FaceId[idInitSurface].uvId3 << std::endl;
349 bool reverse =
false;
350 int idx_tet = tmp[0] < 0 ? tmp[1] : tmp[0];
351 for (idx_vertex = 0; idx_vertex < 4; idx_vertex++)
354 if (host_tets[idx_tet][idx_vertex] != host_triangles[i][0]
355 && host_tets[idx_tet][idx_vertex] != host_triangles[i][1]
356 && host_tets[idx_tet][idx_vertex] != host_triangles[i][2]
360 idx_vertex = host_tets[idx_tet][idx_vertex];
362 ((host_vertices[host_triangles[i][1]] - host_vertices[host_triangles[i][0]]).
cross
363 (host_vertices[host_triangles[i][2]] - host_vertices[host_triangles[i][1]]))
365 (host_vertices[idx_vertex] - host_vertices[host_triangles[i][0]]) > 0
369 outputF <<
"f " << host_triangles[i][0] + 1 <<
" " << host_triangles[i][1] + 1 <<
" " << host_triangles[i][2] + 1 << std::endl;
371 outputF <<
"f " << host_triangles[i][0] + 1 <<
" " << host_triangles[i][2] + 1 <<
" " << host_triangles[i][1] + 1 << std::endl;
378 host_vertices.clear();
379 host_triangles.clear();
380 host_tri2tet.clear();