[vtkusers] Problems with massProperties at getting area from ContourWidget

David Doria daviddoria at gmail.com
Mon Dec 3 14:56:46 EST 2012


This also has nothing to do with the widget. You can produce the same
error with much simpler code (see below).

You can see from here:
http://www.vtk.org/doc/nightly/html/vtkCellType_8h_source.html

that "3" means VTK_LINE, which makes sense, because you added lines to
the polydata. The vtkTriangleFilter doesn't sound like it triangulates
the area inside the contour, but rather only divides existing polygons
into triangles:
http://www.vtk.org/doc/nightly/html/classvtkTriangleFilter.html#details

So what you have to do instead is make a polygon from your points,
then it seems to work:

#include <vtkSmartPointer.h>
#include <vtkPolygon.h>
#include <vtkPolyData.h>
#include <vtkCellArray.h>
#include <vtkPoints.h>
#include <vtkMath.h>
#include <vtkTriangleFilter.h>
#include <vtkMassProperties.h>

int main()
{
  vtkSmartPointer<vtkPolyData> polydata = vtkSmartPointer<vtkPolyData>::New();
  vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
  vtkSmartPointer<vtkCellArray> cellArray =
vtkSmartPointer<vtkCellArray>::New();

  const unsigned int numberOfPoints = 21;
  // Create the polygon
  vtkSmartPointer<vtkPolygon> polygon =
    vtkSmartPointer<vtkPolygon>::New();
  polygon->GetPointIds()->SetNumberOfIds(numberOfPoints);

  vtkIdType* lineIndices = new vtkIdType[numberOfPoints];
  for (int i = 0; i < numberOfPoints; i++)
    {
    polygon->GetPointIds()->SetId(i, i);

    const double angle =
2.0*vtkMath::Pi()*i/static_cast<double>(numberOfPoints);
    points->InsertPoint(static_cast<vtkIdType>(i), 0.1*cos(angle),
                        0.1*sin(angle), 0.0 );
    lineIndices[i] = static_cast<vtkIdType>(i);
    }

  cellArray->InsertNextCell(polygon);
  polydata->SetPoints(points);
  polydata->SetPolys(cellArray);

  vtkSmartPointer< vtkTriangleFilter > triangles =
          vtkSmartPointer< vtkTriangleFilter >::New();
  triangles->SetInputData(polydata);
  triangles->Update();

  vtkSmartPointer< vtkMassProperties > massProp =
          vtkSmartPointer< vtkMassProperties >::New();
  massProp->SetInputConnection(triangles->GetOutputPort());
  massProp->Update();
  double area = massProp->GetSurfaceArea();

  std::cout << area;

}

David



More information about the vtkusers mailing list