How do I get the surface area of vtkUnstructuredGrid

Hello,

I neel get surface area of vtkUnstructuredGrid, and vtkTriangleFilter is used to process VTKUnstructuredgrid, and then vtkMassProperties is used to get the surface area, but the obtained area is 0.

Here is my code, The following is my code, after running the prompt

 Input port 0 of algorithm vtkTriangleFilter(000002CF5EAEB670) has 0 connections but is not optional.

What went wrong, please?

    std::vector<std::array<double, 3>> pointCoordinates;
    // Face 1
    pointCoordinates.push_back({ {0, 2, 0} });  // Node 0
    pointCoordinates.push_back({ {0, 2, 3} });  // Node 1
    pointCoordinates.push_back({ {0, 0, 3} });  // Node 2
    pointCoordinates.push_back({ {0, 0, 0} });  // Node 3
    // Face 2
    pointCoordinates.push_back({ {0, 2, 0} });  // Node 4
    pointCoordinates.push_back({ {0, 1, 0} });  // Node 5
    pointCoordinates.push_back({ {0, 1, 3} });  // Node 6
    pointCoordinates.push_back({ {0, 2, 3} });  // Node 7

    // Create the points.
    vtkNew<vtkPoints> points;

    for (auto i = 0; i < pointCoordinates.size(); ++i)
    {
        points->InsertNextPoint(pointCoordinates[i].data());
    }

    vtkNew<vtkUnstructuredGrid> uGrid;
    uGrid->SetPoints(points);

    vtkIdType ptid[4] = { 0, 1, 2, 3};
    uGrid->InsertNextCell(VTK_QUAD, 4, ptid);
    vtkIdType ptid2[4] = { 4, 5, 6, 7};
    uGrid->InsertNextCell(VTK_QUAD, 4, ptid2);

    vtkSmartPointer<vtkDataSetMapper> mapper = vtkSmartPointer<vtkDataSetMapper>::New();
    mapper->SetInputData(uGrid);

    vtkSmartPointer<vtkPolyData>  pd =
    vtkPolyData::SafeDownCast(mapper->GetInput());

    vtkSmartPointer<vtkTriangleFilter> filter = vtkSmartPointer<vtkTriangleFilter>::New();
    filter->SetInputData(pd);
    filter->Update();

    vtkSmartPointer<vtkMassProperties> m_pvtkMassProperties =
        vtkMassProperties::New();
    m_pvtkMassProperties->SetInputData(filter->GetOutput());
    m_pvtkMassProperties->Update();
    double area = m_pvtkMassProperties->GetSurfaceArea();
    std::cout << area << std::endl;

Can you try to pass the unstructured grid uGrid as input?

1 Like

I’ve tried. It prompts:

Input for connection index 0 on input port index 0 for algorithm vtkTriangleFilter(000002378D5736D0) is of type vtkUnstructuredGrid, but a vtkPolyData is required.

How about adding a vtkGeometryFilter in between to extract the surface of your unstructured grid to polydata. For an example see here.