Skip to content

Commit 08caa1b

Browse files
committed
--cleanup
1 parent 8e7cee7 commit 08caa1b

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
@@ -1090,66 +1090,77 @@ void ResourceManager::computeGeneralMeshAreaAndVolume(
10901090
// Determine that all edges have exactly 2 sides ->
10911091
// idxAra describes exactly 2 pairs of the same idxs, a->b and b->a
10921092
std::unordered_map<uint64_t, int> edgeCount;
1093-
std::unordered_map<uint64_t, int> altEdgeCount;
1093+
std::unordered_map<uint64_t, int> revEdgeCount;
10941094
scene::DrawableMeshTopology meshTopology =
10951095
scene::DrawableMeshTopology::ClosedManifold;
10961096
for (uint32_t idx = 0; idx < numIdxs; idx += 3) {
1097+
// First edge index, encoded in 64bit
10971098
uint64_t vals[] = {uint64_t(idxAra[idx]), uint64_t(idxAra[idx + 1]),
10981099
uint64_t(idxAra[idx + 2])};
10991100
uint64_t shift_vals[] = {vals[0] << 32, vals[1] << 32, vals[2] << 32};
11001101
// for each edge in poly
11011102
for (uint32_t i = 0; i < 3; ++i) {
1103+
// Second edge index
11021104
uint32_t next_i = (i + 1) % 3;
1105+
// Encode directed edge vert idxs in single unsigned long
11031106
auto res =
11041107
edgeCount.emplace(std::make_pair(shift_vals[i] + vals[next_i], 0));
1108+
// Check if duplicate already exists - if so then non-manifold
11051109
if (!res.second) {
1110+
// Keep count of dupes
11061111
res.first->second += 1;
11071112
// Duplicate edge with same orientation
11081113
meshTopology = scene::DrawableMeshTopology::NonManifold;
11091114
}
1110-
// Alt edge placement - verify the alternate edge is present
1111-
altEdgeCount.emplace(std::make_pair(shift_vals[next_i] + vals[i], 0));
1115+
// Reverse edge placement - verify the reverse edge is present
1116+
revEdgeCount.emplace(std::make_pair(shift_vals[next_i] + vals[i], 0));
11121117
}
11131118
}
11141119
// If still closed manifold then check that every edge has an alt edge
11151120
// present
11161121
if (meshTopology == scene::DrawableMeshTopology::ClosedManifold) {
11171122
for (const auto entry : edgeCount) {
1118-
altEdgeCount.erase(entry.first);
1123+
revEdgeCount.erase(entry.first);
11191124
}
1120-
if (altEdgeCount.size() > 0) {
1125+
if (revEdgeCount.size() > 0) {
11211126
meshTopology = scene::DrawableMeshTopology::OpenManifold;
11221127
}
11231128
}
11241129

1125-
// Surface area of the mesh : .5 * ba.cross(bc)
1130+
// Surface area of the mesh M_a : sum(Tri_abc) ( |.5 * ba.cross(bc)|)
11261131
double ttlSurfaceArea = 0.0;
1127-
// Volume of the mesh : 1/6 * (OA.dot(ba.cross(bc)))
1132+
// Volume of the mesh M_v = sum(Tri_abc)( 1/3 (area_abc) h_O
1133+
// = sum(Tri_abc)(1/3 * (bO.dot(.5 * (ba.cross(bc)))))
11281134
// Where O is a distant vertex
11291135
// Only applicable on closed manifold meshes (i.e. all edges have exactly
11301136
// 2 faces)
11311137
double ttlVolume = 0.0f;
11321138
if (meshTopology == scene::DrawableMeshTopology::ClosedManifold) {
11331139
Mn::Vector3 origin{};
11341140
for (uint32_t idx = 0; idx < numIdxs; idx += 3) {
1135-
const auto aVert = posArray[idxAra[idx + 1]];
1136-
Mn::Vector3 aVec = posArray[idxAra[idx]] - aVert;
1137-
Mn::Vector3 bVec = posArray[idxAra[idx + 2]] - aVert;
1138-
Mn::Vector3 areaNormVec = 0.5 * Mn::Math::cross(aVec, bVec);
1139-
double surfArea = areaNormVec.length();
1141+
const auto bVert = posArray[idxAra[idx + 1]];
1142+
Mn::Vector3 baVec = posArray[idxAra[idx]] - bVert;
1143+
Mn::Vector3 bcVec = posArray[idxAra[idx + 2]] - bVert;
1144+
// Magnitude is 2x tri_abc area, direction is orthogonal to tri_abc
1145+
// ("height" dir)
1146+
Mn::Vector3 areaOrthoVec = 0.5 * Mn::Math::cross(baVec, bcVec);
1147+
double surfArea = areaOrthoVec.length();
11401148
ttlSurfaceArea += surfArea;
1141-
Mn::Vector3 c = origin - aVert;
1142-
double signedVol = (Mn::Math::dot(c, areaNormVec)) / 3.0;
1149+
Mn::Vector3 bO = origin - bVert;
1150+
// Project along "height" direction
1151+
double signedVol = (Mn::Math::dot(bO, areaOrthoVec)) / 3.0;
11431152
ttlVolume += signedVol;
11441153
}
11451154
} else {
11461155
// Open or non-manifold meshes won't have an accurate volume calc
11471156
for (uint32_t idx = 0; idx < numIdxs; idx += 3) {
1148-
const auto aVert = posArray[idxAra[idx + 1]];
1149-
Mn::Vector3 aVec = posArray[idxAra[idx]] - aVert;
1150-
Mn::Vector3 bVec = posArray[idxAra[idx + 2]] - aVert;
1151-
Mn::Vector3 areaNormVec = 0.5 * Mn::Math::cross(aVec, bVec);
1152-
double surfArea = areaNormVec.length();
1157+
const auto bVert = posArray[idxAra[idx + 1]];
1158+
Mn::Vector3 baVec = posArray[idxAra[idx]] - bVert;
1159+
Mn::Vector3 bcVec = posArray[idxAra[idx + 2]] - bVert;
1160+
// Magnitude is 2x tri_abc area, direction is orthogonal to tri_abc
1161+
// ("height" dir)
1162+
Mn::Vector3 areaOrthoVec = 0.5 * Mn::Math::cross(baVec, bcVec);
1163+
double surfArea = areaOrthoVec.length();
11531164
ttlSurfaceArea += surfArea;
11541165
}
11551166
}

0 commit comments

Comments
 (0)