[VTK] 코드를 포함하여 자체 Qt 프로젝트로 포팅된 VTK 공식 예제 실린더

지식은 혼자가 아니라 체계적이어야 합니다. 더 많은 개인 요약 및 관련 경험은 Visual Studio 칼럼에서 찾을 수 있습니다 .

일련 번호 콘텐츠
1 [Visual Studio] Visual Studio를 사용하여 Windows에서 VTK 구축
2 [Visual Studio] Qt와 함께 Visual Studio를 사용하여 Windows에서 VTK 구축
【VTK】VTK 디스플레이 작은 공 예시, Qt와 함께 Visual Studio를 사용하여 Windows에서 VTK 구축
4 [VTK] 코드를 포함하여 자체 Qt 프로젝트로 포팅된 VTK 공식 예제 실린더

지식 부족은 이전 몇 기사에서 찾을 수 있습니다.

학습 아이디어는 다음과 같습니다.

  1. 소스 코드를 Cylinder.cxx아래로 복사
  2. 소스 코드 Cylinder.cxx영어 주석 변경 중국어 형식 프로그램ClinerVTKTest.cpp
  3. 이전에 구현된 Qt 디스플레이 예제 [VTK] VTK 디스플레이 볼 예제와 결합하여 Visual Studio 및 Qt를 사용하여 Windows에서 VTK를 빌드하고 프로젝트 이식을 수행하며 Qt 디스플레이 코어를 명확히 합니다.
  4. 드디어 파일을 받았습니다VTKTest.cpp

효과는 다음과 같습니다.

여기에 이미지 설명 삽입

VTKTest.cpp

#include "VTKTest.h"

VTKTest::VTKTest(QWidget* parent)
    : QMainWindow(parent)
{
    
    
    ui.setupUi(this);
}

VTKTest::~VTKTest()
{
    
    }

void VTKTest::on_pushButton_clicked()
{
    
    
    vtkSmartPointer<vtkCylinderSource> cylinder = vtkSmartPointer<vtkCylinderSource>::New();
    vtkSmartPointer<vtkPolyDataMapper> cylinderMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
    vtkSmartPointer<vtkActor> cylinderActor = vtkSmartPointer<vtkActor>::New();
    vtkSmartPointer<vtkRenderer> ren1 = vtkSmartPointer<vtkRenderer>::New();
    vtkSmartPointer<vtkGenericOpenGLRenderWindow> renWin = vtkSmartPointer<vtkGenericOpenGLRenderWindow>::New();

    cylinderMapper->SetInputConnection(cylinder->GetOutputPort());
    
    cylinderActor->SetMapper(cylinderMapper);
    cylinderActor->GetProperty()->SetColor(1.0000, 0.3882, 0.2784);

    ren1->AddActor(cylinderActor);
    ren1->SetBackground(0.1, 0.2, 0.4);

    renWin->AddRenderer(ren1);
    renWin->SetSize(200, 200);
    renWin->Render();

    ui.qvtkWidget->setRenderWindow(renWin);
    ui.qvtkWidget->resize(200, 160);
    ui.qvtkWidget->show();
}

CylinderVTKTest.cpp

#include "VTKTest.h"

VTKTest::VTKTest(QWidget* parent)
    : QMainWindow(parent)
{
    
    
    ui.setupUi(this);
}

VTKTest::~VTKTest()
{
    
    }

void VTKTest::on_pushButton_clicked()
{
    
    
    // This creates a polygonal cylinder model with eight circumferential facets.
    // 这将创建一个具有八个周切面的多边形圆柱体模型。
    vtkCylinderSource* cylinder = vtkCylinderSource::New();
    cylinder->SetResolution(8);

    // The mapper is responsible for pushing the geometry into the graphics
    // library. It may also do color mapping, if scalars or other attributes
    // are defined.
    // 映射器负责将几何图形推送到图形库中。如果定义了标量或其他属性,还可以进行颜色映射。
    vtkPolyDataMapper* cylinderMapper = vtkPolyDataMapper::New();
    cylinderMapper->SetInputConnection(cylinder->GetOutputPort());

    // The actor is a grouping mechanism: besides the geometry (mapper), it
    // also has a property, transformation matrix, and/or texture map.
    // Here we set its color and rotate it -22.5 degrees.
    // 角色是一种分组机制:除了几何体(映射器)外,它还具有属性、变换矩阵和/或纹理贴图。在这里,我们设置其颜色并将其旋转-22.5度。
    vtkActor* cylinderActor = vtkActor::New();
    cylinderActor->SetMapper(cylinderMapper);
    cylinderActor->GetProperty()->SetColor(1.0000, 0.3882, 0.2784);
    cylinderActor->RotateX(30.0);
    cylinderActor->RotateY(-45.0);

    // Create the graphics structure. The renderer renders into the
    // render window. The render window interactor captures mouse events
    // and will perform appropriate camera or actor manipulation
    // depending on the nature of the events.
    // 创建图形结构。渲染器渲染到渲染窗口。渲染窗口交互器捕捉鼠标事件,并根据事件的性质执行适当的摄像机或角色操作。
    vtkRenderer* ren1 = vtkRenderer::New();
    vtkRenderWindow* renWin = vtkRenderWindow::New();
    renWin->AddRenderer(ren1);
    vtkRenderWindowInteractor* iren = vtkRenderWindowInteractor::New();
    iren->SetRenderWindow(renWin);

    // Add the actors to the renderer, set the background and size
    // 将演员添加到渲染器中,设置背景和尺寸
    ren1->AddActor(cylinderActor);
    ren1->SetBackground(0.1, 0.2, 0.4);
    renWin->SetSize(200, 200);

    // We'll zoom in a little by accessing the camera and invoking a "Zoom"
    // method on it.
    // 我们将通过访问摄像机并调用 "Zoom "方法来放大摄像机。
    ren1->ResetCamera();
    ren1->GetActiveCamera()->Zoom(1.5);
    renWin->Render();

    // This starts the event loop and as a side effect causes an initial render.
    // 这将启动事件循环,并作为副作用导致初始渲染。
    iren->Start();

    // Exiting from here, we have to delete all the instances that
    // have been created.
    // 从这里退出,我们必须删除所有已经创建的实例。已经创建的实例。
    cylinder->Delete();
    cylinderMapper->Delete();
    cylinderActor->Delete();
    ren1->Delete();
    renWin->Delete();
    iren->Delete();
}

Cylinder.cxx

/*=========================================================================

  Program:   Visualization Toolkit
  Module:    Cylinder.cxx

  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/
//
// This simple example shows how to do basic rendering and pipeline
// creation using C++.
//
#include "vtkCylinderSource.h"
#include "vtkPolyDataMapper.h"
#include "vtkActor.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkProperty.h"
#include "vtkCamera.h"

int main()
{
    
    
  // This creates a polygonal cylinder model with eight circumferential facets.
  //
  vtkCylinderSource *cylinder = vtkCylinderSource::New();
  cylinder->SetResolution(8);

  // The mapper is responsible for pushing the geometry into the graphics
  // library. It may also do color mapping, if scalars or other attributes
  // are defined.
  vtkPolyDataMapper *cylinderMapper = vtkPolyDataMapper::New();
  cylinderMapper->SetInputConnection(cylinder->GetOutputPort());

  // The actor is a grouping mechanism: besides the geometry (mapper), it
  // also has a property, transformation matrix, and/or texture map.
  // Here we set its color and rotate it -22.5 degrees.
  vtkActor *cylinderActor = vtkActor::New();
  cylinderActor->SetMapper(cylinderMapper);
  cylinderActor->GetProperty()->SetColor(1.0000, 0.3882, 0.2784);
  cylinderActor->RotateX(30.0);
  cylinderActor->RotateY(-45.0);

  // Create the graphics structure. The renderer renders into the
  // render window. The render window interactor captures mouse events
  // and will perform appropriate camera or actor manipulation
  // depending on the nature of the events.
  vtkRenderer *ren1 = vtkRenderer::New();
  vtkRenderWindow *renWin = vtkRenderWindow::New();
  renWin->AddRenderer(ren1);
  vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
  iren->SetRenderWindow(renWin);

  // Add the actors to the renderer, set the background and size
  ren1->AddActor(cylinderActor);
  ren1->SetBackground(0.1, 0.2, 0.4);
  renWin->SetSize(200, 200);

  // We'll zoom in a little by accessing the camera and invoking a "Zoom"
  // method on it.
  ren1->ResetCamera();
  ren1->GetActiveCamera()->Zoom(1.5);
  renWin->Render();

  // This starts the event loop and as a side effect causes an initial render.
  iren->Start();

  // Exiting from here, we have to delete all the instances that
  // have been created.
  cylinder->Delete();
  cylinderMapper->Delete();
  cylinderActor->Delete();
  ren1->Delete();
  renWin->Delete();
  iren->Delete();

  return 0;
}

참조

  1. 시작하기
  2. C++ 예제
  3. 실린더예제
  4. 이 예제의 원래 C++ 소스 코드는 여기에 있습니다.

추천

출처blog.csdn.net/weixin_36815313/article/details/131715669