[vtkusers] Problems with massProperties at getting area from ContourWidget

mbcx9rb9 richard.j.brown at live.co.uk
Fri Apr 3 11:22:01 EDT 2015


Specifically, I used vtkIntersectionPolyDataFilter to get the intersection
between a cylinder and cuboid. I successfully generate numbers from
GetVolume(), so clearly I have created triangles with my intersection data.

However, when I move the two objects, such that the intersection changes,
the volume jumps all over the place. When I render it the cylinder, cuboid
and intersection, everything looks normal.

Below, I have put a small working example (without rendering), then the
results they yield.

Thanks for any help.
Richard

#include <iostream>

#include <vtkSmartPointer.h>
#include <vtkCylinderSource.h>
#include <vtkTransform.h>
#include <vtkTransformPolyDataFilter.h>
#include <vtkCubeSource.h>
#include <vtkTriangleFilter.h>
#include <vtkIntersectionPolyDataFilter.h>
#include <vtkPolygon.h>
#include <vtkCellArray.h>
#include <vtkMassProperties.h>

using namespace std;

int main()
{
    // create cylinder and rotate it by 90 degrees (just so that it
automatically faces camera when rendered)
    vtkSmartPointer<vtkCylinderSource> cylinder =
vtkSmartPointer<vtkCylinderSource>::New();
    cylinder->SetCenter(-10,0,0);
    cylinder->SetHeight(1);
    cylinder->SetRadius(5);
    cylinder->SetResolution(50);
    cylinder->Update();

    // create cuboid
    vtkSmartPointer<vtkCubeSource> cuboid =
vtkSmartPointer<vtkCubeSource>::New();
    cuboid->SetCenter(0,0,0);
    cuboid->SetXLength(10);
    cuboid->SetYLength(1);
    cuboid->SetZLength(10);
    cuboid->Update();

    // triangulate cylinder and cuboid
    vtkSmartPointer<vtkTriangleFilter> triangulatedCylinder =
vtkSmartPointer<vtkTriangleFilter>::New();
    triangulatedCylinder->SetInputConnection(cylinder->GetOutputPort());
    vtkSmartPointer<vtkTriangleFilter> triangulatedSquare =
vtkSmartPointer<vtkTriangleFilter>::New();
    triangulatedSquare->SetInputConnection(cuboid->GetOutputPort());

    // move cylinder from left of cuboid, pass through it, then out the
other side again.
    // logically, volume will increase, reach maximum then decrease until
zero again
    for (int i=-10; i<=10; i++) {

        // move cylinder
        cylinder->SetCenter(i, 0, 0);

        // get intersection
        vtkSmartPointer<vtkIntersectionPolyDataFilter> intersect =
vtkSmartPointer<vtkIntersectionPolyDataFilter>::New();
        intersect->SetInputConnection( 0,
triangulatedCylinder->GetOutputPort() );
        intersect->SetInputConnection( 1,
triangulatedSquare->GetOutputPort() );
        intersect->Update();

        // heres where I trip up - converting lines from
vtkIntersectinoPolyDataFilter to polygons for GetVolume() function
        vtkSmartPointer<vtkPolyData> polyData =
vtkSmartPointer<vtkPolyData>::New();
        vtkSmartPointer<vtkPoints> points =
vtkSmartPointer<vtkPoints>::New();
        vtkSmartPointer<vtkCellArray> cellArray =
vtkSmartPointer<vtkCellArray>::New();
        vtkSmartPointer<vtkPolygon> polygon =
vtkSmartPointer<vtkPolygon>::New();

       
polygon->GetPointIds()->SetNumberOfIds(intersect->GetOutput()->GetNumberOfPoints());

        for (vtkIdType i=0; i<intersect->GetOutput()->GetNumberOfPoints();
i++) {
            polygon->GetPointIds()->SetId(i,i);
            points->InsertNextPoint(intersect->GetOutput()->GetPoint(i));
        }

        cellArray->InsertNextCell(polygon);

        polyData->SetPoints(points);
        polyData->SetPolys(cellArray);

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

        vtkSmartPointer<vtkMassProperties> mass =
vtkSmartPointer<vtkMassProperties>::New();
        mass->SetInputConnection(triangles->GetOutputPort());
        mass->Update();
        cout << mass->GetVolume() << endl;
    }
}


x              volume
-10		0
-9		4.05796
-8		4.26821
-7		2.90052
-6		2.02402
-5		2.31203
-4		5.97756
-3		0.0123537
-2		0.448455
-1		2.58317
0		1.40225
1		1.43162
2		1.50087
3		0.502383
4		0.0582565
5		0.729436
6		0.658901
7		0.414777
8		3.20074
9		1.39525
10		0




--
View this message in context: http://vtk.1045678.n5.nabble.com/Problems-with-massProperties-at-getting-area-from-ContourWidget-tp5717390p5731334.html
Sent from the VTK - Users mailing list archive at Nabble.com.


More information about the vtkusers mailing list