天天看點

VTK8.2 VS2017 Windows10 C++安裝1、安裝2、配置3、測試4、附錄

1、安裝

  1. 首先從 https://vtk.org/download/ 官方下載下傳包,并解壓。
  2. 如果沒有

    cmake

    ,安裝

    cmake

    軟體,在Windows上的

    cmake

    有GUI可以用,很友善。
  3. 打開

    cmake

    選擇

    vtk

    的根目錄(有

    CMakeLists.txt

    檔案)作為

    source

    目錄,然後建立一個檔案夾作為

    cmake

    build

    存放項目的目錄,檔案夾放在哪裡都行,推薦和

    source

    放在同級目錄。
  4. 點選

    config

    進行配置,這裡用的是

    VS2017

    ,是以選擇

    VS2017

    和預設的編譯器。
注意,這裡預設的是win32也就是32位的項目,如果需要x64的項目,請在繼續選擇x64平台,不然在win32的項目裡再改動會出現很多問題。例如開始我沒有注意這個問題,因為我要使用QT64位,是以我将項目改成了x64,結果出現了很多問題,例如

子產品計算機類型“X64”與目标計算機類型“X86”沖突問題

,這是因為開始cmake生成項目的時候配置是win32,然後在改x64的很多配置沒有改過來(也很難一一改正),與其手動一點點修改,不如在cmake的時候指定生産x64的項目,這樣就解決了很多問題。
  1. 配置完成後,會有許多紅色的選項,再點一次

    config

    ,紅色的選項消失。有許多選項,如果沒有特殊需求暫時預設即可,注意有一個選項是

    CMAKE_INSTALL_PREFIX

    ,後面的路徑是存放我們編譯好的

    .h

    lib

    dll

    檔案的,可以自定義設定,然後點選

    generate

    生成項目,完成後項目生成完畢,

    cmake

    的作用結束。
  2. 找到建立的存放

    build

    後檔案的目錄,打開

    .sln

    檔案,生成解決方案(時間有點長),然後再生成

    Install

    項目,至此,所有需要用的庫檔案,将生成在

    CMAKE_INSTALL_PREFIX

    指定的檔案夾内。

2、配置

  1. 建立一個空的項目C++項目。
  2. 在項目->屬性->配置屬性->VC++目錄->包含目錄裡添加

    vtk

    include

    檔案夾。
  3. 在項目->屬性->配置屬性->連結器 附加庫目錄裡添加

    vtk

    lib

    檔案夾。
  4. vtk

    lib

    檔案夾内建立一個

    txt

    檔案,将

    DIR *.lib /B >LIBLIST.TXT

    寫入檔案儲存,改字尾

    .txt

    .bat

    并運作,這是在目錄生成了一個新的

    .txt

    檔案,檔案存有所有

    lib

    檔案的名字。
  5. 編輯項目->屬性->配置屬性->連結器->輸入 附加依賴項,将4.生成的所有

    lib

    檔案的名字粘貼進去。
  6. 在項目->屬性->配置屬性->調試 環境 寫入

    PATH=生成的vtk dll檔案的目錄(bin檔案夾)

    ,這是為了讓

    VS

    找到

    dll

    檔案,也可以将

    dll

    檔案全部複制在項目中,具體可搜尋。

這樣

.h,lib,dll

檔案都配置好了,可以寫代碼了。

3、測試

附錄是官網給的例子(c++),所有案例的網址 https://lorensen.github.io/VTKExamples/site/Cxx/

在開始部分和官方的有所不同,主要是

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

這是因為,官方給出了

cmakelists

檔案,用

cmake

來建構的項目,這裡直接粘貼的代碼,導緻有些不同,要進行一些初始化操作,如果不加的話,會出錯,錯誤大緻如下

Error: no override found for 'vtkPolyDataMapper'.
           

有很多人遇到 方案和解釋如下:

https://stackoverflow.com/questions/18642155/no-override-found-for-vtkpolydatamapper

運作成功後,會出現一個圓柱體。

4、附錄

#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkCylinderSource.h>
#include <vtkNamedColors.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSmartPointer.h>
#include <array>
#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL2);//和官方的例子有些不同,下面解釋
VTK_MODULE_INIT(vtkInteractionStyle);
int main(int, char *[]) {
  vtkSmartPointer<vtkNamedColors> colors =
      vtkSmartPointer<vtkNamedColors>::New();

  // Set the background color.
  std::array<unsigned char, 4> bkg{{26, 51, 102, 255}};
  colors->SetColor("BkgColor", bkg.data());

  // This creates a polygonal cylinder model with eight circumferential facets
  // (i.e, in practice an octagonal prism).
  vtkSmartPointer<vtkCylinderSource> cylinder =
      vtkSmartPointer<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.
  vtkSmartPointer<vtkPolyDataMapper> cylinderMapper =
      vtkSmartPointer<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 around the X and Y axes.
  vtkSmartPointer<vtkActor> cylinderActor = vtkSmartPointer<vtkActor>::New();
  cylinderActor->SetMapper(cylinderMapper);
  cylinderActor->GetProperty()->SetColor(
      colors->GetColor4d("Tomato").GetData());
  cylinderActor->RotateX(30.0);
  cylinderActor->RotateY(-45.0);

  // The renderer generates the image
  // which is then displayed on the render window.
  // It can be thought of as a scene to which the actor is added
  vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
  renderer->AddActor(cylinderActor);
  renderer->SetBackground(colors->GetColor3d("BkgColor").GetData());
  // Zoom in a little by accessing the camera and invoking its "Zoom" method.
  renderer->ResetCamera();
  renderer->GetActiveCamera()->Zoom(1.5);

  // The render window is the actual GUI window
  // that appears on the computer screen
  vtkSmartPointer<vtkRenderWindow> renderWindow =
      vtkSmartPointer<vtkRenderWindow>::New();
  renderWindow->SetSize(300, 300);
  renderWindow->AddRenderer(renderer);
  renderWindow->SetWindowName("Cylinder");

  // The render window interactor captures mouse events
  // and will perform appropriate camera or actor manipulation
  // depending on the nature of the events.
  vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
      vtkSmartPointer<vtkRenderWindowInteractor>::New();
  renderWindowInteractor->SetRenderWindow(renderWindow);

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

  return EXIT_SUCCESS;
}