碰撞检测

#include <vtkVersion.h>
#include <vtkProperty.h>
#include <vtkCellArray.h>
#include<vtkPolyDataMapper.h>
#include<vtkDelaunay3D.h>
#include <vtkSphereSource.h>
#include <vtkPolyData.h>
#include <vtkSmartPointer.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkUnstructuredGrid.h>
#include <vtkVertexGlyphFilter.h>

int main(int , char *[])
{
  // Create a sphere
  vtkSmartPointer<vtkSphereSource> bigSphereSource =
    vtkSmartPointer<vtkSphereSource>::New();
  bigSphereSource->SetCenter(0.0, 0.0, 0.0);
  bigSphereSource->SetRadius(4);
  bigSphereSource->SetThetaResolution(20);
  bigSphereSource->SetPhiResolution(20);
  bigSphereSource->Update();

  vtkSmartPointer<vtkPolyDataMapper> bigSphereMapper =
    vtkSmartPointer<vtkPolyDataMapper>::New();
  bigSphereMapper->SetInputConnection(bigSphereSource->GetOutputPort());

  vtkSmartPointer<vtkActor> bigSphereActor =
    vtkSmartPointer<vtkActor>::New();
  bigSphereActor->SetMapper(bigSphereMapper);
  bigSphereActor->GetProperty()->SetOpacity(.5);

  vtkSmartPointer<vtkSphereSource> smallSphereSource =
    vtkSmartPointer<vtkSphereSource>::New();
  smallSphereSource->SetCenter(1, 2, 1);
  smallSphereSource->SetRadius(2);
  smallSphereSource->SetThetaResolution(20);
  smallSphereSource->SetPhiResolution(20);
  smallSphereSource->Update();
  
  vtkSmartPointer<vtkPolyDataMapper> smallSphereMapper =
    vtkSmartPointer<vtkPolyDataMapper>::New();
  smallSphereMapper->SetInputConnection(smallSphereSource->GetOutputPort());

  vtkSmartPointer<vtkActor> smallSphereActor =
    vtkSmartPointer<vtkActor>::New();
  smallSphereActor->SetMapper(smallSphereMapper);

  vtkSmartPointer<vtkPoints> pointsInside = vtkSmartPointer<vtkPoints>::New();
  vtkSmartPointer<vtkPolyData> polyDataInside = vtkSmartPointer<vtkPolyData>::New();
  double point[3];

  vtkSmartPointer<vtkDelaunay3D> delaunay3D = vtkSmartPointer<vtkDelaunay3D>::New();
  delaunay3D->SetInputConnection (bigSphereSource->GetOutputPort());
  delaunay3D->Update();

  for(vtkIdType i = 0; i < smallSphereSource->GetOutput()->GetNumberOfPoints(); i++)
    {
    smallSphereSource->GetOutput()->GetPoint(i,point);

    double pcoords[3];
    // The number of weights >= max number of nodes in each cell
    double weights[4];

    int subId;
    vtkIdType cellId = delaunay3D->GetOutput()->FindCell(point,NULL, 0, .1,subId, pcoords, weights);

    if (cellId>=0)
      {
      std::cout << "In cell " << cellId << std::endl;
      std::cout << "inside" << std::endl;
      pointsInside->InsertNextPoint(point);
      }
    else
      {
      std::cout << "outside" << std::endl;
      }
    }

  polyDataInside->SetPoints(pointsInside);

  vtkSmartPointer<vtkVertexGlyphFilter> vertexGlyphFilter = vtkSmartPointer<vtkVertexGlyphFilter>::New();
#if VTK_MAJOR_VERSION <= 5
  vertexGlyphFilter->SetInputConnection(polyDataInside->GetProducerPort());
#else
  vertexGlyphFilter->SetInputData(polyDataInside);
#endif
  vertexGlyphFilter->Update();
  
  // Mapper and actor for points inside
  vtkSmartPointer<vtkPolyDataMapper> pointsInsideMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
  pointsInsideMapper->SetInputConnection(vertexGlyphFilter->GetOutputPort());
  vtkSmartPointer<vtkActor> pointsInsideActor = vtkSmartPointer<vtkActor>::New();
  pointsInsideActor->SetMapper(pointsInsideMapper);
  pointsInsideActor->GetProperty()->SetPointSize(6);
  pointsInsideActor->GetProperty()->SetColor(1,0,1);

  vtkSmartPointer<vtkRenderer> renderer =
    vtkSmartPointer<vtkRenderer>::New();
  vtkSmartPointer<vtkRenderWindow> renderWindow =
    vtkSmartPointer<vtkRenderWindow>::New();
  renderWindow->AddRenderer(renderer);
  vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
    vtkSmartPointer<vtkRenderWindowInteractor>::New();
  renderWindowInteractor->SetRenderWindow(renderWindow);

  renderer->AddActor(smallSphereActor);
  renderer->AddActor(bigSphereActor);
  renderer->AddActor(pointsInsideActor);
  renderer->SetBackground(.3, .6, .3); // Background color green
  renderWindow->Render();
  renderWindowInteractor->Start();

  return EXIT_SUCCESS;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值