[vtkusers] How to render an unstructured grid with hexahedron

Robbie Jaeger rjaeger at gmail.com
Sat Aug 9 00:55:18 EDT 2008


Hello,

I am trying to render a vtkHexahedron within a vtkUnstructuredGrid but I do
not see anything when I run the program.  Have I missed something basic?

Thank you for your help.

Here is the c++ code I am using:

#include "vtkHexahedron.h"
#include "vtkPoints.h"
#include "vtkDataSetMapper.h"
#include "vtkUnstructuredGrid.h"
#include "vtkProperty.h"
#include "vtkVolumeProperty.h"
#include "vtkColorTransferFunction.h"
#include "vtkPiecewiseFunction.h"

#include "vtkSphereSource.h"
#include "vtkPolyDataMapper.h"
#include "vtkActor.h"
#include "vtkRenderWindow.h"
#include "vtkRenderer.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkUnstructuredGridVolumeRayCastMapper.h"
#include "vtkDataSetTriangleFilter.h"
#include "vtkUnstructuredGridVolumeMapper.h"
#include "vtkProjectedTetrahedraMapper.h"
#include "vtkFloatArray.h"
#include "vtkPointData.h"

int main ()
{
    float xmin = 0.0;
    float xmax = 1.0;
    float ymin = 0.0;
    float ymax = 1.0;
    float zmin = 0.0;
    float zmax = 1.0;

    //create points of hexahedron
    vtkPoints *hexPoints = vtkPoints::New();
    hexPoints->SetNumberOfPoints(8);

    hexPoints->InsertPoint(0, xmin, ymin, zmin);
    hexPoints->InsertPoint(1, xmax, ymin, zmin);
    hexPoints->InsertPoint(2, xmax, ymax, zmin);
    hexPoints->InsertPoint(3, xmin, ymax, zmin);
    hexPoints->InsertPoint(4, xmin, ymin, zmax);
    hexPoints->InsertPoint(5, xmax, ymin, zmax);
    hexPoints->InsertPoint(6, xmax, ymax, zmax);
    hexPoints->InsertPoint(7, xmin, ymax, zmax);

    //create the hexahedron
    vtkHexahedron *theHex = vtkHexahedron::New();

    for (int i = 0; i <8; i++)
    {
        theHex->GetPointIds()->SetId(i,i);
    }

    //Create scalars
    vtkFloatArray *scalars = vtkFloatArray::New();
    scalars->InsertTuple1(0, 1.0);
    scalars->InsertTuple1(1, 1.2);
    scalars->InsertTuple1(2, 1.0);
    scalars->InsertTuple1(3, 1.2);
    scalars->InsertTuple1(4, 1.0);
    scalars->InsertTuple1(5, 1.2);
    scalars->InsertTuple1(6, 1.0);
    scalars->InsertTuple1(7, 1.2);

    //create an unstructured grid for the hexahedron
    vtkUnstructuredGrid *theGrid = vtkUnstructuredGrid::New();
    theGrid->Allocate(1, 1);
    theGrid->InsertNextCell(theHex->GetCellType(), theHex->GetPointIds());
    theGrid->SetPoints(hexPoints);
    theGrid->GetPointData()->SetScalars(scalars);

    //create a mapper for the hexahedron
    vtkDataSetMapper *theHexMapper = vtkDataSetMapper::New();
    theHexMapper->SetInput(theGrid);

    //create a volume renderer for the unstructured grid
    vtkUnstructuredGridVolumeRayCastMapper *volumeMapper =
vtkUnstructuredGridVolumeRayCastMapper::New();

    //Triangulate the surfaces
    vtkDataSetTriangleFilter *tri = vtkDataSetTriangleFilter::New();
    tri->SetInput(theGrid);
    volumeMapper->SetInput(tri->GetOutput());

    //create the volumeproperty object to define how our volume looks
    vtkColorTransferFunction *colorTF = vtkColorTransferFunction::New();
    colorTF->AddRGBPoint(0.0, 0.0, 0.0, 0.0);
    colorTF->AddRGBPoint(64.0, 1.0, 0.0, 0.0);
    colorTF->AddRGBPoint(128.0, 0.0, 0.0, 1.0);
    colorTF->AddRGBPoint(192.0, 0.0, 1.0, 0.0);
    colorTF->AddRGBPoint(255.0, 0.0, 0.2, 0.0);

    vtkPiecewiseFunction *opacityTF = vtkPiecewiseFunction::New();
    opacityTF->AddPoint(20.0, 0.0);
    opacityTF->AddPoint(255.0, 0.2);

    vtkVolumeProperty *volumeProperty = vtkVolumeProperty::New();
    volumeProperty->SetColor(colorTF);
    volumeProperty->SetScalarOpacity(opacityTF);
    volumeProperty->ShadeOff();
    volumeProperty->SetInterpolationTypeToLinear();

    //create a volume to render
    vtkVolume *theVolume = vtkVolume::New();
    theVolume->SetMapper(volumeMapper);
    theVolume->SetProperty(volumeProperty);

    // a renderer and render window
    vtkRenderer *ren1 = vtkRenderer::New();
    vtkRenderWindow *renWin = vtkRenderWindow::New();
    renWin->AddRenderer(ren1);
    ren1->AddViewProp(theVolume);

    // an interactor
    vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
    iren->SetRenderWindow(renWin);

    ren1->SetBackground(1,1,1); // Background color white

    // render an image (lights and cameras are created automatically)
    renWin->Render();

    // begin mouse interaction
    iren->Initialize();
    iren->Start();

    return 1;
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20080808/855e889d/attachment.htm>


More information about the vtkusers mailing list