[vtkusers] Draw thousands of different size and color 3D rectangles several times per second

Mwoua david.levy at leddartech.com
Thu Mar 2 09:23:50 EST 2017


Mwoua wrote
> I have built my polydata and glyph3D, but I can't find a way to give them
> the color I want. The color is also using the size scaling.
> 
> How can I specify the size and color of each glyph independantly? (ergo
> how can I use my array of colors for each point)


Finally found how to do it, couldnt find any update information about it so
I'll post my code in case someone needs it.

vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New(); 

vtkSmartPointer<vtkCubeSource> cubeSource =
vtkSmartPointer<vtkCubeSource>::New(); 
cubeSource->SetXLength(x); 
cubeSource->SetYLength(y); 
cubeSource->SetZLength(z); 

// Setup scales
vtkSmartPointer<vtkFloatArray> dscale =
vtkSmartPointer<vtkFloatArray>::New();
dscale->SetName("dscale");
vtkSmartPointer<vtkFloatArray> cscale =
vtkSmartPointer<vtkFloatArray>::New();
dscale->SetName("cscale");


//build points and scales 
for(...){ 
    points->InsertNextPoint(lPoint1.x,lPoint1.y,lPoint1.z); 
    dscale->InsertNextValue(lDistance); 
    cscale->InsertNextValue(Amplitude( i ));
} 

// Combine into a polydata
vtkSmartPointer<vtkPolyData> polydata = vtkSmartPointer<vtkPolyData>::New();
polydata->SetPoints(points);
polydata->GetPointData()->SetScalars(dscale);
polydata->GetPointData()->AddArray(cscale); //Thats where you put the color
scale

vtkSmartPointer<vtkGlyph3D> glyph3D = vtkSmartPointer<vtkGlyph3D>::New(); 
glyph3D->SetScaleModeToScaleByScalar(); 
glyph3D->SetSourceConnection(cubeSource->GetOutputPort()); 
glyph3D->SetInputData(polydata); 
glyph3D->Update(); 

//Create a lookup table for the color. In this case its progressive, but its
the opposite of default lut
vtkSmartPointer<vtkLookupTable> lut =
vtkSmartPointer<vtkLookupTable>::New();
lut = vtkLookupTable::New();
lut->SetNumberOfTableValues( 16 );
lut->SetHueRange( 0.66667, 0.0 );
lut->SetSaturationRange( 1, 1 );
lut->SetValueRange( 1, 1 );
lut->Build();


// Create a mapper and actor
vtkSmartPointer<vtkPolyDataMapper> mapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection(glyph3D->GetOutputPort());
mapper->SetScalarModeToUsePointFieldData();
mapper->SelectColorArray(1); //Tell mapper to use second array
mapper->SetScalarRange(0, mMaxAmplitude);
mapper->SetLookupTable(lut);
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);

// Add the actor to the scene 
  renderer->AddActor(actor);



--
View this message in context: http://vtk.1045678.n5.nabble.com/Draw-thousands-of-different-size-and-color-3D-rectangles-several-times-per-second-tp5742310p5742343.html
Sent from the VTK - Users mailing list archive at Nabble.com.


More information about the vtkusers mailing list