vtkAppendPolyData 中的多种颜色

Multiple colors in vtkAppendPolyData

本文关键字:颜色 vtkAppendPolyData      更新时间:2023-10-16

I have vtkAppendPolyData,其中包含 4 个 vtkConeSource。我想用不同的颜色给这 4 个锥体着色。vtk 中有什么方法可以实现这一点。如果您有任何其他建议,请告诉我。

vtkConeSource *cone1 = vtkConeSource::New();
cone1->SetHeight(6.0);
cone1->SetRadius(3.0);
cone1->SetCenter(0, 0, 0);
cone1->SetResolution(10);
vtkPolyData *coneData1 = cone1->GetOutput();
unsigned char red[3] = {255, 0, 0};
vtkUnsignedCharArray *colors = vtkUnsignedCharArray::New();
colors->SetNumberOfComponents(3);
colors->SetName("Colors");
colors->InsertNextTupleValue(red);
coneData1->GetCellData()->SetScalars(colors);
vtkPolyDataMapper *mapper = vtkPolyDataMapper::New();
mapper->SetInput(coneData1);
mapper->Update();
mapper->StaticOn();
vtkActor *coneActor = vtkActor::New();
coneActor->SetMapper( mapper );
vtkRenderer *ren1= vtkRenderer::New();
ren1->AddActor( coneActor );
ren1->SetBackground( 0.1, 0.2, 0.4 );
vtkRenderWindow *renWin = vtkRenderWindow::New();
renWin->AddRenderer( ren1 );
renWin->SetSize( 300, 300 );
vtkRenderWindowInteractor *interactor = vtkRenderWindowInteractor::New();
renWin->SetInteractor(interactor);
renWin->Render();
interactor->Start();

这是我创建圆锥体的代码,我想给它上色,即使我已经设置了锥体数据1->GetCellData()->SetScalars(颜色),它也不会以红色显示圆锥体。

将它们连接到追加过滤器之前,您必须将颜色数组附加到每个 vtkConeSource 输出。你会这样做:

  unsigned char red[3] = {255, 0, 0};
  vtkSmartPointer<vtkUnsignedCharArray> colors =
    vtkSmartPointer<vtkUnsignedCharArray>::New();
  colors->SetNumberOfComponents(3);
  colors->SetName("Colors");
  colors->InsertNextTupleValue(red);
  polydata->GetCellData()->SetScalars(colors);

(这里有一个完整的例子:http://www.vtk.org/Wiki/VTK/Examples/Cxx/PolyData/TriangleSolidColor)

以下是为对象着色的不同方法(直接为数据着色,而不是为演员着色)的描述,可能也值得一看:https://docs.google.com/present/edit?id=0AcyIfGqnlfSoZGdqaGhnMnJfMjc0Z3EybnNkZzQ&hl=en

vtkConeSource *cone1 = vtkConeSource::New();
cone1->SetHeight(6.0);
cone1->SetRadius(3.0);
cone1->SetCenter(0, 0, 0);
cone1->SetResolution(10);
vtkPolyData *coneData1 = cone1->GetOutput();
unsigned char red[3] = {255, 0, 0};
vtkUnsignedCharArray *colors = vtkUnsignedCharArray::New();
colors->SetNumberOfComponents(3);
colors->SetName("Colors");
colors->InsertNextTupleValue(red);
// Before setting color to cell data
// Upadte coneSource
coneData1->GetCellData()->Update();
// This will give celldata Other wise Number of cell data will be zero
// Insert tuples equal to number of Cell present in Polydata
coneData1->GetCellData()->SetScalars(colors);

同样,对于其他锥体,我添加了颜色,这解决了我的问题