36 #include "TrianglesMeshWriter.hpp"
38 #include "AbstractTetrahedralMesh.hpp"
39 #include "Version.hpp"
47 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
49 const std::string& rDirectory,
50 const std::string& rBaseName,
51 const bool clearOutputDir)
56 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
61 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
64 this->mFilesAreBinary=
true;
67 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
74 std::string node_file_name = this->mBaseName +
".node";
75 out_stream p_node_file = this->mpOutputFileHandler->OpenOutputFile(node_file_name, std::ios::binary | std::ios::trunc);
78 unsigned num_attr = 0;
80 if (this->mpMesh && !this->mFilesAreBinary)
83 num_attr = this->mpMesh->GetNumNodeAttributes();
86 unsigned max_bdy_marker = 0;
87 unsigned num_nodes = this->GetNumNodes();
89 *p_node_file << num_nodes <<
"\t";
90 *p_node_file << SPACE_DIM <<
"\t";
91 *p_node_file << num_attr <<
"\t";
92 *p_node_file << max_bdy_marker;
93 if (this->mFilesAreBinary)
95 *p_node_file <<
"\tBIN\n";
102 *p_node_file << std::setprecision(20);
105 for (
unsigned item_num=0; item_num<num_nodes; item_num++)
107 if (this->mpMesh && !this->mFilesAreBinary && num_attr!=0)
111 WriteItem(p_node_file, item_num, this->GetNextNode(), this->mpMesh->GetNode(item_num)->rGetNodeAttributes());
115 WriteItem(p_node_file, item_num, this->GetNextNode());
118 *p_node_file << comment <<
"\n";
119 p_node_file->close();
123 if (ELEMENT_DIM < SPACE_DIM)
125 WriteElementsAsFaces();
131 std::string element_file_name = this->mBaseName +
".ele";
132 out_stream p_element_file = this->mpOutputFileHandler->OpenOutputFile(element_file_name, std::ios::binary | std::ios::trunc);
135 unsigned num_elements = this->GetNumElements();
139 if (num_elements == 0)
141 *p_element_file << 0 <<
"\t";
142 *p_element_file << 0 <<
"\t";
143 *p_element_file << 0;
144 if (this->mFilesAreBinary)
146 *p_element_file <<
"\tBIN\n";
150 *p_element_file <<
"\n";
152 p_element_file->close();
158 unsigned nodes_per_element = element_data.
NodeIndices.size();
159 if (nodes_per_element != ELEMENT_DIM+1)
162 assert(ELEMENT_DIM == SPACE_DIM);
163 assert(nodes_per_element == (ELEMENT_DIM+1)*(ELEMENT_DIM+2)/2);
166 *p_element_file << num_elements <<
"\t";
167 *p_element_file << nodes_per_element <<
"\t";
168 *p_element_file << num_attr;
169 if (this->mFilesAreBinary)
171 *p_element_file <<
"\tBIN\n";
175 *p_element_file <<
"\n";
179 std::vector<double> attribute_values(1);
180 for (
unsigned item_num=0; item_num<num_elements; item_num++)
188 element_data = this->GetNextElement();
191 WriteItem(p_element_file, item_num, element_data.
NodeIndices, attribute_values);
193 *p_element_file << comment <<
"\n";
194 p_element_file->close();
200 std::string face_file_name = this->mBaseName;
202 if (ELEMENT_DIM == 1)
207 else if (ELEMENT_DIM == 2)
209 face_file_name = face_file_name +
".edge";
213 face_file_name = face_file_name +
".face";
215 out_stream p_face_file = this->mpOutputFileHandler->OpenOutputFile(face_file_name, std::ios::binary | std::ios::trunc);
218 if (num_elements != 0)
220 unsigned num_faces = this->GetNumBoundaryFaces();
222 *p_face_file << num_faces <<
"\t";
224 *p_face_file << max_bdy_marker;
225 if (this->mFilesAreBinary)
227 *p_face_file <<
"\tBIN\n";
231 *p_face_file <<
"\n";
235 std::vector<double> default_marker(0);
236 for (
unsigned item_num=0; item_num<num_faces; item_num++)
238 ElementData face_data = this->GetNextBoundaryElement();
239 WriteItem(p_face_file, item_num, face_data.
NodeIndices, default_marker);
241 *p_face_file << comment <<
"\n";
242 p_face_file->close();
244 if (this->GetNumCableElements() > 0)
247 std::string cable_element_file_name = this->mBaseName +
".cable";
248 out_stream p_cable_element_file = this->mpOutputFileHandler->OpenOutputFile(cable_element_file_name, std::ios::binary | std::ios::trunc);
251 unsigned num_cable_elements = this->GetNumCableElements();
255 *p_cable_element_file << num_cable_elements <<
"\t";
256 *p_cable_element_file << 2 <<
"\t";
257 *p_cable_element_file << num_attr;
258 if (this->mFilesAreBinary)
260 *p_cable_element_file <<
"\tBIN\n";
264 *p_cable_element_file <<
"\n";
268 std::vector<double> attribute_values(1);
269 for (
unsigned item_num=0; item_num<num_cable_elements; item_num++)
271 ElementData cable_element_data = this->GetNextCableElement();
273 WriteItem(p_cable_element_file, item_num, cable_element_data.
NodeIndices, attribute_values);
275 *p_cable_element_file << comment;
276 p_cable_element_file->close();
284 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
289 std::string element_file_name = this->mBaseName;
290 if (ELEMENT_DIM == 1 && (SPACE_DIM == 2 || SPACE_DIM == 3))
292 element_file_name = element_file_name +
".edge";
294 else if (ELEMENT_DIM == 2 && SPACE_DIM == 3)
296 element_file_name = element_file_name +
".face";
299 out_stream p_element_file = this->mpOutputFileHandler->OpenOutputFile(element_file_name, std::ios::binary | std::ios::trunc);
302 unsigned num_elements = this->GetNumElements();
303 assert(SPACE_DIM != ELEMENT_DIM);
304 unsigned num_attr = 1;
306 *p_element_file << num_elements <<
"\t";
308 *p_element_file << num_attr;
309 if (this->mFilesAreBinary)
311 *p_element_file <<
"\tBIN\n";
315 *p_element_file <<
"\n";
319 std::vector<double> attribute_values(1);
320 for (
unsigned item_num=0; item_num<num_elements; item_num++)
324 WriteItem(p_element_file, item_num, element_data.
NodeIndices, attribute_values);
327 *p_element_file << comment <<
"\n";
328 p_element_file->close();
331 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
336 if (ELEMENT_DIM == 1 && (SPACE_DIM == 2 || SPACE_DIM == 3))
341 assert(SPACE_DIM == 3 && ELEMENT_DIM == 2);
343 std::string face_file_name = this->mBaseName;
344 face_file_name = face_file_name +
".edge";
346 out_stream p_face_file = this->mpOutputFileHandler->OpenOutputFile(face_file_name, std::ios::binary | std::ios::trunc);
349 unsigned num_faces = this->GetNumBoundaryFaces();
351 unsigned max_bdy_marker = 0;
352 std::vector<double> default_marker(0);
354 *p_face_file << num_faces <<
"\t";
355 *p_face_file << max_bdy_marker;
356 if (this->mFilesAreBinary)
358 *p_face_file <<
"\tBIN\n";
362 *p_face_file <<
"\n";
366 for (
unsigned item_num=0; item_num<num_faces; item_num++)
368 ElementData face_data = this->GetNextBoundaryElement();
369 WriteItem(p_face_file, item_num, face_data.
NodeIndices, default_marker);
371 *p_face_file << comment <<
"\n";
372 p_face_file->close();
376 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
377 template<
class T_DATA>
379 const std::vector<T_DATA> &dataPacket)
383 WriteItem(pFile, itemNumber, dataPacket, std::vector<double>());
387 template<
unsigned ELEMENT_DIM,
unsigned SPACE_DIM>
388 template<
class T_DATA>
390 const std::vector<T_DATA> &dataPacket,
391 const std::vector<double> &rAttributes)
393 if (this->mFilesAreBinary)
397 pFile->write((
char*)&dataPacket[0], dataPacket.size()*
sizeof(T_DATA));
402 if (!rAttributes.empty())
404 pFile->write((
char*)&rAttributes[0], rAttributes.size()*
sizeof(
double));
409 *pFile << itemNumber;
410 for (
unsigned i=0; i<dataPacket.size(); i++)
412 *pFile <<
"\t" << dataPacket[i];
414 for (
unsigned i=0; i<rAttributes.size(); i++)
416 *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()