00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include "TrianglesMeshWriter.hpp"
00030 #include "Version.hpp"
00031
00032 #include <cassert>
00033
00035
00037
00038 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00039 TrianglesMeshWriter<ELEMENT_DIM, SPACE_DIM>::TrianglesMeshWriter(
00040 const std::string &rDirectory,
00041 const std::string &rBaseName,
00042 const bool clearOutputDir)
00043 : AbstractTetrahedralMeshWriter<ELEMENT_DIM, SPACE_DIM>(rDirectory, rBaseName, clearOutputDir)
00044 {
00045 }
00046
00047 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00048 TrianglesMeshWriter<ELEMENT_DIM, SPACE_DIM>::~TrianglesMeshWriter()
00049 {
00050 }
00051
00052 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00053 void TrianglesMeshWriter<ELEMENT_DIM, SPACE_DIM>::SetWriteFilesAsBinary()
00054 {
00055 this->mFilesAreBinary=true;
00056 }
00057
00058 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00059 void TrianglesMeshWriter<ELEMENT_DIM, SPACE_DIM>::WriteFiles()
00060 {
00061 std::string comment = "#\n# " + ChasteBuildInfo::GetProvenanceString();
00062
00063
00064 std::string node_file_name = this->mBaseName + ".node";
00065 out_stream p_node_file = this->mpOutputFileHandler->OpenOutputFile(node_file_name);
00066
00067
00068 unsigned num_attr = 0;
00069 unsigned max_bdy_marker = 0;
00070 unsigned num_nodes = this->GetNumNodes();
00071
00072 *p_node_file << num_nodes << "\t";
00073 *p_node_file << SPACE_DIM << "\t";
00074 *p_node_file << num_attr << "\t";
00075 *p_node_file << max_bdy_marker;
00076 if (this->mFilesAreBinary)
00077 {
00078 *p_node_file << "\tBIN\n";
00079 }
00080 else
00081 {
00082 *p_node_file << "\n";
00083 }
00084
00085 *p_node_file << std::setprecision(20);
00086
00087
00088 unsigned default_marker = UINT_MAX;
00089 for (unsigned item_num=0; item_num<num_nodes; item_num++)
00090 {
00091 WriteItem(p_node_file, item_num, this->GetNextNode(), default_marker);
00092 }
00093 *p_node_file << comment << "\n";
00094 p_node_file->close();
00095
00096 if (ELEMENT_DIM < SPACE_DIM)
00097 {
00098 WriteElementsAsFaces();
00099 WriteFacesAsEdges();
00100 return;
00101 }
00102
00103
00104 std::string element_file_name = this->mBaseName + ".ele";
00105 out_stream p_element_file = this->mpOutputFileHandler->OpenOutputFile(element_file_name);
00106
00107
00108 unsigned num_elements = this->GetNumElements();
00109 num_attr = 1u;
00110
00111 ElementData element_data = this->GetNextElement();
00112
00113 unsigned nodes_per_element = element_data.NodeIndices.size();
00114 if (nodes_per_element != ELEMENT_DIM+1)
00115 {
00116
00117 assert(ELEMENT_DIM == SPACE_DIM);
00118 assert(nodes_per_element == (ELEMENT_DIM+1)*(ELEMENT_DIM+2)/2);
00119 }
00120
00121 *p_element_file << num_elements << "\t";
00122 *p_element_file << nodes_per_element << "\t";
00123 *p_element_file << num_attr;
00124 if (this->mFilesAreBinary)
00125 {
00126 *p_element_file << "\tBIN\n";
00127 }
00128 else
00129 {
00130 *p_element_file << "\n";
00131 }
00132
00133
00134 for (unsigned item_num=0; item_num<num_elements; item_num++)
00135 {
00136
00137
00138 if (item_num>0)
00139 {
00140 element_data = this->GetNextElement();
00141 }
00142
00143 WriteItem(p_element_file, item_num, element_data.NodeIndices, element_data.AttributeValue);
00144 }
00145 *p_element_file << comment << "\n";
00146 p_element_file->close();
00147
00148
00149 std::string face_file_name = this->mBaseName;
00150
00151 if (SPACE_DIM == 1)
00152 {
00153
00154 return;
00155 }
00156 else if (SPACE_DIM == 2)
00157 {
00158 face_file_name = face_file_name + ".edge";
00159 }
00160 else
00161 {
00162 face_file_name = face_file_name + ".face";
00163 }
00164 out_stream p_face_file = this->mpOutputFileHandler->OpenOutputFile(face_file_name);
00165
00166
00167 unsigned num_faces = this->GetNumBoundaryFaces();
00168
00169 *p_face_file << num_faces << "\t";
00170 *p_face_file << max_bdy_marker;
00171 if (this->mFilesAreBinary)
00172 {
00173 *p_face_file << "\tBIN\n";
00174 }
00175 else
00176 {
00177 *p_face_file << "\n";
00178 }
00179
00180
00181 default_marker = UINT_MAX;
00182 for (unsigned item_num=0; item_num<num_faces; item_num++)
00183 {
00184 WriteItem(p_face_file, item_num, this->mBoundaryFaceData[item_num], default_marker);
00185 }
00186 *p_face_file << comment << "\n";
00187 p_face_file->close();
00188 }
00189
00190 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00191 void TrianglesMeshWriter<ELEMENT_DIM, SPACE_DIM>::WriteElementsAsFaces()
00192 {
00193 std::string comment = "#\n# " + ChasteBuildInfo::GetProvenanceString();
00194
00195 std::string element_file_name = this->mBaseName;
00196 if (ELEMENT_DIM == 1 && (SPACE_DIM == 2 || SPACE_DIM == 3))
00197 {
00198 element_file_name = element_file_name + ".edge";
00199 }
00200 else if (ELEMENT_DIM == 2 && SPACE_DIM == 3)
00201 {
00202 element_file_name = element_file_name + ".face";
00203 }
00204
00205 out_stream p_element_file = this->mpOutputFileHandler->OpenOutputFile(element_file_name);
00206
00207
00208 unsigned num_elements = this->GetNumElements();
00209 assert(SPACE_DIM != ELEMENT_DIM);
00210 unsigned num_attr = 0;
00211
00212 *p_element_file << num_elements << "\t";
00213
00214 *p_element_file << num_attr;
00215 if (this->mFilesAreBinary)
00216 {
00217 *p_element_file << "\tBIN\n";
00218 }
00219 else
00220 {
00221 *p_element_file << "\n";
00222 }
00223
00224
00225 for (unsigned item_num=0; item_num<num_elements; item_num++)
00226 {
00227 WriteItem(p_element_file, item_num, this->GetNextElement().NodeIndices);
00228 }
00229 *p_element_file << comment << "\n";
00230 p_element_file->close();
00231
00232 }
00233 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00234 void TrianglesMeshWriter<ELEMENT_DIM, SPACE_DIM>::WriteFacesAsEdges()
00235 {
00236 std::string comment = "#\n# " + ChasteBuildInfo::GetProvenanceString();
00237
00238 if (ELEMENT_DIM == 1 && (SPACE_DIM == 2 || SPACE_DIM == 3))
00239 {
00240 return;
00241 }
00242
00243 assert(SPACE_DIM == 3 && ELEMENT_DIM == 2);
00244
00245 std::string face_file_name = this->mBaseName;
00246 face_file_name = face_file_name + ".edge";
00247
00248 out_stream p_face_file = this->mpOutputFileHandler->OpenOutputFile(face_file_name);
00249
00250
00251 unsigned num_faces = this->GetNumBoundaryFaces();
00252
00253 unsigned max_bdy_marker = 0;
00254 unsigned default_marker = UINT_MAX;
00255
00256 *p_face_file << num_faces << "\t";
00257 *p_face_file << max_bdy_marker;
00258 if (this->mFilesAreBinary)
00259 {
00260 *p_face_file << "\tBIN\n";
00261 }
00262 else
00263 {
00264 *p_face_file << "\n";
00265 }
00266
00267 for (unsigned item_num=0; item_num<num_faces; item_num++)
00268 {
00269 WriteItem(p_face_file, item_num, this->mBoundaryFaceData[item_num], default_marker);
00270 }
00271 *p_face_file << comment << "\n";
00272 p_face_file->close();
00273 }
00274
00275
00276 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00277 template<class T>
00278 void TrianglesMeshWriter<ELEMENT_DIM, SPACE_DIM>::WriteItem(out_stream &pFile, unsigned itemNumber,
00279 const std::vector<T> &dataPacket, unsigned attribute)
00280 {
00281 if (this->mFilesAreBinary)
00282 {
00283
00284
00285 pFile->write((char*)&dataPacket[0], dataPacket.size()*sizeof(T));
00286
00287 if (attribute != UINT_MAX)
00288 {
00289 pFile->write((char*) &attribute, sizeof(attribute));
00290 }
00291 }
00292 else
00293 {
00294 *pFile << itemNumber;
00295 for (unsigned i=0; i<dataPacket.size(); i++)
00296 {
00297 *pFile << "\t" << dataPacket[i];
00298 }
00299 if (attribute != UINT_MAX)
00300 {
00301 *pFile << "\t" << attribute;
00302 }
00303 *pFile << "\n";
00304 }
00305 }
00307
00309
00310 template class TrianglesMeshWriter<1,1>;
00311 template class TrianglesMeshWriter<1,2>;
00312 template class TrianglesMeshWriter<1,3>;
00313 template class TrianglesMeshWriter<2,2>;
00314 template class TrianglesMeshWriter<2,3>;
00315 template class TrianglesMeshWriter<3,3>;
00316
00321 template void TrianglesMeshWriter<1, 1>::WriteItem(out_stream &, unsigned, const std::vector<unsigned> &, unsigned );
00322 template void TrianglesMeshWriter<1, 1>::WriteItem(out_stream &, unsigned, const std::vector<double> &, unsigned );
00323 template void TrianglesMeshWriter<1, 2>::WriteItem(out_stream &, unsigned, const std::vector<unsigned> &, unsigned );
00324 template void TrianglesMeshWriter<1, 2>::WriteItem(out_stream &, unsigned, const std::vector<double> &, unsigned );
00325 template void TrianglesMeshWriter<1, 3>::WriteItem(out_stream &, unsigned, const std::vector<unsigned> &, unsigned );
00326 template void TrianglesMeshWriter<1, 3>::WriteItem(out_stream &, unsigned, const std::vector<double> &, unsigned );
00327 template void TrianglesMeshWriter<2, 2>::WriteItem(out_stream &, unsigned, const std::vector<unsigned> &, unsigned );
00328 template void TrianglesMeshWriter<2, 2>::WriteItem(out_stream &, unsigned, const std::vector<double> &, unsigned );
00329 template void TrianglesMeshWriter<2, 3>::WriteItem(out_stream &, unsigned, const std::vector<unsigned> &, unsigned );
00330 template void TrianglesMeshWriter<2, 3>::WriteItem(out_stream &, unsigned, const std::vector<double> &, unsigned );
00331 template void TrianglesMeshWriter<3, 3>::WriteItem(out_stream &, unsigned, const std::vector<unsigned> &, unsigned );
00332 template void TrianglesMeshWriter<3, 3>::WriteItem(out_stream &, unsigned, const std::vector<double> &, unsigned );