【问题标题】:How to iterate over points in VTK while finding the Nearest Neighbors of each point?如何在找到每个点的最近邻居的同时迭代VTK中的点?
【发布时间】:2012-07-09 21:36:23
【问题描述】:

我正在将 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

【问题讨论】:

    标签: for-loop iterator vtk


    【解决方案1】:

    我发现了问题...我正在使用 /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();
    
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-16
      相关资源
      最近更新 更多