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