显示千个不同的球体

Display thousand of different sphere

本文关键字:千个 显示      更新时间:2023-10-16

我对VTK很陌生。我在带有Qt库的c++上使用它。我已经在我的代码中用管道显示了很多段(约50.000):vtkLine→vtkCellArray→vtkPolyData→vtkPolyDataMapper→vtkActor→ ...我可以在不重新创建分段的情况下更改颜色。我想对球体做同样的事情:大约30000个不同的球体。我想把线段和球体显示在一起。

有没有办法做到这一点并保持平稳的互动?有可能像我的片段一样在一个演员中显示所有球体吗?

提前谢谢。

我终于拿到了!!!使用以下代码,我可以更改球体的大小,我可以改变颜色和不透明度。我也保持着流畅的互动,所以它是完美的。我刚刚为颜色和不透明度添加了一个vtkLookupTable,并用颜色标签(与我的半径类型相同)创建了一个链接。这里是我的100个球体的代码(在圆圈中可以看到所有球体)

#include <vtkSmartPointer.h>
#include <vtkPolyData.h>
#include <vtkCellArray.h>
#include <vtkSphereSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkActor.h>
#include <vtkPoints.h>
#include <math.h>
#include <vtkPointData.h>
#include <vtkFloatArray.h>
#include <vtkGlyph3D.h>
#include <vtkUnstructuredGrid.h>
#include <vtkLookupTable.h>
#define PI 3.14159265
int main(int, char *[])
{
srand(time(NULL));
// create points
vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
// setup scales
vtkSmartPointer<vtkFloatArray> scales = vtkSmartPointer<vtkFloatArray>::New();
scales->SetName("scales");
// setup color label
vtkSmartPointer<vtkFloatArray> col = vtkSmartPointer<vtkFloatArray>::New();
col->SetName("col");
// setup lookupTable and add some colors
vtkSmartPointer<vtkLookupTable> colors = vtkSmartPointer<vtkLookupTable>::New();
colors->SetNumberOfTableValues(4);
colors->SetTableValue(0 ,1.0 ,0.0 ,0.0 ,1.0); // red
colors->SetTableValue(1 ,0.0 ,1.0 ,0.0 ,1.0); // green
colors->SetTableValue(2 ,0.0 ,0.0 ,1.0 ,1.0); // blue
colors->SetTableValue(3 ,1.0 ,1.0 ,0.0 ,1.0); // yellow
// the last double value is for opacity (1->max, 0->min)
for(int i=0; i<100; i++)
{
points->InsertNextPoint(15*cos(i*PI/50), 15*sin(i*PI/50), 0); // sphere in circle
scales->InsertNextValue((rand()% 100)/double(100)); // random radius between 0 and 0.99
col->InsertNextValue((rand()% 4)); // random color label
}
// grid structured to append center, radius and color label
vtkSmartPointer<vtkUnstructuredGrid> grid = vtkSmartPointer<vtkUnstructuredGrid>::New();
grid->SetPoints(points);
grid->GetPointData()->AddArray(scales);    
grid->GetPointData()->SetActiveScalars("scales"); // !!!to set radius first
grid->GetPointData()->AddArray(col);
// create anything you want here, we will use a sphere for the demo
vtkSmartPointer<vtkSphereSource> sphereSource = vtkSmartPointer<vtkSphereSource>::New();
// object to group sphere and grid and keep smooth interaction
vtkSmartPointer<vtkGlyph3D> glyph3D = vtkSmartPointer<vtkGlyph3D>::New();
glyph3D->SetInputData(grid);
glyph3D->SetSourceConnection(sphereSource->GetOutputPort());
// create a mapper and actor
vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection(glyph3D->GetOutputPort());
mapper->SetScalarModeToUsePointFieldData(); // without, color are displayed regarding radius and not color label
mapper->SetScalarRange(0, 3); // to scale color label (without, col should be between 0 and 1)
mapper->SelectColorArray("col"); // !!!to set color (nevertheless you will have nothing)
mapper->SetLookupTable(colors);
vtkActor *actor = vtkActor::New();
actor->SetMapper(mapper);
// create a renderer, render window, and interactor
vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
// add the actor to the scene
renderer->AddActor(actor);
renderer->SetBackground(0, 0, 0);
// render and interact
renderWindow->Render();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}