[vtkusers] help me for volume rendering

gaoqx gaoqx at neusoft.com
Thu Jul 7 03:50:48 EDT 2005


hello:
        I am a newer for vtk. My programe is to render head.  But it isnot appear.
this is my code.
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkVolume16Reader.h"
#include "vtkVolumeRayCastCompositeFunction.h"
#include "vtkVolumeRayCastMapper.h"
#include "vtkVolumeRayCastCompositeFunction.h"
#include "vtkPiecewiseFunction.h"
#include "vtkColorTransferFunction.h"
#include "vtkVolumeProperty.h"
#include "vtkVolume.h"
#include "vtkOutlineFilter.h"
#include "vtkPolyDataMapper.h"
#include "vtkActor.h"
#include "vtkCamera.h"
#include "vtkProperty.h"
#include "vtkInteractorStyleTrackballCamera.h"
#include "vtkImageData.h"
#include "vtkPointData.h"
#include "vtkCellData.h"
#include "vtkDataSetAttributes.h"
#include "vtkVolumeRayCastMIPFunction.h"




int main (int argc, char **argv)
{
  /*if (argc < 2)
    {
      cout << "Usage: " << argv[0] << " DATADIR/headsq/quarter " << endl;
    return 1;
    }*/

vtkVolume16Reader *v16 = vtkVolume16Reader::New();
    v16->SetDataDimensions(64,64);
    v16->SetDataByteOrderToLittleEndian();
   v16->SetFilePrefix ("E:/DATADIR/headsq/quarter");
    v16->SetImageRange(1, 93);
   // v16->SetDataSpacing (3.2, 3.2, 1.5);
    v16->SetDataSpacing (3.2, 3.2, 1.5);
 //v16->ReleaseDataFlagOn();
 //v16->SetDataMask(0x7fff);
 v16->Update();


vtkImageData *imageData = vtkImageData::New();
 imageData->SetDimensions(64,64,93);
 imageData->SetSpacing(3.2, 3.2, 1.5);
 imageData->SetOrigin(0.0,0.0,0.0);
 imageData->SetScalarTypeToUnsignedChar();
 imageData->SetNumberOfScalarComponents(1);
 imageData->AllocateScalars();//Allocate the vtkScalars object associated with this object
 imageData->GetPointData()->CopyAllOn();//return pointer to this dataset's point data THIS METHOD IS THREAD 
 imageData->GetPointData()->CopyScalarsOn();//Turn on copying of all data

/*for (int i=0; i<64*64*93; i++)
{
 imageData->GetPointData()->CopyData( v16->GetOutput()->GetPointData(), i,
i );
} //Copy the attribute data from one id to another
*/
unsigned char *ptr2 =(unsigned char *) v16->GetOutput()->GetScalarPointer();
unsigned char *ptr =(unsigned char *) imageData->GetScalarPointer();

for (int i=0; i<64*64*93 ; i++)
{
 *ptr = *ptr2;
 ptr++;
 ptr2++;
}// Copy the attribute data from one id to another

vtkVolumeRayCastCompositeFunction  *compositeFunction =vtkVolumeRayCastCompositeFunction::New();//a ray function that can be used within a vtkVolumeRayCastMapper.
//vtkVolumeRayCastMIPFunction *compositeFunction = vtkVolumeRayCastMIPFunction::New();
vtkVolumeRayCastMapper *volumeMapper = vtkVolumeRayCastMapper::New();
   volumeMapper->SetInput(imageData);
    //volumeMapper->SetInput(v16); //testing 
 volumeMapper->SetVolumeRayCastFunction(compositeFunction);
 //volumeMapper->CroppingOn();//Turn On/Off orthogonal cropping
 //volumeMapper->SetCroppingRegionPlanes(60,100,60,100,60,100);// Set/Get the Cropping Region Planes
 //volumeMapper->SetCroppingRegionFlags(0x7ffffdf);
// Set the flags for the cropping regions

vtkPiecewiseFunction *tfun = vtkPiecewiseFunction::New();//Defines a piecewise linear function mapping
    
     tfun->AddPoint(20.0,0.0);
     tfun->AddPoint(80.0,0.1);
     tfun->AddPoint(101.0,0.4);
     tfun->AddPoint(105.0,0.5);
     tfun->AddPoint(155.0,0.8);
     //tfun->AddPoint(255,1.0);

vtkColorTransferFunction *ctfun = vtkColorTransferFunction::New();
    
     ctfun->AddRGBPoint(40.0, 0.0, 0.0, 0.0);
     ctfun->AddRGBPoint(60.0, 0.9, 0.2, 0.3);
     ctfun->AddRGBPoint(80.0, 0.81, 2.7, 0.1);
     ctfun->AddRGBPoint(90.0, 0.2, 0.2, 0.2);
     ctfun->AddRGBPoint(105.0, 1.0, 0.0, 0.0);
  ctfun->AddRGBPoint(106.0, 1.0, 1.0, 1.0);
      
vtkVolumeProperty *volumeProperty = vtkVolumeProperty::New();
    volumeProperty->SetColor(ctfun);
    volumeProperty->SetScalarOpacity(tfun);
    volumeProperty->SetInterpolationTypeToLinear();
 //volumeProperty->SetInterpolationTypeToNearest();
   // volumeProperty->SetCompositeMethodToClassifyFirst();
    volumeProperty->ShadeOn();//Set/Get the shading of a volume


vtkVolume *newvol = vtkVolume::New();
    newvol->SetMapper(volumeMapper);
    newvol->SetProperty(volumeProperty);



vtkOutlineFilter *outline = vtkOutlineFilter::New();
    outline->SetInput((vtkDataSet *) v16->GetOutput());
    //outline->SetInput((vtkDataSet *) imageData->GetOutput());//testing

vtkPolyDataMapper *outlineMapper = vtkPolyDataMapper::New();
    outlineMapper->SetInput(outline->GetOutput());

vtkActor *outlineActor = vtkActor::New();
    outlineActor->SetMapper(outlineMapper);
    outlineActor->GetProperty()->SetColor(1,1,1);



vtkCamera *Camera = vtkCamera::New();
    Camera->SetViewUp (0, 0, -1);
    Camera->SetPosition (140, 600, -100);
    Camera->SetFocalPoint (100, 100, 75);
    Camera->ComputeViewPlaneNormal();
    

vtkRenderer *ren = vtkRenderer::New();
    ren->AddActor(outlineActor);
    ren->AddVolume(newvol);
    ren->SetBackground(0,0,0);
    ren->SetActiveCamera(Camera);


vtkRenderWindow *renWin = vtkRenderWindow::New();
    renWin->AddRenderer(ren);
    renWin->SetSize(640,480);
  

vtkInteractorStyleTrackballCamera *style =
vtkInteractorStyleTrackballCamera::New();

vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
    iren->SetRenderWindow(renWin);
    iren->SetInteractorStyle(style);
    iren->Initialize();
    iren->Start();




    v16->Delete();
    compositeFunction->Delete();
    volumeMapper->Delete();
    tfun->Delete();
    ctfun->Delete();
    volumeProperty->Delete();
    newvol->Delete();
    outline->Delete();
    outlineMapper->Delete();
    outlineActor->Delete();
    Camera->Delete();
    ren->Delete();
    renWin->Delete();
    iren->Delete();
    style->Delete();

  return 0;
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20050707/99f72cbd/attachment.htm>


More information about the vtkusers mailing list