Skip to content

Instantly share code, notes, and snippets.

@nicebyte
Last active September 25, 2021 17:48
Show Gist options
  • Save nicebyte/8de116f4eb0bc6dc71a4f89ac440ae28 to your computer and use it in GitHub Desktop.
Save nicebyte/8de116f4eb0bc6dc71a4f89ac440ae28 to your computer and use it in GitHub Desktop.
Make tinyobj::index_t from tinyobjloader hashable
#include <unordered_map>
#define TINYOBJLOADER_IMPLEMENTATION
#include "tiny_obj_loader.h"
namespace tinyobj {
bool operator==(const index_t& lhs, const index_t& rhs) {
return
lhs.vertex_index == rhs.vertex_index &&
lhs.normal_index == rhs.normal_index &&
lhs.texcoord_index == rhs.texcoord_index;
}
}
namespace std {
template<>
struct hash<::tinyobj::index_t> {
std::size_t operator()(const ::tinyobj::index_t& idx) const {
size_t result = 0;
hash_combine(result, idx.vertex_index);
hash_combine(result, idx.normal_index);
hash_combine(result, idx.texcoord_index);
return result;
}
private:
// From boost::hash_combine.
static void hash_combine(size_t& seed, size_t val) {
std::hash<size_t> h;
seed ^= (h(val) + 0x9e3779b9 + (seed<<6) + (seed>>2));
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment