r5sdk/r5dev/mathlib/halton.cpp
Kawe Mazidjatari 7d89a42b56 Mathlib implementation
SourceSDK mathlib port with light modifications.
Renamed Vector to Vector3D (to avoid confusion with std::vector (declared as vector) and Vector2D/Vector4D).
2022-07-06 21:11:32 +02:00

57 lines
994 B
C++
Raw Blame History

//========= Copyright <20> 1996-2005, Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
//=====================================================================================//
#include "core/stdafx.h"
#include "mathlib/halton.h"
// NOTE: This has to be the last file included!
//#include "tier0/memdbgon.h"
HaltonSequenceGenerator_t::HaltonSequenceGenerator_t(int b)
{
base = b;
fbase = (float)b;
seed = 1;
}
float HaltonSequenceGenerator_t::GetElement(int elem)
{
int tmpseed = seed;
float ret = 0.0;
float base_inv = 1.0 / fbase;
while (tmpseed)
{
int dig = tmpseed % base;
ret += ((float)dig) * base_inv;
base_inv /= fbase;
tmpseed /= base;
}
return ret;
}
int InsideOut(int nTotal, int nCounter)
{
int b = 0;
for (int m = nTotal, k = 1; k < nTotal; k <<= 1)
{
if (nCounter << 1 >= m)
{
b += k;
nCounter -= (m + 1) >> 1;
m >>= 1;
}
else
{
m = (m + 1) >> 1;
}
}
Assert((b >= 0) && (b < nTotal));
return b;
}