Skip to content

Compatibilty with VTK cell and point data #18

@ghasemiAb

Description

@ghasemiAb

In domino_utils.py located in physicsnemo-curator/physicsnemo_curator/examples/external_aerodynamics/domino

the following changes make it compatible with both data format

def get_vertices(polydata: "vtk.vtkPolyData") -> np.ndarray:
    """Extract vertex coordinates from VTK polydata object.

    This function converts VTK polydata to a NumPy array containing the 3D
    coordinates of all vertices in the mesh.

    Args:
        polydata: VTK polydata object containing mesh geometry.

    Returns:
        NumPy array of shape (n_points, 3) containing [x, y, z] coordinates
        for each vertex.

    """
    # 1. Use GetPoints() to get the geometric coordinates.
    vtk_points = polydata.GetPoints()

    # 2. Check if points exist to prevent errors.
    if vtk_points and vtk_points.GetNumberOfPoints() > 0:
        # 3. vtk_points is a vtkPoints object, which has the GetData() method.
        vertices = numpy_support.vtk_to_numpy(vtk_points.GetData())
    else:
        print("\nWarning: No points found in the polydata object.")
        vertices = np.empty((0, 3))
    return vertices




def get_volume_data(
    polydata: "vtk.vtkPolyData", variable_names: list[str]
) -> tuple[np.ndarray, list[np.ndarray]]:
    """
    Extracts vertices and field data from a 3D volumetric mesh.
    Ensures that the final field data is associated with points. If data is only
    found on cells, it will be interpolated to the points.
    """
#    vertices = get_vertices(polydata)
    point_data = polydata.GetPointData()
    cell_data = polydata.GetCellData()
    fields = None

    # We check for the first variable name
    if point_data.HasArray(variable_names[0]):
        vertices = get_vertices(polydata)
        print("Using existing PointData.")
        fields = get_fields(point_data, variable_names)

    # If not on points, check if it's on the cells and convert it.
    elif cell_data.HasArray(variable_names[0]):
        print("Data found on cells. Converting CellData to PointData...")

        # Create the conversion filter.
        c2p_filter = vtk.vtkCellDataToPointData()

        #  Set the input mesh for the filter.
        c2p_filter.SetInputData(polydata)

        # Execute the filter.
        c2p_filter.Update()

        #  Get the output, which is a new mesh with interpolated point data.
        processed_polydata = c2p_filter.GetOutput()

        vertices = get_vertices(processed_polydata)

        # Extract the fields from the new point data.
        fields = get_fields(processed_polydata.GetPointData(), variable_names)

    else:
        print(f"\nWarning: Could not find variables '{variable_names}' in PointData or CellData.")
        return vertices, []

    return vertices, fields

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions