Skip to content

Commit

Permalink
Replace map usage with vector
Browse files Browse the repository at this point in the history
  • Loading branch information
stephomi committed Jul 11, 2023
1 parent 96c57c0 commit 8e35908
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 27 deletions.
34 changes: 21 additions & 13 deletions src/manifold/src/boolean_result.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,9 @@ 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());
outR.meshRelation_.properties.reserve(outR.NumVert() * numProp * 1.1);
int idx = 0;

for (int tri = 0; tri < numTri; ++tri) {
Expand All @@ -499,16 +501,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 +519,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
36 changes: 22 additions & 14 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,35 +283,44 @@ 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);
int accM = 0;
out.mergeFromVert.resize(numVert - impl.NumVert());
out.mergeToVert.resize(numVert - impl.NumVert());
int accP = 0;
out.vertProperties.resize(numVert * out.numProp);

for (int run = 0; run < out.runOriginalID.size(); ++run) {
for (int tri = out.runIndex[run] / 3; tri < out.runIndex[run + 1] / 3;
++tri) {
const glm::ivec3 triProp =
impl.meshRelation_.triProperties[triNew2Old[tri]];

for (const int i : {0, 1, 2}) {
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});
const int vert = out.triVerts[3 * tri + i];
const int idx = accP / out.numProp;
out.triVerts[3 * tri + i] = idx;
vertPropPair[prop] = idx;

for (int p : {0, 1, 2}) {
out.vertProperties.push_back(impl.vertPos_[vert][p]);
out.vertProperties[accP + p] = impl.vertPos_[vert][p];
}
accP += 3;
for (int p = 0; p < numProp; ++p) {
out.vertProperties.push_back(
impl.meshRelation_.properties[prop * numProp + p]);
out.vertProperties[accP + p] =
impl.meshRelation_.properties[prop * numProp + p];
}
accP += numProp;

if (updateNormals) {
glm::vec3 normal;
const int start = out.vertProperties.size() - out.numProp;
const int start = accP - out.numProp;
for (int i : {0, 1, 2}) {
normal[i] = out.vertProperties[start + normalIdx[i]];
}
Expand All @@ -325,8 +333,8 @@ MeshGL Manifold::GetMeshGL(glm::ivec3 normalIdx) const {
if (vert2idx[vert] == -1) {
vert2idx[vert] = idx;
} else {
out.mergeFromVert.push_back(idx);
out.mergeToVert.push_back(vert2idx[vert]);
out.mergeFromVert[accM] = idx;
out.mergeToVert[accM++] = vert2idx[vert];
}
}
}
Expand Down

0 comments on commit 8e35908

Please sign in to comment.