3

我正在将 VTK 用于一个项目,但似乎无法弄清楚其中的一部分。我正在尝试遍历数千个点并找到每个点的 5 个最近点。似乎是一个简单的 for 循环操作,但我的问题是,由于某种原因,我被告知相同的 5 个点是我数据中每个点的最近点......我知道这不是真的。我将附上我在下面描述的代码:

  //test
  vtkSmartPointer<vtkPointSource> pointSource =
    vtkSmartPointer<vtkPointSource>::New();
   pointSource->SetNumberOfPoints( Output->GetNumberOfPoints() );
   pointSource->Update();

   vtkSmartPointer<vtkKdTreePointLocator> Tree =
     vtkSmartPointer<vtkKdTreePointLocator>::New();
   Tree->SetDataSet( pointSource->GetOutput() );
   Tree->BuildLocator();

   unsigned int k = 5;
   double testpoint[3];

   vtkSmartPointer<vtkIdList> result = 
     vtkSmartPointer<vtkIdList>::New();

   for(vtkIdType n = 0; n < Output->GetNumberOfPoints(); n++)
     {

     result->Reset();
     Output->GetPoint( n,testpoint );
     Tree->Update();
     std::cout << "Point: " << testpoint[0] << ", " << testpoint[1] << ", " << testpoint[2] << ": " << endl;

     Tree->FindClosestNPoints(k, testpoint, result);

     for(vtkIdType i = 0; i < k; i++)
       {
       vtkIdType point_ind = result->GetId(i);
       double p[3];
       pointSource->GetOutput()->GetPoint(point_ind, p);
       std::cout << "Closest point " << i+1 << ": Point "
       << point_ind << ": (" << p[0] << ", " << p[1] << ", " << p[2] << ")" << std::endl;
       }

     }
     //end test  

这正在做我想做的事情......它正在打印出与兴趣点最近的 5 个点,但是尽管兴趣点发生了变化,但 5 个最近点保持不变。我假设我只是在我的代码中传递了一个小细节,但我可能是错的。如果您需要更多信息来提供帮助,请直接询问。

感谢您的时间和帮助,卢克 H

4

1 回答 1

5

我发现了问题...我正在使用 /vtkPointSource 生成随机点,并且我正在将我的点输入到该函数中。我不知道为什么,但这使得结果变得非常困难,但是一旦我传递了正确的信息并在点 Id 数组和结果 Id 数组上放置了一个 Reset(),它就像一个冠军一样工作。希望这会给其他人带来一些麻烦,因为我花了一些时间来解决这个问题。

卢克 H

 vtkSmartPointer<vtkKdTreePointLocator> Tree =
 vtkSmartPointer<vtkKdTreePointLocator>::New();
 Tree->SetDataSet( Output );
 Tree->BuildLocator();

 vtkSmartPointer<vtkIdList> result = 
   vtkSmartPointer<vtkIdList>::New();

 vtkSmartPointer<vtkIdList> point_ind =
   vtkSmartPointer<vtkIdList>::New();

 unsigned int k = 5;

 double testpoint[3];

 for( vtkIdType n = 0; n < Output->GetNumberOfPoints(); n++ )
   {

   Output->GetPoint( n,testpoint );
   std::cout << "Point: " << testpoint[0] << ", " << testpoint[1] 
     << ", " << testpoint[2] << ": " << endl;

   Tree->FindClosestNPoints( k, testpoint, result );

   for(vtkIdType i = 0; i < k; i++)
     {

 vtkIdType point_ind = result->GetId( i );

     double p[3];

     Output->GetPoint( point_ind, p );
 std::cout << "Closest point " << i+1 << ": Point "
   << point_ind << ": (" << p[0] << ", " << p[1] << ", " 
       << p[2] << ")" << std::endl;

    }

    result->Reset();
point_ind->Reset();

  }
于 2012-07-10T16:30:25.987 回答