知識不是單獨(dú)的,一定是成體系的。更多我的個(gè)人總結(jié)和相關(guān)經(jīng)驗(yàn)可查閱這個(gè)專欄:Visual Studio。
編號 | 內(nèi)容 |
---|---|
1 | 【Visual Studio】在 Windows 上使用 Visual Studio 構(gòu)建 VTK |
2 | 【Visual Studio】在 Windows 上使用 Visual Studio 配合 Qt 構(gòu)建 VTK |
3 | 【VTK】VTK 顯示小球例子,在 Windows 上使用 Visual Studio 配合 Qt 構(gòu)建 VTK |
4 | 【VTK】官方示例,移植到自己的 Qt 工程,含代碼 |
這一篇在 Windows 上完成了使用 Visual Studio 構(gòu)建 VTK,還有一篇是結(jié)合 Qt 實(shí)現(xiàn)在 Windows 上使用 Visual Studio 配合 Qt 構(gòu)建 VTK。另一篇文章鏈接為:【Visual Studio】在 Windows 上使用 Visual Studio 配合 Qt 構(gòu)建 VTK。
1 版本環(huán)境
- win11
- visual studio 2022
- VTK-9.2.6
- CMake 3.26.3
2 構(gòu)建步驟
建議全程打開 CMake 和 Visual Studio 時(shí)均使用管理員身份,防止不必要的麻煩。
1 準(zhǔn)備文件夾結(jié)構(gòu)
我是在 C 盤創(chuàng)建了一個(gè) VTKFolders
來保存所有需要的文件。
2 運(yùn)行 CMake
使用 CMake 生成一個(gè) visual studio 解決方案。首先以管理員身份打開 CMake-GUI。
配置好資源文件夾和 build 文件夾。
然后點(diǎn)擊 Configure。會彈出一個(gè)選擇框,選擇適合自己的配置,然后 Finish。
我們現(xiàn)在有幾個(gè)選項(xiàng),可以根據(jù)需要打開或關(guān)閉。對于本指南所做的唯一更改是
- Check the box after CMAKE_CXX_MP_FLAG. 這允許使用多核進(jìn)行構(gòu)建。
再點(diǎn)擊一次 Configure 應(yīng)用剛才的更改。
單擊 Generate。這將填充 build 子文件夾。
最后,單擊 Open Project 在 Visual Studio 中打開生成的解決方案。
不過這時(shí)候建議退出一下 Visual Studio,因?yàn)槟J(rèn)打開不是以管理員身份的??梢酝顺鲋匦乱怨芾韱T身份打開,而且已打開過的工程也是有記錄的,直接點(diǎn)擊就行了。
3 在 Visual Studio 中構(gòu)建
選擇 Release 和 Win32。
然后選擇 生成 -> 生成 ALL_BUILD。
根據(jù)電腦性能不同,花費(fèi)時(shí)間也不同。我電腦用了 3 分鐘,報(bào)告顯示成功 263,有 2 個(gè)失敗。我繼續(xù)往下進(jìn)行了,目前問題不大。
另外,也可以再切換 Release 成 Debug 再生成一遍,這樣之后在其他工程的 Debug 環(huán)境也可以使用 VTK。
4 安裝 VTK
為了能夠在其他項(xiàng)目中使用 VTK,它首先需要安裝。
再次打開 CMake。
點(diǎn)擊一下 Configure 看一下配置。
修改 PREFIX 的文件夾。
再次點(diǎn)擊 Configure。
然后點(diǎn)擊 Generate。
最后點(diǎn)擊 Open Project。
5 再次在 Visual Studio 中構(gòu)建
這里與第 2 步在 Visual Studio 中構(gòu)建操作完全一樣,不再放過程圖,僅放置一下結(jié)果圖。
下邊是 Release 的生成結(jié)果。
下邊是 Debug 的生成結(jié)果。
然后我們就可以在文件夾中看到很多 .dll
文件。
放一下我自己的文件夾路徑僅供參考 C:\VTKFolders\VTK-build\bin\Release
。
6 添加環(huán)境變量
然后就是根據(jù)自己的文件夾路徑,添加環(huán)境變量。
不知道怎么打開系統(tǒng)環(huán)境變量的可以根據(jù)以下提示:
[start -> Edit the system environment variables -> Advanced -> Environment Variables -> Path -> Edit -> New]
3 測試是否成功
如果一切順利,那么現(xiàn)在應(yīng)該可以編譯并運(yùn)行 C++ 示例之一了。
我用的例子是 HighlightPickedActor,直接下載解壓到一個(gè)文件夾就行了。如果沒辦法下載也可以去文章末尾復(fù)制一下代碼,保存為指定文件名也行。
打開 CMake,并選擇如下圖所示的文件夾配置。
點(diǎn)擊 Configure,應(yīng)該能自動找尋到 VTK_DIR 的路徑,如果找不到,那就是環(huán)境變量沒有配置好,重新配置一下就行了。
然后點(diǎn)擊 Generate,Open Project。
然后就是得到了一個(gè)可以打開的 Visual Studio 工程。右鍵 HighlightPickedActor 工程,選擇 “設(shè)為啟動項(xiàng)目”。
然后啟動工程,如果得到如下程序運(yùn)行結(jié)果,那么就證明已經(jīng)安裝成功了。之后在其他工程中調(diào)用即可。
文章來源:http://www.zghlxwxcb.cn/news/detail-575521.html
文件結(jié)構(gòu)
--- HighlightPickedActor
------ build <--empty
------ CMakeLists.txt
------ HighlightPickedActor.cxx文章來源地址http://www.zghlxwxcb.cn/news/detail-575521.html
CMakeLists.txt 代碼
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
project(HighlightPickedActor)
find_package(VTK COMPONENTS
CommonColor
CommonCore
FiltersSources
InteractionStyle
RenderingContextOpenGL2
RenderingCore
RenderingFreeType
RenderingGL2PSOpenGL2
RenderingOpenGL2
)
if (NOT VTK_FOUND)
message(FATAL_ERROR "HighlightPickedActor: Unable to find the VTK build folder.")
endif()
# Prevent a "command line is too long" failure in Windows.
set(CMAKE_NINJA_FORCE_RESPONSE_FILE "ON" CACHE BOOL "Force Ninja to use response files.")
add_executable(HighlightPickedActor MACOSX_BUNDLE HighlightPickedActor.cxx )
target_link_libraries(HighlightPickedActor PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
TARGETS HighlightPickedActor
MODULES ${VTK_LIBRARIES}
)
HighlightPickedActor.cxx 代碼
#include <vtkActor.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkMinimalStandardRandomSequence.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkObjectFactory.h>
#include <vtkPolyDataMapper.h>
#include <vtkPropPicker.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSphereSource.h>
namespace {
// Handle mouse events
class MouseInteractorHighLightActor : public vtkInteractorStyleTrackballCamera
{
public:
static MouseInteractorHighLightActor* New();
vtkTypeMacro(MouseInteractorHighLightActor,
vtkInteractorStyleTrackballCamera);
MouseInteractorHighLightActor()
{
LastPickedActor = NULL;
LastPickedProperty = vtkProperty::New();
}
virtual ~MouseInteractorHighLightActor()
{
LastPickedProperty->Delete();
}
virtual void OnLeftButtonDown() override
{
vtkNew<vtkNamedColors> colors;
int* clickPos = this->GetInteractor()->GetEventPosition();
// Pick from this location.
vtkNew<vtkPropPicker> picker;
picker->Pick(clickPos[0], clickPos[1], 0, this->GetDefaultRenderer());
// If we picked something before, reset its property
if (this->LastPickedActor)
{
this->LastPickedActor->GetProperty()->DeepCopy(this->LastPickedProperty);
}
this->LastPickedActor = picker->GetActor();
if (this->LastPickedActor)
{
// Save the property of the picked actor so that we can
// restore it next time
this->LastPickedProperty->DeepCopy(this->LastPickedActor->GetProperty());
// Highlight the picked actor by changing its properties
this->LastPickedActor->GetProperty()->SetColor(
colors->GetColor3d("Red").GetData());
this->LastPickedActor->GetProperty()->SetDiffuse(1.0);
this->LastPickedActor->GetProperty()->SetSpecular(0.0);
this->LastPickedActor->GetProperty()->EdgeVisibilityOn();
}
// Forward events
vtkInteractorStyleTrackballCamera::OnLeftButtonDown();
}
private:
vtkActor* LastPickedActor;
vtkProperty* LastPickedProperty;
};
vtkStandardNewMacro(MouseInteractorHighLightActor);
} // namespace
// Execute application.
int main(int argc, char* argv[])
{
vtkNew<vtkNamedColors> colors;
int numberOfSpheres = 10;
if (argc > 1)
{
numberOfSpheres = atoi(argv[1]);
}
// A renderer and render window
vtkNew<vtkRenderer> renderer;
vtkNew<vtkRenderWindow> renderWindow;
renderWindow->SetSize(640, 480);
renderWindow->AddRenderer(renderer);
renderWindow->SetWindowName("HighlightPickedActor");
// An interactor
vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
renderWindowInteractor->SetRenderWindow(renderWindow);
// Set the custom type to use for interaction.
vtkNew<MouseInteractorHighLightActor> style;
style->SetDefaultRenderer(renderer);
renderWindowInteractor->SetInteractorStyle(style);
vtkNew<vtkMinimalStandardRandomSequence> randomSequence;
randomSequence->SetSeed(8775070);
for (int i = 0; i < numberOfSpheres; ++i)
{
vtkNew<vtkSphereSource> source;
double x, y, z, radius;
// random position and radius
x = randomSequence->GetRangeValue(-5.0, 5.0);
randomSequence->Next();
y = randomSequence->GetRangeValue(-5.0, 5.0);
randomSequence->Next();
z = randomSequence->GetRangeValue(-5.0, 5.0);
randomSequence->Next();
radius = randomSequence->GetRangeValue(0.5, 1.0);
randomSequence->Next();
source->SetRadius(radius);
source->SetCenter(x, y, z);
source->SetPhiResolution(11);
source->SetThetaResolution(21);
vtkNew<vtkPolyDataMapper> mapper;
mapper->SetInputConnection(source->GetOutputPort());
vtkNew<vtkActor> actor;
actor->SetMapper(mapper);
double r, g, b;
r = randomSequence->GetRangeValue(0.4, 1.0);
randomSequence->Next();
g = randomSequence->GetRangeValue(0.4, 1.0);
randomSequence->Next();
b = randomSequence->GetRangeValue(0.4, 1.0);
randomSequence->Next();
actor->GetProperty()->SetDiffuseColor(r, g, b);
actor->GetProperty()->SetDiffuse(0.8);
actor->GetProperty()->SetSpecular(0.5);
actor->GetProperty()->SetSpecularColor(
colors->GetColor3d("White").GetData());
actor->GetProperty()->SetSpecularPower(30.0);
renderer->AddActor(actor);
}
renderer->SetBackground(colors->GetColor3d("SteelBlue").GetData());
// Render and interact
renderWindow->Render();
renderWindowInteractor->Initialize();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
Ref.
- 在 Windows 上使用 Visual Studio 構(gòu)建 VTK
- 八、VTK安裝并運(yùn)行一個(gè)例子
- Qt VTK ITK安裝與測試(二)VTK的安裝與測試
- 【轉(zhuǎn)】二、VTK用于QT的安裝
到了這里,關(guān)于【Visual Studio】在 Windows 上使用 Visual Studio 構(gòu)建 VTK的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!