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 #ifndef VTKWRITER_HPP_
00030 #define VTKWRITER_HPP_
00031
00032 #ifdef CHASTE_VTK
00033
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
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();
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
00087 mpVtkUnstructedMesh=vtkUnstructuredGrid::New();
00088 }
00089
00090
00091 void VtkWriter::MakeVtkMesh()
00092 {
00093 vtkPoints *p_pts = vtkPoints::New(VTK_DOUBLE);
00094
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
00103 mpVtkUnstructedMesh->SetPoints(p_pts);
00104 p_pts->Delete();
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();
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
00129
00130
00131 std::string vtk_file_name = this->mpOutputFileHandler->GetOutputDirectoryFullPath() + this->mBaseName+".vtu";
00132 p_writer->SetFileName(vtk_file_name.c_str());
00133
00134 p_writer->Write();
00135 p_writer->Delete();
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();
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();
00164
00165 }
00166 #endif //CHASTE_VTK
00167
00168 #endif