簡體   English   中英

使用python中的VTK查找具有負平均曲率的.stl文件的單元格

[英]Finding cells of a .stl file with negative mean curvature using VTK in python

我有一個.stl文件,我正在嘗試使用VTK和python查找具有負平均曲率的單元格的坐標。 我編寫了這些代碼,可以根據它們的平均曲率更改單元格的顏色,但是我願意實現的是精確的單元格和具有特定平均曲率的三角形的坐標,例如,具有最大負平均曲率的單元格的3d坐標。 以下是代碼:

import vtk


def gaussian_curve(fileNameSTL):
    colors = vtk.vtkNamedColors()

    reader = vtk.vtkSTLReader()
    reader.SetFileName(fileNameSTL)
    reader.Update()

    curveGauss = vtk.vtkCurvatures()
    curveGauss.SetInputConnection(reader.GetOutputPort())
    curveGauss.SetCurvatureTypeToGaussian() # SetCurvatureTypeToMean() works better in the case of kidney.

    ctf = vtk.vtkColorTransferFunction()
    ctf.SetColorSpaceToDiverging()
    p1 = [0.0] + list(colors.GetColor3d("MidnightBlue"))
    p2 = [1.0] + list(colors.GetColor3d("DarkRed"))
    ctf.AddRGBPoint(*p1)
    ctf.AddRGBPoint(*p2)
    cc = list()
    for i in range(256):
        cc.append(ctf.GetColor(float(i) / 255.0))

    lut = vtk.vtkLookupTable()
    lut.SetNumberOfColors(256)
    for i, item in enumerate(cc):
        lut.SetTableValue(i, item[0], item[1], item[2], 1.0)
    lut.SetRange(0, 0) # In the case of kidney, the (0, 0) worked better.
    lut.Build()

    cmapper = vtk.vtkPolyDataMapper()
    cmapper.SetInputConnection(curveGauss.GetOutputPort())
    cmapper.SetLookupTable(lut)
    cmapper.SetUseLookupTableScalarRange(1)

    cActor = vtk.vtkActor()
    cActor.SetMapper(cmapper)

    return cActor


def render_scene(my_actor_list):
    renderer = vtk.vtkRenderer()
    for arg in my_actor_list:
        renderer.AddActor(arg)
    namedColors = vtk.vtkNamedColors()
    renderer.SetBackground(namedColors.GetColor3d("SlateGray"))

    window = vtk.vtkRenderWindow()
    window.SetWindowName("Render Window")
    window.AddRenderer(renderer)

    interactor = vtk.vtkRenderWindowInteractor()
    interactor.SetRenderWindow(window)

    # Visualize
    window.Render()
    interactor.Start()


if __name__ == '__main__':
    fileName = "400_tri.stl"
    my_list = list()
    my_list.append(gaussian_curve(fileName))
    render_scene(my_list)

這段代碼產生紅色的單元格表示正曲率,藍色的單元格表示負曲率。 以上代碼的結果

我需要數組或類似形式的結果(單元格的坐標)。 對於這個問題,我將不勝感激。

vtkplotter可能的解決方案:

from vtkplotter import *

torus1 = Torus().addCurvatureScalars().addScalarBar()
print("list of scalars:", torus1.scalars())

torus2 = torus1.clone().addScalarBar()
torus2.threshold("Gauss_Curvature", vmin=-15, vmax=0)

show(torus1, torus2, N=2) # plot on 2 separate renderers

print("vertex coordinates:", len(torus2.coordinates()))
print("cell centers      :", len(torus2.cellCenters()))

在此處查看生成的屏幕截圖

這里的附加示例。

希望這可以幫助。

所以我從kitware weblog中找到了答案,這是使用vtk.numpy_interfacevtk.util.numpy_support可以正常工作的代碼,但是仍然無法產生normals_array ,我也不知道為什么?

import vtk
from vtk.numpy_interface import dataset_adapter as dsa
from vtk.util.numpy_support import vtk_to_numpy


def curvature_to_numpy(fileNameSTL, curve_type='Mean'):
    colors = vtk.vtkNamedColors()

    reader = vtk.vtkSTLReader()
    reader.SetFileName(fileNameSTL)
    reader.Update()
    # Defining the curvature type.
    curve = vtk.vtkCurvatures()
    curve.SetInputConnection(reader.GetOutputPort())
    if curve_type == "Mean":
        curve.SetCurvatureTypeToMean()
    else:
        curve.SetCurvatureTypeToGaussian()
    curve.Update()

    # Applying color lookup table.
    ctf = vtk.vtkColorTransferFunction()
    ctf.SetColorSpaceToDiverging()
    p1 = [0.0] + list(colors.GetColor3d("MidnightBlue"))
    p2 = [1.0] + list(colors.GetColor3d("DarkOrange"))
    ctf.AddRGBPoint(*p1)
    ctf.AddRGBPoint(*p2)
    cc = list()
    for i in range(256):
        cc.append(ctf.GetColor(float(i) / 255.0))

    lut = vtk.vtkLookupTable()
    lut.SetNumberOfColors(256)
    for i, item in enumerate(cc):
        lut.SetTableValue(i, item[0], item[1], item[2], 1.0)
    lut.SetRange(0, 0)  # In the case of kidney, the (0, 0) worked better.
    lut.Build()

    # Creating Mappers and Actors.
    mapper = vtk.vtkPolyDataMapper()
    mapper.SetInputConnection(curve.GetOutputPort())
    mapper.SetLookupTable(lut)
    mapper.SetUseLookupTableScalarRange(1)

    actor = vtk.vtkActor()
    actor.SetMapper(mapper)

    # Scalar values to numpy array. (Curvature).
    dataObject = dsa.WrapDataObject(curve.GetOutput())
    normals_array = dataObject.PointData['Normals'] # Output array.
    curvature_array = dataObject.PointData['Mean_Curvature'] # output array.

    # Node values to numpy array.
    nodes = curve.GetOutput().GetPoints().GetData()
    nodes_array = vtk_to_numpy(nodes)

    # Creating a report file (.vtk file).
    writer = vtk.vtkPolyDataWriter()
    writer.SetFileName('vtk_file_generic.vtk')
    writer.SetInputConnection(curve.GetOutputPort())
    writer.Write()

   #  EDIT:

   # Creating the point normal array using vtkPolyDataNormals().
    normals = vtk.vtkPolyDataNormals()
    normals.SetInputConnection(reader.GetOutputPort())  # Here "curve" could be replaced by "reader".
    normals.ComputePointNormalsOn()
    normals.SplittingOff()
    normals.Update()
    dataNormals = dsa.WrapDataObject(normals.GetOutput())
    normals_array = dataNormals.PointData["Normals"]



    return actor, normals_array, curvature_array, nodes_array


def render_scene(my_actor_list):
    renderer = vtk.vtkRenderer()
    for arg in my_actor_list:
        renderer.AddActor(arg)
    namedColors = vtk.vtkNamedColors()
    renderer.SetBackground(namedColors.GetColor3d("SlateGray"))

    window = vtk.vtkRenderWindow()
    window.SetWindowName("Render Window")
    window.AddRenderer(renderer)

    interactor = vtk.vtkRenderWindowInteractor()
    interactor.SetRenderWindow(window)

    # Visualize
    window.Render()
    interactor.Start()


if __name__ == '__main__':
    filename = "400_tri.stl"
    my_list = list()
    my_actor, my_normals, my_curve, my_nodes = curvature_to_numpy(filename, curve_type="Mean")
    my_list.append(my_actor)
    render_scene(my_list) # Visualization.
    print(my_nodes) # Data points.
    print(my_normals) # Normal vectors.
    print(my_curve) # Mean curvatures.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM