From 2d242fcab60b3a28921b66f3f13482f4eba61c39 Mon Sep 17 00:00:00 2001 From: Eric Wasylishen Date: Sat, 6 Feb 2021 17:04:34 -0700 Subject: [PATCH] decompile: pick best split --- bsputil/decompile.cpp | 43 ++++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/bsputil/decompile.cpp b/bsputil/decompile.cpp index 0e9d556a..10e9ecd3 100644 --- a/bsputil/decompile.cpp +++ b/bsputil/decompile.cpp @@ -540,40 +540,41 @@ SideNeedsSplitting(const mbsp_t *bsp, const decomp_brush_side_t& side) return false; } -static bool -IsValidSplit(const mbsp_t *bsp, const decomp_brush_side_t& side, const qvec4f& split) -{ - auto [front, back] = side.clipToPlane(qvec3d(split.xyz()), split[3]); - - if (!front.faces.empty() && !back.faces.empty()) { - return true; - } - - return false; -} - static qvec4f SuggestSplit(const mbsp_t *bsp, const decomp_brush_side_t& side) { assert(SideNeedsSplitting(bsp, side)); + size_t bestFaceCount = SIZE_MAX; + qvec4f bestPlane; + // for all possible splits: for (const auto& face : side.faces) { - for (const qvec4f& inwardFacingEdgePlane : face.inwardFacingEdgePlanes) { + for (const qvec4f& split : face.inwardFacingEdgePlanes) { // this is a potential splitting plane. - printf("trying split.."); - if (IsValidSplit(bsp, side, inwardFacingEdgePlane)) { - printf("good\n"); - return inwardFacingEdgePlane; + auto [front, back] = side.clipToPlane(qvec3d(split.xyz()), split[3]); + + // we only consider splits that have at least 1 face on the front and back + if (front.faces.empty()) { + continue; + } + if (back.faces.empty()) { + continue; + } + + const size_t totalFaceCountWithThisSplit = + front.faces.size() + back.faces.size(); + + if (totalFaceCountWithThisSplit < bestFaceCount) { + bestFaceCount = totalFaceCountWithThisSplit; + bestPlane = split; } - printf("bad\n"); } } - // should never happen - assert(0); - return {}; + assert(bestPlane != qvec4f()); + return bestPlane; } static void