Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

migrate to <random> #195

Merged
merged 2 commits into from
Aug 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions include/rviz_visual_tools/rviz_visual_tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include <ros/ros.h>

// C++
#include <random>
#include <string>
#include <vector>

Expand Down Expand Up @@ -1052,6 +1053,13 @@ class RvizVisualTools
static float fRand(float min, float max);
static int iRand(int min, int max);

/**
* \brief Set seed for random methods above
*
* Random sampling uses std::mt19937 internally
*/
static void setRandomSeed(unsigned int seed);

/**
* \brief Display in the console the x,y,z values of a point
*/
Expand Down Expand Up @@ -1144,6 +1152,8 @@ class RvizVisualTools
// Chose random colors from this list
static const std::array<colors, 14> ALL_RAND_COLORS;

static std::mt19937 mt_random_engine_;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this static?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because dRand, fRand, and iRand are and I did not want to break API by removing static from them.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if at least one other team member agrees, I'm also open to change them to non-static members, but that might entail more changes in other places/repos

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to merge this out of expedience to get moveit CI working again, if we want to a further refactoring of this later we can.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because dRand, fRand, and iRand are and I did not want to break API by removing static from them.

Oh, interesting. And you mean ABI?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ABI?

No, API.
RvizVisualTools::iRand(0,1) would not compile anymore if we remove static.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

o. I was asking about mt_random_engine_, not iRand

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, but how would you want to access a non-static random engine from the static iRand?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


public:
EIGEN_MAKE_ALIGNED_OPERATOR_NEW // http://eigen.tuxfamily.org/dox/group__TopicStructHavingEigenMembers.html
}; // class
Expand Down
4 changes: 2 additions & 2 deletions src/imarker_simple_demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ int main(int argc, char** argv)
ros::init(argc, argv, "imarker_simple_demo");
ROS_INFO_STREAM_NAMED("main", "Starting IMarkerSimpleDemo...");

// Seed random number generator
// srand (time(NULL));
// Optionally set a fixed random seed
// rviz_visual_tools::RvizVisualTools::setRandomSeed(0xdeadbeef);

// Allow the action server to recieve and send ros messages
ros::AsyncSpinner spinner(2);
Expand Down
23 changes: 11 additions & 12 deletions src/rviz_visual_tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@
#include <tf2/convert.h>

// C++
#include <chrono>
#include <cmath> // for random poses
#include <random>
#include <set>
#include <string>
#include <utility>
Expand All @@ -61,6 +63,8 @@ const std::array<colors, 14> RvizVisualTools::ALL_RAND_COLORS = { RED, GR
WHITE, ORANGE, YELLOW, BROWN, PINK,
LIME_GREEN, PURPLE, CYAN, MAGENTA };

std::mt19937 RvizVisualTools::mt_random_engine_(std::chrono::system_clock::now().time_since_epoch().count());

RvizVisualTools::RvizVisualTools(std::string base_frame, std::string marker_topic, ros::NodeHandle nh)
: nh_(nh), marker_topic_(std::move(marker_topic)), base_frame_(std::move(base_frame))
{
Expand Down Expand Up @@ -2801,26 +2805,21 @@ bool RvizVisualTools::posesEqual(const Eigen::Isometry3d& pose1, const Eigen::Is

double RvizVisualTools::dRand(double min, double max)
{
double d = static_cast<double>(rand()) / RAND_MAX;
return min + d * (max - min);
return std::uniform_real_distribution<double>(min,max)(mt_random_engine_);
}

float RvizVisualTools::fRand(float min, float max)
{
float d = static_cast<float>(rand()) / RAND_MAX;
return min + d * (max - min);
return std::uniform_real_distribution<float>(min,max)(mt_random_engine_);
}

int RvizVisualTools::iRand(int min, int max)
{
int n = max - min + 1;
int remainder = RAND_MAX % n;
int x;
do
{
x = rand();
} while (x >= RAND_MAX - remainder);
return min + x % n;
return std::uniform_int_distribution<int>(min,max)(mt_random_engine_);
}

void RvizVisualTools::setRandomSeed(unsigned int seed){
mt_random_engine_.seed(seed);
}

void RvizVisualTools::printTranslation(const Eigen::Vector3d& translation)
Expand Down