Skip to content

Commit

Permalink
Merge branch 'concepts' into 'master'
Browse files Browse the repository at this point in the history
Use concepts for some argument types

See merge request OpenMW/openmw!3598
  • Loading branch information
Assumeru committed Nov 18, 2023
2 parents 2150e4c + b17afc4 commit 63875a7
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 54 deletions.
40 changes: 14 additions & 26 deletions apps/benchmarks/detournavigator/navmeshtilescache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <components/esm3/loadland.hpp>

#include <algorithm>
#include <iterator>
#include <random>

namespace
Expand All @@ -24,29 +25,25 @@ namespace
PreparedNavMeshData mValue;
};

template <typename Random>
osg::Vec2i generateVec2i(int max, Random& random)
osg::Vec2i generateVec2i(int max, auto& random)
{
std::uniform_int_distribution<int> distribution(0, max);
return osg::Vec2i(distribution(random), distribution(random));
}

template <typename Random>
osg::Vec3f generateAgentHalfExtents(float min, float max, Random& random)
osg::Vec3f generateAgentHalfExtents(float min, float max, auto& random)
{
std::uniform_int_distribution<int> distribution(min, max);
return osg::Vec3f(distribution(random), distribution(random), distribution(random));
}

template <typename OutputIterator, typename Random>
void generateVertices(OutputIterator out, std::size_t number, Random& random)
void generateVertices(std::output_iterator<int> auto out, std::size_t number, auto& random)
{
std::uniform_real_distribution<float> distribution(0.0, 1.0);
std::generate_n(out, 3 * (number - number % 3), [&] { return distribution(random); });
}

template <typename OutputIterator, typename Random>
void generateIndices(OutputIterator out, int max, std::size_t number, Random& random)
void generateIndices(std::output_iterator<int> auto out, int max, std::size_t number, auto& random)
{
std::uniform_int_distribution<int> distribution(0, max);
std::generate_n(out, number - number % 3, [&] { return distribution(random); });
Expand All @@ -70,30 +67,26 @@ namespace
return AreaType_null;
}

template <typename Random>
AreaType generateAreaType(Random& random)
AreaType generateAreaType(auto& random)
{
std::uniform_int_distribution<int> distribution(0, 4);
return toAreaType(distribution(random));
}

template <typename OutputIterator, typename Random>
void generateAreaTypes(OutputIterator out, std::size_t triangles, Random& random)
void generateAreaTypes(std::output_iterator<AreaType> auto out, std::size_t triangles, auto& random)
{
std::generate_n(out, triangles, [&] { return generateAreaType(random); });
}

template <typename OutputIterator, typename Random>
void generateWater(OutputIterator out, std::size_t count, Random& random)
void generateWater(std::output_iterator<CellWater> auto out, std::size_t count, auto& random)
{
std::uniform_real_distribution<float> distribution(0.0, 1.0);
std::generate_n(out, count, [&] {
return CellWater{ generateVec2i(1000, random), Water{ ESM::Land::REAL_SIZE, distribution(random) } };
});
}

template <class Random>
Mesh generateMesh(std::size_t triangles, Random& random)
Mesh generateMesh(std::size_t triangles, auto& random)
{
std::uniform_real_distribution<float> distribution(0.0, 1.0);
std::vector<float> vertices;
Expand All @@ -109,8 +102,7 @@ namespace
return Mesh(std::move(indices), std::move(vertices), std::move(areaTypes));
}

template <class Random>
Heightfield generateHeightfield(Random& random)
Heightfield generateHeightfield(auto& random)
{
std::uniform_real_distribution<float> distribution(0.0, 1.0);
Heightfield result;
Expand All @@ -127,8 +119,7 @@ namespace
return result;
}

template <class Random>
FlatHeightfield generateFlatHeightfield(Random& random)
FlatHeightfield generateFlatHeightfield(auto& random)
{
std::uniform_real_distribution<float> distribution(0.0, 1.0);
FlatHeightfield result;
Expand All @@ -138,8 +129,7 @@ namespace
return result;
}

template <class Random>
Key generateKey(std::size_t triangles, Random& random)
Key generateKey(std::size_t triangles, auto& random)
{
const CollisionShapeType agentShapeType = CollisionShapeType::Aabb;
const osg::Vec3f agentHalfExtents = generateAgentHalfExtents(0.5, 1.5, random);
Expand All @@ -158,14 +148,12 @@ namespace

constexpr std::size_t trianglesPerTile = 239;

template <typename OutputIterator, typename Random>
void generateKeys(OutputIterator out, std::size_t count, Random& random)
void generateKeys(std::output_iterator<Key> auto out, std::size_t count, auto& random)
{
std::generate_n(out, count, [&] { return generateKey(trianglesPerTile, random); });
}

template <typename OutputIterator, typename Random>
void fillCache(OutputIterator out, Random& random, NavMeshTilesCache& cache)
void fillCache(std::output_iterator<Key> auto out, auto& random, NavMeshTilesCache& cache)
{
std::size_t size = cache.getStats().mNavMeshCacheSize;

Expand Down
40 changes: 19 additions & 21 deletions components/detournavigator/findsmoothpath.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,54 +16,54 @@
#include <array>
#include <cassert>
#include <functional>
#include <iterator>
#include <span>
#include <vector>

namespace DetourNavigator
{
template <class OutputIterator, class Function>
class OutputTransformIterator
template <std::output_iterator<osg::Vec3f> OutputIterator>
class FromNavMeshCoordinatesIterator
{
public:
explicit OutputTransformIterator(OutputIterator& impl, Function&& function)
using iterator_category = std::output_iterator_tag;
using value_type = osg::Vec3f;
using difference_type = std::ptrdiff_t;
using pointer = osg::Vec3f*;
using reference = osg::Vec3f&;

explicit FromNavMeshCoordinatesIterator(OutputIterator& impl, const RecastSettings& settings)
: mImpl(impl)
, mFunction(std::forward<Function>(function))
, mSettings(settings)
{
}

OutputTransformIterator& operator*() { return *this; }
FromNavMeshCoordinatesIterator& operator*() { return *this; }

OutputTransformIterator& operator++()
FromNavMeshCoordinatesIterator& operator++()
{
++mImpl.get();
return *this;
}

OutputTransformIterator operator++(int)
FromNavMeshCoordinatesIterator operator++(int)
{
const auto copy = *this;
++(*this);
return copy;
}

OutputTransformIterator& operator=(const osg::Vec3f& value)
FromNavMeshCoordinatesIterator& operator=(const osg::Vec3f& value)
{
*mImpl.get() = mFunction(value);
*mImpl.get() = fromNavMeshCoordinates(mSettings, value);
return *this;
}

private:
std::reference_wrapper<OutputIterator> mImpl;
Function mFunction;
std::reference_wrapper<const RecastSettings> mSettings;
};

template <class OutputIterator>
auto withFromNavMeshCoordinates(OutputIterator& impl, const RecastSettings& settings)
{
return OutputTransformIterator(
impl, [&settings](const osg::Vec3f& value) { return fromNavMeshCoordinates(settings, value); });
}

inline std::optional<std::size_t> findPath(const dtNavMeshQuery& navMeshQuery, const dtPolyRef startRef,
const dtPolyRef endRef, const osg::Vec3f& startPos, const osg::Vec3f& endPos, const dtQueryFilter& queryFilter,
std::span<dtPolyRef> pathBuffer)
Expand All @@ -78,10 +78,9 @@ namespace DetourNavigator
return static_cast<std::size_t>(pathLen);
}

template <class OutputIterator>
Status makeSmoothPath(const dtNavMeshQuery& navMeshQuery, const osg::Vec3f& start, const osg::Vec3f& end,
std::span<dtPolyRef> polygonPath, std::size_t polygonPathSize, std::size_t maxSmoothPathSize,
OutputIterator& out)
std::output_iterator<osg::Vec3f> auto& out)
{
assert(polygonPathSize <= polygonPath.size());

Expand All @@ -102,10 +101,9 @@ namespace DetourNavigator
return Status::Success;
}

template <class OutputIterator>
Status findSmoothPath(const dtNavMeshQuery& navMeshQuery, const osg::Vec3f& halfExtents, const osg::Vec3f& start,
const osg::Vec3f& end, const Flags includeFlags, const AreaCosts& areaCosts, const DetourSettings& settings,
float endTolerance, OutputIterator out)
float endTolerance, std::output_iterator<osg::Vec3f> auto out)
{
dtQueryFilter queryFilter;
queryFilter.setIncludeFlags(includeFlags);
Expand Down
11 changes: 4 additions & 7 deletions components/detournavigator/navigatorutils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <components/misc/guarded.hpp>

#include <iterator>
#include <optional>

namespace DetourNavigator
Expand All @@ -21,22 +22,18 @@ namespace DetourNavigator
* @param includeFlags setup allowed surfaces for actor to walk.
* @param out the beginning of the destination range.
* @param endTolerance defines maximum allowed distance to end path point in addition to agentHalfExtents
* @return Output iterator to the element in the destination range, one past the last element of found path.
* @return Status.
* Equal to out if no path is found.
*/
template <class OutputIterator>
inline Status findPath(const Navigator& navigator, const AgentBounds& agentBounds, const osg::Vec3f& start,
const osg::Vec3f& end, const Flags includeFlags, const AreaCosts& areaCosts, float endTolerance,
OutputIterator out)
std::output_iterator<osg::Vec3f> auto out)
{
static_assert(std::is_same<typename std::iterator_traits<OutputIterator>::iterator_category,
std::output_iterator_tag>::value,
"out is not an OutputIterator");
const auto navMesh = navigator.getNavMesh(agentBounds);
if (navMesh == nullptr)
return Status::NavMeshNotFound;
const Settings& settings = navigator.getSettings();
auto outTransform = withFromNavMeshCoordinates(out, settings.mRecast);
FromNavMeshCoordinatesIterator outTransform(out, settings.mRecast);
const auto locked = navMesh->lock();
return findSmoothPath(locked->getQuery(), toNavMeshCoordinates(settings.mRecast, agentBounds.mHalfExtents),
toNavMeshCoordinates(settings.mRecast, start), toNavMeshCoordinates(settings.mRecast, end), includeFlags,
Expand Down

0 comments on commit 63875a7

Please sign in to comment.