Hello,
I’m new to VTK and am trying to create a landscape of cuboids with the height of each cuboid specified in a matrix. For that I use mainly the classes vtkCubeSource
and vtkAppendPolyData
:
void create_preview(const Eigen::MatrixXi &heightMap, const int blockSize)
{
// Create a renderer
vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
renderer->SetBackground(1.0, 1.0, 1.0); // White background
// Combine all cuboids into a single polydata
vtkSmartPointer<vtkAppendPolyData> appendFilter = vtkSmartPointer<vtkAppendPolyData>::New();
// Create a cube source for each element in the height map
for (int x = 0; x < heightMap.rows(); x++) {
for (int y = 0; y < heightMap.cols(); y++) {
vtkSmartPointer<vtkCubeSource> cubeSource = vtkSmartPointer<vtkCubeSource>::New();
cubeSource->SetXLength(blockSize);
cubeSource->SetYLength(blockSize);
cubeSource->SetZLength(heightMap(x, y));
cubeSource->SetCenter(y * blockSize, (-1) * x * blockSize, heightMap(x, y) / 2.0);
appendFilter->AddInputConnection(cubeSource->GetOutputPort());
}
}
appendFilter->Update(); //Very slow for large matrices
// Create a mapper and actor for the combined cuboids
vtkSmartPointer<vtkPolyDataMapper> heightMapMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
heightMapMapper->SetInputConnection(appendFilter->GetOutputPort());
vtkSmartPointer<vtkActor> heightMapActor = vtkSmartPointer<vtkActor>::New();
heightMapActor->SetMapper(heightMapMapper);
heightMapActor->GetProperty()->SetColor(0.0, 1.0, 0.0); // Green color
// Add the height map actor to the renderer
renderer->AddActor(heightMapActor);
// Create a render window
vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
renderWindow->SetSize(1280, 720);
// Create an interactor
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
// Set the interactor style to trackball camera
vtkSmartPointer<vtkInteractorStyleTrackballCamera> style = vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();
renderWindowInteractor->SetInteractorStyle(style);
// Start the interaction
renderWindow->Render();
renderWindowInteractor->Start();
}
The result of this code generates this kind of preview, exactly as expected:
My problem is that for bigger matrices, with shape like 560x370 values, the method vtkAppendPolyData::Update()
takes a very long time to complete (on my computer about 60 seconds).
I wanted to ask if there is a way to speed things up by using a different set of classes or methods, because I couldn’t find any useful info fitting my exact use case.
Thanks in advance!