[vtkusers] RE : Newbie question: unstructuredGrid with voxels ofdifferent colours

Mark Jefferson mark.jefferson at qq.com
Wed Jul 9 21:10:47 EDT 2008


Hi, Teresa,
   we usually use vtkColorTransferFunction and vtkPiecewiseFunction in volume rendering.
   for vtkColorTransferFunction, it would define a transfer function for mapping a property to an RGB color value. we could specify the color for the given scalar value by using method AddRGBPoint(). and there are four parameters in this method: the first one is the scalar value, and the others are the r, g and b value. we usually only use several scalar value, not all the scalar value. you could get the scalar value's range from your input data, and choose several point to add color for them.
   for vtkPiecewiseFunction, it could define a piecewise function for the input data by using method AddPoint(). and we only use several points that have different scalar value, not all the points.  method AddPoint() has two parameters: the first one is scalar value, and the last one is the opacity. if the last one is 0.0, then these voxel whose scalar value equal to the first one would be transparent completely, in contrast, if it is 1.0, it would be opaque completely.
   and the reason that you could not get the right result is that your parameters in vtkPiecewiseFunction are wrong, you should change them, for example:
   vtkPiecewiseFunction *fOpacity = vtkPiecewiseFunction::New();
    fOpacity->AddPoint(        0.0, 0.0); // transparent
    fOpacity->AddPoint(nbVoxels, 1.0); // opaque
   hope this could help you!
   M. J.
  
 ------------------ Original ------------------
  From:  "Teresa Azevedo"<dce06003 at fe.up.pt>;
 Date:  Wed, Jul 9, 2008 11:21 PM
 To:  "Mark Jefferson"<mark.jefferson at qq.com>; 
 Cc:  "vtkusers"<vtkusers at vtk.org>; 
 Subject:  Re: RE : Newbie question: unstructuredGrid with voxels ofdifferent colours

  
 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 --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20080710/1dba7de3/attachment.htm>


More information about the vtkusers mailing list