显示千种不同的球体

时间:2015-04-24 06:25:03

标签: c++ vtk

我对VTK很新。我用Qt库在c ++上使用它。我已经在管道中的代码中显示了很多段(~50.000): vtkLine→vtkCellArray→vtkPolyData→vtkPolyDataMapper→vtkActor→... 我能够在不重新创建片段的情况下更改颜色。 我想对球体做同样的事情:大约30,000个不同的球体。我想将片段和球体一起显示出来。

有没有办法做到这一点并保持顺畅的互动?是否可以在像我的片段这样的演员中显示所有球体?

提前谢谢。

1 个答案:

答案 0 :(得分:2)

我终于明白了!使用以下代码,我可以更改球体的大小,我可以更改颜色和不透明度。而且我也保持顺畅的互动,所以它是完美的。我刚刚为颜色和不透明度添加了一个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;
}