mirror of
https://github.com/orange-cpp/omath.git
synced 2026-02-13 07:03:25 +00:00
added files, added hash function
This commit is contained in:
@@ -4,6 +4,9 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
|
||||
namespace omath
|
||||
{
|
||||
class Vector3 {
|
||||
@@ -73,3 +76,22 @@ namespace omath
|
||||
Vector3 Normalized() const;
|
||||
};
|
||||
}
|
||||
// ReSharper disable once CppRedundantNamespaceDefinition
|
||||
namespace std
|
||||
{
|
||||
template<>
|
||||
struct hash<omath::Vector3>
|
||||
{
|
||||
std::size_t operator()(const omath::Vector3& vec) const noexcept
|
||||
{
|
||||
std::size_t hash = 0;
|
||||
constexpr std::hash<float> hasher;
|
||||
|
||||
hash ^= hasher(vec.x) + 0x9e3779b9 + (hash<<6) + (hash>>2);
|
||||
hash ^= hasher(vec.y) + 0x9e3779b9 + (hash<<6) + (hash>>2);
|
||||
hash ^= hasher(vec.z) + 0x9e3779b9 + (hash<<6) + (hash>>2);
|
||||
|
||||
return hash;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
20
include/omath/pathfinding/Astar.h
Normal file
20
include/omath/pathfinding/Astar.h
Normal file
@@ -0,0 +1,20 @@
|
||||
//
|
||||
// Created by Vlad on 28.07.2024.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
#include <vector>
|
||||
#include "NavigationMesh.h"
|
||||
#include "omath/Vector3.h"
|
||||
|
||||
|
||||
namespace omath::pathfinding
|
||||
{
|
||||
class Astar
|
||||
{
|
||||
public:
|
||||
[[nodiscard]]
|
||||
static std::vector<Vector3> FindPath(const Vector3& start, const Vector3& end, const NavigationMesh& navMesh);
|
||||
|
||||
};
|
||||
}
|
||||
33
include/omath/pathfinding/NavigationMesh.h
Normal file
33
include/omath/pathfinding/NavigationMesh.h
Normal file
@@ -0,0 +1,33 @@
|
||||
//
|
||||
// Created by Vlad on 28.07.2024.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "omath/Vector3.h"
|
||||
#include <expected>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
|
||||
namespace omath::pathfinding
|
||||
{
|
||||
struct NavigationVertex
|
||||
{
|
||||
Vector3 origin;
|
||||
std::vector<Vector3*> connections;
|
||||
};
|
||||
|
||||
|
||||
class NavigationMesh final
|
||||
{
|
||||
public:
|
||||
|
||||
[[nodiscard]]
|
||||
std::expected<const NavigationVertex, std::string> GetClossestVertex(const Vector3& point) const;
|
||||
|
||||
private:
|
||||
|
||||
|
||||
};
|
||||
}
|
||||
@@ -5,3 +5,4 @@ target_sources(omath PRIVATE
|
||||
Vector4.cpp)
|
||||
|
||||
add_subdirectory(prediction)
|
||||
add_subdirectory(pathfinding)
|
||||
36
source/pathfinding/Astar.cpp
Normal file
36
source/pathfinding/Astar.cpp
Normal file
@@ -0,0 +1,36 @@
|
||||
//
|
||||
// Created by Vlad on 28.07.2024.
|
||||
//
|
||||
#include "omath/pathfinding/Astar.h"
|
||||
#include <unordered_map>
|
||||
|
||||
|
||||
|
||||
namespace omath::pathfinding
|
||||
{
|
||||
struct PathNode final
|
||||
{
|
||||
PathNode* cameFrom;
|
||||
const NavigationVertex* navVertex;
|
||||
float gCost = 0.f;
|
||||
};
|
||||
|
||||
|
||||
std::vector<Vector3> Astar::FindPath(const Vector3 &start, const Vector3 &end, const NavigationMesh &navMesh)
|
||||
{
|
||||
std::unordered_map<Vector3, PathNode> closedList;
|
||||
std::unordered_map<Vector3, PathNode> openList;
|
||||
|
||||
const auto& startVertex = navMesh.GetClossestVertex(start).value();
|
||||
const auto& endVertex = navMesh.GetClossestVertex(end).value();
|
||||
|
||||
openList.emplace(startVertex.origin, PathNode{nullptr, &startVertex, 0.f});
|
||||
|
||||
while (!openList.empty())
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
}
|
||||
1
source/pathfinding/CMakeLists.txt
Normal file
1
source/pathfinding/CMakeLists.txt
Normal file
@@ -0,0 +1 @@
|
||||
target_sources(omath PRIVATE NavigationMesh.cpp Astar.cpp)
|
||||
4
source/pathfinding/NavigationMesh.cpp
Normal file
4
source/pathfinding/NavigationMesh.cpp
Normal file
@@ -0,0 +1,4 @@
|
||||
//
|
||||
// Created by Vlad on 28.07.2024.
|
||||
//
|
||||
#include "omath/pathfinding/NavigationMesh.h"
|
||||
Reference in New Issue
Block a user