VTK vtkOBBTree 有向包围盒

vtkOBBTree  有向包围盒

vtkOBBTree是一个包围盒的树,它将体的每个cell分割到每个小的包围盒中,由SetNumberOfBuckets确定每个盒中放多少个 Cell。建立一个vtkOBBTree要先设定DataSet,SetDataSet。然后调用BuildLocator。包围盒精细程度由递归深度 (MaxLevel)以及预先设置的NumberOfBuckets决定。

使用场景:

可以用vtkOBBTree和直线、三角形甚至是另一个vtkOBBTree做相交检测、运算,碰撞检测;

可以用来,获得直线与多边形数据的交点;用来求最近点;等;

1.二个vtkOBBTree 求交:

     vtkOBBTree * targetTree = vtkOBBTree::New();
    targetTree->SetDataSet(targetPoly);
    targetTree->BuildLocator();
   
    vtkOBBTree * clipTree = vtkOBBTree::New();
    clipTree->SetDataSet(clipPoly);
    clipTree->BuildLocator();
   

    //使用包围盒求交 ,求交部分在OBBNodeIntersected中。NULL表示不做任何转换
    clipTree->IntersectWithOBBTree(targetTree,NULL,OBBNodeIntersected,this);

    //OBBNodeIntersected函数的定义:
    static int OBBNodeIntersected(vtkOBBNode *, vtkOBBNode *, vtkMatrix4x4 *,void *); 

2.官方样例:OBBTreeExtractCells.cxx

使用 vtkOBBTree 

IntersectWithLine

获取 线和 包围盒的 交点和 交点所在Cell;

vtkOBBTree 返回一条线和数据集的所有交点。 如果要最近的相交,则必须手 动找到它。 在此示例中,我们创建一个球体,并将其与一条直线相交。

这里其实也可以用于求,鼠标点击 Pick 到的点;鼠标的2D点,转成 世界坐标,加上相机的点,再延长;也是一条线,求出最近的点  ;也就是鼠标 Pick 到的点;

VTK vtkOBBTree 有向包围盒_第1张图片

 CODE

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#include 
#include 
#include 
#include 
#include 
#include 
#include 

int main(int, char*[])
{
  vtkNew colors;

  vtkNew sphereSource;
  sphereSource->SetPhiResolution(7);
  sphereSource->SetThetaResolution(15);
  sphereSource->Update();

  // Create the locator
  vtkNew tree;
  tree->SetDataSet(sphereSource->GetOutput());
  tree->BuildLocator();

  // Intersect the locator with the line
  double lineP0[3] = {-0.6, -0.6, -0.6};
  double lineP1[3] = {.6, .6, .6};
  vtkNew intersectPoints;

  vtkNew intersectCells;

  double tol = 1.e-8;
  tree->SetTolerance(tol);
  tree->IntersectWithLine(lineP0, lineP1, intersectPoints, intersectCells);

  std::cout << "NumPoints: " << intersectPoints->GetNumberOfPoints()
            << std::endl;

  // Display list of intersections
  double intersection[3];
  for (int i = 0; i < intersectPoints->GetNumberOfPoints(); i++)
  {
    intersectPoints->GetPoint(i, intersection);
    std::cout << "\tPoint Intersection " << i << ": " << intersection[0] << ", "
              << intersection[1] << ", " << intersection[2] << std::endl;
  }

  std::cout << "NumCells: " << intersectCells->GetNumberOfIds() << std::endl;

  vtkIdType cellId;
  for (int i = 0; i < intersectCells->GetNumberOfIds(); i++)
  {
    cellId = intersectCells->GetId(i);
    std::cout << "\tCellId " << i << ": " << cellId << std::endl;
  }

  // Render the line, sphere and intersected cells
  vtkNew lineSource;
  lineSource->SetPoint1(lineP0);
  lineSource->SetPoint2(lineP1);

  vtkNew lineMapper;
  lineMapper->SetInputConnection(lineSource->GetOutputPort());
  vtkNew lineActor;
  lineActor->SetMapper(lineMapper);

  vtkNew sphereMapper;
  sphereMapper->SetInputConnection(sphereSource->GetOutputPort());
  vtkNew sphereActor;
  sphereActor->SetMapper(sphereMapper);
  sphereActor->GetProperty()->SetRepresentationToWireframe();
  sphereActor->GetProperty()->SetColor(colors->GetColor3d("Gold").GetData());

  vtkNew cellSource;
  cellSource->SetInputConnection(sphereSource->GetOutputPort());
  cellSource->SetCellList(intersectCells);

  vtkNew cellMapper;
  cellMapper->SetInputConnection(cellSource->GetOutputPort());
  vtkNew cellActor;
  cellActor->SetMapper(cellMapper);
  cellActor->GetProperty()->SetColor(colors->GetColor3d("Tomato").GetData());

  vtkNew renderer;
  vtkNew renderWindow;
  renderWindow->AddRenderer(renderer);
  vtkNew renderWindowInteractor;
  renderWindowInteractor->SetRenderWindow(renderWindow);

  renderer->AddActor(lineActor);
  renderer->AddActor(sphereActor);
  renderer->AddActor(cellActor);
  renderer->SetBackground(colors->GetColor3d("CornflowerBlue").GetData());

  renderWindow->SetWindowName("OBBTreeExtractCells");
  renderWindow->Render();
  renderWindowInteractor->Start();

  return EXIT_SUCCESS;
}

3.官方样例 包围盒可视化

VTK vtkOBBTree 有向包围盒_第2张图片

 int maxLevel = 5;
  // Create the tree
  vtkNew obbTree;
  obbTree->SetDataSet(polyData);
  obbTree->SetMaxLevel(maxLevel);
  obbTree->BuildLocator();

  double corner[3] = {0.0, 0.0, 0.0};
  double max[3] = {0.0, 0.0, 0.0};
  double mid[3] = {0.0, 0.0, 0.0};
  double min[3] = {0.0, 0.0, 0.0};
  double size[3] = {0.0, 0.0, 0.0};

  obbTree->ComputeOBB(polyData, corner, max, mid, min, size);

  
  // Initialize the representation
  vtkNew polydata;
  obbTree->GenerateRepresentation(0, polydata);

  vtkNew obbtreeMapper;
  obbtreeMapper->SetInputData(polydata);

  vtkNew obbtreeActor;
  obbtreeActor->SetMapper(obbtreeMapper);
  obbtreeActor->GetProperty()->SetInterpolationToFlat();
  obbtreeActor->GetProperty()->SetOpacity(.5);
  obbtreeActor->GetProperty()->EdgeVisibilityOn();
  obbtreeActor->GetProperty()->SetColor(
      colors->GetColor4d("SpringGreen").GetData());

你可能感兴趣的:(VTK)