Skip to content

Commit

Permalink
add constants
Browse files Browse the repository at this point in the history
  • Loading branch information
shinjiogaki committed Mar 9, 2021
1 parent d789482 commit f90ef1d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 26 deletions.
21 changes: 4 additions & 17 deletions include/voroce.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ namespace voroce

static const auto LCG = 48271;

// These are not recommended because of artifacts especially for hexgrid
static int32_t Hash2DLowQuality(const glm::ivec2& p)
{
return ((p.x * PrimeU) ^ (p.y * PrimeV)) * LCG;
Expand All @@ -33,22 +32,6 @@ namespace voroce
return ((p.x * PrimeU) ^ (p.y * PrimeV) ^ (p.z * PrimeW) ^ (p.w * PrimeT)) * LCG;
}

// These are expensive but behave way better
static int32_t Hash2D(const glm::ivec2& p)
{
return std::hash<int32_t>()(std::hash<int32_t>()(p.x) + p.y);
}

static int32_t Hash3D(const glm::ivec3& p)
{
return std::hash<int32_t>()(std::hash<int32_t>()(std::hash<int32_t>()(p.x) + p.y) + p.z);
}

static int32_t Hash4D(const glm::ivec4& p)
{
return std::hash<int32_t>()(std::hash<int32_t>()(std::hash<int32_t>()(std::hash<int32_t>()(p.x) + p.y) + p.z) + p.w);
}

// minstd_rand (TODO: use better hash, do something beter here, fast but a bit ugly...)

static auto OffsetX(const int32_t seed)
Expand Down Expand Up @@ -91,6 +74,10 @@ namespace voroce
return ((((seed * LCG) * LCG) * LCG) / float(0xFFFFFFFF)) * jitter + 0.5f;
}

// constants
static const float sqrt3;
static const float one_over_sqrt3;

// traversal order
static const int32_t us2[4][13];
static const int32_t vs2[4][13];
Expand Down
18 changes: 9 additions & 9 deletions src/voroce.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

using namespace voroce;

const float Voronoi::sqrt3 = std::sqrt(3.0f);
const float Voronoi::one_over_sqrt3 = 1.0f / std::sqrt(3.0f);

// naive implementation
std::tuple<int32_t, float, glm::vec2> Voronoi::Evaluate2DRef(const glm::vec2& source, int32_t (*my_hash)(const glm::ivec2 &p), const float jitter)
{
Expand Down Expand Up @@ -1024,8 +1027,7 @@ std::tuple<int32_t, float, glm::vec2> Voronoi::Evaluate2DTri(const glm::vec2& so
{
assert(0.0f <= jitter && jitter <= 1.0f);

const auto one_3 = 1.0f / std::sqrt(3.0f);
const auto local = glm::vec2(glm::dot(glm::vec2(1.0f, -one_3), source), glm::dot(glm::vec2(0.0f, 2.0f * one_3), source));
const auto local = glm::vec2(glm::dot(glm::vec2(1.0f, -one_over_sqrt3), source), glm::dot(glm::vec2(0.0f, 2.0f * one_over_sqrt3), source));

const auto origin = glm::vec2(std::floor(local.x), std::floor(local.y));
const auto quantized = glm::ivec2(int32_t(origin.x), int32_t(origin.y));
Expand Down Expand Up @@ -1121,20 +1123,18 @@ std::tuple<int32_t, float, glm::vec2> Voronoi::Evaluate2DHex(const glm::vec2& so
{
assert(0.0f <= jitter && jitter <= 1.0f);

static const auto sqrt_3 = std::sqrt(3.0f);

static const std::array<glm::vec2, 3> basis =
{
glm::vec2(-sqrt_3 * 0.5f, -0.5f),
glm::vec2( sqrt_3 * 0.5f, -0.5f),
glm::vec2(-sqrt3 * 0.5f, -0.5f),
glm::vec2( sqrt3 * 0.5f, -0.5f),
glm::vec2(0, 1)
};

static const std::array<glm::vec2, 3> ortho =
{
glm::vec2(-1 / sqrt_3, -1),
glm::vec2( 1 / sqrt_3, -1),
glm::vec2( 2 / sqrt_3, 0)
glm::vec2( -one_over_sqrt3, -1),
glm::vec2( one_over_sqrt3, -1),
glm::vec2(2 * one_over_sqrt3, 0)
};

const std::array<float, 3> dots =
Expand Down

0 comments on commit f90ef1d

Please sign in to comment.