2021-03-23 22:15:13 +00:00
|
|
|
|
#ifndef OPENMW_COMPONENTS_DETOURNAVIGATOR_SETTINGSUTILS_H
|
2018-03-13 22:49:08 +00:00
|
|
|
|
#define OPENMW_COMPONENTS_DETOURNAVIGATOR_SETTINGSUTILS_H
|
|
|
|
|
|
|
|
|
|
#include "settings.hpp"
|
2018-04-15 22:07:18 +00:00
|
|
|
|
#include "tilebounds.hpp"
|
2018-03-13 22:49:08 +00:00
|
|
|
|
#include "tileposition.hpp"
|
2018-07-20 19:11:34 +00:00
|
|
|
|
|
2018-04-20 23:57:01 +00:00
|
|
|
|
#include <osg/Vec2f>
|
2018-03-13 22:49:08 +00:00
|
|
|
|
#include <osg/Vec3f>
|
|
|
|
|
|
2021-06-27 00:15:45 +00:00
|
|
|
|
#include <algorithm>
|
|
|
|
|
#include <cmath>
|
2018-03-13 22:49:08 +00:00
|
|
|
|
|
|
|
|
|
namespace DetourNavigator
|
|
|
|
|
{
|
2021-11-06 12:46:43 +00:00
|
|
|
|
inline float toNavMeshCoordinates(const RecastSettings& settings, float value)
|
2019-03-08 16:53:37 +00:00
|
|
|
|
{
|
|
|
|
|
return value * settings.mRecastScaleFactor;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-06 12:46:43 +00:00
|
|
|
|
inline osg::Vec2f toNavMeshCoordinates(const RecastSettings& settings, osg::Vec2f position)
|
2021-07-15 22:09:05 +00:00
|
|
|
|
{
|
|
|
|
|
return position * settings.mRecastScaleFactor;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-06 12:46:43 +00:00
|
|
|
|
inline osg::Vec3f toNavMeshCoordinates(const RecastSettings& settings, osg::Vec3f position)
|
2018-03-13 22:49:08 +00:00
|
|
|
|
{
|
|
|
|
|
std::swap(position.y(), position.z());
|
|
|
|
|
return position * settings.mRecastScaleFactor;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-06 12:46:43 +00:00
|
|
|
|
inline TileBounds toNavMeshCoordinates(const RecastSettings& settings, const TileBounds& value)
|
|
|
|
|
{
|
|
|
|
|
return TileBounds{ toNavMeshCoordinates(settings, value.mMin), toNavMeshCoordinates(settings, value.mMax) };
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-01 00:21:47 +00:00
|
|
|
|
inline float fromNavMeshCoordinates(const RecastSettings& settings, float value)
|
|
|
|
|
{
|
|
|
|
|
return value / settings.mRecastScaleFactor;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-06 12:46:43 +00:00
|
|
|
|
inline osg::Vec3f fromNavMeshCoordinates(const RecastSettings& settings, osg::Vec3f position)
|
2018-03-13 22:49:08 +00:00
|
|
|
|
{
|
|
|
|
|
const auto factor = 1.0f / settings.mRecastScaleFactor;
|
|
|
|
|
position *= factor;
|
|
|
|
|
std::swap(position.y(), position.z());
|
|
|
|
|
return position;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-06 12:46:43 +00:00
|
|
|
|
inline float getTileSize(const RecastSettings& settings)
|
2018-03-13 22:49:08 +00:00
|
|
|
|
{
|
2021-02-14 23:07:20 +00:00
|
|
|
|
return static_cast<float>(settings.mTileSize) * settings.mCellSize;
|
2018-03-13 22:49:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-31 23:24:56 +00:00
|
|
|
|
inline int getTilePosition(const RecastSettings& settings, float position)
|
|
|
|
|
{
|
|
|
|
|
const float v = std::floor(position / getTileSize(settings));
|
|
|
|
|
if (v < static_cast<float>(std::numeric_limits<int>::min()))
|
|
|
|
|
return std::numeric_limits<int>::min();
|
|
|
|
|
if (v > static_cast<float>(std::numeric_limits<int>::max() - 1))
|
|
|
|
|
return std::numeric_limits<int>::max() - 1;
|
|
|
|
|
return static_cast<int>(v);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline TilePosition getTilePosition(const RecastSettings& settings, const osg::Vec2f& position)
|
|
|
|
|
{
|
|
|
|
|
return TilePosition(getTilePosition(settings, position.x()), getTilePosition(settings, position.y()));
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-06 12:46:43 +00:00
|
|
|
|
inline TilePosition getTilePosition(const RecastSettings& settings, const osg::Vec3f& position)
|
2018-03-13 22:49:08 +00:00
|
|
|
|
{
|
2022-01-31 23:24:56 +00:00
|
|
|
|
return getTilePosition(settings, osg::Vec2f(position.x(), position.z()));
|
2018-03-13 22:49:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-06 12:46:43 +00:00
|
|
|
|
inline TileBounds makeTileBounds(const RecastSettings& settings, const TilePosition& tilePosition)
|
2018-03-13 22:49:08 +00:00
|
|
|
|
{
|
|
|
|
|
return TileBounds{
|
|
|
|
|
osg::Vec2f(tilePosition.x(), tilePosition.y()) * getTileSize(settings),
|
|
|
|
|
osg::Vec2f(tilePosition.x() + 1, tilePosition.y() + 1) * getTileSize(settings),
|
|
|
|
|
};
|
|
|
|
|
}
|
2018-04-15 19:54:45 +00:00
|
|
|
|
|
2021-11-06 12:46:43 +00:00
|
|
|
|
inline float getBorderSize(const RecastSettings& settings)
|
2018-04-15 19:54:45 +00:00
|
|
|
|
{
|
2021-02-14 23:07:20 +00:00
|
|
|
|
return static_cast<float>(settings.mBorderSize) * settings.mCellSize;
|
2018-04-15 19:54:45 +00:00
|
|
|
|
}
|
2018-07-20 19:11:34 +00:00
|
|
|
|
|
2021-11-06 12:46:43 +00:00
|
|
|
|
inline float getRealTileSize(const RecastSettings& settings)
|
2021-03-23 22:15:13 +00:00
|
|
|
|
{
|
|
|
|
|
return settings.mTileSize * settings.mCellSize / settings.mRecastScaleFactor;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline float getMaxNavmeshAreaRadius(const Settings& settings)
|
|
|
|
|
{
|
|
|
|
|
return std::floor(std::sqrt(settings.mMaxTilesNumber / osg::PI)) - 1;
|
|
|
|
|
}
|
2021-11-05 19:47:11 +00:00
|
|
|
|
|
2021-11-06 12:46:43 +00:00
|
|
|
|
inline TileBounds makeRealTileBoundsWithBorder(const RecastSettings& settings, const TilePosition& tilePosition)
|
2021-11-05 19:47:11 +00:00
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|
2018-03-13 22:49:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|