[vtkusers] coloring a sphere by elevation
Liam Kurmos
quantum.leaf at gmail.com
Fri Aug 19 09:39:28 EDT 2011
I'm creating an example to colour the surface of a sphere using a
lookup table, where the scalar value is just the value of one of the
vertex coords.
The code below essentially works but only shows 2 colors.
2Qs:
-why dont i see a range of colors from the lookup table?
-i thought i should be able to get the coordinates of a vertex from
the vtkPointData but i couldnt do this or find an example and had to
use vtkPolyData->getPoint(i). Is it straight forward to get down to
the points for pointdata?
any thoughts appreciated,
Liam
void Analyser4D::testFunc() {
cout << " test code" << endl;
vtkSmartPointer<vtkSphereSource> sphereSource =
vtkSmartPointer<vtkSphereSource>::New();
float radius = 100000;
sphereSource->SetCenter(0,0,0);
sphereSource->SetRadius(radius);
sphereSource->SetPhiResolution(20);
sphereSource->SetThetaResolution(20);
sphereSource->Update();
vtkPolyData* polyd=sphereSource->GetOutput();
vtkPointData* pointd=polyd->GetPointData();
vtkSmartPointer<vtkFloatArray> scalars=
vtkSmartPointer<vtkFloatArray>::New();
cout<<"point comps "<<pointd->GetNumberOfComponents()<<" tups
"<<pointd->GetNumberOfTuples()<<" arrays
"<<pointd->GetNumberOfArrays()<<endl;
scalars->SetNumberOfValues(pointd->GetNumberOfTuples());
scalars->SetName("scalars");
for (int i=0;i<scalars->GetNumberOfTuples();i++){
//vtkDataArray* dataa=pointd->GetArray(0);
vtkFloatArray* dataa=vtkFloatArray::SafeDownCast(pointd->GetArray(0));
double* point=polyd->GetPoint(i);
scalars->SetValue(i,point[0]);
}
pointd->SetScalars(scalars);
vtkLookupTable* lut=vtkLookupTable::New();
lut->SetHueRange(0,0.6); //Red to Blue
lut->SetAlphaRange(1,1);
lut->SetValueRange(1,1.0);
lut->SetSaturationRange(1.0,1.0);
lut->SetNumberOfTableValues(256*256*256);
lut->SetRange(-100000,100000);//altering range appear not to effect output
lut->Build();
//
vtkSmartPointer<vtkPolyDataMapper> mapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection(sphereSource->GetOutputPort());
mapper->SetLookupTable(lut);
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
actor->GetProperty()->SetAmbient(1); //SetShading(0);
actor->GetProperty()->SetDiffuse(0); //SetShading(0);
actor->GetProperty()->SetSpecular(0); //SetShading(0);
actor->GetProperty()->SetInterpolationToFlat();
//actor->GetProperty()->SetOpacity(0.5);
actor->GetProperty()->SetColor(1, 0, 0);
//
vtkSmartPointer<vtkRenderer> renderer;
vtkSmartPointer<vtkRenderWindow> renderWindow;
renderer = vtkSmartPointer<vtkRenderer>::New();
renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
renderer->SetBackground(0, 0, 0);
renderWindow->AddRenderer(renderer);
renderWindow->SetSize(300, 300);
renderer->SetBackground(0, 0, 0);
renderer->AddActor(actor);
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor
= vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
renderWindow->Render();
renderWindowInteractor->Start();
}
More information about the vtkusers
mailing list