Skip to main content
 首页 » 编程设计

python之vtkCellLocator : FindClosestPoint usage in python

2025年05月04日22Free-Thinker

“FindClosestPoint”函数的定义如下:

   def FindClosestPoint(self, p_float=None, p_float=None_1, p_float=None_2, *args, **kwargs): # real signature unknown; NOTE: unreliably restored from __doc__  
    """ 
    V.FindClosestPoint([float, float, float], [float, float, float], 
        vtkGenericCell, int, int, float) 
    C++: void FindClosestPoint(double x[3], double closestPoint[3], 
        vtkGenericCell *cell, vtkIdType &cellId, int &subId, 
        double &dist2) override; 
    V.FindClosestPoint([float, float, float], [float, float, float], 
        int, int, float) 
    C++: virtual void FindClosestPoint(double x[3], 
        double closestPoint[3], vtkIdType &cellId, int &subId, 
        double &dist2) 
 
    Return the closest point and the cell which is closest to the 
    point x. The closest point is somewhere on a cell, it need not be 
    one of the vertices of the cell.  This version takes in a 
    vtkGenericCell to avoid allocating and deallocating the cell.  
    This is much faster than the version which does not take a *cell, 
    especially when this function is called many times in a row such 
    as by a for loop, where the allocation and deallocation can be 
    done only once outside the for loop.  If a cell is found, "cell" 
    contains the points and ptIds for the cell "cellId" upon exit. 
    """ 
    pass 

在我的代码中,我使用这样的函数:

my_cell_locator = vtk.vtkCellLocator() 
my_cell_locator.SetDataSet(reverse.GetOutput()) # reverse.GetOutput() --> vtkPolyData 
cellId = 0 
c = [0.0, 0.0, 0.0] 
subId = 0 
d = 0.0 
my_cell_locator.FindClosestPoint([-23.7, -48.4, -1096.4], c, cellId, subId, d) 

这会导致以下错误:

进程已完成,退出代码为 -1073741819 (0xC0000005)

我不知道如何正确使用这个功能。怎么了? 正确的用法是什么?

请您参考如下方法:

我必须将代码更改为以下内容:

reverse.Update() 
my_cell_locator = vtk.vtkCellLocator() 
my_cell_locator.SetDataSet(reverse.GetOutput())  # reverse.GetOutput() --> vtkPolyData 
my_cell_locator.BuildLocator() 
 
cellId = vtk.reference(0) 
c = [0.0, 0.0, 0.0] 
subId = vtk.reference(0) 
d = vtk.reference(0.0) 
my_cell_locator.FindClosestPoint([-23.7, -48.4, -906.4], c, cellId, subId, d) 

我添加了以下行:

reverse.Update() 

和

my_cell_locator.BuildLocator() 

然后我更改了以下几行:

cellId = 0 
subId = 0 
d = 0.0 

为了...

cellId = vtk.reference(0) 
subId = vtk.reference(0) 
d = vtk.reference(0.0)