Skip to content

Commit 2cc9882

Browse files
committed
--cleanup
1 parent 0388256 commit 2cc9882

File tree

1 file changed

+30
-19
lines changed

1 file changed

+30
-19
lines changed

Diff for: src/esp/assets/ResourceManager.cpp

+30-19
Original file line numberDiff line numberDiff line change
@@ -1082,66 +1082,77 @@ void ResourceManager::computeGeneralMeshAreaAndVolume(
10821082
// Determine that all edges have exactly 2 sides ->
10831083
// idxAra describes exactly 2 pairs of the same idxs, a->b and b->a
10841084
std::unordered_map<uint64_t, int> edgeCount;
1085-
std::unordered_map<uint64_t, int> altEdgeCount;
1085+
std::unordered_map<uint64_t, int> revEdgeCount;
10861086
scene::DrawableMeshTopology meshTopology =
10871087
scene::DrawableMeshTopology::ClosedManifold;
10881088
for (uint32_t idx = 0; idx < numIdxs; idx += 3) {
1089+
// First edge index, encoded in 64bit
10891090
uint64_t vals[] = {uint64_t(idxAra[idx]), uint64_t(idxAra[idx + 1]),
10901091
uint64_t(idxAra[idx + 2])};
10911092
uint64_t shift_vals[] = {vals[0] << 32, vals[1] << 32, vals[2] << 32};
10921093
// for each edge in poly
10931094
for (uint32_t i = 0; i < 3; ++i) {
1095+
// Second edge index
10941096
uint32_t next_i = (i + 1) % 3;
1097+
// Encode directed edge vert idxs in single unsigned long
10951098
auto res =
10961099
edgeCount.emplace(std::make_pair(shift_vals[i] + vals[next_i], 0));
1100+
// Check if duplicate already exists - if so then non-manifold
10971101
if (!res.second) {
1102+
// Keep count of dupes
10981103
res.first->second += 1;
10991104
// Duplicate edge with same orientation
11001105
meshTopology = scene::DrawableMeshTopology::NonManifold;
11011106
}
1102-
// Alt edge placement - verify the alternate edge is present
1103-
altEdgeCount.emplace(std::make_pair(shift_vals[next_i] + vals[i], 0));
1107+
// Reverse edge placement - verify the reverse edge is present
1108+
revEdgeCount.emplace(std::make_pair(shift_vals[next_i] + vals[i], 0));
11041109
}
11051110
}
11061111
// If still closed manifold then check that every edge has an alt edge
11071112
// present
11081113
if (meshTopology == scene::DrawableMeshTopology::ClosedManifold) {
11091114
for (const auto entry : edgeCount) {
1110-
altEdgeCount.erase(entry.first);
1115+
revEdgeCount.erase(entry.first);
11111116
}
1112-
if (altEdgeCount.size() > 0) {
1117+
if (revEdgeCount.size() > 0) {
11131118
meshTopology = scene::DrawableMeshTopology::OpenManifold;
11141119
}
11151120
}
11161121

1117-
// Surface area of the mesh : .5 * ba.cross(bc)
1122+
// Surface area of the mesh M_a : sum(Tri_abc) ( |.5 * ba.cross(bc)|)
11181123
double ttlSurfaceArea = 0.0;
1119-
// Volume of the mesh : 1/6 * (OA.dot(ba.cross(bc)))
1124+
// Volume of the mesh M_v = sum(Tri_abc)( 1/3 (area_abc) h_O
1125+
// = sum(Tri_abc)(1/3 * (bO.dot(.5 * (ba.cross(bc)))))
11201126
// Where O is a distant vertex
11211127
// Only applicable on closed manifold meshes (i.e. all edges have exactly
11221128
// 2 faces)
11231129
double ttlVolume = 0.0f;
11241130
if (meshTopology == scene::DrawableMeshTopology::ClosedManifold) {
11251131
Mn::Vector3 origin{};
11261132
for (uint32_t idx = 0; idx < numIdxs; idx += 3) {
1127-
const auto aVert = posArray[idxAra[idx + 1]];
1128-
Mn::Vector3 aVec = posArray[idxAra[idx]] - aVert;
1129-
Mn::Vector3 bVec = posArray[idxAra[idx + 2]] - aVert;
1130-
Mn::Vector3 areaNormVec = 0.5 * Mn::Math::cross(aVec, bVec);
1131-
double surfArea = areaNormVec.length();
1133+
const auto bVert = posArray[idxAra[idx + 1]];
1134+
Mn::Vector3 baVec = posArray[idxAra[idx]] - bVert;
1135+
Mn::Vector3 bcVec = posArray[idxAra[idx + 2]] - bVert;
1136+
// Magnitude is 2x tri_abc area, direction is orthogonal to tri_abc
1137+
// ("height" dir)
1138+
Mn::Vector3 areaOrthoVec = 0.5 * Mn::Math::cross(baVec, bcVec);
1139+
double surfArea = areaOrthoVec.length();
11321140
ttlSurfaceArea += surfArea;
1133-
Mn::Vector3 c = origin - aVert;
1134-
double signedVol = (Mn::Math::dot(c, areaNormVec)) / 3.0;
1141+
Mn::Vector3 bO = origin - bVert;
1142+
// Project along "height" direction
1143+
double signedVol = (Mn::Math::dot(bO, areaOrthoVec)) / 3.0;
11351144
ttlVolume += signedVol;
11361145
}
11371146
} else {
11381147
// Open or non-manifold meshes won't have an accurate volume calc
11391148
for (uint32_t idx = 0; idx < numIdxs; idx += 3) {
1140-
const auto aVert = posArray[idxAra[idx + 1]];
1141-
Mn::Vector3 aVec = posArray[idxAra[idx]] - aVert;
1142-
Mn::Vector3 bVec = posArray[idxAra[idx + 2]] - aVert;
1143-
Mn::Vector3 areaNormVec = 0.5 * Mn::Math::cross(aVec, bVec);
1144-
double surfArea = areaNormVec.length();
1149+
const auto bVert = posArray[idxAra[idx + 1]];
1150+
Mn::Vector3 baVec = posArray[idxAra[idx]] - bVert;
1151+
Mn::Vector3 bcVec = posArray[idxAra[idx + 2]] - bVert;
1152+
// Magnitude is 2x tri_abc area, direction is orthogonal to tri_abc
1153+
// ("height" dir)
1154+
Mn::Vector3 areaOrthoVec = 0.5 * Mn::Math::cross(baVec, bcVec);
1155+
double surfArea = areaOrthoVec.length();
11451156
ttlSurfaceArea += surfArea;
11461157
}
11471158
}

0 commit comments

Comments
 (0)