[vtkusers] Simple question in volume rendering

Eon Strife eon_strife at yahoo.com
Tue Feb 26 05:06:02 EST 2008


 Hi, I try to do volume rendering using ray casting on a n x n x n structured points, so basically I want to volume render a cube. Here is some part of my codes :


 ====    

vtkRenderer                    *VTKren1;
vtkRenderWindow                *VTKrenWin;
vtkRenderWindowInteractor    *VTKiren;
vtkUnsignedCharArray        *VTKscalars;
vtkStructuredPoints            *VTKreader;
vtkPiecewiseFunction        *VTKopacityTransferFunction;
vtkColorTransferFunction    *VTKcolorTransferFunction;
vtkVolumeProperty            *VTKvolumeProperty;
vtkVolumeRayCastCompositeFunction  *VTKcompositeFunction;
vtkVolumeRayCastMapper        *VTKvolumeMapper;
vtkVolume                    *VTKvolume;
vtkCamera                    *VTKcamera;

vtkStructuredPointsReader    *VTKstructuredPointsReader;
VTKren1 = vtkRenderer::New();

    VTKcamera = vtkCamera::New();
    VTKcamera->SetClippingRange(0, 100);
    VTKcamera->SetFocalPoint(0,0,0);
    VTKcamera->SetPosition(0, 0, 5);
    VTKcamera->SetViewUp(0, 1, 0);
    VTKren1->SetActiveCamera(VTKcamera);

    VTKrenWin = vtkRenderWindow::New();
    VTKrenWin->AddRenderer(VTKren1);
    VTKiren = vtkRenderWindowInteractor::New();
    VTKiren->SetRenderWindow(VTKrenWin);
    VTKscalars=vtkUnsignedCharArray::New();
    for(unsigned int j=0; j<SIZE; j++)
        for(unsigned int k=0; k<SIZE;k++)
            for(unsigned int i=0; i<SIZE; i++)
                VTKscalars->InsertTuple1(i+k*SIZE+j*SIZE*SIZE, 128);


    VTKreader=vtkStructuredPoints::New();
    VTKreader->SetDimensions(SIZE, SIZE, SIZE);
    VTKreader->SetOrigin(-0.5, -0.5, -0.5);
    VTKreader->SetSpacing(1.0/(SIZE-1), 1.0/(SIZE-1), 1.0/(SIZE-1));
    VTKreader->GetPointData()->SetScalars(VTKscalars);


    VTKopacityTransferFunction=vtkPiecewiseFunction::New();
    VTKopacityTransferFunction->AddPoint(0.0, 0.0);
    VTKopacityTransferFunction->AddPoint(255.0, 1.0);

    VTKcolorTransferFunction=vtkColorTransferFunction::New();
    VTKcolorTransferFunction->AddRGBPoint(0.0,   0.0, 0.0, 0.0);
    VTKcolorTransferFunction->AddRGBPoint(255.0, 0.0, 1.0, 0.0);

    VTKvolumeProperty=vtkVolumeProperty::New();
    VTKvolumeProperty->SetColor(VTKcolorTransferFunction);
    VTKvolumeProperty->SetScalarOpacity(VTKopacityTransferFunction);
    VTKvolumeProperty->ShadeOn();
    VTKvolumeProperty->SetInterpolationTypeToLinear();
    
    VTKcompositeFunction=vtkVolumeRayCastCompositeFunction::New();
    
    VTKvolumeMapper=vtkVolumeRayCastMapper::New();
    VTKvolumeMapper->CroppingOff();
    VTKvolumeMapper->SetVolumeRayCastFunction(VTKcompositeFunction);
    VTKvolumeMapper->SetInput(VTKreader);

    VTKvolume=vtkVolume::New();
    VTKvolume->SetMapper(VTKvolumeMapper);
    VTKvolume->SetProperty(VTKvolumeProperty);

    VTKren1->AddVolume(VTKvolume);
    VTKren1->SetBackground(0, 0, 0);
    VTKrenWin->SetSize(600,600);
    VTKrenWin->SetDesiredUpdateRate(1.0);
    VTKiren->Initialize();
====



 I use vtkStructuredPoint, and the points are arrange in n x n x n cube. When I run the program, however, the object shape seems to change from time to time when I rotate the scene, the object does not stay as cube, the length on either dimension is changing. Sometimes, it seems that the object is clipped. Can anybody tell me what's wrong ?
BTW, this is another version, in TCL (which is based on an example from VTK) for fast viewing :
===
# This is a simple volume rendering example that
# uses a vtkVolumeRayCast mapper

package require vtk
package require vtkinteraction

# Create the standard renderer, render window
# and interactor
vtkRenderer ren1
vtkRenderWindow renWin
    renWin AddRenderer ren1
vtkRenderWindowInteractor iren
    iren SetRenderWindow renWin

# init Array
set sp [expr 1.0/25.0]
vtkUnsignedCharArray scalars
for { set k 0 } { $k < 26 } { incr k 1 } {
    set z [expr -0.5 + $k * $sp]
    set kOffset [expr $k * 26 * 26]
    for { set j 0 } { $j < 26 } { incr j 1 } {
        set y [expr -0.5 + $j * $sp]
        set jOffset [expr $j * 26]
        for { set i 0 } { $i < 26 } { incr i 1 } {
            set x [expr -0.5 + $i * $sp]
            set offset [expr $i + $jOffset + $kOffset]
#            set s [expr (($x * $x + $y * $y + $z * $z - 0.4 * 0.4)+0.16)/0.75*255]
            set s 255
            scalars InsertTuple1 $offset $s
        }
    }
}


vtkStructuredPoints reader
    reader SetDimensions 26 26 26
    reader SetOrigin -0.5 -0.5 -0.5
    reader SetSpacing $sp $sp $sp
    [reader GetPointData] SetScalars scalars

# Create the reader for the data
# vtkStructuredPointsReader reader
#    reader SetFileName "$VTK_DATA_ROOT/Data/ironProt.vtk"

# Create transfer mapping scalar value to opacity
vtkPiecewiseFunction opacityTransferFunction
    opacityTransferFunction AddPoint  10   0.0
    opacityTransferFunction AddPoint  255  0.1

# Create transfer mapping scalar value to color
vtkColorTransferFunction colorTransferFunction
    colorTransferFunction AddRGBPoint      0.0 0.0 0.0 0.0
    colorTransferFunction AddRGBPoint     64.0 1.0 0.0 0.0
    colorTransferFunction AddRGBPoint    128.0 0.0 0.0 1.0
    colorTransferFunction AddRGBPoint    192.0 0.0 1.0 0.0
    colorTransferFunction AddRGBPoint    255.0 0.0 0.2 0.0

# The property describes how the data will look
vtkVolumeProperty volumeProperty
    volumeProperty SetColor colorTransferFunction
    volumeProperty SetScalarOpacity opacityTransferFunction
    volumeProperty ShadeOn
    volumeProperty SetInterpolationTypeToLinear

# The mapper / ray cast function know how to render the data
 vtkVolumeRayCastCompositeFunction  compositeFunction
 vtkVolumeRayCastMapper volumeMapper
    volumeMapper SetVolumeRayCastFunction compositeFunction
    volumeMapper SetInput reader

# The volume holds the mapper and the property and
# can be used to position/orient the volume
vtkVolume volume
    volume SetMapper volumeMapper
    volume SetProperty volumeProperty

ren1 AddVolume volume
ren1 SetBackground 1 1 1
renWin SetSize 600 600
renWin Render
renWin SetDesiredUpdateRate 1.0

proc TkCheckAbort {} {
  set foo [renWin GetEventPending]
  if {$foo != 0} {renWin SetAbortRender 1}
}
renWin AddObserver AbortCheckEvent {TkCheckAbort}

iren AddObserver UserEvent {wm deiconify .vtkInteract}
iren Initialize

wm withdraw .


===

 






Send instant messages to your online friends http://uk.messenger.yahoo.com 



More information about the vtkusers mailing list