1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-01-16 16:29:55 +00:00

Use algorithms to check if PositionCellGrid(s) is/are contained in a range

This commit is contained in:
elsid 2023-10-29 13:31:03 +01:00
parent d71b422615
commit ac891a5eb1
No known key found for this signature in database
GPG key ID: 4DE04C198CBA7625

View file

@ -1,8 +1,9 @@
#include "cellpreloader.hpp"
#include <array>
#include <algorithm>
#include <atomic>
#include <limits>
#include <span>
#include <osg/Stats>
@ -26,32 +27,28 @@
#include "cellstore.hpp"
#include "class.hpp"
namespace MWWorld
{
namespace
{
template <class Contained>
bool contains(const std::vector<MWWorld::PositionCellGrid>& container, const Contained& contained, float tolerance)
bool contains(std::span<const PositionCellGrid> positions, const PositionCellGrid& contained, float tolerance)
{
for (const auto& pos : contained)
{
bool found = false;
for (const auto& pos2 : container)
{
if ((pos.mPosition - pos2.mPosition).length2() < tolerance * tolerance
&& pos.mCellBounds == pos2.mCellBounds)
{
found = true;
break;
const float squaredTolerance = tolerance * tolerance;
const auto predicate = [&](const PositionCellGrid& v) {
return (contained.mPosition - v.mPosition).length2() < squaredTolerance
&& contained.mCellBounds == v.mCellBounds;
};
return std::ranges::any_of(positions, predicate);
}
}
if (!found)
return false;
}
return true;
bool contains(
std::span<const PositionCellGrid> container, std::span<const PositionCellGrid> contained, float tolerance)
{
const auto predicate = [&](const PositionCellGrid& v) { return contains(container, v, tolerance); };
return std::ranges::all_of(contained, predicate);
}
}
namespace MWWorld
{
struct ListModelsVisitor
{
bool operator()(const MWWorld::ConstPtr& ptr)
@ -376,7 +373,7 @@ namespace MWWorld
void CellPreloader::abortTerrainPreloadExcept(const PositionCellGrid* exceptPos)
{
if (exceptPos && contains(mTerrainPreloadPositions, std::array{ *exceptPos }, Constants::CellSizeInUnits))
if (exceptPos != nullptr && contains(mTerrainPreloadPositions, *exceptPos, Constants::CellSizeInUnits))
return;
if (mTerrainPreloadItem && !mTerrainPreloadItem->isDone())
{
@ -419,7 +416,7 @@ namespace MWWorld
bool CellPreloader::isTerrainLoaded(const PositionCellGrid& position, double referenceTime) const
{
return mLoadedTerrainTimestamp + mResourceSystem->getSceneManager()->getExpiryDelay() > referenceTime
&& contains(mLoadedTerrainPositions, std::array{ position }, Constants::CellSizeInUnits);
&& contains(mLoadedTerrainPositions, position, Constants::CellSizeInUnits);
}
void CellPreloader::setTerrain(Terrain::World* terrain)