Skip to content

Commit c0642b6

Browse files
committed
--cleanup
1 parent bf22d2c commit c0642b6

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
@@ -1088,66 +1088,77 @@ void ResourceManager::computeGeneralMeshAreaAndVolume(
10881088
// Determine that all edges have exactly 2 sides ->
10891089
// idxAra describes exactly 2 pairs of the same idxs, a->b and b->a
10901090
std::unordered_map<uint64_t, int> edgeCount;
1091-
std::unordered_map<uint64_t, int> altEdgeCount;
1091+
std::unordered_map<uint64_t, int> revEdgeCount;
10921092
scene::DrawableMeshTopology meshTopology =
10931093
scene::DrawableMeshTopology::ClosedManifold;
10941094
for (uint32_t idx = 0; idx < numIdxs; idx += 3) {
1095+
// First edge index, encoded in 64bit
10951096
uint64_t vals[] = {uint64_t(idxAra[idx]), uint64_t(idxAra[idx + 1]),
10961097
uint64_t(idxAra[idx + 2])};
10971098
uint64_t shift_vals[] = {vals[0] << 32, vals[1] << 32, vals[2] << 32};
10981099
// for each edge in poly
10991100
for (uint32_t i = 0; i < 3; ++i) {
1101+
// Second edge index
11001102
uint32_t next_i = (i + 1) % 3;
1103+
// Encode directed edge vert idxs in single unsigned long
11011104
auto res =
11021105
edgeCount.emplace(std::make_pair(shift_vals[i] + vals[next_i], 0));
1106+
// Check if duplicate already exists - if so then non-manifold
11031107
if (!res.second) {
1108+
// Keep count of dupes
11041109
res.first->second += 1;
11051110
// Duplicate edge with same orientation
11061111
meshTopology = scene::DrawableMeshTopology::NonManifold;
11071112
}
1108-
// Alt edge placement - verify the alternate edge is present
1109-
altEdgeCount.emplace(std::make_pair(shift_vals[next_i] + vals[i], 0));
1113+
// Reverse edge placement - verify the reverse edge is present
1114+
revEdgeCount.emplace(std::make_pair(shift_vals[next_i] + vals[i], 0));
11101115
}
11111116
}
11121117
// If still closed manifold then check that every edge has an alt edge
11131118
// present
11141119
if (meshTopology == scene::DrawableMeshTopology::ClosedManifold) {
11151120
for (const auto entry : edgeCount) {
1116-
altEdgeCount.erase(entry.first);
1121+
revEdgeCount.erase(entry.first);
11171122
}
1118-
if (altEdgeCount.size() > 0) {
1123+
if (revEdgeCount.size() > 0) {
11191124
meshTopology = scene::DrawableMeshTopology::OpenManifold;
11201125
}
11211126
}
11221127

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

0 commit comments

Comments
 (0)