[vtkusers] Problems with massProperties at getting area from ContourWidget

Rodrigo Lovera lobo.theslayer at gmail.com
Mon Dec 3 15:26:23 EST 2012


But, how could I get the polygon out of a contour?? I mean applying the
contour widget lets you define points and you can close that to forma a 2D
contour. Once that's finished how to get the polygon out of that contour??


2012/12/3 David Doria <daviddoria at gmail.com>

> 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
>



-- 
*Rodrigo aka WarHearT*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20121203/7158f61d/attachment.htm>


More information about the vtkusers mailing list