[vtkusers] Problems with massProperties at getting area from ContourWidget

Rodrigo Lovera lobo.theslayer at gmail.com
Mon Dec 3 16:15:37 EST 2012


well, I tried to do what you told, I thought that
GetContourRepresentationAsPolyData() 'd be enough to get the polygon but
following the chode you showed I got to these:

#include <vtkSmartPointer.h>
#include <vtkProperty.h>
#include <vtkContourWidget.h>
#include <vtkOrientedGlyphContourRepresentation.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkCommand.h>
#include <vtkDebugLeaks.h>
#include <vtkCamera.h>
#include <vtkPlane.h>
#include <vtkPolyData.h>
#include <vtkCellArray.h>
#include <vtkPoints.h>
#include <vtkMath.h>
#include <vtkWidgetEvent.h>
#include <vtkWidgetEventTranslator.h>
#include <vtkTriangleFilter.h>
#include <vtkMassProperties.h>
#include <vtkPolygon.h>

int main( int argc, char *argv[] )
{
  // Create the RenderWindow, Renderer and both Actors
  //
  vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
  vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
  renderWindow->AddRenderer(renderer);

  vtkSmartPointer<vtkRenderWindowInteractor> interactor =
      vtkSmartPointer<vtkRenderWindowInteractor>::New();
  interactor->SetRenderWindow(renderWindow);

  renderer->SetBackground(0.1, 0.2, 0.4);
  renderWindow->SetSize(600, 600);

  vtkSmartPointer<vtkOrientedGlyphContourRepresentation> contourRep =
      vtkSmartPointer<vtkOrientedGlyphContourRepresentation>::New();
  contourRep->GetLinesProperty()->SetColor(1, 0, 0); //set color to red

  vtkSmartPointer<vtkContourWidget> contourWidget =
      vtkSmartPointer<vtkContourWidget>::New();
  contourWidget->SetInteractor(interactor);
  contourWidget->SetRepresentation(contourRep);
  contourWidget->On();

  for (int i = 0; i < argc; i++)
    {
    if (strcmp("-Shift", argv[i]) == 0)
      {
      contourWidget->GetEventTranslator()->RemoveTranslation(
                                        vtkCommand::LeftButtonPressEvent );
      contourWidget->GetEventTranslator()->SetTranslation(
                                        vtkCommand::LeftButtonPressEvent,
                                        vtkWidgetEvent::Translate );
      }
    else if (strcmp("-Scale", argv[i]) == 0)
      {
      contourWidget->GetEventTranslator()->RemoveTranslation(
                                        vtkCommand::LeftButtonPressEvent );
      contourWidget->GetEventTranslator()->SetTranslation(
                                        vtkCommand::LeftButtonPressEvent,
                                        vtkWidgetEvent::Scale );
      }
    }


  vtkSmartPointer<vtkPolyData> pd = vtkSmartPointer<vtkPolyData>::New();

  vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
  vtkSmartPointer<vtkCellArray> lines =
vtkSmartPointer<vtkCellArray>::New();
  vtkIdType* lineIndices = new vtkIdType[21];
  for (int i = 0; i< 20; i++)
    {
    const double angle = 2.0*vtkMath::Pi()*i/20.0;
    points->InsertPoint(static_cast<vtkIdType>(i), 0.1*cos(angle),
                        0.1*sin(angle), 0.0 );
    lineIndices[i] = static_cast<vtkIdType>(i);
    }

  lineIndices[20] = 0;
  lines->InsertNextCell(21,lineIndices);
  delete [] lineIndices;
  pd->SetPoints(points);
  pd->SetLines(lines);

  contourWidget->Initialize(pd);
  contourWidget->Render();
  renderer->ResetCamera();
  renderWindow->Render();

  interactor->Initialize();
  interactor->Start();

  vtkSmartPointer< vtkPolygon > polygon =
  vtkSmartPointer< vtkPolygon >::New();
  vtkSmartPointer< vtkPolyData > poly =
  vtkSmartPointer< vtkPolyData >::New();
  vtkSmartPointer< vtkCellArray > cellArray =
  vtkSmartPointer< vtkCellArray >::New();

  int numOfPoints =
contourRep->GetContourRepresentationAsPolyData()->GetNumberOfPoints();
  polygon->GetPointIds()->SetNumberOfIds(numOfPoints);
  for (int i = 0; i < numOfPoints; i++)
  {
  polygon->GetPointIds()->SetId(i, i);
  }

  cellArray->InsertNextCell(polygon);

poly->SetPoints(contourRep->GetContourRepresentationAsPolyData()->GetPoints());
  poly->SetPolys(cellArray);

  vtkSmartPointer< vtkTriangleFilter > triangles =
  vtkSmartPointer< vtkTriangleFilter >::New();
  //triangles->SetInput(contourRep->GetContourRepresentationAsPolyData());
  triangles->SetInput(poly);
  vtkSmartPointer< vtkMassProperties > massProp =
  vtkSmartPointer< vtkMassProperties >::New();
  massProp->SetInput(triangles->GetOutput());
  double area = massProp->GetSurfaceArea();

  std::cout<< area;

  contourWidget->Off();

  return EXIT_SUCCESS;
}

I see area changes as I modified the form of the contour and close the
render window, correct me if there's something wrong please and if you give
me some guide on that  GetContourRepresentationAsPolyData() if there's a
way to use it in a more direct way.

Thx for your help

Regards, Rodrigo


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

> On Mon, Dec 3, 2012 at 3:40 PM, Rodrigo Lovera <lobo.theslayer at gmail.com>
> wrote:
> > OK, I got that poly data but one I got that into the trianglefilter and
> then
> > to the massproperties is where that error shows up, so what I don't get
> is
> > that step between applying
> >
> > contourRep->GetContourRepresentationAsPolyData()
> >
> > and getting the poly into the trianglefilter.
>
> The easiest way would just be to read out the points from the line
> segments produced by the widget to create the polygon like I showed.
>
> David
>



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


More information about the vtkusers mailing list