Skip to content

Commit

Permalink
Use Miniball (https://github.com/hbf/miniball) in spUpdateBounds
Browse files Browse the repository at this point in the history
  • Loading branch information
fo76utils committed Apr 16, 2024
1 parent 96d31eb commit 30c38b7
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
15 changes: 14 additions & 1 deletion src/gl/gltools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -177,14 +178,26 @@ BoundSphere::BoundSphere( const NifModel * nif, const QModelIndex & index )
radius = nif->get<float>( idx, "Radius" );
}

BoundSphere::BoundSphere( const QVector<Vector3> & verts )
BoundSphere::BoundSphere( const QVector<Vector3> & verts, bool useMiniball )
{
if ( verts.isEmpty() ) {
center = Vector3();
radius = -1;
return;
}

if ( useMiniball && verts.size() > 2 ) {
SEB_NAMESPACE::Smallest_enclosing_ball<float, Vector3, QVector<Vector3>> 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
Expand Down
2 changes: 1 addition & 1 deletion src/gl/gltools.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vector3> & vertices );
BoundSphere( const QVector<Vector3> & vertices, bool useMiniball = false );

Vector3 center;
float radius;
Expand Down
4 changes: 2 additions & 2 deletions src/spells/mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 30c38b7

Please sign in to comment.