AbstractTetrahedralMeshWriter.cpp

00001 /*
00002 
00003 Copyright (C) University of Oxford, 2005-2009
00004 
00005 University of Oxford means the Chancellor, Masters and Scholars of the
00006 University of Oxford, having an administrative office at Wellington
00007 Square, Oxford OX1 2JD, UK.
00008 
00009 This file is part of Chaste.
00010 
00011 Chaste is free software: you can redistribute it and/or modify it
00012 under the terms of the GNU Lesser General Public License as published
00013 by the Free Software Foundation, either version 2.1 of the License, or
00014 (at your option) any later version.
00015 
00016 Chaste is distributed in the hope that it will be useful, but WITHOUT
00017 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
00019 License for more details. The offer of Chaste under the terms of the
00020 License is subject to the License being interpreted in accordance with
00021 English Law and subject to any action against the University of Oxford
00022 being under the jurisdiction of the English Courts.
00023 
00024 You should have received a copy of the GNU Lesser General Public License
00025 along with Chaste. If not, see <http://www.gnu.org/licenses/>.
00026 
00027 */
00028 
00029 #include "AbstractTetrahedralMeshWriter.hpp"
00030 #include "AbstractTetrahedralMesh.hpp"
00031 
00033 // Implementation
00035 
00036 
00037 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00038 AbstractTetrahedralMeshWriter<ELEMENT_DIM, SPACE_DIM>::AbstractTetrahedralMeshWriter(const std::string &rDirectory,
00039                    const std::string &rBaseName,
00040                    const bool clearOutputDir)
00041     : mBaseName(rBaseName)
00042 {
00043     mpOutputFileHandler = new OutputFileHandler(rDirectory, clearOutputDir);
00044 }
00045 
00046 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00047 AbstractTetrahedralMeshWriter<ELEMENT_DIM, SPACE_DIM>::~AbstractTetrahedralMeshWriter()
00048 {
00049     delete mpOutputFileHandler;
00050 }
00051 
00052 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00053 unsigned AbstractTetrahedralMeshWriter<ELEMENT_DIM, SPACE_DIM>::GetNumNodes()
00054 {
00055     return mNodeData.size();
00056 }
00057 
00058 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00059 unsigned AbstractTetrahedralMeshWriter<ELEMENT_DIM, SPACE_DIM>::GetNumElements()
00060 {
00061     return mElementData.size();
00062 }
00063 
00064 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00065 unsigned AbstractTetrahedralMeshWriter<ELEMENT_DIM, SPACE_DIM>::GetNumBoundaryFaces()
00066 {
00067     return mBoundaryFaceData.size();
00068 }
00069 
00070 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00071 unsigned AbstractTetrahedralMeshWriter<ELEMENT_DIM, SPACE_DIM>::GetNumBoundaryEdges()
00072 {
00073     return mBoundaryFaceData.size();
00074 }
00075 
00076 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00077 std::string AbstractTetrahedralMeshWriter<ELEMENT_DIM, SPACE_DIM>::GetOutputDirectory()
00078 {
00079     return mpOutputFileHandler->GetOutputDirectoryFullPath();
00080 }
00081 
00082 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00083 void AbstractTetrahedralMeshWriter<ELEMENT_DIM, SPACE_DIM>::SetNextNode(std::vector<double> nextNode)
00084 {
00085     assert(nextNode.size() == SPACE_DIM);
00086     mNodeData.push_back(nextNode);
00087 }
00088 
00089 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00090 void AbstractTetrahedralMeshWriter<ELEMENT_DIM, SPACE_DIM>::SetNextElement(std::vector<unsigned> nextElement)
00091 {
00092     assert(nextElement.size() == ELEMENT_DIM+1);
00093     mElementData.push_back(nextElement);
00094 }
00095 
00096 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00097 void AbstractTetrahedralMeshWriter<ELEMENT_DIM, SPACE_DIM>::SetNextBoundaryFace(std::vector<unsigned> nextFace)
00098 {
00099     assert(nextFace.size() == ELEMENT_DIM);
00100     mBoundaryFaceData.push_back(nextFace);
00101 }
00102 
00103 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00104 void AbstractTetrahedralMeshWriter<ELEMENT_DIM, SPACE_DIM>::WriteFilesUsingMesh(
00105      AbstractTetrahedralMesh<ELEMENT_DIM,SPACE_DIM>& rMesh)
00106 {
00107     NodeMap node_map(rMesh.GetNumAllNodes());
00108     unsigned new_index = 0;
00109     for (unsigned i=0; i<(unsigned)rMesh.GetNumAllNodes(); i++)
00110     {
00111         Node<SPACE_DIM> *p_node = rMesh.GetNode(i);
00112 
00113         if (p_node->IsDeleted() == false)
00114         {
00115             std::vector<double> coords(SPACE_DIM);
00116             for (unsigned j=0; j<SPACE_DIM; j++)
00117             {
00118                 coords[j] = p_node->GetPoint()[j];
00119             }
00120             SetNextNode(coords);
00121             node_map.SetNewIndex(i,new_index++);
00122         }
00123         else
00124         {
00125             node_map.SetDeleted(i);
00126         }
00127     }
00128     assert(new_index==(unsigned)rMesh.GetNumNodes());
00129 
00130     // Get an iterator over the elements of the mesh
00131     for (typename AbstractTetrahedralMesh<ELEMENT_DIM, SPACE_DIM>::ElementIterator iter = rMesh.GetElementIteratorBegin();
00132          iter != rMesh.GetElementIteratorEnd();
00133          ++iter)
00134     {
00135         std::vector<unsigned> indices(iter->GetNumNodes());
00136 
00137         for (unsigned j=0; j<indices.size(); j++)
00138         {
00139             unsigned old_index = iter->GetNodeGlobalIndex(j);
00140             indices[j] = node_map.GetNewIndex(old_index);
00141         }
00142         this->SetNextElement(indices);
00143     }
00144 
00145     // Get a iterator over the boundary elements of the mesh
00146     typename AbstractTetrahedralMesh<ELEMENT_DIM, SPACE_DIM>::BoundaryElementIterator boundary_iter =
00147         rMesh.GetBoundaryElementIteratorBegin();
00148     while (boundary_iter != rMesh.GetBoundaryElementIteratorEnd())
00149     {
00150         if ((*boundary_iter)->IsDeleted() == false)
00151         {
00152             std::vector<unsigned> indices(ELEMENT_DIM);
00153             for (unsigned j=0; j<ELEMENT_DIM; j++)
00154             {
00155                 unsigned old_index=(*boundary_iter)->GetNodeGlobalIndex(j);
00156                 indices[j] = node_map.GetNewIndex(old_index);
00157             }
00158             SetNextBoundaryFace(indices);
00159         }
00160         boundary_iter++;
00161     }
00162     WriteFiles();
00163 }
00164 
00165 
00166 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00167 void AbstractTetrahedralMeshWriter<ELEMENT_DIM, SPACE_DIM>::WriteFilesUsingMesh(
00168      const AbstractTetrahedralMesh<ELEMENT_DIM,SPACE_DIM>& rMesh)
00169 {
00170     NodeMap node_map(rMesh.GetNumAllNodes());
00171     unsigned new_index = 0;
00172     for (unsigned i=0; i<(unsigned)rMesh.GetNumAllNodes(); i++)
00173     {
00174         Node<SPACE_DIM> *p_node = rMesh.GetNode(i);
00175 
00176         if (p_node->IsDeleted() == false)
00177         {
00178             std::vector<double> coords(SPACE_DIM);
00179             for (unsigned j=0; j<SPACE_DIM; j++)
00180             {
00181                 coords[j] = p_node->GetPoint()[j];
00182             }
00183             SetNextNode(coords);
00184             node_map.SetNewIndex(i,new_index++);
00185         }
00186         else
00187         {
00188             node_map.SetDeleted(i);
00189         }
00190     }
00191     assert(new_index==(unsigned)rMesh.GetNumNodes());
00192 
00193     for (unsigned i=0; i<(unsigned)rMesh.GetNumAllElements(); i++)
00194     {
00195         Element<ELEMENT_DIM, SPACE_DIM> *p_element = rMesh.GetElement(i);
00196 
00197         if (p_element->IsDeleted() == false)
00198         {
00199             std::vector<unsigned> indices(p_element->GetNumNodes());
00200 
00201             for (unsigned j=0; j<indices.size(); j++)
00202             {
00203                 unsigned old_index = p_element->GetNodeGlobalIndex(j);
00204                 indices[j] = node_map.GetNewIndex(old_index);
00205             }
00206             this->SetNextElement(indices);
00207         }
00208     }
00209 
00210     for (unsigned i=0; i<(unsigned)rMesh.GetNumAllBoundaryElements(); i++)
00211     {
00212         BoundaryElement<ELEMENT_DIM-1, SPACE_DIM> *p_boundary_element = rMesh.GetBoundaryElement(i);
00213         if (p_boundary_element->IsDeleted() == false)
00214         {
00215             std::vector<unsigned> indices(ELEMENT_DIM);
00216             for (unsigned j=0; j<ELEMENT_DIM; j++)
00217             {
00218                 unsigned old_index=p_boundary_element->GetNodeGlobalIndex(j);
00219                 indices[j] = node_map.GetNewIndex(old_index);
00220             }
00221             SetNextBoundaryFace(indices);
00222         }
00223     }
00224     WriteFiles();
00225 }
00226 
00227 
00228 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00229 void AbstractTetrahedralMeshWriter<ELEMENT_DIM, SPACE_DIM>::WriteFilesUsingMeshReader(
00230     AbstractMeshReader<ELEMENT_DIM, SPACE_DIM>& rMeshReader)
00231 {
00232     for (unsigned i=0; i<rMeshReader.GetNumNodes(); i++)
00233     {
00234         SetNextNode(rMeshReader.GetNextNode());
00235     }
00236     for (unsigned i=0; i<rMeshReader.GetNumElements(); i++)
00237     {
00238         this->SetNextElement(rMeshReader.GetNextElementData().NodeIndices);
00239     }
00240     for (unsigned i=0; i<rMeshReader.GetNumFaces(); i++)
00241     {
00242         this->SetNextBoundaryFace(rMeshReader.GetNextFaceData().NodeIndices);
00243     }
00244     WriteFiles();
00245 }
00246 
00247 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00248 void AbstractTetrahedralMeshWriter<ELEMENT_DIM, SPACE_DIM>::WriteFilesUsingMeshReader(
00249     AbstractMeshReader<ELEMENT_DIM, SPACE_DIM>& rMeshReader,
00250     const std::vector<unsigned>& rNodePermutation)
00251 {
00252     if (rNodePermutation.size() == 0)
00253     {
00254        WriteFilesUsingMeshReader(rMeshReader);
00255     }
00256     else
00257     {
00258         mNodeData.resize(rMeshReader.GetNumNodes());
00259         for (unsigned i=0; i<rMeshReader.GetNumNodes(); i++)
00260         {
00261             assert(rNodePermutation[i] < rMeshReader.GetNumNodes());
00262             mNodeData[ rNodePermutation[i] ] = rMeshReader.GetNextNode();
00263         }
00264 
00265         for (unsigned i=0; i<rMeshReader.GetNumElements(); i++)
00266         {
00267             ElementData element = rMeshReader.GetNextElementData();
00268 
00269             for (unsigned local_index=0; local_index<element.NodeIndices.size(); local_index++)
00270             {
00271                 unsigned old_index = element.NodeIndices[local_index];
00272                 element.NodeIndices[local_index] = rNodePermutation[old_index];
00273             }
00274 
00275             SetNextElement(element.NodeIndices);
00276         }
00277 
00278         for (unsigned i=0; i<rMeshReader.GetNumFaces(); i++)
00279         {
00280             ElementData face = rMeshReader.GetNextFaceData();
00281 
00282             for (unsigned local_index=0; local_index<face.NodeIndices.size(); local_index++)
00283             {
00284                 unsigned old_index = face.NodeIndices[local_index];
00285                 face.NodeIndices[local_index] = rNodePermutation[old_index];
00286             }
00287 
00288             SetNextBoundaryFace(face.NodeIndices);
00289         }
00290         WriteFiles();
00291     }
00292 }
00293 
00294 
00296 // Explicit instantiation
00298 
00299 template class AbstractTetrahedralMeshWriter<1,1>;
00300 template class AbstractTetrahedralMeshWriter<1,2>;
00301 template class AbstractTetrahedralMeshWriter<1,3>;
00302 template class AbstractTetrahedralMeshWriter<2,2>;
00303 template class AbstractTetrahedralMeshWriter<2,3>;
00304 template class AbstractTetrahedralMeshWriter<3,3>;

Generated on Tue Aug 4 16:10:24 2009 for Chaste by  doxygen 1.5.5