diff --git a/components/detournavigator/cachedrecastmeshmanager.cpp b/components/detournavigator/cachedrecastmeshmanager.cpp index d82310dc38..2e1ba45959 100644 --- a/components/detournavigator/cachedrecastmeshmanager.cpp +++ b/components/detournavigator/cachedrecastmeshmanager.cpp @@ -3,9 +3,8 @@ namespace DetourNavigator { - CachedRecastMeshManager::CachedRecastMeshManager(const Settings& settings, const TileBounds& bounds, - std::size_t generation) - : mImpl(settings, bounds, generation) + CachedRecastMeshManager::CachedRecastMeshManager(const TileBounds& bounds, std::size_t generation) + : mImpl(bounds, generation) {} bool CachedRecastMeshManager::addObject(const ObjectId id, const CollisionShape& shape, diff --git a/components/detournavigator/cachedrecastmeshmanager.hpp b/components/detournavigator/cachedrecastmeshmanager.hpp index b1e2570b0a..740e0b73f5 100644 --- a/components/detournavigator/cachedrecastmeshmanager.hpp +++ b/components/detournavigator/cachedrecastmeshmanager.hpp @@ -14,7 +14,7 @@ namespace DetourNavigator class CachedRecastMeshManager { public: - CachedRecastMeshManager(const Settings& settings, const TileBounds& bounds, std::size_t generation); + explicit CachedRecastMeshManager(const TileBounds& bounds, std::size_t generation); bool addObject(const ObjectId id, const CollisionShape& shape, const btTransform& transform, const AreaType areaType); diff --git a/components/detournavigator/recastmeshmanager.cpp b/components/detournavigator/recastmeshmanager.cpp index 2eeb90a9b5..6dcc2b2a42 100644 --- a/components/detournavigator/recastmeshmanager.cpp +++ b/components/detournavigator/recastmeshmanager.cpp @@ -28,9 +28,8 @@ namespace namespace DetourNavigator { - RecastMeshManager::RecastMeshManager(const Settings& settings, const TileBounds& bounds, std::size_t generation) - : mSettings(settings) - , mGeneration(generation) + RecastMeshManager::RecastMeshManager(const TileBounds& bounds, std::size_t generation) + : mGeneration(generation) , mTileBounds(bounds) { } @@ -119,10 +118,7 @@ namespace DetourNavigator std::shared_ptr RecastMeshManager::getMesh() const { - TileBounds tileBounds = mTileBounds; - tileBounds.mMin /= mSettings.mRecastScaleFactor; - tileBounds.mMax /= mSettings.mRecastScaleFactor; - RecastMeshBuilder builder(tileBounds); + RecastMeshBuilder builder(mTileBounds); using Object = std::tuple< osg::ref_ptr, std::reference_wrapper, diff --git a/components/detournavigator/recastmeshmanager.hpp b/components/detournavigator/recastmeshmanager.hpp index e1c1567cb3..33aba97bbf 100644 --- a/components/detournavigator/recastmeshmanager.hpp +++ b/components/detournavigator/recastmeshmanager.hpp @@ -34,7 +34,7 @@ namespace DetourNavigator class RecastMeshManager { public: - RecastMeshManager(const Settings& settings, const TileBounds& bounds, std::size_t generation); + explicit RecastMeshManager(const TileBounds& bounds, std::size_t generation); bool addObject(const ObjectId id, const CollisionShape& shape, const btTransform& transform, const AreaType areaType); @@ -73,7 +73,6 @@ namespace DetourNavigator HeightfieldShape mShape; }; - const Settings& mSettings; const std::size_t mGeneration; const TileBounds mTileBounds; mutable std::mutex mMutex; diff --git a/components/detournavigator/settingsutils.hpp b/components/detournavigator/settingsutils.hpp index 258eb64c65..6f15faaa3e 100644 --- a/components/detournavigator/settingsutils.hpp +++ b/components/detournavigator/settingsutils.hpp @@ -96,6 +96,17 @@ namespace DetourNavigator { return std::floor(std::sqrt(settings.mMaxTilesNumber / osg::PI)) - 1; } + + inline TileBounds makeRealTileBoundsWithBorder(const Settings& settings, const TilePosition& tilePosition) + { + TileBounds result = makeTileBounds(settings, tilePosition); + const float border = getBorderSize(settings); + result.mMin -= osg::Vec2f(border, border); + result.mMax += osg::Vec2f(border, border); + result.mMin /= settings.mRecastScaleFactor; + result.mMax /= settings.mRecastScaleFactor; + return result; + } } #endif diff --git a/components/detournavigator/tilecachedrecastmeshmanager.cpp b/components/detournavigator/tilecachedrecastmeshmanager.cpp index d6e3e55005..f82ce9de83 100644 --- a/components/detournavigator/tilecachedrecastmeshmanager.cpp +++ b/components/detournavigator/tilecachedrecastmeshmanager.cpp @@ -18,12 +18,11 @@ namespace DetourNavigator const btTransform& transform, const AreaType areaType) { std::vector tilesPositions; - const auto border = getBorderSize(mSettings); { auto tiles = mTiles.lock(); getTilesPositions(shape.getShape(), transform, mSettings, [&] (const TilePosition& tilePosition) { - if (addTile(id, shape, transform, areaType, tilePosition, border, tiles.get())) + if (addTile(id, shape, transform, areaType, tilePosition, tiles.get())) tilesPositions.push_back(tilePosition); }); } @@ -58,8 +57,6 @@ namespace DetourNavigator bool TileCachedRecastMeshManager::addWater(const osg::Vec2i& cellPosition, const int cellSize, const osg::Vec3f& shift) { - const auto border = getBorderSize(mSettings); - auto& tilesPositions = mWaterTilesPositions[cellPosition]; bool result = false; @@ -84,11 +81,9 @@ namespace DetourNavigator auto tile = tiles->find(tilePosition); if (tile == tiles->end()) { - auto tileBounds = makeTileBounds(mSettings, tilePosition); - tileBounds.mMin -= osg::Vec2f(border, border); - tileBounds.mMax += osg::Vec2f(border, border); - tile = tiles->insert(std::make_pair(tilePosition, - std::make_shared(mSettings, tileBounds, mTilesGeneration))).first; + const TileBounds tileBounds = makeRealTileBoundsWithBorder(mSettings, tilePosition); + tile = tiles->emplace(tilePosition, + std::make_shared(tileBounds, mTilesGeneration)).first; } if (tile->second->addWater(cellPosition, cellSize, shift)) { @@ -133,8 +128,6 @@ namespace DetourNavigator bool TileCachedRecastMeshManager::addHeightfield(const osg::Vec2i& cellPosition, int cellSize, const osg::Vec3f& shift, const HeightfieldShape& shape) { - const auto border = getBorderSize(mSettings); - auto& tilesPositions = mHeightfieldTilesPositions[cellPosition]; bool result = false; @@ -145,11 +138,9 @@ namespace DetourNavigator auto tile = tiles->find(tilePosition); if (tile == tiles->end()) { - auto tileBounds = makeTileBounds(mSettings, tilePosition); - tileBounds.mMin -= osg::Vec2f(border, border); - tileBounds.mMax += osg::Vec2f(border, border); - tile = tiles->insert(std::make_pair(tilePosition, - std::make_shared(mSettings, tileBounds, mTilesGeneration))).first; + const TileBounds tileBounds = makeRealTileBoundsWithBorder(mSettings, tilePosition); + tile = tiles->emplace(tilePosition, + std::make_shared(tileBounds, mTilesGeneration)).first; } if (tile->second->addHeightfield(cellPosition, cellSize, shift, shape)) { @@ -219,17 +210,15 @@ namespace DetourNavigator } bool TileCachedRecastMeshManager::addTile(const ObjectId id, const CollisionShape& shape, - const btTransform& transform, const AreaType areaType, const TilePosition& tilePosition, float border, + const btTransform& transform, const AreaType areaType, const TilePosition& tilePosition, TilesMap& tiles) { auto tile = tiles.find(tilePosition); if (tile == tiles.end()) { - auto tileBounds = makeTileBounds(mSettings, tilePosition); - tileBounds.mMin -= osg::Vec2f(border, border); - tileBounds.mMax += osg::Vec2f(border, border); - tile = tiles.insert(std::make_pair( - tilePosition, std::make_shared(mSettings, tileBounds, mTilesGeneration))).first; + const TileBounds tileBounds = makeRealTileBoundsWithBorder(mSettings, tilePosition); + tile = tiles.emplace(tilePosition, + std::make_shared(tileBounds, mTilesGeneration)).first; } return tile->second->addObject(id, shape, transform, areaType); } diff --git a/components/detournavigator/tilecachedrecastmeshmanager.hpp b/components/detournavigator/tilecachedrecastmeshmanager.hpp index 7b5480e006..6b930c411f 100644 --- a/components/detournavigator/tilecachedrecastmeshmanager.hpp +++ b/components/detournavigator/tilecachedrecastmeshmanager.hpp @@ -33,7 +33,6 @@ namespace DetourNavigator if (object == mObjectsTilesPositions.end()) return false; auto& currentTiles = object->second; - const auto border = getBorderSize(mSettings); bool changed = false; std::vector newTiles; { @@ -49,7 +48,7 @@ namespace DetourNavigator changed = true; } } - else if (addTile(id, shape, transform, areaType, tilePosition, border, tiles.get())) + else if (addTile(id, shape, transform, areaType, tilePosition, tiles.get())) { newTiles.push_back(tilePosition); onChangedTile(tilePosition); @@ -113,7 +112,7 @@ namespace DetourNavigator std::size_t mTilesGeneration = 0; bool addTile(const ObjectId id, const CollisionShape& shape, const btTransform& transform, - const AreaType areaType, const TilePosition& tilePosition, float border, TilesMap& tiles); + const AreaType areaType, const TilePosition& tilePosition, TilesMap& tiles); bool updateTile(const ObjectId id, const btTransform& transform, const AreaType areaType, const TilePosition& tilePosition, TilesMap& tiles);