VtkWriter.hpp

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 #ifndef VTKWRITER_HPP_
00030 #define VTKWRITER_HPP_
00031 
00032 #ifdef CHASTE_VTK 
00033 //Requires  "sudo aptitude install libvtk5-dev" or similar 
00034 #define _BACKWARD_BACKWARD_WARNING_H 1 //Cut out the strstream deprecated warning for now (gcc4.3)
00035 #include <vtkDoubleArray.h>
00036 #include <vtkCellData.h>
00037 #include <vtkPointData.h>
00038 #include <vtkTetra.h>
00039 #include <vtkUnstructuredGrid.h>
00040 #include <vtkUnstructuredGridWriter.h>
00041 #include <vtkXMLUnstructuredGridWriter.h>
00042 
00043 #include <vtkDataCompressor.h>
00044 #include "AbstractMeshWriter.hpp"
00045 
00046 
00047 
00054 class VtkWriter : public AbstractMeshWriter<3,3>
00055 {
00056 
00057 #ifdef CHASTE_VTK 
00058 //Requires  "sudo aptitude install libvtk5-dev" or similar 
00059     
00060 private:
00061     vtkUnstructuredGrid* mpVtkUnstructedMesh;
00062 
00063     void MakeVtkMesh();
00064 #endif //CHASTE_VTK
00065 public:
00066     VtkWriter(const std::string &rDirectory,
00067                 const std::string &rBaseName,
00068                 const bool &rCleanDirectory=true);
00069     void WriteFiles();
00070     void AddCellData(std::string name, std::vector<double> data);
00071     void AddPointData(std::string name, std::vector<double> data);
00072     virtual ~VtkWriter()
00073     {
00074         mpVtkUnstructedMesh->Delete(); //Reference counted
00075     }
00076 };
00077 
00078 
00079 VtkWriter::VtkWriter(const std::string &rDirectory,
00080                          const std::string &rBaseName,
00081                          const bool &rCleanDirectory)
00082         : AbstractMeshWriter<3,3>(rDirectory, rBaseName, rCleanDirectory)
00083 {
00084     this->mIndexFromZero=true;
00085     
00086     //Dubious, since we shouldn't yet know what any details of the mesh are.
00087     mpVtkUnstructedMesh=vtkUnstructuredGrid::New();
00088 }
00089 
00090 
00091 void VtkWriter::MakeVtkMesh()
00092 {
00093     vtkPoints *p_pts = vtkPoints::New(VTK_DOUBLE);
00094     //p_pts->SetDataTypeToDouble(); 
00095     p_pts->GetData()->SetName("Vertex positions");
00096     for (unsigned item_num=0; item_num<this->GetNumNodes(); item_num++)
00097     {
00098         std::vector<double> current_item = this->mNodeData[item_num];
00099         p_pts->InsertPoint(item_num, current_item[0], current_item[1], current_item[2]);
00100     }
00101   
00102     //mpVtkUnstructedMesh->Allocate(rMesh.GetNumNodes(), rMesh.GetNumNodes());
00103     mpVtkUnstructedMesh->SetPoints(p_pts); 
00104     p_pts->Delete(); //Reference counted    
00105     for (unsigned item_num=0; item_num<this->GetNumElements(); item_num++)
00106     {
00107         std::vector<unsigned> current_element = this->mElementData[item_num];
00108         vtkTetra * p_tetra = vtkTetra::New();
00109         vtkIdList * p_tetra_id_list = p_tetra->GetPointIds();
00110         for (int j = 0; j < 4; ++j)
00111         {
00112             p_tetra_id_list->SetId(j, current_element[j]);
00113         }
00114         mpVtkUnstructedMesh->InsertNextCell(p_tetra->GetCellType(), p_tetra_id_list);
00115         p_tetra->Delete(); //Reference counted
00116     }
00117     
00118 }    
00119 
00120 
00121 void VtkWriter::WriteFiles()
00122 {
00123     MakeVtkMesh();
00124     assert(mpVtkUnstructedMesh->CheckAttributes() == 0);
00125     vtkXMLUnstructuredGridWriter *p_writer = vtkXMLUnstructuredGridWriter::New();
00126     p_writer->SetInput(mpVtkUnstructedMesh);
00127     p_writer->SetDataMode(vtkXMLWriter::Appended); 
00128     //Not sure how the uninitialised stuff arises, but you can remove 
00129     //valgrind problems by removing compression:
00130     //p_writer->SetCompressor(NULL);
00131     std::string vtk_file_name = this->mpOutputFileHandler->GetOutputDirectoryFullPath() + this->mBaseName+".vtu";
00132     p_writer->SetFileName(vtk_file_name.c_str());
00133     //p_writer->PrintSelf(std::cout, vtkIndent());
00134     p_writer->Write();
00135     p_writer->Delete(); //Reference counted
00136 }
00137 
00138 void VtkWriter::AddCellData(std::string dataName, std::vector<double> dataPayload)
00139 {    
00140     vtkDoubleArray *p_scalars = vtkDoubleArray::New();
00141     p_scalars->SetName(dataName.c_str());    
00142     for (unsigned i=0; i<dataPayload.size(); i++)
00143     {
00144         p_scalars->InsertNextValue(dataPayload[i]);
00145     }
00146     
00147     vtkCellData *p_cell_data = mpVtkUnstructedMesh->GetCellData();
00148     p_cell_data->AddArray(p_scalars);
00149     p_scalars->Delete(); //Reference counted
00150 }
00151 
00152 void VtkWriter::AddPointData(std::string dataName, std::vector<double> dataPayload)
00153 {
00154     vtkDoubleArray *p_scalars = vtkDoubleArray::New();
00155     p_scalars->SetName(dataName.c_str());    
00156     for (unsigned i=0; i<dataPayload.size(); i++)
00157     {
00158         p_scalars->InsertNextValue(dataPayload[i]);
00159     }
00160     
00161     vtkPointData *p_point_data = mpVtkUnstructedMesh->GetPointData();
00162     p_point_data->AddArray(p_scalars);
00163     p_scalars->Delete(); //Reference counted
00164     
00165 }
00166 #endif //CHASTE_VTK 
00167 
00168 #endif /*VTKWRITER_HPP_*/

Generated on Wed Mar 18 12:51:56 2009 for Chaste by  doxygen 1.5.5