Setting multiple scalars in point data

I wrote the following and trying to understand why only the density scalar is visible in the VTP file written out.

What is the correct way to add multiple point data ?

void example_02()
{
	std::string filename ("vtp_polys_02.vtp");

	auto points = vtkSmartPointer<vtkPoints>::New();
	points->InsertNextPoint(0, 0, 0);
	points->InsertNextPoint(0, 1, 0);
	points->InsertNextPoint(1, 0, 0);

	auto cells = vtkSmartPointer<vtkCellArray>::New();
	cells->InsertNextCell({0,1,2});

	auto polydata = vtkSmartPointer<vtkPolyData>::New();
	polydata->SetPoints(points);
	polydata->SetPolys(cells);

	{
		vtkSmartPointer<vtkFloatArray> velocity =
				vtkSmartPointer<vtkFloatArray>::New();
		velocity->SetName("velocity");
		velocity->SetNumberOfComponents(3);
		float u[3] = {1,0,0};
		float v[3] = {0,10,0};
		float w[3] = {0,0,100};
		velocity->InsertNextTuple(u);
		velocity->InsertNextTuple(v);
		velocity->InsertNextTuple(w);

		polydata->GetPointData()->SetScalars(velocity);
	}
	{
		vtkSmartPointer<vtkFloatArray> density =
				vtkSmartPointer<vtkFloatArray>::New();
		density->SetName("density");
		density->SetNumberOfComponents(1);
		float d0 = 0.5;
		float d1 = 1;
		float d2 = 2.7;
		density->InsertNextTuple(&d0);
		density->InsertNextTuple(&d1);
		density->InsertNextTuple(&d2);

		polydata->GetPointData()->SetScalars(density);
	}

	auto writer = vtkSmartPointer<vtkXMLPolyDataWriter>::New();
	writer->SetFileName(filename.c_str());
	writer->SetInputData(polydata);
	writer->Write();
}

Looks like your second call to SetScalars simply replaces the data set in the first call.
I’m no expert on vtk data structures but from what I can say from a quick search, you probably need to use AddArray instead.

Edit: Now I found this bug which seems to indicate that the SetScalars method is a bit dated and probably should be avoided.

1 Like

Thank you @codeling , that was indeed the problem.

I am now using AddArray instead of the deprecated SetScalars/SetVectors call.

Cheers.