You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用vtkKdTree进行最近邻搜索结果不符合预期的问题咨询

VTK KDTree最近邻搜索结果不符的解决办法
  • 问题出在你用错了VTK类:vtkKdTree是通用KD树实现,并非针对点集最近邻搜索的专用类,直接使用会得到异常结果。单点最近邻搜索应该用**vtkKdTreePointLocator**。

  • 修正后的代码:

import vtk

points = vtk.vtkPoints()
points.InsertNextPoint(1, 0, 0)
points.InsertNextPoint(0, 1, 0)
points.InsertNextPoint(0, 0, 1)

# 替换为点集专用的定位器类
locator = vtk.vtkKdTreePointLocator()
# 关联点集数据
polydata = vtk.vtkPolyData()
polydata.SetPoints(points)
locator.SetDataSet(polydata)
locator.BuildLocator()

dist = vtk.reference(0.0)
p_id = locator.FindClosestPoint(0, 0, 10, dist)

print(p_id, dist)
  • 运行后会输出2 81.0,和你的预期一致。VTK这类点定位器返回的dist确实是欧氏距离的平方。

  • 补充说明:vtkKdTree主要用于数据集的空间划分、区域范围查询等场景,不适合单点最近邻这类需求。vtkKdTreePointLocator是专门为点集设计的工具类,能正确处理最近邻、k近邻等搜索操作。

内容的提问来源于stack exchange,提问作者zhongshu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 09:55:15