From 3dfea1dc1b12ce243c41b124ea2387228517946f Mon Sep 17 00:00:00 2001 From: elsid Date: Fri, 28 Apr 2023 13:39:37 +0200 Subject: [PATCH] Make OutputTransformIterator more generic --- components/detournavigator/findsmoothpath.hpp | 17 ++++++++++++----- components/detournavigator/navigatorutils.hpp | 2 +- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/components/detournavigator/findsmoothpath.hpp b/components/detournavigator/findsmoothpath.hpp index f5d5b99e59..23dc842426 100644 --- a/components/detournavigator/findsmoothpath.hpp +++ b/components/detournavigator/findsmoothpath.hpp @@ -49,13 +49,13 @@ namespace DetourNavigator std::optional getSteerTarget(const dtNavMeshQuery& navQuery, const osg::Vec3f& startPos, const osg::Vec3f& endPos, const float minTargetDist, const dtPolyRef* path, const std::size_t pathSize); - template + template class OutputTransformIterator { public: - explicit OutputTransformIterator(OutputIterator& impl, const RecastSettings& settings) + explicit OutputTransformIterator(OutputIterator& impl, Function&& function) : mImpl(impl) - , mSettings(settings) + , mFunction(std::forward(function)) { } @@ -76,15 +76,22 @@ namespace DetourNavigator OutputTransformIterator& operator=(const osg::Vec3f& value) { - *mImpl.get() = fromNavMeshCoordinates(mSettings, value); + *mImpl.get() = mFunction(value); return *this; } private: std::reference_wrapper mImpl; - std::reference_wrapper mSettings; + Function mFunction; }; + template + auto withFromNavMeshCoordinates(OutputIterator& impl, const RecastSettings& settings) + { + return OutputTransformIterator( + impl, [&settings](const osg::Vec3f& value) { return fromNavMeshCoordinates(settings, value); }); + } + dtPolyRef findNearestPoly(const dtNavMeshQuery& query, const dtQueryFilter& filter, const osg::Vec3f& center, const osg::Vec3f& halfExtents); diff --git a/components/detournavigator/navigatorutils.hpp b/components/detournavigator/navigatorutils.hpp index 2e98930f18..8d2cec3729 100644 --- a/components/detournavigator/navigatorutils.hpp +++ b/components/detournavigator/navigatorutils.hpp @@ -36,7 +36,7 @@ namespace DetourNavigator if (navMesh == nullptr) return Status::NavMeshNotFound; const Settings& settings = navigator.getSettings(); - auto outTransform = OutputTransformIterator(out, settings.mRecast); + auto outTransform = withFromNavMeshCoordinates(out, settings.mRecast); const auto locked = navMesh->lock(); return findSmoothPath(locked->getQuery(), toNavMeshCoordinates(settings.mRecast, agentBounds.mHalfExtents), toNavMeshCoordinates(settings.mRecast, stepSize), toNavMeshCoordinates(settings.mRecast, start),