[vtkusers] Help changing view of structured grid

Heath Johnson heathbjohnson at gmail.com
Fri Dec 30 15:35:38 EST 2011


I am getting started with vtk-5.6 and am trying to make a very simple
example problem with a structured grid which can be viewed in
different ways - outline of edges, wireframe of connected points, and
simply the points themselves.  I started from the example problem
here:

http://www.vtk.org/Wiki/VTK/Examples/Cxx/StructuredGrid/VisualizeStructuredGrid

The code below displays the outline of the structured grid (using
vtkStructuredGridOutlineFilter) but I am unable to update the view to
show the wireframe or points.  I can follow other examples which show
only the points or the wireframe, but I want to be able to change how
a structured grid is viewed.  I know I am missing something about how
VTK works, but I cannot figure out how to do it correctly.  Can
somebody please help?

------------------------------------
#include <vtkSmartPointer.h>
#include <vtkStructuredGrid.h>
#include <vtkMath.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkProperty.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkStructuredGridOutlineFilter.h>

int main(int, char *[])
{
  // Create a grid
  vtkSmartPointer<vtkStructuredGrid> structuredGrid =
    vtkSmartPointer<vtkStructuredGrid>::New();

  vtkSmartPointer<vtkPoints> points =
    vtkSmartPointer<vtkPoints>::New();
  unsigned int numi = 3;
  unsigned int numj = 5;
  unsigned int numk = 4;

  for(unsigned int k = 0; k < numk; k++)
    {
    for(unsigned int j = 0; j < numj; j++)
      {
      for(unsigned int i = 0; i < numi; i++)
        {
        points->InsertNextPoint(i, j, k);
        }
      }
    }

  //specify the dimensions of the grid
  structuredGrid->SetDimensions(numi, numj, numk);
  structuredGrid->SetPoints(points);

  std::cout << "There are " << structuredGrid->GetNumberOfPoints() <<
" points." << std::endl;
  std::cout << "There are " << structuredGrid->GetNumberOfCells() << "
cells." << std::endl;

  vtkSmartPointer<vtkStructuredGridOutlineFilter> outlineFilter =
    vtkSmartPointer<vtkStructuredGridOutlineFilter>::New();
  outlineFilter->SetInputConnection(structuredGrid->GetProducerPort());
  outlineFilter->Update();

  // Create a mapper and actor
  vtkSmartPointer<vtkPolyDataMapper> mapper =
    vtkSmartPointer<vtkPolyDataMapper>::New();
  mapper->SetInputConnection(outlineFilter->GetOutputPort());
  vtkSmartPointer<vtkActor> actor =
    vtkSmartPointer<vtkActor>::New();
  actor->SetMapper(mapper);

  // Visualize
  vtkSmartPointer<vtkRenderer> renderer =
      vtkSmartPointer<vtkRenderer>::New();
  vtkSmartPointer<vtkRenderWindow> renderWindow =
      vtkSmartPointer<vtkRenderWindow>::New();
  renderWindow->AddRenderer(renderer);
  vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
      vtkSmartPointer<vtkRenderWindowInteractor>::New();
  renderWindowInteractor->SetRenderWindow(renderWindow);

  renderer->AddActor(actor);

  renderWindow->Render();

  // --- Everything above is from the example.
  // --- The commands below are what I am trying to do ---

  // Change how structured grid is viewed

  // -- View grid as wireframe between connected points
  sleep(2);
  std::cout << "Changing to surface representation" << std::endl;
  actor->GetProperty()->SetRepresentationToSurface();
  renderWindow->Render();

  // View grid as points only
  sleep(2);
  std::cout << "Changing to points representation" << std::endl;
  actor->GetProperty()->SetRepresentationToPoints();
  renderWindow->Render();

  renderWindowInteractor->Start();

  return EXIT_SUCCESS;
}
------------------------------------



More information about the vtkusers mailing list