[vtkusers] Wrong volume calculation (vtkMassProperties)

AlexeyM ya-lex at mail.ru
Mon Jan 16 10:02:21 EST 2017


Hello,
I want to compute volume of arbitary polyhedron. I made test code, where
polyhedron is created, converted to PolyData, triangulated and, finally,
volume is computed. So, the volume of created polyhedron should be equal to
1.5, but vtkMassProperties output 0.4375. Points are entered right and
Paraview visualise figure right. Please, help me to understand what's wrong
with code? Thanks.

#include <iostream>
#include <ostream>

#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 "vtkDataArray.h"
#include <vtkMassProperties.h>
#include "vtkUnstructuredGrid.h"
#include "vtkVersion.h"
#include "vtkIdList.h"
#include "vtkPoints.h"
#include "vtkPolyhedron.h"
#include "vtkPolyData.h"
#include "vtkCellArray.h"
#include "vtkPointData.h"
#include "vtkXMLUnstructuredGridWriter.h"

using namespace std;

int main()
{
	vtkSmartPointer<vtkPolyhedron> aDodecahedron =
vtkSmartPointer<vtkPolyhedron>::New();

	for (int i = 0; i < 10; ++i)
	{
		aDodecahedron->GetPointIds()->InsertNextId(i);
	}
	vtkIdType dodechedronPointsIds[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

	aDodecahedron->GetPoints()->InsertNextPoint(0, 0, 0);
	aDodecahedron->GetPoints()->InsertNextPoint(1, 0, 0);
	aDodecahedron->GetPoints()->InsertNextPoint(0, 1, 0);
	aDodecahedron->GetPoints()->InsertNextPoint(1, 1, 0);
	aDodecahedron->GetPoints()->InsertNextPoint(0, 0, 1); //4
	aDodecahedron->GetPoints()->InsertNextPoint(1, 0, 1); //5
	aDodecahedron->GetPoints()->InsertNextPoint(0, 1, 1); //6
	aDodecahedron->GetPoints()->InsertNextPoint(1, 1, 1); //7
	aDodecahedron->GetPoints()->InsertNextPoint(2, 0.5, 1); //8
	aDodecahedron->GetPoints()->InsertNextPoint(2, 0.5, 0); //9

	vtkIdType face[7][5] = { { 0, 1, 9, 3, 2 },
	{ 4, 5, 8, 7, 6 },
	{ 0, 1, 5, 4 },
	{ 3, 9, 8, 7 },
	{ 2, 3, 7, 6 },
	{ 0, 2, 6, 4 },
	{ 1, 9, 8, 5 } };

	vtkSmartPointer<vtkCellArray> dodechedronFaces =
vtkSmartPointer<vtkCellArray>::New();

	dodechedronFaces->InsertNextCell(5, face[0]);
	dodechedronFaces->InsertNextCell(5, face[1]);
	dodechedronFaces->InsertNextCell(4, face[2]);
	dodechedronFaces->InsertNextCell(4, face[3]);
	dodechedronFaces->InsertNextCell(4, face[4]);
	dodechedronFaces->InsertNextCell(4, face[5]);
	dodechedronFaces->InsertNextCell(4, face[6]);

	
	vtkSmartPointer<vtkUnstructuredGrid> ugrid1 =
vtkSmartPointer<vtkUnstructuredGrid>::New();
	ugrid1->SetPoints(aDodecahedron->GetPoints());
	ugrid1->InsertNextCell(VTK_POLYHEDRON, 10, dodechedronPointsIds, 7,
dodechedronFaces->GetPointer());

	vtkSmartPointer<vtkXMLUnstructuredGridWriter> writer =
vtkSmartPointer<vtkXMLUnstructuredGridWriter>::New();

	writer->SetInputData(ugrid1);

	writer->SetFileName("polyhedron1.vtu");
	writer->SetDataModeToBinary();
	writer->Update();

	aDodecahedron->SetFaces(ugrid1->GetCell(0)->GetFaces());
	aDodecahedron->Initialize();

	//vtkSmartPointer<vtkPolyData> polyData = aDodecahedron->GetPolyData();
	
	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(ugrid1->GetCell(0)->GetNumberOfPoints());

	points = ugrid1->GetCell(0)->GetPoints();

	for (int i = 0; i < ugrid1->GetCell(0)->GetNumberOfFaces(); i++)
	cellArray->InsertNextCell(ugrid1->GetCell(0)->GetFace(i));

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


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




		std::ostream& os = std::cout;
		triangles->Print(os);

	vtkSmartPointer<vtkMassProperties> mass =
vtkSmartPointer<vtkMassProperties>::New();
	//mass->SetInputConnection(triangles->GetOutputPort());
	mass->SetInputData(triangles->GetOutput());
	mass->Update();

	mass->Print(os);

		return 1;
	}




--
View this message in context: http://vtk.1045678.n5.nabble.com/Wrong-volume-calculation-vtkMassProperties-tp5741807.html
Sent from the VTK - Users mailing list archive at Nabble.com.


More information about the vtkusers mailing list