Skip to content

Commit

Permalink
Replace map with unordered_map
Browse files Browse the repository at this point in the history
  • Loading branch information
stephomi committed Jul 11, 2023
1 parent 96c57c0 commit 930925e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
33 changes: 20 additions & 13 deletions src/manifold/src/boolean_result.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,8 @@ void CreateProperties(Manifold::Impl &outR, const VecDH<TriRef> &refPQ,
inP.halfedge_.cptrD(), inQ.halfedge_.cptrD(),
outR.halfedge_.cptrD(), outR.precision_}));

std::map<std::tuple<int, int, int, int>, int> propIdx;
using Entry = std::pair<glm::ivec3, int>;
std::vector<std::vector<Entry>> propIdx(outR.NumVert());
int idx = 0;

for (int tri = 0; tri < numTri; ++tri) {
Expand All @@ -499,16 +500,15 @@ void CreateProperties(Manifold::Impl &outR, const VecDH<TriRef> &refPQ,

for (const int i : {0, 1, 2}) {
const int vert = outR.halfedge_[3 * tri + i].startVert;
const glm::vec3 uvw = bary[3 * tri + i];
const glm::vec3 &uvw = bary[3 * tri + i];

auto key = std::make_tuple(PQ, -1, -1, -1);
glm::ivec4 key(PQ, vert, -1, -1);
if (oldNumProp > 0) {
std::get<1>(key) = vert;
int edge = -1;
for (const int j : {0, 1, 2}) {
if (uvw[j] == 1) {
// On a retained vert, the propVert must also match
std::get<2>(key) = triProp[j];
key[2] = triProp[j];
edge = -1;
break;
}
Expand All @@ -518,18 +518,25 @@ void CreateProperties(Manifold::Impl &outR, const VecDH<TriRef> &refPQ,
// On an edge, both propVerts must match
const int p0 = triProp[Next3(edge)];
const int p1 = triProp[Prev3(edge)];
std::get<2>(key) = glm::min(p0, p1);
std::get<3>(key) = glm::max(p0, p1);
key[2] = glm::min(p0, p1);
key[3] = glm::max(p0, p1);
}
} else {
key[2] = -2;
}

const auto it = propIdx.find(key);
if (it != propIdx.end()) {
outR.meshRelation_.triProperties[tri][i] = it->second;
continue;
auto &bin = propIdx[key.y];
bool bFound = false;
for (int k = 0; k < bin.size(); ++k) {
if (bin[k].first == glm::ivec3(key.x, key.z, key.w)) {
bFound = true;
outR.meshRelation_.triProperties[tri][i] = bin[k].second;
break;
}
}
outR.meshRelation_.triProperties[tri][i] = idx;
propIdx.insert({key, idx++});
if (bFound) continue;
bin.push_back(std::make_pair(glm::ivec3(key.x, key.z, key.w), idx));
outR.meshRelation_.triProperties[tri][i] = idx++;

for (int p = 0; p < numProp; ++p) {
if (p < oldNumProp) {
Expand Down
10 changes: 4 additions & 6 deletions src/manifold/src/manifold.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
// limitations under the License.

#include <algorithm>
#include <map>
#include <numeric>

#include "boolean3.h"
Expand Down Expand Up @@ -284,7 +283,7 @@ MeshGL Manifold::GetMeshGL(glm::ivec3 normalIdx) const {

// Duplicate verts with different props
std::vector<int> vert2idx(impl.NumVert(), -1);
std::map<std::pair<int, int>, int> vertPropPair;
std::vector<int> vertPropPair(numVert, -1);
for (int run = 0; run < out.runOriginalID.size(); ++run) {
for (int tri = out.runIndex[run] / 3; tri < out.runIndex[run + 1] / 3;
++tri) {
Expand All @@ -294,14 +293,13 @@ MeshGL Manifold::GetMeshGL(glm::ivec3 normalIdx) const {
const int prop = triProp[i];
const int vert = out.triVerts[3 * tri + i];

const auto it = vertPropPair.find({vert, prop});
if (it != vertPropPair.end()) {
out.triVerts[3 * tri + i] = it->second;
if (vertPropPair[prop] >= 0) {
out.triVerts[3 * tri + i] = vertPropPair[prop];
continue;
}
const int idx = out.vertProperties.size() / out.numProp;
vertPropPair.insert({{vert, prop}, idx});
out.triVerts[3 * tri + i] = idx;
vertPropPair[prop] = idx;

for (int p : {0, 1, 2}) {
out.vertProperties.push_back(impl.vertPos_[vert][p]);
Expand Down

0 comments on commit 930925e

Please sign in to comment.