读/写vtk文件无法正常显示

时间:2013-11-08 08:00:03

标签: c++ vtk

我有一个非结构化的网格,我需要将其写入文件并在其他地方打开。原始文件看起来像这样;

Original file

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

File read from the written one 编写文件的代码是;

    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 filename =“ouputTest.vtu”;
        vtkSmartPointer reader = 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(); 

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

1 个答案:

答案 0 :(得分:1)

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

  1. 您的可视化管道与最初创建的管道类似。 (轮廓滤波器和类似的查找表)
  2. 加载了标量(您可以使用阅读器输出的PrintSelf方法知道这一点:reader-&gt; GetDataObject(0) - &gt; PrintSelf())。检查标量与保存前的元素是否相同。