[vtkusers] How do I get the scalar value of polygons created in a section?
Philip Morris Jones
philip at cd.co.uk
Wed Feb 27 09:50:35 EST 2002
Hi
I want to use VTK as an engine and render the output using another
technique. I have found out how to extract the polygon data but cannot seem
to find the scalar value. The best I can do at the moment is to redner using
a black to whire colour scale and find the colour of the points and use
this, but this gives me only a value from 0 to 255 which is too coarse. Can
anybody help?
Thanks
I have modified and written some examples to get to this C++ code:
//
// read in a .vtk file and create a section plot
//
// first include the required header files for the vtk classes we are using
#include "vtkUnstructuredGridReader.h"
#include "vtkRenderWindow.h"
#include "vtkGeometryFilter.h"
#include "vtkPolyDataMapper.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkCutter.h"
#include "vtkDataSetAttributes.h"
#include "vtkCellDataToPointData.h"
int main( int argc, char *argv[] )
{
// create an unstructuredgridreader
vtkUnstructuredGridReader* ProMeshC = vtkUnstructuredGridReader::New();
char fname[256];
sprintf(fname,"tut2.vtk");
ProMeshC->SetFileName(fname);
// force a read
ProMeshC->Update();
vtkCellDataToPointData* ProMesh = vtkCellDataToPointData::New();
ProMesh->SetInput( ProMeshC->GetOutput() );
// section cut
vtkLookupTable *lut1 = vtkLookupTable::New();
lut1->SetHueRange(0, 0.0);
lut1->SetSaturationRange(0, 0);
lut1->SetValueRange(0, 1);
vtkPlane* cutplane = vtkPlane::New();
cutplane->SetOrigin(0,0,-.01);
cutplane->SetNormal(0,0,1);
vtkCutter* planeCut = vtkCutter::New();
planeCut->SetInput( ProMesh->GetOutput() );
planeCut->SetCutFunction(cutplane);
vtkPolyDataMapper* cutMapper = vtkPolyDataMapper::New();
cutMapper->SetInput ( planeCut->GetOutput() );
cutMapper->SetScalarRange ( 0, 20 );
cutMapper->SetLookupTable( lut1 );
vtkActor *cutActor = vtkActor::New();
cutActor->SetMapper( cutMapper );
// get poly data
cutActor->GetMapper()->GetInput()->Update();
vtkPolyData *polyData = (vtkPolyData *) cutActor->GetMapper()->GetInput();
vtkUnsignedCharArray *colorArray = cutActor->GetMapper()->MapScalars(1.0);
// get primitive arrays
vtkCellArray *polys;
polys = polyData->GetPolys();
int numPolys = polys->GetNumberOfCells();
cerr << " " << numPolys << " polys";
cerr.flush();
int i, prim = 0, vert = 0;
int npts, *pts;
for (polys->InitTraversal(); polys->GetNextCell(npts, pts); prim++)
for (i=0; i < npts; i++)
{
float *aVertex = polyData->GetPoint(pts[i]);
cout << "v mxv " << aVertex[0] << " " << aVertex[1] << " " << aVertex[2]
<< "\n";
unsigned char *aColor = colorArray->GetPointer(pts[i]);
cout << (int) aColor[0] << "\n";
}
if(npts==3)
{
cout << "c mxv - 1 mxv - 2 mxv - 3 mxv - 3\n";
}
else
{
cout << "c mxv - 1 mxv - 2 mxv - 3 mxv - 4\n";
}
}
vtkRenderer *ren1 = vtkRenderer::New();
vtkRenderWindow *renWin = vtkRenderWindow::New();
renWin->AddRenderer(ren1);
// an interactor
vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
iren->SetRenderWindow(renWin);
ren1->AddActor(cutActor);
ren1->SetBackground(1,1,1); // Background color white
// render an image (lights and cameras are created automatically)
renWin->Render();
// begin mouse interaction
iren->Start();
return 0;
}
philip at cd.co.uk
More information about the vtkusers
mailing list