简体   繁体   English

如何使用boost :: uniform_on_sphere?

[英]How to use boost::uniform_on_sphere?

I am trying to pick a random point on a unit sphere, and found that boost provides the distribution that does exactly this. 我试图在单位球体上选择一个随机点,并发现boost提供了完全相同的分布。 But when I try to use it, all generated values are nan . 但是当我尝试使用它时,所有生成的值都是nan I don't know what I am doing wrong, could you please enlighten me? 我不知道我做错了什么,请你赐教? This small code depicts what I am trying to do: 这个小代码描述了我想要做的事情:

#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';
  }
}

The output is: 输出是:

nan nan nan 
nan nan nan 
nan nan nan 
nan nan nan 

ad infinitum... 无限的...

I'm assuming you are using Boost 1.49. 我假设你正在使用Boost 1.49。 The method below works in that case. 在这种情况下,下面的方法适用。

This is where Boost is a little complicated. 这是Boost有点复杂的地方。 The object that is uniform_on_sphere is just one component of what you need in order to draw points, it is the distribution part.. You also need to create a boost::variate_generator along the lines of the following working .cpp file below. uniform_on_sphere对象只是绘制点所需要的一个组件,它是分布部分。你还需要在下面的下面的工作.cpp文件的行中创建一个boost::variate_generator

Note, this has my own includes for using the system time to seed, etc. You should be able to modify this to suit you. 注意,这有我自己的包括使用系统时间播种等。您应该能够修改它以适合您。

// 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;
}

When I run this in the console, I get seemingly correct stuff: 当我在控制台中运行它时,我得到看似正确的东西:

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 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM