Test.cxx

From KitwarePublic
Jump to navigationJump to search
//
// This example reads a volume dataset through ITK 
// and then displays it through VTK.
//
// Press "i"-Button to get the ClippingBox
//
// adapted from
// http://isgwww.cs.uni-magdeburg.de/cv/lehre/Visualization/uebungsunterlagen/direktVolVisRayCast_Clipping01.cxx

#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkActor.h"

#include "vtkColorTransferFunction.h"
#include "vtkPiecewiseFunction.h"
#include "vtkVolumeProperty.h"
#include "vtkVolumeRayCastCompositeFunction.h"
#include "vtkFixedPointVolumeRayCastMapper.h"
#include "vtkVolume.h"

#include "vtkBoxWidget.h"
#include "vtkPlanes.h"
#include "vtkProperty.h"
#include "vtkCommand.h"

#include "itkImageToVTKImageFilter.h"
#include "itkImage.h"
#include "itkImageFileReader.h"

// Callback for the interaction -----------------------------------------------
class vtkMyCallback : public vtkCommand
{
public:
  vtkFixedPointVolumeRayCastMapper *rcm;
  static vtkMyCallback *New() 
    { return new vtkMyCallback; }
    virtual void Execute(vtkObject *caller, unsigned long, void*)
    {
	   vtkBoxWidget *boxWidget = reinterpret_cast<vtkBoxWidget*>(caller);
	   vtkPlanes *planes = vtkPlanes::New();
	   // The implicit function vtkPlanes is used in conjunction with the
	   // volume ray cast mapper to limit which portion of the volume is
	   // volume rendered.
	   boxWidget->GetPlanes(planes);
	   this->rcm->SetClippingPlanes(planes);
    }
};
// -----------------------------------------------------------------------------

int main (int argc, char **argv) {

  // Create the renderer, the render window, and the interactor. The renderer
  // draws into the render window, the interactor enables mouse- and 
  // keyboard-based interaction with the data within the render window.
  vtkRenderer *aRenderer = vtkRenderer::New();
  vtkRenderWindow *renWin = vtkRenderWindow::New();
    renWin->AddRenderer(aRenderer);
  vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
    iren->SetRenderWindow(renWin);


  // Load the Image via ITK

  const unsigned int Dimension = 3;
  typedef unsigned char InputPixelType;

  typedef itk::Image<InputPixelType, Dimension> InputImageType;
  typedef itk::ImageFileReader<InputImageType> ReaderType;
  ReaderType::Pointer reader = ReaderType::New();
  reader->SetFileName("/home/alex/interp/61457548-roi-8bit.tif");

  try
    {
        reader->Update();
    }
  catch( itk::ExceptionObject & excep )
     {
        std::cerr << "Exception while reading input!"<< std::endl;
        std::cerr << excep << std::endl;
     }

  typedef itk::ImageToVTKImageFilter<InputImageType> FilterType;
  FilterType::Pointer imtoimfilt = FilterType::New();
  imtoimfilt->SetInput( reader->GetOutput() );
  
  try
    {
            imtoimfilt->Update();
    }
  catch( itk::ExceptionObject & except)
    {
            std::cerr << "Exception while converting!"<< std::endl;
            std::cerr << except << std::endl;
    }
/*
  // Create transfer mapping scalar value to color
  vtkColorTransferFunction *colTransFunc = vtkColorTransferFunction::New();
    colTransFunc->AddRGBPoint(   0.0,0.0,0.0,0.0);
    colTransFunc->AddRGBPoint( 500.0,1.0,0.49,0.25);
    colTransFunc->AddRGBPoint(1150.0,0.8,0.8,0.8);
    colTransFunc->AddRGBPoint(2000.0,0.0,0.2,0.0);
*/
  // Create transfer mapping scalar value to opacity
  vtkPiecewiseFunction *opacTransFunc = vtkPiecewiseFunction::New();
    opacTransFunc->AddPoint(0, 1.0);
//    opacTransFunc->AddPoint(50, 0.5);
    opacTransFunc->AddPoint(100, 0.0);
  
  // The property describes how the data will look
  vtkVolumeProperty *vp = vtkVolumeProperty::New();
//    vp->SetColor(colTransFunc);
    vp->SetScalarOpacity(opacTransFunc);
    vp->ShadeOn();
    vp->SetInterpolationTypeToLinear();

//  vtkVolumeRayCastCompositeFunction *rcf = vtkVolumeRayCastCompositeFunction::New();
  vtkFixedPointVolumeRayCastMapper *rcm = vtkFixedPointVolumeRayCastMapper::New();
//    rcm->SetVolumeRayCastFunction(rcf);
    rcm->SetInput( imtoimfilt->GetOutput());
  // The volume holds the mapper and the property and
  // can be used to position/orient the volume
  vtkVolume *volHead= vtkVolume::New();
  volHead->SetMapper(rcm);
  volHead->SetProperty(vp);

  // Actors are added to the renderer.
  aRenderer->AddActor(volHead);

  // Set the size of the render window (expressed in pixels).
  renWin->SetSize(640, 480);
  aRenderer->SetBackground(0.3, 0.6, 1.0);

  // ------------------------------------------------------------- CLIP-BOX -----------
  // The SetInteractor method is how 3D widgets are associated with the render
  // window interactor. Internally, SetInteractor sets up a bunch of callbacks
  // using the Command/Observer mechanism (AddObserver()).
  vtkBoxWidget *boxWidget = vtkBoxWidget::New();
    boxWidget->SetInteractor(iren);
    boxWidget->SetPlaceFactor(1.0);
 
  // - - - - - - - - - - - - - - - - - - - - -
  vtkMyCallback *mcb = vtkMyCallback::New();
  mcb->rcm=rcm;

  boxWidget->AddObserver(vtkCommand::InteractionEvent, mcb);
  mcb->Delete();

  // Place the interactor initially. The output of the reader is used to place
  // the box widget.
    boxWidget->SetInput((vtkDataSet *) imtoimfilt->GetOutput());
    boxWidget->PlaceWidget();
    boxWidget->InsideOutOn();
    boxWidget->GetOutlineProperty()->SetRepresentationToWireframe();
    boxWidget->GetOutlineProperty()->SetAmbient(1.0);
    boxWidget->GetOutlineProperty()->SetAmbientColor(1,1,1);
    boxWidget->GetOutlineProperty()->SetLineWidth(3);
    boxWidget->GetSelectedOutlineProperty()->SetRepresentationToWireframe();
    boxWidget->GetSelectedOutlineProperty()->SetAmbient(1.0);
    boxWidget->GetSelectedOutlineProperty()->SetAmbientColor(1,0,0);
    boxWidget->GetSelectedOutlineProperty()->SetLineWidth(3);


  // Start the Interactor
  iren->Initialize();
  iren->Start(); 


  // It is important to delete all objects created previously to prevent
  // memory leaks. In this case, since the program is on its way to
  // exiting, it is not so important. But in applications it is
  // essential.
//  colTransFunc->Delete();
//  opacTransFunc->Delete();
//  vp->Delete();

  boxWidget->Delete();

  aRenderer->Delete();
  renWin->Delete();
  iren->Delete();

  return 0;
}



Complete Setup: [Welcome | Site Map]