[vtkusers] vtkCutter at bounds produces no results

Carl Trapani carl at skytopsoftware.com
Tue Sep 9 15:06:54 EDT 2008


Hi all,

I'm trying to cut a 2D slice out of a 3D object using vtkCutter and 
vtkPlane as the cutting function. It works very well except at the 
boundary conditions of the 3D object, specifically at the min boundary. 
I've created a program (CutterBoundaryTest.cxx - adapted from Cone6.cxx) 
to test using vtkCubeSource. The program source is pasted below along 
with CMakeLists.txt.

Running the program with boundary inputs produces the following images:

CutterBoundaryTest.exe 1.0    (z-max)
http://www.skytopsoftware.com/vtk/cut_at_1_image.jpg    (expected image 
output)

CutterBoundaryTest.exe -1.0    (z-min)
http://www.skytopsoftware.com/vtk/cut_at_-1_image.jpg    (NO IMAGE!)

CutterBoundaryTest.exe -0.99    (near z-min)
http://www.skytopsoftware.com/vtk/cut_at_-0.99_image.jpg    (expected 
image output)

1) Is this expected behavior or a bug?
2) Does DataSet.GetBounds(double[] bounds) return actual component point 
values from the dataset or should I try getting the extent?
3) Any suggestions on how I can get a cut at the lower bounds?

Thanks,
Carl Trapani

-----CutterBoundaryTest.cxx source--------------------------
#include <iostream>
#include <string>
#include <cstdlib>
#include <sstream>

#include "vtkCubeSource.h"
#include "vtkPolyDataMapper.h"
#include "vtkActor.h"
#include "vtkProperty.h"
#include "vtkRenderer.h"
#include "vtkPlane.h"
#include "vtkCutter.h"
#include "vtkWindowToImageFilter.h"
#include "vtkTiffWriter.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkInteractorStyleTrackballCamera.h"

/******************************************************************
* This program allows testing the cutting of a cube using a plane.
* There seems to be no results when cutting at the z-min bounds.
*
* The application creates a cube, a cutter and sets a plane
* as the cutting function. The plane origin is set to
* (0, 0, argv[1]) and the plane normal is set to (0, 0, 1) so that
* the cutting plane is parallel to the xy plane and cuts through
* the z axis at the specified argv[1] input. The resulting
* rendered image is then written to a file.
*
* usage: CutterBoundaryTest.exe <z-coord-to-cut-at>
* for example: CutterBoundaryTest.exe -1.0
*
* Outputs: a 2D image of the cut performated.
******************************************************************/
int main(int argc, char* argv[])
{
    std::string cutZCoordIn = "0.99";
    if (argc >= 2) {
        cutZCoordIn = std::string( argv[1] );
    }
    double cutZCoord;
    cutZCoord = atof( cutZCoordIn.c_str() );

    // Create a 2x2x2 cube
    vtkCubeSource *cube = vtkCubeSource::New();
    cube->SetXLength( 2.0 );
    cube->SetYLength( 2.0 );
    cube->SetZLength( 2.0 );
    cube->Update();
    cube->Print( cout );

    // Create a mapper for the cube
    vtkPolyDataMapper *cubeMapper = vtkPolyDataMapper::New();
    cubeMapper->SetInputConnection( cube->GetOutputPort() );
    cubeMapper->Update();

    // Get the dataSet for the cube from the mapper
    vtkPolyData *polyData = cubeMapper->GetInput();
    polyData->Update();
    double bounds[6];
    polyData->GetBounds( bounds );
    polyData->Print( cout );

    // Setup an actor for the cube
    vtkActor *cubeActor = vtkActor::New();
    cubeActor->SetMapper( cubeMapper );
    cubeActor->GetProperty()->SetColor( 0.0, 0.0, 0.0 );

    // Setup the cutting plane
    vtkPlane *cuttingPlane = vtkPlane::New();
    cuttingPlane->SetOrigin( 0.0, 0.0, cutZCoord );
    cuttingPlane->SetNormal( 0.0, 0.0, 1.0 );

    // Setup the cutter
    vtkCutter *cutter = vtkCutter::New();
    cutter->SetCutFunction( cuttingPlane );
    cutter->SetInput( polyData );
    cutter->Update();

    // Setup the cutter mapper
    vtkPolyDataMapper *cutMapper = vtkPolyDataMapper::New();
    cutMapper->SetInputConnection( cutter->GetOutputPort() );
    cutMapper->Update();

    // Setup the cutter actor
    vtkActor *cutActor = vtkActor::New();
    cutActor->SetMapper( cutMapper );
    cutActor->GetProperty()->SetColor( 0.0, 0.0, 0.0 );

    // Setup the cutter renderer
    vtkRenderer    *cutRen = vtkRenderer::New();
    cutRen->SetBackground( 1.0, 1.0, 1.0 );
    cutRen->AddActor( cutActor );

    // Write the output to a file
    vtkRenderWindow *cutRenWin = vtkRenderWindow::New();
    cutRenWin->AddRenderer( cutRen );
    cutRenWin->SetSize( 300, 300 );
    cutRenWin->Render();

    vtkWindowToImageFilter* w2i = vtkWindowToImageFilter::New();
    w2i->SetInput( cutRenWin );
    w2i->Update();
    vtkTIFFWriter* writer = vtkTIFFWriter::New();
    writer->SetCompressionToNoCompression();
    writer->SetInputConnection( w2i->GetOutputPort() );

    // Write the file for cutZCoord input
    std::ostringstream ost1;
    ost1 << "cut_at_" << cutZCoord << "_image.tif";
    writer->SetFileName( ost1.str().c_str() );
    writer->Update();
    writer->Write();

    // Show the 3D object and
    vtkRenderer *cubeRen= vtkRenderer::New();
    cubeRen->AddActor( cubeActor );
    cubeRen->SetBackground( 1.0, 1.0, 1.0 );

    vtkRenderWindow *cubeRenWin = vtkRenderWindow::New();
    cubeRenWin->AddRenderer( cubeRen );
    cubeRenWin->SetSize( 300, 300 );

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

    vtkInteractorStyleTrackballCamera *style =
        vtkInteractorStyleTrackballCamera::New();
    iren->SetInteractorStyle(style);

    // Start the event loop.
    iren->Initialize();
    iren->Start();

    //
    // Free up any objects we created.
    cube->Delete();
    cubeMapper->Delete();
    cubeActor->Delete();
    cuttingPlane->Delete();
    cutter->Delete();
    cutMapper->Delete();
    cutActor->Delete();
    cutRen->Delete();
    cutRenWin->Delete();
    w2i->Delete();
    writer->Delete();

    cubeRen->Delete();
    cubeRenWin->Delete();
    iren->Delete();
    style->Delete();

    return 0;
}


------CMakeLists.txt--------------------------------------
CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
PROJECT (CutterBoundaryTest)

FIND_PACKAGE(VTK REQUIRED)
IF(NOT VTK_USE_RENDERING)
  MESSAGE(FATAL_ERROR "Example ${PROJECT_NAME} requires VTK_USE_RENDERING.")
ENDIF(NOT VTK_USE_RENDERING)
INCLUDE(${VTK_USE_FILE})

ADD_EXECUTABLE(CutterBoundaryTest CutterBoundaryTest.cxx)

TARGET_LINK_LIBRARIES(CutterBoundaryTest vtkRendering)



More information about the vtkusers mailing list