[vtkusers] vtkusers Digest, Vol 92, Issue 37

Heath Johnson heathbjohnson at gmail.com
Mon Jan 2 13:15:25 EST 2012


Lubos,

Thank you very much for your help in answering my question.  I know
that the code below is very simple, but I'm including it as a working
example for archival purposes, in case it may help other beginners.
This example code starts with the default surface representation of a
structured grid.  Then it changes to point and outline
representations, before going back to surface.

Heath

----------------------
#include <vtkSmartPointer.h>
#include <vtkStructuredGrid.h>
#include <vtkMath.h>
#include <vtkActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkProperty.h>
#include <vtkDataSetMapper.h>
#include <vtkPolyDataMapper.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkStructuredGridOutlineFilter.h>
#include <vtkStructuredGridGeometryFilter.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();

  vtkSmartPointer<vtkStructuredGridGeometryFilter> pointFilter =
    vtkSmartPointer<vtkStructuredGridGeometryFilter>::New();
  pointFilter->SetInputConnection(structuredGrid->GetProducerPort());
  pointFilter->Update();

  // Create a mapper
  vtkSmartPointer<vtkDataSetMapper> mapper =
    vtkSmartPointer<vtkDataSetMapper>::New();
  mapper->SetInputConnection(structuredGrid->GetProducerPort());

  // Create an actor
  vtkSmartPointer<vtkActor> actor =
    vtkSmartPointer<vtkActor>::New();
  actor->SetMapper(mapper);
  actor->GetProperty()->SetPointSize(3);

  // 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();

  sleep(2);
  std::cout << "Changing to point representation" << std::endl;
  mapper->SetInputConnection(pointFilter->GetOutputPort());
  actor->SetMapper(mapper);
  renderWindow->Render();

  sleep(2);
  std::cout << "Changing to outline representation" << std::endl;
  mapper->SetInputConnection(outlineFilter->GetOutputPort());
  actor->SetMapper(mapper);
  renderWindow->Render();

  sleep(2);
  std::cout << "Changing to surface representation" << std::endl;
  mapper->SetInputConnection(structuredGrid->GetProducerPort());
  actor->SetMapper(mapper);
  renderWindow->Render();

  std::cout << "Starting interactor" << std::endl;
  renderWindowInteractor->Start();

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

> ---------- Forwarded message ----------
> From: Lubos Brieda <lbrieda at yahoo.com>
> To: "vtkusers at vtk.org" <vtkusers at vtk.org>
> Cc:
> Date: Fri, 30 Dec 2011 13:56:52 -0800 (PST)
> Subject: Re: [vtkusers] Help changing view of structured grid
> Heath,
>
> vtkOutlineFilter will give you the outline, i.e. the bounding box for the mesh. To show the structured grid as a wireframe, simply do the following:
>
> mapper->SetInputConnection(structuredGrid->GetProducerPort());
>   vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
>   actor->SetMapper(mapper);
>   actor->GetProperty()->SetRepresentationToWireframe();
>
>
> The default representation is surface.
>
> Also, under vtkStructuredGrid at http://www.vtk.org/Wiki/VTK/Examples/Cxx you will find few additional examples for visualizing structured grids.
>
> --
> Lubos Brieda
> particleincell.com



More information about the vtkusers mailing list