#include #include "vtkObjectFactory.h" #include #include #include // #include #include #include #include #include #include #include #include #include #include #include #include #include //---------------------------------------------------------------------------- vtkStandardNewMacro( vtkMetaSurfaceMesh ); vtkCxxRevisionMacro(vtkMetaSurfaceMesh, "$Revision: 1.6 $"); //---------------------------------------------------------------------------- vtkMetaSurfaceMesh::vtkMetaSurfaceMesh() { this->Type = vtkMetaDataSet::VTK_META_SURFACE_MESH; } //---------------------------------------------------------------------------- vtkMetaSurfaceMesh::~vtkMetaSurfaceMesh() { } //---------------------------------------------------------------------------- void vtkMetaSurfaceMesh::Initialize (void) { this->Superclass::Initialize(); if (!this->DataSet) return; vtkProperty* property = vtkProperty::SafeDownCast(this->GetProperty()); if (!property) { property = vtkProperty::New(); this->SetProperty (property); property->Delete(); } } //---------------------------------------------------------------------------- vtkPolyData* vtkMetaSurfaceMesh::GetPolyData (void) const { if (!this->DataSet) return NULL; return vtkPolyData::SafeDownCast (this->DataSet); } //---------------------------------------------------------------------------- void vtkMetaSurfaceMesh::ReadVtkFile (const char* filename) { vtkPolyDataReader* reader = vtkPolyDataReader::New(); reader->SetFileName (filename); try { reader->Update(); this->SetDataSet (reader->GetOutput()); } catch (vtkErrorCode::ErrorIds error) { reader->Delete(); throw error; } reader->Delete(); } //---------------------------------------------------------------------------- void vtkMetaSurfaceMesh::Read (const char* filename) { unsigned long format = vtkMetaSurfaceMesh::CanReadFile (filename); try { std::cout << "Reading : " << filename <<"... "; switch (format) { case vtkMetaSurfaceMesh::FILE_IS_VTK : this->ReadVtkFile (filename); break; case vtkMetaSurfaceMesh::FILE_IS_MESH : this->ReadMeshFile (filename); break; default : vtkErrorMacro(<<"unknown dataset type : "<SetFilePath ((vtksys::SystemTools::GetFilenamePath (filename)).c_str()); } //---------------------------------------------------------------------------- void vtkMetaSurfaceMesh::WriteVtkFile (const char* filename) { if (!this->DataSet) { vtkErrorMacro(<<"No DataSet to write"<DataSet); if (!c_mesh) { vtkErrorMacro(<<"DataSet is not a polydata object"<SetFileName (filename); try { writer->SetInput (c_mesh); writer->Write(); writer->Delete(); } catch (vtkErrorCode::ErrorIds error) { writer->Delete(); throw error; } this->SetFilePath ((vtksys::SystemTools::GetFilenamePath (filename)).c_str()); } //---------------------------------------------------------------------------- void vtkMetaSurfaceMesh::Write (const char* filename) { try { std::cout<<"writing "<WriteVtkFile (filename); std::cout<<"done."<SetFileName (filename); if (reader->IsFilePolyData ()) { reader->Delete(); return vtkMetaSurfaceMesh::FILE_IS_VTK; } reader->Delete(); } catch (vtkErrorCode::ErrorIds) { } return 0; } void vtkMetaSurfaceMesh::ReadMeshFile (const char* filename) { std::ifstream file (filename ); char str[256]; if(file.fail()) { vtkErrorMacro("File not found\n"); throw vtkErrorCode::FileNotFoundError; } vtkPoints* points = vtkPoints::New(); vtkUnsignedShortArray* pointarray = vtkUnsignedShortArray::New(); vtkUnsignedShortArray* cellarray = vtkUnsignedShortArray::New(); vtkPolyData* outputmesh = vtkPolyData::New(); unsigned short ref = 0; file >> str; while( (strcmp (str, "Vertices") != 0) && strcmp (str, "End") != 0) { file >> str; } if(strcmp (str, "End") == 0) { vtkErrorMacro(<<"Unexpected end of file"<> NVertices; points->SetNumberOfPoints (NVertices); pointarray->SetName ("Point array"); pointarray->Allocate(NVertices); // read vertex position for(unsigned int i=0; i> pos[0] >> pos[1] >> pos[2] >> ref; points->SetPoint (i, pos[0], pos[1], pos[2]); pointarray->InsertNextValue(ref); } outputmesh->SetPoints (points); points->Delete(); if (outputmesh->GetPointData()) { outputmesh->GetPointData()->AddArray (pointarray); } pointarray->Delete(); file >> str; while( (strcmp (str, "Triangles") != 0) && strcmp (str, "End") != 0) { file >> str; } if(strcmp (str, "End") == 0) { vtkErrorMacro(<<"Unexpected end of file"<> NTriangles; outputmesh->Allocate (NTriangles); cellarray->SetName ("Zones"); cellarray->Allocate(NTriangles); for(unsigned int i=0; i> ids[0] >> ids[1] >> ids[2] >> ref; vtkIdList* idlist = vtkIdList::New(); idlist->InsertNextId (ids[0]-1); idlist->InsertNextId (ids[1]-1); idlist->InsertNextId (ids[2]-1); outputmesh->InsertNextCell (VTK_TRIANGLE, idlist); idlist->Delete(); cellarray->InsertNextValue(ref); } if (outputmesh->GetCellData()) { outputmesh->GetCellData()->AddArray (cellarray); } cellarray->Delete(); this->SetDataSet (outputmesh); outputmesh->Delete(); }