Spheres:
// Array for sphere positions
Points = vtkSmartPointer::New();
// Array for sphere radii
radii = vtkSmartPointer::New();
radii->Initialize();
radii->SetName(“radii”);
// Array for sphere colors
colors = vtkSmartPointer::New();
colors->Initialize();
colors->SetNumberOfComponents(4);
colors->SetName(“colors”);
// Set position, radius and color
for (int i = 0; i < NUMBER OF SPHERES; i++)
{
points->InsertNextPoint(X, Y, Z);
radii->InsertNextValue(RADIUS);
colors->InsertNextTypedTuple(R, G, B, ALPHA);
}
// Create an unstructured grid
grid = vtkSmartPointer::New();
grid->SetPoints(points);
grid->GetPointData()->AddArray(radii);
grid->GetPointData()->AddArray(colors);
grid->GetPointData()->SetActiveScalars(“radii”);
// The points should be visualized as spheres
sourceBall = vtkSmartPointer::New();
sourceBall->SetRadius(1);
// Use VTK’s Glyph3D filter to create a single VTK actor
// which includes multiple spheres at certain locations
source = vtkSmartPointer::New();
source->SetColorModeToColorByScalar();
source->SetSourceConnection(spSourceBall->GetOutputPort());
source->SetInputData(grid);
source->SetScaleModeToScaleByScalar();
// Create VTK actor / mapper
vtkSmartPointer mapper = vtkSmartPointer::New();
mapper->SetInputConnection(spSource->GetOutputPort());
mapper->SetScalarModeToUsePointFieldData();
mapper->SelectColorArray(“colors”);
mapper->SetScalarVisibility(true);
actor = vtkSmartPointer::New();
actor->SetMapper(mapper);
Cylinders:
Just replace the sphere source with a cylinder source.
In order to orient the cylinders, create an additional array like this:
normals = vtkSmartPointer::New();
normals->SetNumberOfComponents(3);
Add the normals to the unstructured grid:
grid->GetPointData()->SetNormals(normals);
Moreover, you call the following method for the cylinder source:
source->SetVectorModeToUseNormal();
I hope this helps.