Skip to content

Commit

Permalink
Add GraphQuery
Browse files Browse the repository at this point in the history
  • Loading branch information
ducphamhong committed Dec 2, 2024
1 parent 78a38e3 commit d00a1f8
Show file tree
Hide file tree
Showing 7 changed files with 208 additions and 39 deletions.
5 changes: 4 additions & 1 deletion Projects/Skylicht/Collision/Collision/COctreeNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ namespace Skylicht
COctreeNode::~COctreeNode()
{
for (u32 i = 0; i != 8; ++i)
delete Childs[i];
{
if (Childs[i])
delete Childs[i];
}
}
}
85 changes: 85 additions & 0 deletions Projects/Skylicht/Graph/Graph/CGraphQuery.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#include "pch.h"
#include "CGraphQuery.h"

namespace Skylicht
{
namespace Graph
{
COctreeNode::COctreeNode() :
Parent(NULL)
{
for (u32 i = 0; i < 8; i++)
Childs[i] = NULL;
}

COctreeNode::~COctreeNode()
{
for (u32 i = 0; i != 8; ++i)
{
if (Childs[i])
delete Childs[i];
}
}


CGraphQuery::CGraphQuery() :
m_root(NULL)
{

}

CGraphQuery::~CGraphQuery()
{
release();
}

void CGraphQuery::release()
{
if (m_root)
{
delete m_root;
m_root = NULL;
}
}

void CGraphQuery::buildIndexNavMesh(CMesh* navMesh, CObstacleAvoidance* obstacle, float minCellSize)
{
release();

m_root = new COctreeNode();

// check used tile
IMeshBuffer* mb = navMesh->getMeshBuffer(0);
IVertexBuffer* vb = mb->getVertexBuffer();
IIndexBuffer* ib = mb->getIndexBuffer();

for (u32 i = 0, n = ib->getIndexCount(); i < n; i += 3)
{
u32 ixA = ib->getIndex(i);
u32 ixB = ib->getIndex(i + 1);
u32 ixC = ib->getIndex(i + 2);

S3DVertex* a = (S3DVertex*)vb->getVertex(ixA);
S3DVertex* b = (S3DVertex*)vb->getVertex(ixB);
S3DVertex* c = (S3DVertex*)vb->getVertex(ixC);

if (i == 0)
m_root->Box.reset(a->Pos);
else
{
m_root->Box.addInternalPoint(a->Pos);
m_root->Box.addInternalPoint(b->Pos);
m_root->Box.addInternalPoint(c->Pos);
}

core::triangle3df tri(a->Pos, b->Pos, c->Pos);
m_root->Triangles.push_back(tri);
}
}

void CGraphQuery::constructOctree(COctreeNode* node)
{

}
}
}
75 changes: 75 additions & 0 deletions Projects/Skylicht/Graph/Graph/CGraphQuery.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
!@
MIT License
Copyright (c) 2024 Skylicht Technology CO., LTD
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
This file is part of the "Skylicht Engine".
https://github.com/skylicht-lab/skylicht-engine
!#
*/

#pragma once

#include "RenderMesh/CRenderMeshData.h"
#include "ObstacleAvoidance/CObstacleAvoidance.h"

namespace Skylicht
{
namespace Graph
{
class COctreeNode
{
public:
core::aabbox3df Box;

COctreeNode* Parent;
COctreeNode* Childs[8];

core::array<core::triangle3df> Triangles;
CObstacleAvoidance Obstacle;

COctreeNode();

virtual ~COctreeNode();
};

class CGraphQuery
{
protected:
COctreeNode* m_root;

public:
CGraphQuery();

virtual ~CGraphQuery();

void release();

void buildIndexNavMesh(CMesh* navMesh, CObstacleAvoidance* obstacle, float minCellSize = 1.0f);

inline COctreeNode* getRootNode()
{
return m_root;
}

protected:

void constructOctree(COctreeNode* node);

};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,25 @@ This file is part of the "Skylicht Engine".
*/

#include "pch.h"
#include "CWalkingMap.h"
#include "CWalkingTileMap.h"

namespace Skylicht
{
namespace Graph
{
CWalkingMap::CWalkingMap() :
CWalkingTileMap::CWalkingTileMap() :
m_tileWidth(0.0f),
m_tileHeight(0.0f)
{

}

CWalkingMap::~CWalkingMap()
CWalkingTileMap::~CWalkingTileMap()
{
release();
}

void CWalkingMap::generate(float tileWidth, float tileHeight, const core::aabbox3df& bbox)
void CWalkingTileMap::generate(float tileWidth, float tileHeight, const core::aabbox3df& bbox)
{
release();

Expand Down Expand Up @@ -74,12 +74,12 @@ namespace Skylicht
}
}

void CWalkingMap::generate(float tileWidth, float tileHeight, CMesh* recastMesh, CObstacleAvoidance* obstacle)
void CWalkingTileMap::generate(float tileWidth, float tileHeight, CMesh* navMesh, CObstacleAvoidance* obstacle)
{
generate(tileWidth, tileHeight, recastMesh->getBoundingBox());
generate(tileWidth, tileHeight, navMesh->getBoundingBox());

// check used tile
IMeshBuffer* mb = recastMesh->getMeshBuffer(0);
IMeshBuffer* mb = navMesh->getMeshBuffer(0);
IVertexBuffer* vb = mb->getVertexBuffer();
IIndexBuffer* ib = mb->getIndexBuffer();

Expand Down Expand Up @@ -171,8 +171,6 @@ namespace Skylicht

STileXYZ tile(t->X, t->Y, t->Z);
m_hashTiles[tile] = t;

obstacle->copySegments(&t->Obstacle, t->BBox);
}

// link Neighbour
Expand All @@ -195,8 +193,7 @@ namespace Skylicht
t->Z + z);
if (nei)
{
if (!t->Obstacle.isLineHit(t->Position, nei->Position, tileHeight) &&
!nei->Obstacle.isLineHit(t->Position, nei->Position, tileHeight))
if (!obstacle->isLineHit(t->Position, nei->Position, tileHeight))
{
t->Neighbours.push_back(nei);
}
Expand All @@ -207,7 +204,7 @@ namespace Skylicht
}
}

STile* CWalkingMap::getTile(int x, int y, int z)
STile* CWalkingTileMap::getTile(int x, int y, int z)
{
STileXYZ tile(x, y, z);
auto it = m_hashTiles.find(tile);
Expand All @@ -216,7 +213,7 @@ namespace Skylicht
return it->second;
}

bool CWalkingMap::hitTris(const core::line3df& line, core::array<core::triangle3df>& tris, core::vector3df& outPoint)
bool CWalkingTileMap::hitTris(const core::line3df& line, core::array<core::triangle3df>& tris, core::vector3df& outPoint)
{
bool hit = false;
for (int i = 0, n = tris.size(); i < n; i++)
Expand All @@ -231,7 +228,7 @@ namespace Skylicht
return hit;
}

void CWalkingMap::release()
void CWalkingTileMap::release()
{
for (u32 i = 0, n = m_tiles.size(); i < n; i++)
{
Expand All @@ -241,7 +238,7 @@ namespace Skylicht
m_hashTiles.clear();
}

void CWalkingMap::resetVisit()
void CWalkingTileMap::resetVisit()
{
for (u32 i = 0, n = m_tiles.size(); i < n; i++)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ namespace Skylicht
int Z;
core::vector3df Position;
core::aabbox3df BBox;
CObstacleAvoidance Obstacle;
core::array<core::triangle3df> Tris;
core::array<STile*> Neighbours;
bool Visit;
Expand Down Expand Up @@ -92,7 +91,7 @@ namespace Skylicht

typedef std::map<STileXYZ, STile*, CompareTile> TileValueMap;

class CWalkingMap
class CWalkingTileMap
{
protected:
core::array<STile*> m_tiles;
Expand All @@ -103,11 +102,11 @@ namespace Skylicht
float m_tileHeight;

public:
CWalkingMap();
CWalkingTileMap();

virtual ~CWalkingMap();
virtual ~CWalkingTileMap();

void generate(float tileWidth, float tileHeight, CMesh* recastMesh, CObstacleAvoidance* obstacle);
void generate(float tileWidth, float tileHeight, CMesh* navMesh, CObstacleAvoidance* obstacle);

void release();

Expand Down
Loading

0 comments on commit d00a1f8

Please sign in to comment.