Skip to content

Commit

Permalink
#5576: Move hashing helpers and constants to libs/render/VertexHashin…
Browse files Browse the repository at this point in the history
…g.h such that they can be re-used from the unit-tests
  • Loading branch information
codereader committed Apr 5, 2021
1 parent 09f567e commit cef901e
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 26 deletions.
41 changes: 22 additions & 19 deletions radiantcore/model/import/Hashing.h → libs/render/VertexHashing.h
Expand Up @@ -23,24 +23,27 @@
* duplicates in meshes with many verts. Each "colliding" vertex will then be compared
* in-depth by the std::equal_to<> specialisation, where the epsilon-comparison is performed.
*/
namespace
namespace render
{
// A hash combination function based on the one used in boost and found on stackoverflow
inline void combineHash(std::size_t& seed, std::size_t hash)
namespace detail
{
seed ^= hash + 0x9e3779b9 + (seed << 6) + (seed >> 2);
// A hash combination function based on the one used in boost and found on stackoverflow
inline void combineHash(std::size_t& seed, std::size_t hash)
{
seed ^= hash + 0x9e3779b9 + (seed << 6) + (seed >> 2);
}

// Delivers 10.0^signficantDigits
constexpr double RoundingFactor(std::size_t significantVertexDigits)
{
return significantVertexDigits == 1 ? 10.0 : 10.0 * RoundingFactor(significantVertexDigits - 1);
}
}

// These epsilons below correspond to the CVARs in the game code
constexpr double VertexEpsilon = 0.01; // r_slopVertex
constexpr double NormalEpsilon = 0.02; // r_slopNormal
constexpr double TexCoordEpsilon = 0.001; // r_slopTexCoord

// Delivers 10.0^signficantDigits
constexpr double RoundingFactor(std::size_t significantVertexDigits)
{
return significantVertexDigits == 1 ? 10.0 : 10.0 * RoundingFactor(significantVertexDigits - 1);
}
}

// Coarse hash of the 3-component vector
Expand All @@ -54,12 +57,12 @@ struct std::hash<Vector3>

size_t operator()(const Vector3& v) const
{
auto xHash = static_cast<std::size_t>(v.x() * RoundingFactor(SignificantVertexDigits));
auto yHash = static_cast<std::size_t>(v.y() * RoundingFactor(SignificantVertexDigits));
auto zHash = static_cast<std::size_t>(v.z() * RoundingFactor(SignificantVertexDigits));
auto xHash = static_cast<std::size_t>(v.x() * render::detail::RoundingFactor(SignificantVertexDigits));
auto yHash = static_cast<std::size_t>(v.y() * render::detail::RoundingFactor(SignificantVertexDigits));
auto zHash = static_cast<std::size_t>(v.z() * render::detail::RoundingFactor(SignificantVertexDigits));

combineHash(xHash, yHash);
combineHash(xHash, zHash);
render::detail::combineHash(xHash, yHash);
render::detail::combineHash(xHash, zHash);

return xHash;
}
Expand All @@ -85,9 +88,9 @@ struct std::equal_to<ArbitraryMeshVertex>
{
bool operator()(const ArbitraryMeshVertex& a, const ArbitraryMeshVertex& b) const
{
return a.vertex.isEqual(b.vertex, VertexEpsilon) &&
a.normal.dot(b.normal) > (1.0 - NormalEpsilon) &&
a.texcoord.isEqual(b.texcoord, TexCoordEpsilon) &&
a.colour.isEqual(b.colour, VertexEpsilon);
return a.vertex.isEqual(b.vertex, render::VertexEpsilon) &&
a.normal.dot(b.normal) > (1.0 - render::NormalEpsilon) &&
a.texcoord.isEqual(b.texcoord, render::TexCoordEpsilon) &&
a.colour.isEqual(b.colour, render::VertexEpsilon);
}
};
2 changes: 1 addition & 1 deletion radiantcore/model/import/AseModel.cpp
Expand Up @@ -6,7 +6,7 @@
#include "string/case_conv.h"
#include "string/trim.h"

#include "Hashing.h"
#include "render/VertexHashing.h"

/* -----------------------------------------------------------------------------
Expand Down
6 changes: 4 additions & 2 deletions test/Models.cpp
Expand Up @@ -3,6 +3,8 @@
#include "imodelsurface.h"
#include "imodelcache.h"

#include "render/VertexHashing.h"

namespace test
{

Expand Down Expand Up @@ -245,7 +247,7 @@ void expectVertexWithNormal(const model::IModelSurface& surface, const Vertex3f&
{
EXPECT_TRUE(surfaceHasVertexWith(surface, [&](const ArbitraryMeshVertex& v)->bool
{
return v.vertex.isEqual(vertex, 0.01) && v.normal.dot(normal) > 1.0 - 0.02;
return v.vertex.isEqual(vertex, render::VertexEpsilon) && v.normal.dot(normal) > 1.0 - render::NormalEpsilon;
})) << "Could not find a vertex with xyz = " << vertex << " and normal " << normal;
}

Expand Down Expand Up @@ -284,7 +286,7 @@ void expectVertexWithColour(const model::IModelSurface& surface, const Vertex3f&
{
EXPECT_TRUE(surfaceHasVertexWith(surface, [&](const ArbitraryMeshVertex& v)->bool
{
return v.vertex.isEqual(vertex, 1e-5) && v.colour.isEqual(colour, 1e-5);
return v.vertex.isEqual(vertex, render::VertexEpsilon) && v.colour.isEqual(colour, render::VertexEpsilon);
})) << "Could not find a vertex with xyz = " << vertex << " and colour " << colour;
}

Expand Down
1 change: 0 additions & 1 deletion tools/msvc/DarkRadiantCore.vcxproj
Expand Up @@ -860,7 +860,6 @@
<ClInclude Include="..\..\radiantcore\model\export\WavefrontExporter.h" />
<ClInclude Include="..\..\radiantcore\model\import\AseModel.h" />
<ClInclude Include="..\..\radiantcore\model\import\AseModelLoader.h" />
<ClInclude Include="..\..\radiantcore\model\import\Hashing.h" />
<ClInclude Include="..\..\radiantcore\model\import\ModelImporterBase.h" />
<ClInclude Include="..\..\radiantcore\model\md5\MD5Anim.h" />
<ClInclude Include="..\..\radiantcore\model\md5\MD5AnimationCache.h" />
Expand Down
3 changes: 0 additions & 3 deletions tools/msvc/DarkRadiantCore.vcxproj.filters
Expand Up @@ -2115,8 +2115,5 @@
<ClInclude Include="..\..\radiantcore\model\import\AseModel.h">
<Filter>src\model\import</Filter>
</ClInclude>
<ClInclude Include="..\..\radiantcore\model\import\Hashing.h">
<Filter>src\model\import</Filter>
</ClInclude>
</ItemGroup>
</Project>
1 change: 1 addition & 0 deletions tools/msvc/libs.vcxproj
Expand Up @@ -211,6 +211,7 @@
<ClInclude Include="..\..\libs\render\VectorLightList.h" />
<ClInclude Include="..\..\libs\render\Vertex3f.h" />
<ClInclude Include="..\..\libs\render\VertexCb.h" />
<ClInclude Include="..\..\libs\render\VertexHashing.h" />
<ClInclude Include="..\..\libs\render\VertexNCb.h" />
<ClInclude Include="..\..\libs\render\VertexNT.h" />
<ClInclude Include="..\..\libs\render\View.h" />
Expand Down
3 changes: 3 additions & 0 deletions tools/msvc/libs.vcxproj.filters
Expand Up @@ -290,6 +290,9 @@
<ClInclude Include="..\..\libs\materials\FrobStageSetup.h">
<Filter>materials</Filter>
</ClInclude>
<ClInclude Include="..\..\libs\render\VertexHashing.h">
<Filter>render</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Filter Include="util">
Expand Down

0 comments on commit cef901e

Please sign in to comment.