Hi,
I am a beginner, when I my vtkUnstructuredGrid object have a attribute temperature, there are 2 methods to put it into vtkUnstructuredGrid object, one is that put 1 by 1 using InsertNextTuple1() , this method work well. the other is that put all at once using SetArray, all work well , but the result color is shown according to the input temperature?
this is my code:
void MainWindow::showCyScene()
{
double xyz[][3]={{0, 0 ,2 },
{1, 0 ,2 },
{1, 1 ,2 },
{0, 1 ,2 },
{2, 0 ,2 },
{3, 0 ,2 },
{3, 1 ,2 },
{2, 1 ,2 }};
vtkIdType cells[][3]={{0 ,1,2 },
{0 ,2,3 },
{1 ,4,2 },
{4 ,7,2 },
{4 ,5,7 },
{5 ,6,7 }};
double temperature[8]={1.0 ,2.0 ,2.0 ,1.0,3.0, 4.0 ,4.0, 3.0};
vtkSmartPointer<vtkPoints> pts=vtkSmartPointer<vtkPoints>::New();
vtkSmartPointer<vtkUnstructuredGrid> ug=vtkSmartPointer<vtkUnstructuredGrid>::New();
for(int i=0;i<8;i++)
pts->InsertNextPoint(xyz[i]);
for (int i = 0; i < 6; ++i) {
ug->InsertNextCell(VTK_TRIANGLE,3,cells[i]);
}
ug->SetPoints(pts);
vtkSmartPointer<vtkDoubleArray> da=vtkSmartPointer<vtkDoubleArray>::New();
da->SetName("temp");
da->SetArray(temperature,8,1); //Method 1: using SetArray() add temperature at once is not effective.
// for(int i=0;i<8;i++)
// da->InsertNextTuple1(temperature[i]); //Method 2: using InsertNextTuple() add temperature 1 by 1 is effective.
ug->GetPointData()->SetScalars(da);
qDebug()<<ug->GetPointData()->GetArrayName(0);
ug->GetPointData()->SetActiveScalars("temp");
vtkSmartPointer<vtkLookupTable> lut=vtkSmartPointer<vtkLookupTable>::New();
lut->SetNumberOfColors(3);
lut->SetTableValue(0,1,1,1);
lut->SetTableValue(1,0,1,0);
lut->SetTableValue(2,0,0,1);
lut->Build();
vtkSmartPointer<vtkDataSetMapper> mapper=vtkSmartPointer<vtkDataSetMapper>::New();
mapper->SetInputData(ug);
mapper->SetScalarRange(1.0,5.0);
mapper->SetLookupTable(lut);
mapper->ScalarVisibilityOn();
mapper->SetScalarModeToUsePointData();
vtkSmartPointer<vtkActor> actor= vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
vtkSmartPointer<vtkRenderer> renderer=
vtkSmartPointer<vtkRenderer>::New();
renderer->SetBackground(1,1,1);
renderer->AddActor(actor);
vtkSmartPointer<vtkAxesActor> actor2 = vtkSmartPointer<vtkAxesActor>::New();
actor2->SetPosition(0, 0, 0);
actor2->SetTotalLength(2,2,2);
actor2->SetShaftType(0);
actor2->SetAxisLabels(0);
actor2->SetCylinderRadius(0.02);
renderer->AddActor(actor2);
renderer->SetBackground(0.1,0.2,0.4);
renderer->ResetCamera();
this->qvtkWidget->GetRenderWindow()->AddRenderer(renderer);
}
Thank you for your help
Leo