remove Rand.* and <random> (no C++11)

This commit is contained in:
erwincoumans
2019-01-23 17:35:45 -08:00
parent 03549ca7c6
commit e637b24237
5 changed files with 4 additions and 309 deletions

View File

@@ -3,7 +3,6 @@
#define _USE_MATH_DEFINES
#include <math.h>
cRand cMathUtil::gRand = cRand();
int cMathUtil::Clamp(int val, int min, int max)
{
@@ -45,69 +44,6 @@ double cMathUtil::NormalizeAngle(double theta)
return norm_theta;
}
double cMathUtil::RandDouble()
{
return RandDouble(0, 1);
}
double cMathUtil::RandDouble(double min, double max)
{
return gRand.RandDouble(min, max);
}
double cMathUtil::RandDoubleNorm(double mean, double stdev)
{
return gRand.RandDoubleNorm(mean, stdev);
}
double cMathUtil::RandDoubleExp(double lambda)
{
return gRand.RandDoubleExp(lambda);
}
double cMathUtil::RandDoubleSeed(double seed)
{
unsigned int int_seed = *reinterpret_cast<unsigned int*>(&seed);
std::default_random_engine rand_gen(int_seed);
std::uniform_real_distribution<double> dist;
return dist(rand_gen);
}
int cMathUtil::RandInt()
{
return gRand.RandInt();
}
int cMathUtil::RandInt(int min, int max)
{
return gRand.RandInt(min, max);
}
int cMathUtil::RandUint()
{
return gRand.RandUint();
}
int cMathUtil::RandUint(unsigned int min, unsigned int max)
{
return gRand.RandUint(min, max);
}
int cMathUtil::RandIntExclude(int min, int max, int exc)
{
return gRand.RandIntExclude(min, max, exc);
}
void cMathUtil::SeedRand(unsigned long int seed)
{
gRand.Seed(seed);
srand(gRand.RandInt());
}
int cMathUtil::RandSign()
{
return gRand.RandSign();
}
double cMathUtil::SmoothStep(double t)
{
@@ -115,10 +51,6 @@ double cMathUtil::SmoothStep(double t)
return val;
}
bool cMathUtil::FlipCoin(double p)
{
return gRand.FlipCoin(p);
}
tMatrix cMathUtil::TranslateMat(const tVector& trans)
{
@@ -665,26 +597,6 @@ double cMathUtil::Sigmoid(double x, double gamma, double bias)
return val;
}
int cMathUtil::SampleDiscreteProb(const Eigen::VectorXd& probs)
{
assert(std::abs(probs.sum() - 1) < 0.00001);
double rand = RandDouble();
int rand_idx = gInvalidIdx;
int num_probs = static_cast<int>(probs.size());
for (int i = 0; i < num_probs; ++i)
{
double curr_prob = probs[i];
rand -= curr_prob;
if (rand <= 0)
{
rand_idx = i;
break;
}
}
return rand_idx;
}
tVector cMathUtil::CalcBarycentric(const tVector& p, const tVector& a, const tVector& b, const tVector& c)
{
@@ -791,32 +703,7 @@ bool cMathUtil::CheckNextInterval(double delta, double curr_val, double int_size
return new_action;
}
tVector cMathUtil::SampleRandPt(const tVector& bound_min, const tVector& bound_max)
{
tVector pt = tVector(RandDouble(bound_min[0], bound_max[0]),
RandDouble(bound_min[1], bound_max[1]),
RandDouble(bound_min[2], bound_max[2]), 0);
return pt;
}
tVector cMathUtil::SampleRandPtBias(const tVector& bound_min, const tVector& bound_max)
{
return SampleRandPtBias(bound_min, bound_max, 0.5 * (bound_max + bound_min));
}
tVector cMathUtil::SampleRandPtBias(const tVector& bound_min, const tVector& bound_max, const tVector& focus)
{
double t = RandDouble(0, 1);
tVector size = bound_max - bound_min;
tVector new_min = focus + (t * 0.5) * size;
tVector new_max = focus - (t * 0.5) * size;
tVector offset = (bound_min - new_min).cwiseMax(0);
offset += (bound_max - new_max).cwiseMin(0);
new_min += offset;
new_max += offset;
return SampleRandPt(new_min, new_max);
}
void cMathUtil::QuatSwingTwistDecomposition(const tQuaternion& q, const tVector& dir, tQuaternion& out_swing, tQuaternion& out_twist)
{

View File

@@ -1,11 +1,10 @@
#pragma once
#include <random>
#include "Eigen/Dense"
#include "Eigen/StdVector"
#include "Eigen/Geometry"
#include "Rand.h"
#define _USE_MATH_DEFINES
#include "math.h"
@@ -47,20 +46,6 @@ public:
static double NormalizeAngle(double theta);
// rand number
static double RandDouble();
static double RandDouble(double min, double max);
static double RandDoubleNorm(double mean, double stdev);
static double RandDoubleExp(double lambda);
static double RandDoubleSeed(double seed);
static int RandInt();
static int RandInt(int min, int max);
static int RandUint();
static int RandUint(unsigned int min, unsigned int max);
static int RandIntExclude(int min, int max, int exc);
static void SeedRand(unsigned long int seed);
static int RandSign();
static bool FlipCoin(double p = 0.5);
static double SmoothStep(double t);
// matrices
@@ -117,7 +102,6 @@ public:
static double Sigmoid(double x);
static double Sigmoid(double x, double gamma, double bias);
static int SampleDiscreteProb(const Eigen::VectorXd& probs);
static tVector CalcBarycentric(const tVector& p, const tVector& a, const tVector& b, const tVector& c);
static bool ContainsAABB(const tVector& pt, const tVector& aabb_min, const tVector& aabb_max);
@@ -134,10 +118,6 @@ public:
// check if curr_val and curr_val - delta belong to different intervals
static bool CheckNextInterval(double delta, double curr_val, double int_size);
static tVector SampleRandPt(const tVector& bound_min, const tVector& bound_max);
// samples a bound within the given bounds with a benter towards the focus pt
static tVector SampleRandPtBias(const tVector& bound_min, const tVector& bound_max);
static tVector SampleRandPtBias(const tVector& bound_min, const tVector& bound_max, const tVector& focus);
static void QuatSwingTwistDecomposition(const tQuaternion& q, const tVector& dir, tQuaternion& out_swing, tQuaternion& out_twist);
static tQuaternion ProjectQuat(const tQuaternion& q, const tVector& dir);
@@ -145,7 +125,6 @@ public:
static void ButterworthFilter(double dt, double cutoff, Eigen::VectorXd& out_x);
private:
static cRand gRand;
template <typename T>
static T SignAux(T val)

View File

@@ -1,140 +0,0 @@
#include "Rand.h"
#include <time.h>
#include <assert.h>
#include <algorithm>
cRand::cRand()
{
unsigned long int seed = static_cast<unsigned long int>(time(NULL));
mRandGen = std::default_random_engine(seed);
mRandDoubleDist = std::uniform_real_distribution<double>(0, 1);
mRandDoubleDistNorm = std::normal_distribution<double>(0, 1);
mRandIntDist = std::uniform_int_distribution<int>(std::numeric_limits<int>::min() + 1, std::numeric_limits<int>::max()); // + 1 since there is one more neg int than pos int
mRandUintDist = std::uniform_int_distribution<unsigned int>(std::numeric_limits<unsigned int>::min(), std::numeric_limits<unsigned int>::max());
}
cRand::cRand(unsigned long int seed)
{
Seed(seed);
}
cRand::~cRand()
{
}
double cRand::RandDouble()
{
return mRandDoubleDist(mRandGen);
}
double cRand::RandDouble(double min, double max)
{
if (min == max)
{
return min;
}
// generate random double in [min, max]
double rand_double = mRandDoubleDist(mRandGen);
rand_double = min + (rand_double * (max - min));
return rand_double;
}
double cRand::RandDoubleExp(double lambda)
{
std::exponential_distribution<double> dist(lambda);
double rand_double = dist(mRandGen);
return rand_double;
}
double cRand::RandDoubleNorm(double mean, double stdev)
{
double rand_double = mRandDoubleDistNorm(mRandGen);
rand_double = mean + stdev * rand_double;
return rand_double;
}
int cRand::RandInt()
{
return mRandIntDist(mRandGen);
}
int cRand::RandInt(int min, int max)
{
if (min == max)
{
return min;
}
// generate random double in [min, max)
int delta = max - min;
int rand_int = std::abs(RandInt());
rand_int = min + rand_int % delta;
return rand_int;
}
int cRand::RandUint()
{
return mRandUintDist(mRandGen);
}
int cRand::RandUint(unsigned int min, unsigned int max)
{
if (min == max)
{
return min;
}
// generate random double in [min, max)
int delta = max - min;
int rand_int = RandUint();
rand_int = min + rand_int % delta;
return rand_int;
}
int cRand::RandIntExclude(int min, int max, int exc)
{
int rand_int = 0;
if (exc < min || exc >= max)
{
rand_int = RandInt(min, max);
}
else
{
int new_max = max - 1;
if (new_max <= min)
{
rand_int = min;
}
else
{
rand_int = RandInt(min, new_max);
if (rand_int >= exc)
{
++rand_int;
}
}
}
return rand_int;
}
void cRand::Seed(unsigned long int seed)
{
mRandGen.seed(seed);
mRandDoubleDist.reset();
mRandDoubleDistNorm.reset();
mRandIntDist.reset();
mRandUintDist.reset();
}
int cRand::RandSign()
{
return FlipCoin() ? -1 : 1;
}
bool cRand::FlipCoin(double p)
{
return (RandDouble(0, 1) < p);
}

View File

@@ -1,31 +0,0 @@
#pragma once
#include <random>
class cRand
{
public:
cRand();
cRand(unsigned long int seed);
virtual ~cRand();
virtual double RandDouble();
virtual double RandDouble(double min, double max);
virtual double RandDoubleExp(double lambda);
virtual double RandDoubleNorm(double mean, double stdev);
virtual int RandInt();
virtual int RandInt(int min, int max);
virtual int RandUint();
virtual int RandUint(unsigned int min, unsigned int max);
virtual int RandIntExclude(int min, int max, int exc);
virtual void Seed(unsigned long int seed);
virtual int RandSign();
virtual bool FlipCoin(double p = 0.5);
private:
std::default_random_engine mRandGen;
std::uniform_real_distribution<double> mRandDoubleDist;
std::normal_distribution<double> mRandDoubleDistNorm;
std::uniform_int_distribution<int> mRandIntDist;
std::uniform_int_distribution<unsigned int> mRandUintDist;
};

View File

@@ -122,7 +122,7 @@ void btConvexPolyhedron::initialize()
for (int p = 0; p < m_uniqueEdges.size(); p++)
{
if (IsAlmostZero(m_uniqueEdges[p] - edge) ||
if (IsAlmostZero1(m_uniqueEdges[p] - edge) ||
IsAlmostZero1(m_uniqueEdges[p] + edge))
{
found = true;