I initialize the vtkUnstructuredGrid object with the following code. The test model has about 1 million VTK_HEXAHEDRON cells. When executing vtkRenderer->Render(), it takes about 10 seconds.Is there a better way to reduce the time spent on Render?
std::vector<double> coordinates;
std::vector<vtkIdType> cellData;
std::vector<vtkIdType> offsetData;
std::vector<int> intCellTypes;
std::vector<int> ptModelShowData; // PointData 0: hide 1:show
// Assume it is initialized
vtkNew<vtkDoubleArray> pDataArray;
pDataArray->SetNumberOfComponents(3);
pDataArray->SetArray(coordinates.data(), coordinates.size(), 1);
vtkNew<vtkPoints> pPoints;
pPoints->SetData(pDataArray);
vtkNew<vtkIdTypeArray> idArray;
idArray->SetNumberOfComponents(1);
idArray->SetArray(cellData.data(), cellData.size(), 1);
vtkNew<vtkIdTypeArray> offsetArray;
offsetArray->SetNumberOfComponents(1);
offsetArray->SetArray(offsetData.data(), offsetData.size(), 1);
vtkNew<vtkCellArray> pCellArray;
pCellArray->SetData(offsetArray, idArray);
const vtkIdType ncells = intCellTypes.size();
vtkNew<vtkUnsignedCharArray> cellTypes;
cellTypes->SetNumberOfTuples(ncells);
auto typeRange = vtk::DataArrayValueRange<1>(cellTypes);
std::transform(intCellTypes.data(), intCellTypes.data() + ncells, typeRange.begin(),
[](int t) -> unsigned char { return static_cast<unsigned char>(t); });
vtkNew<vtkUnstructuredGrid> pGrid;
pGrid->SetPoints(pPoints);
pGrid->SetCells(cellTypes, pCellArray);
vtkNew<vtkIntArray> ptDataArray;
ptDataArray->SetNumberOfComponents(1);
ptDataArray->SetArray(ptModelShowData.data(), ptModelShowData.size(), 1);
pGrid->GetPointData()->SetAttribute(ptDataArray, 0);
vtkNew<vtkLookupTable> pModelLookupTable;
pModelLookupTable->SetNumberOfTableValues(2);
pModelLookupTable->SetTableRange(0, 1);
pModelLookupTable->SetTableValue(0, 0, 0, 0, 0);
pModelLookupTable->SetTableValue(1, 0, 1, 1, 1);
pModelLookupTable->Build();
vtkNew<vtkDataSetMapper> pMapper;
pMapper->SetInputData(pGrid);
pMapper->SetLookupTable(pModelLookupTable);
pMapper->Update();
vtkNew<vtkActor> actor;
actor->SetMapper(pMapper);
vtkRenderer->AddActor(actor);
vtkRenderer->Render();
I have two questions:
1.Is there a better way to reduce the time spent on Render?
2.When I update PointData to control the display of the cell, it still takes 10 seconds, which confuses me, and I think it should take less time, but I don’t know what to do
// PointData 0: hide 1:show
ptModelShowData[8] = 0;
ptModelShowData[9] = 0;
ptModelShowData[10] = 0;
ptModelShowData[11] = 0;
ptModelShowData[12] = 0;
ptModelShowData[13] = 0;
ptModelShowData[14] = 0;
ptModelShowData[15] = 0;
ptDataArray->Modified();
vtkRenderer->Render();
Thanks you for your reply!