Recoloring

Starting from a set of 3D points, I generate a 2D triangular unstructured grid in a vtkPolyData and use the z values to assign colors to the points, using a vtkLookupTable. The vtkPolyData is then rendered as a surface. This all works fine. I want to be able to change the color lookup table to recolor the rendered surface. So far my attempts to do this have not been successful. Here is the code (points is a vtkPoints object that has been populated with npts 3D points.)

vtkPolyData *inputPolyData = vtkPolyData::New();
inputPolyData->SetPoints(points);

// Triangulate the grid points
vtkDelaunay2D *delaunay = vtkDelaunay2D::New();

#if VTK_MAJOR_VERSION_5
delaunay->SetInput(inputPolyData);
#else
delaunay->SetInputData(inputPolyData);
#endif

delaunay->Update();
vtkPolyData *outputPolyData = delaunay->GetOutput();
double minz, maxz;
double *colarray = new double[npts];
 minz = 0;
 maxz = 0.18;

// Create the color map
vtkLookupTable *colorLookupTable = vtkLookupTable::New();
colorLookupTable->SetTableRange(minz, maxz);
colorLookupTable->Build();
// Generate the colors for each point based on the color map
vtkUnsignedCharArray *colors = vtkUnsignedCharArray::New();
colors->SetNumberOfComponents(3);
// Create a mapper and actor
vtkPolyDataMapper *mapper = vtkPolyDataMapper::New();
vtkActor *actor = vtkActor::New();
actor->SetMapper(mapper);
int npts = outputPolyData->GetNumberOfPoints();
for (int i = 0; i < npts; i++) {
double *p = outputPolyData->GetPoint(i);
colarray[i] = p[2];
double dcolor[3];
colorLookupTable->GetColor(colarray[i],dcolor);
unsigned char *color = new unsigned char[3];
for (int j = 0; j < 3; j++) {
color[j] = (unsigned char)( 255 * dcolor[j] );
}
colors->InsertNextTuple3(color[0], color[1], color[2] );
}
outputPolyData->GetPointData()->SetScalars(colors);

#if VTK_MAJOR_VERSION_5
mapper->SetInputConnection(outputPolyData->GetProducerPort());
#else
mapper.SetInputData(outputPolyData);
#endif

vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
renderWindow->SetSize(640,640);
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = 
  vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);

// Add the actor to the scene
renderer->AddActor(actor);
renderer->SetBackground(.1, .3,.2); // Background color dark green
// Render and interact
renderWindow->Render();
renderWindowInteractor->Start();

// Try changing the colours
minz = 0.99*maxz;
colorLookupTable->SetTableRange(minz, maxz);
colorLookupTable->Build();
colors->Reset();
for (int i = 0; i < npts; i++) {
double dcolor[3];
colorLookupTable->GetColor(colarray[i],dcolor);
unsigned char *color = new unsigned char[3];
for (int j = 0; j < 3; j++) {
color[j] = (unsigned char)( 255 * dcolor[j] );
}
colors->InsertNextTuple3(color[0], color[1], color[2] );
}

outputPolyData->GetPointData()->SetScalars(colors);
outputPolyData->Update();
mapper->Update();
renderWindow->Render();
return;

The code after “// Try changing the colours” has no effect on what is displayed. (If I put “minz = 0.99*maxz” after the line “maxz = 0.18” I get a very different coloring from the initial render.)

What am I doing wrong?