Skip to content

Commit 1706eb8

Browse files
Jiali Duanfacebook-github-bot
Jiali Duan
authored andcommitted
Simplify MC C++ hashing logic
Summary: To be consistent with CUDA hashing, the diff replaces boost hasher with a simplified hasher for storing unique global edge_ids. Reviewed By: kjchalup Differential Revision: D41140382 fbshipit-source-id: 2ce598e5edcf6369fe13bd15d1f5e014b252027b
1 parent 8b82918 commit 1706eb8

File tree

2 files changed

+12
-40
lines changed

2 files changed

+12
-40
lines changed

pytorch3d/csrc/marching_cubes/marching_cubes_cpu.cpp

+1-8
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ std::tuple<at::Tensor, at::Tensor, at::Tensor> MarchingCubesCpu(
3636

3737
// Create tensor accessors
3838
auto vol_a = vol.accessor<float, 3>();
39-
// vpair_to_edge maps a pair of vertex ids to its corresponding edge id
40-
std::unordered_map<std::pair<int, int>, int64_t> vpair_to_edge;
4139
// edge_id_to_v maps from an edge id to a vertex position
4240
std::unordered_map<int64_t, Vertex> edge_id_to_v;
4341
// uniq_edge_id: used to remove redundant edge ids
@@ -64,12 +62,7 @@ std::tuple<at::Tensor, at::Tensor, at::Tensor> MarchingCubesCpu(
6462
const int e = _FACE_TABLE[cube.cubeindex][j];
6563
interp_points[e] = cube.VertexInterp(isolevel, e, vol_a);
6664

67-
auto vpair = cube.GetVPairFromEdge(e, W, H);
68-
if (!vpair_to_edge.count(vpair)) {
69-
vpair_to_edge[vpair] = vpair_to_edge.size();
70-
}
71-
72-
int64_t edge = vpair_to_edge[vpair];
65+
int64_t edge = cube.HashVpair(e, W, H, D);
7366
tri.push_back(edge);
7467
ps.push_back(interp_points[e]);
7568

pytorch3d/csrc/marching_cubes/marching_cubes_utils.h

+11-32
Original file line numberDiff line numberDiff line change
@@ -122,45 +122,24 @@ struct Cube {
122122
return p1 * (1 - ratio) + p2 * ratio;
123123
}
124124

125-
// Get a tuple of global vertex ID from a local edge ID
126-
// Global vertex ID is calculated as (x + dx) + (y + dy) * W + (z + dz) * W *
127-
// H
128-
125+
// Hash an edge into a global edge_id. The function binds an
126+
// edge with an integer to address floating point precision issue.
127+
//
129128
// Args:
130-
// edge: local edge ID in the cube
131-
// W: width of x dimension
132-
// H: height of y dimension
129+
// v1_id: global id of vertex 1
130+
// v2_id: global id of vertex 2
131+
// W: width of the 3d grid
132+
// H: height of the 3d grid
133+
// D: depth of the 3d grid
133134
//
134135
// Returns:
135-
// a pair of global vertex ID
136+
// hashing for a pair of vertex ids
136137
//
137-
std::pair<int, int> GetVPairFromEdge(const int edge, int W, int H) {
138+
int64_t HashVpair(const int edge, int W, int H, int D) {
138139
const int v1 = _EDGE_TO_VERTICES[edge][0];
139140
const int v2 = _EDGE_TO_VERTICES[edge][1];
140141
const int v1_id = p[v1].x + p[v1].y * W + p[v1].z * W * H;
141142
const int v2_id = p[v2].x + p[v2].y * W + p[v2].z * W * H;
142-
return std::make_pair(v1_id, v2_id);
143+
return (int64_t)v1_id * (W + W * H + W * H * D) + (int64_t)v2_id;
143144
}
144145
};
145-
146-
// helper functions for hashing
147-
namespace std {
148-
// Taken from boost library to combine several hash functions
149-
template <class T>
150-
inline void hash_combine(size_t& s, const T& v) {
151-
std::hash<T> h;
152-
s ^= h(v) + 0x9e3779b9 + (s << 6) + (s >> 2);
153-
}
154-
155-
// Function for hashing a pair of vertices
156-
template <>
157-
struct hash<std::pair<int, int>> {
158-
size_t operator()(const std::pair<int, int>& p) const {
159-
size_t res = 0;
160-
hash_combine(res, p.first);
161-
hash_combine(res, p.second);
162-
return res;
163-
}
164-
};
165-
166-
} // namespace std

0 commit comments

Comments
 (0)