24 using namespace StrUtils;
25 using namespace FileUtils;
27 template<
typename IndexType >
28 inline IndexType
get_type(
const IndexType size,
const IndexType dim)
47 template<
typename CoordType,
52 const std::vector< std::vector<CoordType> >& points_coords,
53 const std::vector< std::vector<CoordNType> >& normals_coords,
54 const std::vector< std::vector<CoordCType> >& vertex_color_coords,
55 const std::vector< std::vector<IndexType> >& line_indices,
56 const std::vector< std::vector<CoordCType> >& lines_color_coords,
57 const std::vector< std::vector<IndexType> >& face_indices,
58 const std::vector< std::vector<CoordCType> >& face_color_coords,
59 const std::vector< std::vector< std::vector<double> > >& field_attributes,
60 const std::vector< std::string >& field_names)
65 std::cerr <<
"MSH Writer: file extension is inappropriate" << std::endl;
70 if (vertex_color_coords.size()!=0 && vertex_color_coords.size()!=points_coords.size())
72 std::cerr <<
"MSH Writer: Not every node are colored" << std::endl;
77 if (normals_coords.size()>0 && normals_coords.size()!=points_coords.size())
79 std::cerr <<
"MSH Writer: Not every node are normalized" << std::endl;
84 if (face_color_coords.size()!=0 && face_color_coords.size()!=face_indices.size())
86 std::cerr <<
"MSH Writer: Not every 2D element are colored" << std::endl;
91 if (line_indices.size()!=0 && face_indices.size()!=0)
93 std::cerr <<
"MSH Writer: It is impossible to have 2D elements with 3D elements" << std::endl;
96 std::vector<std::string> names;
97 if (field_names.size() != field_attributes.size())
99 names.resize(field_attributes.size());
103 for (
unsigned long i(0); i<field_attributes.size(); ++i){
104 if( (points_coords.size() == field_attributes[i].size()) &&
105 ((points_coords.size() != face_indices.size()) || (field_attributes[i][0].size()>1)))
106 names[i] = std::string(
"Shifting_") +
convert(i);
108 names[i] = std::string(
"cell_law_")+
convert(i);
116 IndexType dim = (face_indices.size()) ? 2 : 3;
117 const std::vector< std::vector<IndexType> >& elem = (face_indices.size()) ? face_indices : line_indices;
118 const std::vector< std::vector<CoordCType> >& color = (face_indices.size()) ? face_color_coords : lines_color_coords;
121 FILE* file = fopen(file_path.c_str(),
"w");
124 std::cerr <<
"MSH Reader: Unable to open file : " << file_path << std::endl;
129 fprintf(file,
"$MeshFormat\n2.2 0 %zd\n$EndMeshFormat\n$Nodes\n%zd\n",
sizeof(
double), points_coords.size());
132 if (points_coords.size()!=0)
135 for(
unsigned long i(0); i<points_coords.size(); i++)
137 fprintf(file,
"%ld %.10g %.10g %.10g\n", i+1,
static_cast<double>(points_coords[i][0]),
static_cast<double>(points_coords[i][1]),
static_cast<double>(points_coords[i][2]));
139 fprintf(file,
"$EndNodes\n");
142 fprintf(file,
"$Elements\n");
147 fprintf(file,
"%zd\n", elem.size());
150 for(
unsigned long i(0); i<elem.size(); i++)
152 type =
get_type(
static_cast<IndexType
>(elem[i].size()), dim);
156 std::cerr <<
"MSH Writer: Type of element is unknown" << std::endl;
160 fprintf(file,
"%ld %ld 2 99 2", i+1,
static_cast<long>(type));
161 for(
unsigned long j(0); j<elem[i].size(); j++)
163 fprintf(file,
" %s",
convert(elem[i][j]+1).c_str());
167 fprintf(file,
"$EndElements");
172 fprintf(file,
"%zd\n", points_coords.size());
173 for(
unsigned long i(0); i<points_coords.size(); i++)
175 fprintf(file,
"%lu 15 2 99 2 %lu\n", i+1, i+1);
177 fprintf(file,
"$EndElements");
181 if (vertex_color_coords.size()!=0)
183 fprintf(file,
"\n$NodeData\n1\n\"Color\"\n0\n1\n%zd\n", vertex_color_coords.size());
184 for(
unsigned long i(0); i<vertex_color_coords.size(); i++)
186 if (vertex_color_coords[i].size()!=3)
188 std::cerr <<
"MSH Writer: Wrong number of values for declaration of colors" << std::endl;
192 fprintf(file,
"%ld %.10g %.10g %.10g\n", i+1,
static_cast<double>(vertex_color_coords[i][0]),
static_cast<double>(vertex_color_coords[i][1]),
static_cast<double>(vertex_color_coords[i][2]));
194 fprintf(file,
"$EndNodeData");
198 if (normals_coords.size()!=0)
200 fprintf(file,
"\n$NodeData\n1\n\"Normal\"\n0\n1\n%zd\n", normals_coords.size());
201 for(
unsigned long i(0); i<normals_coords.size(); i++)
203 if (normals_coords[i].size()!=3)
205 std::cerr <<
"MSH Writer: Wrong number of values for declaration of normals" << std::endl;
209 fprintf(file,
"%ld %.10g %.10g %.10g\n", i+1,
static_cast<double>(normals_coords[i][0]),
static_cast<double>(normals_coords[i][1]),
static_cast<double>(normals_coords[i][2]));
211 fprintf(file,
"$EndNodeData");
217 fprintf(file,
"\n$ElementData\n1\n\"Color\"\n0\n1\n%zd\n", color.size());
218 for(
unsigned long i(0); i<color.size(); i++)
220 if (color[i].size()!=3)
222 std::cerr <<
"MSH Writer: Wrong number of values for declaration of colors" << std::endl;
226 fprintf(file,
"%ld %.10g %.10g %.10g\n", i+1,
static_cast<double>(color[i][0]),
static_cast<double>(color[i][1]),
static_cast<double>(color[i][2]));
228 fprintf(file,
"$EndElementData");
234 for(
unsigned long i(0); i<names.size(); ++i)
237 if ( names[i].find(
"POINT_DATA_") != std::string::npos )
240 fprintf(file,
"$NodeData\n1\n\"%s\"\n0\n1\n%zd\n", names[i].substr(11).c_str(), field_attributes[i].size());
242 else if ( (points_coords.size()==field_attributes[i].size()) && ( (points_coords.size()!=face_indices.size()) || (field_attributes[i][0].size()>1)) )
245 fprintf(file,
"$NodeData\n1\n\"%s\"\n0\n1\n%zd\n", names[i].c_str(), field_attributes[i].size());
247 else if (names[i].find(
"ELEMENT_DATA_") != std::string::npos)
250 fprintf(file,
"$ElementData\n1\n\"%s\"\n0\n1\n%zd\n", names[i].substr(13).c_str(), field_attributes[i].size());
252 else if (names[i].find(
"CELL_DATA_") != std::string::npos)
255 fprintf(file,
"$ElementData\n1\n\"%s\"\n0\n1\n%zd\n", names[i].substr(10).c_str(), field_attributes[i].size());
260 fprintf(file,
"$ElementData\n1\n\"%s\"\n0\n1\n%zd\n", names[i].c_str(), field_attributes[i].size());
262 for(
unsigned long j(0); j<field_attributes[i].size(); ++j)
264 fprintf(file,
"%lu", j+1);
265 for(
unsigned long k(0); k<field_attributes[i][j].size(); ++k)
267 fprintf(file,
" %.10g", field_attributes[i][j][k]);
272 fprintf(file,
"$EndNodeData");
274 fprintf(file,
"$EndElementData");
281 fprintf(file,
"$EndNodes\n$Elements\n0\n$EndElements");