#include #include "vtkObjectFactory.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include // for sort algorithm //---------------------------------------------------------------------------- vtkStandardNewMacro( vtkMetaDataSetSequence ); vtkCxxRevisionMacro(vtkMetaDataSetSequence, "$Revision: 1.4 $"); //---------------------------------------------------------------------------- vtkMetaDataSetSequence::vtkMetaDataSetSequence() { this->SequenceDuration = 2.0; this->CurrentId = -1; this->Type = vtkMetaDataSet::VTK_META_UNKNOWN; this->SameGeometryFlag = true; } //---------------------------------------------------------------------------- vtkMetaDataSetSequence::~vtkMetaDataSetSequence() { for (unsigned int i=0; iMetaDataSetList.size(); i++) this->MetaDataSetList[i]->Delete(); this->MetaDataSetList.clear(); } //---------------------------------------------------------------------------- void vtkMetaDataSetSequence::Initialize (void) { this->Superclass::Initialize(); // if (this->DataSet) // this->DataSet->GetPointData()->CopyScalarsOn(); } //---------------------------------------------------------------------------- void vtkMetaDataSetSequence::Read(const char* dirname) { vtkDirectory* directory = vtkDirectory::New(); int ret = directory->Open(dirname); if (!ret) { vtkErrorMacro(<<"Cannot open directory"< list; std::string firstfilename; if (directory->GetNumberOfFiles() < 2) { vtkErrorMacro(<<"not enougth files in directory \n cannot build sequence !"<GetNumberOfFiles(); i++) { if ( (strcmp(directory->GetFile (i), ".") == 0 ) || (strcmp(directory->GetFile (i), "..") == 0 )) continue; if (!firstfilename.size()) firstfilename = directoryname+directory->GetFile (i); list.push_back((directoryname+directory->GetFile (i))); } std::sort(list.begin(), list.end()); unsigned long type = vtkMetaDataSet::VTK_META_UNKNOWN; if (vtkMetaSurfaceMesh::CanReadFile (firstfilename.c_str())) type = vtkMetaDataSet::VTK_META_SURFACE_MESH; else if (vtkMetaVolumeMesh::CanReadFile (firstfilename.c_str())) type = vtkMetaDataSet::VTK_META_VOLUME_MESH; else if (vtkMetaImageData::CanReadFile (firstfilename.c_str())) type = vtkMetaDataSet::VTK_META_IMAGE_DATA; else { vtkErrorMacro(<<"unable to open the first file : "<SequenceDuration; if (duration <= 0) { duration = 2.5; } for (unsigned int i = 0; i < list.size(); i++) { vtkMetaDataSet* metadataset = NULL; std::string filename = list[i]; double time = (double)(i)*duration/(double)(list.size()); switch(type) { case vtkMetaDataSet::VTK_META_SURFACE_MESH: metadataset = vtkMetaSurfaceMesh::New(); break; case vtkMetaDataSet::VTK_META_VOLUME_MESH: metadataset = vtkMetaVolumeMesh::New(); break; case vtkMetaDataSet::VTK_META_IMAGE_DATA: metadataset = vtkMetaImageData::New(); break; } try { metadataset->Read(filename.c_str()); metadataset->SetTime (time); metadataset->SetName((vtksys::SystemTools::GetFilenameName (filename.c_str())).c_str()); this->AddMetaDataSet(metadataset); metadataset->Delete(); } catch (vtkErrorCode::ErrorIds) { // we don't throw exception as we want to continue scanning the directory std::cerr<<"skipping file "<Delete(); } //---------------------------------------------------------------------------- void vtkMetaDataSetSequence::Write(const char* dirname) { std::string directoryname = dirname; std::string::size_type slashpos; // check slash position #ifdef WIN32 slashpos = directoryname.rfind ("\\"); // last occurence of "/" if (slashpos != directoryname.size()-1) directoryname+="\\"; #else slashpos = directoryname.rfind ("/"); // last occurence of "/" if (slashpos != directoryname.size()-1) directoryname+="/"; #endif for (unsigned int i=0; iGetNumberOfMetaDataSets(); i++) { try { vtkMetaDataSet* metadataset = this->GetMetaDataSet (i); std::string filename = directoryname+metadataset->GetName(); metadataset->Write (filename.c_str()); } catch (vtkErrorCode::ErrorIds error) { throw error; } } } //---------------------------------------------------------------------------- void vtkMetaDataSetSequence::BuildSequenceFromGeometry(const char* filename, unsigned int Niter) { unsigned long type = vtkMetaDataSet::VTK_META_UNKNOWN; if (vtkMetaSurfaceMesh::CanReadFile (filename)) type = vtkMetaDataSet::VTK_META_SURFACE_MESH; else if (vtkMetaVolumeMesh::CanReadFile (filename)) type = vtkMetaDataSet::VTK_META_VOLUME_MESH; else { vtkErrorMacro(<<"unknown file type (should be a geometry) : "<Read(filename); mainmetadataset->SetTime (0.0); mainmetadataset->SetName((vtksys::SystemTools::GetFilenameName (filename)).c_str()); this->BuildMetaDataSetFromMetaDataSet(mainmetadataset); mainmetadataset->Delete(); } catch (vtkErrorCode::ErrorIds error) { mainmetadataset->Delete(); vtkErrorMacro(<<"error in reading."<DataSet); double duration = this->SequenceDuration; for (unsigned int i=0; iGetName() << "#" << i+1; metadataset->SetName (os.str().c_str()); metadataset->SetTime (time); pointset->CopyStructure (mainpointset); metadataset->SetDataSet(pointset); try { this->AddMetaDataSet (metadataset); } catch (vtkErrorCode::ErrorIds) { pointset->Delete(); metadataset->Delete(); vtkWarningMacro(<<"Error while adding dataset, skipping..."<Delete(); metadataset->Delete(); } } //---------------------------------------------------------------------------- void vtkMetaDataSetSequence::AddMetaDataSet (vtkMetaDataSet* metadataset) { if (!metadataset) { vtkErrorMacro(<<"NULL object !"<Type == vtkMetaDataSet::VTK_META_UNKNOWN) this->Type = metadataset->GetType(); if ( metadataset->GetType() != this->Type) { vtkErrorMacro(<<"Cannot add heterogeneous type datasets to sequence !"<GetImageData(); vtkImageData* image2 = vtkImageData::SafeDownCast (this->GetDataSet()); if (image1 && image2) { if ((image1->GetDimensions()[0] != image2->GetDimensions()[0]) || (image1->GetDimensions()[1] != image2->GetDimensions()[1]) || (image1->GetDimensions()[2] != image2->GetDimensions()[2]) ) { vtkErrorMacro(<<"Images must have the same dimensions !"<::iterator it; bool inserted = false; for (it = this->MetaDataSetList.begin(); it != this->MetaDataSetList.end(); it++) { if ((*it)->GetTime() > metadataset->GetTime()) { this->MetaDataSetList.insert(it, metadataset); inserted = true; } } if (!inserted) { this->MetaDataSetList.push_back (metadataset); } metadataset->Register(this); try { if (!this->GetDataSet()) { this->Time = metadataset->GetTime(); this->CurrentId = this->MetaDataSetList.size() - 1; this->BuildMetaDataSetFromMetaDataSet (metadataset); } this->ComputeSequenceDuration(); } catch (vtkErrorCode::ErrorIds error) { throw error; } } //---------------------------------------------------------------------------- void vtkMetaDataSetSequence::RemoveMetaDataSet (vtkMetaDataSet* metadataset) { if (!metadataset) return; std::vector templist = this->MetaDataSetList; this->MetaDataSetList.clear(); for (unsigned int i=0; iMetaDataSetList.push_back (metadataset); else { metadataset->UnRegister(this); this->ComputeSequenceDuration(); } } if (this->MetaDataSetList.size() == 0) { this->Type = vtkMetaDataSet::VTK_META_UNKNOWN; if (this->DataSet) { this->DataSet->Delete(); } } } //---------------------------------------------------------------------------- void vtkMetaDataSetSequence::RemoveAllMetaDataSets (void) { for (unsigned int i=0; iMetaDataSetList.size(); i++) this->MetaDataSetList[i]->UnRegister(this); this->MetaDataSetList.clear(); this->ComputeSequenceDuration(); this->Type = vtkMetaDataSet::VTK_META_UNKNOWN; if (this->DataSet) { this->DataSet->Delete(); } } //---------------------------------------------------------------------------- vtkMetaDataSet* vtkMetaDataSetSequence::GetMetaDataSet (unsigned int i) { if (i<0 || i>=this->MetaDataSetList.size()) return NULL; return this->MetaDataSetList[i]; } //---------------------------------------------------------------------------- bool vtkMetaDataSetSequence::HasMetaDataSet (vtkMetaDataSet* metadataset) { for (unsigned int i=0; iMetaDataSetList.size(); i++) { if (this->MetaDataSetList[i] == metadataset) return true; } return false; } //---------------------------------------------------------------------------- double vtkMetaDataSetSequence::GetRelativeTime (double time) { if (this->MetaDataSetList.size() == 0) return 0.0; if (this->SequenceDuration <= 0 ) return 0; double t = time; while (t > (this->SequenceDuration + 0.000001) ) { t -= this->SequenceDuration; } return (t); } //---------------------------------------------------------------------------- void vtkMetaDataSetSequence::BuildMetaDataSetFromMetaDataSet (vtkMetaDataSet* metadataset) { if (this->DataSet) { this->DataSet->Delete(); this->DataSet = NULL; } this->CopyInformation(metadataset); vtkDataSet* dataset = NULL; switch (metadataset->GetDataSet()->GetDataObjectType()) { case VTK_IMAGE_DATA : dataset = vtkImageData::New(); break; case VTK_POLY_DATA : dataset = vtkPolyData::New(); break; case VTK_UNSTRUCTURED_GRID : dataset = vtkUnstructuredGrid::New(); break; default: vtkErrorMacro(<<"Unknown type !"<DeepCopy (metadataset->GetDataSet()); this->SetDataSet(dataset); dataset->Delete(); } //---------------------------------------------------------------------------- double vtkMetaDataSetSequence::GetMinTime() const { double ret = 0; for (unsigned int i=0; iMetaDataSetList.size(); i++) if ( ret > this->MetaDataSetList[i]->GetTime()) ret = this->MetaDataSetList[i]->GetTime(); return ret; } //---------------------------------------------------------------------------- double vtkMetaDataSetSequence::GetMaxTime() const { double ret = 0; for (unsigned int i=0; iMetaDataSetList.size(); i++) if ( ret < this->MetaDataSetList[i]->GetTime()) ret = this->MetaDataSetList[i]->GetTime(); return ret; } //---------------------------------------------------------------------------- double vtkMetaDataSetSequence::GetTimeResolution() { this->ComputeSequenceDuration(); double ret = this->SequenceDuration / (double)this->GetNumberOfMetaDataSets(); return ret; } //---------------------------------------------------------------------------- void vtkMetaDataSetSequence::ComputeSequenceDuration (void) { if (this->MetaDataSetList.size() < 2) { this->SequenceDuration = this->GetMaxTime(); return; } double step = (this->GetMaxTime() - this->GetMinTime())/(double)(this->GetNumberOfMetaDataSets()-1); this->SequenceDuration = this->GetMaxTime() - this->GetMinTime() + step; } //---------------------------------------------------------------------------- void vtkMetaDataSetSequence::UpdateToTime (double time) { if (this->MetaDataSetList.size() == 0) return; int id = -1; for (unsigned int i=0; iMetaDataSetList.size(); i++) { if (time >= this->MetaDataSetList[i]->GetTime()) id = i; } if (id < 0 || id == (int)this->CurrentId) return; this->UpdateToIndex ((unsigned int)(id)); } //---------------------------------------------------------------------------- void vtkMetaDataSetSequence::UpdateToIndex (unsigned int id) { if (this->MetaDataSetList.size() == 0) return; if (id < 0) id = 0; if (id >= this->MetaDataSetList.size()) id = this->MetaDataSetList.size()-1; double time = this->MetaDataSetList[id]->GetTime(); vtkDataSet* datasettoshow = this->MetaDataSetList[id]->GetDataSet(); if (!datasettoshow) return; if (datasettoshow->GetPointData() && datasettoshow->GetPointData()->GetNumberOfArrays()) { for (int i=0; iGetPointData()->GetNumberOfArrays(); i++) { vtkDataArray* array = datasettoshow->GetPointData()->GetArray (i); if ( array->GetName() ) { if (this->GetDataSet()->GetPointData()->HasArray (array->GetName()) ) this->GetDataSet()->GetPointData()->RemoveArray ( array->GetName() ); this->GetDataSet()->GetPointData()->AddArray (array); } } vtkImageData* imagetoshow = vtkImageData::SafeDownCast (datasettoshow); vtkImageData* imagetochange = vtkImageData::SafeDownCast (this->GetDataSet()); if (imagetoshow && imagetochange && imagetoshow->GetPointData()->GetScalars()) { imagetochange->GetPointData()->SetScalars (imagetoshow->GetPointData()->GetScalars()); } } vtkPolyData* polydatatoshow = vtkPolyData::SafeDownCast (datasettoshow); vtkPolyData* polydatatochange = vtkPolyData::SafeDownCast (this->GetDataSet()); vtkUnstructuredGrid* unstructuredgridtoshow = vtkUnstructuredGrid::SafeDownCast (datasettoshow); vtkUnstructuredGrid* unstructuredgridtochange = vtkUnstructuredGrid::SafeDownCast (this->GetDataSet()); if (polydatatoshow && polydatatochange) { if (!this->SameGeometryFlag) polydatatochange->SetPolys (polydatatoshow->GetPolys()); polydatatochange->SetPoints(polydatatoshow->GetPoints()); } else if (unstructuredgridtoshow && unstructuredgridtochange) { if (!this->SameGeometryFlag) { int t = unstructuredgridtoshow->GetCellType (0); unstructuredgridtochange->SetCells(t, unstructuredgridtoshow->GetCells()); } unstructuredgridtochange->SetPoints(unstructuredgridtoshow->GetPoints()); } this->CurrentId = id; this->SetTime (time); } //---------------------------------------------------------------------------- void vtkMetaDataSetSequence::ColorByArray(vtkDataArray* array) { if (!this->MetaDataSetList.size()) return; bool array_is_in_points = false; if (this->DataSet->GetPointData()->HasArray (array->GetName())) array_is_in_points = true; double min = 0, max = 0; for (unsigned int i=0; iMetaDataSetList.size(); i++) { vtkDataArray* junk; if (array_is_in_points) junk = this->MetaDataSetList[i]->GetDataSet()->GetPointData()->GetArray (array->GetName()); else junk = this->MetaDataSetList[i]->GetDataSet()->GetCellData()->GetArray (array->GetName()); if (!junk) continue; if (min > junk->GetRange()[0]) min = junk->GetRange()[0]; if (max < junk->GetRange()[1]) max = junk->GetRange()[1]; vtkLookupTable* lut = array->GetLookupTable(); if (lut) { junk->SetLookupTable (lut); } } for (int i=0; iActorList->GetNumberOfItems(); i++) { vtkActor* actor = this->GetActor (i); if (!actor) continue; vtkMapper* mapper = actor->GetMapper(); if (!array_is_in_points) mapper->SetScalarModeToUseCellFieldData(); else mapper->SetScalarModeToUsePointFieldData(); vtkLookupTable* lut = array->GetLookupTable(); if (lut) { lut->SetRange (min, max); mapper->SetLookupTable (lut); } mapper->SetScalarRange (min, max); mapper->SelectColorArray (array->GetName()); } } //---------------------------------------------------------------------------- void vtkMetaDataSetSequence::ReadPointData (unsigned int iter, const char* filename, const char* name) { if (iter < 0 || iter >= this->MetaDataSetList.size()) return; try { this->MetaDataSetList[iter]->ReadPointData (filename, name); } catch (vtkErrorCode::ErrorIds error) { throw error; } this->UpdateToIndex (0); } //---------------------------------------------------------------------------- void vtkMetaDataSetSequence::PrintSelf(ostream& os, vtkIndent indent) { this->Superclass::PrintSelf(os,indent); os << indent << "name \t: " << this->GetName() << endl; os << indent << "delay \t: " << this->GetTimeResolution() << endl; os << indent << "duration \t: " << this->SequenceDuration << endl; os << indent << "type \t: " << this->Type << endl; os << indent << "number of items \t: " << this->GetNumberOfMetaDataSets() << endl; }