C++ VTK 生成高斯随机数
大眼睛兔兔 人气:0VTK实例高斯随机数的生成
这个例子演示了从一个平均数是0.0和标准偏差是2.2的高斯分布中随机生成3个随机数。先创建两个文件:GaussianRandomNumber.cpp和CMakeLists.txt。
代码如下:
GaussianRandomNumber.cxx
#include <vtkBoxMuellerRandomSequence.h> #include <vtkNew.h> int main(int, char*[]) { // The number of random numbers we wish to produce unsigned int numRand = 3; vtkNew<vtkBoxMuellerRandomSequence> randomSequence; // Generate numRand random numbers from a Gaussian distribution with mean 0.0 // and standard deviation 2.2 auto mean = 0.0; auto standardDeviation = 2.2; for (unsigned int i = 0; i < numRand; i++) { auto a = randomSequence->GetScaledValue(mean, standardDeviation); randomSequence->Next(); std::cout << a << std::endl; } return EXIT_SUCCESS; }
CMakeLists.txt
cmake_minimum_required(VERSION 3.3 FATAL_ERROR) project(GaussianRandomNumber) find_package(VTK COMPONENTS vtkCommonCore QUIET ) if (NOT VTK_FOUND) message("Skipping GaussianRandomNumber: ${VTK_NOT_FOUND_MESSAGE}") return () endif() message (STATUS "VTK_VERSION: ${VTK_VERSION}") if (VTK_VERSION VERSION_LESS "8.90.0") # old system include(${VTK_USE_FILE}) add_executable(GaussianRandomNumber MACOSX_BUNDLE GaussianRandomNumber.cxx ) target_link_libraries(GaussianRandomNumber PRIVATE ${VTK_LIBRARIES}) else () # include all components add_executable(GaussianRandomNumber MACOSX_BUNDLE GaussianRandomNumber.cxx ) target_link_libraries(GaussianRandomNumber PRIVATE ${VTK_LIBRARIES}) # vtk_module_autoinit is needed vtk_module_autoinit( TARGETS GaussianRandomNumber MODULES ${VTK_LIBRARIES} ) endif ()
编译
将以上两个文件放在自定义文件夹里,再新建一个build目录,用于存放编译后生成的文件。打开CMake程序,编译设置界面如下:
调试
在刚刚生成的build目录下,以管理员身份打开vs2019,再打开.sln文件,点击build生成解决方案,如下图:
并将GaussianRandomNumber设为启动项,
进行本地调试,生成三个随机数:
//注:确保将VTK bin目录添加到路径中,这将在运行时解析VTK 的dll文件。
加载全部内容