如何使用提升::uniform_on_sphere
How to use boost::uniform_on_sphere?
我试图在单位球面上选择一个随机点,发现 boost 提供了正是这样做的分布。但是当我尝试使用它时,所有生成的值都nan
.我不知道我做错了什么,你能开导我吗?这个小代码描述了我正在尝试做的事情:
#include <iostream>
#include <fstream>
#include <vector>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/uniform_on_sphere.hpp>
int main()
{
boost::mt19937 gen;
boost::uniform_on_sphere<float> dist(3);
{ // Seed from system random device
std::ifstream rand_device("/dev/urandom");
uint32_t seed;
rand_device >> seed;
gen.seed(seed);
}
while(1) {
// Generate a point on a unit sphere
std::vector<float> res = dist(gen);
// Print the coordinates
for(int i = 0; i < res.size(); ++i) {
std::cout << res[i] << ' ';
}
std::cout << 'n';
}
}
输出为:
nan nan nan
nan nan nan
nan nan nan
nan nan nan
广告无限...
我假设你使用的是Boost 1.49。在这种情况下,下面的方法有效。
这就是Boost有点复杂的地方。uniform_on_sphere
的对象只是绘制点所需的一个组成部分,它是分布部分。您还需要按照以下工作.cpp文件的行创建一个boost::variate_generator
。
请注意,这有我自己的包括使用系统时间来播种等。您应该能够对其进行修改以适合您。
// Standards, w/ctime to seed based on system time.
#include <iostream>
#include <fstream>
#include <vector>
#include <ctime>
// Boost. Requires variate_generator to actually draw the values.
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/uniform_on_sphere.hpp>
#include <boost/random/variate_generator.hpp>
int main(){
typedef boost::random::mt19937 gen_type;
// You can seed this your way as well, but this is my quick and dirty way.
gen_type rand_gen;
rand_gen.seed(static_cast<unsigned int>(std::time(0)));
// Create the distribution object.
boost::uniform_on_sphere<float> unif_sphere(3);
// This is what will actually supply drawn values.
boost::variate_generator<gen_type&, boost::uniform_on_sphere<float> > random_on_sphere(rand_gen, unif_sphere);
// Now you can draw a vector of drawn coordinates as such:
std::vector<float> random_sphere_point = random_on_sphere();
// Print out the drawn sphere triple's values.
for(int i = 0; i < random_sphere_point.size(); ++i) {
std::cout << random_sphere_point.at(i) << ' ';
}
std::cout << 'n';
return 0;
}
当我在控制台中运行它时,我得到的东西似乎是正确的:
ely@AMDESK:~/Desktop/Programming/C++/RandOnSphere$ g++ -I /usr/include/boost_1_49/ -L /usr/include/boost_1_49/stage/lib randomOnSphere.cpp -o ros
ely@AMDESK:~/Desktop/Programming/C++/RandOnSphere$ ./ros
0.876326 -0.0441729 0.479689
ely@AMDESK:~/Desktop/Programming/C++/RandOnSphere$ ./ros
-0.039037 -0.17792 0.98327
ely@AMDESK:~/Desktop/Programming/C++/RandOnSphere$ ./ros
-0.896734 0.419589 -0.14076
相关文章:
- 瓦尔格林德:数学函数"Conditional jump or move depends on uninitialised value(s)"
- 循环挂起迭代的 std::擦除 on std::list
- SIGSEGV on Boost UDP 套接字关闭 - tcache_get at malloc.c.
- CPP 中的瓦尔格林德和记忆泄漏:"Conditional jump or move depends on uninitialised values"
- std::bind on statd::array 的运算符 []
- 您将如何连接"on the fly"文本+整数并将其传递给函数?
- 如何修复输出日志中的"EnableInput can only be specified on a Pawn for its Controller"错误
- VS2019 - Sudo Remote Debugging on Linux with Cmake project
- C++ library with Tensorflow on Android
- SFML 交叉编译 for Windows on Linux.
- How to recover system gcc compiler on centos 6
- Cmake with Flex/Bison on windows
- 按原样保存用户输入 - Ruby on Rails
- OpenAL C++ on Linux
- Boost::process on Windows - with MinGW?
- CMake on FindOpenGL.cmake 中的错误,当使用导入的目标 OpenGL::GLU?
- Utilization of atomic_flag on C++
- anaconda cythonize C++ on windows 10
- CMake & MinGW Compileilation on Windows,无需 -G "MinGW Makefiles"标志
- 为什么我不能使用 Sphere 来缩放字形?