0

我有一个非结构化网格,我需要将其写入文件并在其他地方打开。原始文件如下所示;

原始文件

当我写入文件并再次读取时,它显示如下,

从书面文件中读取的文件 编写文件的代码是;

    std::string filename = "ouputTest.vtu";

    //pre-constructed grid
    (vtkSmartPointer<vtkUnstructuredGrid>) grid;

    vtkSmartPointer<vtkGeometryFilter> geometryFilter = vtkSmartPointer<vtkGeometryFilter>::New();
    vtkSmartPointer<vtkPolyData> polydata;
    geometryFilter->SetInput(grid);
    geometryFilter->Update();
    polydata = geometryFilter->GetOutput();


    vtkSmartPointer<vtkDoubleArray> tempArray = vtkSmartPointer<vtkDoubleArray>::New();

    for(int i=0;i<4;i++)
    {
            tempArray->InsertNextValue(i/2.5);
    }
    polydata->GetPointData()->SetScalars(tempArray);
    grid->GetPointData()->SetScalars(tempArray);


    vtkSmartPointer<vtkLookupTable> colorLookupTable = vtkSmartPointer<vtkLookupTable>::New();

    colorLookupTable->SetTableRange(-100,500);
    double vmin,vmax;
    colorLookupTable->GetHueRange(vmin,vmax);
    if(vmin!=vmax)
    {
            colorLookupTable->SetHueRange(0.6667,0.0);
    }
    colorLookupTable->Build();

    vtkSmartPointer<vtkContourFilter> contourFilter = vtkSmartPointer<vtkContourFilter>::New();
    contourFilter->SetInputConnection(polydata->GetProducerPort());
    contourFilter->ComputeScalarsOn();
    contourFilter->GenerateValues(10,-100,500);

    vtkSmartPointer<vtkPolyDataMapper> mapper1 = vtkSmartPointer<vtkPolyDataMapper>::New();
    mapper1->SetInputConnection(contourFilter->GetOutputPort());
    mapper1->SetScalarRange(-100,500);
    mapper1->SetLookupTable(colorLookupTable);
    mapper1->ScalarVisibilityOn();

    vtkSmartPointer<vtkActor> actor1 = vtkSmartPointer<vtkActor>::New();
    actor1->SetMapper(mapper1);

    vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
    mapper->SetInput(polydata);
    mapper->ScalarVisibilityOn();
    mapper->InterpolateScalarsBeforeMappingOn();
    mapper->SetLookupTable(colorLookupTable);
    mapper->SetScalarRange(-100,500);

    vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
    actor->SetMapper(mapper);

    // Visualize
    vtkSmartPointer<vtkRenderer> renderer =vtkSmartPointer<vtkRenderer>::New();
    vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
    renderWindow->AddRenderer(renderer);

    vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
    renderWindowInteractor->SetRenderWindow(renderWindow);

    renderer->AddActor(actor1);
    renderer->AddActor(actor);
    renderer->SetBackground(.3, .6, .3); // Background color green

    // Write file
    vtkSmartPointer<vtkXMLUnstructuredGridWriter> writer =
            vtkSmartPointer<vtkXMLUnstructuredGridWriter>::New();
    writer->SetFileName(filename.c_str());
 #if VTK_MAJOR_VERSION <= 5
    writer->SetInput(grid);
 #else
    writer->SetInputData(grid);
 #endif
    writer->SetDataModeToAscii();
    writer->Write();
    renderWindow->Render();
    renderWindowInteractor->Start(); 

读取文件的代码是;

std::string 文件名 = "ouputTest.vtu";
vtkSmartPointer 阅读器 = vtkSmartPointer::New();

    reader->SetFileName(filename.c_str());
    reader->Update();

    vtkSmartPointer<vtkDataSetMapper> mapper = vtkSmartPointer<vtkDataSetMapper>::New();
    mapper->SetInputConnection(reader->GetOutputPort());

    vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
    actor->SetMapper(mapper);

    vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
    vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
    renderWindow->AddRenderer(renderer);

    vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
    renderWindowInteractor->SetRenderWindow(renderWindow);

    renderer->AddActor(actor);
    renderer->SetBackground(1, 1, 1);
    renderWindow->Render();
    renderWindowInteractor->Start(); 

我应该怎么做才能让它看起来像原来的一样。我不知道问题是文件写入还是读取。任何帮助,将不胜感激。谢谢。

4

1 回答 1

1

加载时,请确保以下内容:

  1. 您的可视化管道与您最初创建的管道相似。(轮廓过滤器和类似的查找表)
  2. 标量已加载(您可以使用阅读器输出的 PrintSelf 方法了解这一点:reader->GetDataObject(0)->PrintSelf())。检查标量是否具有与保存前相同的元素。
于 2013-11-22T14:27:42.363 回答