如何在找到每个点的最近邻居时迭代VTK中的点?

时间:2012-07-09 21:36:24

标签: for-loop iterator vtk

我正在使用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

1 个答案:

答案 0 :(得分:4)

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

Luke 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();

  }