Skip to content

Commit

Permalink
Convert from rand() to C++11 random
Browse files Browse the repository at this point in the history
  • Loading branch information
walbourn committed May 30, 2021
1 parent 67dd58e commit 4ca2c5c
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 2 deletions.
6 changes: 5 additions & 1 deletion UVAtlas/isochart/UVAtlasRepacker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ CUVAtlasRepacker::CUVAtlasRepacker(std::vector<UVAtlasVertex>* pvVertexArray,
m_pOurChartNumber(pChartNumber),
m_pOurIterationTimes(pIterationTimes)
{
std::random_device randomDevice;
m_randomEngine.seed(randomDevice());
}

/***************************************************************************\
Expand Down Expand Up @@ -1301,6 +1303,8 @@ void CUVAtlasRepacker::PutChart(uint32_t index)

m_triedInternalSpace = int(1e8f);

std::uniform_int_distribution<> dis(0, 1);

for (uint32_t i = 0; i < m_iRotateNum; i++)
{
// for every position of chart, first do tessellation on it
Expand All @@ -1318,7 +1322,7 @@ void CUVAtlasRepacker::PutChart(uint32_t index)
else if (m_currAspectRatio < m_AspectRatio)
PutSide = 1;
else
PutSide = int(floorf(float(rand()) + 0.5f));
PutSide = dis(m_randomEngine);

if (PutSide == 0) // put on left or right side
{
Expand Down
2 changes: 2 additions & 0 deletions UVAtlas/isochart/UVAtlasRepacker.h
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,8 @@ namespace IsochartRepacker
size_t* m_pOurIterationTimes;

Isochart::CCallbackSchemer m_callbackSchemer;

std::mt19937_64 m_randomEngine;
};

}
2 changes: 2 additions & 0 deletions UVAtlas/isochart/isochartengine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ CIsochartEngine::CIsochartEngine() :
#endif
m_dwOptions(_OPTION_ISOCHART_DEFAULT)
{
std::random_device randomDevice;
m_randomEngine.seed(randomDevice());
}

CIsochartEngine::~CIsochartEngine()
Expand Down
9 changes: 9 additions & 0 deletions UVAtlas/isochart/isochartengine.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ namespace Isochart
const uint32_t* pdwFaceAdjacentArrayIn) noexcept override;

HRESULT CreateEngineMutex();

float UniformRand(float maxValue) const
{
std::uniform_real_distribution<float> dis(0.f, maxValue);
return dis(m_randomEngine);
}

private:
enum EngineState
{
Expand Down Expand Up @@ -248,6 +255,8 @@ namespace Isochart

unsigned int m_dwOptions;

mutable std::mt19937_64 m_randomEngine;

friend CIsochartMesh;
};

Expand Down
2 changes: 1 addition & 1 deletion UVAtlas/isochart/meshoptimizestretch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1820,7 +1820,7 @@ bool CIsochartMesh::OptimizeVertexStretchAroundCenter(
while (iteration < optimizeInfo.dwRandOptOneVertTimes)
{
// 1. Get a new random position in the optimizing circle range
float fAngle = float(rand()) * 2.f * XM_PI / RAND_MAX;
float fAngle = m_IsochartEngine.UniformRand(2.f * XM_PI);
vertInfo.end.x =
vertInfo.center.x + vertInfo.fRadius * cosf(fAngle);
vertInfo.end.y =
Expand Down
1 change: 1 addition & 0 deletions UVAtlas/pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
#include <functional>
#include <memory>
#include <new>
#include <random>
#include <utility>
#include <vector>
#include <queue>
Expand Down

0 comments on commit 4ca2c5c

Please sign in to comment.