[vtkusers] VTK_POLYHEDRON display and cutting issue

Reece Neel neel at aerosoftinc.com
Wed Mar 23 10:06:54 EDT 2016


I'm using a vtkUnstructuredGrid to make polyhedrons of different
shapes.  For example, I have a test routine that creates four polyhedrons
of the following shapes: a cube, a pyramid, a prism, and a tetrahedra.
The polyhedrons are all connected.   I then use a cutter to cut through the
resulting object.   Several issues that I've observed:

(1) The cutter does not do a complete cut through all the polyhedrons.
(2) One of the faces does not render shaded (when shading is on).
(3) Changing the order of the prism faces changes the cutting (makes it 
cut correctly).
      I've commented this out in the included code, line 191.

I've attached the routine in C and a picture of the results.   I'm doing 
this in vtk 7.0.
I'm applying this approach to more a more general case and I'm getting
gaps in the cutting plane where it appears the cutting fails to cut 
select polyhedrons.

I'm looking to see if I'm doing anything wrong or if there is a possible 
bug in the cutting
and display of VTK_POLYHEDRON cells.

Thanks,
Reece
-------------- next part --------------

#include <vtkSmartPointer.h>
#include <vtkVersion.h>
#include <vtkUnstructuredGrid.h>
#include <vtkDataSetMapper.h>
#include <vtkActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkCamera.h>
#include <vtkTextProperty.h>
#include <vtkTextMapper.h>
#include <vtkActor2D.h>
#include <vtkProperty.h>
#include <vtkGeometryFilter.h>
#include <vtkPlane.h>
#include <vtkCutter.h>
#include <vtkInteractorStyleTrackballCamera.h>

// This function returns a vtkUnstructured grid corresponding to the object.
vtkSmartPointer<vtkUnstructuredGrid> MakeFourPolyhedrons();
 

int TestCutterVTK(void)
{
  std::string title;
  vtkSmartPointer<vtkTextMapper>  textMapper;
  vtkSmartPointer<vtkActor2D> textActor;
 
  vtkSmartPointer<vtkUnstructuredGrid> uGrid;
  vtkSmartPointer<vtkDataSetMapper> mapper1;
  vtkSmartPointer<vtkDataSetMapper> mapper2;
  vtkSmartPointer<vtkActor> actor1;
  vtkSmartPointer<vtkActor> actor2;
  vtkSmartPointer<vtkRenderer> renderer;
 
  uGrid = MakeFourPolyhedrons();
  title = "Polyhedrons";

  vtkSmartPointer<vtkRenderWindow> renWin =
    vtkSmartPointer<vtkRenderWindow>::New();
  renWin->SetSize(600, 600);
  renWin->SetWindowName("Polyhedron Demonstration");
 
  vtkSmartPointer<vtkRenderWindowInteractor> iRen =
    vtkSmartPointer<vtkRenderWindowInteractor>::New();
  iRen->SetRenderWindow(renWin);
 
  // Create one text property for all
  vtkSmartPointer<vtkTextProperty> textProperty =
    vtkSmartPointer<vtkTextProperty>::New();
  textProperty->SetFontSize(10);
  textProperty->SetJustificationToCentered();
 
  // Create and link the mapper, actor, and renderer together.
  textMapper = vtkSmartPointer<vtkTextMapper>::New();
  textActor = vtkSmartPointer<vtkActor2D>::New();
 
  mapper1 = vtkSmartPointer<vtkDataSetMapper>::New();
  mapper2 = vtkSmartPointer<vtkDataSetMapper>::New();
  actor1 = vtkSmartPointer<vtkActor>::New();
  actor2 = vtkSmartPointer<vtkActor>::New();
  renderer = vtkSmartPointer<vtkRenderer>::New();
 
  // Geometric filter
  vtkGeometryFilter *filterGrid = vtkGeometryFilter::New();
  filterGrid->SetInputData(uGrid);
  filterGrid->Update(); 

  // Plane for cutting
  vtkSmartPointer<vtkPlane> p3d = vtkSmartPointer<vtkPlane>::New();
  p3d->SetOrigin(0.5,0.5,0.2);
  p3d->SetNormal(0,0,1);

  // Cutter
  vtkSmartPointer<vtkCutter> cutter = vtkSmartPointer<vtkCutter>::New();
  cutter->SetCutFunction(p3d);
  cutter->SetInputData(uGrid);
  cutter->Update();

  vtkPolyData* output = vtkPolyData::SafeDownCast(cutter->GetOutputDataObject(0));

  printf("Cutter: Number of cells = %d \n",(int)cutter->GetOutput()->GetNumberOfCells());

  mapper1->SetInputData(uGrid);
  mapper2->SetInputData(output);

  //  actor for cells/grid
  actor1->SetMapper(mapper1);
  //actor1->GetProperty()->SetRepresentationToWireframe();
  actor1->GetProperty()->SetOpacity(1.0);

  //  actor for the cutter
  actor2->SetMapper(mapper2);
  actor2->GetProperty()->SetRepresentationToWireframe();
  actor2->GetProperty()->SetColor(255, 0, 0);

  renderer->AddViewProp(actor1);
  renderer->AddViewProp(actor2);
 
  textMapper->SetInput(title.c_str());
  textActor->SetMapper(textMapper);
  textActor->SetPosition(50, 10);
  renderer->AddViewProp(textActor);
 
  renWin->AddRenderer(renderer);

  //  trackball style
  vtkInteractorStyleTrackballCamera *style = vtkInteractorStyleTrackballCamera::New();
  iRen->SetInteractorStyle(style);

 
  //  Viewport/Renderer set up
  int rendererSize = 600;
 
  renWin->SetSize(rendererSize, rendererSize);
 
  // (xmin, ymin, xmax, ymax)
  double viewport[4] = {0.0, 0.0, (double)rendererSize, (double)rendererSize};
 
  renderer->SetViewport(viewport);
  renderer->SetBackground(.2, .3, .4);
  renderer->ResetCamera();
  renderer->GetActiveCamera()->Azimuth(30);
  renderer->GetActiveCamera()->Elevation(-30);
  renderer->GetActiveCamera()->Zoom(0.85);
  renderer->ResetCameraClippingRange();
 
  iRen->Initialize();
 
  renWin->Render();
 
  iRen->Start();
 
  return EXIT_SUCCESS;
}



vtkSmartPointer<vtkUnstructuredGrid> MakeFourPolyhedrons()
{
 
  // Make four polyhedrons that consists of a cube,
  // a pyramid, a prism, and a tetrahedra
 
  vtkSmartPointer<vtkPoints> points =
    vtkSmartPointer<vtkPoints>::New();
  points->InsertNextPoint( 0.0,       0.0,         0.0);
  points->InsertNextPoint( 1.0,       0.0,         0.0);
  points->InsertNextPoint( 1.0,       1.0,         0.0);
  points->InsertNextPoint( 0.0,       1.0,         0.0);
  points->InsertNextPoint( 0.0,       0.0,         1.0);
  points->InsertNextPoint( 1.0,       0.0,         1.0);
  points->InsertNextPoint( 1.0,       1.0,         1.0);
  points->InsertNextPoint( 0.0,       1.0,         1.0);
  points->InsertNextPoint( 0.5,       1.5,         0.5);
  points->InsertNextPoint( 1.5,       0.5,         0.9);
  points->InsertNextPoint( 1.5,       0.5,         0.5);
  points->InsertNextPoint( 1.25,      0.5,        -0.1);

  //  Cube
  vtkIdType nFaceCube = 8;

  vtkIdType cubeFaces[30] = 
      {4, 0, 1, 2, 3,
       4, 0, 1, 5, 4,
       4, 0, 3, 7, 4,
       4, 3, 2, 6, 7,
       4, 1, 2, 6, 5,
       4, 4, 5, 6, 7};
 
  //  Pyramid
  vtkIdType nFacePyramid = 5; 

  vtkIdType pyramidFaces[21] = 
     {4, 3, 2, 7, 6,
      3, 3, 2, 8,
      3, 2, 6, 8,
      3, 3, 7, 8,
      3, 6, 7, 8};

  //  Prism (Wedge)
  vtkIdType nFacePrism = 5; 

  vtkIdType prismFaces[23] =
      {4, 2, 10, 9, 6,
       4, 2, 6, 5, 1,
       4, 1, 5, 9, 10,
       3, 1, 2, 10,
       3, 5, 6, 9};
/*  Changing to this gives a different result
  vtkIdType prismFaces[23] =
      {3, 1, 2, 10,
       3, 5, 6, 9,
       4, 2, 10, 9, 6,
       4, 2, 6, 5, 1,
       4, 1, 5, 9, 10};
*/

  //  Tetrahedra
  vtkIdType nFaceTet = 4; 

  vtkIdType tetFaces[16] =
      {3, 1, 11, 2, 
       3, 11, 10, 2,
       3, 10, 11, 1,
       3, 1, 2, 10}; 

  vtkSmartPointer<vtkUnstructuredGrid> uGrid =
    vtkSmartPointer<vtkUnstructuredGrid>::New();

  uGrid->InsertNextCell(VTK_POLYHEDRON, nFaceCube, cubeFaces);

  uGrid->InsertNextCell(VTK_POLYHEDRON, nFacePyramid, pyramidFaces);

  uGrid->InsertNextCell(VTK_POLYHEDRON, nFacePrism, prismFaces);

  uGrid->InsertNextCell(VTK_POLYHEDRON, nFaceTet, tetFaces);

  uGrid->SetPoints(points);
 
  return uGrid;
}

-------------- next part --------------
A non-text attachment was scrubbed...
Name: polyhedronCut.png
Type: image/png
Size: 12567 bytes
Desc: not available
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20160323/9e39f0aa/attachment.png>


More information about the vtkusers mailing list