CmguiMeshWriter.cpp
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 #include "Exception.hpp"
00029 #include "CmguiMeshWriter.hpp"
00030 #include "Version.hpp"
00031
00033
00035 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00036 CmguiMeshWriter<ELEMENT_DIM,SPACE_DIM>::CmguiMeshWriter(const std::string &rDirectory,
00037 const std::string &rBaseName,
00038 bool cleanDirectory)
00039 : AbstractTetrahedralMeshWriter<ELEMENT_DIM,SPACE_DIM>(rDirectory, rBaseName, cleanDirectory)
00040 {
00041 this->mIndexFromZero = false;
00042 mGroupName = this->mBaseName;
00043 }
00044
00045 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00046 void CmguiMeshWriter<ELEMENT_DIM,SPACE_DIM>::WriteFiles()
00047 {
00049
00051 std::string node_file_name = this->mBaseName + ".exnode";
00052 out_stream p_node_file = this->mpOutputFileHandler->OpenOutputFile(node_file_name);
00053
00054 WriteNodeFileHeader(p_node_file);
00055
00056
00057 for (unsigned item_num=0; item_num<this->GetNumNodes(); item_num++)
00058 {
00059 std::vector<double> current_item = this->GetNextNode();
00060
00061 *p_node_file << "Node:\t" << item_num+1 << "\t";
00062 for (unsigned i=0; i<ELEMENT_DIM; i++)
00063 {
00064 *p_node_file << current_item[i] << "\t";
00065 }
00066
00067 *p_node_file << "\n";
00068 }
00069 p_node_file->close();
00070
00072
00074 std::string elem_file_name = this->mBaseName + ".exelem";
00075 out_stream p_elem_file = this->mpOutputFileHandler->OpenOutputFile(elem_file_name);
00076
00077
00078
00079
00080 std::string comment = "! " + ChasteBuildInfo::GetProvenanceString();
00081 *p_elem_file << comment;
00082
00083 *p_elem_file << "Group name: " << mGroupName << "\n";
00084 switch (ELEMENT_DIM)
00085 {
00086 case 1:
00087 {
00088 *p_elem_file << CmguiElementFileHeader1D;
00089 break;
00090 }
00091 case 2:
00092 {
00093 *p_elem_file << CmguiElementFileHeader2D;
00094 break;
00095 }
00096 case 3:
00097 {
00098 *p_elem_file << CmguiElementFileHeader3D;
00099 break;
00100 }
00101 default:
00102 {
00103 NEVER_REACHED;
00104 }
00105 }
00106
00107
00108
00109 unsigned number_of_fields = mAdditionalFieldNames.size();
00110 std::stringstream string_of_number_of_fields;
00111
00112 string_of_number_of_fields << number_of_fields+1;
00113
00114 *p_elem_file << " #Fields="<<string_of_number_of_fields.str()<<"\n";
00115
00116
00117 switch (ELEMENT_DIM)
00118 {
00119 case 1:
00120 {
00121 *p_elem_file << CmguiCoordinatesFileHeader1D;
00122 break;
00123 }
00124 case 2:
00125 {
00126 *p_elem_file << CmguiCoordinatesFileHeader2D;
00127 break;
00128 }
00129 case 3:
00130 {
00131 *p_elem_file << CmguiCoordinatesFileHeader3D;
00132 break;
00133 }
00134 default:
00135 {
00136 NEVER_REACHED;
00137 }
00138 }
00139
00140
00141
00142 for (unsigned i = 0; i < number_of_fields; i++)
00143 {
00144
00145 std::stringstream i_string;
00146 i_string << i+2;
00147 *p_elem_file<<i_string.str()<<") "<<mAdditionalFieldNames[i]<<" ,";
00148 switch (ELEMENT_DIM)
00149 {
00150 case 1:
00151 {
00152 *p_elem_file << CmguiAdditonalFieldHeader1D;
00153 break;
00154 }
00155 case 2:
00156 {
00157 *p_elem_file << CmguiAdditonalFieldHeader2D;
00158 break;
00159 }
00160 case 3:
00161 {
00162 *p_elem_file << CmguiAdditonalFieldHeader3D;
00163 break;
00164 }
00165 default:
00166 {
00167 NEVER_REACHED;
00168 }
00169 }
00170
00171 }
00172
00173
00174 for (unsigned item_num=0; item_num<this->GetNumElements(); item_num++)
00175 {
00176 std::vector<unsigned> current_element = this->GetNextElement().NodeIndices;
00177
00178 *p_elem_file << "Element:\t" << item_num+1 << " 0 0 Nodes:\t";
00179 for (unsigned i=0; i<(ELEMENT_DIM+1); i++)
00180 {
00181 *p_elem_file << current_element[i]+1 << "\t";
00182 }
00183
00184 *p_elem_file << "\n";
00185 }
00186 p_elem_file->close();
00187 }
00188
00189 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00190 void CmguiMeshWriter<ELEMENT_DIM,SPACE_DIM>::SetAdditionalFieldNames(std::vector<std::string>& rFieldNames)
00191 {
00192 mAdditionalFieldNames = rFieldNames;
00193 }
00194
00195 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00196 void CmguiMeshWriter<ELEMENT_DIM,SPACE_DIM>::WriteNodeFileHeader(out_stream& rpNodeFile)
00197 {
00198
00199 std::string comment = "! " + ChasteBuildInfo::GetProvenanceString();
00200 *rpNodeFile << comment;
00201
00202
00203 *rpNodeFile << "Group name: " << this->mGroupName << "\n";
00204 switch (SPACE_DIM)
00205 {
00206 case 1:
00207 {
00208 *rpNodeFile << CmguiNodeFileHeader1D;
00209 break;
00210 }
00211 case 2:
00212 {
00213 *rpNodeFile << CmguiNodeFileHeader2D;
00214 break;
00215 }
00216 case 3:
00217 {
00218 *rpNodeFile << CmguiNodeFileHeader3D;
00219 break;
00220 }
00221 default:
00222 {
00223 NEVER_REACHED;
00224 }
00225 }
00226 }
00227
00228 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00229 bool CmguiMeshWriter<ELEMENT_DIM,SPACE_DIM>::CompareCmguiFiles(std::string& rPath1, std::string& rPath2)
00230 {
00231 std::string compare_command = "diff --ignore-matching-lines=\"! \" ";
00232 compare_command += rPath1;
00233 compare_command += " ";
00234 compare_command += rPath2;
00235
00236
00237 if (system(compare_command.c_str()) == 0)
00238 {
00239 return true;
00240 }
00241 else
00242 {
00243 return false;
00244 }
00245 }
00246
00248
00250
00251 template class CmguiMeshWriter<1,1>;
00252 template class CmguiMeshWriter<1,2>;
00253 template class CmguiMeshWriter<1,3>;
00254 template class CmguiMeshWriter<2,2>;
00255 template class CmguiMeshWriter<2,3>;
00256 template class CmguiMeshWriter<3,3>;