36 #include "TrianglesMeshWriter.hpp"
38 #include "AbstractTetrahedralMesh.hpp"
39 #include "Version.hpp"
46 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
48 const std::string& rDirectory,
49 const std::string& rBaseName,
50 const bool clearOutputDir)
55 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
60 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
63 this->mFilesAreBinary=
true;
66 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
73 std::string node_file_name = this->mBaseName +
".node";
74 out_stream p_node_file = this->mpOutputFileHandler->OpenOutputFile(node_file_name, std::ios::binary | std::ios::trunc);
77 unsigned num_attr = 0;
79 if (this->mpMesh && !this->mFilesAreBinary)
82 num_attr = this->mpMesh->GetNumNodeAttributes();
85 unsigned max_bdy_marker = 0;
86 unsigned num_nodes = this->GetNumNodes();
88 *p_node_file << num_nodes <<
"\t";
89 *p_node_file << SPACE_DIM <<
"\t";
90 *p_node_file << num_attr <<
"\t";
91 *p_node_file << max_bdy_marker;
92 if (this->mFilesAreBinary)
94 *p_node_file <<
"\tBIN\n";
101 *p_node_file << std::setprecision(20);
104 for (
unsigned item_num=0; item_num<num_nodes; item_num++)
106 if (this->mpMesh && !this->mFilesAreBinary && num_attr!=0)
110 WriteItem(p_node_file, item_num, this->GetNextNode(), this->mpMesh->GetNode(item_num)->rGetNodeAttributes());
114 WriteItem(p_node_file, item_num, this->GetNextNode());
117 *p_node_file << comment <<
"\n";
118 p_node_file->close();
122 if (ELEMENT_DIM < SPACE_DIM)
124 WriteElementsAsFaces();
130 std::string element_file_name = this->mBaseName +
".ele";
131 out_stream p_element_file = this->mpOutputFileHandler->OpenOutputFile(element_file_name, std::ios::binary | std::ios::trunc);
134 unsigned num_elements = this->GetNumElements();
138 if (num_elements == 0)
140 *p_element_file << 0 <<
"\t";
141 *p_element_file << 0 <<
"\t";
142 *p_element_file << 0;
143 if (this->mFilesAreBinary)
145 *p_element_file <<
"\tBIN\n";
149 *p_element_file <<
"\n";
151 p_element_file->close();
157 unsigned nodes_per_element = element_data.
NodeIndices.size();
158 if (nodes_per_element != ELEMENT_DIM+1)
161 assert(ELEMENT_DIM == SPACE_DIM);
162 assert(nodes_per_element == (ELEMENT_DIM+1)*(ELEMENT_DIM+2)/2);
165 *p_element_file << num_elements <<
"\t";
166 *p_element_file << nodes_per_element <<
"\t";
167 *p_element_file << num_attr;
168 if (this->mFilesAreBinary)
170 *p_element_file <<
"\tBIN\n";
174 *p_element_file <<
"\n";
178 std::vector<double> attribute_values(1);
179 for (
unsigned item_num=0; item_num<num_elements; item_num++)
187 element_data = this->GetNextElement();
190 WriteItem(p_element_file, item_num, element_data.
NodeIndices, attribute_values);
192 *p_element_file << comment <<
"\n";
193 p_element_file->close();
199 std::string face_file_name = this->mBaseName;
201 if (ELEMENT_DIM == 1)
206 else if (ELEMENT_DIM == 2)
208 face_file_name = face_file_name +
".edge";
212 face_file_name = face_file_name +
".face";
214 out_stream p_face_file = this->mpOutputFileHandler->OpenOutputFile(face_file_name, std::ios::binary | std::ios::trunc);
217 if (num_elements != 0)
219 unsigned num_faces = this->GetNumBoundaryFaces();
221 *p_face_file << num_faces <<
"\t";
223 *p_face_file << max_bdy_marker;
224 if (this->mFilesAreBinary)
226 *p_face_file <<
"\tBIN\n";
230 *p_face_file <<
"\n";
234 std::vector<double> default_marker(0);
235 for (
unsigned item_num=0; item_num<num_faces; item_num++)
237 ElementData face_data = this->GetNextBoundaryElement();
238 WriteItem(p_face_file, item_num, face_data.
NodeIndices, default_marker);
240 *p_face_file << comment <<
"\n";
241 p_face_file->close();
243 if (this->GetNumCableElements() > 0)
246 std::string cable_element_file_name = this->mBaseName +
".cable";
247 out_stream p_cable_element_file = this->mpOutputFileHandler->OpenOutputFile(cable_element_file_name, std::ios::binary | std::ios::trunc);
250 unsigned num_cable_elements = this->GetNumCableElements();
254 *p_cable_element_file << num_cable_elements <<
"\t";
255 *p_cable_element_file << 2 <<
"\t";
256 *p_cable_element_file << num_attr;
257 if (this->mFilesAreBinary)
259 *p_cable_element_file <<
"\tBIN\n";
263 *p_cable_element_file <<
"\n";
267 std::vector<double> attribute_values(1);
268 for (
unsigned item_num=0; item_num<num_cable_elements; item_num++)
270 ElementData cable_element_data = this->GetNextCableElement();
272 WriteItem(p_cable_element_file, item_num, cable_element_data.
NodeIndices, attribute_values);
274 *p_cable_element_file << comment;
275 p_cable_element_file->close();
282 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
287 std::string element_file_name = this->mBaseName;
288 if (ELEMENT_DIM == 1 && (SPACE_DIM == 2 || SPACE_DIM == 3))
290 element_file_name = element_file_name +
".edge";
292 else if (ELEMENT_DIM == 2 && SPACE_DIM == 3)
294 element_file_name = element_file_name +
".face";
297 out_stream p_element_file = this->mpOutputFileHandler->OpenOutputFile(element_file_name, std::ios::binary | std::ios::trunc);
300 unsigned num_elements = this->GetNumElements();
301 assert(SPACE_DIM != ELEMENT_DIM);
302 unsigned num_attr = 1;
304 *p_element_file << num_elements <<
"\t";
306 *p_element_file << num_attr;
307 if (this->mFilesAreBinary)
309 *p_element_file <<
"\tBIN\n";
313 *p_element_file <<
"\n";
317 std::vector<double> attribute_values(1);
318 for (
unsigned item_num=0; item_num<num_elements; item_num++)
322 WriteItem(p_element_file, item_num, element_data.
NodeIndices, attribute_values);
325 *p_element_file << comment <<
"\n";
326 p_element_file->close();
329 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
334 if (ELEMENT_DIM == 1 && (SPACE_DIM == 2 || SPACE_DIM == 3))
339 assert(SPACE_DIM == 3 && ELEMENT_DIM == 2);
341 std::string face_file_name = this->mBaseName;
342 face_file_name = face_file_name +
".edge";
344 out_stream p_face_file = this->mpOutputFileHandler->OpenOutputFile(face_file_name, std::ios::binary | std::ios::trunc);
347 unsigned num_faces = this->GetNumBoundaryFaces();
349 unsigned max_bdy_marker = 0;
350 std::vector<double> default_marker(0);
352 *p_face_file << num_faces <<
"\t";
353 *p_face_file << max_bdy_marker;
354 if (this->mFilesAreBinary)
356 *p_face_file <<
"\tBIN\n";
360 *p_face_file <<
"\n";
364 for (
unsigned item_num=0; item_num<num_faces; item_num++)
366 ElementData face_data = this->GetNextBoundaryElement();
367 WriteItem(p_face_file, item_num, face_data.
NodeIndices, default_marker);
369 *p_face_file << comment <<
"\n";
370 p_face_file->close();
373 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
374 template<
class T_DATA>
376 const std::vector<T_DATA> &dataPacket)
380 WriteItem(pFile, itemNumber, dataPacket, std::vector<double>());
383 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
384 template<
class T_DATA>
386 const std::vector<T_DATA> &dataPacket,
387 const std::vector<double> &rAttributes)
389 if (this->mFilesAreBinary)
393 pFile->write((
char*)&dataPacket[0], dataPacket.size()*
sizeof(T_DATA));
398 if (!rAttributes.empty())
400 pFile->write((
char*)&rAttributes[0], rAttributes.size()*
sizeof(
double));
405 *pFile << itemNumber;
406 for (
unsigned i=0; i<dataPacket.size(); i++)
408 *pFile <<
"\t" << dataPacket[i];
410 for (
unsigned i=0; i<rAttributes.size(); i++)
412 *pFile <<
"\t" << rAttributes[i];
virtual ~TrianglesMeshWriter()
static void BeginEvent(unsigned event)
void WriteItem(out_stream &pFile, unsigned itemNumber, const std::vector< T_DATA > &dataPacket, const std::vector< double > &rAttributes)
TrianglesMeshWriter(const std::string &rDirectory, const std::string &rBaseName, const bool clearOutputDir=true)
std::vector< unsigned > NodeIndices
static std::string GetProvenanceString()
void SetWriteFilesAsBinary()
static void EndEvent(unsigned event)
void WriteElementsAsFaces()