[vtkusers] Can't get a vtkActor2D and vtkPolyDataMapper2D to render

Philip Schuchardt ohc at vt.edu
Fri Oct 27 00:45:58 EDT 2006


I'm pretty new to VTK.  I trying to get a simple polygon to render in 2d.  I 
can get the same polygon to render in 3d but not in 2d.

Suggestions?

Thanks
Philip Schuchardt

Source:
#include "vtkActor2D.h"
#include "vtkCellArray.h"
#include "vtkDoubleArray.h"
#include "vtkFloatArray.h"
#include "vtkIntArray.h"
#include "vtkPointData.h"
#include "vtkPoints.h"
#include "vtkPolyData.h"
#include "vtkPolyDataMapper2D.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkRenderer.h"
#include "vtkProperty2D.h" 
#include <iostream>
#include "vtkCoordinate.h"

int main()
{
    int i;

  // Create a float array which represents the points.
  vtkFloatArray* pcoords = vtkFloatArray::New();

  // Note that by default, an array has 1 component.
  // We have to change it to 3 for points
  pcoords->SetNumberOfComponents(3);
  // We ask pcoords to allocate room for at least 4 tuples
  // and set the number of tuples to 4.
  pcoords->SetNumberOfTuples(4);
  // Assign each tuple. There are 5 specialized versions of SetTuple:
  // SetTuple1 SetTuple2 SetTuple3 SetTuple4 SetTuple9
  // These take 1, 2, 3, 4 and 9 components respectively.
  float pts[4][3] = { {0.0, 0.0, 0.0}, {0.0, 2.0, 0.0},
                      {3.0, 0.0, 0.0}, {2.3, 1.0, 0.0} };
  for (i=0; i<4; i++)
    {
    pcoords->SetTuple(i, pts[i]);
    }

  // Create vtkPoints and assign pcoords as the internal data array.
  vtkPoints* points = vtkPoints::New();
  points->SetData(pcoords);

  // Create the cells. In this case, a triangle strip with 2 triangles
  // (which can be represented by 4 points)
  vtkCellArray* strips = vtkCellArray::New();
  strips->InsertNextCell(4);
  strips->InsertCellPoint(0);
  strips->InsertCellPoint(1);
  strips->InsertCellPoint(2);
  strips->InsertCellPoint(3);

  // Create the dataset. In this case, we create a vtkPolyData
  vtkPolyData* polydata = vtkPolyData::New();
  // Assign points and cells
  polydata->SetPoints(points);
  polydata->SetStrips(strips);


  vtkPolyDataMapper2D* mapper = vtkPolyDataMapper2D::New();
  mapper->SetInput(polydata);
  
  // Create an actor.
  vtkActor2D* actor = vtkActor2D::New();
  actor->SetMapper(mapper);
  
  
  // Create the rendering objects.

  vtkRenderer* ren = vtkRenderer::New();
  ren->AddActor(actor);
  ren->SetLayer(0);

  
  vtkRenderWindow* renWin = vtkRenderWindow::New();
  renWin->SetNumberOfLayers(1);
  renWin->AddRenderer(ren);
  
  vtkRenderWindowInteractor* iren = vtkRenderWindowInteractor::New();
  iren->SetRenderWindow(renWin);
  iren->Initialize();
  iren->Start();

  pcoords->Delete();
  points->Delete();
  strips->Delete();
  polydata->Delete();
  mapper->Delete();
  actor->Delete();
  ren->Delete();
  renWin->Delete();
  iren->Delete();
  return 0;
}



More information about the vtkusers mailing list