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

esm4-texture
elsid 1 year ago
parent d71b422615
commit ac891a5eb1
No known key found for this signature in database
GPG Key ID: 4DE04C198CBA7625

@ -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
namespace MWWorld
{
template <class Contained>
bool contains(const std::vector<MWWorld::PositionCellGrid>& container, const Contained& contained, float tolerance)
namespace
{
for (const auto& pos : contained)
bool contains(std::span<const PositionCellGrid> positions, const PositionCellGrid& contained, float tolerance)
{
bool found = false;
for (const auto& pos2 : container)
{
if ((pos.mPosition - pos2.mPosition).length2() < tolerance * tolerance
&& pos.mCellBounds == pos2.mCellBounds)
{
found = true;
break;
}
}
if (!found)
return false;
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);
}
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);
}
return true;
}
}
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)

Loading…
Cancel
Save