From 30c38b7916f950ffd68b0605646021215adf5258 Mon Sep 17 00:00:00 2001 From: fo76utils <87907510+fo76utils@users.noreply.github.com> Date: Tue, 16 Apr 2024 18:56:27 +0200 Subject: [PATCH] Use Miniball (https://github.com/hbf/miniball) in spUpdateBounds --- src/gl/gltools.cpp | 15 ++++++++++++++- src/gl/gltools.h | 2 +- src/spells/mesh.cpp | 4 ++-- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/gl/gltools.cpp b/src/gl/gltools.cpp index 47a9bda0..8d3f0b5d 100644 --- a/src/gl/gltools.cpp +++ b/src/gl/gltools.cpp @@ -45,6 +45,7 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "libfo76utils/src/fp32vec4.hpp" #include "gamemanager.h" +#include "miniball/Seb.h" //! \file gltools.cpp GL helper functions @@ -177,7 +178,7 @@ BoundSphere::BoundSphere( const NifModel * nif, const QModelIndex & index ) radius = nif->get( idx, "Radius" ); } -BoundSphere::BoundSphere( const QVector & verts ) +BoundSphere::BoundSphere( const QVector & verts, bool useMiniball ) { if ( verts.isEmpty() ) { center = Vector3(); @@ -185,6 +186,18 @@ BoundSphere::BoundSphere( const QVector & verts ) return; } + if ( useMiniball && verts.size() > 2 ) { + SEB_NAMESPACE::Smallest_enclosing_ball> mb( 3, verts ); + auto i = mb.center_begin(); + center[0] = *i; + i++; + center[1] = *i; + i++; + center[2] = *i; + radius = mb.radius(); + return; + } + // old algorithm: center of bounding sphere = bounds1 = centroid of verts FloatVector4 bounds1( 0.0f ); // p1 and p2 are searched for Ritter's algorithm diff --git a/src/gl/gltools.h b/src/gl/gltools.h index 69c8df9c..05e40f7f 100644 --- a/src/gl/gltools.h +++ b/src/gl/gltools.h @@ -56,7 +56,7 @@ class BoundSphere final BoundSphere( const BoundSphere & ); BoundSphere( const NifModel * nif, const QModelIndex & ); BoundSphere( const Vector3 & center, float radius ); - BoundSphere( const QVector & vertices ); + BoundSphere( const QVector & vertices, bool useMiniball = false ); Vector3 center; float radius; diff --git a/src/spells/mesh.cpp b/src/spells/mesh.cpp index 6280100a..2155ea46 100644 --- a/src/spells/mesh.cpp +++ b/src/spells/mesh.cpp @@ -731,7 +731,7 @@ class spUpdateBounds final : public Spell return index; // Creating a bounding sphere from the verts - BoundSphere bounds = BoundSphere( verts ); + BoundSphere bounds = BoundSphere( verts, true ); bounds.update( nif, index ); if ( nif->getBSVersion() >= 151 ) { @@ -780,7 +780,7 @@ QModelIndex spUpdateBounds::cast_Starfield( NifModel * nif, const QModelIndex & // FIXME: mesh flags are not updated if ( meshFile.isValid() && meshFile.positions.size() > 0 && !boundsCalculated ) { // Creating a bounding sphere and bounding box from the verts - bounds = BoundSphere( meshFile.positions ); + bounds = BoundSphere( meshFile.positions, true ); calculateBoundingBox( bndCenter, bndDims, meshFile.positions ); boundsCalculated = true; }