[vtkusers] problem to use vtkCutter from vtkImplicitPlaneWidget

khinwee khinwee at yahoo.com
Fri Oct 29 05:39:43 EDT 2010


Hi To All VTK user,
I am writing the codes below to get the 2D plane from
vtkImplicitPlaneWidget, but the resultant image shows blue color only,
what's wrong with my codes here? Please see the figure and codes below:

//#include "stdafx.h"
#include "vtkActor.h"
#include "vtkAppendPolyData.h"
#include "vtkClipPolyData.h"
#include "vtkCommand.h"
#include "vtkConeSource.h"
#include "vtkGlyph3D.h"
#include "vtkImplicitPlaneWidget.h"
#include "vtkInteractorEventRecorder.h"
#include "vtkLODActor.h"
#include "vtkPlane.h"
#include "vtkPolyData.h"
#include "vtkImageShiftScale.h"
#include "vtkProperty.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkRenderer.h"
#include "vtkSphereSource.h"
//#include "vtkRegressionTestImage.h"
#include "vtkDebugLeaks.h"
#include "vtkDICOMImageReader.h"
#include "vtkPiecewiseFunction.h"
#include "vtkColorTransferFunction.h"
#include "vtkVolumeProperty.h"
#include "vtkVolumeRayCastCompositeFunction.h"
#include "vtkVolumeRayCastMapper.h"
#include "vtkVolume.h"
#include "vtkImageCast.h"
#include "vtkCutter.h"
#include "vtkPolyDataMapper.h"
#include "vtkCamera.h"
#include "vtkOutlineFilter.h"
#include <vtkJPEGReader.h>

class vtkTIPWCallback : public vtkCommand
{
public:
        static vtkTIPWCallback *New() 
        { return new vtkTIPWCallback; }

        virtual void Execute(vtkObject *caller, unsigned long, void*)
        {
                vtkImplicitPlaneWidget *planeWidget = 
                        reinterpret_cast<vtkImplicitPlaneWidget*>(caller);
                planeWidget->GetPlane(this-> Plane);
                this->Volume->VisibilityOn();
                planeWidget->DrawPlaneOff();

        }
                vtkTIPWCallback():
Plane(0),Volume(0),Camera(0),Renderer(0){}

        vtkRenderer *Renderer; 
        vtkCamera *Camera; 
        vtkPlane *Plane;
        vtkVolume *Volume;
};
int main( int argc, char *argv[] )
{
vtkJPEGReader * reader = vtkJPEGReader::New();
reader->SetDataExtent(0,237,0,174,1,180);
reader->SetFilePrefix("C:\\ Desktop\\test1\\img");
reader->SetFilePattern("%s%d.jpg");
reader->SetDataSpacing (10, 10, 10);

        reader->Update();
        int extent[6]; 
        reader->GetDataExtent(extent); 
        double spacing [3];  
        reader->GetDataSpacing(spacing);   
        int xx,yy,zz;
        xx=(int)((extent[1]+1)*spacing[0]); 
        yy=(int)((extent[3]+1)*spacing[1]); 
        zz=(int)((extent[5]+1)*spacing[2]); 

        vtkImageShiftScale *ShiftScale = vtkImageShiftScale::New();
        ShiftScale->SetInput((vtkDataObject *)reader->GetOutput());
        ShiftScale->SetOutputScalarTypeToShort();
        ShiftScale->SetShift (1024); 
        ShiftScale->ClampOverflowOn(); 

        vtkImageCast *readerImageCast = vtkImageCast::New(); 
        readerImageCast->SetInput((vtkDataObject *)ShiftScale->GetOutput()); 
        readerImageCast->SetOutputScalarTypeToUnsignedShort();        
        readerImageCast->ClampOverflowOn(); 

        vtkPiecewiseFunction *opacityTransferFunction =
vtkPiecewiseFunction::New(); 
        opacityTransferFunction->AddPoint(1024+20, 0.0); 
        opacityTransferFunction->AddPoint(1024+255, 0.2); 

        vtkColorTransferFunction *colorTransferFunction =
vtkColorTransferFunction::New(); 
        colorTransferFunction->AddRGBPoint(1024+0.0, 0.0, 0.5, 0.0);
        colorTransferFunction->AddRGBPoint(1024+60.0, 1.0, 0.0, 0.0); 
        colorTransferFunction->AddRGBPoint(1024+128.0, 0.2, 0.1, 0.9); //
        colorTransferFunction->AddRGBPoint(1024+196.0, 0.27, 0.21, 0.1); //
        colorTransferFunction->AddRGBPoint(1024+255.0, 0.8, 0.8, 0.8); //

        vtkVolumeProperty *volumeProperty = vtkVolumeProperty::New(); //
        volumeProperty->SetColor(colorTransferFunction); ///
        volumeProperty->SetScalarOpacity(opacityTransferFunction);//
        volumeProperty->ShadeOn(); //
        volumeProperty->SetInterpolationTypeToLinear(); //
        volumeProperty->SetAmbient(0.2); //
        volumeProperty->SetDiffuse(0.9); //
        volumeProperty->SetSpecular(0.2); //
        volumeProperty->SetSpecularPower(10); //

                vtkPlane *plane = vtkPlane::New();

        vtkVolumeRayCastCompositeFunction *compositeFunction =
vtkVolumeRayCastCompositeFunction::New(); //
        vtkVolumeRayCastMapper *volumeMapper =
vtkVolumeRayCastMapper::New(); //
        volumeMapper->SetVolumeRayCastFunction(compositeFunction); //
       
volumeMapper->SetInputConnection(readerImageCast->GetOutputPort());//
                volumeMapper->AddClippingPlane(plane); //

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

        vtkOutlineFilter *outline = vtkOutlineFilter::New();
        outline->SetInputConnection( readerImageCast->GetOutputPort() );
        vtkPolyDataMapper *outlinemapper = vtkPolyDataMapper::New();
        outlinemapper->SetInputConnection( outline->GetOutputPort() );
        vtkActor * outlineactor = vtkActor::New();
        outlineactor->SetMapper( outlinemapper );
        outlineactor->GetProperty()->SetColor( 0.0, 0.0, 1.0 );

                //vtkCutter start here
        vtkCutter *planecutter=vtkCutter::New();
//      planecutter->SetInputConnection( readerImageCast->GetOutputPort() );
                planecutter->SetInput(readerImageCast->GetInput());
        planecutter->SetCutFunction(plane);
                planecutter->Update();
       
        vtkPolyDataMapper *cutterMapper=vtkPolyDataMapper::New();
        cutterMapper->SetInputConnection(planecutter->GetOutputPort() );

        vtkActor *cut=vtkActor::New();
        cut->SetMapper(cutterMapper);

        vtkRenderer *image_renderer = vtkRenderer::New();
         image_renderer->AddActor( cut );
        image_renderer->AddActor( outlineactor );
        image_renderer->SetViewport( 0.5, 0.0, 1.0, 1.0 );
        image_renderer->SetBackground( 0.0, 0.5, 0.5 );

        vtkRenderer *ren1 = vtkRenderer::New(); 
                ren1->AddActor( volume );
        ren1->AddActor( outlineactor );
        ren1->SetViewport( 0.0, 0.0, 0.5, 1.0 );
        ren1->SetBackground( 1.0, 1.0, 1.0 ); 

        vtkRenderWindow *renWin = vtkRenderWindow::New(); 
        renWin->AddRenderer(ren1); 
        renWin->AddRenderer(image_renderer );

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


                vtkTIPWCallback *myCallback = vtkTIPWCallback::New();
        vtkCamera *ccamera = vtkCamera::New();
        myCallback-> Plane = plane;
        myCallback->Volume = volume;
        myCallback->Camera = ccamera;
        myCallback->Renderer = image_renderer;

        vtkImplicitPlaneWidget *planeWidget = vtkImplicitPlaneWidget::New(); 
        planeWidget->SetCurrentRenderer( ren1 );
        planeWidget->SetInteractor(iren); 
        planeWidget->SetPlaceFactor(1.25); 
        planeWidget->GetPlaneProperty()->SetOpacity  ( 0.1 );   
        planeWidget->GetOutlineProperty()->SetColor(0,0,1);  
        planeWidget->SetInput((vtkDataSet *)readerImageCast->GetOutput()); 
        planeWidget-> PlaceWidget(); 
        planeWidget->OutlineTranslationOff(); 
        planeWidget->SetNormal( 1.0, 0.0, 0.0 );
        planeWidget->SetOrigin( xx/2,yy/2,zz/2 ); 
        planeWidget->On();
               
planeWidget->AddObserver(vtkCommand::InteractionEvent,myCallback); 

        renWin->SetSize(800, 600);
        iren->SetKeyCode('i');
        iren->Initialize();
        renWin->Render();
        iren->Start();
        myCallback->Delete();
        plane->Delete();
        planeWidget->Delete();
        iren->Delete();
        renWin->Delete();
        ren1->Delete();
        image_renderer->Delete();

        return 0;
}

http://vtk.1045678.n5.nabble.com/file/n3241759/aa.jpg 
the resultant 2d images only appear as blue plane

http://vtk.1045678.n5.nabble.com/file/n3241759/bb.jpg 

is that possible i disable the codes below:

//#include "vtkRegressionTestImage.h"

If i include this class, the program appear as error : cannot open include
directory. I am using VTK 5.6 version. I don't understand why this class is
not included in the latest version of VTK installation. Anyone knows?

regards,
vislai
-- 
View this message in context: http://vtk.1045678.n5.nabble.com/problem-to-use-vtkCutter-from-vtkImplicitPlaneWidget-tp3241759p3241759.html
Sent from the VTK - Users mailing list archive at Nabble.com.



More information about the vtkusers mailing list