[vtkusers] RE : Newbie question: unstructuredGrid with voxels of different colours
Teresa Azevedo
dce06003 at fe.up.pt
Wed Jul 9 11:21:48 EDT 2008
Thanks for your answer, Mark.
I tried to use that function, using this code (build a 3x3x3 blue-red
3D chessboard):
int nbVoxels = 3*3*3;
int voxelSize = 1;
vtkPoints *voxelPoints = vtkPoints::New();
voxelPoints->SetNumberOfPoints(8*nbVoxels);
voxelPoints->SetDataTypeToDouble();
vtkUnstructuredGrid *grid = vtkUnstructuredGrid::New();
grid->Allocate(nbVoxels);
vtkVoxel *voxel = vtkVoxel::New();
int count = 0;
int posX = 0;
int posY = 0;
int posZ = 0;
for ( int v=0; v<nbVoxels ; v++ )
{
voxelPoints->InsertPoint(count*8+0, posX,
posY, posZ);
voxelPoints->InsertPoint(count*8+1, posX+voxelSize,
posY, posZ);
voxelPoints->InsertPoint(count*8+2, posX,
posY+voxelSize, posZ);
voxelPoints->InsertPoint(count*8+3, posX+voxelSize,
posY+voxelSize, posZ);
voxelPoints->InsertPoint(count*8+4, posX,
posY, posZ+voxelSize);
voxelPoints->InsertPoint(count*8+5, posX+voxelSize,
posY, posZ+voxelSize);
voxelPoints->InsertPoint(count*8+6, posX,
posY+voxelSize, posZ+voxelSize);
voxelPoints->InsertPoint(count*8+7, posX+voxelSize,
posY+voxelSize, posZ+voxelSize);
voxel->GetPointIds()->SetId(0, count*8+0);
voxel->GetPointIds()->SetId(1, count*8+1);
voxel->GetPointIds()->SetId(2, count*8+2);
voxel->GetPointIds()->SetId(3, count*8+3);
voxel->GetPointIds()->SetId(4, count*8+4);
voxel->GetPointIds()->SetId(5, count*8+5);
voxel->GetPointIds()->SetId(6, count*8+6);
voxel->GetPointIds()->SetId(7, count*8+7);
grid->InsertNextCell(VTK_VOXEL, voxel->GetPointIds());
count++;
posX += voxelSize;
if ( posX == 3*voxelSize )
{
posX = 0;
posY += voxelSize;
if ( posY == 3*voxelSize )
{
posY = 0;
posZ += voxelSize;
}
}
}
grid->SetPoints(voxelPoints);
//extract edges from unstructured grid
vtkExtractEdges *edges = vtkExtractEdges::New();
edges->SetInput(grid);
vtkPolyDataMapper *gridMapper = vtkPolyDataMapper::New();
gridMapper->SetInput(edges->GetOutput());
vtkActor *gridActor = vtkActor::New();
gridActor->SetMapper(gridMapper);
gridActor->GetProperty()->SetColor(0.0,0.0,0.0);
vtkDoubleArray *colourPts = vtkDoubleArray::New();
for(int i=0; i < nbVoxels; i++)
colourPts->InsertNextValue(i);
vtkCellData *cellData = grid->GetCellData();
cellData->SetNumberOfTuples(nbVoxels);
cellData->SetScalars(colourPts);
//create a transfer function mapping scalar value to color
vtkColorTransferFunction *fColor = vtkColorTransferFunction::New();
for (int idx = 0; idx < nbVoxels; idx++)
{
if (idx & 0x01)
fColor->AddRGBPoint(colourPts->GetValue(idx),
1, 0, 0);
else
fColor->AddRGBPoint(colourPts->GetValue(idx),
0, 0, 1);
}
//create a transfer function mapping scalar value to opacity
vtkPiecewiseFunction *fOpacity = vtkPiecewiseFunction::New();
fOpacity->AddPoint(0, 1);
fOpacity->AddPoint(nbVoxels, 1);
vtkVolumeProperty *volProp = vtkVolumeProperty::New();
volProp->SetColor(fColor);
volProp->SetScalarOpacity(fOpacity);
// make sure we have only tetrahedra
vtkDataSetTriangleFilter *filter = vtkDataSetTriangleFilter::New();
filter->SetInput(grid);
vtkUnstructuredGridVolumeRayCastMapper *vrcm =
vtkUnstructuredGridVolumeRayCastMapper::New();
vrcm->SetInput(filter->GetOutput());
vtkVolume *volume = vtkVolume::New();
volume->SetMapper(vrcm);
volume->SetProperty(volProp);
// create a rendering window and renderer
vtkRenderer *renderer = vtkRenderer::New();
vtkRenderWindow *renderWindow = vtkRenderWindow::New();
renderWindow->AddRenderer(renderer);
vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
iren->SetRenderWindow(renderWindow);
renderer->AddActor(volume);
renderer->AddActor(gridActor);
renderer->SetBackground(1,1,1);
// interact with data
renderWindow->Render();
iren->Start();
The result is on the attached figure. As you can see both the opacity
and colors are
wrong... but I do not know why...
Can someone please help me?
Teresa
Quoting Mark Jefferson <mark.jefferson at qq.com>:
> Hi, Teresa,
> you can try class vtkColorTransferFunction. for example:
> // Create a transfer function mapping scalar value to color (color)
> vtkColorTransferFunction *cTFun = vtkColorTransferFunction::New();
> cTFun->AddRGBPoint( 0, 1.0, 0.0, 0.0 );
> cTFun->AddRGBPoint( 64, 1.0, 1.0, 0.0 );
> cTFun->AddRGBPoint( 128, 0.0, 1.0, 0.0 );
> cTFun->AddRGBPoint( 192, 0.0, 1.0, 1.0 );
> cTFun->AddRGBPoint( 255, 0.0, 0.0, 1.0 );
> hope this could help you.
> M. J.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 3x3x3cube.gif
Type: image/gif
Size: 47626 bytes
Desc: not available
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20080709/f6bd97e3/attachment.gif>
More information about the vtkusers
mailing list