25 <<
"Failed to open file: "<< fileName<<
" in call "
26 <<
"to ImportFromOBJFile \n";
32 std::string file_line;
33 std::string delimiter =
" ";
35 while (std::getline(file,file_line))
40 size_t beg_of_word = file_line.find_first_not_of(delimiter);
41 size_t end_of_word = file_line.find(delimiter,beg_of_word-beg_of_word);
42 std::string first_word = file_line.substr(beg_of_word,end_of_word);
46 if (first_word ==
"v")
49 for (
int k=1;k<=3;k++)
52 beg_of_word = file_line.find_first_not_of(delimiter, end_of_word);
53 end_of_word = file_line.find(delimiter, beg_of_word);
54 sub_word = file_line.substr(beg_of_word, end_of_word-beg_of_word);
59 double numValue = std::stod(sub_word);
61 if (k==1) newVertex.
x = numValue + transform.
x;
62 else if (k==2) newVertex.
y = numValue + transform.
y;
63 else if (k==3) newVertex.
z = numValue + transform.
z;
67 catch(
const std::invalid_argument& ia)
69 std::cout<<
"Exception caught!"<<std::endl;
73 if (end_of_word==std::string::npos) {
break;}
79 if (first_word.compare(
"vt")==0) {
81 for (
int k=1;k<=2;k++)
84 beg_of_word = file_line.find_first_not_of(delimiter, end_of_word);
85 end_of_word = file_line.find(delimiter, beg_of_word);
86 sub_word = file_line.substr(beg_of_word, end_of_word-beg_of_word);
89 try{
double numValue = std::stod(sub_word);
93 newVertex.
x = numValue;
97 newVertex.
y = numValue;
101 newVertex.
z = numValue;
106 catch(
const std::invalid_argument& ia)
109 std::cout<<
"Exception caught!"<<std::endl;
113 if (end_of_word==std::string::npos) {
break;}
119 if (first_word.compare(
"vn")==0) {
121 for (
int k=1;k<=3;k++)
124 beg_of_word = file_line.find_first_not_of(delimiter, end_of_word);
125 end_of_word = file_line.find(delimiter, beg_of_word);
126 sub_word = file_line.substr(beg_of_word, end_of_word-beg_of_word);
129 try{
double numValue = std::stod(sub_word);
133 newNormal.
x = numValue;
137 newNormal.
y = numValue;
141 newNormal.
z = numValue;
146 catch(
const std::invalid_argument& ia)
149 std::cout<<
"Exception caught!"<<std::endl;
153 if (end_of_word==std::string::npos) {
break;}
155 this->
normals_.push_back(newNormal);
159 if (first_word.compare(
"f")==0)
161 int number_of_verts = std::count(file_line.begin(),file_line.end(),
'/')/2;
162 if ((number_of_verts==3) && (!as_poly))
166 for (
int k=1;k<=3;k++)
169 beg_of_word = file_line.find_first_not_of(delimiter, end_of_word);
170 end_of_word = file_line.find(delimiter, beg_of_word);
171 sub_word = file_line.substr(beg_of_word, end_of_word-beg_of_word);
174 size_t first_dash = sub_word.find(
"/");
175 size_t last_dash = sub_word.find_last_of(
"/");
178 std::string vert_word = sub_word.substr(0,first_dash-0);
179 std::string norm_word = sub_word.substr(last_dash+1,sub_word.length()-last_dash-1);
182 try{
int numValue = std::stoi(vert_word);
183 newFace->
v_index[k-1] = numValue-1;
185 catch(
const std::invalid_argument& ia){std::cout<<
"Exception caught!"<<std::endl; }
188 try{
int numValue = std::stoi(norm_word);
189 newFace->
n_index[k-1] = numValue-1;
191 catch(
const std::invalid_argument& ia){std::cout<<
"Exception caught!"<<std::endl; }
194 if (last_dash>(first_dash+1)){
195 std::string tvert_word = sub_word.substr(first_dash+1,last_dash-first_dash-1);
196 try{
int numValue = std::stoi(tvert_word);
197 newFace->
vt_index[k-1] = numValue-1;
199 catch(
const std::invalid_argument& ia)
201 std::cout<<
"Exception caught!"<<std::endl;
206 if (end_of_word==std::string::npos) {
break;}
220 this->
faces_.push_back(*newFace);
226 for (
int k=1;k<=number_of_verts;k++)
229 beg_of_word = file_line.find_first_not_of(delimiter, end_of_word);
230 end_of_word = file_line.find(delimiter, beg_of_word);
231 sub_word = file_line.substr(beg_of_word, end_of_word-beg_of_word);
234 size_t first_dash = sub_word.find(
"/");
235 size_t last_dash = sub_word.find_last_of(
"/");
238 std::string vert_word = sub_word.substr(0,first_dash-0);
239 std::string norm_word = sub_word.substr(last_dash+1,sub_word.length()-last_dash-1);
242 try{
int numValue = std::stoi(vert_word);
243 newFace->
v_indices.push_back(numValue-1);
245 catch(
const std::invalid_argument& ia){std::cout<<
"Exception caught!"<<std::endl; }
248 try{
int numValue = std::stoi(norm_word);
251 catch(
const std::invalid_argument& ia){std::cout<<
"Exception caught!"<<std::endl; }
254 if (last_dash>(first_dash+1)){
255 std::string tvert_word = sub_word.substr(first_dash+1,last_dash-first_dash-1);
256 try{
int numValue = std::stoi(tvert_word);
259 catch(
const std::invalid_argument& ia)
261 std::cout<<
"Exception caught!"<<std::endl;
266 if (end_of_word==std::string::npos) {
break;}
269 for (
int v=0;v<(newFace->
v_indices.size());v++)
271 int* side_indices =
new int[4];
274 side_indices[1] = newFace->
v_indices[v+1];
275 side_indices[2] = -1;
276 side_indices[3] = -1;
283 newFace->
edges.push_back(side_indices);
295 if (first_word.compare(
"l")==0){
299 beg_of_word = file_line.find_first_not_of(delimiter, end_of_word);
300 end_of_word = file_line.find(delimiter, beg_of_word);
301 sub_word = file_line.substr(beg_of_word, end_of_word-beg_of_word);
304 try{
int numValue = std::stoi(sub_word);
305 newEdge.
v_index[0] = numValue-1;
307 catch(
const std::invalid_argument& ia){std::cout<<
"Exception caught!"<<std::endl; }
310 beg_of_word = file_line.find_first_not_of(delimiter, end_of_word);
311 end_of_word = file_line.find(delimiter, beg_of_word);
312 sub_word = file_line.substr(beg_of_word, end_of_word-beg_of_word);
315 try{
int numValue = std::stoi(sub_word);
316 newEdge.
v_index[1] = numValue-1;
318 catch(
const std::invalid_argument& ia){std::cout<<
"Exception caught!"<<std::endl; }
323 this->
lines_.push_back(newEdge);
332 std::vector<chi_mesh::Face>::iterator curFace;
333 for (curFace = this->
faces_.begin(); curFace != this->faces_.end(); curFace++)
343 curFace->geometric_normal = vAB.
Cross(vBC);
344 curFace->geometric_normal = curFace->geometric_normal/curFace->geometric_normal.
Norm();
352 nAvg = nAvg/nAvg.
Norm();
354 curFace->assigned_normal = nAvg;
357 curFace->face_centroid = (vA+vB+vC)/3.0;
359 std::vector<chi_mesh::PolyFace*>::iterator curPFace;
365 int num_verts = (*curPFace)->v_indices.size();
366 for (
int v=0; v<num_verts; v++)
367 centroid = centroid +
vertices_[(*curPFace)->v_indices[v]];
369 centroid = centroid/num_verts;
371 (*curPFace)->face_centroid = centroid;
375 centroid -
vertices_[(*curPFace)->v_indices[1]]);
378 (*curPFace)->geometric_normal = n;
385 <<
"Surface mesh loaded with "
386 << this->
faces_.size() <<
" triangle faces and "
398 std::string node_filename = std::string(fileName) +
399 std::string(
".1.node");
400 std::string tria_filename = std::string(fileName) +
401 std::string(
".1.ele");
405 file.open(node_filename);
409 <<
"Failed to open file: "<< node_filename <<
" in call "
410 <<
"to ImportFromOBJFile \n";
417 file.getline(line,250);
418 for (
int v=1; v<=num_verts; v++)
422 file >> vert_index >> vertex.
x >> vertex.
y;
423 file.getline(line,250);
425 vertices_.push_back(vertex);
431 file.open(tria_filename);
435 <<
"Failed to open file: "<< tria_filename <<
" in call "
436 <<
"to ImportFromOBJFile \n";
443 file.getline(line,250);
444 for (
int v=1; v<=num_tris; v++)
450 file >> tri_index >> v0 >> v1 >> v2;
451 file.getline(line,250);
459 for (
int e=0; e<3; e++)
461 int* side_indices =
new int[4];
466 side_indices[1] = newFace->
v_indices[e+1];
467 side_indices[2] = -1;
468 side_indices[3] = -1;
474 side_indices[2] = -1;
475 side_indices[3] = -1;
477 newFace->
edges.push_back(side_indices);
480 poly_faces_.push_back(newFace);
486 std::vector<chi_mesh::Face>::iterator curFace;
487 for (curFace = this->faces_.begin(); curFace != this->faces_.end(); curFace++)
497 curFace->geometric_normal = vAB.
Cross(vBC);
498 curFace->geometric_normal = curFace->geometric_normal/curFace->geometric_normal.
Norm();
506 nAvg = nAvg/nAvg.
Norm();
508 curFace->assigned_normal = nAvg;
511 curFace->face_centroid = (vA+vB+vC)/3.0;
513 std::vector<chi_mesh::PolyFace*>::iterator curPFace;
514 for (curPFace = this->poly_faces_.begin();
515 curPFace!=this->poly_faces_.end();
519 int num_verts = (*curPFace)->v_indices.size();
520 for (
int v=0; v<num_verts; v++)
521 centroid = centroid + vertices_[(*curPFace)->v_indices[v]];
523 centroid = centroid/num_verts;
525 (*curPFace)->face_centroid = centroid;
528 vertices_[(*curPFace)->v_indices[0]]).
Cross(
529 centroid - vertices_[(*curPFace)->v_indices[1]]);
532 (*curPFace)->geometric_normal = n;
535 UpdateInternalConnectivity();
539 <<
"Surface mesh loaded with "
540 << this->faces_.size() <<
" triangle faces and "
541 << this->poly_faces_.size() <<
" polygon faces.";
561 std::vector<double>& vertices_1d_y)
564 if (vertices_1d_x.empty())
567 <<
"chi_mesh::SurfaceMesh::CreateFromDivisions. Empty vertex_x list.";
570 if (vertices_1d_y.empty())
573 <<
"chi_mesh::SurfaceMesh::CreateFromDivisions. Empty vertex_y list.";
578 int Nvx = vertices_1d_x.size();
579 int Nvy = vertices_1d_y.size();
584 std::vector<chi_mesh::Vertex> vertices_x;
585 std::vector<chi_mesh::Vertex> vertices_y;
587 vertices_x.reserve(Nvx);
588 vertices_y.reserve(Nvy);
590 for (
double v : vertices_1d_x)
591 vertices_x.emplace_back(v,0.0,0.0);
593 for (
double v : vertices_1d_y)
594 vertices_y.emplace_back(0.0,v,0.0);
600 std::vector<std::vector<int>> vert_ij_map(Nvx,std::vector<int>(Nvx,-1));
601 for (
int i=0; i<Nvy; ++i)
603 for (
int j=0; j<Nvx; ++j)
605 surf_mesh->vertices_.push_back(vertices_x[j] + vertices_y[i]);
606 vert_ij_map[i][j] = surf_mesh->vertices_.size() - 1;
611 for (
int i=0; i<Ncy; ++i)
613 for (
int j=0; j<Ncx; ++j)
616 new_face->v_indices.push_back(vert_ij_map[i ][j ]);
617 new_face->v_indices.push_back(vert_ij_map[i ][j+1]);
618 new_face->v_indices.push_back(vert_ij_map[i+1][j+1]);
619 new_face->v_indices.push_back(vert_ij_map[i+1][j]);
621 for (
int v=0;v<(new_face->v_indices.size());v++)
623 int* side_indices =
new int[4];
625 side_indices[0] = new_face->v_indices[v];
626 side_indices[1] = new_face->v_indices[v+1];
627 side_indices[2] = -1;
628 side_indices[3] = -1;
630 if ((v+1)>=new_face->v_indices.size())
631 side_indices[1] = new_face->v_indices[0];
633 new_face->edges.push_back(side_indices);
636 surf_mesh->poly_faces_.push_back(new_face);
641 for (
auto poly_face : surf_mesh->poly_faces_)
644 int num_verts = poly_face->v_indices.size();
645 for (
int v=0; v<num_verts; v++)
646 centroid = centroid + surf_mesh->vertices_[poly_face->v_indices[v]];
648 centroid = centroid/num_verts;
650 poly_face->face_centroid = centroid;
653 surf_mesh->vertices_[poly_face->v_indices[0]]).
Cross(
654 centroid - surf_mesh->vertices_[poly_face->v_indices[1]]);
657 poly_face->geometric_normal = n;
660 surf_mesh->UpdateInternalConnectivity();
670 const std::string node_section_name=
"$Nodes";
671 const std::string elements_section_name =
"$Elements";
673 std::istringstream iss;
677 file.open(std::string(fileName));
682 <<
"Failed to open file: "<< fileName <<
" in call "
683 <<
"to ImportFromMshFiles \n";
689 while (std::getline(file, line))
691 if ( node_section_name.compare(line)==0 )
695 std::getline(file, line);
696 iss = std::istringstream(line);
698 if ( !(iss >> num_nodes) )
704 vertices_.resize(num_nodes);
706 for (
int n=0; n<num_nodes; n++)
708 std::getline(file, line);
709 iss = std::istringstream(line);
713 if ( !(iss >> vert_index) )
719 if (!(iss >> vertex.
x >> vertex.
y >> vertex.
z))
725 vertices_[vert_index - 1] = vertex;
732 while (std::getline(file, line))
734 if ( elements_section_name.compare(line)==0 )
738 std::getline(file, line);
739 iss = std::istringstream(line);
741 if (!(iss >> num_elems))
747 for (
int n=0; n<num_elems; n++)
749 int elem_type, num_tags, physical_reg, tag, element_index;
751 std::getline(file, line);
752 iss = std::istringstream(line);
754 if ( !(iss >> element_index >> elem_type >> num_tags) )
756 Chi::log.
LogAllError()<<
"Failed while reading element index, element type, and number of tags.\n";
760 if( !(iss>>physical_reg) )
766 for (
int i=1; i<num_tags; i++)
775 const int num_nodes = 3;
777 int nodes[num_nodes];
778 for (
int i=0; i<num_nodes; i++)
779 if ( !(iss >> nodes[i]) )
786 for (
int i=0; i<num_nodes; i++)
789 }
else if (elem_type == 3)
791 const int num_nodes = 4;
793 int nodes[num_nodes];
794 for (
int & node : nodes)
795 if ( !(iss >> node) )
802 for (
int i=0; i<num_nodes; i++)
810 const size_t total_nodes = newFace->
v_indices.size();
812 for (
size_t e=0; e<total_nodes; e++)
814 int* side_indices =
new int[total_nodes];
818 side_indices[1] = newFace->
v_indices[e+1];
822 side_indices[2] = -1;
823 side_indices[3] = -1;
825 newFace->
edges.push_back(side_indices);
828 poly_faces_.push_back(newFace);
829 physical_region_map_.push_back(physical_reg);
835 for (
const auto& poly_face : poly_faces_)
838 size_t num_verts = poly_face->v_indices.size();
840 for (
size_t v=0; v<num_verts; v++)
841 centroid = centroid + vertices_[poly_face->v_indices[v]];
843 centroid = centroid/
static_cast<double>(num_verts);
845 poly_face->face_centroid = centroid;
848 vertices_[poly_face->v_indices[0]]).Cross(
849 centroid - vertices_[poly_face->v_indices[1]]);
853 poly_face->geometric_normal = n;
856 UpdateInternalConnectivity();
872 FILE* outputFile = fopen(fileName,
"w");
873 if (outputFile==
nullptr)
875 printf(
"Error creating file %s!\n",fileName);
879 fprintf(outputFile,
"# Exported mesh file from tringulation script\n");
880 fprintf(outputFile,
"o %s\n",
"ChitechTriMesh");
882 std::vector<chi_mesh::Vertex>::iterator cur_v;
883 for (cur_v = this->vertices_.begin();
884 cur_v != this->vertices_.end();
887 fprintf(outputFile,
"v %9.6f %9.6f %9.6f\n",cur_v->x,cur_v->y,cur_v->z);
890 for (
unsigned ell=0; ell<this->lines_.size(); ell++)
892 fprintf(outputFile,
"l %d %d \n", lines_[ell].v_index[0] + 1,
893 lines_[ell].v_index[1] + 1);
902 fprintf(outputFile,
"s off\n");
904 std::vector<chi_mesh::Face>::iterator cur_face;
905 for (cur_face = this->faces_.begin();
906 cur_face != this->faces_.end();
909 fprintf(outputFile,
"f %d//1 %d//1 %d//1\n",cur_face->v_index[0]+1,
910 cur_face->v_index[1]+1,
911 cur_face->v_index[2]+1);
914 if (!poly_faces_.empty())
920 fprintf(outputFile,
"s off\n");
922 for (
auto & poly_face : poly_faces_)
924 fprintf(outputFile,
"f ");
925 for (
int v_indice : poly_face->v_indices)
927 fprintf(outputFile,
"%d//1 ",v_indice+1);
929 fprintf(outputFile,
"\n");
934 printf(
"Exported mesh to %s\n",fileName);
941 FILE* outputFile = fopen(fileName,
"w");
942 if (outputFile==
nullptr)
944 printf(
"Error creating file %s!\n",fileName);
948 fprintf(outputFile,
"%lu 2 0 0\n", vertices_.size());
949 for (
int v=0; v < vertices_.size(); v++)
951 fprintf(outputFile,
"%d %.15f %.15f 0\n",v+1, vertices_[v].x, vertices_[v].y);
954 fprintf(outputFile,
"%lu 0\n", lines_.size());
955 for (
int e=0; e < lines_.size(); e++)
957 fprintf(outputFile,
"%d %d %d\n",e+1, lines_[e].v_index[0] + 1,
958 lines_[e].v_index[1] + 1);
961 fprintf(outputFile,
"0");
965 printf(
"Exported mesh to %s\n",fileName);
static void Exit(int error_code)
LogStream Log(LOG_LVL level=LOG_0)
static SurfaceMesh * CreateFromDivisions(std::vector< double > &vertices_1d_x, std::vector< double > &vertices_1d_y)
int ImportFromTriangleFiles(const char *fileName, bool as_poly)
void ExportToOBJFile(const char *fileName)
void ExportToPolyFile(const char *fileName)
std::vector< chi_mesh::Edge > lines_
int ImportFromMshFiles(const char *fileName, bool as_poly)
std::vector< chi_mesh::PolyFace * > poly_faces_
Polygonal faces.
void UpdateInternalConnectivity()
std::vector< chi_mesh::Vertex > tex_vertices_
Texture vertices.
std::vector< chi_mesh::Vertex > vertices_
int ImportFromOBJFile(const std::string &fileName, bool as_poly=false, const chi_mesh::Vector3 &transform=Vector3(0, 0, 0))
std::vector< chi_mesh::Face > faces_
std::vector< chi_mesh::Normal > normals_
chi_mesh::Vertex vertices[2]
Vector vertices.
int v_index[2]
Indices of the vertices.
chi_mesh::Normal geometric_normal
std::vector< int > v_indices
std::vector< int * > edges
chi_mesh::Normal geometric_normal
std::string PrintStr() const
Vector3 Cross(const Vector3 &that) const