Kelsidavis-WoWee/tools/editor/cli_weld.cpp
Kelsi 869124fa96 refactor(editor): extract vertex weld into shared cli_weld utility
Three callers were each open-coding the same quantize-and-
bucket pass over vertex positions: --info-mesh-stats,
--info-wob-stats, and --bake-wom-collision. Move the
implementation to cli_weld.{hpp,cpp} as buildWeldMap() and
have each caller pass a flat positions array.

Identical std::map-based exact-equality keying preserved
(unbalanced-hash collisions remain absent). All three call
sites verified to produce byte-identical output:

  • info-mesh-stats firepit: 240→80 verts, 0 boundary
  • info-wob-stats cube:     8→8 verts, watertight YES
  • bake-wom-collision tent: 18→6 verts, 8-tri WOC

About 75 lines of duplication removed; future weld-using
commands now opt in by including one header.
2026-05-09 11:05:54 -07:00

39 lines
1.1 KiB
C++

#include "cli_weld.hpp"
#include <algorithm>
#include <cmath>
#include <map>
#include <tuple>
namespace wowee {
namespace editor {
namespace cli {
std::vector<uint32_t> buildWeldMap(
const std::vector<glm::vec3>& positions,
float eps,
std::size_t& uniqueOut) {
const float invEps = 1.0f / std::max(eps, 1e-9f);
using QKey = std::tuple<int64_t, int64_t, int64_t>;
std::map<QKey, uint32_t> bucket;
std::vector<uint32_t> canon(positions.size());
for (std::size_t v = 0; v < positions.size(); ++v) {
const auto& p = positions[v];
QKey k{static_cast<int64_t>(std::lround(p.x * invEps)),
static_cast<int64_t>(std::lround(p.y * invEps)),
static_cast<int64_t>(std::lround(p.z * invEps))};
auto it = bucket.find(k);
if (it == bucket.end()) {
bucket.emplace(k, static_cast<uint32_t>(v));
canon[v] = static_cast<uint32_t>(v);
} else {
canon[v] = it->second;
}
}
uniqueOut = bucket.size();
return canon;
}
} // namespace cli
} // namespace editor
} // namespace wowee