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
00030
00031
00032
00033
00034
00035
00036
00037 #ifdef CHASTE_VTK
00038
00039 #include "VtkMeshReader.hpp"
00040 #include "Exception.hpp"
00041
00042 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00043 VtkMeshReader<ELEMENT_DIM,SPACE_DIM>::VtkMeshReader(std::string pathBaseName) :
00044 mIndexFromZero(true),
00045 mNumNodes(0),
00046 mNumElements(0),
00047 mNumFaces(0),
00048 mNodesRead(0),
00049 mElementsRead(0),
00050 mFacesRead(0),
00051 mBoundaryFacesRead(0),
00052 mNumElementAttributes(0),
00053 mNumFaceAttributes(0),
00054 mOrderOfElements(1),
00055 mNodesPerElement(4)
00056 {
00057 vtkXMLUnstructuredGridReader* vtk_xml_unstructured_grid_reader;
00058
00059
00060 mVtuFile.open(pathBaseName.c_str());
00061 if ( !mVtuFile.is_open() )
00062 {
00063 EXCEPTION("Could not open VTU file: " + pathBaseName);
00064 }
00065 mVtuFile.close();
00066
00067
00068 vtk_xml_unstructured_grid_reader = vtkXMLUnstructuredGridReader::New();
00069 vtk_xml_unstructured_grid_reader->SetFileName( pathBaseName.c_str() );
00070 vtk_xml_unstructured_grid_reader->Update();
00071 mpVtkUnstructuredGrid = vtk_xml_unstructured_grid_reader->GetOutput();
00072
00073 mNumNodes = vtk_xml_unstructured_grid_reader->GetNumberOfPoints();
00074 mNumElements = vtk_xml_unstructured_grid_reader->GetNumberOfCells();
00075
00076
00077 mpVtkGeometryFilter = vtkGeometryFilter::New();
00078 mpVtkGeometryFilter->SetInput(mpVtkUnstructuredGrid);
00079 mpVtkGeometryFilter->Update();
00080
00081 mNumFaces = mpVtkGeometryFilter->GetOutput()->GetNumberOfCells();
00082
00083 vtk_xml_unstructured_grid_reader->Delete();
00084 }
00085
00086 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00087 VtkMeshReader<ELEMENT_DIM,SPACE_DIM>::VtkMeshReader(vtkUnstructuredGrid* p_vtkUnstructuredGrid) :
00088 mIndexFromZero(true),
00089 mNumNodes(0),
00090 mNumElements(0),
00091 mNumFaces(0),
00092 mNodesRead(0),
00093 mElementsRead(0),
00094 mFacesRead(0),
00095 mBoundaryFacesRead(0),
00096 mNumElementAttributes(0),
00097 mNumFaceAttributes(0),
00098 mOrderOfElements(1),
00099 mNodesPerElement(4)
00100 {
00101 mpVtkUnstructuredGrid = p_vtkUnstructuredGrid;
00102
00103 mNumNodes = mpVtkUnstructuredGrid->GetNumberOfPoints();
00104 mNumElements = mpVtkUnstructuredGrid->GetNumberOfCells();
00105
00106
00107 mpVtkGeometryFilter = vtkGeometryFilter::New();
00108 mpVtkGeometryFilter->SetInput(mpVtkUnstructuredGrid);
00109 mpVtkGeometryFilter->Update();
00110
00111 mNumFaces = mpVtkGeometryFilter->GetOutput()->GetNumberOfCells();
00112 }
00113
00114 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00115 VtkMeshReader<ELEMENT_DIM,SPACE_DIM>::~VtkMeshReader()
00116 {
00117 mpVtkGeometryFilter->Delete();
00118 }
00119
00120 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00121 unsigned VtkMeshReader<ELEMENT_DIM,SPACE_DIM>::GetNumElements() const
00122 {
00123 return mNumElements;
00124 }
00125
00126 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00127 unsigned VtkMeshReader<ELEMENT_DIM,SPACE_DIM>::GetNumNodes() const
00128 {
00129 return mNumNodes;
00130 }
00131
00132 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00133 unsigned VtkMeshReader<ELEMENT_DIM,SPACE_DIM>::GetNumFaces() const
00134 {
00135 return mNumFaces;
00136 }
00137
00138 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00139 unsigned VtkMeshReader<ELEMENT_DIM,SPACE_DIM>::GetNumEdges() const
00140 {
00141 return mNumFaces;
00142 }
00143
00144 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00145 unsigned VtkMeshReader<ELEMENT_DIM,SPACE_DIM>::GetNumElementAttributes() const
00146 {
00147 return mNumElementAttributes;
00148 }
00149
00150 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00151 unsigned VtkMeshReader<ELEMENT_DIM,SPACE_DIM>::GetNumFaceAttributes() const
00152 {
00153 return mNumFaceAttributes;
00154 }
00155
00156 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00157 void VtkMeshReader<ELEMENT_DIM,SPACE_DIM>::Reset()
00158 {
00159
00160
00161
00162
00163 mNodesRead=0;
00164 mElementsRead=0;
00165 mFacesRead=0;
00166 mBoundaryFacesRead=0;
00167 }
00168
00169 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00170 void VtkMeshReader<ELEMENT_DIM,SPACE_DIM>::Initialize()
00171 {
00172 mpVtkUnstructuredGrid->Initialize();
00173 }
00174
00175 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00176 std::vector<double> VtkMeshReader<ELEMENT_DIM,SPACE_DIM>::GetNextNode()
00177 {
00178 if ( mNodesRead >= mNumNodes )
00179 {
00180 EXCEPTION( "Trying to read data for a node that doesn't exist" );
00181 }
00182
00183 std::vector<double> next_node;
00184
00185 for (unsigned i = 0; i < 3; i++)
00186 {
00187 next_node.push_back( mpVtkUnstructuredGrid->GetPoint(mNodesRead)[i] );
00188 }
00189
00190 mNodesRead++;
00191 return next_node;
00192 }
00193
00194 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00195 ElementData VtkMeshReader<ELEMENT_DIM,SPACE_DIM>::GetNextElementData()
00196 {
00197 if ( mElementsRead >= mNumElements )
00198 {
00199 EXCEPTION( "Trying to read data for an element that doesn't exist" );
00200 }
00201
00202 if( !mpVtkUnstructuredGrid->GetCell(mElementsRead)->IsA("vtkTetra") )
00203 {
00204 EXCEPTION("Element is not a vtkTetra");
00205 }
00206
00207 ElementData next_element_data;
00208
00209 for (unsigned i = 0; i < mNodesPerElement; i++)
00210 {
00211 next_element_data.NodeIndices.push_back(mpVtkUnstructuredGrid->GetCell(mElementsRead)->GetPointId(i));
00212 }
00213
00214
00215 next_element_data.AttributeValue = 0;
00216
00217 mElementsRead++;
00218 return next_element_data;
00219 }
00220
00221 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00222 ElementData VtkMeshReader<ELEMENT_DIM,SPACE_DIM>::GetNextFaceData()
00223 {
00224 if ( mBoundaryFacesRead >= mNumFaces)
00225 {
00226 EXCEPTION( "Trying to read data for a boundary element that doesn't exist");
00227 }
00228
00229 ElementData next_face_data;
00230
00231 for (unsigned i = 0; i < 3; i++)
00232 {
00233 next_face_data.NodeIndices.push_back(mpVtkGeometryFilter->GetOutput()->GetCell(mBoundaryFacesRead)->GetPointId(i));
00234 }
00235
00236 mBoundaryFacesRead++;
00237 return next_face_data;
00238 }
00239
00240
00241 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00242 std::vector<double> VtkMeshReader<ELEMENT_DIM,SPACE_DIM>::GetCellData(std::string dataName)
00243 {
00244 vtkCellData *p_cell_data = mpVtkUnstructuredGrid->GetCellData();
00245
00246 if ( !p_cell_data->HasArray(dataName.c_str()) )
00247 {
00248 EXCEPTION("No cell data '" + dataName + "'");
00249 }
00250
00251 vtkDataArray *p_scalars = p_cell_data->GetArray( dataName.c_str() );
00252 std::vector<double> data_payload;
00253
00254 for (unsigned i = 0; i < mNumElements; i++)
00255 {
00256 data_payload.push_back( p_scalars->GetTuple(i)[0] );
00257 }
00258
00259 return data_payload;
00260 }
00261
00262 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00263 std::vector<double> VtkMeshReader<ELEMENT_DIM,SPACE_DIM>::GetPointData(std::string dataName)
00264 {
00265 vtkPointData *p_point_data = mpVtkUnstructuredGrid->GetPointData();
00266
00267 if ( !p_point_data->HasArray(dataName.c_str()) )
00268 {
00269 EXCEPTION("No point data '" + dataName + "'");
00270 }
00271
00272 vtkDataArray *p_scalars = p_point_data->GetArray( dataName.c_str() );
00273 std::vector<double> data_payload;
00274
00275 for (unsigned i = 0; i < mNumNodes; i++)
00276 {
00277 data_payload.push_back( p_scalars->GetTuple(i)[0] );
00278 }
00279
00280 return data_payload;
00281 }
00282
00283 template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00284 vtkUnstructuredGrid* VtkMeshReader<ELEMENT_DIM,SPACE_DIM>::OutputMeshAsVtkUnstructuredGrid()
00285 {
00286 return mpVtkUnstructuredGrid;
00287 }
00288
00290
00292
00293 template class VtkMeshReader<1,1>;
00294 template class VtkMeshReader<1,2>;
00295 template class VtkMeshReader<1,3>;
00296 template class VtkMeshReader<2,2>;
00297 template class VtkMeshReader<2,3>;
00298 template class VtkMeshReader<3,3>;
00299 #endif // CHASTE_VTK