解决:下面的cxx文件正在按预期工作。感谢@tomj,因为我也使用了他的想法。

我有一个数据文件(.vtk)文件,可从中读取非结构化网格。如何将单元格属性显示为标签?

cxx:

#include <vtkLookupTable.h>
#include <vtkCellData.h>
#include <vtkSmartPointer.h>
#include <vtkActor2D.h>
#include <vtkProperty.h>
#include <vtkDataSetMapper.h>
#include <vtkLabeledDataMapper.h>
#include <vtkUnstructuredGridReader.h>
#include <vtkUnstructuredGrid.h>
#include <vtkUnstructuredGridGeometryFilter.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include "vtkIdFilter.h"
#include "vtkCellCenters.h"
#include <iostream>

int main(int argc, char* argv[])
{
    std::string inputFilename = argv[1];

    // read file.
    vtkSmartPointer<vtkUnstructuredGridReader> reader = vtkSmartPointer<vtkUnstructuredGridReader>::New();
    reader->SetFileName(inputFilename.c_str());
    reader->ReadAllScalarsOn();
    reader->SetScalarsName(reader->GetScalarsNameInFile(0));
    reader->Update();

    unsigned int ncell = reader->GetOutput()->GetNumberOfCells();

    // get attributes.
    vtkSmartPointer<vtkUnstructuredGrid> ugrid = reader->GetOutput();
    vtkSmartPointer<vtkCellData> cellData = ugrid->GetCellData();
    vtkSmartPointer<vtkDataArray> data = cellData->GetScalars(reader->GetScalarsNameInFile(0));

    // validate that attributes are read correctly.
    for (int i=0; i<ncell; i++)
    {
        std::cout<< i << ": " << data->GetComponent(i,0)<< std::endl;
    }
    data = cellData->GetScalars(reader->GetScalarsNameInFile(1));
    for (int i=0; i<ncell; i++)
    {
        std::cout<< i << ": " << data->GetComponent(i,0)<< std::endl;
    }

    data = cellData->GetScalars(reader->GetScalarsNameInFile(0));

    // geometry filter.
    vtkSmartPointer<vtkUnstructuredGridGeometryFilter> geometryFilter = vtkSmartPointer<vtkUnstructuredGridGeometryFilter>::New();
    geometryFilter->SetInputConnection(reader->GetOutputPort());
    geometryFilter->Update();

    // Generate data arrays containing point and cell ids
    vtkSmartPointer<vtkIdFilter> ids = vtkSmartPointer<vtkIdFilter>::New();
    ids->SetInputConnection(geometryFilter->GetOutputPort());
    ids->PointIdsOff();
    ids->CellIdsOff();
    ids->FieldDataOn();

    // Create labels for cells
    vtkSmartPointer<vtkCellCenters> cc = vtkSmartPointer<vtkCellCenters>::New();
    cc->SetInputConnection(ids->GetOutputPort());

    // lut
    vtkSmartPointer<vtkLookupTable> lut = vtkSmartPointer<vtkLookupTable>::New();
    lut->SetNumberOfTableValues(ncell);
    lut->Build();
    lut->SetTableValue(0, 1, 0, 0, 1); // red.
    lut->SetTableValue(1, 0, 1, 0, 1); // green.
    lut->SetTableValue(2, 0, 0, 1, 1); // blue.

    // mapper.
    vtkSmartPointer<vtkDataSetMapper> mapper = vtkSmartPointer<vtkDataSetMapper>::New();
    mapper->SetInputConnection(geometryFilter->GetOutputPort());
    mapper->SetLookupTable(lut);
    mapper->SetScalarVisibility(1);
    mapper->SetScalarModeToUseCellData();
    mapper->SetScalarRange(11, 13);
    mapper->GetInput()->GetCellData()->SetActiveScalars("cell_tag");

    // label mapper.
    vtkSmartPointer<vtkLabeledDataMapper> label_mapper = vtkSmartPointer<vtkLabeledDataMapper>::New();
    label_mapper->SetInputConnection(cc->GetOutputPort());
    label_mapper->SetLabelModeToLabelScalars();

    // actor.
    vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
    actor->SetMapper(mapper);
    actor->GetProperty()->SetRepresentationToWireframe();

    // label actor.
    vtkSmartPointer<vtkActor2D> label_actor = vtkSmartPointer<vtkActor2D>::New();
    label_actor->SetMapper(label_mapper);

    // renderer.
    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->AddActor(label_actor);
    renderWindow->Render();
    renderWindowInteractor->Start();

    return EXIT_SUCCESS;
}


数据文件(.vtk):

# vtk DataFile Version 3.0
All in VTK format
ASCII
DATASET UNSTRUCTURED_GRID
POINTS 8 float
-20 0 0
-12.898 0 0
-7.65367 18.4776 0
-14.1421 14.1421 0
-18.4776 7.65367 0
-11.8832 4.95205 0
-9.03623 9.14123 0
-4.79931 11.937 0

CELLS 3 15
4 0 1 5 4
4 4 5 6 3
4 3 6 7 2
CELL_TYPES 3
9
9
9
CELL_DATA 3
SCALARS oga_cell_type int 1
LOOKUP_TABLE default
5
6
7
SCALARS cell_tag int 1
LOOKUP_TABLE default
11
12
13


输出:

c&#43;&#43; - 如何将单元格属性可视化为标签-LMLPHP

最佳答案

See for example this tutorial,但可以归结为:

mapper->SetLookupTable(lut); // lut is a lookup table for colors, see the linked tutorial or other vtk tutorials about look up tables
mapper->SetScalarVisibility(1);
mapper->SetScalarModeToUseCellData();
mapper->GetInput()->GetCellData()->SetActiveScalars("nameOfTheArrayToUseForColoringTheCells");

关于c++ - 如何将单元格属性可视化为标签,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42934716/

10-12 22:10