VTK学习笔记(十七)切割立方体

这篇博客展示了如何使用 Visualization Toolkit (VTK) 创建一个3D立方体,并通过平面切割来呈现切割效果。代码示例中,创建了一个立方体源,设置切割平面并应用切割操作,最后通过映射和渲染展示切割结果和原始立方体。
摘要由CSDN通过智能技术生成

简单的绘制了一个立方体,并对其进行切割展示。示例不复杂,很好的展示了如何制作。

 

#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkCubeSource.h>
#include <vtkCutter.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPlane.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>


#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
VTK_MODULE_INIT(vtkRenderingContextOpenGL2);

int main(int, char*[])
{
  vtkNew<vtkNamedColors> colors;
  vtkNew<vtkCubeSource> cube;
  cube->SetXLength(40);
  cube->SetYLength(30);
  cube->SetZLength(20);
  vtkNew<vtkPolyDataMapper> cubeMapper;
  cubeMapper->SetInputConnection(cube->GetOutputPort());

  // Create a plane to cut,here it cuts in the XZ direction (xz
  // normal=(1,0,0);XY =(0,0,1),YZ =(0,1,0)
  vtkNew<vtkPlane> plane;
  plane->SetOrigin(10, 0, 0);
  plane->SetNormal(1, 0, 0);

  // Create cutter
  vtkNew<vtkCutter> cutter;
  cutter->SetCutFunction(plane);
  cutter->SetInputConnection(cube->GetOutputPort());
  cutter->Update();

  vtkNew<vtkPolyDataMapper> cutterMapper;
  cutterMapper->SetInputConnection(cutter->GetOutputPort());
  cutterMapper->SetResolveCoincidentTopologyToPolygonOffset();

  // Create plane actor
  vtkNew<vtkActor> planeActor;
  planeActor->GetProperty()->SetColor(colors->GetColor3d("Yellow").GetData());
  planeActor->GetProperty()->SetLineWidth(2);
  planeActor->GetProperty()->SetAmbient(1.0);
  planeActor->GetProperty()->SetDiffuse(0.0);
  planeActor->SetMapper(cutterMapper);

  // Create cube actor
  vtkNew<vtkActor> cubeActor;
  cubeActor->GetProperty()->SetColor(
      colors->GetColor3d("Aquamarine").GetData());
  cubeActor->GetProperty()->SetOpacity(0.5);
  cubeActor->SetMapper(cubeMapper);

  // Create renderers and add actors of plane and cube
  vtkNew<vtkRenderer> renderer;
  renderer->AddActor(planeActor); // display the rectangle resulting from the cut
  renderer->AddActor(cubeActor);  // display the cube

  // Add renderer to renderwindow and render
  vtkNew<vtkRenderWindow> renderWindow;
  renderWindow->AddRenderer(renderer);
  renderWindow->SetSize(600, 600);
  renderWindow->SetWindowName("Cutter");

  vtkNew<vtkRenderWindowInteractor> interactor;
  interactor->SetRenderWindow(renderWindow);
  renderer->SetBackground(colors->GetColor3d("Silver").GetData());
  renderWindow->Render();

  auto camera = renderer->GetActiveCamera();
  camera->SetPosition(-37.2611, -86.2155, 44.841);
  camera->SetFocalPoint(0.569422, -1.65124, -2.49482);
  camera->SetViewUp(0.160129, 0.42663, 0.890138);
  camera->SetDistance(104.033);
  camera->SetClippingRange(55.2019, 165.753);

  renderWindow->Render();

  interactor->Start();

  return EXIT_SUCCESS;
  }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值